866df90d8a99815d9487150af6dc1adfc3ca6401
[slock.git] / slock.c
1 /* © 2006-2007 Anselm R. Garbe <garbeam at gmail dot com>
2  * See LICENSE file for license details. */
3 #define _XOPEN_SOURCE 500
4 #if HAVE_SHADOW_H
5 #include <shadow.h>
6 #endif
7
8 #include <ctype.h>
9 #include <pwd.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <sys/types.h>
15 #include <X11/keysym.h>
16 #include <X11/Xlib.h>
17 #include <X11/Xutil.h>
18
19 const char *
20 get_password() { /* only run as root */
21         const char *rval;
22         struct passwd *pw;
23
24         if(geteuid() != 0) {
25                 fputs("slock: cannot retrieve password entry (make sure to suid slock)\n", stderr);
26                 exit(EXIT_FAILURE);
27         }
28         pw = getpwuid(getuid());
29         endpwent();
30         rval =  pw->pw_passwd;
31
32 #if HAVE_SHADOW_H
33         {
34                 struct spwd *sp;
35                 sp = getspnam(getenv("USER"));
36                 endspent();
37                 rval = sp->sp_pwdp;
38         }
39 #endif
40         /* drop privileges */
41         if(setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0) {
42                 fputs("slock: cannot drop privileges\n",stdout);
43                 exit(EXIT_FAILURE);
44         }
45         return rval;
46 }
47
48 int
49 main(int argc, char **argv) {
50         char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
51         char buf[32], passwd[256];
52         int num, screen;
53         const char *pws;
54         unsigned int len;
55         Bool running = True;
56         Cursor invisible;
57         Display *dpy;
58         KeySym ksym;
59         Pixmap pmap;
60         Window root, w;
61         XColor black, dummy;
62         XEvent ev;
63         XSetWindowAttributes wa;
64
65         if((argc > 1) && !strncmp(argv[1], "-v", 3)) {
66                 fputs("slock-"VERSION", © 2006-2007 Anselm R. Garbe\n", stdout);
67                 exit(EXIT_SUCCESS);
68         }
69         pws = get_password();
70         if(!(dpy = XOpenDisplay(0))) {
71                 fputs("slock: cannot open display\n", stderr);
72                 exit(EXIT_FAILURE);
73         }
74         screen = DefaultScreen(dpy);
75         root = RootWindow(dpy, screen);
76
77         /* init */
78         wa.override_redirect = 1;
79         wa.background_pixel = BlackPixel(dpy, screen);
80         w = XCreateWindow(dpy, root, 0, 0, DisplayWidth(dpy, screen), DisplayHeight(dpy, screen),
81                         0, DefaultDepth(dpy, screen), CopyFromParent,
82                         DefaultVisual(dpy, screen), CWOverrideRedirect | CWBackPixel, &wa);
83         XAllocNamedColor(dpy, DefaultColormap(dpy, screen), "black", &black, &dummy);
84         pmap = XCreateBitmapFromData(dpy, w, curs, 8, 8);
85         invisible = XCreatePixmapCursor(dpy, pmap, pmap, &black, &black, 0, 0);
86         XDefineCursor(dpy, w, invisible);
87         XMapRaised(dpy, w);
88         for(len = 1000; len; len--) {
89                 if(XGrabPointer(dpy, root, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
90                         GrabModeAsync, GrabModeSync, None, invisible, CurrentTime) == GrabSuccess)
91                         break;
92                 usleep(1000);
93         }
94         if((running = running && (len > 0))) {
95                 for(len = 1000; len; len--) {
96                         if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
97                                 == GrabSuccess)
98                                 break;
99                         usleep(1000);
100                 }
101                 running = (len > 0);
102         }
103         len = 0;
104         XSync(dpy, False);
105
106         /* main event loop */
107         while(running && !XNextEvent(dpy, &ev))
108                 if(ev.type == KeyPress) {
109                         buf[0] = 0;
110                         num = XLookupString(&ev.xkey, buf, sizeof buf, &ksym, 0);
111                         if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
112                                         || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
113                                         || IsPrivateKeypadKey(ksym))
114                                 continue;
115                         switch(ksym) {
116                         case XK_Return:
117                                 passwd[len] = 0;
118                                 if((running = strcmp(crypt(passwd, pws), pws)) != 0)
119                                         XBell(dpy, 100);
120                                 len = 0;
121                                 break;
122                         case XK_Escape:
123                                 len = 0;
124                                 break;
125                         case XK_BackSpace:
126                                 if(len)
127                                         --len;
128                                 break;
129                         default:
130                                 if(num && !iscntrl((int) buf[0]) && (len + num < sizeof passwd)) { 
131                                         memcpy(passwd + len, buf, num);
132                                         len += num;
133                                 }
134                                 break;
135                         }
136                 }
137         XUngrabPointer(dpy, CurrentTime);
138         XFreePixmap(dpy, pmap);
139         XDestroyWindow(dpy, w);
140         XCloseDisplay(dpy);
141         return 0;
142 }