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)
56 output_headers(1, 1, 2, 0, 0, 0);
57 wprintf("<div id=\"login_screen\">\n");
59 if ((mesg != NULL) && (!IsEmptyStr(mesg))) {
60 stresc(buf, SIZ, mesg, 0, 0);
61 svprintf(HKEY("MESG"), WCS_STRING, "%s", buf);
64 svprintf(HKEY("LOGIN_INSTRUCTIONS"), WCS_STRING,
66 "<li>Enter your OpenID URL and click "Login"."
67 "<li><a href=\"http://www.citadel.org/doku.php/documentation:openid\">"
68 "Click here to learn what OpenID is and how Citadel is using it.</a>"
69 "<li>Please log off properly when finished. "
70 "<li>You must use a browser that supports <i>frames</i> and "
72 "<li>Also keep in mind that if your browser is "
73 "configured to block pop-up windows, you will not be able "
74 "to receive any instant messages.<br>"
78 svput("HELLO", WCS_SERVCMD, "MESG hello");
80 svprintf(HKEY("OFFER_CONVENTIONAL_LOGIN"), WCS_STRING,
81 "<div id=\"convlogin\">"
82 "<a href=\"display_login\">"
86 "Log in using a user name and password"
89 do_template("openid_login", NULL);
94 void display_openid_name_request(const StrBuf *claimed_id, const StrBuf *username)
98 output_headers(1, 1, 2, 0, 0, 0);
99 wprintf("<div id=\"login_screen\">\n");
101 Buf = NewStrBufPlain(NULL, StrLength(claimed_id));
102 StrEscAppend(Buf, claimed_id, NULL, 0, 0);
103 svprintf(HKEY("VERIFIED"), WCS_STRING, _("Your OpenID <tt>%s</tt> was successfully verified."),
105 SVPutBuf("CLAIMED_ID", Buf, 0);
108 if (StrLength(username) > 0) {
109 Buf = NewStrBufPlain(NULL, StrLength(username));
110 StrEscAppend(Buf, claimed_id, NULL, 0, 0);
111 svprintf(HKEY("REASON"), WCS_STRING,
112 _("However, the user name '%s' conflicts with an existing user."),
117 svput("REASON", WCS_STRING, "");
120 svput("ACTION_REQUESTED", WCS_STRING, _("Please specify the user name you would like to use."));
122 svput("USERNAME_BOX", WCS_STRING, _("User name:"));
123 svput("NEWUSER_BUTTON", WCS_STRING, _("New User"));
124 svput("EXIT_BUTTON", WCS_STRING, _("Exit"));
126 svprintf(HKEY("BOXTITLE"), WCS_STRING, _("%s - powered by <a href=\"http://www.citadel.org\">Citadel</a>"),
127 ChrPtr(serv_info.serv_humannode));
129 do_template("openid_manual_create", NULL);
135 /* Initialize the session
137 * This function needs to get called whenever the session changes from
138 * not-logged-in to logged-in, either by an explicit login by the user or
139 * by a timed-out session automatically re-establishing with a little help
140 * from the browser cookie. Either way, we need to load access controls and
141 * preferences from the server.
145 * serv_response The parameters returned from a Citadel USER or NEWU command
147 void become_logged_in(const StrBuf *user, const StrBuf *pass, StrBuf *serv_response)
155 if (WCC->wc_fullname == NULL)
156 WCC->wc_fullname = NewStrBufPlain(NULL, StrLength(serv_response));
157 StrBufExtract_token(WCC->wc_fullname, serv_response, 0, '|');
158 StrBufCutLeft(WCC->wc_fullname, 4 );
160 if (WCC->wc_username == NULL)
161 WCC->wc_username = NewStrBufDup(user);
163 FlushStrBuf(WCC->wc_username);
164 StrBufAppendBuf(WCC->wc_username, user, 0);
167 if (WCC->wc_password == NULL)
168 WCC->wc_password = NewStrBufDup(pass);
170 FlushStrBuf(WCC->wc_password);
171 StrBufAppendBuf(WCC->wc_password, pass, 0);
174 WCC->axlevel = StrBufExtract_int(serv_response, 1, '|');
175 if (WCC->axlevel >= 6) { /* TODO: make this a define, else it might trick us later */
182 serv_getln(buf, sizeof buf);
184 WC->new_mail = extract_int(&buf[4], 0);
185 WC->need_regi = extract_int(&buf[4], 1);
186 WC->need_vali = extract_int(&buf[4], 2);
187 extract_token(WC->cs_inet_email, &buf[4], 3, '|', sizeof WC->cs_inet_email);
190 get_preference("floordiv_expanded", &FloorDiv);
191 WC->floordiv_expanded = FloorDiv;
196 * Perform authentication using a user name and password
203 if (havebstr("language")) {
204 set_selected_language(bstr("language"));
205 go_selected_language();
208 if (havebstr("exit_action")) {
213 if (havebstr("login_action")) {
214 serv_printf("USER %s", bstr("name"));
215 StrBuf_ServGetln(Buf);
216 if (GetServerStatus(Buf, NULL) == 3) {
217 serv_printf("PASS %s", bstr("pass"));
218 StrBuf_ServGetln(Buf);
219 if (GetServerStatus(Buf, NULL) == 2) {
220 become_logged_in(sbstr("name"), sbstr("pass"), Buf);
222 snprintf(WCC->ImportantMessage,
223 sizeof (WCC->ImportantMessage),
231 snprintf(WCC->ImportantMessage,
232 sizeof (WCC->ImportantMessage),
240 if (havebstr("newuser_action")) {
241 if (!havebstr("pass")) {
242 snprintf(WCC->ImportantMessage,
243 sizeof (WCC->ImportantMessage),
245 _("Blank passwords are not allowed."));
250 serv_printf("NEWU %s", bstr("name"));
251 StrBuf_ServGetln(Buf);
252 if (GetServerStatus(Buf, NULL) == 2) {
253 become_logged_in(sbstr("name"), sbstr("pass"), Buf);
254 serv_printf("SETP %s", bstr("pass"));
255 StrBuf_ServGetln(Buf); /* Don't care? */
257 snprintf(WCC->ImportantMessage,
258 sizeof (WCC->ImportantMessage),
266 if (WCC->logged_in) {
267 set_preference("language", NewStrBufPlain(bstr("language"), -1), 1);
268 if (WCC->need_regi) {
270 } else if (WCC->need_vali) {
276 snprintf(WCC->ImportantMessage,
277 sizeof (WCC->ImportantMessage),
279 _("Your password was not accepted."));
286 * Try to create an account manually after an OpenID was verified
288 void openid_manual_create(void)
292 if (havebstr("exit_action")) {
297 if (havebstr("newuser_action")) {
299 serv_printf("OIDC %s", bstr("name"));
300 StrBuf_ServGetln(Buf);
301 if (GetServerStatus(Buf, NULL) == 2) {
305 serv_puts("SETP GENERATE_RANDOM_PASSWORD");
306 StrBuf_ServGetln(gpass);
307 StrBufCutLeft(gpass, 4);
308 become_logged_in(sbstr("name"), gpass, Buf);
317 } else if (WC->need_vali) {
323 display_openid_name_request(sbstr("openid_url"), sbstr("name"));
330 * Perform authentication using OpenID
331 * assemble the checkid_setup request and then redirect to the user's identity provider
333 void do_openid_login(void)
338 if (havebstr("language")) {
339 set_selected_language(bstr("language"));
340 go_selected_language();
343 if (havebstr("exit_action")) {
347 if (havebstr("login_action")) {
348 snprintf(buf, sizeof buf,
349 "OIDS %s|%s://%s/finalize_openid_login|%s://%s",
351 (is_https ? "https" : "http"), ChrPtr(WCC->http_host),
352 (is_https ? "https" : "http"), ChrPtr(WCC->http_host)
356 serv_getln(buf, sizeof buf);
358 lprintf(CTDL_DEBUG, "OpenID server contacted; redirecting to %s\n", &buf[4]);
359 http_redirect(&buf[4]);
363 display_openid_login(&buf[4]);
368 /* If we get to this point then something failed. */
369 display_openid_login(_("Your password was not accepted."));
373 * Complete the authentication using OpenID
374 * This function handles the positive or negative assertion from the user's Identity Provider
376 void finalize_openid_login(void)
380 int already_logged_in = (WCC->logged_in) ;
382 StrBuf *result = NULL;
383 StrBuf *username = NULL;
384 StrBuf *password = NULL;
385 StrBuf *logged_in_response = NULL;
386 StrBuf *claimed_id = NULL;
388 if (havebstr("openid.mode")) {
389 if (!strcasecmp(bstr("openid.mode"), "id_res")) {
392 StrBuf_ServGetln(Buf);
393 if (GetServerStatus(Buf, NULL) == 8) {
400 Cursor = GetNewHashPos (WCC->urlstrings, 0);
401 while (GetNextHashPos(WCC->urlstrings, Cursor, &HKLen, &HKey, &U)) {
403 if (!strncasecmp(u->url_key, "openid.", 7)) {
404 serv_printf("%s|%s", &u->url_key[7], ChrPtr(u->url_data));
411 while (StrBuf_ServGetln(Buf),
412 (StrLength(Buf)==3) &&
413 !strcmp(ChrPtr(Buf), "000"))
415 if (linecount == 0) result = NewStrBufDup(Buf);
416 if (!strcasecmp(ChrPtr(result), "authenticate")) {
417 if (linecount == 1) {
418 username = NewStrBufDup(Buf);
420 else if (linecount == 2) {
421 password = NewStrBufDup(Buf);
423 else if (linecount == 3) {
424 logged_in_response = NewStrBufDup(Buf);
427 else if (!strcasecmp(ChrPtr(result), "verify_only")) {
428 if (linecount == 1) {
429 claimed_id = NewStrBufDup(Buf);
431 if (linecount == 2) {
432 username = NewStrBufDup(Buf);
442 /* If we were already logged in, this was an attempt to associate an OpenID account */
443 if (already_logged_in) {
446 FreeStrBuf(&username);
447 FreeStrBuf(&password);
448 FreeStrBuf(&claimed_id);
449 FreeStrBuf(&logged_in_response);
453 /* If this operation logged us in, either by connecting with an existing account or by
454 * auto-creating one using Simple Registration Extension, we're already on our way.
456 if (!strcasecmp(ChrPtr(result), "authenticate")) {
457 become_logged_in(username, password, logged_in_response);
460 /* The specified OpenID was verified but the desired user name was either not specified via SRI
461 * or conflicts with an existing user. Either way the user will need to specify a new name.
464 else if (!strcasecmp(ChrPtr(result), "verify_only")) {
465 display_openid_name_request(claimed_id, username);
468 /* Did we manage to log in? If so, continue with the normal flow... */
476 display_openid_login(_("Your password was not accepted."));
480 FreeStrBuf(&username);
481 FreeStrBuf(&password);
482 FreeStrBuf(&claimed_id);
483 FreeStrBuf(&logged_in_response);
488 * Display a welcome screen to the user.
490 * If this is the first time login, and the web based setup is enabled,
491 * lead the user through the setup routines
493 void do_welcome(void)
496 #ifdef XXX_NOT_FINISHED_YET_XXX
501 * See if we have to run the first-time setup wizard
506 sprintf(wizard_filename, "setupwiz.%s.%s",
508 len = strlen(wizard_filename);
509 for (i=0; i<len; ++i) {
510 if ( (wizard_filename[i]==' ')
511 || (wizard_filename[i] == '/')
513 wizard_filename[i] = '_';
517 fp = fopen(wizard_filename, "r");
519 fgets(buf, sizeof buf, fp);
520 buf[strlen(buf)-1] = 0;
522 if (atoi(buf) == serv_info.serv_rev_level) {
523 setup_wizard = 1; /**< already run */
529 http_redirect("setup_wizard");
535 * Go to the user's preferred start page
537 if (!get_preference("startpage", &Buf)) {
539 StrBufPrintf(Buf, "dotskip&room=_BASEROOM_");
540 set_preference("startpage", Buf, 1);
542 if (ChrPtr(Buf)[0] == '/') {
543 StrBufCutLeft(Buf, 1);
545 if (StrLength(Buf) == 0)
546 StrBufAppendBufPlain(Buf, "dotgoto?room=_BASEROOM_", -1, 0);
547 http_redirect(ChrPtr(Buf));
552 * Disconnect from the Citadel server, and end this WebCit session
554 void end_webcit_session(void) {
558 /* close() of citadel socket will be done by do_housekeeping() */
568 FlushStrBuf(WC->wc_username);
569 FlushStrBuf(WC->wc_password);
570 FlushStrBuf(WC->wc_roomname);
571 FlushStrBuf(WC->wc_fullname);
573 /** Calling output_headers() this way causes the cookies to be un-set */
574 output_headers(1, 1, 0, 1, 0, 0);
576 wprintf("<div id=\"logout_screen\">");
577 wprintf("<div class=\"box\">");
578 wprintf("<div class=\"boxlabel\">");
579 wprintf(_("Log off"));
580 wprintf("</div><div class=\"boxcontent\">");
581 serv_puts("MESG goodbye");
582 serv_getln(buf, sizeof buf);
584 if (WC->serv_sock >= 0) {
588 wprintf("Goodbye\n");
592 wprintf(_("This program was unable to connect or stay "
593 "connected to the Citadel server. Please report "
594 "this problem to your system administrator.")
596 wprintf("<a href=\"http://www.citadel.org/doku.php/"
597 "faq:mastering_your_os:net#netstat\">%s</a>",
601 wprintf("<hr /><div class=\"buttons\"> "
602 "<span class=\"button_link\"><a href=\".\">");
603 wprintf(_("Log in again"));
604 wprintf("</a></span> <span class=\"button_link\">"
605 "<a href=\"javascript:window.close();\">");
606 wprintf(_("Close window"));
607 wprintf("</a></span></div></div></div></div>\n");
609 end_webcit_session();
623 output_headers(1, 1, 2, 0, 0, 0);
624 wprintf("<div id=\"banner\">\n");
626 wprintf(_("Validate new users"));
630 wprintf("<div id=\"content\" class=\"service\">\n");
632 /* If the user just submitted a validation, process it... */
633 safestrncpy(buf, bstr("user"), sizeof buf);
634 if (!IsEmptyStr(buf)) {
635 if (havebstr("axlevel")) {
636 serv_printf("VALI %s|%s", buf, bstr("axlevel"));
637 serv_getln(buf, sizeof buf);
639 wprintf("<b>%s</b><br>\n", &buf[4]);
644 /* Now see if any more users require validation. */
646 serv_getln(buf, sizeof buf);
649 wprintf(_("No users require validation at this time."));
650 wprintf("</b><br>\n");
655 wprintf("<b>%s</b><br>\n", &buf[4]);
660 wprintf("<div class=\"fix_scrollbar_bug\">"
661 "<table class=\"auth_validate\"><tr><td>\n");
662 wprintf("<div id=\"validate\">");
664 safestrncpy(user, &buf[4], sizeof user);
665 serv_printf("GREG %s", user);
666 serv_getln(cmd, sizeof cmd);
670 serv_getln(buf, sizeof buf);
673 wprintf("#%s<br><H1>%s</H1>",
682 while (!IsEmptyStr(pch))
686 else if (isalpha(*pch))
697 pch = _("very weak");
710 wprintf("PW: %s<br>\n", pch);
713 wprintf("%s<br>\n", buf);
715 wprintf("%s<br>\n", buf);
717 wprintf("%s, ", buf);
721 wprintf("%s<br>\n", buf);
723 wprintf("%s<br>\n", buf);
725 wprintf(_("Current access level: %d (%s)\n"),
726 atoi(buf), axdefs[atoi(buf)]);
727 } while (strcmp(buf, "000"));
729 wprintf("<H1>%s</H1>%s<br />\n", user, &cmd[4]);
733 wprintf(_("Select access level for this user:"));
735 for (a = 0; a <= 6; ++a) {
736 wprintf("<a href=\"validate?nonce=%d?user=", WC->nonce);
738 wprintf("&axlevel=%d\">%s</A> \n",
744 wprintf("</td></tr></table></div>\n");
751 * Display form for registration.
753 * (Set during_login to 1 if this registration is being performed during
754 * new user login and will require chaining to the proper screen.)
756 void display_reg(int during_login)
760 if (goto_config_room() != 0) {
761 if (during_login) do_welcome();
762 else display_main_menu();
766 vcard_msgnum = locate_user_vcard_in_this_room();
767 if (vcard_msgnum < 0L) {
768 if (during_login) do_welcome();
769 else display_main_menu();
774 do_edit_vcard(vcard_msgnum, "1", "do_welcome", USERCONFIGROOM);
777 do_edit_vcard(vcard_msgnum, "1", "display_main_menu", USERCONFIGROOM);
786 * display form for changing your password
788 void display_changepw(void)
790 WCTemplputParams SubTP;
793 output_headers(1, 1, 1, 0, 0, 0);
795 Buf = NewStrBufPlain(_("Change your password"), -1);
796 memset(&SubTP, 0, sizeof(WCTemplputParams));
797 SubTP.Filter.ContextType = CTX_STRBUF;
799 DoTemplate(HKEY("beginbox"), NULL, &SubTP);
803 if (!IsEmptyStr(WC->ImportantMessage)) {
804 wprintf("<span class=\"errormsg\">"
805 "%s</span><br />\n", WC->ImportantMessage);
806 safestrncpy(WC->ImportantMessage, "", sizeof WC->ImportantMessage);
809 serv_puts("MESG changepw");
810 serv_getln(buf, sizeof buf);
815 wprintf("<form name=\"changepwform\" action=\"changepw\" method=\"post\">\n");
816 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
817 wprintf("<table class=\"altern\" ");
818 wprintf("<tr class=\"even\"><td>");
819 wprintf(_("Enter new password:"));
820 wprintf("</td><td>");
821 wprintf("<input type=\"password\" name=\"newpass1\" value=\"\" maxlength=\"20\"></td></tr>\n");
822 wprintf("<tr class=\"odd\"><td>");
823 wprintf(_("Enter it again to confirm:"));
824 wprintf("</td><td>");
825 wprintf("<input type=\"password\" name=\"newpass2\" value=\"\" maxlength=\"20\"></td></tr>\n");
826 wprintf("</table>\n");
828 wprintf("<div class=\"buttons\">\n");
829 wprintf("<input type=\"submit\" name=\"change_action\" value=\"%s\">", _("Change password"));
831 wprintf("<input type=\"submit\" name=\"cancel_action\" value=\"%s\">\n", _("Cancel"));
833 wprintf("</form>\n");
835 do_template("endbox", NULL);
841 * if passwords match, propagate it to citserver.
846 char newpass1[32], newpass2[32];
848 if (!havebstr("change_action")) {
849 safestrncpy(WC->ImportantMessage,
850 _("Cancelled. Password was not changed."),
851 sizeof WC->ImportantMessage);
856 safestrncpy(newpass1, bstr("newpass1"), sizeof newpass1);
857 safestrncpy(newpass2, bstr("newpass2"), sizeof newpass2);
859 if (strcasecmp(newpass1, newpass2)) {
860 safestrncpy(WC->ImportantMessage,
861 _("They don't match. Password was not changed."),
862 sizeof WC->ImportantMessage);
867 if (IsEmptyStr(newpass1)) {
868 safestrncpy(WC->ImportantMessage,
869 _("Blank passwords are not allowed."),
870 sizeof WC->ImportantMessage);
875 serv_printf("SETP %s", newpass1);
876 serv_getln(buf, sizeof buf);
877 sprintf(WC->ImportantMessage, "%s", &buf[4]);
879 if (WC->wc_password == NULL)
880 WC->wc_password = NewStrBufPlain(buf, -1);
882 FlushStrBuf(WC->wc_password);
883 StrBufAppendBufPlain(WC->wc_password, buf, -1, 0);
892 int ConditionalAide(StrBuf *Target, WCTemplputParams *TP)
894 return (WC->is_aide == 0);
897 int ConditionalRoomAide(StrBuf *Target, WCTemplputParams *TP)
899 return (WC->is_room_aide == 0);
902 int ConditionalIsLoggedIn(StrBuf *Target, WCTemplputParams *TP) {
903 return (WC->logged_in == 0);
905 int ConditionalRoomAcessDelete(StrBuf *Target, WCTemplputParams *TP)
908 return ( (WCC->is_room_aide) || (WCC->is_mailbox) || (WCC->room_flags2 & QR2_COLLABDEL) );
913 void _display_openid_login(void) {display_openid_login(NULL);}
914 void _display_reg(void) {display_reg(0);}
921 WebcitAddUrlHandler(HKEY("do_welcome"), do_welcome, ANONYMOUS);
922 WebcitAddUrlHandler(HKEY("login"), do_login, ANONYMOUS);
923 WebcitAddUrlHandler(HKEY("display_openid_login"), _display_openid_login, ANONYMOUS);
924 WebcitAddUrlHandler(HKEY("openid_login"), do_openid_login, ANONYMOUS);
925 WebcitAddUrlHandler(HKEY("finalize_openid_login"), finalize_openid_login, ANONYMOUS);
926 WebcitAddUrlHandler(HKEY("openid_manual_create"), openid_manual_create, ANONYMOUS);
927 WebcitAddUrlHandler(HKEY("do_logout"), do_logout, 0);
928 WebcitAddUrlHandler(HKEY("validate"), validate, 0);
929 WebcitAddUrlHandler(HKEY("display_reg"), _display_reg, 0);
930 WebcitAddUrlHandler(HKEY("display_changepw"), display_changepw, 0);
931 WebcitAddUrlHandler(HKEY("changepw"), changepw, 0);
932 WebcitAddUrlHandler(HKEY("termquit"), do_logout, 0);
934 RegisterConditional(HKEY("COND:AIDE"), 2, ConditionalAide, CTX_NONE);
935 RegisterConditional(HKEY("COND:ROOMAIDE"), 2, ConditionalRoomAide, CTX_NONE);
936 RegisterConditional(HKEY("COND:ACCESS:DELETE"), 2, ConditionalRoomAcessDelete, CTX_NONE);
937 RegisterConditional(HKEY("COND:LOGGEDIN"), 2, ConditionalIsLoggedIn, CTX_NONE);