2 /* See LICENSE file for license details. */
3 #define _XOPEN_SOURCE 500
16 #include <sys/types.h>
17 #include <X11/keysym.h>
19 #include <X11/Xutil.h>
22 #include <login_cap.h>
30 unsigned long colors[2];
35 static Bool running = True;
38 die(const char *errstr, ...) {
42 vfprintf(stderr, errstr, ap);
54 fd = open("/proc/self/oom_score_adj", O_WRONLY);
55 if (fd < 0 && errno == ENOENT)
57 if (fd < 0 || write(fd, "-1000\n", 6) != 6 || close(fd) != 0)
58 die("cannot disable the out-of-memory killer for this process\n");
64 getpw(void) { /* only run as root */
69 pw = getpwuid(getuid());
72 die("slock: getpwuid: %s\n", strerror(errno));
74 die("slock: cannot retrieve password entry (make sure to suid or sgid slock)\n");
80 if (rval[0] == 'x' && rval[1] == '\0') {
82 sp = getspnam(getenv("USER"));
84 die("slock: cannot retrieve shadow entry (make sure to suid or sgid slock)\n");
92 && ((getegid() != pw->pw_gid && setgid(pw->pw_gid) < 0) || setuid(pw->pw_uid) < 0))
93 die("slock: cannot drop privileges\n");
102 readpw(Display *dpy, const char *pws)
105 char buf[32], passwd[256];
107 unsigned int len, llen;
114 /* As "slock" stands for "Simple X display locker", the DPMS settings
115 * had been removed and you can set it with "xset" or some other
116 * utility. This way the user can easily set a customized DPMS
118 while(running && !XNextEvent(dpy, &ev)) {
119 if(ev.type == KeyPress) {
121 num = XLookupString(&ev.xkey, buf, sizeof buf, &ksym, 0);
122 if(IsKeypadKey(ksym)) {
123 if(ksym == XK_KP_Enter)
125 else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
126 ksym = (ksym - XK_KP_0) + XK_0;
128 if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
129 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
130 || IsPrivateKeypadKey(ksym))
136 running = !auth_userokay(getlogin(), NULL, "auth-xlock", passwd);
138 running = !!strcmp(crypt(passwd, pws), pws);
152 if(num && !iscntrl((int) buf[0]) && (len + num < sizeof passwd)) {
153 memcpy(passwd + len, buf, num);
158 if(llen == 0 && len != 0) {
159 for(screen = 0; screen < nscreens; screen++) {
160 XSetWindowBackground(dpy, locks[screen]->win, locks[screen]->colors[1]);
161 XClearWindow(dpy, locks[screen]->win);
163 } else if(llen != 0 && len == 0) {
164 for(screen = 0; screen < nscreens; screen++) {
165 XSetWindowBackground(dpy, locks[screen]->win, locks[screen]->colors[0]);
166 XClearWindow(dpy, locks[screen]->win);
171 else for(screen = 0; screen < nscreens; screen++)
172 XRaiseWindow(dpy, locks[screen]->win);
177 unlockscreen(Display *dpy, Lock *lock) {
178 if(dpy == NULL || lock == NULL)
181 XUngrabPointer(dpy, CurrentTime);
182 XFreeColors(dpy, DefaultColormap(dpy, lock->screen), lock->colors, 2, 0);
183 XFreePixmap(dpy, lock->pmap);
184 XDestroyWindow(dpy, lock->win);
190 lockscreen(Display *dpy, int screen) {
191 char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
195 XSetWindowAttributes wa;
198 if(dpy == NULL || screen < 0)
201 lock = malloc(sizeof(Lock));
205 lock->screen = screen;
207 lock->root = RootWindow(dpy, lock->screen);
210 wa.override_redirect = 1;
211 wa.background_pixel = BlackPixel(dpy, lock->screen);
212 lock->win = XCreateWindow(dpy, lock->root, 0, 0, DisplayWidth(dpy, lock->screen), DisplayHeight(dpy, lock->screen),
213 0, DefaultDepth(dpy, lock->screen), CopyFromParent,
214 DefaultVisual(dpy, lock->screen), CWOverrideRedirect | CWBackPixel, &wa);
215 XAllocNamedColor(dpy, DefaultColormap(dpy, lock->screen), COLOR2, &color, &dummy);
216 lock->colors[1] = color.pixel;
217 XAllocNamedColor(dpy, DefaultColormap(dpy, lock->screen), COLOR1, &color, &dummy);
218 lock->colors[0] = color.pixel;
219 lock->pmap = XCreateBitmapFromData(dpy, lock->win, curs, 8, 8);
220 invisible = XCreatePixmapCursor(dpy, lock->pmap, lock->pmap, &color, &color, 0, 0);
221 XDefineCursor(dpy, lock->win, invisible);
222 XMapRaised(dpy, lock->win);
223 for(len = 1000; len; len--) {
224 if(XGrabPointer(dpy, lock->root, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
225 GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
229 if(running && (len > 0)) {
230 for(len = 1000; len; len--) {
231 if(XGrabKeyboard(dpy, lock->root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
238 running &= (len > 0);
240 unlockscreen(dpy, lock);
244 XSelectInput(dpy, lock->root, SubstructureNotifyMask);
251 fprintf(stderr, "usage: slock [-v]\n");
256 main(int argc, char **argv) {
257 #ifndef HAVE_BSD_AUTH
263 if((argc == 2) && !strcmp("-v", argv[1]))
264 die("slock-%s, © 2006-2012 Anselm R Garbe\n", VERSION);
272 if(!getpwuid(getuid()))
273 die("slock: no passwd entry for you\n");
275 #ifndef HAVE_BSD_AUTH
279 if(!(dpy = XOpenDisplay(0)))
280 die("slock: cannot open display\n");
281 /* Get the number of screens in display "dpy" and blank them all. */
282 nscreens = ScreenCount(dpy);
283 locks = malloc(sizeof(Lock *) * nscreens);
285 die("slock: malloc: %s\n", strerror(errno));
287 for(screen = 0; screen < nscreens; screen++) {
288 if ( (locks[screen] = lockscreen(dpy, screen)) != NULL)
293 /* Did we actually manage to lock something? */
294 if (nlocks == 0) { // nothing to protect
300 /* Everything is now blank. Now wait for the correct password. */
307 /* Password ok, unlock everything and quit. */
308 for(screen = 0; screen < nscreens; screen++)
309 unlockscreen(dpy, locks[screen]);