diff options
Diffstat (limited to 'slock.c')
-rw-r--r-- | slock.c | 34 |
1 files changed, 30 insertions, 4 deletions
@@ -1,4 +1,4 @@ | |||
1 | /* © 2006-2007 Anselm R. Garbe <garbeam at gmail dot com> | 1 | /* © 2006-2008 Anselm R Garbe <garbeam at gmail dot com> |
2 | * See LICENSE file for license details. */ | 2 | * See LICENSE file for license details. */ |
3 | #define _XOPEN_SOURCE 500 | 3 | #define _XOPEN_SOURCE 500 |
4 | #if HAVE_SHADOW_H | 4 | #if HAVE_SHADOW_H |
@@ -16,6 +16,12 @@ | |||
16 | #include <X11/keysym.h> | 16 | #include <X11/keysym.h> |
17 | #include <X11/Xlib.h> | 17 | #include <X11/Xlib.h> |
18 | #include <X11/Xutil.h> | 18 | #include <X11/Xutil.h> |
19 | #include <X11/extensions/dpms.h> | ||
20 | |||
21 | #if HAVE_BSD_AUTH | ||
22 | #include <login_cap.h> | ||
23 | #include <bsd_auth.h> | ||
24 | #endif | ||
19 | 25 | ||
20 | void | 26 | void |
21 | eprint(const char *errstr, ...) { | 27 | eprint(const char *errstr, ...) { |
@@ -27,6 +33,7 @@ eprint(const char *errstr, ...) { | |||
27 | exit(EXIT_FAILURE); | 33 | exit(EXIT_FAILURE); |
28 | } | 34 | } |
29 | 35 | ||
36 | #ifndef HAVE_BSD_AUTH | ||
30 | const char * | 37 | const char * |
31 | get_password() { /* only run as root */ | 38 | get_password() { /* only run as root */ |
32 | const char *rval; | 39 | const char *rval; |
@@ -46,18 +53,23 @@ get_password() { /* only run as root */ | |||
46 | rval = sp->sp_pwdp; | 53 | rval = sp->sp_pwdp; |
47 | } | 54 | } |
48 | #endif | 55 | #endif |
56 | |||
49 | /* drop privileges */ | 57 | /* drop privileges */ |
50 | if(setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0) | 58 | if(setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0) |
51 | eprint("slock: cannot drop privileges\n"); | 59 | eprint("slock: cannot drop privileges\n"); |
52 | return rval; | 60 | return rval; |
53 | } | 61 | } |
62 | #endif | ||
54 | 63 | ||
55 | int | 64 | int |
56 | main(int argc, char **argv) { | 65 | main(int argc, char **argv) { |
57 | char curs[] = {0, 0, 0, 0, 0, 0, 0, 0}; | 66 | char curs[] = {0, 0, 0, 0, 0, 0, 0, 0}; |
58 | char buf[32], passwd[256]; | 67 | char buf[32], passwd[256]; |
59 | int num, screen; | 68 | int num, screen; |
69 | |||
70 | #ifndef HAVE_BSD_AUTH | ||
60 | const char *pws; | 71 | const char *pws; |
72 | #endif | ||
61 | unsigned int len; | 73 | unsigned int len; |
62 | Bool running = True; | 74 | Bool running = True; |
63 | Cursor invisible; | 75 | Cursor invisible; |
@@ -70,10 +82,14 @@ main(int argc, char **argv) { | |||
70 | XSetWindowAttributes wa; | 82 | XSetWindowAttributes wa; |
71 | 83 | ||
72 | if((argc == 2) && !strcmp("-v", argv[1])) | 84 | if((argc == 2) && !strcmp("-v", argv[1])) |
73 | eprint("slock-"VERSION", © 2006-2007 Anselm R. Garbe\n"); | 85 | eprint("slock-"VERSION", © 2006-2008 Anselm R Garbe\n"); |
74 | else if(argc != 1) | 86 | else if(argc != 1) |
75 | eprint("usage: slock [-v]\n"); | 87 | eprint("usage: slock [-v]\n"); |
88 | |||
89 | #ifndef HAVE_BSD_AUTH | ||
76 | pws = get_password(); | 90 | pws = get_password(); |
91 | #endif | ||
92 | |||
77 | if(!(dpy = XOpenDisplay(0))) | 93 | if(!(dpy = XOpenDisplay(0))) |
78 | eprint("slock: cannot open display\n"); | 94 | eprint("slock: cannot open display\n"); |
79 | screen = DefaultScreen(dpy); | 95 | screen = DefaultScreen(dpy); |
@@ -109,7 +125,9 @@ main(int argc, char **argv) { | |||
109 | XSync(dpy, False); | 125 | XSync(dpy, False); |
110 | 126 | ||
111 | /* main event loop */ | 127 | /* main event loop */ |
112 | while(running && !XNextEvent(dpy, &ev)) | 128 | while(running && !XNextEvent(dpy, &ev)) { |
129 | if(len == 0) | ||
130 | DPMSForceLevel(dpy, DPMSModeOff); | ||
113 | if(ev.type == KeyPress) { | 131 | if(ev.type == KeyPress) { |
114 | buf[0] = 0; | 132 | buf[0] = 0; |
115 | num = XLookupString(&ev.xkey, buf, sizeof buf, &ksym, 0); | 133 | num = XLookupString(&ev.xkey, buf, sizeof buf, &ksym, 0); |
@@ -120,7 +138,14 @@ main(int argc, char **argv) { | |||
120 | switch(ksym) { | 138 | switch(ksym) { |
121 | case XK_Return: | 139 | case XK_Return: |
122 | passwd[len] = 0; | 140 | passwd[len] = 0; |
123 | if((running = strcmp(crypt(passwd, pws), pws)) != 0) | 141 | |
142 | #ifdef HAVE_BSD_AUTH | ||
143 | running = !auth_userokay(getlogin(), NULL, "auth-xlock", passwd); | ||
144 | #else | ||
145 | running = strcmp(crypt(passwd, pws), pws); | ||
146 | #endif | ||
147 | |||
148 | if (running != 0) | ||
124 | XBell(dpy, 100); | 149 | XBell(dpy, 100); |
125 | len = 0; | 150 | len = 0; |
126 | break; | 151 | break; |
@@ -139,6 +164,7 @@ main(int argc, char **argv) { | |||
139 | break; | 164 | break; |
140 | } | 165 | } |
141 | } | 166 | } |
167 | } | ||
142 | XUngrabPointer(dpy, CurrentTime); | 168 | XUngrabPointer(dpy, CurrentTime); |
143 | XFreePixmap(dpy, pmap); | 169 | XFreePixmap(dpy, pmap); |
144 | XDestroyWindow(dpy, w); | 170 | XDestroyWindow(dpy, w); |