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