2dc96483426b360fe8c051e7776c13cf0530f35f
[slock.git] / slock.c
1 /* © 2006-2008 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 <stdarg.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 #include <X11/extensions/dpms.h>
20
21 #if HAVE_BSD_AUTH
22 #include <login_cap.h>
23 #include <bsd_auth.h>
24 #endif
25
26 void
27 eprint(const char *errstr, ...) {
28         va_list ap;
29
30         va_start(ap, errstr);
31         vfprintf(stderr, errstr, ap);
32         va_end(ap);
33         exit(EXIT_FAILURE);
34 }
35
36 #ifndef HAVE_BSD_AUTH
37 const char *
38 get_password() { /* only run as root */
39         const char *rval;
40         struct passwd *pw;
41
42         if(geteuid() != 0)
43                 eprint("slock: cannot retrieve password entry (make sure to suid slock)\n");
44         pw = getpwuid(getuid());
45         endpwent();
46         rval =  pw->pw_passwd;
47
48 #if HAVE_SHADOW_H
49         {
50                 struct spwd *sp;
51                 sp = getspnam(getenv("USER"));
52                 endspent();
53                 rval = sp->sp_pwdp;
54         }
55 #endif
56
57         /* drop privileges */
58         if(setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0)
59                 eprint("slock: cannot drop privileges\n");
60         return rval;
61 }
62 #endif
63
64 int
65 main(int argc, char **argv) {
66         char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
67         char buf[32], passwd[256];
68         int num, screen;
69
70 #ifndef HAVE_BSD_AUTH
71         const char *pws;
72 #endif
73         unsigned int len;
74         Bool running = True;
75         Cursor invisible;
76         Display *dpy;
77         KeySym ksym;
78         Pixmap pmap;
79         Window root, w;
80         XColor black, dummy;
81         XEvent ev;
82         XSetWindowAttributes wa;
83
84         if((argc == 2) && !strcmp("-v", argv[1]))
85                 eprint("slock-"VERSION", © 2006-2008 Anselm R Garbe\n");
86         else if(argc != 1)
87                 eprint("usage: slock [-v]\n");
88
89 #ifndef HAVE_BSD_AUTH
90         pws = get_password();
91 #endif
92
93         if(!(dpy = XOpenDisplay(0)))
94                 eprint("slock: cannot open display\n");
95         screen = DefaultScreen(dpy);
96         root = RootWindow(dpy, screen);
97
98         /* init */
99         wa.override_redirect = 1;
100         wa.background_pixel = BlackPixel(dpy, screen);
101         w = XCreateWindow(dpy, root, 0, 0, DisplayWidth(dpy, screen), DisplayHeight(dpy, screen),
102                         0, DefaultDepth(dpy, screen), CopyFromParent,
103                         DefaultVisual(dpy, screen), CWOverrideRedirect | CWBackPixel, &wa);
104         XAllocNamedColor(dpy, DefaultColormap(dpy, screen), "black", &black, &dummy);
105         pmap = XCreateBitmapFromData(dpy, w, curs, 8, 8);
106         invisible = XCreatePixmapCursor(dpy, pmap, pmap, &black, &black, 0, 0);
107         XDefineCursor(dpy, w, invisible);
108         XMapRaised(dpy, w);
109         for(len = 1000; len; len--) {
110                 if(XGrabPointer(dpy, root, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
111                         GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
112                         break;
113                 usleep(1000);
114         }
115         if((running = running && (len > 0))) {
116                 for(len = 1000; len; len--) {
117                         if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
118                                 == GrabSuccess)
119                                 break;
120                         usleep(1000);
121                 }
122                 running = (len > 0);
123         }
124         len = 0;
125         XSync(dpy, False);
126
127         /* main event loop */
128         while(running && !XNextEvent(dpy, &ev)) {
129                 if(len == 0) 
130                         DPMSForceLevel(dpy, DPMSModeOff);
131                 if(ev.type == KeyPress) {
132                         buf[0] = 0;
133                         num = XLookupString(&ev.xkey, buf, sizeof buf, &ksym, 0);
134                         if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
135                                         || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
136                                         || IsPrivateKeypadKey(ksym))
137                                 continue;
138                         switch(ksym) {
139                         case XK_Return:
140                                 passwd[len] = 0;
141
142 #ifdef HAVE_BSD_AUTH
143                                 running = !auth_userokay(getlogin(), NULL, "auth-xlock", passwd);
144 #else
145                                 running = strcmp(crypt(passwd, pws), pws);
146 #endif
147
148                                 if (running != 0)
149                                         XBell(dpy, 100);
150                                 len = 0;
151                                 break;
152                         case XK_Escape:
153                                 len = 0;
154                                 break;
155                         case XK_BackSpace:
156                                 if(len)
157                                         --len;
158                                 break;
159                         default:
160                                 if(num && !iscntrl((int) buf[0]) && (len + num < sizeof passwd)) { 
161                                         memcpy(passwd + len, buf, num);
162                                         len += num;
163                                 }
164                                 break;
165                         }
166                 }
167         }
168         XUngrabPointer(dpy, CurrentTime);
169         XFreePixmap(dpy, pmap);
170         XDestroyWindow(dpy, w);
171         XCloseDisplay(dpy);
172         return 0;
173 }