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