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;
92 fprintf(stderr,"xtrlock (version %s): no arguments allowed\n",program_version);
96 errno=0; pw= getpwuid(getuid());
97 if (!pw) { perror("password entry for uid not found"); exit(1); }
99 sp = getspnam(pw->pw_name);
101 pw->pw_passwd = sp->sp_pwdp;
105 /* logically, if we need to do the following then the same
106 applies to being installed setgid shadow.
107 we do this first, because of a bug in linux. --jdamery */
109 /* we can be installed setuid root to support shadow passwords,
110 and we don't need root privileges any longer. --marekm */
113 if (strlen(pw->pw_passwd) < 13) {
114 fputs("password entry has no pwd\n",stderr); exit(1);
117 display= XOpenDisplay(0);
120 fprintf(stderr,"xtrlock (version %s): cannot open display\n",
125 attrib.override_redirect= True;
126 window= XCreateWindow(display,DefaultRootWindow(display),
127 0,0,1,1,0,CopyFromParent,InputOnly,CopyFromParent,
128 CWOverrideRedirect,&attrib);
130 XSelectInput(display,window,KeyPressMask|KeyReleaseMask);
132 csr_source= XCreateBitmapFromData(display,window,lock_bits,lock_width,lock_height);
133 csr_mask= XCreateBitmapFromData(display,window,mask_bits,mask_width,mask_height);
135 ret = XAllocNamedColor(display,
136 DefaultColormap(display, DefaultScreen(display)),
140 XAllocNamedColor(display,
141 DefaultColormap(display, DefaultScreen(display)),
145 ret = XAllocNamedColor(display,
146 DefaultColormap(display,DefaultScreen(display)),
150 XAllocNamedColor(display,
151 DefaultColormap(display, DefaultScreen(display)),
157 cursor= XCreatePixmapCursor(display,csr_source,csr_mask,&csr_fg,&csr_bg,
158 lock_x_hot,lock_y_hot);
160 XMapWindow(display,window);
162 /*Sometimes the WM doesn't ungrab the keyboard quickly enough if
163 *launching xtrlock from a keystroke shortcut, meaning xtrlock fails
164 *to start We deal with this by waiting (up to 100 times) for 10,000
165 *microsecs and trying to grab each time. If we still fail
166 *(i.e. after 1s in total), then give up, and emit an error
169 gs=0; /*gs==grab successful*/
170 for (tvt=0 ; tvt<100; tvt++) {
171 ret = XGrabKeyboard(display,window,False,GrabModeAsync,GrabModeAsync,
173 if (ret == GrabSuccess) {
177 /*grab failed; wait .01s*/
180 select(1,NULL,NULL,NULL,&tv);
183 fprintf(stderr,"xtrlock (version %s): cannot grab keyboard\n",
188 if (XGrabPointer(display,window,False,(KeyPressMask|KeyReleaseMask)&0,
189 GrabModeAsync,GrabModeAsync,None,
190 cursor,CurrentTime)!=GrabSuccess) {
191 XUngrabKeyboard(display,CurrentTime);
192 fprintf(stderr,"xtrlock (version %s): cannot grab pointer\n",
198 XNextEvent(display,&ev);
201 if (ev.xkey.time < timeout) { XBell(display,0); break; }
202 clen= XLookupString(&ev.xkey,cbuf,9,&ks,0);
204 case XK_Escape: case XK_Clear:
206 case XK_Delete: case XK_BackSpace:
209 case XK_Linefeed: case XK_Return:
212 if (passwordok(rbuf)) goto loop_x;
216 goodwill+= ev.xkey.time - timeout;
217 if (goodwill > MAXGOODWILL) {
218 goodwill= MAXGOODWILL;
221 timeout= -goodwill*GOODWILLPORTION;
223 timeout+= ev.xkey.time + TIMEOUTPERATTEMPT;
226 if (clen != 1) break;
227 /* allow space for the trailing \0 */
228 if (rlen < (sizeof(rbuf) - 1)){