forked from dannydulai/xl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
92 lines (84 loc) · 2.02 KB
/
main.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
/*
* Danny Dulai
*
* Simple, transparent screen locker in X11
*/
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <crypt.h>
#include <string.h>
#include <pwd.h>
#ifdef NEED_SHADOW
#include <shadow.h>
#endif
int
main(int argc, char *argv[])
{
Display *display;
Window root;
int len = 0, i;
XEvent ev;
char keybuffer[1024], *passwd;
if (argc > 1) {
setenv("XLPASSWD", argv[1], 1);
execl("/proc/self/exe", argv[0], NULL);
fprintf(stderr, "failed to execute self, use XLPASSWD environment\n");
exit(1);
}
else if ((passwd = getenv("XLPASSWD")))
passwd = strdup(crypt(passwd, "ax"));
else {
#ifndef NEED_SHADOW
passwd = getpwuid(getuid())->pw_passwd;
#else
{
struct spwd *sp = getspnam(getpwuid(getuid())->pw_name);
if (sp)
passwd = sp->sp_pwdp;
else {
fprintf(stderr, "could not get shadow entry\n");
exit(1);
}
}
#endif
if (!passwd) {
fprintf(stderr, "could not get passwd\n");
exit(1);
}
}
if (strlen(passwd) < 2) {
fprintf(stderr, "passwd too short\n");
exit(1);
}
if ((display = XOpenDisplay(NULL)) == NULL) {
fprintf(stderr, "could not connect to $DISPLAY\n");
exit(1);
}
root = DefaultRootWindow(display);
XGrabPointer(display, root, 1, ButtonPress, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
XGrabKeyboard(display, root, 0, GrabModeAsync, GrabModeAsync, CurrentTime);
XSelectInput(display, root, KeyPressMask);
while (XNextEvent(display, &ev), 1) {
if (ev.type == KeyPress) {
KeySym keysym;
XComposeStatus compose;
if (len > sizeof(keybuffer)-10)
len = 0;
i = XLookupString(&ev.xkey, keybuffer+len, 10, &keysym, &compose);
if (keysym == XK_Return) {
keybuffer[len] = 0;
if (len && !strcmp(crypt(keybuffer, passwd), passwd)) {
XUngrabKeyboard(display, CurrentTime);
XUngrabPointer(display, CurrentTime);
exit(0);
} else
len = 0;
} else
len += i;
}
}
}