Xinqi Bao's Git

6502c86157ec2022f8ca2a3782a5e4880412000a
[slock.git] / slock.c
1 /* See LICENSE file for license details. */
2 #define _XOPEN_SOURCE 500
3 #if HAVE_SHADOW_H
4 #include <shadow.h>
5 #endif
6
7 #include <ctype.h>
8 #include <errno.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/extensions/Xrandr.h>
17 #include <X11/keysym.h>
18 #include <X11/Xlib.h>
19 #include <X11/Xutil.h>
20
21 #if HAVE_BSD_AUTH
22 #include <login_cap.h>
23 #include <bsd_auth.h>
24 #endif
25
26 enum {
27 INIT,
28 INPUT,
29 EMPTY,
30 NUMCOLS
31 };
32
33 #include "config.h"
34
35 typedef struct {
36 int screen;
37 Window root, win;
38 Pixmap pmap;
39 unsigned long colors[NUMCOLS];
40 } Lock;
41
42 static Lock **locks;
43 static int nscreens;
44 static Bool running = True;
45 static Bool rr;
46 static int rrevbase;
47 static int rrerrbase;
48
49 static void
50 die(const char *errstr, ...)
51 {
52 va_list ap;
53
54 va_start(ap, errstr);
55 vfprintf(stderr, errstr, ap);
56 va_end(ap);
57 exit(1);
58 }
59
60 #ifdef __linux__
61 #include <fcntl.h>
62
63 static void
64 dontkillme(void)
65 {
66 int fd;
67
68 fd = open("/proc/self/oom_score_adj", O_WRONLY);
69 if (fd < 0 && errno == ENOENT)
70 return;
71 if (fd < 0 || write(fd, "-1000\n", 6) != 6 || close(fd) != 0)
72 die("cannot disable the out-of-memory killer for this process\n");
73 }
74 #endif
75
76 #ifndef HAVE_BSD_AUTH
77 /* only run as root */
78 static const char *
79 getpw(void)
80 {
81 const char *rval;
82 struct passwd *pw;
83
84 errno = 0;
85 pw = getpwuid(getuid());
86 if (!pw) {
87 if (errno)
88 die("slock: getpwuid: %s\n", strerror(errno));
89 else
90 die("slock: cannot retrieve password entry\n");
91 }
92 rval = pw->pw_passwd;
93
94 #if HAVE_SHADOW_H
95 if (rval[0] == 'x' && rval[1] == '\0') {
96 struct spwd *sp;
97 sp = getspnam(getenv("USER"));
98 if (!sp)
99 die("slock: cannot retrieve shadow entry (make sure to suid or sgid slock)\n");
100 rval = sp->sp_pwdp;
101 }
102 #endif
103
104 /* drop privileges */
105 if (geteuid() == 0 &&
106 ((getegid() != pw->pw_gid && setgid(pw->pw_gid) < 0) || setuid(pw->pw_uid) < 0))
107 die("slock: cannot drop privileges\n");
108 return rval;
109 }
110 #endif
111
112 static void
113 #ifdef HAVE_BSD_AUTH
114 readpw(Display *dpy)
115 #else
116 readpw(Display *dpy, const char *pws)
117 #endif
118 {
119 char buf[32], passwd[256];
120 int num, screen;
121 unsigned int len, llen;
122 KeySym ksym;
123 XEvent ev;
124
125 len = llen = 0;
126 running = True;
127
128 /* As "slock" stands for "Simple X display locker", the DPMS settings
129 * had been removed and you can set it with "xset" or some other
130 * utility. This way the user can easily set a customized DPMS
131 * timeout. */
132 while (running && !XNextEvent(dpy, &ev)) {
133 if (ev.type == KeyPress) {
134 buf[0] = 0;
135 num = XLookupString(&ev.xkey, buf, sizeof buf, &ksym, 0);
136 if (IsKeypadKey(ksym)) {
137 if (ksym == XK_KP_Enter)
138 ksym = XK_Return;
139 else if (ksym >= XK_KP_0 && ksym <= XK_KP_9)
140 ksym = (ksym - XK_KP_0) + XK_0;
141 }
142 if (IsFunctionKey(ksym) ||
143 IsKeypadKey(ksym) ||
144 IsMiscFunctionKey(ksym) ||
145 IsPFKey(ksym) ||
146 IsPrivateKeypadKey(ksym))
147 continue;
148 switch (ksym) {
149 case XK_Return:
150 passwd[len] = 0;
151 #ifdef HAVE_BSD_AUTH
152 running = !auth_userokay(getlogin(), NULL, "auth-xlock", passwd);
153 #else
154 running = !!strcmp(crypt(passwd, pws), pws);
155 #endif
156 if (running)
157 XBell(dpy, 100);
158 len = 0;
159 break;
160 case XK_Escape:
161 len = 0;
162 break;
163 case XK_BackSpace:
164 if (len)
165 --len;
166 break;
167 default:
168 if (num && !iscntrl((int) buf[0]) && (len + num < sizeof passwd)) {
169 memcpy(passwd + len, buf, num);
170 len += num;
171 }
172 break;
173 }
174 if (llen == 0 && len != 0) {
175 for (screen = 0; screen < nscreens; screen++) {
176 XSetWindowBackground(dpy, locks[screen]->win, locks[screen]->colors[INPUT]);
177 XClearWindow(dpy, locks[screen]->win);
178 }
179 } else if (llen != 0 && len == 0) {
180 for (screen = 0; screen < nscreens; screen++) {
181 XSetWindowBackground(dpy, locks[screen]->win, locks[screen]->colors[EMPTY]);
182 XClearWindow(dpy, locks[screen]->win);
183 }
184 }
185 llen = len;
186 } else if (rr && ev.type == rrevbase + RRScreenChangeNotify) {
187 XRRScreenChangeNotifyEvent *rre = (XRRScreenChangeNotifyEvent*)&ev;
188 for (screen = 0; screen < nscreens; screen++) {
189 if (locks[screen]->win == rre->window) {
190 XResizeWindow(dpy, locks[screen]->win, rre->width, rre->height);
191 XClearWindow(dpy, locks[screen]->win);
192 }
193 }
194 } else for (screen = 0; screen < nscreens; screen++)
195 XRaiseWindow(dpy, locks[screen]->win);
196 }
197 }
198
199 static void
200 unlockscreen(Display *dpy, Lock *lock)
201 {
202 if(dpy == NULL || lock == NULL)
203 return;
204
205 XUngrabPointer(dpy, CurrentTime);
206 XFreeColors(dpy, DefaultColormap(dpy, lock->screen), lock->colors, NUMCOLS, 0);
207 XFreePixmap(dpy, lock->pmap);
208 XDestroyWindow(dpy, lock->win);
209
210 free(lock);
211 }
212
213 static Lock *
214 lockscreen(Display *dpy, int screen)
215 {
216 char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
217 unsigned int len;
218 int i;
219 Lock *lock;
220 XColor color, dummy;
221 XSetWindowAttributes wa;
222 Cursor invisible;
223
224 if (dpy == NULL || screen < 0)
225 return NULL;
226
227 lock = malloc(sizeof(Lock));
228 if (lock == NULL)
229 return NULL;
230
231 lock->screen = screen;
232
233 lock->root = RootWindow(dpy, lock->screen);
234
235 for (i = 0; i < NUMCOLS; i++) {
236 XAllocNamedColor(dpy, DefaultColormap(dpy, lock->screen), colorname[i], &color, &dummy);
237 lock->colors[i] = color.pixel;
238 }
239
240 /* init */
241 wa.override_redirect = 1;
242 wa.background_pixel = lock->colors[INIT];
243 lock->win = XCreateWindow(dpy, lock->root, 0, 0, DisplayWidth(dpy, lock->screen), DisplayHeight(dpy, lock->screen),
244 0, DefaultDepth(dpy, lock->screen), CopyFromParent,
245 DefaultVisual(dpy, lock->screen), CWOverrideRedirect | CWBackPixel, &wa);
246 lock->pmap = XCreateBitmapFromData(dpy, lock->win, curs, 8, 8);
247 invisible = XCreatePixmapCursor(dpy, lock->pmap, lock->pmap, &color, &color, 0, 0);
248 XDefineCursor(dpy, lock->win, invisible);
249 XMapRaised(dpy, lock->win);
250 if (rr)
251 XRRSelectInput(dpy, lock->win, RRScreenChangeNotifyMask);
252 for (len = 1000; len; len--) {
253 if (XGrabPointer(dpy, lock->root, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
254 GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
255 break;
256 usleep(1000);
257 }
258 if (running && (len > 0)) {
259 for (len = 1000; len; len--) {
260 if (XGrabKeyboard(dpy, lock->root, True, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
261 break;
262 usleep(1000);
263 }
264 }
265
266 running &= (len > 0);
267 if (!running) {
268 unlockscreen(dpy, lock);
269 lock = NULL;
270 }
271 else {
272 XSelectInput(dpy, lock->root, SubstructureNotifyMask);
273 }
274
275 return lock;
276 }
277
278 static void
279 usage(void)
280 {
281 fprintf(stderr, "usage: slock [-v]\n");
282 exit(1);
283 }
284
285 int
286 main(int argc, char **argv) {
287 #ifndef HAVE_BSD_AUTH
288 const char *pws;
289 #endif
290 Display *dpy;
291 int screen;
292
293 if ((argc == 2) && !strcmp("-v", argv[1]))
294 die("slock-%s, © 2006-2015 slock engineers\n", VERSION);
295 else if (argc != 1)
296 usage();
297
298 #ifdef __linux__
299 dontkillme();
300 #endif
301
302 if (!getpwuid(getuid()))
303 die("slock: no passwd entry for you\n");
304
305 #ifndef HAVE_BSD_AUTH
306 pws = getpw();
307 #endif
308
309 if (!(dpy = XOpenDisplay(0)))
310 die("slock: cannot open display\n");
311 rr = XRRQueryExtension(dpy, &rrevbase, &rrerrbase);
312 /* Get the number of screens in display "dpy" and blank them all. */
313 nscreens = ScreenCount(dpy);
314 locks = malloc(sizeof(Lock *) * nscreens);
315 if (locks == NULL)
316 die("slock: malloc: %s\n", strerror(errno));
317 int nlocks = 0;
318 for (screen = 0; screen < nscreens; screen++) {
319 if ( (locks[screen] = lockscreen(dpy, screen)) != NULL)
320 nlocks++;
321 }
322 XSync(dpy, False);
323
324 /* Did we actually manage to lock something? */
325 if (nlocks == 0) { /* nothing to protect */
326 free(locks);
327 XCloseDisplay(dpy);
328 return 1;
329 }
330
331 /* Everything is now blank. Now wait for the correct password. */
332 #ifdef HAVE_BSD_AUTH
333 readpw(dpy);
334 #else
335 readpw(dpy, pws);
336 #endif
337
338 /* Password ok, unlock everything and quit. */
339 for (screen = 0; screen < nscreens; screen++)
340 unlockscreen(dpy, locks[screen]);
341
342 free(locks);
343 XCloseDisplay(dpy);
344
345 return 0;
346 }