http://slackware.bholcomb.com/slackware/slackware-11.0/source/xap/xscreensaver/xscree...
[xscreensaver] / driver / test-passwd.c
1 /* xscreensaver, Copyright (c) 1998-2005 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 int on_path_p (const char *program) { return 0; }
63 void shutdown_stderr (saver_info *si) { }
64
65 const char *blurb(void) { return progname; }
66 Atom XA_SCREENSAVER, XA_DEMO, XA_PREFS;
67
68 void
69 get_screen_viewport (saver_screen_info *ssi,
70                      int *x_ret, int *y_ret,
71                      int *w_ret, int *h_ret,
72                      int tx, int ty,
73                      Bool verbose_p)
74 {
75   *x_ret = 0;
76   *y_ret = 0;
77   *w_ret = WidthOfScreen (ssi->screen);
78   *h_ret = HeightOfScreen (ssi->screen);
79
80   if (*w_ret > *h_ret * 2) *w_ret /= 2;  /* xinerama kludge */
81 }
82
83
84 void
85 idle_timer (XtPointer closure, XtIntervalId *id)
86 {
87   saver_info *si = (saver_info *) closure;
88   XEvent fake_event;
89   fake_event.type = 0;  /* XAnyEvent type, ignored. */
90   fake_event.xany.display = si->dpy;
91   fake_event.xany.window  = 0;
92   XPutBackEvent (si->dpy, &fake_event);
93 }
94
95
96 #ifdef __GNUC__
97  __extension__     /* shut up about "string length is greater than the length
98                       ISO C89 compilers are required to support" when including
99                       the .ad file... */
100 #endif
101
102 static char *fallback[] = {
103 #include "XScreenSaver_ad.h"
104  0
105 };
106
107 int
108 main (int argc, char **argv)
109 {
110   enum { PASS, SPLASH, TTY } which;
111   Widget toplevel_shell = 0;
112   saver_screen_info ssip;
113   saver_info sip;
114   saver_info *si = &sip;
115   saver_preferences *p = &si->prefs;
116
117   memset(&sip, 0, sizeof(sip));
118   memset(&ssip, 0, sizeof(ssip));
119
120   si->nscreens = 1;
121   si->screens = si->default_screen = &ssip;
122   ssip.global = si;
123
124   global_si_kludge = si;
125   real_stderr = stderr;
126   real_stdout = stdout;
127
128   si->version = (char *) malloc (5);
129   memcpy (si->version, screensaver_id + 17, 4);
130   progname = argv[0];
131   {
132     char *s = strrchr(progname, '/');
133     if (*s) strcpy (progname, s+1);
134   }
135
136   if (argc != 2) goto USAGE;
137   else if (!strcmp (argv[1], "pass"))   which = PASS;
138   else if (!strcmp (argv[1], "splash")) which = SPLASH;
139   else if (!strcmp (argv[1], "tty"))    which = TTY;
140   else
141     {
142     USAGE:
143       fprintf (stderr, "usage: %s [ pass | splash | tty ]\n", progname);
144       exit (1);
145     }
146
147 #ifdef NO_LOCKING
148   if (which == PASS || which == TTY)
149     {
150       fprintf (stderr, "%s: compiled with NO_LOCKING\n", progname);
151       exit (1);
152     }
153 #endif
154
155 #ifndef NO_LOCKING
156   /* before hack_uid() for proper permissions */
157   lock_priv_init (argc, argv, True);
158
159   hack_uid (si);
160
161   if (! lock_init (argc, argv, True))
162     {
163       si->locking_disabled_p = True;
164       si->nolock_reason = "error getting password";
165     }
166 #endif
167
168   progclass = "XScreenSaver";
169
170   if (which != TTY)
171     {
172       toplevel_shell = XtAppInitialize (&si->app, progclass, 0, 0,
173                                         &argc, argv, fallback,
174                                         0, 0);
175
176       si->dpy = XtDisplay (toplevel_shell);
177       p->db = XtDatabase (si->dpy);
178       si->default_screen->toplevel_shell = toplevel_shell;
179       si->default_screen->screen = XtScreen(toplevel_shell);
180       si->default_screen->default_visual =
181         si->default_screen->current_visual =
182         DefaultVisualOfScreen(si->default_screen->screen);
183       si->default_screen->screensaver_window =
184         RootWindowOfScreen(si->default_screen->screen);
185       si->default_screen->current_depth =
186         visual_depth(si->default_screen->screen,
187                      si->default_screen->current_visual);
188
189       db = p->db;
190       XtGetApplicationNameAndClass (si->dpy, &progname, &progclass);
191
192       load_init_file (si->dpy, &si->prefs);
193     }
194
195   p->verbose_p = True;
196
197   while (1)
198     {
199 #ifndef NO_LOCKING
200       if (which == PASS)
201         {
202           if (unlock_p (si))
203             fprintf (stderr, "%s: password correct\n", progname);
204           else
205             fprintf (stderr, "%s: password INCORRECT!\n", progname);
206
207           XSync(si->dpy, False);
208           sleep (3);
209         }
210       else
211 #endif
212       if (which == SPLASH)
213         {
214           XEvent event;
215           make_splash_dialog (si);
216           XtAppAddTimeOut (si->app, p->splash_duration + 1000,
217                            idle_timer, (XtPointer) si);
218           while (si->splash_dialog)
219             {
220               XtAppNextEvent (si->app, &event);
221               if (event.xany.window == si->splash_dialog)
222                 handle_splash_event (si, &event);
223               XtDispatchEvent (&event);
224             }
225           XSync (si->dpy, False);
226           sleep (1);
227         }
228       else if (which == TTY)
229         {
230           char *pass;
231           char buf[255];
232           struct passwd *p = getpwuid (getuid ());
233           printf ("\n%s: %s's password: ", progname, p->pw_name);
234
235           pass = fgets (buf, sizeof(buf)-1, stdin);
236           if (!pass || !*pass)
237             exit (0);
238           if (pass[strlen(pass)-1] == '\n')
239             pass[strlen(pass)-1] = 0;
240
241 #ifndef NO_LOCKING
242           if (passwd_valid_p (pass, True))
243             printf ("%s: Ok!\n", progname);
244           else
245             printf ("%s: Wrong!\n", progname);
246 #endif
247         }
248       else
249         abort();
250     }
251 }