forked from benruijl/sflock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsflock.c
310 lines (265 loc) · 8.71 KB
/
sflock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* See LICENSE file for license details. */
#define _XOPEN_SOURCE 500
#if HAVE_SHADOW_H
#include <shadow.h>
#endif
#include <ctype.h>
#include <pwd.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <linux/vt.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <X11/keysym.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/dpms.h>
#if HAVE_BSD_AUTH
#include <login_cap.h>
#include <bsd_auth.h>
#endif
static void
die(const char *errstr, ...) {
va_list ap;
va_start(ap, errstr);
vfprintf(stderr, errstr, ap);
va_end(ap);
exit(EXIT_FAILURE);
}
#ifndef HAVE_BSD_AUTH
static const char *
get_password() { /* only run as root */
const char *rval;
struct passwd *pw;
if(geteuid() != 0)
die("sflock: cannot retrieve password entry (make sure to suid sflock)\n");
pw = getpwuid(getuid());
endpwent();
rval = pw->pw_passwd;
#if HAVE_SHADOW_H
{
struct spwd *sp;
sp = getspnam(getenv("USER"));
endspent();
rval = sp->sp_pwdp;
}
#endif
/* drop privileges temporarily */
if (setreuid(0, pw->pw_uid) == -1)
die("sflock: cannot drop privileges\n");
return rval;
}
#endif
int
main(int argc, char **argv) {
char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
char buf[32], passwd[256], passdisp[256];
int num, screen, width, height, update, sleepmode, term, pid;
#ifndef HAVE_BSD_AUTH
const char *pws;
#endif
unsigned int len;
Bool running = True;
Cursor invisible;
Display *dpy;
KeySym ksym;
Pixmap pmap;
Window root, w;
XColor black, red, dummy;
XEvent ev;
XSetWindowAttributes wa;
XFontStruct* font;
GC gc;
XGCValues values;
// defaults
char* passchar = "*";
char* fontname = "-bitstream-terminal-medium-r-normal--18-140-100-100-c-110-iso8859-1-";
char* username = "";
int showline = 1;
int xshift = 0;
for (int i = 0; i < argc; i++) {
if (!strcmp(argv[i], "-c")) {
if (i + 1 < argc)
passchar = argv[i + 1];
else
die("error: no password character given.\n");
} else
if (!strcmp(argv[i], "-f")) {
if (i + 1 < argc)
fontname = argv[i + 1];
else
die("error: font not specified.\n");
}
else
if (!strcmp(argv[i], "-v"))
die("sflock-"VERSION", © 2015 Ben Ruijl\n");
else
if (!strcmp(argv[i], "-h"))
showline = 0;
else
if (!strcmp(argv[i], "-xshift")) {
if (i+1 == argc)
die("error: missing xshift value\n");
xshift = atoi(argv[i + 1]);
}
else
if (!strcmp(argv[i], "?"))
die("usage: sflock [-v] [-c passchars] [-f fontname] [-xshift horizontal shift]\n");
}
// fill with password characters
for (int i = 0; i < sizeof passdisp; i+= strlen(passchar))
for (int j = 0; j < strlen(passchar) && i + j < sizeof passdisp; j++)
passdisp[i + j] = passchar[j];
/* disable tty switching */
if ((term = open("/dev/console", O_RDWR)) == -1) {
perror("error opening console");
}
if ((ioctl(term, VT_LOCKSWITCH)) == -1) {
perror("error locking console");
}
/* deamonize */
pid = fork();
if (pid < 0)
die("Could not fork sflock.");
if (pid > 0)
exit(0); // exit parent
#ifndef HAVE_BSD_AUTH
pws = get_password();
username = getpwuid(geteuid())->pw_name;
#else
username = getlogin();
#endif
if(!(dpy = XOpenDisplay(0)))
die("sflock: cannot open dpy\n");
screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen);
width = DisplayWidth(dpy, screen);
height = DisplayHeight(dpy, screen);
wa.override_redirect = 1;
wa.background_pixel = XBlackPixel(dpy, screen);
w = XCreateWindow(dpy, root, 0, 0, width, height,
0, DefaultDepth(dpy, screen), CopyFromParent,
DefaultVisual(dpy, screen), CWOverrideRedirect | CWBackPixel, &wa);
XAllocNamedColor(dpy, DefaultColormap(dpy, screen), "orange red", &red, &dummy);
XAllocNamedColor(dpy, DefaultColormap(dpy, screen), "black", &black, &dummy);
pmap = XCreateBitmapFromData(dpy, w, curs, 8, 8);
invisible = XCreatePixmapCursor(dpy, pmap, pmap, &black, &black, 0, 0);
XDefineCursor(dpy, w, invisible);
XMapRaised(dpy, w);
font = XLoadQueryFont(dpy, fontname);
if (font == 0) {
die("error: could not find font. Try using a full description.\n");
}
gc = XCreateGC(dpy, w, (unsigned long)0, &values);
XSetFont(dpy, gc, font->fid);
XSetForeground(dpy, gc, XWhitePixel(dpy, screen));
for(len = 1000; len; len--) {
if(XGrabPointer(dpy, root, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
break;
usleep(1000);
}
if((running = running && (len > 0))) {
for(len = 1000; len; len--) {
if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
== GrabSuccess)
break;
usleep(1000);
}
running = (len > 0);
}
len = 0;
XSync(dpy, False);
update = True;
sleepmode = False;
/* main event loop */
while(running && !XNextEvent(dpy, &ev)) {
if (sleepmode) {
DPMSEnable(dpy);
DPMSForceLevel(dpy, DPMSModeOff);
XFlush(dpy);
}
if (update) {
int x, y, dir, ascent, descent;
XCharStruct overall;
XClearWindow(dpy, w);
XTextExtents (font, passdisp, len, &dir, &ascent, &descent, &overall);
x = (width - overall.width) / 2;
y = (height + ascent - descent) / 2;
XDrawString(dpy,w,gc, (width - XTextWidth(font, username, strlen(username))) / 2 + xshift, y - ascent - 20, username, strlen(username));
if (showline)
XDrawLine(dpy, w, gc, width * 3 / 8 + xshift, y - ascent - 10, width * 5 / 8 + xshift, y - ascent - 10);
XDrawString(dpy,w,gc, x + xshift, y, passdisp, len);
update = False;
}
if (ev.type == MotionNotify) {
sleepmode = False;
}
if(ev.type == KeyPress) {
sleepmode = False;
buf[0] = 0;
num = XLookupString(&ev.xkey, buf, sizeof buf, &ksym, 0);
if(IsKeypadKey(ksym)) {
if(ksym == XK_KP_Enter)
ksym = XK_Return;
else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
ksym = (ksym - XK_KP_0) + XK_0;
}
if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
|| IsMiscFunctionKey(ksym) || IsPFKey(ksym)
|| IsPrivateKeypadKey(ksym))
continue;
switch(ksym) {
case XK_Return:
passwd[len] = 0;
#ifdef HAVE_BSD_AUTH
running = !auth_userokay(getlogin(), NULL, "auth-xlock", passwd);
#else
running = strcmp(crypt(passwd, pws), pws);
#endif
if (running != 0)
// change background on wrong password
XSetWindowBackground(dpy, w, red.pixel);
len = 0;
break;
case XK_Escape:
len = 0;
if (DPMSCapable(dpy)) {
sleepmode = True;
}
break;
case XK_BackSpace:
if(len)
--len;
break;
default:
if(num && !iscntrl((int) buf[0]) && (len + num < sizeof passwd)) {
memcpy(passwd + len, buf, num);
len += num;
}
break;
}
update = True; // show changes
}
}
/* free and unlock */
setreuid(geteuid(), 0);
if ((ioctl(term, VT_UNLOCKSWITCH)) == -1) {
perror("error unlocking console");
}
close(term);
setuid(getuid()); // drop rights permanently
XUngrabPointer(dpy, CurrentTime);
XFreePixmap(dpy, pmap);
XFreeFont(dpy, font);
XFreeGC(dpy, gc);
XDestroyWindow(dpy, w);
XCloseDisplay(dpy);
return 0;
}