forked from jaseg/Better-XTrLock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxtrlock.c
557 lines (512 loc) · 23.4 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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*
* 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/Xos.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <crypt.h>
#include <ctype.h>
#include <errno.h>
#include <grp.h>
#include <limits.h>
#include <math.h>
#include <pwd.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <getopt.h>
#include <values.h>
#include <libnotify/notify.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
//#include "resources/lock.xbm"
//#include "resources/lock.xbm"
#include "resources/lock_origin.xbm"
#include "resources/lock_origin_mask.xbm"
#ifdef SHADOW_PWD
#include <shadow.h>
#endif
#ifdef DEBUG
#define debug_print(...) \
do { \
printf(__VA_ARGS__); \
} while (0)
#else
#define debug_print(...) \
do { \
} while (0)
#endif
#define msleep(x) usleep(x * 1000)
Display* display;
Window window, blank_window, trans_window;
int total_fail=0;
#define TIMEOUTPERATTEMPT 30000
#define MAXGOODWILL (TIMEOUTPERATTEMPT * 5)
#define INITIALGOODWILL MAXGOODWILL
#define GOODWILLPORTION 0.3
bool blank_screen = false;
bool send_notification = false;
bool keep_cursor=false;
int blink_delay = 100;
struct { /*Setting correspond to the custom passwd setting. --d0048*/
bool enable;
char* pwd;
} cust_pw_setting;
struct passwd* pw;
int passwordok(const char* s)
{
/* simpler, and should work with crypt() algorithms using longer
salt strings (like the md5-based one on freebsd). --marekm */
debug_print("%s, %i\n", s, (int)strlen(s));
if (cust_pw_setting.enable) {
debug_print("Entered_de: %s\n", s);
debug_print("Original_de: %s\n", cust_pw_setting.pwd);
char* enter = crypt(s, cust_pw_setting.pwd);
char* original = cust_pw_setting.pwd;
if (NULL == enter) {
fprintf(stderr, "\"strdup\" or \"crypt\":%s\n", strerror(errno));
return false;
}
debug_print("Entered: %s\n", enter);
debug_print("Original: %s\n", original);
return !strcmp(enter, original);
} else { /*salt in the second argument seems to be automatically used by crypt*/
char* result = crypt(s, pw->pw_passwd);
if (NULL == result) {
fprintf(stderr, "\"strdup\" or \"crypt\":%s\n", strerror(errno));
return false;
}
debug_print("Entered_de: %s\n", result);
debug_print("Original_de: %s\n", pw->pw_passwd);
return !strcmp(result, pw->pw_passwd);
}
}
void print_help()
{
printf("Xtrlock:\n"
" -h --help show this help\n"
" -l --lock-user-password lock immediately with the user's default password\n"
" -p --password [password_string] use custom non-encrypted password\n"
" -e --encrypted-password [password_hash] use encrypted custom password with salt of itself\n"
" -c --calculate [password_string] calculate the password string that can be used with the \"-c\" option\n"
" -b --block-screen lock with a blank screen\n"
" -d --delay of blink [delay_usec] milliseconds the screen blinks on successful locks(0 for no-delay & 100000 for 0.1 s)\n"
" -n --notify send message notification on lock and unlock\n"
" -k --keep-cursor keep the little blue lock cursor icon\n"
"Thanks for using!\n");
}
char rand_ch()
{ /*range of characters in int: [A-Z]|[a-z][0-9](rand % max-min+1+min)*/
/*time based rand refreshes too slow thus not the best choice*/
/*use a-z in this case only*/
return (char)((rand() % (90 - 65 + 1)) + 65);
}
int notify_lock(bool lock){/*0 for unlock, 1 for lock. Function relies on notify-osd*/
notify_init("Xtrlock");
if(lock){/*lock*/
NotifyNotification* nlock = notify_notification_new ("Successfully Locked",
"The screen has been locked",
0);
notify_notification_set_timeout(nlock, 1000); /* 1 second. Seems to be ignored by some severs*/
#ifdef LOCK_IMG_PATH
GError* err = NULL;
GdkPixbuf* pixlock = gdk_pixbuf_new_from_file(LOCK_IMG_PATH, &err);
if(!err && pixlock){
notify_notification_set_image_from_pixbuf(nlock, pixlock);
debug_print("Successfully load image\n");
}
else{
fprintf(stderr,"Failed to read notification icon: ");
*err->message? fprintf(stderr,"%s\n",err->message) : fprintf(stderr,"Nothing to show\n");
g_error_free(err);
}
#endif
if (!notify_notification_show(nlock, 0)){
fprintf(stderr, "Fail to notify\n");
return -1;
}
}else{/*unlock*/
if(total_fail>0){/*with wrong attempts*/
char* tail = " failed attempts";
char* message = malloc(sizeof(char)*2+sizeof(tail));/*never freed, fine in this case*/
snprintf(message, sizeof(char)*2, "%d", total_fail);
strcat(message, tail);
NotifyNotification* nunlock = notify_notification_new ("Successfully Unlocked",
message,
0);
notify_notification_set_timeout(nunlock, 1000);
#ifdef WARN_IMG_PATH
GError* err = NULL;
GdkPixbuf* pixunlock = gdk_pixbuf_new_from_file(WARN_IMG_PATH, &err);
if(!err && pixunlock){
notify_notification_set_image_from_pixbuf(nunlock, pixunlock);
debug_print("Successfully load image\n");
}
else{
fprintf(stderr,"Failed to read notification icon: ");
*err->message? fprintf(stderr,"%s\n",err->message) : fprintf(stderr,"Nothing to show\n");
g_error_free(err);
}
#endif
if (!notify_notification_show(nunlock, 0)){
fprintf(stderr, "Fail to notify\n");
return -1;
}
}else{/*without wrong attempts*/
NotifyNotification* nunlock = notify_notification_new ("Successfully Unlocked",
"The screen has been unlocked",
0);
notify_notification_set_timeout(nunlock, 1000);
#ifdef UNLOCK_IMG_PATH
GError* err = NULL;
GdkPixbuf* pixunlock = gdk_pixbuf_new_from_file(UNLOCK_IMG_PATH, &err);
if(!err && pixunlock){
notify_notification_set_image_from_pixbuf(nunlock, pixunlock);
debug_print("Successfully load image\n");
}
else{
fprintf(stderr,"Failed to read notification icon: ");
*err->message? fprintf(stderr,"%s\n",err->message) : fprintf(stderr,"Nothing to show\n");
g_error_free(err);
}
#endif
if (!notify_notification_show(nunlock, 0)){
fprintf(stderr, "Fail to notify\n");
return -1;
}
}
}
return 0;
}
void onfail(){
total_fail++;
}
int lock()
{
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;
Pixmap csr;
XColor xcolor;
int ret;
static char csr_bits[]= {0x00};
struct timeval tv;
int tvt, gs;
if (!cust_pw_setting.enable) {
debug_print("Sensing user pwd\n");
#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
debug_print("Shadow pwd enabled\n");
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) <= 1) { /*mark as 'x', which means shadow password is enabled.*/
fputs("password entry has no pwd\n", stderr);
debug_print("Found the following at entry:%s|\n", pw->pw_passwd);
debug_print("Found the following at shadow entry:%s|\n", sp->sp_pwdp);
exit(1);
}
debug_print("Passwd sensed:%s\n", pw->pw_passwd);
}
display = XOpenDisplay(0);
if (display == NULL) {
fprintf(stderr, "xtrlock: cannot open display\n");
exit(1);
}
attrib.override_redirect = True;
attrib.background_pixel = BlackPixel(display, DefaultScreen(display));
blank_window = XCreateWindow(display, DefaultRootWindow(display), /*init blank window*/
0, 0, DisplayWidth(display, DefaultScreen(display)),
DisplayHeight(display, DefaultScreen(display)),
0, DefaultDepth(display, DefaultScreen(display)), CopyFromParent, DefaultVisual(display, DefaultScreen(display)),
CWOverrideRedirect | CWBackPixel, &attrib);
trans_window = XCreateWindow(display, DefaultRootWindow(display), /*init window identical to Background*/
0, 0, 1, 1, 0, CopyFromParent, InputOnly, CopyFromParent,
CWOverrideRedirect, &attrib);
window = trans_window;
XSelectInput(display, window, KeyPressMask | KeyReleaseMask);
if(keep_cursor){/*display cursor*/
debug_print("Using keep pointer mode!\n");
csr_source= XCreateBitmapFromData(display,window,lock_origin_bits,lock_origin_width,lock_origin_height);
csr_mask= XCreateBitmapFromData(display,window,lock_origin_mask_bits,lock_origin_mask_width,lock_origin_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);
else{
debug_print("Error allocating color!\n");}
ret = XAllocNamedColor(display,
DefaultColormap(display,DefaultScreen(display)),
"grey25",
&dummy, &csr_fg);
if (ret==0)
XAllocNamedColor(display,
DefaultColormap(display, DefaultScreen(display)),
"white",
&dummy, &csr_bg);
else{
debug_print("Error allocating color!\n");}
cursor= XCreatePixmapCursor(display,csr_source,csr_mask,&csr_fg,&csr_bg,
lock_origin_x_hot,lock_origin_y_hot);
}
else{
csr = XCreateBitmapFromData(display, window, csr_bits, 1, 1);
cursor = XCreatePixmapCursor(display, csr, csr, &xcolor, &xcolor, 1, 1);
}
XMapWindow(display, trans_window);
/*Sometimes the WM doesn't ungrab the keyboard quickly enough if
*launching xtrlock from a keystroke shortcut, meaning xtrlock fails
*to start We deal with this by waiting (up to 100 times) for 10,000
*microsecs and trying to grab each time. If we still fail
*(i.e. after 1s in total), then give up, and emit an error.
*Also, only blank the screen to indicate a success lock.
*/
gs = 0; /*gs==grab successful*/
for (tvt = 0; tvt < 100; tvt++) {
ret = XGrabKeyboard(display, window, False, GrabModeAsync, GrabModeAsync,
CurrentTime);
if (ret == GrabSuccess) {
gs = 1;
break;
}
/*grab failed; wait .01s*/
tv.tv_sec = 0;
tv.tv_usec = 10000;
select(1, NULL, NULL, NULL, &tv);
}
if (gs == 0) {
fprintf(stderr, "xtrlock: cannot grab keyboard\n");
exit(1);
}
if (XGrabPointer(display, window, False, (KeyPressMask | KeyReleaseMask) & 0,
GrabModeAsync, GrabModeAsync, None,
cursor, CurrentTime)
!= GrabSuccess) {
XUngrabKeyboard(display, CurrentTime);
fprintf(stderr, "xtrlock: cannot grab pointer\n");
exit(1);
}
if (!blank_screen && blink_delay != 0) { /*blink to indicate a successful lock*/
XMapWindow(display, blank_window);
XFlush(display);
msleep(blink_delay); /*0.1s as default, or custom value*/
XUnmapWindow(display, blank_window);
debug_print("Unmapped after %i u seconds\n", blink_delay);
XFlush(display);
debug_print("locked and blinked\n");
}
if(blank_screen){
XMapWindow(display, blank_window);
XFlush(display);
}
if(send_notification){
notify_lock(true);
}
XSync(display, False);
printf("Successfully locked\n");
for (;;) { /*start checker loop*/
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;
else
rbuf[rlen] = 0;
if (passwordok(rbuf)){
goto loop_x;
}
else{
onfail();
}
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;
}
} /*end checker loop*/
loop_x: /*loop exit*/
if(send_notification){
notify_lock(false);
}
notify_uninit();
exit(0);
}
int main(int argc, char** argv)
{ /*TODO: Add keeper process*/
/*TODO: Record failed trails -with time*/
/*TODO: add deb/rpm packages*/
/*TODO: add no mouse override mod*/
errno = 0;
bool need_lock = false;
cust_pw_setting.enable = false;
FILE* fp_dev_rand = NULL;
fp_dev_rand = fopen("/dev/urandom", "r");
if (!fp_dev_rand) {
fprintf(stderr, "failed to open /dev/random: %s\n", strerror(errno));
exit(1);
}
unsigned int seed;
fread(&seed, sizeof(int), 1, fp_dev_rand);
debug_print("Read seed from /dev/rand: %u\n", seed);
srand(seed);
fclose(fp_dev_rand);
static struct option long_options[] =
{
{"help", no_argument, NULL, 'h'},
{"password", required_argument, NULL, 'p'},
{"encrypted-password", required_argument, NULL, 'e'},
{"calculate", required_argument, NULL, 'c'},
{"lock-user-password", no_argument, NULL, 'l'},
{"block-screen", no_argument, NULL, 'b'},
{"delay-of-blink", required_argument, NULL, 'd'},
{"notify", no_argument, NULL, 'n'},
{"keep-cursor", no_argument, NULL, 'k'},
{NULL, 0, NULL, 0}
};
char opt = 0;
//while ((opt = getopt(argc, argv, "hp:e:c:lbd:n")) != -1) {
while ((opt = getopt_long(argc, argv, "hp:e:c:lbd:nk", long_options, NULL)) != -1) {
debug_print("Processing args: \"%c|%c\"\n", opt, optopt);
if(255==opt) break; /*supporting getopt on arm devices*/
if ('h' == opt) { /*help(no arg)*/
print_help();
exit(0);
}
if ('p' == opt) { /*custom pwd without encryption*/
char f_salt[3] = { rand_ch(), rand_ch(), '\0' };
debug_print("salt_generated: %s\n", f_salt);
cust_pw_setting.enable = true;
cust_pw_setting.pwd = strdup(crypt(optarg, f_salt)); /*never freed, fine in this case*/
need_lock = true;
if (NULL == cust_pw_setting.pwd) {
fprintf(stderr, "strdup:%s\n", strerror(errno));
exit(-1);
}
}
if ('e' == opt) { /*custom pwd encrypted already*/
cust_pw_setting.enable = true;
cust_pw_setting.pwd = optarg;
need_lock = true;
}
if ('c' == opt) { /*encryption of pwd*/
char f_salt[2] = { rand_ch(), rand_ch() };
printf("%s\n", crypt(optarg, f_salt));
exit(0);
}
if ('l' == opt) { /*lock with user default password after delay(no arg)*/
debug_print("locked immidiently\n");
need_lock = true;
}
if ('b' == opt) { /*lock with a blank screen(no arg)*/
blank_screen = true;
debug_print("blank_screen mode \n");
}
if ('d' == opt) { /*delay of screen blinks*/
blink_delay = atoi(optarg);
if (blink_delay < 0) {
fprintf(stderr, "Delay value not valid\n");
exit(1);
}
}
if('n' == opt){/*send notification*/
send_notification = true;
}
if('k' == opt){/*keep cursor*/
keep_cursor = true;
}
if('?' == opt){
print_help();
exit(1);
}
}
if (need_lock)
lock();
print_help();
exit(1);
}