9ef2cfe190daeb7fd488bffc989975b5819b85f2
[xscreensaver] / driver / test-passwd.c
1 /* xscreensaver, Copyright (c) 1998, 2001 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
36 char *progname = 0;
37 char *progclass = 0;
38 XrmDatabase db = 0;
39 saver_info *global_si_kludge;
40
41 FILE *real_stderr, *real_stdout;
42
43 void monitor_power_on (saver_info *si) {}
44 Bool monitor_powered_on_p (saver_info *si) { return True; }
45 void initialize_screensaver_window (saver_info *si) {}
46 void raise_window (saver_info *si, Bool i, Bool b, Bool d) {}
47 Bool blank_screen (saver_info *si) {return False;}
48 void unblank_screen (saver_info *si) {}
49 Bool select_visual (saver_screen_info *ssi, const char *v) { return False; }
50 Bool window_exists_p (Display *dpy, Window window) {return True;}
51 void start_notice_events_timer (saver_info *si, Window w, Bool b) {}
52 Bool handle_clientmessage (saver_info *si, XEvent *e, Bool u) { return False; }
53 int BadWindow_ehandler (Display *dpy, XErrorEvent *error) { exit(1); }
54 const char *signal_name(int signal) { return "???"; }
55 Bool restore_real_vroot (saver_info *si) { return False; }
56 void store_saver_status (saver_info *si) {}
57 void saver_exit (saver_info *si, int status, const char *core) { exit(status);}
58 int move_mouse_grab (saver_info *si, Window to, Cursor c, int ts) { return 0; }
59 int mouse_screen (saver_info *si) { return 0; }
60 void check_for_leaks (const char *where) { }
61 void exec_command (const char *shell, const char *command, int nice) { }
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
80
81 void
82 idle_timer (XtPointer closure, XtIntervalId *id)
83 {
84   saver_info *si = (saver_info *) closure;
85   XEvent fake_event;
86   fake_event.type = 0;  /* XAnyEvent type, ignored. */
87   fake_event.xany.display = si->dpy;
88   fake_event.xany.window  = 0;
89   XPutBackEvent (si->dpy, &fake_event);
90 }
91
92
93 static char *fallback[] = {
94 #include "XScreenSaver_ad.h"
95  0
96 };
97
98 int
99 main (int argc, char **argv)
100 {
101   enum { PASS, SPLASH, TTY } which;
102   Widget toplevel_shell = 0;
103   saver_screen_info ssip;
104   saver_info sip;
105   saver_info *si = &sip;
106   saver_preferences *p = &si->prefs;
107
108   memset(&sip, 0, sizeof(sip));
109   memset(&ssip, 0, sizeof(ssip));
110
111   si->nscreens = 1;
112   si->screens = si->default_screen = &ssip;
113   ssip.global = si;
114
115   global_si_kludge = si;
116   real_stderr = stderr;
117   real_stdout = stdout;
118
119   si->version = (char *) malloc (5);
120   memcpy (si->version, screensaver_id + 17, 4);
121   progname = argv[0];
122   {
123     char *s = strrchr(progname, '/');
124     if (*s) strcpy (progname, s+1);
125   }
126
127   if (argc != 2) goto USAGE;
128   else if (!strcmp (argv[1], "pass"))   which = PASS;
129   else if (!strcmp (argv[1], "splash")) which = SPLASH;
130   else if (!strcmp (argv[1], "tty"))    which = TTY;
131   else
132     {
133     USAGE:
134       fprintf (stderr, "usage: %s [ pass | splash | tty ]\n", progname);
135       exit (1);
136     }
137
138   /* before hack_uid() for proper permissions */
139   lock_priv_init (argc, argv, True);
140
141   hack_uid (si);
142
143   if (! lock_init (argc, argv, si->prefs.verbose_p))
144     {
145       si->locking_disabled_p = True;
146       si->nolock_reason = "error getting password";
147     }
148
149   progclass = "XScreenSaver";
150
151   if (which != TTY)
152     {
153       toplevel_shell = XtAppInitialize (&si->app, progclass, 0, 0,
154                                         &argc, argv, fallback,
155                                         0, 0);
156
157       si->dpy = XtDisplay (toplevel_shell);
158       p->db = XtDatabase (si->dpy);
159       si->default_screen->toplevel_shell = toplevel_shell;
160       si->default_screen->screen = XtScreen(toplevel_shell);
161       si->default_screen->default_visual =
162         si->default_screen->current_visual =
163         DefaultVisualOfScreen(si->default_screen->screen);
164       si->default_screen->screensaver_window =
165         RootWindowOfScreen(si->default_screen->screen);
166       si->default_screen->current_depth =
167         visual_depth(si->default_screen->screen,
168                      si->default_screen->current_visual);
169
170       db = p->db;
171       XtGetApplicationNameAndClass (si->dpy, &progname, &progclass);
172
173       load_init_file (&si->prefs);
174
175     }
176
177   p->verbose_p = True;
178
179   while (1)
180     {
181       if (which == PASS)
182         {
183           if (unlock_p (si))
184             fprintf (stderr, "%s: password correct\n", progname);
185           else
186             fprintf (stderr, "%s: password INCORRECT!\n", progname);
187
188           XSync(si->dpy, False);
189           sleep (3);
190         }
191       else if (which == SPLASH)
192         {
193           XEvent event;
194           make_splash_dialog (si);
195           XtAppAddTimeOut (si->app, p->splash_duration + 1000,
196                            idle_timer, (XtPointer) si);
197           while (si->splash_dialog)
198             {
199               XtAppNextEvent (si->app, &event);
200               if (event.xany.window == si->splash_dialog)
201                 handle_splash_event (si, &event);
202               XtDispatchEvent (&event);
203             }
204           XSync (si->dpy, False);
205           sleep (1);
206         }
207       else if (which == TTY)
208         {
209           char *pass;
210           char buf[255];
211           struct passwd *p = getpwuid (getuid ());
212           printf ("\n%s: %s's password: ", progname, p->pw_name);
213
214           pass = fgets (buf, sizeof(buf)-1, stdin);
215           if (!pass || !*pass)
216             exit (0);
217           if (pass[strlen(pass)-1] == '\n')
218             pass[strlen(pass)-1] = 0;
219
220           if (passwd_valid_p (pass, True))
221             printf ("%s: Ok!\n", progname);
222           else
223             printf ("%s: Wrong!\n", progname);
224         }
225       else
226         abort();
227     }
228 }