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