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