http://www.tienza.es/crux/src/www.jwz.org/xscreensaver/xscreensaver-5.05.tar.gz
[xscreensaver] / driver / test-passwd.c
1 /* xscreensaver, Copyright (c) 1998-2007 Jamie Zawinski <jwz@jwz.org>
2  *
3  * Permission to use, copy, modify, distribute, and sell this software and its
4  * documentation for any purpose is hereby granted without fee, provided that
5  * the above copyright notice appear in all copies and that both that
6  * copyright notice and this permission notice appear in supporting
7  * documentation.  No representations are made about the suitability of this
8  * software for any purpose.  It is provided "as is" without express or 
9  * implied warranty.
10  */
11
12 /* This is a kludgy test harness for debugging the password dialog box.
13    It's somewhat easier to debug it here than in the xscreensaver executable
14    itself.
15  */
16
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif
20
21 #include <stdio.h>
22 #include <ctype.h>
23 #include <pwd.h>
24
25 #include <X11/Xlib.h>
26 #include <X11/Xatom.h>
27 #include <X11/Intrinsic.h>
28 #include <X11/StringDefs.h>
29 #include <X11/Shell.h>
30
31 #include "xscreensaver.h"
32 #include "resources.h"
33 #include "version.h"
34 #include "visual.h"
35 #include "auth.h"
36
37 char *progname = 0;
38 char *progclass = 0;
39 XrmDatabase db = 0;
40 saver_info *global_si_kludge;
41
42 FILE *real_stderr, *real_stdout;
43
44 void monitor_power_on (saver_info *si) {}
45 Bool monitor_powered_on_p (saver_info *si) { return True; }
46 void initialize_screensaver_window (saver_info *si) {}
47 void raise_window (saver_info *si, Bool i, Bool b, Bool d) {}
48 Bool blank_screen (saver_info *si) {return False;}
49 void unblank_screen (saver_info *si) {}
50 Bool select_visual (saver_screen_info *ssi, const char *v) { return False; }
51 Bool window_exists_p (Display *dpy, Window window) {return True;}
52 void start_notice_events_timer (saver_info *si, Window w, Bool b) {}
53 Bool handle_clientmessage (saver_info *si, XEvent *e, Bool u) { return False; }
54 int BadWindow_ehandler (Display *dpy, XErrorEvent *error) { exit(1); }
55 const char *signal_name(int signal) { return "???"; }
56 Bool restore_real_vroot (saver_info *si) { return False; }
57 void store_saver_status (saver_info *si) {}
58 void saver_exit (saver_info *si, int status, const char *core) { exit(status);}
59 int move_mouse_grab (saver_info *si, Window to, Cursor c, int ts) { return 0; }
60 int mouse_screen (saver_info *si) { return 0; }
61 void check_for_leaks (const char *where) { }
62 void shutdown_stderr (saver_info *si) { }
63
64 const char *blurb(void) { return progname; }
65 Atom XA_SCREENSAVER, XA_DEMO, XA_PREFS;
66
67 void
68 get_screen_viewport (saver_screen_info *ssi,
69                      int *x_ret, int *y_ret,
70                      int *w_ret, int *h_ret,
71                      int tx, int ty,
72                      Bool verbose_p)
73 {
74   *x_ret = 0;
75   *y_ret = 0;
76   *w_ret = WidthOfScreen (ssi->screen);
77   *h_ret = HeightOfScreen (ssi->screen);
78
79   if (*w_ret > *h_ret * 2) *w_ret /= 2;  /* xinerama kludge */
80 }
81
82 void
83 idle_timer (XtPointer closure, XtIntervalId *id)
84 {
85   saver_info *si = (saver_info *) closure;
86   XEvent fake_event;
87   fake_event.type = 0;  /* XAnyEvent type, ignored. */
88   fake_event.xany.display = si->dpy;
89   fake_event.xany.window  = 0;
90   XPutBackEvent (si->dpy, &fake_event);
91 }
92
93
94 static int
95 text_auth_conv (
96   int num_msg,
97   const struct auth_message *auth_msgs,
98   struct auth_response **resp,
99   saver_info *si)
100 {
101   char *input;
102   char buf[255];
103   struct auth_response *responses;
104   int i;
105
106   responses = calloc(num_msg, sizeof(struct auth_response));
107   if (!responses)
108     return -1;
109
110   /* The unlock state won't actually be used until this function returns and
111    * the auth module processes the response, but set it anyway for consistency
112    */
113   si->unlock_state = ul_read;
114
115   for (i = 0; i < num_msg; ++i)
116     {
117       printf ("\n%s: %s", progname, auth_msgs[i].msg);
118       if (   auth_msgs[i].type == AUTH_MSGTYPE_PROMPT_NOECHO
119           || auth_msgs[i].type == AUTH_MSGTYPE_PROMPT_ECHO)
120         {
121           input = fgets (buf, sizeof(buf)-1, stdin);
122           if (!input || !*input)
123             exit (0);
124           if (input[strlen(input)-1] == '\n')
125             input[strlen(input)-1] = 0;
126
127           responses[i].response = strdup(input);
128         }
129     }
130
131   *resp = responses;
132
133   si->unlock_state = ul_finished;
134
135   return 0;
136 }
137
138
139 #ifdef __GNUC__
140  __extension__     /* shut up about "string length is greater than the length
141                       ISO C89 compilers are required to support" when including
142                       the .ad file... */
143 #endif
144
145 static char *fallback[] = {
146 #include "XScreenSaver_ad.h"
147  0
148 };
149
150 int
151 main (int argc, char **argv)
152 {
153   enum { PASS, SPLASH, TTY } which;
154   Widget toplevel_shell = 0;
155   saver_screen_info ssip;
156   saver_info sip;
157   saver_info *si = &sip;
158   saver_preferences *p = &si->prefs;
159   struct passwd *pw;
160
161   memset(&sip, 0, sizeof(sip));
162   memset(&ssip, 0, sizeof(ssip));
163
164   si->nscreens = 1;
165   si->screens = si->default_screen = &ssip;
166   ssip.global = si;
167
168   global_si_kludge = si;
169   real_stderr = stderr;
170   real_stdout = stdout;
171
172   si->version = (char *) malloc (5);
173   memcpy (si->version, screensaver_id + 17, 4);
174   progname = argv[0];
175   {
176     char *s = strrchr(progname, '/');
177     if (*s) strcpy (progname, s+1);
178   }
179
180   if (argc != 2) goto USAGE;
181   else if (!strcmp (argv[1], "pass"))   which = PASS;
182   else if (!strcmp (argv[1], "splash")) which = SPLASH;
183   else if (!strcmp (argv[1], "tty"))    which = TTY;
184   else
185     {
186     USAGE:
187       fprintf (stderr, "usage: %s [ pass | splash | tty ]\n", progname);
188       exit (1);
189     }
190
191 #ifdef NO_LOCKING
192   if (which == PASS || which == TTY)
193     {
194       fprintf (stderr, "%s: compiled with NO_LOCKING\n", progname);
195       exit (1);
196     }
197 #endif
198
199 #ifndef NO_LOCKING
200   /* before hack_uid() for proper permissions */
201   lock_priv_init (argc, argv, True);
202
203   hack_uid (si);
204
205   if (! lock_init (argc, argv, True))
206     {
207       si->locking_disabled_p = True;
208       si->nolock_reason = "error getting password";
209     }
210 #endif
211
212   progclass = "XScreenSaver";
213
214   if (which != TTY)
215     {
216       toplevel_shell = XtAppInitialize (&si->app, progclass, 0, 0,
217                                         &argc, argv, fallback,
218                                         0, 0);
219
220       si->dpy = XtDisplay (toplevel_shell);
221       p->db = XtDatabase (si->dpy);
222       si->default_screen->toplevel_shell = toplevel_shell;
223       si->default_screen->screen = XtScreen(toplevel_shell);
224       si->default_screen->default_visual =
225         si->default_screen->current_visual =
226         DefaultVisualOfScreen(si->default_screen->screen);
227       si->default_screen->screensaver_window =
228         RootWindowOfScreen(si->default_screen->screen);
229       si->default_screen->current_depth =
230         visual_depth(si->default_screen->screen,
231                      si->default_screen->current_visual);
232
233       /* I could call get_screen_viewport(), but it is not worthwhile.
234        * These are used by the save_under pixmap. */
235       ssip.width = WidthOfScreen(ssip.screen);
236       ssip.height = HeightOfScreen(ssip.screen);
237
238       db = p->db;
239       XtGetApplicationNameAndClass (si->dpy, &progname, &progclass);
240
241       load_init_file (si->dpy, &si->prefs);
242     }
243
244   p->verbose_p = True;
245
246   pw = getpwuid (getuid ());
247   si->user = strdup (pw->pw_name);
248
249   while (1)
250     {
251 #ifndef NO_LOCKING
252       if (which == PASS)
253         {
254           si->unlock_cb = gui_auth_conv;
255           si->auth_finished_cb = auth_finished_cb;
256
257           xss_authenticate(si, True);
258
259           if (si->unlock_state == ul_success)
260             fprintf (stderr, "%s: authentication succeeded\n", progname);
261           else
262             fprintf (stderr, "%s: authentication FAILED!\n", progname);
263
264           XSync(si->dpy, False);
265           fprintf (stderr, "\n######################################\n\n");
266           sleep (3);
267         }
268       else
269 #endif
270       if (which == SPLASH)
271         {
272           XEvent event;
273           make_splash_dialog (si);
274           XtAppAddTimeOut (si->app, p->splash_duration + 1000,
275                            idle_timer, (XtPointer) si);
276           while (si->splash_dialog)
277             {
278               XtAppNextEvent (si->app, &event);
279               if (event.xany.window == si->splash_dialog)
280                 handle_splash_event (si, &event);
281               XtDispatchEvent (&event);
282             }
283           XSync (si->dpy, False);
284           sleep (1);
285         }
286 #ifndef NO_LOCKING
287       else if (which == TTY)
288         {
289           si->unlock_cb = text_auth_conv;
290
291           printf ("%s: Authenticating user %s\n", progname, si->user);
292           xss_authenticate(si, True);
293
294           if (si->unlock_state == ul_success)
295             printf ("%s: Ok!\n", progname);
296           else
297             printf ("%s: Wrong!\n", progname);
298         }
299 #endif
300       else
301         abort();
302     }
303
304   free(si->user);
305 }