1 /* See LICENSE file for license details. */
2 #define _XOPEN_SOURCE 500
16 #include <sys/types.h>
17 #include <X11/extensions/Xrandr.h>
18 #include <X11/keysym.h>
20 #include <X11/Xutil.h>
40 unsigned long colors[NUMCOLS];
50 die(const char *errstr, ...)
55 vfprintf(stderr, errstr, ap);
62 #include <linux/oom.h>
68 const char oomfile[] = "/proc/self/oom_score_adj";
70 if (!(f = fopen(oomfile, "w"))) {
73 die("slock: fopen %s: %s\n", oomfile, strerror(errno));
75 fprintf(f, "%d", OOM_SCORE_ADJ_MIN);
78 die("slock: unable to disable OOM killer. "
79 "Make sure to suid or sgid slock.\n");
81 die("slock: fclose %s: %s\n", oomfile, strerror(errno));
92 /* Check if the current user has a password entry */
94 if (!(pw = getpwuid(getuid()))) {
96 die("slock: getpwuid: %s\n", strerror(errno));
98 die("slock: cannot retrieve password entry\n");
100 hash = pw->pw_passwd;
103 if (hash[0] == 'x' && hash[1] == '\0') {
105 if (!(sp = getspnam(pw->pw_name)))
106 die("slock: getspnam: cannot retrieve shadow entry. "
107 "Make sure to suid or sgid slock.\n");
111 if (hash[0] == '*' && hash[1] == '\0') {
113 if (!(pw = getpwuid_shadow(getuid())))
114 die("slock: getpwnam_shadow: cannot retrieve shadow entry. "
115 "Make sure to suid or sgid slock.\n");
116 hash = pw->pw_passwd;
118 die("slock: getpwuid: cannot retrieve shadow entry. "
119 "Make sure to suid or sgid slock.\n");
120 #endif /* __OpenBSD__ */
122 #endif /* HAVE_SHADOW_H */
128 readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens,
131 XRRScreenChangeNotifyEvent *rre;
132 char buf[32], passwd[256], *inputhash;
133 int num, screen, running, failure;
134 unsigned int len, color;
137 static int oldc = INIT;
143 while (running && !XNextEvent(dpy, &ev)) {
144 if (ev.type == KeyPress) {
145 explicit_bzero(&buf, sizeof(buf));
146 num = XLookupString(&ev.xkey, buf, sizeof(buf), &ksym, 0);
147 if (IsKeypadKey(ksym)) {
148 if (ksym == XK_KP_Enter)
150 else if (ksym >= XK_KP_0 && ksym <= XK_KP_9)
151 ksym = (ksym - XK_KP_0) + XK_0;
153 if (IsFunctionKey(ksym) ||
155 IsMiscFunctionKey(ksym) ||
157 IsPrivateKeypadKey(ksym))
163 if (!(inputhash = crypt(passwd, hash)))
164 fprintf(stderr, "slock: crypt: %s\n", strerror(errno));
166 running = !!strcmp(inputhash, hash);
171 explicit_bzero(&passwd, sizeof(passwd));
175 explicit_bzero(&passwd, sizeof(passwd));
183 if (num && !iscntrl((int)buf[0]) &&
184 (len + num < sizeof(passwd))) {
185 memcpy(passwd + len, buf, num);
190 color = len ? INPUT : ((failure || failonclear) ? FAILED : INIT);
191 if (running && oldc != color) {
192 for (screen = 0; screen < nscreens; screen++) {
193 XSetWindowBackground(dpy,
195 locks[screen]->colors[color]);
196 XClearWindow(dpy, locks[screen]->win);
200 } else if (rr->active && ev.type == rr->evbase + RRScreenChangeNotify) {
201 rre = (XRRScreenChangeNotifyEvent*)&ev;
202 for (screen = 0; screen < nscreens; screen++) {
203 if (locks[screen]->win == rre->window) {
204 XResizeWindow(dpy, locks[screen]->win,
205 rre->width, rre->height);
206 XClearWindow(dpy, locks[screen]->win);
209 } else for (screen = 0; screen < nscreens; screen++)
210 XRaiseWindow(dpy, locks[screen]->win);
215 lockscreen(Display *dpy, struct xrandr *rr, int screen)
217 char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
218 int i, ptgrab, kbgrab;
221 XSetWindowAttributes wa;
224 if (dpy == NULL || screen < 0 || !(lock = malloc(sizeof(struct lock))))
227 lock->screen = screen;
228 lock->root = RootWindow(dpy, lock->screen);
230 for (i = 0; i < NUMCOLS; i++) {
231 XAllocNamedColor(dpy, DefaultColormap(dpy, lock->screen),
232 colorname[i], &color, &dummy);
233 lock->colors[i] = color.pixel;
237 wa.override_redirect = 1;
238 wa.background_pixel = lock->colors[INIT];
239 lock->win = XCreateWindow(dpy, lock->root, 0, 0,
240 DisplayWidth(dpy, lock->screen),
241 DisplayHeight(dpy, lock->screen),
242 0, DefaultDepth(dpy, lock->screen),
244 DefaultVisual(dpy, lock->screen),
245 CWOverrideRedirect | CWBackPixel, &wa);
246 lock->pmap = XCreateBitmapFromData(dpy, lock->win, curs, 8, 8);
247 invisible = XCreatePixmapCursor(dpy, lock->pmap, lock->pmap,
248 &color, &color, 0, 0);
249 XDefineCursor(dpy, lock->win, invisible);
251 /* Try to grab mouse pointer *and* keyboard for 600ms, else fail the lock */
252 for (i = 0, ptgrab = kbgrab = -1; i < 6; i++) {
253 if (ptgrab != GrabSuccess) {
254 ptgrab = XGrabPointer(dpy, lock->root, False,
255 ButtonPressMask | ButtonReleaseMask |
256 PointerMotionMask, GrabModeAsync,
257 GrabModeAsync, None, invisible, CurrentTime);
259 if (kbgrab != GrabSuccess) {
260 kbgrab = XGrabKeyboard(dpy, lock->root, True,
261 GrabModeAsync, GrabModeAsync, CurrentTime);
264 /* input is grabbed: we can lock the screen */
265 if (ptgrab == GrabSuccess && kbgrab == GrabSuccess) {
266 XMapRaised(dpy, lock->win);
268 XRRSelectInput(dpy, lock->win, RRScreenChangeNotifyMask);
270 XSelectInput(dpy, lock->root, SubstructureNotifyMask);
274 /* retry on AlreadyGrabbed but fail on other errors */
275 if ((ptgrab != AlreadyGrabbed && ptgrab != GrabSuccess) ||
276 (kbgrab != AlreadyGrabbed && kbgrab != GrabSuccess))
282 /* we couldn't grab all input: fail out */
283 if (ptgrab != GrabSuccess)
284 fprintf(stderr, "slock: unable to grab mouse pointer for screen %d\n",
286 if (kbgrab != GrabSuccess)
287 fprintf(stderr, "slock: unable to grab keyboard for screen %d\n",
295 die("usage: slock [-v] [cmd [arg ...]]\n");
299 main(int argc, char **argv) {
308 int s, nlocks, nscreens;
312 fprintf(stderr, "slock-"VERSION"\n");
318 /* validate drop-user and -group */
320 if (!(pwd = getpwnam(user)))
321 die("slock: getpwnam %s: %s\n", user,
322 errno ? strerror(errno) : "user entry not found");
325 if (!(grp = getgrnam(group)))
326 die("slock: getgrnam %s: %s\n", group,
327 errno ? strerror(errno) : "group entry not found");
336 if (!crypt("", hash))
337 die("slock: crypt: %s\n", strerror(errno));
339 if (!(dpy = XOpenDisplay(NULL)))
340 die("slock: cannot open display\n");
342 /* drop privileges */
343 if (setgroups(0, NULL) < 0)
344 die("slock: setgroups: %s\n", strerror(errno));
345 if (setgid(dgid) < 0)
346 die("slock: setgid: %s\n", strerror(errno));
347 if (setuid(duid) < 0)
348 die("slock: setuid: %s\n", strerror(errno));
350 /* check for Xrandr support */
351 rr.active = XRRQueryExtension(dpy, &rr.evbase, &rr.errbase);
353 /* get number of screens in display "dpy" and blank them */
354 nscreens = ScreenCount(dpy);
355 if (!(locks = calloc(nscreens, sizeof(struct lock *))))
356 die("slock: out of memory\n");
357 for (nlocks = 0, s = 0; s < nscreens; s++) {
358 if ((locks[s] = lockscreen(dpy, &rr, s)) != NULL)
365 /* did we manage to lock everything? */
366 if (nlocks != nscreens)
369 /* run post-lock command */
373 die("slock: fork failed: %s\n", strerror(errno));
375 if (close(ConnectionNumber(dpy)) < 0)
376 die("slock: close: %s\n", strerror(errno));
377 execvp(argv[0], argv);
378 fprintf(stderr, "slock: execvp %s: %s\n", argv[0], strerror(errno));
383 /* everything is now blank. Wait for the correct password */
384 readpw(dpy, &rr, locks, nscreens, hash);