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