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