chiark / gitweb /
changelog for 2.7
[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, black;
84   int ret, screen, blank = 0;
85 #ifdef SHADOW_PWD
86   struct spwd *sp;
87 #endif
88   struct timeval tv;
89   int tvt, gs;
90
91   if ((argc == 2) && (strcmp(argv[1], "-b") == 0)) {
92     blank = 1;
93   } else if (argc > 1) {
94     fprintf(stderr,"xtrlock (version %s); usage: xtrlock [-b]\n",
95             program_version);
96     exit(1);
97   }
98   
99   errno=0;  pw= getpwuid(getuid());
100   if (!pw) { perror("password entry for uid not found"); exit(1); }
101 #ifdef SHADOW_PWD
102   sp = getspnam(pw->pw_name);
103   if (sp)
104     pw->pw_passwd = sp->sp_pwdp;
105   endspent();
106 #endif
107
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); }
115
116   if (strlen(pw->pw_passwd) < 13) {
117     fputs("password entry has no pwd\n",stderr); exit(1);
118   }
119   
120   display= XOpenDisplay(0);
121
122   if (display==NULL) {
123     fprintf(stderr,"xtrlock (version %s): cannot open display\n",
124             program_version);
125     exit(1);
126   }
127   
128   attrib.override_redirect= True;
129
130   if (blank) {
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);
138   } else {
139     window= XCreateWindow(display,DefaultRootWindow(display),
140                           0,0,1,1,0,CopyFromParent,InputOnly,CopyFromParent,
141                           CWOverrideRedirect,&attrib);
142   }
143                         
144   XSelectInput(display,window,KeyPressMask|KeyReleaseMask);
145
146   csr_source= XCreateBitmapFromData(display,window,lock_bits,lock_width,lock_height);
147   csr_mask= XCreateBitmapFromData(display,window,mask_bits,mask_width,mask_height);
148
149   ret = XAllocNamedColor(display,
150                         DefaultColormap(display, DefaultScreen(display)),
151                         "steelblue3",
152                         &dummy, &csr_bg);
153   if (ret==0)
154     XAllocNamedColor(display,
155                     DefaultColormap(display, DefaultScreen(display)),
156                     "black",
157                     &dummy, &csr_bg);
158
159   ret = XAllocNamedColor(display,
160                         DefaultColormap(display,DefaultScreen(display)),
161                         "grey25",
162                         &dummy, &csr_fg);
163   if (ret==0)
164     XAllocNamedColor(display,
165                     DefaultColormap(display, DefaultScreen(display)),
166                     "white",
167                     &dummy, &csr_bg);
168
169
170
171   cursor= XCreatePixmapCursor(display,csr_source,csr_mask,&csr_fg,&csr_bg,
172                               lock_x_hot,lock_y_hot);
173
174   XMapWindow(display,window);
175
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
181    */
182   
183   gs=0; /*gs==grab successful*/
184   for (tvt=0 ; tvt<100; tvt++) {
185     ret = XGrabKeyboard(display,window,False,GrabModeAsync,GrabModeAsync,
186                         CurrentTime);
187     if (ret == GrabSuccess) {
188       gs=1;
189       break;
190     }
191     /*grab failed; wait .01s*/
192     tv.tv_sec=0;
193     tv.tv_usec=10000;
194     select(1,NULL,NULL,NULL,&tv);
195   }
196   if (gs==0){
197     fprintf(stderr,"xtrlock (version %s): cannot grab keyboard\n",
198             program_version);
199     exit(1);
200   }
201
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",
207             program_version);
208     exit(1);
209   }
210
211   for (;;) {
212     XNextEvent(display,&ev);
213     switch (ev.type) {
214     case KeyPress:
215       if (ev.xkey.time < timeout) { XBell(display,0); break; }
216       clen= XLookupString(&ev.xkey,cbuf,9,&ks,0);
217       switch (ks) {
218       case XK_Escape: case XK_Clear:
219         rlen=0; break;
220       case XK_Delete: case XK_BackSpace:
221         if (rlen>0) rlen--;
222         break;
223       case XK_Linefeed: case XK_Return:
224         if (rlen==0) break;
225         rbuf[rlen]=0;
226         if (passwordok(rbuf)) goto loop_x;
227         XBell(display,0);
228         rlen= 0;
229         if (timeout) {
230           goodwill+= ev.xkey.time - timeout;
231           if (goodwill > MAXGOODWILL) {
232             goodwill= MAXGOODWILL;
233           }
234         }
235         timeout= -goodwill*GOODWILLPORTION;
236         goodwill+= timeout;
237         timeout+= ev.xkey.time + TIMEOUTPERATTEMPT;
238         break;
239       default:
240         if (clen != 1) break;
241         /* allow space for the trailing \0 */
242         if (rlen < (sizeof(rbuf) - 1)){
243           rbuf[rlen]=cbuf[0];
244           rlen++;
245         }
246         break;
247       }
248       break;
249     default:
250       break;
251     }
252   }
253  loop_x:
254   exit(0);
255 }