1 /* See LICENSE file for license details. */
2 #define _XOPEN_SOURCE 500
15 #include <sys/types.h>
16 #include <X11/extensions/Xrandr.h>
17 #include <X11/keysym.h>
19 #include <X11/Xutil.h>
22 #include <login_cap.h>
39 unsigned long colors[NUMCOLS];
44 static Bool running = True;
45 static Bool failure = False;
51 die(const char *errstr, ...)
55 fputs("slock: ", stderr);
57 vfprintf(stderr, errstr, ap);
70 fd = open("/proc/self/oom_score_adj", O_WRONLY);
71 if (fd < 0 && errno == ENOENT) {
74 if (fd < 0 || write(fd, "-1000\n", (sizeof("-1000\n") - 1)) !=
75 (sizeof("-1000\n") - 1) || close(fd) != 0) {
76 die("can't tame the oom-killer. is suid or sgid set?\n");
82 /* only run as root */
90 if (!(pw = getpwuid(getuid()))) {
92 die("getpwuid: %s\n", strerror(errno));
94 die("cannot retrieve password entry\n");
99 if (rval[0] == 'x' && rval[1] == '\0') {
101 if (!(sp = getspnam(getenv("USER"))))
102 die("cannot retrieve shadow entry (make sure to suid or sgid slock)\n");
107 /* drop privileges */
108 if (geteuid() == 0 &&
109 ((getegid() != pw->pw_gid && setgid(pw->pw_gid) < 0) || setuid(pw->pw_uid) < 0))
110 die("cannot drop privileges\n");
119 readpw(Display *dpy, const char *pws)
122 char buf[32], passwd[256];
124 unsigned int len, color;
127 static int oldc = INIT;
132 /* As "slock" stands for "Simple X display locker", the DPMS settings
133 * had been removed and you can set it with "xset" or some other
134 * utility. This way the user can easily set a customized DPMS
136 while (running && !XNextEvent(dpy, &ev)) {
137 if (ev.type == KeyPress) {
139 num = XLookupString(&ev.xkey, buf, sizeof(buf), &ksym, 0);
140 if (IsKeypadKey(ksym)) {
141 if (ksym == XK_KP_Enter)
143 else if (ksym >= XK_KP_0 && ksym <= XK_KP_9)
144 ksym = (ksym - XK_KP_0) + XK_0;
146 if (IsFunctionKey(ksym) ||
148 IsMiscFunctionKey(ksym) ||
150 IsPrivateKeypadKey(ksym))
156 running = !auth_userokay(getlogin(), NULL, "auth-xlock", passwd);
158 running = !!strcmp(crypt(passwd, pws), pws);
174 if (num && !iscntrl((int)buf[0]) && (len + num < sizeof(passwd))) {
175 memcpy(passwd + len, buf, num);
180 color = len ? INPUT : (failure || failonclear ? FAILED : INIT);
181 if (running && oldc != color) {
182 for (screen = 0; screen < nscreens; screen++) {
183 XSetWindowBackground(dpy, locks[screen]->win, locks[screen]->colors[color]);
184 XClearWindow(dpy, locks[screen]->win);
188 } else if (rr && ev.type == rrevbase + RRScreenChangeNotify) {
189 XRRScreenChangeNotifyEvent *rre = (XRRScreenChangeNotifyEvent*)&ev;
190 for (screen = 0; screen < nscreens; screen++) {
191 if (locks[screen]->win == rre->window) {
192 XResizeWindow(dpy, locks[screen]->win, rre->width, rre->height);
193 XClearWindow(dpy, locks[screen]->win);
196 } else for (screen = 0; screen < nscreens; screen++)
197 XRaiseWindow(dpy, locks[screen]->win);
202 unlockscreen(Display *dpy, Lock *lock)
204 if(dpy == NULL || lock == NULL)
207 XUngrabPointer(dpy, CurrentTime);
208 XFreeColors(dpy, DefaultColormap(dpy, lock->screen), lock->colors, NUMCOLS, 0);
209 XFreePixmap(dpy, lock->pmap);
210 XDestroyWindow(dpy, lock->win);
216 lockscreen(Display *dpy, int screen)
218 char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
223 XSetWindowAttributes wa;
226 if (!running || dpy == NULL || screen < 0 || !(lock = malloc(sizeof(Lock))))
229 lock->screen = screen;
230 lock->root = RootWindow(dpy, lock->screen);
232 for (i = 0; i < NUMCOLS; i++) {
233 XAllocNamedColor(dpy, DefaultColormap(dpy, lock->screen), colorname[i], &color, &dummy);
234 lock->colors[i] = color.pixel;
238 wa.override_redirect = 1;
239 wa.background_pixel = lock->colors[INIT];
240 lock->win = XCreateWindow(dpy, lock->root, 0, 0, DisplayWidth(dpy, lock->screen), DisplayHeight(dpy, lock->screen),
241 0, DefaultDepth(dpy, lock->screen), CopyFromParent,
242 DefaultVisual(dpy, lock->screen), CWOverrideRedirect | CWBackPixel, &wa);
243 lock->pmap = XCreateBitmapFromData(dpy, lock->win, curs, 8, 8);
244 invisible = XCreatePixmapCursor(dpy, lock->pmap, lock->pmap, &color, &color, 0, 0);
245 XDefineCursor(dpy, lock->win, invisible);
246 XMapRaised(dpy, lock->win);
248 XRRSelectInput(dpy, lock->win, RRScreenChangeNotifyMask);
250 /* Try to grab mouse pointer *and* keyboard, else fail the lock */
251 for (len = 1000; len; len--) {
252 if (XGrabPointer(dpy, lock->root, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
253 GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
258 fprintf(stderr, "slock: unable to grab mouse pointer for screen %d\n", screen);
260 for (len = 1000; len; len--) {
261 if (XGrabKeyboard(dpy, lock->root, True, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess) {
262 /* everything fine, we grabbed both inputs */
263 XSelectInput(dpy, lock->root, SubstructureNotifyMask);
268 fprintf(stderr, "slock: unable to grab keyboard for screen %d\n", screen);
270 /* grabbing one of the inputs failed */
272 unlockscreen(dpy, lock);
279 fprintf(stderr, "usage: slock [-v|POST_LOCK_CMD]\n");
284 main(int argc, char **argv) {
285 #ifndef HAVE_BSD_AUTH
291 if ((argc >= 2) && !strcmp("-v", argv[1]))
292 die("version %s, © 2006-2016 slock engineers\n", VERSION);
294 /* treat first argument starting with a '-' as option */
295 if ((argc >= 2) && argv[1][0] == '-')
302 if (!getpwuid(getuid()))
303 die("no passwd entry for you\n");
305 #ifndef HAVE_BSD_AUTH
309 if (!(dpy = XOpenDisplay(0)))
310 die("cannot open display\n");
311 rr = XRRQueryExtension(dpy, &rrevbase, &rrerrbase);
312 /* Get the number of screens in display "dpy" and blank them all. */
313 nscreens = ScreenCount(dpy);
314 if (!(locks = malloc(sizeof(Lock*) * nscreens)))
315 die("Out of memory.\n");
317 for (screen = 0; screen < nscreens; screen++) {
318 if ((locks[screen] = lockscreen(dpy, screen)) != NULL)
323 /* Did we actually manage to lock something? */
324 if (nlocks == 0) { /* nothing to protect */
330 if (argc >= 2 && fork() == 0) {
332 close(ConnectionNumber(dpy));
333 execvp(argv[1], argv+1);
334 die("execvp %s failed: %s\n", argv[1], strerror(errno));
337 /* Everything is now blank. Now wait for the correct password. */
344 /* Password ok, unlock everything and quit. */
345 for (screen = 0; screen < nscreens; screen++)
346 unlockscreen(dpy, locks[screen]);