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, ...)
56 vfprintf(stderr, errstr, ap);
69 fd = open("/proc/self/oom_score_adj", O_WRONLY);
70 if (fd < 0 && errno == ENOENT) {
73 if (fd < 0 || write(fd, "-1000\n", (sizeof("-1000\n") - 1)) !=
74 (sizeof("-1000\n") - 1) || close(fd) != 0) {
75 die("can't tame the oom-killer. is suid or sgid set?\n");
81 /* only run as root */
89 if (!(pw = getpwuid(getuid()))) {
91 die("slock: getpwuid: %s\n", strerror(errno));
93 die("slock: cannot retrieve password entry\n");
98 if (rval[0] == 'x' && rval[1] == '\0') {
100 if (!(sp = getspnam(getenv("USER"))))
101 die("slock: cannot retrieve shadow entry (make sure to suid or sgid slock)\n");
106 /* drop privileges */
107 if (geteuid() == 0 &&
108 ((getegid() != pw->pw_gid && setgid(pw->pw_gid) < 0) || setuid(pw->pw_uid) < 0))
109 die("slock: cannot drop privileges\n");
118 readpw(Display *dpy, const char *pws)
121 char buf[32], passwd[256];
123 unsigned int len, color;
126 static int oldc = INIT;
131 /* As "slock" stands for "Simple X display locker", the DPMS settings
132 * had been removed and you can set it with "xset" or some other
133 * utility. This way the user can easily set a customized DPMS
135 while (running && !XNextEvent(dpy, &ev)) {
136 if (ev.type == KeyPress) {
138 num = XLookupString(&ev.xkey, buf, sizeof(buf), &ksym, 0);
139 if (IsKeypadKey(ksym)) {
140 if (ksym == XK_KP_Enter)
142 else if (ksym >= XK_KP_0 && ksym <= XK_KP_9)
143 ksym = (ksym - XK_KP_0) + XK_0;
145 if (IsFunctionKey(ksym) ||
147 IsMiscFunctionKey(ksym) ||
149 IsPrivateKeypadKey(ksym))
155 running = !auth_userokay(getlogin(), NULL, "auth-xlock", passwd);
157 running = !!strcmp(crypt(passwd, pws), pws);
173 if (num && !iscntrl((int)buf[0]) && (len + num < sizeof(passwd))) {
174 memcpy(passwd + len, buf, num);
179 color = len ? INPUT : (failure || failonclear ? FAILED : INIT);
180 if (running && oldc != color) {
181 for (screen = 0; screen < nscreens; screen++) {
182 XSetWindowBackground(dpy, locks[screen]->win, locks[screen]->colors[color]);
183 XClearWindow(dpy, locks[screen]->win);
187 } else if (rr && ev.type == rrevbase + RRScreenChangeNotify) {
188 XRRScreenChangeNotifyEvent *rre = (XRRScreenChangeNotifyEvent*)&ev;
189 for (screen = 0; screen < nscreens; screen++) {
190 if (locks[screen]->win == rre->window) {
191 XResizeWindow(dpy, locks[screen]->win, rre->width, rre->height);
192 XClearWindow(dpy, locks[screen]->win);
195 } else for (screen = 0; screen < nscreens; screen++)
196 XRaiseWindow(dpy, locks[screen]->win);
201 unlockscreen(Display *dpy, Lock *lock)
203 if(dpy == NULL || lock == NULL)
206 XUngrabPointer(dpy, CurrentTime);
207 XFreeColors(dpy, DefaultColormap(dpy, lock->screen), lock->colors, NUMCOLS, 0);
208 XFreePixmap(dpy, lock->pmap);
209 XDestroyWindow(dpy, lock->win);
215 lockscreen(Display *dpy, int screen)
217 char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
222 XSetWindowAttributes wa;
225 if (!running || dpy == NULL || screen < 0 || !(lock = malloc(sizeof(Lock))))
228 lock->screen = screen;
229 lock->root = RootWindow(dpy, lock->screen);
231 for (i = 0; i < NUMCOLS; i++) {
232 XAllocNamedColor(dpy, DefaultColormap(dpy, lock->screen), 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, DisplayWidth(dpy, lock->screen), DisplayHeight(dpy, lock->screen),
240 0, DefaultDepth(dpy, lock->screen), CopyFromParent,
241 DefaultVisual(dpy, lock->screen), CWOverrideRedirect | CWBackPixel, &wa);
242 lock->pmap = XCreateBitmapFromData(dpy, lock->win, curs, 8, 8);
243 invisible = XCreatePixmapCursor(dpy, lock->pmap, lock->pmap, &color, &color, 0, 0);
244 XDefineCursor(dpy, lock->win, invisible);
245 XMapRaised(dpy, lock->win);
247 XRRSelectInput(dpy, lock->win, RRScreenChangeNotifyMask);
249 /* Try to grab mouse pointer *and* keyboard, else fail the lock */
250 for (len = 1000; len; len--) {
251 if (XGrabPointer(dpy, lock->root, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
252 GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
257 fprintf(stderr, "slock: unable to grab mouse pointer for screen %d\n", screen);
259 for (len = 1000; len; len--) {
260 if (XGrabKeyboard(dpy, lock->root, True, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess) {
261 /* everything fine, we grabbed both inputs */
262 XSelectInput(dpy, lock->root, SubstructureNotifyMask);
267 fprintf(stderr, "slock: unable to grab keyboard for screen %d\n", screen);
269 /* grabbing one of the inputs failed */
271 unlockscreen(dpy, lock);
278 fprintf(stderr, "usage: slock [-v|POST_LOCK_CMD]\n");
283 main(int argc, char **argv) {
284 #ifndef HAVE_BSD_AUTH
290 if ((argc == 2) && !strcmp("-v", argv[1]))
291 die("slock-%s, © 2006-2016 slock engineers\n", VERSION);
293 if ((argc == 2) && !strcmp("-h", argv[1]))
300 if (!getpwuid(getuid()))
301 die("slock: no passwd entry for you\n");
303 #ifndef HAVE_BSD_AUTH
307 if (!(dpy = XOpenDisplay(0)))
308 die("slock: cannot open display\n");
309 rr = XRRQueryExtension(dpy, &rrevbase, &rrerrbase);
310 /* Get the number of screens in display "dpy" and blank them all. */
311 nscreens = ScreenCount(dpy);
312 if (!(locks = malloc(sizeof(Lock*) * nscreens)))
313 die("slock: malloc: %s\n", strerror(errno));
315 for (screen = 0; screen < nscreens; screen++) {
316 if ((locks[screen] = lockscreen(dpy, screen)) != NULL)
321 /* Did we actually manage to lock something? */
322 if (nlocks == 0) { /* nothing to protect */
328 if (argc >= 2 && fork() == 0) {
330 close(ConnectionNumber(dpy));
331 execvp(argv[1], argv+1);
332 die("slock: execvp %s failed: %s\n", argv[1], strerror(errno));
335 /* Everything is now blank. Now wait for the correct password. */
342 /* Password ok, unlock everything and quit. */
343 for (screen = 0; screen < nscreens; screen++)
344 unlockscreen(dpy, locks[screen]);