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