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