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