Moved all the OpenID Relying Party code that I've written so far
[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
10 /*
11  * Access level definitions.  This is initialized from a function rather than a
12  * static array so that the strings may be localized.
13  */
14 char *axdefs[7]; 
15
16 void initialize_axdefs(void) {
17         axdefs[0] = _("Deleted");       /* an erased user */
18         axdefs[1] = _("New User");      /* a new user */
19         axdefs[2] = _("Problem User");  /* a trouble maker */
20         axdefs[3] = _("Local User");    /* user with normal privileges */
21         axdefs[4] = _("Network User");  /* a user that may access network resources */
22         axdefs[5] = _("Preferred User");/* a moderator */
23         axdefs[6] = _("Aide");          /* chief */
24 }
25
26
27
28
29 /* 
30  * Display the login screen
31  * mesg = the error message if last attempt failed.
32  */
33 void display_login(char *mesg)
34 {
35         char buf[SIZ];
36
37         output_headers(1, 1, 2, 0, 0, 0);
38         wprintf("<div id=\"login_screen\">\n");
39
40         if (mesg != NULL) if (!IsEmptyStr(mesg)) {
41                         stresc(buf, SIZ,  mesg, 0, 0);
42                         svprintf(HKEY("MESG"), WCS_STRING, "%s", buf);
43         }
44
45         svprintf(HKEY("LOGIN_INSTRUCTIONS"), WCS_STRING,
46                 _("<ul>"
47                 "<li><b>If you already have an account on %s</b>, "
48                 "enter your user name and password and click &quot;Login.&quot; "
49                 "<li><b>If you are a new user</b>, enter the name and password "
50                 "you wish to use, "
51                 "and click &quot;New User.&quot; "
52                 "<li>Please log off properly when finished. "
53                 "<li>You must use a browser that supports <i>frames</i> and "
54                 "<i>cookies</i>. "
55                 "<li>Also keep in mind that if your browser is "
56                 "configured to block pop-up windows, you will not be able "
57                 "to receive any instant messages.<br />"
58                 "</ul>"),
59                 serv_info.serv_humannode
60         );
61
62         svput("USERNAME_BOX", WCS_STRING, _("User name:"));
63         svput("PASSWORD_BOX", WCS_STRING, _("Password:"));
64         svput("LANGUAGE_BOX", WCS_STRING, _("Language:"));
65         svput("LOGIN_BUTTON", WCS_STRING, _("Login"));
66         svput("NEWUSER_BUTTON", WCS_STRING, _("New User"));
67         svput("EXIT_BUTTON", WCS_STRING, _("Exit"));
68         svput("HELLO", WCS_SERVCMD, "MESG hello");
69         svprintf(HKEY("BOXTITLE"), WCS_STRING, _("%s - powered by <a href=\"http://www.citadel.org\">Citadel</a>"),
70                 serv_info.serv_humannode);
71         svcallback("DO_LANGUAGE_BOX", offer_languages);
72         if (serv_info.serv_newuser_disabled) {
73                 svput("NEWUSER_BUTTON_PRE", WCS_STRING, "<div style=\"display:none;\">");
74                 svput("NEWUSER_BUTTON_POST", WCS_STRING, "</div>");
75         }
76         else {
77                 svput("NEWUSER_BUTTON_PRE", WCS_STRING, "");
78                 svput("NEWUSER_BUTTON_POST", WCS_STRING, "");
79         }
80
81 #ifdef TECH_PREVIEW
82                 svprintf(HKEY("OFFER_OPENID_LOGIN"), WCS_STRING,
83                         "<div align=center>"
84                         "<a href=\"display_openid_login\">"
85                         "<img src=\"static/openid-small.gif\" border=0 valign=middle>"
86                         "%s</a>"
87                         "</div>"
88                         ,
89                         "Log in using OpenID"
90                 );
91 #else
92                 svput("OFFER_OPENID_LOGIN", WCS_STRING, "");
93 #endif
94
95         do_template("login");
96
97         wDumpContent(2);
98 }
99
100
101
102
103 /* 
104  * Display the openid-enabled login screen
105  * mesg = the error message if last attempt failed.
106  */
107 void display_openid_login(char *mesg)
108 {
109         char buf[SIZ];
110
111         output_headers(1, 1, 2, 0, 0, 0);
112         wprintf("<div id=\"login_screen\">\n");
113
114         if (mesg != NULL) if (!IsEmptyStr(mesg)) {
115                         stresc(buf, SIZ,  mesg, 0, 0);
116                         svprintf(HKEY("MESG"), WCS_STRING, "%s", buf);
117         }
118
119         svprintf(HKEY("LOGIN_INSTRUCTIONS"), WCS_STRING,
120                 _("<ul>"
121                 "<li>Enter your OpenID URL and click &quot;Login&quot;."
122                 "<li>Please log off properly when finished. "
123                 "<li>You must use a browser that supports <i>frames</i> and "
124                 "<i>cookies</i>. "
125                 "<li>Also keep in mind that if your browser is "
126                 "configured to block pop-up windows, you will not be able "
127                 "to receive any instant messages.<br />"
128                 "</ul>")
129         );
130
131         svput("OPENID_BOX", WCS_STRING, _("OpenID URL:"));
132         svput("LANGUAGE_BOX", WCS_STRING, _("Language:"));
133         svput("LOGIN_BUTTON", WCS_STRING, _("Login"));
134         svput("EXIT_BUTTON", WCS_STRING, _("Exit"));
135         svput("HELLO", WCS_SERVCMD, "MESG hello");
136         svprintf(HKEY("BOXTITLE"), WCS_STRING, _("%s - powered by <a href=\"http://www.citadel.org\">Citadel</a>"),
137                 serv_info.serv_humannode);
138         svcallback("DO_LANGUAGE_BOX", offer_languages);
139
140         svprintf(HKEY("OFFER_CONVENTIONAL_LOGIN"), WCS_STRING,
141                 "<div align=center>"
142                 "<a href=\"display_login\">"
143                 "%s</a>"
144                 "</div>"
145                 ,
146                 "Log in using a user name and password"
147         );
148
149         do_template("openid_login");
150         wDumpContent(2);
151 }
152
153
154
155
156 /* Initialize the session
157  *
158  * This function needs to get called whenever the session changes from
159  * not-logged-in to logged-in, either by an explicit login by the user or
160  * by a timed-out session automatically re-establishing with a little help
161  * from the browser cookie.  Either way, we need to load access controls and
162  * preferences from the server.
163  *
164  * user                 the username
165  * pass                 his password
166  * serv_response        The parameters returned from a Citadel USER or NEWU command
167  */
168 void become_logged_in(char *user, char *pass, char *serv_response)
169 {
170         char buf[SIZ];
171
172         WC->logged_in = 1;
173         extract_token(WC->wc_fullname, &serv_response[4], 0, '|', sizeof WC->wc_fullname);
174         safestrncpy(WC->wc_username, user, sizeof WC->wc_username);
175         safestrncpy(WC->wc_password, pass, sizeof WC->wc_password);
176         WC->axlevel = extract_int(&serv_response[4], 1);
177         if (WC->axlevel >= 6) {
178                 WC->is_aide = 1;
179         }
180
181         load_preferences();
182
183         serv_puts("CHEK");
184         serv_getln(buf, sizeof buf);
185         if (buf[0] == '2') {
186                 WC->new_mail = extract_int(&buf[4], 0);
187                 WC->need_regi = extract_int(&buf[4], 1);
188                 WC->need_vali = extract_int(&buf[4], 2);
189                 extract_token(WC->cs_inet_email, &buf[4], 3, '|', sizeof WC->cs_inet_email);
190         }
191
192         get_preference("current_iconbar", buf, sizeof buf);
193         WC->current_iconbar = atoi(buf);
194
195         get_preference("floordiv_expanded", WC->floordiv_expanded, sizeof WC->floordiv_expanded);
196 }
197
198
199 /* 
200  * Perform authentication using a user name and password
201  */
202 void do_login(void)
203 {
204         char buf[SIZ];
205
206         if (havebstr("language")) {
207                 set_selected_language(bstr("language"));
208                 go_selected_language();
209         }
210
211         if (havebstr("exit_action")) {
212                 do_logout();
213                 return;
214         }
215         if (havebstr("login_action")) {
216                 serv_printf("USER %s", bstr("name"));
217                 serv_getln(buf, sizeof buf);
218                 if (buf[0] == '3') {
219                         serv_printf("PASS %s", bstr("pass"));
220                         serv_getln(buf, sizeof buf);
221                         if (buf[0] == '2') {
222                                 become_logged_in(bstr("name"),
223                                                  bstr("pass"), buf);
224                         } else {
225                                 display_login(&buf[4]);
226                                 return;
227                         }
228                 } else {
229                         display_login(&buf[4]);
230                         return;
231                 }
232         }
233         if (havebstr("newuser_action")) {
234                 if (!havebstr("pass")) {
235                         display_login(_("Blank passwords are not allowed."));
236                         return;
237                 }
238                 serv_printf("NEWU %s", bstr("name"));
239                 serv_getln(buf, sizeof buf);
240                 if (buf[0] == '2') {
241                         become_logged_in(bstr("name"), bstr("pass"), buf);
242                         serv_printf("SETP %s", bstr("pass"));
243                         serv_getln(buf, sizeof buf);
244                 } else {
245                         display_login(&buf[4]);
246                         return;
247                 }
248         }
249         if (WC->logged_in) {
250                 if (WC->need_regi) {
251                         display_reg(1);
252                 } else {
253                         do_welcome();
254                 }
255         } else {
256                 display_login(_("Your password was not accepted."));
257         }
258
259 }
260
261
262 /* 
263  * Perform authentication using OpenID
264  * assemble the checkid_setup request and then redirect to the user's identity provider
265  */
266 void do_openid_login(void)
267 {
268         char buf[4096];
269
270         if (havebstr("language")) {
271                 set_selected_language(bstr("language"));
272                 go_selected_language();
273         }
274
275         if (havebstr("exit_action")) {
276                 do_logout();
277                 return;
278         }
279         if (havebstr("login_action")) {
280                 snprintf(buf, sizeof buf,
281                         "OID1 %s|%s://%s/finish_openid_login|%s://%s",
282                         bstr("openid_url"),
283                         (is_https ? "https" : "http"), WC->http_host,
284                         (is_https ? "https" : "http"), WC->http_host
285                 );
286
287                 serv_puts(buf);
288                 serv_getln(buf, sizeof buf);
289                 if (buf[0] == '2') {
290                         lprintf(CTDL_DEBUG, "OpenID server contacted; redirecting to %s\n", &buf[4]);
291                         http_redirect(&buf[4]);
292                         return;
293                 }
294                 else {
295                         display_openid_login(&buf[4]);
296                         return;
297                 }
298         }
299
300         /* If we get to this point then something failed. */
301         display_openid_login(_("Your password was not accepted."));
302 }
303
304 /* 
305  * Complete the authentication using OpenID
306  * This function handles the positive or negative assertion from the user's Identity Provider
307  */
308 void finish_openid_login(void)
309 {
310         if (havebstr("openid.mode")) {
311                 if (!strcasecmp(bstr("openid.mode"), "id_res")) {
312
313                         display_openid_login("FIXME id accepted but the code isn't finished");
314                         //FIXME finish this
315
316                 }
317         }
318
319         if (WC->logged_in) {
320                 if (WC->need_regi) {
321                         display_reg(1);
322                 } else {
323                         do_welcome();
324                 }
325         } else {
326                 display_openid_login(_("Your password was not accepted."));
327         }
328
329 }
330
331
332
333
334
335
336
337
338 /*
339  * display the user a welcome screen.
340  *
341  * if this is the first time login, and the web based setup is enabled, 
342  * lead the user through the setup routines
343  */
344 void do_welcome(void)
345 {
346         char buf[SIZ];
347 #ifdef XXX_NOT_FINISHED_YET_XXX
348         FILE *fp;
349         int i;
350
351         /**
352          * See if we have to run the first-time setup wizard
353          */
354         if (WC->is_aide) {
355                 if (!setup_wizard) {
356                         int len;
357                         sprintf(wizard_filename, "setupwiz.%s.%s",
358                                 ctdlhost, ctdlport);
359                         len = strlen(wizard_filename);
360                         for (i=0; i<len; ++i) {
361                                 if (    (wizard_filename[i]==' ')
362                                         || (wizard_filename[i] == '/')
363                                 ) {
364                                         wizard_filename[i] = '_';
365                                 }
366                         }
367         
368                         fp = fopen(wizard_filename, "r");
369                         if (fp != NULL) {
370                                 fgets(buf, sizeof buf, fp);
371                                 buf[strlen(buf)-1] = 0;
372                                 fclose(fp);
373                                 if (atoi(buf) == serv_info.serv_rev_level) {
374                                         setup_wizard = 1; /**< already run */
375                                 }
376                         }
377                 }
378
379                 if (!setup_wizard) {
380                         http_redirect("setup_wizard");
381                 }
382         }
383 #endif
384
385         /*
386          * Go to the user's preferred start page
387          */
388         get_preference("startpage", buf, sizeof buf);
389         if (IsEmptyStr(buf)) {
390                 safestrncpy(buf, "dotskip&room=_BASEROOM_", sizeof buf);
391                 set_preference("startpage", buf, 1);
392         }
393         if (buf[0] == '/') {
394                 strcpy(buf, &buf[1]);
395         }
396         http_redirect(buf);
397 }
398
399
400 /*
401  * Disconnect from the Citadel server, and end this WebCit session
402  */
403 void end_webcit_session(void) {
404         char buf[256];
405
406         if (WC->logged_in) {
407                 sprintf(buf, "%d", WC->current_iconbar);
408                 set_preference("current_iconbar", buf, 0);
409                 set_preference("floordiv_expanded", WC->floordiv_expanded, 1);
410         }
411
412         serv_puts("QUIT");
413         WC->killthis = 1;
414         /* close() of citadel socket will be done by do_housekeeping() */
415 }
416
417 /* 
418  * execute the logout
419  */
420 void do_logout(void)
421 {
422         char buf[SIZ];
423
424         safestrncpy(WC->wc_username, "", sizeof WC->wc_username);
425         safestrncpy(WC->wc_password, "", sizeof WC->wc_password);
426         safestrncpy(WC->wc_roomname, "", sizeof WC->wc_roomname);
427         safestrncpy(WC->wc_fullname, "", sizeof WC->wc_fullname);
428
429         /** Calling output_headers() this way causes the cookies to be un-set */
430         output_headers(1, 1, 0, 1, 0, 0);
431
432         wprintf("<div id=\"logout_screen\">");
433         wprintf("<div class=\"box\">");
434         wprintf("<div class=\"boxlabel\">");
435         wprintf(_("Log off"));
436         wprintf("</div><div class=\"boxcontent\">");    
437         serv_puts("MESG goodbye");
438         serv_getln(buf, sizeof buf);
439
440         if (WC->serv_sock >= 0) {
441                 if (buf[0] == '1') {
442                         fmout("CENTER");
443                 } else {
444                         wprintf("Goodbye\n");
445                 }
446         }
447         else {
448                 wprintf(_("This program was unable to connect or stay "
449                         "connected to the Citadel server.  Please report "
450                         "this problem to your system administrator.")
451                 );
452                 wprintf("<a href=\"http://www.citadel.org/doku.php/"
453                         "faq:mastering_your_os:net#netstat\">%s</a>", 
454                         _("Read More..."));
455         }
456
457         wprintf("<hr /><div class=\"buttons\"> "
458                 "<span class=\"button_link\"><a href=\".\">");
459         wprintf(_("Log in again"));
460         wprintf("</a></span>&nbsp;&nbsp;&nbsp;<span class=\"button_link\">"
461                 "<a href=\"javascript:window.close();\">");
462         wprintf(_("Close window"));
463         wprintf("</a></span></div></div></div></div>\n");
464         wDumpContent(2);
465         end_webcit_session();
466 }
467
468
469 /*
470  * validate new users
471  */
472 void validate(void)
473 {
474         char cmd[SIZ];
475         char user[SIZ];
476         char buf[SIZ];
477         int a;
478
479         output_headers(1, 1, 2, 0, 0, 0);
480         wprintf("<div id=\"banner\">\n");
481         wprintf("<h1>");
482         wprintf(_("Validate new users"));
483         wprintf("</h1>");
484         wprintf("</div>\n");
485
486         wprintf("<div id=\"content\" class=\"service\">\n");
487
488         /* If the user just submitted a validation, process it... */
489         safestrncpy(buf, bstr("user"), sizeof buf);
490         if (!IsEmptyStr(buf)) {
491                 if (havebstr("axlevel")) {
492                         serv_printf("VALI %s|%s", buf, bstr("axlevel"));
493                         serv_getln(buf, sizeof buf);
494                         if (buf[0] != '2') {
495                                 wprintf("<b>%s</b><br />\n", &buf[4]);
496                         }
497                 }
498         }
499
500         /* Now see if any more users require validation. */
501         serv_puts("GNUR");
502         serv_getln(buf, sizeof buf);
503         if (buf[0] == '2') {
504                 wprintf("<b>");
505                 wprintf(_("No users require validation at this time."));
506                 wprintf("</b><br />\n");
507                 wDumpContent(1);
508                 return;
509         }
510         if (buf[0] != '3') {
511                 wprintf("<b>%s</b><br />\n", &buf[4]);
512                 wDumpContent(1);
513                 return;
514         }
515
516         wprintf("<div class=\"fix_scrollbar_bug\">"
517                 "<table class=\"auth_validate\"><tr><td>\n");
518         wprintf("<center>");
519
520         safestrncpy(user, &buf[4], sizeof user);
521         serv_printf("GREG %s", user);
522         serv_getln(cmd, sizeof cmd);
523         if (cmd[0] == '1') {
524                 a = 0;
525                 do {
526                         serv_getln(buf, sizeof buf);
527                         ++a;
528                         if (a == 1)
529                                 wprintf("#%s<br /><H1>%s</H1>",
530                                         buf, &cmd[4]);
531                         if (a == 2)
532                                 wprintf("PW: %s<br />\n", buf);
533                         if (a == 3)
534                                 wprintf("%s<br />\n", buf);
535                         if (a == 4)
536                                 wprintf("%s<br />\n", buf);
537                         if (a == 5)
538                                 wprintf("%s, ", buf);
539                         if (a == 6)
540                                 wprintf("%s ", buf);
541                         if (a == 7)
542                                 wprintf("%s<br />\n", buf);
543                         if (a == 8)
544                                 wprintf("%s<br />\n", buf);
545                         if (a == 9)
546                                 wprintf(_("Current access level: %d (%s)\n"),
547                                         atoi(buf), axdefs[atoi(buf)]);
548                 } while (strcmp(buf, "000"));
549         } else {
550                 wprintf("<H1>%s</H1>%s<br />\n", user, &cmd[4]);
551         }
552
553         wprintf("<hr />");
554         wprintf(_("Select access level for this user:"));
555         wprintf("<br />\n");
556         for (a = 0; a <= 6; ++a) {
557                 wprintf("<a href=\"validate?nonce=%ld?user=", WC->nonce);
558                 urlescputs(user);
559                 wprintf("&axlevel=%d\">%s</A>&nbsp;&nbsp;&nbsp;\n",
560                         a, axdefs[a]);
561         }
562         wprintf("<br />\n");
563
564         wprintf("</CENTER>\n");
565         wprintf("</td></tr></table></div>\n");
566         wDumpContent(1);
567 }
568
569
570
571 /*
572  * Display form for registration.
573  *
574  * (Set during_login to 1 if this registration is being performed during
575  * new user login and will require chaining to the proper screen.)
576  */
577 void display_reg(int during_login)
578 {
579         long vcard_msgnum;
580
581         if (goto_config_room() != 0) {
582                 if (during_login) do_welcome();
583                 else display_main_menu();
584                 return;
585         }
586
587         vcard_msgnum = locate_user_vcard(WC->wc_fullname, -1);
588         if (vcard_msgnum < 0L) {
589                 if (during_login) do_welcome();
590                 else display_main_menu();
591                 return;
592         }
593
594         if (during_login) {
595                 do_edit_vcard(vcard_msgnum, "1", "do_welcome", USERCONFIGROOM);
596         }
597         else {
598                 do_edit_vcard(vcard_msgnum, "1", "display_main_menu", USERCONFIGROOM);
599         }
600
601 }
602
603
604
605
606 /*
607  * display form for changing your password
608  */
609 void display_changepw(void)
610 {
611         char buf[SIZ];
612
613         output_headers(1, 1, 1, 0, 0, 0);
614
615         svput("BOXTITLE", WCS_STRING, _("Change your password"));
616         do_template("beginbox");
617
618         if (!IsEmptyStr(WC->ImportantMessage)) {
619                 wprintf("<span class=\"errormsg\">"
620                         "%s</span><br />\n", WC->ImportantMessage);
621                 safestrncpy(WC->ImportantMessage, "", sizeof WC->ImportantMessage);
622         }
623
624         serv_puts("MESG changepw");
625         serv_getln(buf, sizeof buf);
626         if (buf[0] == '1') {
627                 fmout("CENTER");
628         }
629
630         wprintf("<form name=\"changepwform\" action=\"changepw\" method=\"post\">\n");
631         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
632         wprintf("<table class=\"altern\" ");
633         wprintf("<tr class=\"even\"><td>");
634         wprintf(_("Enter new password:"));
635         wprintf("</td><td>");
636         wprintf("<input type=\"password\" name=\"newpass1\" value=\"\" maxlength=\"20\"></td></tr>\n");
637         wprintf("<tr class=\"odd\"><td>");
638         wprintf(_("Enter it again to confirm:"));
639         wprintf("</td><td>");
640         wprintf("<input type=\"password\" name=\"newpass2\" value=\"\" maxlength=\"20\"></td></tr>\n");
641         wprintf("</table>\n");
642
643         wprintf("<div class=\"buttons\">\n");
644         wprintf("<input type=\"submit\" name=\"change_action\" value=\"%s\">", _("Change password"));
645         wprintf("&nbsp;");
646         wprintf("<input type=\"submit\" name=\"cancel_action\" value=\"%s\">\n", _("Cancel"));
647         wprintf("</div>\n");
648         wprintf("</form>\n");
649
650         do_template("endbox");
651         wDumpContent(1);
652 }
653
654 /*
655  * change password
656  * if passwords match, propagate it to citserver.
657  */
658 void changepw(void)
659 {
660         char buf[SIZ];
661         char newpass1[32], newpass2[32];
662
663         if (!havebstr("change_action")) {
664                 safestrncpy(WC->ImportantMessage, 
665                         _("Cancelled.  Password was not changed."),
666                         sizeof WC->ImportantMessage);
667                 display_main_menu();
668                 return;
669         }
670
671         safestrncpy(newpass1, bstr("newpass1"), sizeof newpass1);
672         safestrncpy(newpass2, bstr("newpass2"), sizeof newpass2);
673
674         if (strcasecmp(newpass1, newpass2)) {
675                 safestrncpy(WC->ImportantMessage, 
676                         _("They don't match.  Password was not changed."),
677                         sizeof WC->ImportantMessage);
678                 display_changepw();
679                 return;
680         }
681
682         if (IsEmptyStr(newpass1)) {
683                 safestrncpy(WC->ImportantMessage, 
684                         _("Blank passwords are not allowed."),
685                         sizeof WC->ImportantMessage);
686                 display_changepw();
687                 return;
688         }
689
690         serv_printf("SETP %s", newpass1);
691         serv_getln(buf, sizeof buf);
692         sprintf(WC->ImportantMessage, "%s", &buf[4]);
693         if (buf[0] == '2') {
694                 safestrncpy(WC->wc_password, buf, sizeof WC->wc_password);
695                 display_main_menu();
696         }
697         else {
698                 display_changepw();
699         }
700 }
701
702
703
704 void InitModule_AUTH(void)
705 {
706         WebcitAddUrlHandler(HKEY("do_welcome"), do_welcome, 0);
707         return ;
708 }