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