4 * WebcitAuth; Handles authentication of users to a Citadel server.
12 void display_reg(int during_login);
15 * Access level definitions. This is initialized from a function rather than a
16 * static array so that the strings may be localized.
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 */
34 * Display the login screen
35 * mesg = the error message if last attempt failed.
37 void display_login(void)
40 output_headers(1, 0, 0, 0, 1, 0);
41 do_template("login", NULL);
49 * Display the openid-enabled login screen
50 * mesg = the error message if last attempt failed.
52 void display_openid_login(char *mesg)
55 output_headers(1, 0, 0, 0, 1, 0);
56 do_template("openid_login", NULL);
61 void display_openid_name_request(const StrBuf *claimed_id, const StrBuf *username)
65 output_headers(1, 1, 2, 0, 0, 0);
66 wprintf("<div id=\"login_screen\">\n");
68 Buf = NewStrBufPlain(NULL, StrLength(claimed_id));
69 StrEscAppend(Buf, claimed_id, NULL, 0, 0);
70 svprintf(HKEY("VERIFIED"), WCS_STRING, _("Your OpenID <tt>%s</tt> was successfully verified."),
72 SVPutBuf("CLAIMED_ID", Buf, 0);
75 if (StrLength(username) > 0) {
76 Buf = NewStrBufPlain(NULL, StrLength(username));
77 StrEscAppend(Buf, claimed_id, NULL, 0, 0);
78 svprintf(HKEY("REASON"), WCS_STRING,
79 _("However, the user name '%s' conflicts with an existing user."),
84 svput("REASON", WCS_STRING, "");
87 svput("ACTION_REQUESTED", WCS_STRING, _("Please specify the user name you would like to use."));
89 svput("USERNAME_BOX", WCS_STRING, _("User name:"));
90 svput("NEWUSER_BUTTON", WCS_STRING, _("New User"));
91 svput("EXIT_BUTTON", WCS_STRING, _("Exit"));
93 svprintf(HKEY("BOXTITLE"), WCS_STRING, _("%s - powered by <a href=\"http://www.citadel.org\">Citadel</a>"),
94 ChrPtr(WC->serv_info->serv_humannode));
96 do_template("openid_manual_create", NULL);
102 /* Initialize the session
104 * This function needs to get called whenever the session changes from
105 * not-logged-in to logged-in, either by an explicit login by the user or
106 * by a timed-out session automatically re-establishing with a little help
107 * from the browser cookie. Either way, we need to load access controls and
108 * preferences from the server.
112 * serv_response The parameters returned from a Citadel USER or NEWU command
114 void become_logged_in(const StrBuf *user, const StrBuf *pass, StrBuf *serv_response)
119 StrBuf *Language = NULL;
123 if (WCC->wc_fullname == NULL)
124 WCC->wc_fullname = NewStrBufPlain(NULL, StrLength(serv_response));
125 StrBufExtract_token(WCC->wc_fullname, serv_response, 0, '|');
126 StrBufCutLeft(WCC->wc_fullname, 4 );
128 if (WCC->wc_username == NULL)
129 WCC->wc_username = NewStrBufDup(user);
131 FlushStrBuf(WCC->wc_username);
132 StrBufAppendBuf(WCC->wc_username, user, 0);
135 if (WCC->wc_password == NULL)
136 WCC->wc_password = NewStrBufDup(pass);
138 FlushStrBuf(WCC->wc_password);
139 StrBufAppendBuf(WCC->wc_password, pass, 0);
142 WCC->axlevel = StrBufExtract_int(serv_response, 1, '|');
143 if (WCC->axlevel >= 6) { /* TODO: make this a define, else it might trick us later */
150 serv_getln(buf, sizeof buf);
152 WC->new_mail = extract_int(&buf[4], 0);
153 WC->need_regi = extract_int(&buf[4], 1);
154 WC->need_vali = extract_int(&buf[4], 2);
155 extract_token(WC->cs_inet_email, &buf[4], 3, '|', sizeof WC->cs_inet_email);
157 if (havebstr("language"))
158 set_preference("language", NewStrBufDup(SBSTR("language")), 1);
160 get_preference("language", &Language);
161 if (Language != NULL) {
162 set_selected_language(ChrPtr(Language));
163 go_selected_language(); /* set locale */
166 get_preference("floordiv_expanded", &FloorDiv);
167 WC->floordiv_expanded = FloorDiv;
172 * Perform authentication using a user name and password
179 if (havebstr("language")) {
180 set_selected_language(bstr("language"));
181 go_selected_language();
184 if (havebstr("exit_action")) {
189 if (havebstr("login_action")) {
190 serv_printf("USER %s", bstr("name"));
191 StrBuf_ServGetln(Buf);
192 if (GetServerStatus(Buf, NULL) == 3) {
193 serv_printf("PASS %s", bstr("pass"));
194 StrBuf_ServGetln(Buf);
195 if (GetServerStatus(Buf, NULL) == 2) {
196 become_logged_in(sbstr("name"), sbstr("pass"), Buf);
198 snprintf(WCC->ImportantMessage,
199 sizeof (WCC->ImportantMessage),
207 snprintf(WCC->ImportantMessage,
208 sizeof (WCC->ImportantMessage),
216 if (havebstr("newuser_action")) {
217 if (!havebstr("pass")) {
218 snprintf(WCC->ImportantMessage,
219 sizeof (WCC->ImportantMessage),
221 _("Blank passwords are not allowed."));
226 serv_printf("NEWU %s", bstr("name"));
227 StrBuf_ServGetln(Buf);
228 if (GetServerStatus(Buf, NULL) == 2) {
229 become_logged_in(sbstr("name"), sbstr("pass"), Buf);
230 serv_printf("SETP %s", bstr("pass"));
231 StrBuf_ServGetln(Buf); /* Don't care? */
233 snprintf(WCC->ImportantMessage,
234 sizeof (WCC->ImportantMessage),
242 if (WCC->logged_in) {
243 if (WCC->need_regi) {
245 } else if (WCC->need_vali) {
251 snprintf(WCC->ImportantMessage,
252 sizeof (WCC->ImportantMessage),
254 _("Your password was not accepted."));
261 * Try to create an account manually after an OpenID was verified
263 void openid_manual_create(void)
267 if (havebstr("exit_action")) {
272 if (havebstr("newuser_action")) {
274 serv_printf("OIDC %s", bstr("name"));
275 StrBuf_ServGetln(Buf);
276 if (GetServerStatus(Buf, NULL) == 2) {
280 serv_puts("SETP GENERATE_RANDOM_PASSWORD");
281 StrBuf_ServGetln(gpass);
282 StrBufCutLeft(gpass, 4);
283 become_logged_in(sbstr("name"), gpass, Buf);
292 } else if (WC->need_vali) {
298 display_openid_name_request(sbstr("openid_url"), sbstr("name"));
305 * Perform authentication using OpenID
306 * assemble the checkid_setup request and then redirect to the user's identity provider
308 void do_openid_login(void)
313 if (havebstr("language")) {
314 set_selected_language(bstr("language"));
315 go_selected_language();
318 if (havebstr("exit_action")) {
322 if (havebstr("login_action")) {
323 snprintf(buf, sizeof buf,
324 "OIDS %s|%s://%s/finalize_openid_login|%s://%s",
326 (is_https ? "https" : "http"), ChrPtr(WCC->http_host),
327 (is_https ? "https" : "http"), ChrPtr(WCC->http_host)
331 serv_getln(buf, sizeof buf);
333 lprintf(CTDL_DEBUG, "OpenID server contacted; redirecting to %s\n", &buf[4]);
334 http_redirect(&buf[4]);
338 display_openid_login(&buf[4]);
343 /* If we get to this point then something failed. */
344 display_openid_login(_("Your password was not accepted."));
348 * Complete the authentication using OpenID
349 * This function handles the positive or negative assertion from the user's Identity Provider
351 void finalize_openid_login(void)
355 int already_logged_in = (WCC->logged_in) ;
357 StrBuf *result = NULL;
358 StrBuf *username = NULL;
359 StrBuf *password = NULL;
360 StrBuf *logged_in_response = NULL;
361 StrBuf *claimed_id = NULL;
363 if (havebstr("openid.mode")) {
364 if (!strcasecmp(bstr("openid.mode"), "id_res")) {
367 StrBuf_ServGetln(Buf);
368 if (GetServerStatus(Buf, NULL) == 8) {
375 Cursor = GetNewHashPos (WCC->urlstrings, 0);
376 while (GetNextHashPos(WCC->urlstrings, Cursor, &HKLen, &HKey, &U)) {
378 if (!strncasecmp(u->url_key, "openid.", 7)) {
379 serv_printf("%s|%s", &u->url_key[7], ChrPtr(u->url_data));
386 while (StrBuf_ServGetln(Buf), strcmp(ChrPtr(Buf), "000"))
388 if (linecount == 0) result = NewStrBufDup(Buf);
389 if (!strcasecmp(ChrPtr(result), "authenticate")) {
390 if (linecount == 1) {
391 username = NewStrBufDup(Buf);
393 else if (linecount == 2) {
394 password = NewStrBufDup(Buf);
396 else if (linecount == 3) {
397 logged_in_response = NewStrBufDup(Buf);
400 else if (!strcasecmp(ChrPtr(result), "verify_only")) {
401 if (linecount == 1) {
402 claimed_id = NewStrBufDup(Buf);
404 if (linecount == 2) {
405 username = NewStrBufDup(Buf);
415 /* If we were already logged in, this was an attempt to associate an OpenID account */
416 if (already_logged_in) {
419 FreeStrBuf(&username);
420 FreeStrBuf(&password);
421 FreeStrBuf(&claimed_id);
422 FreeStrBuf(&logged_in_response);
426 /* If this operation logged us in, either by connecting with an existing account or by
427 * auto-creating one using Simple Registration Extension, we're already on our way.
429 if (!strcasecmp(ChrPtr(result), "authenticate")) {
430 become_logged_in(username, password, logged_in_response);
433 /* The specified OpenID was verified but the desired user name was either not specified via SRI
434 * or conflicts with an existing user. Either way the user will need to specify a new name.
437 else if (!strcasecmp(ChrPtr(result), "verify_only")) {
438 display_openid_name_request(claimed_id, username);
441 /* Did we manage to log in? If so, continue with the normal flow... */
449 display_openid_login(_("Your password was not accepted."));
453 FreeStrBuf(&username);
454 FreeStrBuf(&password);
455 FreeStrBuf(&claimed_id);
456 FreeStrBuf(&logged_in_response);
461 * Display a welcome screen to the user.
463 * If this is the first time login, and the web based setup is enabled,
464 * lead the user through the setup routines
466 void do_welcome(void)
469 #ifdef XXX_NOT_FINISHED_YET_XXX
474 * See if we have to run the first-time setup wizard
479 sprintf(wizard_filename, "setupwiz.%s.%s",
481 len = strlen(wizard_filename);
482 for (i=0; i<len; ++i) {
483 if ( (wizard_filename[i]==' ')
484 || (wizard_filename[i] == '/')
486 wizard_filename[i] = '_';
490 fp = fopen(wizard_filename, "r");
492 fgets(buf, sizeof buf, fp);
493 buf[strlen(buf)-1] = 0;
495 if (atoi(buf) == serv_info.serv_rev_level) {
496 setup_wizard = 1; /**< already run */
502 http_redirect("setup_wizard");
508 * Go to the user's preferred start page
510 if (!get_preference("startpage", &Buf)) {
512 StrBufPrintf(Buf, "dotskip&room=_BASEROOM_");
513 set_preference("startpage", Buf, 1);
515 if (ChrPtr(Buf)[0] == '/') {
516 StrBufCutLeft(Buf, 1);
518 if (StrLength(Buf) == 0)
519 StrBufAppendBufPlain(Buf, "dotgoto?room=_BASEROOM_", -1, 0);
520 http_redirect(ChrPtr(Buf));
525 * Disconnect from the Citadel server, and end this WebCit session
527 void end_webcit_session(void) {
531 /* close() of citadel socket will be done by do_housekeeping() */
541 FlushStrBuf(WC->wc_username);
542 FlushStrBuf(WC->wc_password);
543 FlushStrBuf(WC->wc_roomname);
544 FlushStrBuf(WC->wc_fullname);
546 /* FIXME: this is to suppress the iconbar displaying, because we aren't
547 actually logged out yet */
550 /** Calling output_headers() this way causes the cookies to be un-set */
551 output_headers(1, 1, 0, 1, 0, 0);
553 wprintf("<div id=\"logout_screen\">");
554 wprintf("<div class=\"box\">");
555 wprintf("<div class=\"boxlabel\">");
556 wprintf(_("Log off"));
557 wprintf("</div><div class=\"boxcontent\">");
558 serv_puts("MESG goodbye");
559 serv_getln(buf, sizeof buf);
561 if (WC->serv_sock >= 0) {
565 wprintf("Goodbye\n");
569 wprintf(_("This program was unable to connect or stay "
570 "connected to the Citadel server. Please report "
571 "this problem to your system administrator.")
573 wprintf("<a href=\"http://www.citadel.org/doku.php/"
574 "faq:mastering_your_os:net#netstat\">%s</a>",
578 wprintf("<hr /><div class=\"buttons\"> "
579 "<span class=\"button_link\"><a href=\".\">");
580 wprintf(_("Log in again"));
581 wprintf("</a></span> <span class=\"button_link\">"
582 "<a href=\"javascript:window.close();\">");
583 wprintf(_("Close window"));
584 wprintf("</a></span></div></div></div></div>\n");
586 end_webcit_session();
600 output_headers(1, 1, 2, 0, 0, 0);
601 wprintf("<div id=\"banner\">\n");
603 wprintf(_("Validate new users"));
607 wprintf("<div id=\"content\" class=\"service\">\n");
609 /* If the user just submitted a validation, process it... */
610 safestrncpy(buf, bstr("user"), sizeof buf);
611 if (!IsEmptyStr(buf)) {
612 if (havebstr("axlevel")) {
613 serv_printf("VALI %s|%s", buf, bstr("axlevel"));
614 serv_getln(buf, sizeof buf);
616 wprintf("<b>%s</b><br>\n", &buf[4]);
621 /* Now see if any more users require validation. */
623 serv_getln(buf, sizeof buf);
626 wprintf(_("No users require validation at this time."));
627 wprintf("</b><br>\n");
632 wprintf("<b>%s</b><br>\n", &buf[4]);
637 wprintf("<div class=\"fix_scrollbar_bug\">"
638 "<table class=\"auth_validate\"><tr><td>\n");
639 wprintf("<div id=\"validate\">");
641 safestrncpy(user, &buf[4], sizeof user);
642 serv_printf("GREG %s", user);
643 serv_getln(cmd, sizeof cmd);
647 serv_getln(buf, sizeof buf);
650 wprintf("#%s<br><H1>%s</H1>",
659 while (!IsEmptyStr(pch))
663 else if (isalpha(*pch))
674 pch = _("very weak");
687 wprintf("PW: %s<br>\n", pch);
690 wprintf("%s<br>\n", buf);
692 wprintf("%s<br>\n", buf);
694 wprintf("%s, ", buf);
698 wprintf("%s<br>\n", buf);
700 wprintf("%s<br>\n", buf);
702 wprintf(_("Current access level: %d (%s)\n"),
703 atoi(buf), axdefs[atoi(buf)]);
704 } while (strcmp(buf, "000"));
706 wprintf("<H1>%s</H1>%s<br />\n", user, &cmd[4]);
710 wprintf(_("Select access level for this user:"));
712 for (a = 0; a <= 6; ++a) {
713 wprintf("<a href=\"validate?nonce=%d?user=", WC->nonce);
715 wprintf("&axlevel=%d\">%s</A> \n",
721 wprintf("</td></tr></table></div>\n");
728 * Display form for registration.
730 * (Set during_login to 1 if this registration is being performed during
731 * new user login and will require chaining to the proper screen.)
733 void display_reg(int during_login)
737 if (goto_config_room() != 0) {
738 if (during_login) do_welcome();
739 else display_main_menu();
743 vcard_msgnum = locate_user_vcard_in_this_room();
744 if (vcard_msgnum < 0L) {
745 if (during_login) do_welcome();
746 else display_main_menu();
751 do_edit_vcard(vcard_msgnum, "1", "do_welcome", USERCONFIGROOM);
754 do_edit_vcard(vcard_msgnum, "1", "display_main_menu", USERCONFIGROOM);
763 * display form for changing your password
765 void display_changepw(void)
767 WCTemplputParams SubTP;
770 output_headers(1, 1, 1, 0, 0, 0);
772 Buf = NewStrBufPlain(_("Change your password"), -1);
773 memset(&SubTP, 0, sizeof(WCTemplputParams));
774 SubTP.Filter.ContextType = CTX_STRBUF;
776 DoTemplate(HKEY("beginbox"), NULL, &SubTP);
780 if (!IsEmptyStr(WC->ImportantMessage)) {
781 wprintf("<span class=\"errormsg\">"
782 "%s</span><br />\n", WC->ImportantMessage);
783 safestrncpy(WC->ImportantMessage, "", sizeof WC->ImportantMessage);
786 serv_puts("MESG changepw");
787 serv_getln(buf, sizeof buf);
792 wprintf("<form name=\"changepwform\" action=\"changepw\" method=\"post\">\n");
793 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
794 wprintf("<table class=\"altern\" ");
795 wprintf("<tr class=\"even\"><td>");
796 wprintf(_("Enter new password:"));
797 wprintf("</td><td>");
798 wprintf("<input type=\"password\" name=\"newpass1\" value=\"\" maxlength=\"20\"></td></tr>\n");
799 wprintf("<tr class=\"odd\"><td>");
800 wprintf(_("Enter it again to confirm:"));
801 wprintf("</td><td>");
802 wprintf("<input type=\"password\" name=\"newpass2\" value=\"\" maxlength=\"20\"></td></tr>\n");
803 wprintf("</table>\n");
805 wprintf("<div class=\"buttons\">\n");
806 wprintf("<input type=\"submit\" name=\"change_action\" value=\"%s\">", _("Change password"));
808 wprintf("<input type=\"submit\" name=\"cancel_action\" value=\"%s\">\n", _("Cancel"));
810 wprintf("</form>\n");
812 do_template("endbox", NULL);
818 * if passwords match, propagate it to citserver.
823 char newpass1[32], newpass2[32];
825 if (!havebstr("change_action")) {
826 safestrncpy(WC->ImportantMessage,
827 _("Cancelled. Password was not changed."),
828 sizeof WC->ImportantMessage);
833 safestrncpy(newpass1, bstr("newpass1"), sizeof newpass1);
834 safestrncpy(newpass2, bstr("newpass2"), sizeof newpass2);
836 if (strcasecmp(newpass1, newpass2)) {
837 safestrncpy(WC->ImportantMessage,
838 _("They don't match. Password was not changed."),
839 sizeof WC->ImportantMessage);
844 if (IsEmptyStr(newpass1)) {
845 safestrncpy(WC->ImportantMessage,
846 _("Blank passwords are not allowed."),
847 sizeof WC->ImportantMessage);
852 serv_printf("SETP %s", newpass1);
853 serv_getln(buf, sizeof buf);
854 sprintf(WC->ImportantMessage, "%s", &buf[4]);
856 if (WC->wc_password == NULL)
857 WC->wc_password = NewStrBufPlain(buf, -1);
859 FlushStrBuf(WC->wc_password);
860 StrBufAppendBufPlain(WC->wc_password, buf, -1, 0);
869 int ConditionalAide(StrBuf *Target, WCTemplputParams *TP)
871 return (WC->is_aide == 0);
874 int ConditionalRoomAide(StrBuf *Target, WCTemplputParams *TP)
876 return (WC->is_room_aide == 0);
879 int ConditionalIsLoggedIn(StrBuf *Target, WCTemplputParams *TP) {
880 return (WC->logged_in == 0);
882 int ConditionalRoomAcessDelete(StrBuf *Target, WCTemplputParams *TP)
885 return ( (WCC->is_room_aide) || (WCC->is_mailbox) || (WCC->room_flags2 & QR2_COLLABDEL) );
890 void _display_openid_login(void) {display_openid_login(NULL);}
891 void _display_reg(void) {display_reg(0);}
898 WebcitAddUrlHandler(HKEY("do_welcome"), do_welcome, ANONYMOUS);
899 WebcitAddUrlHandler(HKEY("login"), do_login, ANONYMOUS);
900 WebcitAddUrlHandler(HKEY("display_openid_login"), _display_openid_login, ANONYMOUS);
901 WebcitAddUrlHandler(HKEY("openid_login"), do_openid_login, ANONYMOUS);
902 WebcitAddUrlHandler(HKEY("finalize_openid_login"), finalize_openid_login, ANONYMOUS);
903 WebcitAddUrlHandler(HKEY("openid_manual_create"), openid_manual_create, ANONYMOUS);
904 WebcitAddUrlHandler(HKEY("do_logout"), do_logout, 0);
905 WebcitAddUrlHandler(HKEY("validate"), validate, 0);
906 WebcitAddUrlHandler(HKEY("display_reg"), _display_reg, 0);
907 WebcitAddUrlHandler(HKEY("display_changepw"), display_changepw, 0);
908 WebcitAddUrlHandler(HKEY("changepw"), changepw, 0);
909 WebcitAddUrlHandler(HKEY("termquit"), do_logout, 0);
911 RegisterConditional(HKEY("COND:AIDE"), 2, ConditionalAide, CTX_NONE);
912 RegisterConditional(HKEY("COND:ROOMAIDE"), 2, ConditionalRoomAide, CTX_NONE);
913 RegisterConditional(HKEY("COND:ACCESS:DELETE"), 2, ConditionalRoomAcessDelete, CTX_NONE);
914 RegisterConditional(HKEY("COND:LOGGEDIN"), 2, ConditionalIsLoggedIn, CTX_NONE);