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(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)
122 if (WCC->wc_fullname == NULL)
123 WCC->wc_fullname = NewStrBufPlain(NULL, StrLength(serv_response));
124 StrBufExtract_token(WCC->wc_fullname, serv_response, 0, '|');
125 StrBufCutLeft(WCC->wc_fullname, 4 );
127 if (WCC->wc_username == NULL)
128 WCC->wc_username = NewStrBufDup(user);
130 FlushStrBuf(WCC->wc_username);
131 StrBufAppendBuf(WCC->wc_username, user, 0);
134 if (WCC->wc_password == NULL)
135 WCC->wc_password = NewStrBufDup(pass);
137 FlushStrBuf(WCC->wc_password);
138 StrBufAppendBuf(WCC->wc_password, pass, 0);
141 WCC->axlevel = StrBufExtract_int(serv_response, 1, '|');
142 if (WCC->axlevel >= 6) { /* TODO: make this a define, else it might trick us later */
149 serv_getln(buf, sizeof buf);
151 WC->new_mail = extract_int(&buf[4], 0);
152 WC->need_regi = extract_int(&buf[4], 1);
153 WC->need_vali = extract_int(&buf[4], 2);
154 extract_token(WC->cs_inet_email, &buf[4], 3, '|', sizeof WC->cs_inet_email);
157 get_preference("floordiv_expanded", &FloorDiv);
158 WC->floordiv_expanded = FloorDiv;
163 * Perform authentication using a user name and password
170 if (havebstr("language")) {
171 set_selected_language(bstr("language"));
172 go_selected_language();
175 if (havebstr("exit_action")) {
180 if (havebstr("login_action")) {
181 serv_printf("USER %s", bstr("name"));
182 StrBuf_ServGetln(Buf);
183 if (GetServerStatus(Buf, NULL) == 3) {
184 serv_printf("PASS %s", bstr("pass"));
185 StrBuf_ServGetln(Buf);
186 if (GetServerStatus(Buf, NULL) == 2) {
187 become_logged_in(sbstr("name"), sbstr("pass"), Buf);
189 snprintf(WCC->ImportantMessage,
190 sizeof (WCC->ImportantMessage),
198 snprintf(WCC->ImportantMessage,
199 sizeof (WCC->ImportantMessage),
207 if (havebstr("newuser_action")) {
208 if (!havebstr("pass")) {
209 snprintf(WCC->ImportantMessage,
210 sizeof (WCC->ImportantMessage),
212 _("Blank passwords are not allowed."));
217 serv_printf("NEWU %s", bstr("name"));
218 StrBuf_ServGetln(Buf);
219 if (GetServerStatus(Buf, NULL) == 2) {
220 become_logged_in(sbstr("name"), sbstr("pass"), Buf);
221 serv_printf("SETP %s", bstr("pass"));
222 StrBuf_ServGetln(Buf); /* Don't care? */
224 snprintf(WCC->ImportantMessage,
225 sizeof (WCC->ImportantMessage),
233 if (WCC->logged_in) {
234 set_preference("language", NewStrBufPlain(bstr("language"), -1), 1);
235 if (WCC->need_regi) {
237 } else if (WCC->need_vali) {
243 snprintf(WCC->ImportantMessage,
244 sizeof (WCC->ImportantMessage),
246 _("Your password was not accepted."));
253 * Try to create an account manually after an OpenID was verified
255 void openid_manual_create(void)
259 if (havebstr("exit_action")) {
264 if (havebstr("newuser_action")) {
266 serv_printf("OIDC %s", bstr("name"));
267 StrBuf_ServGetln(Buf);
268 if (GetServerStatus(Buf, NULL) == 2) {
272 serv_puts("SETP GENERATE_RANDOM_PASSWORD");
273 StrBuf_ServGetln(gpass);
274 StrBufCutLeft(gpass, 4);
275 become_logged_in(sbstr("name"), gpass, Buf);
284 } else if (WC->need_vali) {
290 display_openid_name_request(sbstr("openid_url"), sbstr("name"));
297 * Perform authentication using OpenID
298 * assemble the checkid_setup request and then redirect to the user's identity provider
300 void do_openid_login(void)
305 if (havebstr("language")) {
306 set_selected_language(bstr("language"));
307 go_selected_language();
310 if (havebstr("exit_action")) {
314 if (havebstr("login_action")) {
315 snprintf(buf, sizeof buf,
316 "OIDS %s|%s://%s/finalize_openid_login|%s://%s",
318 (is_https ? "https" : "http"), ChrPtr(WCC->http_host),
319 (is_https ? "https" : "http"), ChrPtr(WCC->http_host)
323 serv_getln(buf, sizeof buf);
325 lprintf(CTDL_DEBUG, "OpenID server contacted; redirecting to %s\n", &buf[4]);
326 http_redirect(&buf[4]);
330 display_openid_login(&buf[4]);
335 /* If we get to this point then something failed. */
336 display_openid_login(_("Your password was not accepted."));
340 * Complete the authentication using OpenID
341 * This function handles the positive or negative assertion from the user's Identity Provider
343 void finalize_openid_login(void)
347 int already_logged_in = (WCC->logged_in) ;
349 StrBuf *result = NULL;
350 StrBuf *username = NULL;
351 StrBuf *password = NULL;
352 StrBuf *logged_in_response = NULL;
353 StrBuf *claimed_id = NULL;
355 if (havebstr("openid.mode")) {
356 if (!strcasecmp(bstr("openid.mode"), "id_res")) {
359 StrBuf_ServGetln(Buf);
360 if (GetServerStatus(Buf, NULL) == 8) {
367 Cursor = GetNewHashPos (WCC->urlstrings, 0);
368 while (GetNextHashPos(WCC->urlstrings, Cursor, &HKLen, &HKey, &U)) {
370 if (!strncasecmp(u->url_key, "openid.", 7)) {
371 serv_printf("%s|%s", &u->url_key[7], ChrPtr(u->url_data));
378 while (StrBuf_ServGetln(Buf), strcmp(ChrPtr(Buf), "000"))
380 if (linecount == 0) result = NewStrBufDup(Buf);
381 if (!strcasecmp(ChrPtr(result), "authenticate")) {
382 if (linecount == 1) {
383 username = NewStrBufDup(Buf);
385 else if (linecount == 2) {
386 password = NewStrBufDup(Buf);
388 else if (linecount == 3) {
389 logged_in_response = NewStrBufDup(Buf);
392 else if (!strcasecmp(ChrPtr(result), "verify_only")) {
393 if (linecount == 1) {
394 claimed_id = NewStrBufDup(Buf);
396 if (linecount == 2) {
397 username = NewStrBufDup(Buf);
407 /* If we were already logged in, this was an attempt to associate an OpenID account */
408 if (already_logged_in) {
411 FreeStrBuf(&username);
412 FreeStrBuf(&password);
413 FreeStrBuf(&claimed_id);
414 FreeStrBuf(&logged_in_response);
418 /* If this operation logged us in, either by connecting with an existing account or by
419 * auto-creating one using Simple Registration Extension, we're already on our way.
421 if (!strcasecmp(ChrPtr(result), "authenticate")) {
422 become_logged_in(username, password, logged_in_response);
425 /* The specified OpenID was verified but the desired user name was either not specified via SRI
426 * or conflicts with an existing user. Either way the user will need to specify a new name.
429 else if (!strcasecmp(ChrPtr(result), "verify_only")) {
430 display_openid_name_request(claimed_id, username);
433 /* Did we manage to log in? If so, continue with the normal flow... */
441 display_openid_login(_("Your password was not accepted."));
445 FreeStrBuf(&username);
446 FreeStrBuf(&password);
447 FreeStrBuf(&claimed_id);
448 FreeStrBuf(&logged_in_response);
453 * Display a welcome screen to the user.
455 * If this is the first time login, and the web based setup is enabled,
456 * lead the user through the setup routines
458 void do_welcome(void)
461 #ifdef XXX_NOT_FINISHED_YET_XXX
466 * See if we have to run the first-time setup wizard
471 sprintf(wizard_filename, "setupwiz.%s.%s",
473 len = strlen(wizard_filename);
474 for (i=0; i<len; ++i) {
475 if ( (wizard_filename[i]==' ')
476 || (wizard_filename[i] == '/')
478 wizard_filename[i] = '_';
482 fp = fopen(wizard_filename, "r");
484 fgets(buf, sizeof buf, fp);
485 buf[strlen(buf)-1] = 0;
487 if (atoi(buf) == serv_info.serv_rev_level) {
488 setup_wizard = 1; /**< already run */
494 http_redirect("setup_wizard");
500 * Go to the user's preferred start page
502 if (!get_preference("startpage", &Buf)) {
504 StrBufPrintf(Buf, "dotskip&room=_BASEROOM_");
505 set_preference("startpage", Buf, 1);
507 if (ChrPtr(Buf)[0] == '/') {
508 StrBufCutLeft(Buf, 1);
510 if (StrLength(Buf) == 0)
511 StrBufAppendBufPlain(Buf, "dotgoto?room=_BASEROOM_", -1, 0);
512 http_redirect(ChrPtr(Buf));
517 * Disconnect from the Citadel server, and end this WebCit session
519 void end_webcit_session(void) {
523 /* close() of citadel socket will be done by do_housekeeping() */
533 FlushStrBuf(WC->wc_username);
534 FlushStrBuf(WC->wc_password);
535 FlushStrBuf(WC->wc_roomname);
536 FlushStrBuf(WC->wc_fullname);
538 /* FIXME: this is to suppress the iconbar displaying, because we aren't
539 actually logged out yet */
542 /** Calling output_headers() this way causes the cookies to be un-set */
543 output_headers(1, 1, 0, 1, 0, 0);
545 wprintf("<div id=\"logout_screen\">");
546 wprintf("<div class=\"box\">");
547 wprintf("<div class=\"boxlabel\">");
548 wprintf(_("Log off"));
549 wprintf("</div><div class=\"boxcontent\">");
550 serv_puts("MESG goodbye");
551 serv_getln(buf, sizeof buf);
553 if (WC->serv_sock >= 0) {
557 wprintf("Goodbye\n");
561 wprintf(_("This program was unable to connect or stay "
562 "connected to the Citadel server. Please report "
563 "this problem to your system administrator.")
565 wprintf("<a href=\"http://www.citadel.org/doku.php/"
566 "faq:mastering_your_os:net#netstat\">%s</a>",
570 wprintf("<hr /><div class=\"buttons\"> "
571 "<span class=\"button_link\"><a href=\".\">");
572 wprintf(_("Log in again"));
573 wprintf("</a></span> <span class=\"button_link\">"
574 "<a href=\"javascript:window.close();\">");
575 wprintf(_("Close window"));
576 wprintf("</a></span></div></div></div></div>\n");
578 end_webcit_session();
592 output_headers(1, 1, 2, 0, 0, 0);
593 wprintf("<div id=\"banner\">\n");
595 wprintf(_("Validate new users"));
599 wprintf("<div id=\"content\" class=\"service\">\n");
601 /* If the user just submitted a validation, process it... */
602 safestrncpy(buf, bstr("user"), sizeof buf);
603 if (!IsEmptyStr(buf)) {
604 if (havebstr("axlevel")) {
605 serv_printf("VALI %s|%s", buf, bstr("axlevel"));
606 serv_getln(buf, sizeof buf);
608 wprintf("<b>%s</b><br>\n", &buf[4]);
613 /* Now see if any more users require validation. */
615 serv_getln(buf, sizeof buf);
618 wprintf(_("No users require validation at this time."));
619 wprintf("</b><br>\n");
624 wprintf("<b>%s</b><br>\n", &buf[4]);
629 wprintf("<div class=\"fix_scrollbar_bug\">"
630 "<table class=\"auth_validate\"><tr><td>\n");
631 wprintf("<div id=\"validate\">");
633 safestrncpy(user, &buf[4], sizeof user);
634 serv_printf("GREG %s", user);
635 serv_getln(cmd, sizeof cmd);
639 serv_getln(buf, sizeof buf);
642 wprintf("#%s<br><H1>%s</H1>",
651 while (!IsEmptyStr(pch))
655 else if (isalpha(*pch))
666 pch = _("very weak");
679 wprintf("PW: %s<br>\n", pch);
682 wprintf("%s<br>\n", buf);
684 wprintf("%s<br>\n", buf);
686 wprintf("%s, ", buf);
690 wprintf("%s<br>\n", buf);
692 wprintf("%s<br>\n", buf);
694 wprintf(_("Current access level: %d (%s)\n"),
695 atoi(buf), axdefs[atoi(buf)]);
696 } while (strcmp(buf, "000"));
698 wprintf("<H1>%s</H1>%s<br />\n", user, &cmd[4]);
702 wprintf(_("Select access level for this user:"));
704 for (a = 0; a <= 6; ++a) {
705 wprintf("<a href=\"validate?nonce=%d?user=", WC->nonce);
707 wprintf("&axlevel=%d\">%s</A> \n",
713 wprintf("</td></tr></table></div>\n");
720 * Display form for registration.
722 * (Set during_login to 1 if this registration is being performed during
723 * new user login and will require chaining to the proper screen.)
725 void display_reg(int during_login)
729 if (goto_config_room() != 0) {
730 if (during_login) do_welcome();
731 else display_main_menu();
735 vcard_msgnum = locate_user_vcard_in_this_room();
736 if (vcard_msgnum < 0L) {
737 if (during_login) do_welcome();
738 else display_main_menu();
743 do_edit_vcard(vcard_msgnum, "1", "do_welcome", USERCONFIGROOM);
746 do_edit_vcard(vcard_msgnum, "1", "display_main_menu", USERCONFIGROOM);
755 * display form for changing your password
757 void display_changepw(void)
759 WCTemplputParams SubTP;
762 output_headers(1, 1, 1, 0, 0, 0);
764 Buf = NewStrBufPlain(_("Change your password"), -1);
765 memset(&SubTP, 0, sizeof(WCTemplputParams));
766 SubTP.Filter.ContextType = CTX_STRBUF;
768 DoTemplate(HKEY("beginbox"), NULL, &SubTP);
772 if (!IsEmptyStr(WC->ImportantMessage)) {
773 wprintf("<span class=\"errormsg\">"
774 "%s</span><br />\n", WC->ImportantMessage);
775 safestrncpy(WC->ImportantMessage, "", sizeof WC->ImportantMessage);
778 serv_puts("MESG changepw");
779 serv_getln(buf, sizeof buf);
784 wprintf("<form name=\"changepwform\" action=\"changepw\" method=\"post\">\n");
785 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
786 wprintf("<table class=\"altern\" ");
787 wprintf("<tr class=\"even\"><td>");
788 wprintf(_("Enter new password:"));
789 wprintf("</td><td>");
790 wprintf("<input type=\"password\" name=\"newpass1\" value=\"\" maxlength=\"20\"></td></tr>\n");
791 wprintf("<tr class=\"odd\"><td>");
792 wprintf(_("Enter it again to confirm:"));
793 wprintf("</td><td>");
794 wprintf("<input type=\"password\" name=\"newpass2\" value=\"\" maxlength=\"20\"></td></tr>\n");
795 wprintf("</table>\n");
797 wprintf("<div class=\"buttons\">\n");
798 wprintf("<input type=\"submit\" name=\"change_action\" value=\"%s\">", _("Change password"));
800 wprintf("<input type=\"submit\" name=\"cancel_action\" value=\"%s\">\n", _("Cancel"));
802 wprintf("</form>\n");
804 do_template("endbox", NULL);
810 * if passwords match, propagate it to citserver.
815 char newpass1[32], newpass2[32];
817 if (!havebstr("change_action")) {
818 safestrncpy(WC->ImportantMessage,
819 _("Cancelled. Password was not changed."),
820 sizeof WC->ImportantMessage);
825 safestrncpy(newpass1, bstr("newpass1"), sizeof newpass1);
826 safestrncpy(newpass2, bstr("newpass2"), sizeof newpass2);
828 if (strcasecmp(newpass1, newpass2)) {
829 safestrncpy(WC->ImportantMessage,
830 _("They don't match. Password was not changed."),
831 sizeof WC->ImportantMessage);
836 if (IsEmptyStr(newpass1)) {
837 safestrncpy(WC->ImportantMessage,
838 _("Blank passwords are not allowed."),
839 sizeof WC->ImportantMessage);
844 serv_printf("SETP %s", newpass1);
845 serv_getln(buf, sizeof buf);
846 sprintf(WC->ImportantMessage, "%s", &buf[4]);
848 if (WC->wc_password == NULL)
849 WC->wc_password = NewStrBufPlain(buf, -1);
851 FlushStrBuf(WC->wc_password);
852 StrBufAppendBufPlain(WC->wc_password, buf, -1, 0);
861 int ConditionalAide(StrBuf *Target, WCTemplputParams *TP)
863 return (WC->is_aide == 0);
866 int ConditionalRoomAide(StrBuf *Target, WCTemplputParams *TP)
868 return (WC->is_room_aide == 0);
871 int ConditionalIsLoggedIn(StrBuf *Target, WCTemplputParams *TP) {
872 return (WC->logged_in == 0);
874 int ConditionalRoomAcessDelete(StrBuf *Target, WCTemplputParams *TP)
877 return ( (WCC->is_room_aide) || (WCC->is_mailbox) || (WCC->room_flags2 & QR2_COLLABDEL) );
882 void _display_openid_login(void) {display_openid_login(NULL);}
883 void _display_reg(void) {display_reg(0);}
890 WebcitAddUrlHandler(HKEY("do_welcome"), do_welcome, ANONYMOUS);
891 WebcitAddUrlHandler(HKEY("login"), do_login, ANONYMOUS);
892 WebcitAddUrlHandler(HKEY("display_openid_login"), _display_openid_login, ANONYMOUS);
893 WebcitAddUrlHandler(HKEY("openid_login"), do_openid_login, ANONYMOUS);
894 WebcitAddUrlHandler(HKEY("finalize_openid_login"), finalize_openid_login, ANONYMOUS);
895 WebcitAddUrlHandler(HKEY("openid_manual_create"), openid_manual_create, ANONYMOUS);
896 WebcitAddUrlHandler(HKEY("do_logout"), do_logout, 0);
897 WebcitAddUrlHandler(HKEY("validate"), validate, 0);
898 WebcitAddUrlHandler(HKEY("display_reg"), _display_reg, 0);
899 WebcitAddUrlHandler(HKEY("display_changepw"), display_changepw, 0);
900 WebcitAddUrlHandler(HKEY("changepw"), changepw, 0);
901 WebcitAddUrlHandler(HKEY("termquit"), do_logout, 0);
903 RegisterConditional(HKEY("COND:AIDE"), 2, ConditionalAide, CTX_NONE);
904 RegisterConditional(HKEY("COND:ROOMAIDE"), 2, ConditionalRoomAide, CTX_NONE);
905 RegisterConditional(HKEY("COND:ACCESS:DELETE"), 2, ConditionalRoomAcessDelete, CTX_NONE);
906 RegisterConditional(HKEY("COND:LOGGEDIN"), 2, ConditionalIsLoggedIn, CTX_NONE);