chiark / gitweb /
Commit version 2.3 to git
[xtrlock.git] / xtrlock.c
1 /*
2  * xtrlock.c
3  *
4  * X Transparent Lock
5  *
6  * Copyright (C)1993,1994 Ian Jackson
7  *
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)
11  * any later version.
12  *
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.
17  */
18
19 #include <X11/X.h>
20 #include <X11/Xlib.h>
21 #include <X11/Xutil.h>
22 #include <X11/keysym.h>
23 #include <X11/Xos.h>
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <errno.h>
30 #include <pwd.h>
31 #include <grp.h>
32 #include <limits.h>
33 #include <string.h>
34 #include <crypt.h>
35 #include <unistd.h>
36 #include <math.h>
37 #include <ctype.h>
38 #include <values.h>
39
40 #ifdef SHADOW_PWD
41 #include <shadow.h>
42 #endif
43
44 #include "lock.bitmap"
45 #include "mask.bitmap"
46 #include "patchlevel.h"
47
48 Display *display;
49 Window window, root;
50
51 #define TIMEOUTPERATTEMPT 30000
52 #define MAXGOODWILL  (TIMEOUTPERATTEMPT*5)
53 #define INITIALGOODWILL MAXGOODWILL
54 #define GOODWILLPORTION 0.3
55
56 struct passwd *pw;
57 int passwordok(const char *s) {
58 #if 0
59   char key[3];
60   char *encr;
61   
62   key[0] = *(pw->pw_passwd);
63   key[1] =  (pw->pw_passwd)[1];
64   key[2] =  0;
65   encr = crypt(s, key);
66   return !strcmp(encr, pw->pw_passwd);
67 #else
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);
71 #endif
72 }
73
74 int main(int argc, char **argv){
75   XEvent ev;
76   KeySym ks;
77   char cbuf[10], rbuf[128]; /* shadow appears to suggest 127 a good value here */
78   int clen, rlen=0;
79   long goodwill= INITIALGOODWILL, timeout= 0;
80   XSetWindowAttributes attrib;
81   Cursor cursor;
82   Pixmap csr_source,csr_mask;
83   XColor csr_fg, csr_bg, dummy;
84   int ret;
85 #ifdef SHADOW_PWD
86   struct spwd *sp;
87 #endif
88   struct timeval tv;
89   int tvt, gs;
90
91   if (argc != 1) {
92     fprintf(stderr,"xtrlock (version %s): no arguments allowed\n",program_version);
93     exit(1);
94   }
95   
96   errno=0;  pw= getpwuid(getuid());
97   if (!pw) { perror("password entry for uid not found"); exit(1); }
98 #ifdef SHADOW_PWD
99   sp = getspnam(pw->pw_name);
100   if (sp)
101     pw->pw_passwd = sp->sp_pwdp;
102   endspent();
103 #endif
104
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 */ 
108   setgid(getgid());
109   /* we can be installed setuid root to support shadow passwords,
110      and we don't need root privileges any longer.  --marekm */
111   setuid(getuid());
112
113   if (strlen(pw->pw_passwd) < 13) {
114     fputs("password entry has no pwd\n",stderr); exit(1);
115   }
116   
117   display= XOpenDisplay(0);
118
119   if (display==NULL) {
120     fprintf(stderr,"xtrlock (version %s): cannot open display\n",
121             program_version);
122     exit(1);
123   }
124
125   attrib.override_redirect= True;
126   window= XCreateWindow(display,DefaultRootWindow(display),
127                         0,0,1,1,0,CopyFromParent,InputOnly,CopyFromParent,
128                         CWOverrideRedirect,&attrib);
129                         
130   XSelectInput(display,window,KeyPressMask|KeyReleaseMask);
131
132   csr_source= XCreateBitmapFromData(display,window,lock_bits,lock_width,lock_height);
133   csr_mask= XCreateBitmapFromData(display,window,mask_bits,mask_width,mask_height);
134
135   ret = XAllocNamedColor(display,
136                         DefaultColormap(display, DefaultScreen(display)),
137                         "steelblue3",
138                         &dummy, &csr_bg);
139   if (ret==0)
140     XAllocNamedColor(display,
141                     DefaultColormap(display, DefaultScreen(display)),
142                     "black",
143                     &dummy, &csr_bg);
144
145   ret = XAllocNamedColor(display,
146                         DefaultColormap(display,DefaultScreen(display)),
147                         "grey25",
148                         &dummy, &csr_fg);
149   if (ret==0)
150     XAllocNamedColor(display,
151                     DefaultColormap(display, DefaultScreen(display)),
152                     "white",
153                     &dummy, &csr_bg);
154
155
156
157   cursor= XCreatePixmapCursor(display,csr_source,csr_mask,&csr_fg,&csr_bg,
158                               lock_x_hot,lock_y_hot);
159
160   XMapWindow(display,window);
161
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
167    */
168   
169   gs=0; /*gs==grab successful*/
170   for (tvt=0 ; tvt<100; tvt++) {
171     ret = XGrabKeyboard(display,window,False,GrabModeAsync,GrabModeAsync,
172                         CurrentTime);
173     if (ret == GrabSuccess) {
174       gs=1;
175       break;
176     }
177     /*grab failed; wait .01s*/
178     tv.tv_sec=0;
179     tv.tv_usec=10000;
180     select(1,NULL,NULL,NULL,&tv);
181   }
182   if (gs==0){
183     fprintf(stderr,"xtrlock (version %s): cannot grab keyboard\n",
184             program_version);
185     exit(1);
186   }
187
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",
193             program_version);
194     exit(1);
195   }
196
197   for (;;) {
198     XNextEvent(display,&ev);
199     switch (ev.type) {
200     case KeyPress:
201       if (ev.xkey.time < timeout) { XBell(display,0); break; }
202       clen= XLookupString(&ev.xkey,cbuf,9,&ks,0);
203       switch (ks) {
204       case XK_Escape: case XK_Clear:
205         rlen=0; break;
206       case XK_Delete: case XK_BackSpace:
207         if (rlen>0) rlen--;
208         break;
209       case XK_Linefeed: case XK_Return:
210         if (rlen==0) break;
211         rbuf[rlen]=0;
212         if (passwordok(rbuf)) goto loop_x;
213         XBell(display,0);
214         rlen= 0;
215         if (timeout) {
216           goodwill+= ev.xkey.time - timeout;
217           if (goodwill > MAXGOODWILL) {
218             goodwill= MAXGOODWILL;
219           }
220         }
221         timeout= -goodwill*GOODWILLPORTION;
222         goodwill+= timeout;
223         timeout+= ev.xkey.time + TIMEOUTPERATTEMPT;
224         break;
225       default:
226         if (clen != 1) break;
227         /* allow space for the trailing \0 */
228         if (rlen < (sizeof(rbuf) - 1)){
229           rbuf[rlen]=cbuf[0];
230           rlen++;
231         }
232         break;
233       }
234       break;
235     default:
236       break;
237     }
238   }
239  loop_x:
240   exit(0);
241 }