forked from jaseg/Better-XTrLock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xtrlock.c
221 lines (195 loc) · 5.33 KB
/
xtrlock.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
/*
* xtrlock.c
*
* X Transparent Lock
*
* Copyright (C)1993,1994 Ian Jackson
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <X11/Xos.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <pwd.h>
#include <grp.h>
#include <limits.h>
#include <string.h>
#include <crypt.h>
#include <unistd.h>
#include <math.h>
#include <ctype.h>
#include <values.h>
#ifdef SHADOW_PWD
#include <shadow.h>
#endif
#include "lock.bitmap"
#include "mask.bitmap"
#include "patchlevel.h"
Display *display;
Window window, root;
#define TIMEOUTPERATTEMPT 30000
#define MAXGOODWILL (TIMEOUTPERATTEMPT*5)
#define INITIALGOODWILL MAXGOODWILL
#define GOODWILLPORTION 0.3
struct passwd *pw;
int passwordok(const char *s) {
#if 0
char key[3];
char *encr;
key[0] = *(pw->pw_passwd);
key[1] = (pw->pw_passwd)[1];
key[2] = 0;
encr = crypt(s, key);
return !strcmp(encr, pw->pw_passwd);
#else
/* simpler, and should work with crypt() algorithms using longer
salt strings (like the md5-based one on freebsd). --marekm */
return !strcmp(crypt(s, pw->pw_passwd), pw->pw_passwd);
#endif
}
int main(int argc, char **argv){
if (argc != 1) {
fprintf(stderr,"xtrlock (version %s): no arguments allowed\n",program_version);
exit(1);
}
if(!fork()){
XEvent ev;
KeySym ks;
char cbuf[10], rbuf[128]; /* shadow appears to suggest 127 a good value here */
int clen, rlen=0;
long goodwill= INITIALGOODWILL, timeout= 0;
XSetWindowAttributes attrib;
Cursor cursor;
Pixmap csr_source,csr_mask;
XColor csr_fg, csr_bg, dummy;
int ret;
#ifdef SHADOW_PWD
struct spwd *sp;
#endif
errno=0; pw= getpwuid(getuid());
if (!pw) { perror("password entry for uid not found"); exit(1); }
#ifdef SHADOW_PWD
sp = getspnam(pw->pw_name);
if (sp)
pw->pw_passwd = sp->sp_pwdp;
endspent();
#endif
/* logically, if we need to do the following then the same
applies to being installed setgid shadow.
we do this first, because of a bug in linux. --jdamery */
setgid(getgid());
/* we can be installed setuid root to support shadow passwords,
and we don't need root privileges any longer. --marekm */
setuid(getuid());
if (strlen(pw->pw_passwd) < 13) {
printf("%s\n", pw->pw_passwd);
fputs("password entry has no pwd\n",stderr); exit(1);
}
display= XOpenDisplay(0);
if (display==NULL) {
fprintf(stderr,"xtrlock (version %s): cannot open display\n",
program_version);
exit(1);
}
attrib.override_redirect= True;
window= XCreateWindow(display,DefaultRootWindow(display),
0,0,1,1,0,CopyFromParent,InputOnly,CopyFromParent,
CWOverrideRedirect,&attrib);
XSelectInput(display,window,KeyPressMask|KeyReleaseMask);
csr_source= XCreateBitmapFromData(display,window,lock_bits,lock_width,lock_height);
csr_mask= XCreateBitmapFromData(display,window,mask_bits,mask_width,mask_height);
ret = XAllocNamedColor(display,
DefaultColormap(display, DefaultScreen(display)),
"steelblue3",
&dummy, &csr_bg);
if (ret==0)
XAllocNamedColor(display,
DefaultColormap(display, DefaultScreen(display)),
"black",
&dummy, &csr_bg);
ret = XAllocNamedColor(display,
DefaultColormap(display,DefaultScreen(display)),
"grey25",
&dummy, &csr_fg);
if (ret==0)
XAllocNamedColor(display,
DefaultColormap(display, DefaultScreen(display)),
"white",
&dummy, &csr_bg);
cursor= XCreatePixmapCursor(display,csr_source,csr_mask,&csr_fg,&csr_bg,
lock_x_hot,lock_y_hot);
XMapWindow(display,window);
if (XGrabKeyboard(display,window,False,GrabModeAsync,GrabModeAsync,
CurrentTime)!=GrabSuccess) {
exit(1);
}
if (XGrabPointer(display,window,False,(KeyPressMask|KeyReleaseMask)&0,
GrabModeAsync,GrabModeAsync,None,
cursor,CurrentTime)!=GrabSuccess) {
XUngrabKeyboard(display,CurrentTime);
exit(1);
}
for (;;) {
XNextEvent(display,&ev);
switch (ev.type) {
case KeyPress:
if (ev.xkey.time < timeout) { XBell(display,0); break; }
clen= XLookupString(&ev.xkey,cbuf,9,&ks,0);
switch (ks) {
case XK_Escape: case XK_Clear:
rlen=0; break;
case XK_Delete: case XK_BackSpace:
if (rlen>0) rlen--;
break;
case XK_Linefeed: case XK_Return:
if (rlen==0) break;
rbuf[rlen]=0;
if (passwordok(rbuf)) goto loop_x;
XBell(display,0);
rlen= 0;
if (timeout) {
goodwill+= ev.xkey.time - timeout;
if (goodwill > MAXGOODWILL) {
goodwill= MAXGOODWILL;
}
}
timeout= -goodwill*GOODWILLPORTION;
goodwill+= timeout;
timeout+= ev.xkey.time + TIMEOUTPERATTEMPT;
break;
default:
if (clen != 1) break;
/* allow space for the trailing \0 */
if (rlen < (sizeof(rbuf) - 1)){
rbuf[rlen]=cbuf[0];
rlen++;
}
break;
}
break;
default:
break;
}
}
loop_x:
exit(0);
}else{
system("clear");
}
exit(0);
}