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