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