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