6 * Copyright (C)1993,1994 Ian Jackson
8 * This is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
13 * This is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
21 #include <X11/Xutil.h>
22 #include <X11/keysym.h>
27 #include <sys/types.h>
44 #include "lock.bitmap"
45 #include "mask.bitmap"
46 #include "patchlevel.h"
51 #define TIMEOUTPERATTEMPT 30000
52 #define MAXGOODWILL (TIMEOUTPERATTEMPT*5)
53 #define INITIALGOODWILL MAXGOODWILL
54 #define GOODWILLPORTION 0.3
57 int passwordok(const char *s) {
62 key[0] = *(pw->pw_passwd);
63 key[1] = (pw->pw_passwd)[1];
66 return !strcmp(encr, pw->pw_passwd);
68 /* simpler, and should work with crypt() algorithms using longer
69 salt strings (like the md5-based one on freebsd). --marekm */
70 return !strcmp(crypt(s, pw->pw_passwd), pw->pw_passwd);
74 int main(int argc, char **argv){
77 char cbuf[10], rbuf[128]; /* shadow appears to suggest 127 a good value here */
79 long goodwill= INITIALGOODWILL, timeout= 0;
80 XSetWindowAttributes attrib;
82 Pixmap csr_source,csr_mask;
83 XColor csr_fg, csr_bg, dummy, black;
84 int ret, screen, blank = 0;
91 if ((argc == 2) && (strcmp(argv[1], "-b") == 0)) {
93 } else if (argc > 1) {
94 fprintf(stderr,"xtrlock (version %s); usage: xtrlock [-b]\n",
99 errno=0; pw= getpwuid(getuid());
100 if (!pw) { perror("password entry for uid not found"); exit(1); }
102 sp = getspnam(pw->pw_name);
104 pw->pw_passwd = sp->sp_pwdp;
108 /* logically, if we need to do the following then the same
109 applies to being installed setgid shadow.
110 we do this first, because of a bug in linux. --jdamery */
111 if (setgid(getgid())) { perror("setgid"); exit(1); }
112 /* we can be installed setuid root to support shadow passwords,
113 and we don't need root privileges any longer. --marekm */
114 if (setuid(getuid())) { perror("setuid"); exit(1); }
116 if (strlen(pw->pw_passwd) < 13) {
117 fputs("password entry has no pwd\n",stderr); exit(1);
120 display= XOpenDisplay(0);
123 fprintf(stderr,"xtrlock (version %s): cannot open display\n",
128 attrib.override_redirect= True;
131 screen = DefaultScreen(display);
132 attrib.background_pixel = BlackPixel(display, screen);
133 window= XCreateWindow(display,DefaultRootWindow(display),
134 0,0,DisplayWidth(display, screen),DisplayHeight(display, screen),
135 0,DefaultDepth(display, screen), CopyFromParent, DefaultVisual(display, screen),
136 CWOverrideRedirect|CWBackPixel,&attrib);
137 XAllocNamedColor(display, DefaultColormap(display, screen), "black", &black, &dummy);
139 window= XCreateWindow(display,DefaultRootWindow(display),
140 0,0,1,1,0,CopyFromParent,InputOnly,CopyFromParent,
141 CWOverrideRedirect,&attrib);
144 XSelectInput(display,window,KeyPressMask|KeyReleaseMask);
146 csr_source= XCreateBitmapFromData(display,window,lock_bits,lock_width,lock_height);
147 csr_mask= XCreateBitmapFromData(display,window,mask_bits,mask_width,mask_height);
149 ret = XAllocNamedColor(display,
150 DefaultColormap(display, DefaultScreen(display)),
154 XAllocNamedColor(display,
155 DefaultColormap(display, DefaultScreen(display)),
159 ret = XAllocNamedColor(display,
160 DefaultColormap(display,DefaultScreen(display)),
164 XAllocNamedColor(display,
165 DefaultColormap(display, DefaultScreen(display)),
171 cursor= XCreatePixmapCursor(display,csr_source,csr_mask,&csr_fg,&csr_bg,
172 lock_x_hot,lock_y_hot);
174 XMapWindow(display,window);
176 /*Sometimes the WM doesn't ungrab the keyboard quickly enough if
177 *launching xtrlock from a keystroke shortcut, meaning xtrlock fails
178 *to start We deal with this by waiting (up to 100 times) for 10,000
179 *microsecs and trying to grab each time. If we still fail
180 *(i.e. after 1s in total), then give up, and emit an error
183 gs=0; /*gs==grab successful*/
184 for (tvt=0 ; tvt<100; tvt++) {
185 ret = XGrabKeyboard(display,window,False,GrabModeAsync,GrabModeAsync,
187 if (ret == GrabSuccess) {
191 /*grab failed; wait .01s*/
194 select(1,NULL,NULL,NULL,&tv);
197 fprintf(stderr,"xtrlock (version %s): cannot grab keyboard\n",
202 if (XGrabPointer(display,window,False,(KeyPressMask|KeyReleaseMask)&0,
203 GrabModeAsync,GrabModeAsync,None,
204 cursor,CurrentTime)!=GrabSuccess) {
205 XUngrabKeyboard(display,CurrentTime);
206 fprintf(stderr,"xtrlock (version %s): cannot grab pointer\n",
212 XNextEvent(display,&ev);
215 if (ev.xkey.time < timeout) { XBell(display,0); break; }
216 clen= XLookupString(&ev.xkey,cbuf,9,&ks,0);
218 case XK_Escape: case XK_Clear:
220 case XK_Delete: case XK_BackSpace:
223 case XK_Linefeed: case XK_Return:
226 if (passwordok(rbuf)) goto loop_x;
230 goodwill+= ev.xkey.time - timeout;
231 if (goodwill > MAXGOODWILL) {
232 goodwill= MAXGOODWILL;
235 timeout= -goodwill*GOODWILLPORTION;
237 timeout+= ev.xkey.time + TIMEOUTPERATTEMPT;
240 if (clen != 1) break;
241 /* allow space for the trailing \0 */
242 if (rlen < (sizeof(rbuf) - 1)){