1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
4 #define _XOPEN_SOURCE 500
15 #include <sys/types.h>
16 #include <X11/keysym.h>
18 #include <X11/Xutil.h>
21 get_password() { /* only run as root */
26 fputs("slock: cannot retrieve password entry (make sure to suid slock)\n", stderr);
29 pw = getpwuid(getuid());
36 sp = getspnam(getenv("USER"));
42 if(setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0) {
43 fputs("slock: cannot drop privileges\n",stdout);
50 main(int argc, char **argv) {
51 char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
52 char buf[32], passwd[256];
64 XSetWindowAttributes wa;
66 if((argc > 1) && !strncmp(argv[1], "-v", 3)) {
67 fputs("slock-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
71 if(!(dpy = XOpenDisplay(0))) {
72 fputs("slock: cannot open display\n", stderr);
75 screen = DefaultScreen(dpy);
78 wa.override_redirect = 1;
79 wa.background_pixel = BlackPixel(dpy, screen);
80 w = XCreateWindow(dpy, RootWindow(dpy, screen), 0, 0,
81 DisplayWidth(dpy, screen), DisplayHeight(dpy, screen),
82 0, DefaultDepth(dpy, screen), CopyFromParent,
83 DefaultVisual(dpy, screen), CWOverrideRedirect | CWBackPixel, &wa);
85 XAllocNamedColor(dpy, DefaultColormap(dpy, screen), "black", &black, &dummy);
86 pmap = XCreateBitmapFromData(dpy, w, curs, 8, 8);
87 invisible = XCreatePixmapCursor(dpy, pmap, pmap, &black, &black, 0, 0);
88 XDefineCursor(dpy, w, invisible);
89 for(len = 1000; len && (XGrabPointer(dpy, RootWindow(dpy, screen), False,
90 ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
91 GrabModeAsync, GrabModeSync, None, invisible, CurrentTime) != GrabSuccess); len--)
93 running = running && (len > 0);
94 for(len = 1000; len && (XGrabKeyboard(dpy, RootWindow(dpy, screen), True, GrabModeAsync,
95 GrabModeAsync, CurrentTime) != GrabSuccess); len--)
97 running = running && (len > 0);
102 /* main event loop */
103 while(running && !XNextEvent(dpy, &ev))
104 if(ev.type == KeyPress) {
106 num = XLookupString(&ev.xkey, buf, sizeof buf, &ksym, 0);
107 if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
108 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
109 || IsPrivateKeypadKey(ksym))
114 if((running = strcmp(crypt(passwd, pws), pws)) != 0)
126 if(num && !iscntrl((int) buf[0]) && (len + num < sizeof passwd)) {
127 memcpy(passwd + len, buf, num);
133 XUngrabPointer(dpy, CurrentTime);
134 XFreePixmap(dpy, pmap);
135 XDestroyWindow(dpy, w);