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