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;
52 die(const char *errstr, ...)
57 fprintf(stderr, "%s: ", argv0);
58 vfprintf(stderr, errstr, ap);
71 fd = open("/proc/self/oom_score_adj", O_WRONLY);
72 if (fd < 0 && errno == ENOENT) {
75 if (fd < 0 || write(fd, "-1000\n", (sizeof("-1000\n") - 1)) !=
76 (sizeof("-1000\n") - 1) || close(fd) != 0) {
77 die("can't tame the oom-killer. is suid or sgid set?\n");
83 /* only run as root */
91 if (!(pw = getpwuid(getuid()))) {
93 die("getpwuid: %s\n", strerror(errno));
95 die("cannot retrieve password entry\n");
100 if (rval[0] == 'x' && rval[1] == '\0') {
102 if (!(sp = getspnam(getenv("USER"))))
103 die("cannot retrieve shadow entry (make sure to suid or sgid slock)\n");
108 /* drop privileges */
109 if (geteuid() == 0 &&
110 ((getegid() != pw->pw_gid && setgid(pw->pw_gid) < 0) || setuid(pw->pw_uid) < 0))
111 die("cannot drop privileges\n");
120 readpw(Display *dpy, const char *pws)
123 char buf[32], passwd[256];
125 unsigned int len, color;
128 static int oldc = INIT;
133 /* As "slock" stands for "Simple X display locker", the DPMS settings
134 * had been removed and you can set it with "xset" or some other
135 * utility. This way the user can easily set a customized DPMS
137 while (running && !XNextEvent(dpy, &ev)) {
138 if (ev.type == KeyPress) {
140 num = XLookupString(&ev.xkey, buf, sizeof(buf), &ksym, 0);
141 if (IsKeypadKey(ksym)) {
142 if (ksym == XK_KP_Enter)
144 else if (ksym >= XK_KP_0 && ksym <= XK_KP_9)
145 ksym = (ksym - XK_KP_0) + XK_0;
147 if (IsFunctionKey(ksym) ||
149 IsMiscFunctionKey(ksym) ||
151 IsPrivateKeypadKey(ksym))
157 running = !auth_userokay(getlogin(), NULL, "auth-xlock", passwd);
159 running = !!strcmp(crypt(passwd, pws), pws);
175 if (num && !iscntrl((int)buf[0]) && (len + num < sizeof(passwd))) {
176 memcpy(passwd + len, buf, num);
181 color = len ? INPUT : (failure || failonclear ? FAILED : INIT);
182 if (running && oldc != color) {
183 for (screen = 0; screen < nscreens; screen++) {
184 XSetWindowBackground(dpy, locks[screen]->win, locks[screen]->colors[color]);
185 XClearWindow(dpy, locks[screen]->win);
189 } else if (rr && ev.type == rrevbase + RRScreenChangeNotify) {
190 XRRScreenChangeNotifyEvent *rre = (XRRScreenChangeNotifyEvent*)&ev;
191 for (screen = 0; screen < nscreens; screen++) {
192 if (locks[screen]->win == rre->window) {
193 XResizeWindow(dpy, locks[screen]->win, rre->width, rre->height);
194 XClearWindow(dpy, locks[screen]->win);
197 } else for (screen = 0; screen < nscreens; screen++)
198 XRaiseWindow(dpy, locks[screen]->win);
203 unlockscreen(Display *dpy, Lock *lock)
205 if(dpy == NULL || lock == NULL)
208 XUngrabPointer(dpy, CurrentTime);
209 XFreeColors(dpy, DefaultColormap(dpy, lock->screen), lock->colors, NUMCOLS, 0);
210 XFreePixmap(dpy, lock->pmap);
211 XDestroyWindow(dpy, lock->win);
217 lockscreen(Display *dpy, int screen)
219 char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
224 XSetWindowAttributes wa;
227 if (!running || dpy == NULL || screen < 0 || !(lock = malloc(sizeof(Lock))))
230 lock->screen = screen;
231 lock->root = RootWindow(dpy, lock->screen);
233 for (i = 0; i < NUMCOLS; i++) {
234 XAllocNamedColor(dpy, DefaultColormap(dpy, lock->screen), colorname[i], &color, &dummy);
235 lock->colors[i] = color.pixel;
239 wa.override_redirect = 1;
240 wa.background_pixel = lock->colors[INIT];
241 lock->win = XCreateWindow(dpy, lock->root, 0, 0, DisplayWidth(dpy, lock->screen), DisplayHeight(dpy, lock->screen),
242 0, DefaultDepth(dpy, lock->screen), CopyFromParent,
243 DefaultVisual(dpy, lock->screen), CWOverrideRedirect | CWBackPixel, &wa);
244 lock->pmap = XCreateBitmapFromData(dpy, lock->win, curs, 8, 8);
245 invisible = XCreatePixmapCursor(dpy, lock->pmap, lock->pmap, &color, &color, 0, 0);
246 XDefineCursor(dpy, lock->win, invisible);
247 XMapRaised(dpy, lock->win);
249 XRRSelectInput(dpy, lock->win, RRScreenChangeNotifyMask);
251 /* Try to grab mouse pointer *and* keyboard, else fail the lock */
252 for (len = 1000; len; len--) {
253 if (XGrabPointer(dpy, lock->root, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
254 GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
259 fprintf(stderr, "unable to grab mouse pointer for screen %d\n", screen);
261 for (len = 1000; len; len--) {
262 if (XGrabKeyboard(dpy, lock->root, True, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess) {
263 /* everything fine, we grabbed both inputs */
264 XSelectInput(dpy, lock->root, SubstructureNotifyMask);
269 fprintf(stderr, "unable to grab keyboard for screen %d\n", screen);
271 /* grabbing one of the inputs failed */
273 unlockscreen(dpy, lock);
278 main(int argc, char **argv)
280 #ifndef HAVE_BSD_AUTH
286 argv0 = argv[0], argc--, argv++;
292 if (!getpwuid(getuid()))
293 die("no passwd entry for you\n");
295 #ifndef HAVE_BSD_AUTH
299 if (!(dpy = XOpenDisplay(0)))
300 die("cannot open display\n");
301 rr = XRRQueryExtension(dpy, &rrevbase, &rrerrbase);
302 /* Get the number of screens in display "dpy" and blank them all. */
303 nscreens = ScreenCount(dpy);
304 if (!(locks = malloc(sizeof(Lock*) * nscreens)))
305 die("Out of memory.\n");
307 for (screen = 0; screen < nscreens; screen++) {
308 if ((locks[screen] = lockscreen(dpy, screen)) != NULL)
313 /* Did we actually manage to lock something? */
314 if (nlocks == 0) { /* nothing to protect */
320 if (argc >= 1 && fork() == 0) {
322 close(ConnectionNumber(dpy));
323 execvp(argv[0], argv);
324 die("execvp %s failed: %s\n", argv[0], strerror(errno));
327 /* Everything is now blank. Now wait for the correct password. */
334 /* Password ok, unlock everything and quit. */
335 for (screen = 0; screen < nscreens; screen++)
336 unlockscreen(dpy, locks[screen]);