Xinqi Bao's Git

final fixes and cleanups
[slock.git] / slock.c
1 /* © 2006-2007 Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details. */
3 #define _XOPEN_SOURCE 500
4 #if HAVE_SHADOW_H
5 #include <shadow.h>
6 #endif
7
8 #include <ctype.h>
9 #include <pwd.h>
10 #include <stdarg.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <sys/types.h>
16 #include <X11/keysym.h>
17 #include <X11/Xlib.h>
18 #include <X11/Xutil.h>
19
20 void
21 eprint(const char *errstr, ...) {
22 va_list ap;
23
24 va_start(ap, errstr);
25 vfprintf(stderr, errstr, ap);
26 va_end(ap);
27 exit(EXIT_FAILURE);
28 }
29
30 const char *
31 get_password() { /* only run as root */
32 const char *rval;
33 struct passwd *pw;
34
35 if(geteuid() != 0)
36 eprint("slock: cannot retrieve password entry (make sure to suid slock)\n");
37 pw = getpwuid(getuid());
38 endpwent();
39 rval = pw->pw_passwd;
40
41 #if HAVE_SHADOW_H
42 {
43 struct spwd *sp;
44 sp = getspnam(getenv("USER"));
45 endspent();
46 rval = sp->sp_pwdp;
47 }
48 #endif
49 /* drop privileges */
50 if(setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0)
51 eprint("slock: cannot drop privileges\n");
52 return rval;
53 }
54
55 int
56 main(int argc, char **argv) {
57 char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
58 char buf[32], passwd[256];
59 int num, screen;
60 const char *pws;
61 unsigned int len;
62 Bool running = True;
63 Cursor invisible;
64 Display *dpy;
65 KeySym ksym;
66 Pixmap pmap;
67 Window root, w;
68 XColor black, dummy;
69 XEvent ev;
70 XSetWindowAttributes wa;
71
72 if((argc > 1) && !strncmp(argv[1], "-v", 3))
73 eprint("slock-"VERSION", © 2006-2007 Anselm R. Garbe, Sander van Dijk\n");
74 pws = get_password();
75 if(!(dpy = XOpenDisplay(0)))
76 eprint("slock: cannot open display\n");
77 screen = DefaultScreen(dpy);
78 root = RootWindow(dpy, screen);
79
80 /* init */
81 wa.override_redirect = 1;
82 wa.background_pixel = BlackPixel(dpy, screen);
83 w = XCreateWindow(dpy, root, 0, 0, DisplayWidth(dpy, screen), DisplayHeight(dpy, screen),
84 0, DefaultDepth(dpy, screen), CopyFromParent,
85 DefaultVisual(dpy, screen), CWOverrideRedirect | CWBackPixel, &wa);
86 XAllocNamedColor(dpy, DefaultColormap(dpy, screen), "black", &black, &dummy);
87 pmap = XCreateBitmapFromData(dpy, w, curs, 8, 8);
88 invisible = XCreatePixmapCursor(dpy, pmap, pmap, &black, &black, 0, 0);
89 XDefineCursor(dpy, w, invisible);
90 XMapRaised(dpy, w);
91 for(len = 1000; len; len--) {
92 if(XGrabPointer(dpy, root, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
93 GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
94 break;
95 usleep(1000);
96 }
97 if((running = running && (len > 0))) {
98 for(len = 1000; len; len--) {
99 if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
100 == GrabSuccess)
101 break;
102 usleep(1000);
103 }
104 running = (len > 0);
105 }
106 len = 0;
107 XSync(dpy, False);
108
109 /* main event loop */
110 while(running && !XNextEvent(dpy, &ev))
111 if(ev.type == KeyPress) {
112 buf[0] = 0;
113 num = XLookupString(&ev.xkey, buf, sizeof buf, &ksym, 0);
114 if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
115 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
116 || IsPrivateKeypadKey(ksym))
117 continue;
118 switch(ksym) {
119 case XK_Return:
120 passwd[len] = 0;
121 if((running = strcmp(crypt(passwd, pws), pws)) != 0)
122 XBell(dpy, 100);
123 len = 0;
124 break;
125 case XK_Escape:
126 len = 0;
127 break;
128 case XK_BackSpace:
129 if(len)
130 --len;
131 break;
132 default:
133 if(num && !iscntrl((int) buf[0]) && (len + num < sizeof passwd)) {
134 memcpy(passwd + len, buf, num);
135 len += num;
136 }
137 break;
138 }
139 }
140 XUngrabPointer(dpy, CurrentTime);
141 XFreePixmap(dpy, pmap);
142 XDestroyWindow(dpy, w);
143 XCloseDisplay(dpy);
144 return 0;
145 }