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