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