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