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