Xinqi Bao's Git

af5d2066793f049f2bf5a5af7e679d15258ff69f
[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 == 2) && !strcmp("-v", argv[1]))
73 eprint("slock-"VERSION", © 2006-2007 Anselm R. Garbe, Sander van Dijk\n");
74 else if(argc != 1)
75 eprint("usage: slock [-v]\n");
76 pws = get_password();
77 if(!(dpy = XOpenDisplay(0)))
78 eprint("slock: cannot open display\n");
79 screen = DefaultScreen(dpy);
80 root = RootWindow(dpy, screen);
81
82 /* init */
83 wa.override_redirect = 1;
84 wa.background_pixel = BlackPixel(dpy, screen);
85 w = XCreateWindow(dpy, root, 0, 0, DisplayWidth(dpy, screen), DisplayHeight(dpy, screen),
86 0, DefaultDepth(dpy, screen), CopyFromParent,
87 DefaultVisual(dpy, screen), CWOverrideRedirect | CWBackPixel, &wa);
88 XAllocNamedColor(dpy, DefaultColormap(dpy, screen), "black", &black, &dummy);
89 pmap = XCreateBitmapFromData(dpy, w, curs, 8, 8);
90 invisible = XCreatePixmapCursor(dpy, pmap, pmap, &black, &black, 0, 0);
91 XDefineCursor(dpy, w, invisible);
92 XMapRaised(dpy, w);
93 for(len = 1000; len; len--) {
94 if(XGrabPointer(dpy, root, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
95 GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
96 break;
97 usleep(1000);
98 }
99 if((running = running && (len > 0))) {
100 for(len = 1000; len; len--) {
101 if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
102 == GrabSuccess)
103 break;
104 usleep(1000);
105 }
106 running = (len > 0);
107 }
108 len = 0;
109 XSync(dpy, False);
110
111 /* main event loop */
112 while(running && !XNextEvent(dpy, &ev))
113 if(ev.type == KeyPress) {
114 buf[0] = 0;
115 num = XLookupString(&ev.xkey, buf, sizeof buf, &ksym, 0);
116 if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
117 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
118 || IsPrivateKeypadKey(ksym))
119 continue;
120 switch(ksym) {
121 case XK_Return:
122 passwd[len] = 0;
123 if((running = strcmp(crypt(passwd, pws), pws)) != 0)
124 XBell(dpy, 100);
125 len = 0;
126 break;
127 case XK_Escape:
128 len = 0;
129 break;
130 case XK_BackSpace:
131 if(len)
132 --len;
133 break;
134 default:
135 if(num && !iscntrl((int) buf[0]) && (len + num < sizeof passwd)) {
136 memcpy(passwd + len, buf, num);
137 len += num;
138 }
139 break;
140 }
141 }
142 XUngrabPointer(dpy, CurrentTime);
143 XFreePixmap(dpy, pmap);
144 XDestroyWindow(dpy, w);
145 XCloseDisplay(dpy);
146 return 0;
147 }