Xinqi Bao's Git

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