http://apple.doit.wisc.edu/mirrors/amug/linux/linuxppc/sources/tarballs/xscreensaver...
[xscreensaver] / driver / test-passwd.c
1 /* xscreensaver, Copyright (c) 1998 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 #define WHICH 0
18
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 #include <stdio.h>
24 #include <ctype.h>
25 #include <pwd.h>
26
27 #include <X11/Xlib.h>
28 #include <X11/Xatom.h>
29 #include <X11/Intrinsic.h>
30 #include <X11/StringDefs.h>
31 #include <X11/Shell.h>
32
33 #include "xscreensaver.h"
34 #include "resources.h"
35 #include "version.h"
36 #include "visual.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) {}
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 void restore_real_vroot (saver_info *si) {}
58 void saver_exit (saver_info *si, int status, const char *core) { exit(status);}
59
60 const char *blurb(void) { return progname; }
61 Atom XA_SCREENSAVER, XA_DEMO, XA_PREFS;
62
63 void
64 get_screen_viewport (saver_screen_info *ssi,
65                      int *x_ret, int *y_ret,
66                      int *w_ret, int *h_ret,
67                      Bool verbose_p)
68 {
69   *x_ret = 0;
70   *y_ret = 0;
71   *w_ret = WidthOfScreen (ssi->screen);
72   *h_ret = HeightOfScreen (ssi->screen);
73 }
74
75
76 void
77 idle_timer (XtPointer closure, XtIntervalId *id)
78 {
79   saver_info *si = (saver_info *) closure;
80   XEvent fake_event;
81   fake_event.type = 0;  /* XAnyEvent type, ignored. */
82   fake_event.xany.display = si->dpy;
83   fake_event.xany.window  = 0;
84   XPutBackEvent (si->dpy, &fake_event);
85 }
86
87
88 static char *fallback[] = {
89 #include "XScreenSaver_ad.h"
90  0
91 };
92
93 int
94 main (int argc, char **argv)
95 {
96   Widget toplevel_shell = 0;
97   saver_screen_info ssip;
98   saver_info sip;
99   saver_info *si = &sip;
100   saver_preferences *p = &si->prefs;
101
102   memset(&sip, 0, sizeof(sip));
103   memset(&ssip, 0, sizeof(ssip));
104
105   si->nscreens = 1;
106   si->screens = si->default_screen = &ssip;
107   ssip.global = si;
108
109   global_si_kludge = si;
110   real_stderr = stderr;
111   real_stdout = stdout;
112
113   si->version = (char *) malloc (5);
114   memcpy (si->version, screensaver_id + 17, 4);
115   progname = argv[0];
116   {
117     char *s = strrchr(progname, '/');
118     if (*s) strcpy (progname, s+1);
119   }
120
121   /* before hack_uid() for proper permissions */
122   lock_priv_init (argc, argv, True);
123
124   hack_uid (si);
125
126   if (! lock_init (argc, argv, si->prefs.verbose_p))
127     {
128       si->locking_disabled_p = True;
129       si->nolock_reason = "error getting password";
130     }
131
132   progclass = "XScreenSaver";
133
134 #if (WHICH != 2)
135   toplevel_shell = XtAppInitialize (&si->app, progclass, 0, 0,
136                                     &argc, argv, fallback,
137                                     0, 0);
138
139   si->dpy = XtDisplay (toplevel_shell);
140   p->db = XtDatabase (si->dpy);
141   si->default_screen->toplevel_shell = toplevel_shell;
142   si->default_screen->screen = XtScreen(toplevel_shell);
143   si->default_screen->default_visual =
144     si->default_screen->current_visual =
145       DefaultVisualOfScreen(si->default_screen->screen);
146   si->default_screen->screensaver_window =
147     RootWindowOfScreen(si->default_screen->screen);
148   si->default_screen->current_depth =
149     visual_depth(si->default_screen->screen,
150                  si->default_screen->current_visual);
151
152   db = p->db;
153   XtGetApplicationNameAndClass (si->dpy, &progname, &progclass);
154
155   load_init_file (&si->prefs);
156
157 #endif /* (WHICH != 2) */
158
159   p->verbose_p = True;
160
161   while (1)
162     {
163 #if WHICH == 0
164       if (unlock_p (si))
165         fprintf (stderr, "%s: password correct\n", progname);
166       else
167         fprintf (stderr, "%s: password INCORRECT!\n", progname);
168
169       XSync(si->dpy, False);
170       sleep (3);
171 #elif WHICH == 1
172       {
173         XEvent event;
174         make_splash_dialog (si);
175         XtAppAddTimeOut (si->app, p->splash_duration + 1000,
176                          idle_timer, (XtPointer) si);
177         while (si->splash_dialog)
178           {
179             XtAppNextEvent (si->app, &event);
180             if (event.xany.window == si->splash_dialog)
181               handle_splash_event (si, &event);
182             XtDispatchEvent (&event);
183           }
184         XSync (si->dpy, False);
185         sleep (1);
186       }
187 #elif WHICH == 2
188       {
189         char *pass;
190         char buf[255];
191         struct passwd *p = getpwuid (getuid ());
192         printf ("\n%s: %s's password: ", progname, p->pw_name);
193
194         pass = fgets (buf, sizeof(buf)-1, stdin);
195         if (!pass || !*pass)
196           exit (0);
197         if (pass[strlen(pass)-1] == '\n')
198           pass[strlen(pass)-1] = 0;
199
200         if (passwd_valid_p (pass, True))
201           printf ("%s: Ok!\n", progname);
202         else
203           printf ("%s: Wrong!\n", progname);
204       }
205 #endif
206     }
207 }