* correct all GetNextHashPos() calls to have const chars
[citadel.git] / webcit / auth.c
1 /*
2  * $Id$
3  *
4  * WebcitAuth; Handles authentication of users to a Citadel server.
5  */
6
7 #include "webcit.h"
8 #include "webserver.h"
9 #include <ctype.h>
10
11
12 void display_reg(int during_login);
13
14 /*
15  * Access level definitions.  This is initialized from a function rather than a
16  * static array so that the strings may be localized.
17  */
18 char *axdefs[7]; 
19
20 void initialize_axdefs(void) {
21         axdefs[0] = _("Deleted");       /* an erased user */
22         axdefs[1] = _("New User");      /* a new user */
23         axdefs[2] = _("Problem User");  /* a trouble maker */
24         axdefs[3] = _("Local User");    /* user with normal privileges */
25         axdefs[4] = _("Network User");  /* a user that may access network resources */
26         axdefs[5] = _("Preferred User");/* a moderator */
27         axdefs[6] = _("Aide");          /* chief */
28 }
29
30
31
32
33 /* 
34  * Display the login screen
35  * mesg = the error message if last attempt failed.
36  */
37 void display_login(char *mesg)
38 {
39         char buf[SIZ];
40
41         output_headers(1, 1, 2, 0, 0, 0);
42         wprintf("<div id=\"login_screen\">\n");
43
44         if (mesg != NULL) if (!IsEmptyStr(mesg)) {
45                         stresc(buf, SIZ,  mesg, 0, 0);
46                         svprintf(HKEY("MESG"), WCS_STRING, "%s", buf);
47         }
48
49         svprintf(HKEY("LOGIN_INSTRUCTIONS"), WCS_STRING,
50                 _("<ul>"
51                 "<li><b>If you already have an account on %s</b>, "
52                 "enter your user name and password and click &quot;Login.&quot; "
53                 "<li><b>If you are a new user</b>, enter the name and password "
54                 "you wish to use, "
55                 "and click &quot;New User.&quot; "
56                 "<li>Please log off properly when finished. "
57                 "<li>You must use a browser that supports <i>frames</i> and "
58                 "<i>cookies</i>. "
59                 "<li>Also keep in mind that if your browser is "
60                 "configured to block pop-up windows, you will not be able "
61                 "to receive any instant messages.<br />"
62                 "</ul>"),
63                 serv_info.serv_humannode
64         );
65
66         svput("USERNAME_BOX", WCS_STRING, _("User name:"));
67         svput("PASSWORD_BOX", WCS_STRING, _("Password:"));
68         svput("LANGUAGE_BOX", WCS_STRING, _("Language:"));
69         svput("LOGIN_BUTTON", WCS_STRING, _("Login"));
70         svput("NEWUSER_BUTTON", WCS_STRING, _("New User"));
71         svput("EXIT_BUTTON", WCS_STRING, _("Exit"));
72         svput("HELLO", WCS_SERVCMD, "MESG hello");
73         svprintf(HKEY("BOXTITLE"), WCS_STRING, _("%s - powered by <a href=\"http://www.citadel.org\">Citadel</a>"),
74                 serv_info.serv_humannode);
75         svcallback("DO_LANGUAGE_BOX", offer_languages);
76         if (serv_info.serv_newuser_disabled) {
77                 svput("NEWUSER_BUTTON_PRE", WCS_STRING, "<div style=\"display:none;\">");
78                 svput("NEWUSER_BUTTON_POST", WCS_STRING, "</div>");
79         }
80         else {
81                 svput("NEWUSER_BUTTON_PRE", WCS_STRING, "");
82                 svput("NEWUSER_BUTTON_POST", WCS_STRING, "");
83         }
84
85         if (serv_info.serv_supports_openid) {
86                 svprintf(HKEY("OFFER_OPENID_LOGIN"), WCS_STRING,
87                         "<div align=center>"
88                         "<a href=\"display_openid_login\">"
89                         "<img src=\"static/openid-small.gif\" border=0 valign=middle>"
90                         "%s</a>"
91                         "</div>"
92                         ,
93                         "Log in using OpenID"
94                 );
95         }
96         else {
97                 svput("OFFER_OPENID_LOGIN", WCS_STRING, "");
98         }
99
100         do_template("login", NULL);
101
102         wDumpContent(2);
103 }
104
105
106
107
108 /* 
109  * Display the openid-enabled login screen
110  * mesg = the error message if last attempt failed.
111  */
112 void display_openid_login(char *mesg)
113 {
114         char buf[SIZ];
115
116         output_headers(1, 1, 2, 0, 0, 0);
117         wprintf("<div id=\"login_screen\">\n");
118
119         if (mesg != NULL) if (!IsEmptyStr(mesg)) {
120                         stresc(buf, SIZ,  mesg, 0, 0);
121                         svprintf(HKEY("MESG"), WCS_STRING, "%s", buf);
122         }
123
124         svprintf(HKEY("LOGIN_INSTRUCTIONS"), WCS_STRING,
125                 _("<ul>"
126                 "<li>Enter your OpenID URL and click &quot;Login&quot;."
127                 "<li><a href=\"http://www.citadel.org/doku.php/documentation:openid\">"
128                 "Click here to learn what OpenID is and how Citadel is using it.</a>"
129                 "<li>Please log off properly when finished. "
130                 "<li>You must use a browser that supports <i>frames</i> and "
131                 "<i>cookies</i>. "
132                 "<li>Also keep in mind that if your browser is "
133                 "configured to block pop-up windows, you will not be able "
134                 "to receive any instant messages.<br />"
135                 "</ul>")
136         );
137
138         svput("OPENID_BOX", WCS_STRING, _("OpenID URL:"));
139         svput("LANGUAGE_BOX", WCS_STRING, _("Language:"));
140         svput("LOGIN_BUTTON", WCS_STRING, _("Login"));
141         svput("EXIT_BUTTON", WCS_STRING, _("Exit"));
142         svput("HELLO", WCS_SERVCMD, "MESG hello");
143         svprintf(HKEY("BOXTITLE"), WCS_STRING, _("%s - powered by <a href=\"http://www.citadel.org\">Citadel</a>"),
144                 serv_info.serv_humannode);
145         svcallback("DO_LANGUAGE_BOX", offer_languages);
146
147         svprintf(HKEY("OFFER_CONVENTIONAL_LOGIN"), WCS_STRING,
148                 "<div align=center>"
149                 "<a href=\"display_login\">"
150                 "%s</a>"
151                 "</div>"
152                 ,
153                 "Log in using a user name and password"
154         );
155
156         do_template("openid_login", NULL);
157         wDumpContent(2);
158 }
159
160
161 void display_openid_name_request(char *claimed_id, char *username) {
162         char buf[SIZ];
163
164         output_headers(1, 1, 2, 0, 0, 0);
165         wprintf("<div id=\"login_screen\">\n");
166
167         stresc(buf, sizeof buf, claimed_id, 0, 0);
168         svprintf(HKEY("VERIFIED"), WCS_STRING, _("Your OpenID <tt>%s</tt> was successfully verified."),
169                 claimed_id);
170         svput("CLAIMED_ID", WCS_STRING, claimed_id);
171
172
173         if (!IsEmptyStr(username)) {
174                 stresc(buf, sizeof buf, username, 0, 0);
175                 svprintf(HKEY("REASON"), WCS_STRING,
176                         _("However, the user name '%s' conflicts with an existing user."), username);
177         }
178         else {
179                 svput("REASON", WCS_STRING, "");
180         }
181
182         svput("ACTION_REQUESTED", WCS_STRING, _("Please specify the user name you would like to use."));
183
184         svput("USERNAME_BOX", WCS_STRING, _("User name:"));
185         svput("NEWUSER_BUTTON", WCS_STRING, _("New User"));
186         svput("EXIT_BUTTON", WCS_STRING, _("Exit"));
187
188         svprintf(HKEY("BOXTITLE"), WCS_STRING, _("%s - powered by <a href=\"http://www.citadel.org\">Citadel</a>"),
189                 serv_info.serv_humannode);
190
191         do_template("openid_manual_create", NULL);
192         wDumpContent(2);
193 }
194
195
196
197 /* Initialize the session
198  *
199  * This function needs to get called whenever the session changes from
200  * not-logged-in to logged-in, either by an explicit login by the user or
201  * by a timed-out session automatically re-establishing with a little help
202  * from the browser cookie.  Either way, we need to load access controls and
203  * preferences from the server.
204  *
205  * user                 the username
206  * pass                 his password
207  * serv_response        The parameters returned from a Citadel USER or NEWU command
208  */
209 void become_logged_in(char *user, char *pass, char *serv_response)
210 {
211         char buf[SIZ];
212
213         WC->logged_in = 1;
214         extract_token(WC->wc_fullname, &serv_response[4], 0, '|', sizeof WC->wc_fullname);
215         safestrncpy(WC->wc_username, user, sizeof WC->wc_username);
216         safestrncpy(WC->wc_password, pass, sizeof WC->wc_password);
217         WC->axlevel = extract_int(&serv_response[4], 1);
218         if (WC->axlevel >= 6) {
219                 WC->is_aide = 1;
220         }
221
222         load_preferences();
223
224         serv_puts("CHEK");
225         serv_getln(buf, sizeof buf);
226         if (buf[0] == '2') {
227                 WC->new_mail = extract_int(&buf[4], 0);
228                 WC->need_regi = extract_int(&buf[4], 1);
229                 WC->need_vali = extract_int(&buf[4], 2);
230                 extract_token(WC->cs_inet_email, &buf[4], 3, '|', sizeof WC->cs_inet_email);
231         }
232
233         get_pref_long("current_iconbar", &WC->current_iconbar, current_iconbar_menu);
234
235         get_preference("floordiv_expanded", &WC->floordiv_expanded);
236 }
237
238
239 /* 
240  * Perform authentication using a user name and password
241  */
242 void do_login(void)
243 {
244         char buf[SIZ];
245
246         if (havebstr("language")) {
247                 set_selected_language(bstr("language"));
248                 go_selected_language();
249         }
250
251         if (havebstr("exit_action")) {
252                 do_logout();
253                 return;
254         }
255         if (havebstr("login_action")) {
256                 serv_printf("USER %s", bstr("name"));
257                 serv_getln(buf, sizeof buf);
258                 if (buf[0] == '3') {
259                         serv_printf("PASS %s", bstr("pass"));
260                         serv_getln(buf, sizeof buf);
261                         if (buf[0] == '2') {
262                                 become_logged_in(bstr("name"), bstr("pass"), buf);
263                         } else {
264                                 display_login(&buf[4]);
265                                 return;
266                         }
267                 } else {
268                         display_login(&buf[4]);
269                         return;
270                 }
271         }
272         if (havebstr("newuser_action")) {
273                 if (!havebstr("pass")) {
274                         display_login(_("Blank passwords are not allowed."));
275                         return;
276                 }
277                 serv_printf("NEWU %s", bstr("name"));
278                 serv_getln(buf, sizeof buf);
279                 if (buf[0] == '2') {
280                         become_logged_in(bstr("name"), bstr("pass"), buf);
281                         serv_printf("SETP %s", bstr("pass"));
282                         serv_getln(buf, sizeof buf);
283                 } else {
284                         display_login(&buf[4]);
285                         return;
286                 }
287         }
288         if (WC->logged_in) {
289                 set_preference("language", NewStrBufPlain(bstr("language"), -1), 1);
290                 if (WC->need_regi) {
291                         display_reg(1);
292                 } else if (WC->need_vali) {
293                         validate();
294                 } else {
295                         do_welcome();
296                 }
297         } else {
298                 display_login(_("Your password was not accepted."));
299         }
300
301 }
302
303 /* 
304  * Try to create an account manually after an OpenID was verified
305  */
306 void openid_manual_create(void)
307 {
308         if (havebstr("exit_action")) {
309                 do_logout();
310                 return;
311         }
312
313         char buf[1024];
314         if (havebstr("newuser_action")) {
315                 serv_printf("OIDC %s", bstr("name"));
316                 serv_getln(buf, sizeof buf);
317                 if (buf[0] == '2') {
318                         char gpass[1024] = "";
319                         serv_puts("SETP GENERATE_RANDOM_PASSWORD");
320                         serv_getln(gpass, sizeof gpass);
321                         become_logged_in(bstr("name"), &gpass[4], buf);
322                 }
323         }
324
325         if (WC->logged_in) {
326                 if (WC->need_regi) {
327                         display_reg(1);
328                 } else if (WC->need_vali) {
329                         validate();
330                 } else {
331                         do_welcome();
332                 }
333         } else {
334                 display_openid_name_request(bstr("openid_url"), bstr("name"));
335         }
336
337 }
338
339
340 /* 
341  * Perform authentication using OpenID
342  * assemble the checkid_setup request and then redirect to the user's identity provider
343  */
344 void do_openid_login(void)
345 {
346         char buf[4096];
347
348         if (havebstr("language")) {
349                 set_selected_language(bstr("language"));
350                 go_selected_language();
351         }
352
353         if (havebstr("exit_action")) {
354                 do_logout();
355                 return;
356         }
357         if (havebstr("login_action")) {
358                 snprintf(buf, sizeof buf,
359                         "OIDS %s|%s://%s/finalize_openid_login|%s://%s",
360                         bstr("openid_url"),
361                         (is_https ? "https" : "http"), WC->http_host,
362                         (is_https ? "https" : "http"), WC->http_host
363                 );
364
365                 serv_puts(buf);
366                 serv_getln(buf, sizeof buf);
367                 if (buf[0] == '2') {
368                         lprintf(CTDL_DEBUG, "OpenID server contacted; redirecting to %s\n", &buf[4]);
369                         http_redirect(&buf[4]);
370                         return;
371                 }
372                 else {
373                         display_openid_login(&buf[4]);
374                         return;
375                 }
376         }
377
378         /* If we get to this point then something failed. */
379         display_openid_login(_("Your password was not accepted."));
380 }
381
382 /* 
383  * Complete the authentication using OpenID
384  * This function handles the positive or negative assertion from the user's Identity Provider
385  */
386 void finalize_openid_login(void)
387 {
388         char buf[1024];
389         struct wcsession *WCC = WC;
390         int already_logged_in = (WCC->logged_in) ;
391         int linecount = 0;
392         char result[128] = "";
393         char username[128] = "";
394         char password[128] = "";
395         char logged_in_response[1024] = "";
396         char claimed_id[1024] = "";
397
398         if (havebstr("openid.mode")) {
399                 if (!strcasecmp(bstr("openid.mode"), "id_res")) {
400
401                         serv_puts("OIDF");
402                         serv_getln(buf, sizeof buf);
403
404                         if (buf[0] == '8') {
405                                 urlcontent *u;
406                                 void *U;
407                                 long HKLen;
408                                 const char *HKey;
409                                 HashPos *Cursor;
410                                 
411                                 Cursor = GetNewHashPos ();
412                                 while (GetNextHashPos(WCC->urlstrings, Cursor, &HKLen, &HKey, &U)) {
413                                         u = (urlcontent*) U;
414                                         if (!strncasecmp(u->url_key, "openid.", 7)) {
415                                                 serv_printf("%s|%s", &u->url_key[7], ChrPtr(u->url_data));
416                                         }
417                                 }
418
419                                 serv_puts("000");
420
421                                 linecount = 0;
422                                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
423                                         if (linecount == 0) safestrncpy(result, buf, sizeof result);
424                                         if (!strcasecmp(result, "authenticate")) {
425                                                 if (linecount == 1) {
426                                                         safestrncpy(username, buf, sizeof username);
427                                                 }
428                                                 else if (linecount == 2) {
429                                                         safestrncpy(password, buf, sizeof password);
430                                                 }
431                                                 else if (linecount == 3) {
432                                                         safestrncpy(logged_in_response, buf,
433                                                                 sizeof logged_in_response);
434                                                 }
435                                         }
436                                         else if (!strcasecmp(result, "verify_only")) {
437                                                 if (linecount == 1) {
438                                                         safestrncpy(claimed_id, buf, sizeof claimed_id);
439                                                 }
440                                                 if (linecount == 2) {
441                                                         safestrncpy(username, buf, sizeof username);
442                                                 }
443                                         }
444                                         ++linecount;
445                                 }
446                         }
447                 }
448         }
449
450         /* If we were already logged in, this was an attempt to associate an OpenID account */
451         if (already_logged_in) {
452                 display_openids();
453                 return;
454         }
455
456         /* If this operation logged us in, either by connecting with an existing account or by
457          * auto-creating one using Simple Registration Extension, we're already on our way.
458          */
459         if (!strcasecmp(result, "authenticate")) {
460                 become_logged_in(username, password, logged_in_response);
461         }
462
463         /* The specified OpenID was verified but the desired user name was either not specified via SRI
464          * or conflicts with an existing user.  Either way the user will need to specify a new name.
465          */
466
467         else if (!strcasecmp(result, "verify_only")) {
468                 display_openid_name_request(claimed_id, username);
469         }
470
471         /* Did we manage to log in?  If so, continue with the normal flow... */
472         if (WC->logged_in) {
473                 if (WC->need_regi) {
474                         display_reg(1);
475                 } else {
476                         do_welcome();
477                 }
478         } else {
479                 display_openid_login(_("Your password was not accepted."));
480         }
481
482 }
483
484
485 /*
486  * Display a welcome screen to the user.
487  *
488  * If this is the first time login, and the web based setup is enabled, 
489  * lead the user through the setup routines
490  */
491 void do_welcome(void)
492 {
493         StrBuf *Buf;
494 #ifdef XXX_NOT_FINISHED_YET_XXX
495         FILE *fp;
496         int i;
497
498         /**
499          * See if we have to run the first-time setup wizard
500          */
501         if (WC->is_aide) {
502                 if (!setup_wizard) {
503                         int len;
504                         sprintf(wizard_filename, "setupwiz.%s.%s",
505                                 ctdlhost, ctdlport);
506                         len = strlen(wizard_filename);
507                         for (i=0; i<len; ++i) {
508                                 if (    (wizard_filename[i]==' ')
509                                         || (wizard_filename[i] == '/')
510                                 ) {
511                                         wizard_filename[i] = '_';
512                                 }
513                         }
514         
515                         fp = fopen(wizard_filename, "r");
516                         if (fp != NULL) {
517                                 fgets(buf, sizeof buf, fp);
518                                 buf[strlen(buf)-1] = 0;
519                                 fclose(fp);
520                                 if (atoi(buf) == serv_info.serv_rev_level) {
521                                         setup_wizard = 1; /**< already run */
522                                 }
523                         }
524                 }
525
526                 if (!setup_wizard) {
527                         http_redirect("setup_wizard");
528                 }
529         }
530 #endif
531
532         /*
533          * Go to the user's preferred start page
534          */
535         if (!get_preference("startpage", &Buf)) {
536                 Buf = NewStrBuf ();
537                 StrBufPrintf(Buf, "dotskip&room=_BASEROOM_");
538                 set_preference("startpage", Buf, 1);
539         }
540         if (ChrPtr(Buf)[0] == '/') {
541                 StrBufCutLeft(Buf, 1);
542         }
543         if (StrLength(Buf) == 0)
544                 StrBufAppendBufPlain(Buf, "dotgoto?room=_BASEROOM_", -1, 0);
545         http_redirect(ChrPtr(Buf));
546 }
547
548
549 /*
550  * Disconnect from the Citadel server, and end this WebCit session
551  */
552 void end_webcit_session(void) {
553         
554         if (WC->logged_in) {
555                 set_pref_long("current_iconbar", WC->current_iconbar, 0);
556                 set_preference("floordiv_expanded", WC->floordiv_expanded, 1);
557         }
558
559         serv_puts("QUIT");
560         WC->killthis = 1;
561         /* close() of citadel socket will be done by do_housekeeping() */
562 }
563
564 /* 
565  * execute the logout
566  */
567 void do_logout(void)
568 {
569         char buf[SIZ];
570
571         safestrncpy(WC->wc_username, "", sizeof WC->wc_username);
572         safestrncpy(WC->wc_password, "", sizeof WC->wc_password);
573         safestrncpy(WC->wc_roomname, "", sizeof WC->wc_roomname);
574         safestrncpy(WC->wc_fullname, "", sizeof WC->wc_fullname);
575
576         /** Calling output_headers() this way causes the cookies to be un-set */
577         output_headers(1, 1, 0, 1, 0, 0);
578
579         wprintf("<div id=\"logout_screen\">");
580         wprintf("<div class=\"box\">");
581         wprintf("<div class=\"boxlabel\">");
582         wprintf(_("Log off"));
583         wprintf("</div><div class=\"boxcontent\">");    
584         serv_puts("MESG goodbye");
585         serv_getln(buf, sizeof buf);
586
587         if (WC->serv_sock >= 0) {
588                 if (buf[0] == '1') {
589                         fmout("CENTER");
590                 } else {
591                         wprintf("Goodbye\n");
592                 }
593         }
594         else {
595                 wprintf(_("This program was unable to connect or stay "
596                         "connected to the Citadel server.  Please report "
597                         "this problem to your system administrator.")
598                 );
599                 wprintf("<a href=\"http://www.citadel.org/doku.php/"
600                         "faq:mastering_your_os:net#netstat\">%s</a>", 
601                         _("Read More..."));
602         }
603
604         wprintf("<hr /><div class=\"buttons\"> "
605                 "<span class=\"button_link\"><a href=\".\">");
606         wprintf(_("Log in again"));
607         wprintf("</a></span>&nbsp;&nbsp;&nbsp;<span class=\"button_link\">"
608                 "<a href=\"javascript:window.close();\">");
609         wprintf(_("Close window"));
610         wprintf("</a></span></div></div></div></div>\n");
611         wDumpContent(2);
612         end_webcit_session();
613 }
614
615
616 /*
617  * validate new users
618  */
619 void validate(void)
620 {
621         char cmd[SIZ];
622         char user[SIZ];
623         char buf[SIZ];
624         int a;
625
626         output_headers(1, 1, 2, 0, 0, 0);
627         wprintf("<div id=\"banner\">\n");
628         wprintf("<h1>");
629         wprintf(_("Validate new users"));
630         wprintf("</h1>");
631         wprintf("</div>\n");
632
633         wprintf("<div id=\"content\" class=\"service\">\n");
634
635         /* If the user just submitted a validation, process it... */
636         safestrncpy(buf, bstr("user"), sizeof buf);
637         if (!IsEmptyStr(buf)) {
638                 if (havebstr("axlevel")) {
639                         serv_printf("VALI %s|%s", buf, bstr("axlevel"));
640                         serv_getln(buf, sizeof buf);
641                         if (buf[0] != '2') {
642                                 wprintf("<b>%s</b><br />\n", &buf[4]);
643                         }
644                 }
645         }
646
647         /* Now see if any more users require validation. */
648         serv_puts("GNUR");
649         serv_getln(buf, sizeof buf);
650         if (buf[0] == '2') {
651                 wprintf("<b>");
652                 wprintf(_("No users require validation at this time."));
653                 wprintf("</b><br />\n");
654                 wDumpContent(1);
655                 return;
656         }
657         if (buf[0] != '3') {
658                 wprintf("<b>%s</b><br />\n", &buf[4]);
659                 wDumpContent(1);
660                 return;
661         }
662
663         wprintf("<div class=\"fix_scrollbar_bug\">"
664                 "<table class=\"auth_validate\"><tr><td>\n");
665         wprintf("<center>");
666
667         safestrncpy(user, &buf[4], sizeof user);
668         serv_printf("GREG %s", user);
669         serv_getln(cmd, sizeof cmd);
670         if (cmd[0] == '1') {
671                 a = 0;
672                 do {
673                         serv_getln(buf, sizeof buf);
674                         ++a;
675                         if (a == 1)
676                                 wprintf("#%s<br /><H1>%s</H1>",
677                                         buf, &cmd[4]);
678                         if (a == 2) {
679                                 char *pch;
680                                 int haveChar = 0;
681                                 int haveNum = 0;
682                                 int haveOther = 0;
683                                 int count = 0;
684                                 pch = buf;
685                                 while (!IsEmptyStr(pch))
686                                 {
687                                         if (isdigit(*pch))
688                                                 haveNum = 1;
689                                         else if (isalpha(*pch))
690                                                 haveChar = 1;
691                                         else
692                                                 haveOther = 1;
693                                         pch ++;
694                                 }
695                                 count = pch - buf;
696                                 if (count > 7)
697                                         count = 0;
698                                 switch (count){
699                                 case 0:
700                                         pch = _("very weak");
701                                         break;
702                                 case 1:
703                                         pch = _("weak");
704                                         break;
705                                 case 2:
706                                         pch = _("ok");
707                                         break;
708                                 case 3:
709                                 default:
710                                         pch = _("strong");
711                                 }
712
713                                 wprintf("PW: %s<br />\n", pch);
714                         }
715                         if (a == 3)
716                                 wprintf("%s<br />\n", buf);
717                         if (a == 4)
718                                 wprintf("%s<br />\n", buf);
719                         if (a == 5)
720                                 wprintf("%s, ", buf);
721                         if (a == 6)
722                                 wprintf("%s ", buf);
723                         if (a == 7)
724                                 wprintf("%s<br />\n", buf);
725                         if (a == 8)
726                                 wprintf("%s<br />\n", buf);
727                         if (a == 9)
728                                 wprintf(_("Current access level: %d (%s)\n"),
729                                         atoi(buf), axdefs[atoi(buf)]);
730                 } while (strcmp(buf, "000"));
731         } else {
732                 wprintf("<H1>%s</H1>%s<br />\n", user, &cmd[4]);
733         }
734
735         wprintf("<hr />");
736         wprintf(_("Select access level for this user:"));
737         wprintf("<br />\n");
738         for (a = 0; a <= 6; ++a) {
739                 wprintf("<a href=\"validate?nonce=%d?user=", WC->nonce);
740                 urlescputs(user);
741                 wprintf("&axlevel=%d\">%s</A>&nbsp;&nbsp;&nbsp;\n",
742                         a, axdefs[a]);
743         }
744         wprintf("<br />\n");
745
746         wprintf("</CENTER>\n");
747         wprintf("</td></tr></table></div>\n");
748         wDumpContent(1);
749 }
750
751
752
753 /*
754  * Display form for registration.
755  *
756  * (Set during_login to 1 if this registration is being performed during
757  * new user login and will require chaining to the proper screen.)
758  */
759 void display_reg(int during_login)
760 {
761         long vcard_msgnum;
762
763         if (goto_config_room() != 0) {
764                 if (during_login) do_welcome();
765                 else display_main_menu();
766                 return;
767         }
768
769         vcard_msgnum = locate_user_vcard(WC->wc_fullname, -1);
770         if (vcard_msgnum < 0L) {
771                 if (during_login) do_welcome();
772                 else display_main_menu();
773                 return;
774         }
775
776         if (during_login) {
777                 do_edit_vcard(vcard_msgnum, "1", "do_welcome", USERCONFIGROOM);
778         }
779         else {
780                 do_edit_vcard(vcard_msgnum, "1", "display_main_menu", USERCONFIGROOM);
781         }
782
783 }
784
785
786
787
788 /*
789  * display form for changing your password
790  */
791 void display_changepw(void)
792 {
793         char buf[SIZ];
794
795         output_headers(1, 1, 1, 0, 0, 0);
796
797         svput("BOXTITLE", WCS_STRING, _("Change your password"));
798         do_template("beginbox", NULL);
799
800         if (!IsEmptyStr(WC->ImportantMessage)) {
801                 wprintf("<span class=\"errormsg\">"
802                         "%s</span><br />\n", WC->ImportantMessage);
803                 safestrncpy(WC->ImportantMessage, "", sizeof WC->ImportantMessage);
804         }
805
806         serv_puts("MESG changepw");
807         serv_getln(buf, sizeof buf);
808         if (buf[0] == '1') {
809                 fmout("CENTER");
810         }
811
812         wprintf("<form name=\"changepwform\" action=\"changepw\" method=\"post\">\n");
813         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
814         wprintf("<table class=\"altern\" ");
815         wprintf("<tr class=\"even\"><td>");
816         wprintf(_("Enter new password:"));
817         wprintf("</td><td>");
818         wprintf("<input type=\"password\" name=\"newpass1\" value=\"\" maxlength=\"20\"></td></tr>\n");
819         wprintf("<tr class=\"odd\"><td>");
820         wprintf(_("Enter it again to confirm:"));
821         wprintf("</td><td>");
822         wprintf("<input type=\"password\" name=\"newpass2\" value=\"\" maxlength=\"20\"></td></tr>\n");
823         wprintf("</table>\n");
824
825         wprintf("<div class=\"buttons\">\n");
826         wprintf("<input type=\"submit\" name=\"change_action\" value=\"%s\">", _("Change password"));
827         wprintf("&nbsp;");
828         wprintf("<input type=\"submit\" name=\"cancel_action\" value=\"%s\">\n", _("Cancel"));
829         wprintf("</div>\n");
830         wprintf("</form>\n");
831
832         do_template("endbox", NULL);
833         wDumpContent(1);
834 }
835
836 /*
837  * change password
838  * if passwords match, propagate it to citserver.
839  */
840 void changepw(void)
841 {
842         char buf[SIZ];
843         char newpass1[32], newpass2[32];
844
845         if (!havebstr("change_action")) {
846                 safestrncpy(WC->ImportantMessage, 
847                         _("Cancelled.  Password was not changed."),
848                         sizeof WC->ImportantMessage);
849                 display_main_menu();
850                 return;
851         }
852
853         safestrncpy(newpass1, bstr("newpass1"), sizeof newpass1);
854         safestrncpy(newpass2, bstr("newpass2"), sizeof newpass2);
855
856         if (strcasecmp(newpass1, newpass2)) {
857                 safestrncpy(WC->ImportantMessage, 
858                         _("They don't match.  Password was not changed."),
859                         sizeof WC->ImportantMessage);
860                 display_changepw();
861                 return;
862         }
863
864         if (IsEmptyStr(newpass1)) {
865                 safestrncpy(WC->ImportantMessage, 
866                         _("Blank passwords are not allowed."),
867                         sizeof WC->ImportantMessage);
868                 display_changepw();
869                 return;
870         }
871
872         serv_printf("SETP %s", newpass1);
873         serv_getln(buf, sizeof buf);
874         sprintf(WC->ImportantMessage, "%s", &buf[4]);
875         if (buf[0] == '2') {
876                 safestrncpy(WC->wc_password, buf, sizeof WC->wc_password);
877                 display_main_menu();
878         }
879         else {
880                 display_changepw();
881         }
882 }
883
884 void _display_openid_login(void) {display_openid_login(NULL);}
885 void _display_reg(void) {display_reg(0);}
886
887
888 void 
889 InitModule_AUTH
890 (void)
891 {
892         WebcitAddUrlHandler(HKEY("do_welcome"), do_welcome, ANONYMOUS);
893         WebcitAddUrlHandler(HKEY("login"), do_login, ANONYMOUS);
894         WebcitAddUrlHandler(HKEY("display_openid_login"), _display_openid_login, ANONYMOUS);
895         WebcitAddUrlHandler(HKEY("openid_login"), do_openid_login, ANONYMOUS);
896         WebcitAddUrlHandler(HKEY("finalize_openid_login"), finalize_openid_login, ANONYMOUS);
897         WebcitAddUrlHandler(HKEY("openid_manual_create"), openid_manual_create, ANONYMOUS);
898         WebcitAddUrlHandler(HKEY("do_logout"), do_logout, 0);
899         WebcitAddUrlHandler(HKEY("validate"), validate, 0);
900         WebcitAddUrlHandler(HKEY("display_reg"), _display_reg, 0);
901         WebcitAddUrlHandler(HKEY("display_changepw"), display_changepw, 0);
902         WebcitAddUrlHandler(HKEY("changepw"), changepw, 0);
903         WebcitAddUrlHandler(HKEY("termquit"), do_logout, 0);
904         return ;
905 }