aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--slock.c80
1 files changed, 43 insertions, 37 deletions
diff --git a/slock.c b/slock.c
index 164a464..6541b70 100644
--- a/slock.c
+++ b/slock.c
@@ -3,32 +3,55 @@
3 */ 3 */
4#define _XOPEN_SOURCE 500 4#define _XOPEN_SOURCE 500
5 5
6#if HAVE_SHADOW_H
7#include <shadow.h>
8#else
9#include <pwd.h>
10#endif
11
12#include <ctype.h> 6#include <ctype.h>
7#include <pwd.h>
13#include <stdlib.h> 8#include <stdlib.h>
14#include <stdio.h> 9#include <stdio.h>
15#include <string.h> 10#include <string.h>
16#include <unistd.h> 11#include <unistd.h>
12#if HAVE_SHADOW_H
13#include <shadow.h>
14#endif
17#include <sys/types.h> 15#include <sys/types.h>
18#include <X11/keysym.h> 16#include <X11/keysym.h>
19#include <X11/Xlib.h> 17#include <X11/Xlib.h>
20#include <X11/Xutil.h> 18#include <X11/Xutil.h>
21 19
20const char *
21get_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
22int 49int
23main(int argc, char **argv) { 50main(int argc, char **argv) {
24 char curs[] = {0, 0, 0, 0, 0, 0, 0, 0}; 51 char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
25 char buf[32], passwd[256]; 52 char buf[32], passwd[256];
26 int num, screen; 53 int num, screen;
27#if HAVE_SHADOW_H 54 const char *pws;
28 struct spwd *sp;
29#else
30 struct passwd *pw;
31#endif
32 unsigned int len; 55 unsigned int len;
33 Bool running = True; 56 Bool running = True;
34 Cursor invisible; 57 Cursor invisible;
@@ -44,17 +67,7 @@ main(int argc, char **argv) {
44 fputs("slock-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout); 67 fputs("slock-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
45 exit(EXIT_SUCCESS); 68 exit(EXIT_SUCCESS);
46 } 69 }
47 if(geteuid() != 0) { 70 pws = get_password();
48 fputs("slock: cannot retrieve password entry (make sure to suid slock)\n", stderr);
49 exit(EXIT_FAILURE);
50 }
51#if HAVE_SHADOW_H
52 sp = getspnam(getenv("USER"));
53 endspent();
54#else
55 pw = getpwuid(getuid());
56 endpwent();
57#endif
58 if(!(dpy = XOpenDisplay(0))) { 71 if(!(dpy = XOpenDisplay(0))) {
59 fputs("slock: cannot open display\n", stderr); 72 fputs("slock: cannot open display\n", stderr);
60 exit(EXIT_FAILURE); 73 exit(EXIT_FAILURE);
@@ -62,7 +75,7 @@ main(int argc, char **argv) {
62 screen = DefaultScreen(dpy); 75 screen = DefaultScreen(dpy);
63 76
64 /* init */ 77 /* init */
65 passwd[0] = 0; 78 len = 0;
66 79
67 wa.override_redirect = 1; 80 wa.override_redirect = 1;
68 wa.background_pixel = BlackPixel(dpy, screen); 81 wa.background_pixel = BlackPixel(dpy, screen);
@@ -89,7 +102,6 @@ main(int argc, char **argv) {
89 /* main event loop */ 102 /* main event loop */
90 while(running && !XNextEvent(dpy, &ev)) 103 while(running && !XNextEvent(dpy, &ev))
91 if(ev.type == KeyPress) { 104 if(ev.type == KeyPress) {
92 len = strlen(passwd);
93 buf[0] = 0; 105 buf[0] = 0;
94 num = XLookupString(&ev.xkey, buf, sizeof(buf), &ksym, 0); 106 num = XLookupString(&ev.xkey, buf, sizeof(buf), &ksym, 0);
95 if(IsFunctionKey(ksym) || IsKeypadKey(ksym) 107 if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
@@ -98,28 +110,22 @@ main(int argc, char **argv) {
98 continue; 110 continue;
99 switch(ksym) { 111 switch(ksym) {
100 case XK_Return: 112 case XK_Return:
101#if HAVE_SHADOW_H 113 passwd[len] = 0;
102 if((running = strncmp(crypt(passwd, sp->sp_pwdp), sp->sp_pwdp, sizeof(passwd)))) 114 if((running = strcmp(crypt(passwd, pws), pws)) != 0)
103#else
104 if((running = strncmp(crypt(passwd, pw->pw_passwd), pw->pw_passwd, sizeof(passwd))))
105#endif
106 XBell(dpy, 100); 115 XBell(dpy, 100);
107 passwd[0] = 0; 116 len = 0;
108 break; 117 break;
109 case XK_Escape: 118 case XK_Escape:
110 passwd[0] = 0; 119 len = 0;
111 break; 120 break;
112 case XK_BackSpace: 121 case XK_BackSpace:
113 if(len) 122 if(len)
114 passwd[--len] = 0; 123 --len;
115 break; 124 break;
116 default: 125 default:
117 if(num && !iscntrl((int) buf[0])) { 126 if(num && !iscntrl((int) buf[0])) {
118 buf[num] = 0; 127 memcpy(passwd + len,buf,num);
119 if(len) 128 len += num;
120 strncat(passwd, buf, sizeof(passwd));
121 else
122 strncpy(passwd, buf, sizeof(passwd));
123 } 129 }
124 break; 130 break;
125 } 131 }