]> git.armaanb.net Git - slock.git/blob - slock.c
consistently use () with sizeof
[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, llen;
123         KeySym ksym;
124         XEvent ev;
125
126         len = llen = 0;
127         running = True;
128
129         /* As "slock" stands for "Simple X display locker", the DPMS settings
130          * had been removed and you can set it with "xset" or some other
131          * utility. This way the user can easily set a customized DPMS
132          * timeout. */
133         while (running && !XNextEvent(dpy, &ev)) {
134                 if (ev.type == KeyPress) {
135                         buf[0] = 0;
136                         num = XLookupString(&ev.xkey, buf, sizeof(buf), &ksym, 0);
137                         if (IsKeypadKey(ksym)) {
138                                 if (ksym == XK_KP_Enter)
139                                         ksym = XK_Return;
140                                 else if (ksym >= XK_KP_0 && ksym <= XK_KP_9)
141                                         ksym = (ksym - XK_KP_0) + XK_0;
142                         }
143                         if (IsFunctionKey(ksym) ||
144                             IsKeypadKey(ksym) ||
145                             IsMiscFunctionKey(ksym) ||
146                             IsPFKey(ksym) ||
147                             IsPrivateKeypadKey(ksym))
148                                 continue;
149                         switch (ksym) {
150                         case XK_Return:
151                                 passwd[len] = 0;
152 #ifdef HAVE_BSD_AUTH
153                                 running = !auth_userokay(getlogin(), NULL, "auth-xlock", passwd);
154 #else
155                                 running = !!strcmp(crypt(passwd, pws), pws);
156 #endif
157                                 if (running) {
158                                         XBell(dpy, 100);
159                                         failure = True;
160                                 }
161                                 len = 0;
162                                 break;
163                         case XK_Escape:
164                                 len = 0;
165                                 break;
166                         case XK_BackSpace:
167                                 if (len)
168                                         --len;
169                                 break;
170                         default:
171                                 if (num && !iscntrl((int) buf[0]) && (len + num < sizeof(passwd))) {
172                                         memcpy(passwd + len, buf, num);
173                                         len += num;
174                                 }
175                                 break;
176                         }
177                         if (llen == 0 && len != 0) {
178                                 for (screen = 0; screen < nscreens; screen++) {
179                                         XSetWindowBackground(dpy, locks[screen]->win, locks[screen]->colors[INPUT]);
180                                         XClearWindow(dpy, locks[screen]->win);
181                                 }
182                         } else if (llen != 0 && len == 0) {
183                                 for (screen = 0; screen < nscreens; screen++) {
184                                         XSetWindowBackground(dpy, locks[screen]->win, locks[screen]->colors[failure || failonclear ? FAILED : INIT]);
185                                         XClearWindow(dpy, locks[screen]->win);
186                                 }
187                         }
188                         llen = len;
189                 } else if (rr && ev.type == rrevbase + RRScreenChangeNotify) {
190                         XRRScreenChangeNotifyEvent *rre = (XRRScreenChangeNotifyEvent*)&ev;
191                         for (screen = 0; screen < nscreens; screen++) {
192                                 if (locks[screen]->win == rre->window) {
193                                         XResizeWindow(dpy, locks[screen]->win, rre->width, rre->height);
194                                         XClearWindow(dpy, locks[screen]->win);
195                                 }
196                         }
197                 } else for (screen = 0; screen < nscreens; screen++)
198                         XRaiseWindow(dpy, locks[screen]->win);
199         }
200 }
201
202 static void
203 unlockscreen(Display *dpy, Lock *lock)
204 {
205         if(dpy == NULL || lock == NULL)
206                 return;
207
208         XUngrabPointer(dpy, CurrentTime);
209         XFreeColors(dpy, DefaultColormap(dpy, lock->screen), lock->colors, NUMCOLS, 0);
210         XFreePixmap(dpy, lock->pmap);
211         XDestroyWindow(dpy, lock->win);
212
213         free(lock);
214 }
215
216 static Lock *
217 lockscreen(Display *dpy, int screen)
218 {
219         char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
220         unsigned int len;
221         int i;
222         Lock *lock;
223         XColor color, dummy;
224         XSetWindowAttributes wa;
225         Cursor invisible;
226
227         if (dpy == NULL || screen < 0)
228                 return NULL;
229
230         lock = malloc(sizeof(Lock));
231         if (lock == NULL)
232                 return NULL;
233
234         lock->screen = screen;
235
236         lock->root = RootWindow(dpy, lock->screen);
237
238         for (i = 0; i < NUMCOLS; i++) {
239                 XAllocNamedColor(dpy, DefaultColormap(dpy, lock->screen), colorname[i], &color, &dummy);
240                 lock->colors[i] = color.pixel;
241         }
242
243         /* init */
244         wa.override_redirect = 1;
245         wa.background_pixel = lock->colors[INIT];
246         lock->win = XCreateWindow(dpy, lock->root, 0, 0, DisplayWidth(dpy, lock->screen), DisplayHeight(dpy, lock->screen),
247                                   0, DefaultDepth(dpy, lock->screen), CopyFromParent,
248                                   DefaultVisual(dpy, lock->screen), CWOverrideRedirect | CWBackPixel, &wa);
249         lock->pmap = XCreateBitmapFromData(dpy, lock->win, curs, 8, 8);
250         invisible = XCreatePixmapCursor(dpy, lock->pmap, lock->pmap, &color, &color, 0, 0);
251         XDefineCursor(dpy, lock->win, invisible);
252         XMapRaised(dpy, lock->win);
253         if (rr)
254                 XRRSelectInput(dpy, lock->win, RRScreenChangeNotifyMask);
255         for (len = 1000; len; len--) {
256                 if (XGrabPointer(dpy, lock->root, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
257                     GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
258                         break;
259                 usleep(1000);
260         }
261         if (running && (len > 0)) {
262                 for (len = 1000; len; len--) {
263                         if (XGrabKeyboard(dpy, lock->root, True, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
264                                 break;
265                         usleep(1000);
266                 }
267         }
268
269         running &= (len > 0);
270         if (!running) {
271                 unlockscreen(dpy, lock);
272                 lock = NULL;
273         }
274         else {
275                 XSelectInput(dpy, lock->root, SubstructureNotifyMask);
276         }
277
278         return lock;
279 }
280
281 static void
282 usage(void)
283 {
284         fprintf(stderr, "usage: slock [-v]\n");
285         exit(1);
286 }
287
288 int
289 main(int argc, char **argv) {
290 #ifndef HAVE_BSD_AUTH
291         const char *pws;
292 #endif
293         Display *dpy;
294         int screen;
295
296         if ((argc == 2) && !strcmp("-v", argv[1]))
297                 die("slock-%s, © 2006-2015 slock engineers\n", VERSION);
298         else if (argc != 1)
299                 usage();
300
301 #ifdef __linux__
302         dontkillme();
303 #endif
304
305         if (!getpwuid(getuid()))
306                 die("slock: no passwd entry for you\n");
307
308 #ifndef HAVE_BSD_AUTH
309         pws = getpw();
310 #endif
311
312         if (!(dpy = XOpenDisplay(0)))
313                 die("slock: cannot open display\n");
314         rr = XRRQueryExtension(dpy, &rrevbase, &rrerrbase);
315         /* Get the number of screens in display "dpy" and blank them all. */
316         nscreens = ScreenCount(dpy);
317         locks = malloc(sizeof(Lock *) * nscreens);
318         if (locks == NULL)
319                 die("slock: malloc: %s\n", strerror(errno));
320         int nlocks = 0;
321         for (screen = 0; screen < nscreens; screen++) {
322                 if ( (locks[screen] = lockscreen(dpy, screen)) != NULL)
323                         nlocks++;
324         }
325         XSync(dpy, False);
326
327         /* Did we actually manage to lock something? */
328         if (nlocks == 0) { /* nothing to protect */
329                 free(locks);
330                 XCloseDisplay(dpy);
331                 return 1;
332         }
333
334         /* Everything is now blank. Now wait for the correct password. */
335 #ifdef HAVE_BSD_AUTH
336         readpw(dpy);
337 #else
338         readpw(dpy, pws);
339 #endif
340
341         /* Password ok, unlock everything and quit. */
342         for (screen = 0; screen < nscreens; screen++)
343                 unlockscreen(dpy, locks[screen]);
344
345         free(locks);
346         XCloseDisplay(dpy);
347
348         return 0;
349 }