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