]> git.armaanb.net Git - slock.git/blob - slock.c
rework setting window color
[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 <errno.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/extensions/Xrandr.h>
17 #include <X11/keysym.h>
18 #include <X11/Xlib.h>
19 #include <X11/Xutil.h>
20
21 #if HAVE_BSD_AUTH
22 #include <login_cap.h>
23 #include <bsd_auth.h>
24 #endif
25
26 enum {
27         INIT,
28         INPUT,
29         FAILED,
30         NUMCOLS
31 };
32
33 #include "config.h"
34
35 typedef struct {
36         int screen;
37         Window root, win;
38         Pixmap pmap;
39         unsigned long colors[NUMCOLS];
40 } Lock;
41
42 static Lock **locks;
43 static int nscreens;
44 static Bool running = True;
45 static Bool failure = False;
46 static Bool rr;
47 static int rrevbase;
48 static int rrerrbase;
49
50 static void
51 die(const char *errstr, ...)
52 {
53         va_list ap;
54
55         va_start(ap, errstr);
56         vfprintf(stderr, errstr, ap);
57         va_end(ap);
58         exit(1);
59 }
60
61 #ifdef __linux__
62 #include <fcntl.h>
63
64 static void
65 dontkillme(void)
66 {
67         int fd;
68
69         fd = open("/proc/self/oom_score_adj", O_WRONLY);
70         if (fd < 0 && errno == ENOENT)
71                 return;
72         if (fd < 0 || write(fd, "-1000\n", 6) != 6 || close(fd) != 0)
73                 die("cannot disable the out-of-memory killer for this process\n");
74 }
75 #endif
76
77 #ifndef HAVE_BSD_AUTH
78 /* only run as root */
79 static const char *
80 getpw(void)
81 {
82         const char *rval;
83         struct passwd *pw;
84
85         errno = 0;
86         pw = getpwuid(getuid());
87         if (!pw) {
88                 if (errno)
89                         die("slock: getpwuid: %s\n", strerror(errno));
90                 else
91                         die("slock: cannot retrieve password entry\n");
92         }
93         rval =  pw->pw_passwd;
94
95 #if HAVE_SHADOW_H
96         if (rval[0] == 'x' && rval[1] == '\0') {
97                 struct spwd *sp;
98                 sp = getspnam(getenv("USER"));
99                 if (!sp)
100                         die("slock: cannot retrieve shadow entry (make sure to suid or sgid slock)\n");
101                 rval = sp->sp_pwdp;
102         }
103 #endif
104
105         /* drop privileges */
106         if (geteuid() == 0 &&
107             ((getegid() != pw->pw_gid && setgid(pw->pw_gid) < 0) || setuid(pw->pw_uid) < 0))
108                 die("slock: cannot drop privileges\n");
109         return rval;
110 }
111 #endif
112
113 static void
114 #ifdef HAVE_BSD_AUTH
115 readpw(Display *dpy)
116 #else
117 readpw(Display *dpy, const char *pws)
118 #endif
119 {
120         char buf[32], passwd[256];
121         int num, screen;
122         unsigned int len, color;
123         KeySym ksym;
124         XEvent ev;
125         static int oldc = INIT;
126
127         len = 0;
128         running = True;
129
130         /* As "slock" stands for "Simple X display locker", the DPMS settings
131          * had been removed and you can set it with "xset" or some other
132          * utility. This way the user can easily set a customized DPMS
133          * timeout. */
134         while (running && !XNextEvent(dpy, &ev)) {
135                 if (ev.type == KeyPress) {
136                         buf[0] = 0;
137                         num = XLookupString(&ev.xkey, buf, sizeof(buf), &ksym, 0);
138                         if (IsKeypadKey(ksym)) {
139                                 if (ksym == XK_KP_Enter)
140                                         ksym = XK_Return;
141                                 else if (ksym >= XK_KP_0 && ksym <= XK_KP_9)
142                                         ksym = (ksym - XK_KP_0) + XK_0;
143                         }
144                         if (IsFunctionKey(ksym) ||
145                             IsKeypadKey(ksym) ||
146                             IsMiscFunctionKey(ksym) ||
147                             IsPFKey(ksym) ||
148                             IsPrivateKeypadKey(ksym))
149                                 continue;
150                         switch (ksym) {
151                         case XK_Return:
152                                 passwd[len] = 0;
153 #ifdef HAVE_BSD_AUTH
154                                 running = !auth_userokay(getlogin(), NULL, "auth-xlock", passwd);
155 #else
156                                 running = !!strcmp(crypt(passwd, pws), pws);
157 #endif
158                                 if (running) {
159                                         XBell(dpy, 100);
160                                         failure = True;
161                                 }
162                                 len = 0;
163                                 break;
164                         case XK_Escape:
165                                 len = 0;
166                                 break;
167                         case XK_BackSpace:
168                                 if (len)
169                                         --len;
170                                 break;
171                         default:
172                                 if (num && !iscntrl((int) buf[0]) && (len + num < sizeof(passwd))) {
173                                         memcpy(passwd + len, buf, num);
174                                         len += num;
175                                 }
176                                 break;
177                         }
178                         color = len ? INPUT : (failure || failonclear ? FAILED : INIT);
179                         if (oldc != color) {
180                                 for (screen = 0; screen < nscreens; screen++) {
181                                         XSetWindowBackground(dpy, locks[screen]->win, locks[screen]->colors[color]);
182                                         XClearWindow(dpy, locks[screen]->win);
183                                 }
184                                 oldc = color;
185                         }
186                 } else if (rr && ev.type == rrevbase + RRScreenChangeNotify) {
187                         XRRScreenChangeNotifyEvent *rre = (XRRScreenChangeNotifyEvent*)&ev;
188                         for (screen = 0; screen < nscreens; screen++) {
189                                 if (locks[screen]->win == rre->window) {
190                                         XResizeWindow(dpy, locks[screen]->win, rre->width, rre->height);
191                                         XClearWindow(dpy, locks[screen]->win);
192                                 }
193                         }
194                 } else for (screen = 0; screen < nscreens; screen++)
195                         XRaiseWindow(dpy, locks[screen]->win);
196         }
197 }
198
199 static void
200 unlockscreen(Display *dpy, Lock *lock)
201 {
202         if(dpy == NULL || lock == NULL)
203                 return;
204
205         XUngrabPointer(dpy, CurrentTime);
206         XFreeColors(dpy, DefaultColormap(dpy, lock->screen), lock->colors, NUMCOLS, 0);
207         XFreePixmap(dpy, lock->pmap);
208         XDestroyWindow(dpy, lock->win);
209
210         free(lock);
211 }
212
213 static Lock *
214 lockscreen(Display *dpy, int screen)
215 {
216         char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
217         unsigned int len;
218         int i;
219         Lock *lock;
220         XColor color, dummy;
221         XSetWindowAttributes wa;
222         Cursor invisible;
223
224         if (dpy == NULL || screen < 0)
225                 return NULL;
226
227         lock = malloc(sizeof(Lock));
228         if (lock == NULL)
229                 return NULL;
230
231         lock->screen = screen;
232
233         lock->root = RootWindow(dpy, lock->screen);
234
235         for (i = 0; i < NUMCOLS; i++) {
236                 XAllocNamedColor(dpy, DefaultColormap(dpy, lock->screen), colorname[i], &color, &dummy);
237                 lock->colors[i] = color.pixel;
238         }
239
240         /* init */
241         wa.override_redirect = 1;
242         wa.background_pixel = lock->colors[INIT];
243         lock->win = XCreateWindow(dpy, lock->root, 0, 0, DisplayWidth(dpy, lock->screen), DisplayHeight(dpy, lock->screen),
244                                   0, DefaultDepth(dpy, lock->screen), CopyFromParent,
245                                   DefaultVisual(dpy, lock->screen), CWOverrideRedirect | CWBackPixel, &wa);
246         lock->pmap = XCreateBitmapFromData(dpy, lock->win, curs, 8, 8);
247         invisible = XCreatePixmapCursor(dpy, lock->pmap, lock->pmap, &color, &color, 0, 0);
248         XDefineCursor(dpy, lock->win, invisible);
249         XMapRaised(dpy, lock->win);
250         if (rr)
251                 XRRSelectInput(dpy, lock->win, RRScreenChangeNotifyMask);
252         for (len = 1000; len; len--) {
253                 if (XGrabPointer(dpy, lock->root, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
254                     GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
255                         break;
256                 usleep(1000);
257         }
258         if (running && (len > 0)) {
259                 for (len = 1000; len; len--) {
260                         if (XGrabKeyboard(dpy, lock->root, True, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
261                                 break;
262                         usleep(1000);
263                 }
264         }
265
266         running &= (len > 0);
267         if (!running) {
268                 unlockscreen(dpy, lock);
269                 lock = NULL;
270         }
271         else {
272                 XSelectInput(dpy, lock->root, SubstructureNotifyMask);
273         }
274
275         return lock;
276 }
277
278 static void
279 usage(void)
280 {
281         fprintf(stderr, "usage: slock [-v]\n");
282         exit(1);
283 }
284
285 int
286 main(int argc, char **argv) {
287 #ifndef HAVE_BSD_AUTH
288         const char *pws;
289 #endif
290         Display *dpy;
291         int screen;
292
293         if ((argc == 2) && !strcmp("-v", argv[1]))
294                 die("slock-%s, © 2006-2015 slock engineers\n", VERSION);
295         else if (argc != 1)
296                 usage();
297
298 #ifdef __linux__
299         dontkillme();
300 #endif
301
302         if (!getpwuid(getuid()))
303                 die("slock: no passwd entry for you\n");
304
305 #ifndef HAVE_BSD_AUTH
306         pws = getpw();
307 #endif
308
309         if (!(dpy = XOpenDisplay(0)))
310                 die("slock: cannot open display\n");
311         rr = XRRQueryExtension(dpy, &rrevbase, &rrerrbase);
312         /* Get the number of screens in display "dpy" and blank them all. */
313         nscreens = ScreenCount(dpy);
314         locks = malloc(sizeof(Lock *) * nscreens);
315         if (locks == NULL)
316                 die("slock: malloc: %s\n", strerror(errno));
317         int nlocks = 0;
318         for (screen = 0; screen < nscreens; screen++) {
319                 if ( (locks[screen] = lockscreen(dpy, screen)) != NULL)
320                         nlocks++;
321         }
322         XSync(dpy, False);
323
324         /* Did we actually manage to lock something? */
325         if (nlocks == 0) { /* nothing to protect */
326                 free(locks);
327                 XCloseDisplay(dpy);
328                 return 1;
329         }
330
331         /* Everything is now blank. Now wait for the correct password. */
332 #ifdef HAVE_BSD_AUTH
333         readpw(dpy);
334 #else
335         readpw(dpy, pws);
336 #endif
337
338         /* Password ok, unlock everything and quit. */
339         for (screen = 0; screen < nscreens; screen++)
340                 unlockscreen(dpy, locks[screen]);
341
342         free(locks);
343         XCloseDisplay(dpy);
344
345         return 0;
346 }