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