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