More license declaration changes which omit the bit about getting in touch with confi...
[citadel.git] / webcit / auth.c
1 /*
2  * These functions handle authentication of users to a Citadel server.
3  *
4  * Copyright (c) 1996-2012 by the citadel.org team
5  *
6  * This program is open source software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License, version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include "webcit.h"
16 #include "webserver.h"
17 #include <ctype.h>
18
19 extern uint32_t hashlittle( const void *key, size_t length, uint32_t initval);
20
21 /*
22  * Access level definitions.  This is initialized from a function rather than a
23  * static array so that the strings may be localized.
24  */
25 char *axdefs[7]; 
26
27 void initialize_axdefs(void) {
28
29         /* an erased user */
30         axdefs[0] = _("Deleted");       
31
32         /* a new user */
33         axdefs[1] = _("New User");      
34
35         /* a trouble maker */
36         axdefs[2] = _("Problem User");  
37
38         /* user with normal privileges */
39         axdefs[3] = _("Local User");    
40
41         /* a user that may access network resources */
42         axdefs[4] = _("Network User");  
43
44         /* a moderator */
45         axdefs[5] = _("Preferred User");
46
47         /* chief */
48         axdefs[6] = _("Aide");          
49 }
50
51
52
53 /* 
54  * Display the login screen
55  * mesg = the error message if last attempt failed.
56  */
57 void display_login(void)
58 {
59         begin_burst();
60         output_headers(1, 0, 0, 0, 1, 0);
61         do_template("login");
62         end_burst();
63 }
64
65
66
67
68
69 /* Initialize the session
70  *
71  * This function needs to get called whenever the session changes from
72  * not-logged-in to logged-in, either by an explicit login by the user or
73  * by a timed-out session automatically re-establishing with a little help
74  * from the browser cookie.  Either way, we need to load access controls and
75  * preferences from the server.
76  *
77  * user                 the username
78  * pass                 his password
79  * serv_response        The parameters returned from a Citadel USER or NEWU command
80  */
81 void become_logged_in(const StrBuf *user, const StrBuf *pass, StrBuf *serv_response)
82 {
83         wcsession *WCC = WC;
84         StrBuf *Buf;
85         StrBuf *FloorDiv;
86
87         WCC->logged_in = 1;
88
89         if (WCC->wc_fullname == NULL)
90                 WCC->wc_fullname = NewStrBufPlain(NULL, StrLength(serv_response));
91         StrBufExtract_token(WCC->wc_fullname, serv_response, 0, '|');
92         StrBufCutLeft(WCC->wc_fullname, 4 );
93         
94         if (WCC->wc_username == NULL)
95                 WCC->wc_username = NewStrBufDup(user);
96         else {
97                 FlushStrBuf(WCC->wc_username);
98                 StrBufAppendBuf(WCC->wc_username, user, 0);
99         }
100
101         if (WCC->wc_password == NULL)
102                 WCC->wc_password = NewStrBufDup(pass);
103         else {
104                 FlushStrBuf(WCC->wc_password);
105                 StrBufAppendBuf(WCC->wc_password, pass, 0);
106         }
107
108         WCC->axlevel = StrBufExtract_int(serv_response, 1, '|');
109         if (WCC->axlevel >= 6) {
110                 WCC->is_aide = 1;
111         }
112
113         load_preferences();
114
115         Buf = NewStrBuf();
116         serv_puts("CHEK");
117         StrBuf_ServGetln(Buf);
118         if (GetServerStatus(Buf, NULL) == 2) {
119                 const char *pch;
120
121                 pch = ChrPtr(Buf) + 4;
122                 /*WCC->new_mail  =*/ StrBufExtractNext_long(Buf, &pch, '|');
123                 WCC->need_regi = StrBufExtractNext_long(Buf, &pch, '|');
124                 WCC->need_vali = StrBufExtractNext_long(Buf, &pch, '|');
125                 if (WCC->cs_inet_email == NULL)
126                         WCC->cs_inet_email  = NewStrBuf();
127                 StrBufExtract_NextToken(WCC->cs_inet_email, Buf, &pch, '|');
128         }
129         get_preference("floordiv_expanded", &FloorDiv);
130         WCC->floordiv_expanded = FloorDiv;
131         FreeStrBuf(&Buf);
132         FlushRoomlist();
133 }
134
135
136 /* 
137  * modal/ajax version of 'login' (username and password)
138  */
139 void ajax_login_username_password(void) {
140         StrBuf *Buf = NewStrBuf();
141
142         serv_printf("USER %s", bstr("name"));
143         StrBuf_ServGetln(Buf);
144         if (GetServerStatus(Buf, NULL) == 3) {
145                 serv_printf("PASS %s", bstr("pass"));
146                 StrBuf_ServGetln(Buf);
147                 if (GetServerStatus(Buf, NULL) == 2) {
148                         become_logged_in(sbstr("name"), sbstr("pass"), Buf);
149                 }
150         }
151
152         /* The client is expecting to read back a citadel protocol response */
153         wc_printf("%s", ChrPtr(Buf));
154         FreeStrBuf(&Buf);
155 }
156
157
158
159 /* 
160  * modal/ajax version of 'new user' (username and password)
161  */
162 void ajax_login_newuser(void) {
163         StrBuf *NBuf = NewStrBuf();
164         StrBuf *SBuf = NewStrBuf();
165
166         serv_printf("NEWU %s", bstr("name"));
167         StrBuf_ServGetln(NBuf);
168         if (GetServerStatus(NBuf, NULL) == 2) {
169                 become_logged_in(sbstr("name"), sbstr("pass"), NBuf);
170                 serv_printf("SETP %s", bstr("pass"));
171                 StrBuf_ServGetln(SBuf);
172         }
173
174         /* The client is expecting to read back a citadel protocol response */
175         wc_printf("%s", ChrPtr(NBuf));
176         FreeStrBuf(&NBuf);
177         FreeStrBuf(&SBuf);
178 }
179
180
181
182 /* 
183  * Try to create an account manually after an OpenID was verified
184  */
185 void openid_manual_create(void)
186 {
187         StrBuf *Buf;
188
189         /* Did the user change his mind?  Pack up and go home. */
190         if (havebstr("exit_action")) {
191                 begin_burst();
192                 output_headers(1, 0, 0, 0, 1, 0);
193                 do_template("authpopup_finished");
194                 end_burst();
195                 return;
196         }
197
198
199         /* Ok, let's give this a try.  Can we create the new user? */
200
201         Buf = NewStrBuf();
202         serv_printf("OIDC %s", bstr("name"));
203         StrBuf_ServGetln(Buf);
204         if (GetServerStatus(Buf, NULL) == 2) {
205                 StrBuf *gpass;
206
207                 gpass = NewStrBuf();
208                 serv_puts("SETP GENERATE_RANDOM_PASSWORD");
209                 StrBuf_ServGetln(gpass);
210                 StrBufCutLeft(gpass, 4);
211                 become_logged_in(sbstr("name"), gpass, Buf);
212                 FreeStrBuf(&gpass);
213         }
214         FreeStrBuf(&Buf);
215
216         /* Did we manage to log in?  If so, continue with the normal flow... */
217         if (WC->logged_in) {
218                 if (WC->logged_in) {
219                         begin_burst();
220                         output_headers(1, 0, 0, 0, 1, 0);
221                         do_template("authpopup_finished");
222                         end_burst();
223                 }
224         } else {
225                 /* Still no good!  Go back to teh dialog to select a username */
226                 const StrBuf *Buf;
227                 putbstr("__claimed_id", NewStrBufDup(sbstr("openid_url")));
228                 Buf = sbstr("name");
229                 if (StrLength(Buf) > 0)
230                         putbstr("__username", NewStrBufDup(Buf));
231                 begin_burst();
232                 output_headers(1, 0, 0, 0, 1, 0);
233                 wc_printf("<html><body>");
234                 do_template("openid_manual_create");
235                 wc_printf("</body></html>");
236                 end_burst();
237         }
238
239 }
240
241
242 /* 
243  * Perform authentication using OpenID
244  * assemble the checkid_setup request and then redirect to the user's identity provider
245  */
246 void do_openid_login(void)
247 {
248         char buf[4096];
249
250         snprintf(buf, sizeof buf,
251                 "OIDS %s|%s/finalize_openid_login|%s",
252                 bstr("openid_url"),
253                 ChrPtr(site_prefix),
254                 ChrPtr(site_prefix)
255         );
256
257         serv_puts(buf);
258         serv_getln(buf, sizeof buf);
259         if (buf[0] == '2') {
260                 syslog(LOG_DEBUG, "OpenID server contacted; redirecting to %s\n", &buf[4]);
261                 http_redirect(&buf[4]);
262                 return;
263         }
264
265         begin_burst();
266         output_headers(1, 0, 0, 0, 1, 0);
267         wc_printf("<html><body>");
268         escputs(&buf[4]);
269         wc_printf("</body></html>");
270         end_burst();
271 }
272
273
274 /* 
275  * Complete the authentication using OpenID
276  * This function handles the positive or negative assertion from the user's Identity Provider
277  */
278 void finalize_openid_login(void)
279 {
280         StrBuf *Buf;
281         wcsession *WCC = WC;
282         int linecount = 0;
283         StrBuf *result = NULL;
284         StrBuf *username = NULL;
285         StrBuf *password = NULL;
286         StrBuf *logged_in_response = NULL;
287         StrBuf *claimed_id = NULL;
288
289         if (havebstr("openid.mode")) {
290                 if (!strcasecmp(bstr("openid.mode"), "id_res")) {
291                         Buf = NewStrBuf();
292                         serv_puts("OIDF");
293                         StrBuf_ServGetln(Buf);
294                         if (GetServerStatus(Buf, NULL) == 8) {
295                                 urlcontent *u;
296                                 void *U;
297                                 long HKLen;
298                                 const char *HKey;
299                                 HashPos *Cursor;
300                                 int len;
301                                 
302                                 Cursor = GetNewHashPos (WCC->Hdr->urlstrings, 0);
303                                 while (GetNextHashPos(WCC->Hdr->urlstrings, Cursor, &HKLen, &HKey, &U)) {
304                                         u = (urlcontent*) U;
305                                         if (!strncasecmp(u->url_key, "openid.", 7)) {
306                                                 serv_printf("%s|%s", &u->url_key[7], ChrPtr(u->url_data));
307                                         }
308                                 }
309
310                                 serv_puts("000");
311
312                                 linecount = 0;
313                                 while (len = StrBuf_ServGetln(Buf), 
314                                        ((len >= 0) &&
315                                         ((len != 3) || strcmp(ChrPtr(Buf), "000") )))
316                                 {
317                                         if (linecount == 0) result = NewStrBufDup(Buf);
318                                         if (!strcasecmp(ChrPtr(result), "authenticate")) {
319                                                 if (linecount == 1) {
320                                                         username = NewStrBufDup(Buf);
321                                                 }
322                                                 else if (linecount == 2) {
323                                                         password = NewStrBufDup(Buf);
324                                                 }
325                                                 else if (linecount == 3) {
326                                                         logged_in_response = NewStrBufDup(Buf);
327                                                 }
328                                         }
329                                         else if (!strcasecmp(ChrPtr(result), "verify_only")) {
330                                                 if (linecount == 1) {
331                                                         claimed_id = NewStrBufDup(Buf);
332                                                 }
333                                                 if (linecount == 2) {
334                                                         username = NewStrBufDup(Buf);
335                                                 }
336                                         }
337                                         ++linecount;
338                                 }
339                         }
340                         FreeStrBuf(&Buf);
341                 }
342         }
343
344         /*
345          * Is this an attempt to associate a new OpenID with an account that is already logged in?
346          */
347         if ( (WCC->logged_in) && (havebstr("attach_existing")) ) {
348                 display_openids();
349         }
350
351         /* If this operation logged us in, either by connecting with an existing account or by
352          * auto-creating one using Simple Registration Extension, we're already on our way.
353          */
354         else if (!strcasecmp(ChrPtr(result), "authenticate")) {
355                 become_logged_in(username, password, logged_in_response);
356
357                 /* Did we manage to log in?  If so, continue with the normal flow... */
358                 if (WC->logged_in) {
359                         begin_burst();
360                         output_headers(1, 0, 0, 0, 1, 0);
361                         do_template("authpopup_finished");
362                         end_burst();
363                 } else {
364                         begin_burst();
365                         output_headers(1, 0, 0, 0, 1, 0);
366                         wc_printf("<html><body>");
367                         wc_printf(_("An error has occurred."));
368                         wc_printf("</body></html>");
369                         end_burst();
370                 }
371         }
372
373         /* The specified OpenID was verified but the desired user name was either not specified via SRE
374          * or conflicts with an existing user.  Either way the user will need to specify a new name.
375          */
376         else if (!strcasecmp(ChrPtr(result), "verify_only")) {
377                 putbstr("__claimed_id", claimed_id);
378                 claimed_id = NULL;
379                 if (StrLength(username) > 0) {
380                         putbstr("__username", username);
381                         username = NULL;
382                 }
383                 begin_burst();
384                 output_headers(1, 0, 0, 0, 1, 0);
385                 wc_printf("<html><body>");
386                 do_template("openid_manual_create");
387                 wc_printf("</body></html>");
388                 end_burst();
389         }
390
391         /* Something went VERY wrong if we get to this point */
392         else {
393                 syslog(1, "finalize_openid_login() failed to do anything.  This is a code problem.\n");
394                 begin_burst();
395                 output_headers(1, 0, 0, 0, 1, 0);
396                 wc_printf("<html><body>");
397                 wc_printf(_("An error has occurred."));
398                 wc_printf("</body></html>");
399                 end_burst();
400         }
401
402         FreeStrBuf(&result);
403         FreeStrBuf(&username);
404         FreeStrBuf(&password);
405         FreeStrBuf(&claimed_id);
406         FreeStrBuf(&logged_in_response);
407 }
408
409
410 /*
411  * Display a welcome screen to the user.
412  *
413  * If this is the first time login, and the web based setup is enabled, 
414  * lead the user through the setup routines
415  */
416 void do_welcome(void)
417 {
418         StrBuf *Buf;
419 #ifdef XXX_NOT_FINISHED_YET_XXX
420         FILE *fp;
421         int i;
422
423         /**
424          * See if we have to run the first-time setup wizard
425          */
426         if (WC->is_aide) {
427                 if (!setup_wizard) {
428                         int len;
429                         sprintf(wizard_filename, "setupwiz.%s.%s",
430                                 abs(HashLittle(ctdlhost, strlen(ctdlhost))),
431                                 abs(HashLittle(ctdlport, strlen(ctdlport)))
432                         );
433
434                         fp = fopen(wizard_filename, "r");
435                         if (fp != NULL) {
436                                 fgets(buf, sizeof buf, fp);
437                                 buf[strlen(buf)-1] = 0;
438                                 fclose(fp);
439                                 if (atoi(buf) == serv_info.serv_rev_level) {
440                                         setup_wizard = 1;       /* already run */
441                                 }
442                         }
443                 }
444
445                 if (!setup_wizard) {
446                         http_redirect("setup_wizard");
447                 }
448         }
449 #endif
450
451         /*
452          * Go to the user's preferred start page
453          */
454         if (!get_preference("startpage", &Buf)) {
455                 Buf = NewStrBuf ();
456                 StrBufPrintf(Buf, "dotskip?room=_BASEROOM_");
457                 set_preference("startpage", Buf, 1);
458         }
459         if (ChrPtr(Buf)[0] == '/') {
460                 StrBufCutLeft(Buf, 1);
461         }
462         if (StrLength(Buf) == 0) {
463                 StrBufAppendBufPlain(Buf, "dotgoto?room=_BASEROOM_", -1, 0);
464         }
465         syslog(9, "Redirecting to user's start page: %s\n", ChrPtr(Buf));
466         http_redirect(ChrPtr(Buf));
467 }
468
469
470 /*
471  * Disconnect from the Citadel server, and end this WebCit session
472  */
473 void end_webcit_session(void) {
474         serv_puts("QUIT");
475         WC->killthis = 1;
476         /* close() of citadel socket will be done by do_housekeeping() */
477 }
478
479
480 /* 
481  * Log out the session with the Citadel server
482  */
483 void do_logout(void)
484 {
485         wcsession *WCC = WC;
486         char buf[SIZ];
487
488         FlushStrBuf(WCC->wc_username);
489         FlushStrBuf(WCC->wc_password);
490         FlushStrBuf(WCC->wc_fullname);
491         FlushRoomlist();
492
493         serv_puts("LOUT");
494         serv_getln(buf, sizeof buf);
495         WCC->logged_in = 0;
496
497         FlushStrBuf(WCC->CurRoom.name);
498
499         /* Calling output_headers() this way causes the cookies to be un-set */
500         output_headers(1, 1, 0, 1, 0, 0);
501
502         /* For sites in guest mode, redirect to the landing page after we're logged out */
503         if (WC->serv_info->serv_supports_guest) {
504                 wc_printf("     <script type=\"text/javascript\">       "
505                         "       window.location='/';                    "
506                         "       </script>                               "
507                 );
508         }
509
510         wc_printf("<div id=\"logout_screen\">");
511         wc_printf("<div class=\"box\">");
512         wc_printf("<div class=\"boxlabel\">");
513         wc_printf(_("Log off"));
514         wc_printf("</div><div class=\"boxcontent\">");
515         serv_puts("MESG goodbye");
516         serv_getln(buf, sizeof buf);
517
518         if (WCC->serv_sock >= 0) {
519                 if (buf[0] == '1') {
520                         fmout("'CENTER'");
521                 } else {
522                         wc_printf("Goodbye\n");
523                 }
524         }
525         else {
526                 wc_printf(_("This program was unable to connect or stay "
527                         "connected to the Citadel server.  Please report "
528                         "this problem to your system administrator.")
529                 );
530                 wc_printf("<a href=\"http://www.citadel.org/doku.php/"
531                         "faq:mastering_your_os:net#netstat\">%s</a>",
532                         _("Read More..."));
533         }
534
535         wc_printf("<hr /><div class=\"buttons\"> "
536                 "<span class=\"button_link\"><a href=\".\">");
537         wc_printf(_("Log in again"));
538         wc_printf("</a></span>");
539         wc_printf("</div></div></div>\n");
540         if (WC->serv_info->serv_supports_guest) {
541                 display_default_landing_page();
542                 return;
543         }
544
545         wDumpContent(2);
546         end_webcit_session();
547 }
548
549
550 /* 
551  * Special page for monitoring scripts etc
552  */
553 void monitor(void)
554 {
555         output_headers(0, 0, 0, 0, 0, 0);
556
557         hprintf("Content-type: text/plain\r\n"
558                 "Server: " PACKAGE_STRING "\r\n"
559                 "Connection: close\r\n"
560         );
561         begin_burst();
562
563         wc_printf("Connection to Citadel server at %s:%s : %s\r\n",
564                 ctdlhost, ctdlport,
565                 (WC->connected ? "SUCCESS" : "FAIL")
566         );
567
568         wDumpContent(0);
569 }
570
571
572 /*
573  * validate new users
574  */
575 void validate(void)
576 {
577         char cmd[SIZ];
578         char user[SIZ];
579         char buf[SIZ];
580         int a;
581
582         output_headers(1, 1, 1, 0, 0, 0);
583
584         do_template("box_begin_1");
585         StrBufAppendBufPlain(WC->WBuf, _("Validate new users"), -1, 0);
586         do_template("box_begin_2");
587
588         /* If the user just submitted a validation, process it... */
589         safestrncpy(buf, bstr("user"), sizeof buf);
590         if (!IsEmptyStr(buf)) {
591                 if (havebstr("axlevel")) {
592                         serv_printf("VALI %s|%s", buf, bstr("axlevel"));
593                         serv_getln(buf, sizeof buf);
594                         if (buf[0] != '2') {
595                                 wc_printf("<b>%s</b><br>\n", &buf[4]);
596                         }
597                 }
598         }
599
600         /* Now see if any more users require validation. */
601         serv_puts("GNUR");
602         serv_getln(buf, sizeof buf);
603         if (buf[0] == '2') {
604                 wc_printf("<b>");
605                 wc_printf(_("No users require validation at this time."));
606                 wc_printf("</b><br>\n");
607                 wDumpContent(1);
608                 return;
609         }
610         if (buf[0] != '3') {
611                 wc_printf("<b>%s</b><br>\n", &buf[4]);
612                 wDumpContent(1);
613                 return;
614         }
615
616         wc_printf("<table class=\"auth_validate\"><tr><td>\n");
617         wc_printf("<div id=\"validate\">");
618
619         safestrncpy(user, &buf[4], sizeof user);
620         serv_printf("GREG %s", user);
621         serv_getln(cmd, sizeof cmd);
622         if (cmd[0] == '1') {
623                 a = 0;
624                 do {
625                         serv_getln(buf, sizeof buf);
626                         ++a;
627                         if (a == 1)
628                                 wc_printf("#%s<br><H1>%s</H1>",
629                                         buf, &cmd[4]);
630                         if (a == 2) {
631                                 char *pch;
632                                 int haveChar = 0;
633                                 int haveNum = 0;
634                                 int haveOther = 0;
635                                 int haveLong = 0;
636                                 pch = buf;
637                                 while (!IsEmptyStr(pch))
638                                 {
639                                         if (isdigit(*pch))
640                                                 haveNum = 1;
641                                         else if (isalpha(*pch))
642                                                 haveChar = 1;
643                                         else
644                                                 haveOther = 1;
645                                         pch ++;
646                                 }
647                                 if (pch - buf > 7)
648                                         haveLong = 1;
649                                 switch (haveLong + 
650                                         haveChar + 
651                                         haveNum + 
652                                         haveOther)
653                                 {
654                                 case 0:
655                                         pch = _("very weak");
656                                         break;
657                                 case 1:
658                                         pch = _("weak");
659                                         break;
660                                 case 2:
661                                         pch = _("ok");
662                                         break;
663                                 case 3:
664                                 default:
665                                         pch = _("strong");
666                                 }
667
668                                 wc_printf("PW: %s<br>\n", pch);
669                         }
670                         if (a == 3)
671                                 wc_printf("%s<br>\n", buf);
672                         if (a == 4)
673                                 wc_printf("%s<br>\n", buf);
674                         if (a == 5)
675                                 wc_printf("%s, ", buf);
676                         if (a == 6)
677                                 wc_printf("%s ", buf);
678                         if (a == 7)
679                                 wc_printf("%s<br>\n", buf);
680                         if (a == 8)
681                                 wc_printf("%s<br>\n", buf);
682                         if (a == 9)
683                                 wc_printf(_("Current access level: %d (%s)\n"),
684                                         atoi(buf), axdefs[atoi(buf)]);
685                 } while (strcmp(buf, "000"));
686         } else {
687                 wc_printf("<H1>%s</H1>%s<br>\n", user, &cmd[4]);
688         }
689
690         wc_printf("<hr />");
691         wc_printf(_("Select access level for this user:"));
692         wc_printf("<br>\n");
693         for (a = 0; a <= 6; ++a) {
694                 wc_printf("<a href=\"validate?nonce=%d?user=", WC->nonce);
695                 urlescputs(user);
696                 wc_printf("&axlevel=%d\">%s</A>&nbsp;&nbsp;&nbsp;\n",
697                         a, axdefs[a]);
698         }
699         wc_printf("<br>\n");
700
701         wc_printf("</div>\n");
702         wc_printf("</td></tr></table>\n");
703         do_template("box_end");
704         wDumpContent(1);
705 }
706
707
708 /*
709  * Display form for registration.
710  *
711  * (Set during_login to 1 if this registration is being performed during
712  * new user login and will require chaining to the proper screen.)
713  */
714 void display_reg(int during_login)
715 {
716         folder Room;
717         StrBuf *Buf;
718         message_summary *VCMsg = NULL;
719         wc_mime_attachment *VCAtt = NULL;
720         long vcard_msgnum;
721
722         Buf = NewStrBuf();
723         memset(&Room, 0, sizeof(folder));
724         if (goto_config_room(Buf, &Room) != 0) {
725                 syslog(9, "display_reg() exiting because goto_config_room() failed\n");
726                 if (during_login) {
727                         pop_destination();
728                 }
729                 else {
730                         display_main_menu();
731                 }
732                 FreeStrBuf(&Buf);
733                 FlushFolder(&Room);             
734                 return;
735         }
736         FlushFolder(&Room);
737
738         FreeStrBuf(&Buf);
739         vcard_msgnum = locate_user_vcard_in_this_room(&VCMsg, &VCAtt);
740         if (vcard_msgnum < 0L) {
741                 syslog(9, "display_reg() exiting because locate_user_vcard_in_this_room() failed\n");
742                 if (during_login) {
743                         pop_destination();
744                 }
745                 else {
746                         display_main_menu();
747                 }
748                 return;
749         }
750
751         if (during_login) {
752                 do_edit_vcard(vcard_msgnum, "1", VCMsg, VCAtt, "pop", USERCONFIGROOM);
753         }
754         else {
755                 StrBuf *ReturnTo;
756                 ReturnTo = NewStrBufPlain(HKEY("display_main_menu?go="));
757                 StrBufAppendBuf(ReturnTo, WC->CurRoom.name, 0);
758                 do_edit_vcard(vcard_msgnum, "1", VCMsg, VCAtt, ChrPtr(ReturnTo), USERCONFIGROOM);
759                 FreeStrBuf(&ReturnTo);
760         }
761
762 }
763
764
765 /*
766  * display form for changing your password
767  */
768 void display_changepw(void)
769 {
770         wcsession *WCC = WC;
771         WCTemplputParams SubTP;
772         char buf[SIZ];
773         StrBuf *Buf;
774         output_headers(1, 1, 1, 0, 0, 0);
775
776         Buf = NewStrBufPlain(_("Change your password"), -1);
777         memset(&SubTP, 0, sizeof(WCTemplputParams));
778         SubTP.Filter.ContextType = CTX_STRBUF;
779         SubTP.Context = Buf;
780         DoTemplate(HKEY("box_begin"), NULL, &SubTP);
781
782         FreeStrBuf(&Buf);
783
784         if (StrLength(WCC->ImportantMsg) > 0) {
785                 wc_printf("<span class=\"errormsg\">"
786                           "%s</span><br>\n", ChrPtr(WCC->ImportantMsg));
787                 FlushStrBuf(WCC->ImportantMsg);
788         }
789
790         serv_puts("MESG changepw");
791         serv_getln(buf, sizeof buf);
792         if (buf[0] == '1') {
793                 fmout("CENTER");
794         }
795
796         wc_printf("<form name=\"changepwform\" action=\"changepw\" method=\"post\">\n");
797         wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
798         wc_printf("<table class=\"altern\" ");
799         wc_printf("<tr class=\"even\"><td>");
800         wc_printf(_("Enter new password:"));
801         wc_printf("</td><td>");
802         wc_printf("<input type=\"password\" name=\"newpass1\" value=\"\" maxlength=\"20\"></td></tr>\n");
803         wc_printf("<tr class=\"odd\"><td>");
804         wc_printf(_("Enter it again to confirm:"));
805         wc_printf("</td><td>");
806         wc_printf("<input type=\"password\" name=\"newpass2\" value=\"\" maxlength=\"20\"></td></tr>\n");
807         wc_printf("</table>\n");
808
809         wc_printf("<div class=\"buttons\">\n");
810         wc_printf("<input type=\"submit\" name=\"change_action\" value=\"%s\">", _("Change password"));
811         wc_printf("&nbsp;");
812         wc_printf("<input type=\"submit\" name=\"cancel_action\" value=\"%s\">\n", _("Cancel"));
813         wc_printf("</div>\n");
814         wc_printf("</form>\n");
815
816         do_template("box_end");
817         wDumpContent(1);
818 }
819
820 /*
821  * change password
822  * if passwords match, propagate it to citserver.
823  */
824 void changepw(void)
825 {
826         StrBuf *Line;
827         char newpass1[32], newpass2[32];
828
829         if (!havebstr("change_action")) {
830                 AppendImportantMessage(_("Cancelled.  Password was not changed."), -1);
831                 display_main_menu();
832                 return;
833         }
834
835         safestrncpy(newpass1, bstr("newpass1"), sizeof newpass1);
836         safestrncpy(newpass2, bstr("newpass2"), sizeof newpass2);
837
838         if (strcasecmp(newpass1, newpass2)) {
839                 AppendImportantMessage(_("They don't match.  Password was not changed."), -1);
840                 display_changepw();
841                 return;
842         }
843
844         if (IsEmptyStr(newpass1)) {
845                 AppendImportantMessage(_("Blank passwords are not allowed."), -1);
846                 display_changepw();
847                 return;
848         }
849
850         Line = NewStrBuf();
851         serv_printf("SETP %s", newpass1);
852         StrBuf_ServGetln(Line);
853         if (GetServerStatusMsg(Line, NULL, 1, 0) == 2) {
854                 if (WC->wc_password == NULL)
855                         WC->wc_password = NewStrBufPlain(
856                                 ChrPtr(Line) + 4, 
857                                 StrLength(Line) - 4);
858                 else {
859                         FlushStrBuf(WC->wc_password);
860                         StrBufAppendBufPlain(WC->wc_password,  
861                                              ChrPtr(Line) + 4, 
862                                              StrLength(Line) - 4, 0);
863                 }
864                 display_main_menu();
865         }
866         else {
867                 display_changepw();
868         }
869         FreeStrBuf(&Line);
870 }
871
872
873 int ConditionalHaveAccessCreateRoom(StrBuf *Target, WCTemplputParams *TP)
874 {
875         StrBuf *Buf;    
876
877         Buf = NewStrBuf();
878         serv_puts("CRE8 0");
879         StrBuf_ServGetln(Buf);
880
881         if (GetServerStatus(Buf, NULL) == 2) {
882                 StrBufCutLeft(Buf, 4);
883                 AppendImportantMessage(SKEY(Buf));
884                 FreeStrBuf(&Buf);
885                 return 0;
886         }
887         FreeStrBuf(&Buf);
888         return 1;
889 }
890
891
892 int ConditionalAide(StrBuf *Target, WCTemplputParams *TP)
893 {
894         wcsession *WCC = WC;
895         return (WCC != NULL) ? ((WCC->logged_in == 0)||(WC->is_aide == 0)) : 0;
896 }
897
898
899 int ConditionalIsLoggedIn(StrBuf *Target, WCTemplputParams *TP) 
900 {
901         wcsession *WCC = WC;
902         return (WCC != NULL) ? (WCC->logged_in == 0) : 0;
903
904 }
905
906
907 /* 
908  * toggle the session over to a different language
909  */
910 void switch_language(void) {
911         set_selected_language(bstr("lang"));
912         pop_destination();
913 }
914
915
916 void _display_reg(void) {
917         display_reg(0);
918 }
919
920
921 void Header_HandleAuth(StrBuf *Line, ParsedHttpHdrs *hdr)
922 {
923         if (hdr->HR.got_auth == NO_AUTH) /* don't override cookie auth... */
924         {
925                 if (strncasecmp(ChrPtr(Line), "Basic", 5) == 0) {
926                         StrBufCutLeft(Line, 6);
927                         StrBufDecodeBase64(Line);
928                         hdr->HR.plainauth = Line;
929                         hdr->HR.got_auth = AUTH_BASIC;
930                 }
931                 else 
932                         syslog(1, "Authentication scheme not supported! [%s]\n", ChrPtr(Line));
933         }
934 }
935
936
937 void CheckAuthBasic(ParsedHttpHdrs *hdr)
938 {
939 /*
940   todo: enable this if we can have other sessions than authenticated ones.
941         if (hdr->DontNeedAuth)
942                 return;
943 */
944         StrBufAppendBufPlain(hdr->HR.plainauth, HKEY(":"), 0);
945         StrBufAppendBuf(hdr->HR.plainauth, hdr->HR.user_agent, 0);
946 }
947
948
949 void GetAuthBasic(ParsedHttpHdrs *hdr)
950 {
951         const char *Pos = NULL;
952         if (hdr->c_username == NULL)
953                 hdr->c_username = NewStrBufPlain(HKEY(DEFAULT_HTTPAUTH_USER));
954         if (hdr->c_password == NULL)
955                 hdr->c_password = NewStrBufPlain(HKEY(DEFAULT_HTTPAUTH_PASS));
956         StrBufExtract_NextToken(hdr->c_username, hdr->HR.plainauth, &Pos, ':');
957         StrBufExtract_NextToken(hdr->c_password, hdr->HR.plainauth, &Pos, ':');
958 }
959
960
961 void Header_HandleCookie(StrBuf *Line, ParsedHttpHdrs *hdr)
962 {
963         const char *pch;
964 /*
965   todo: enable this if we can have other sessions than authenticated ones.
966         if (hdr->DontNeedAuth)
967                 return;
968 */
969         pch = strstr(ChrPtr(Line), "webcit=");
970         if (pch == NULL) {
971                 return;
972         }
973
974         hdr->HR.RawCookie = Line;
975         StrBufCutLeft(hdr->HR.RawCookie, (pch - ChrPtr(hdr->HR.RawCookie)) + 7);
976         StrBufDecodeHex(hdr->HR.RawCookie);
977
978         cookie_to_stuff(Line, &hdr->HR.desired_session,
979                         hdr->c_username,
980                         hdr->c_password,
981                         hdr->c_roomname,
982                         hdr->c_language
983         );
984         hdr->HR.got_auth = AUTH_COOKIE;
985 }
986
987
988 void 
989 HttpNewModule_AUTH
990 (ParsedHttpHdrs *httpreq)
991 {
992         httpreq->c_username = NewStrBufPlain(HKEY(DEFAULT_HTTPAUTH_USER));
993         httpreq->c_password = NewStrBufPlain(HKEY(DEFAULT_HTTPAUTH_PASS));
994         httpreq->c_roomname = NewStrBuf();
995         httpreq->c_language = NewStrBuf();
996 }
997
998
999 void 
1000 HttpDetachModule_AUTH
1001 (ParsedHttpHdrs *httpreq)
1002 {
1003         FLUSHStrBuf(httpreq->c_username);
1004         FLUSHStrBuf(httpreq->c_password);
1005         FLUSHStrBuf(httpreq->c_roomname);
1006         FLUSHStrBuf(httpreq->c_language);
1007 }
1008
1009
1010 void 
1011 HttpDestroyModule_AUTH
1012 (ParsedHttpHdrs *httpreq)
1013 {
1014         FreeStrBuf(&httpreq->c_username);
1015         FreeStrBuf(&httpreq->c_password);
1016         FreeStrBuf(&httpreq->c_roomname);
1017         FreeStrBuf(&httpreq->c_language);
1018 }
1019
1020
1021 void 
1022 InitModule_AUTH
1023 (void)
1024 {
1025         initialize_axdefs();
1026         RegisterHeaderHandler(HKEY("COOKIE"), Header_HandleCookie);
1027         RegisterHeaderHandler(HKEY("AUTHORIZATION"), Header_HandleAuth);
1028
1029         /* no url pattern at all? Show login. */
1030         WebcitAddUrlHandler(HKEY(""), "", 0, do_welcome, ANONYMOUS|COOKIEUNNEEDED);
1031
1032         WebcitAddUrlHandler(HKEY("do_welcome"), "", 0, do_welcome, ANONYMOUS|COOKIEUNNEEDED);
1033         WebcitAddUrlHandler(HKEY("openid_login"), "", 0, do_openid_login, ANONYMOUS);
1034         WebcitAddUrlHandler(HKEY("finalize_openid_login"), "", 0, finalize_openid_login, ANONYMOUS);
1035         WebcitAddUrlHandler(HKEY("openid_manual_create"), "", 0, openid_manual_create, ANONYMOUS);
1036         WebcitAddUrlHandler(HKEY("validate"), "", 0, validate, 0);
1037         WebcitAddUrlHandler(HKEY("do_welcome"), "", 0, do_welcome, 0);
1038         WebcitAddUrlHandler(HKEY("display_reg"), "", 0, _display_reg, 0);
1039         WebcitAddUrlHandler(HKEY("display_changepw"), "", 0, display_changepw, 0);
1040         WebcitAddUrlHandler(HKEY("changepw"), "", 0, changepw, 0);
1041         WebcitAddUrlHandler(HKEY("termquit"), "", 0, do_logout, 0);
1042         WebcitAddUrlHandler(HKEY("do_logout"), "", 0, do_logout, ANONYMOUS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
1043         WebcitAddUrlHandler(HKEY("monitor"), "", 0, monitor, ANONYMOUS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
1044         WebcitAddUrlHandler(HKEY("ajax_login_username_password"), "", 0, ajax_login_username_password, AJAX|ANONYMOUS);
1045         WebcitAddUrlHandler(HKEY("ajax_login_newuser"), "", 0, ajax_login_newuser, AJAX|ANONYMOUS);
1046         WebcitAddUrlHandler(HKEY("switch_language"), "", 0, switch_language, ANONYMOUS);
1047         RegisterConditional(HKEY("COND:AIDE"), 2, ConditionalAide, CTX_NONE);
1048         RegisterConditional(HKEY("COND:LOGGEDIN"), 2, ConditionalIsLoggedIn, CTX_NONE);
1049         RegisterConditional(HKEY("COND:MAY_CREATE_ROOM"), 2,  ConditionalHaveAccessCreateRoom, CTX_NONE);
1050         return;
1051 }
1052
1053
1054 void 
1055 SessionDestroyModule_AUTH
1056 (wcsession *sess)
1057 {
1058         FreeStrBuf(&sess->wc_username);
1059         FreeStrBuf(&sess->wc_fullname);
1060         FreeStrBuf(&sess->wc_password);
1061         FreeStrBuf(&sess->httpauth_pass);
1062         FreeStrBuf(&sess->cs_inet_email);
1063 }