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