2 * These functions handle authentication of users to a Citadel server.
4 * Copyright (c) 1996-2012 by the citadel.org team
6 * This program is open source software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include "webserver.h"
19 extern uint32_t hashlittle( const void *key, size_t length, uint32_t initval);
22 * Access level definitions. This is initialized from a function rather than a
23 * static array so that the strings may be localized.
27 void initialize_axdefs(void) {
30 axdefs[0] = _("Deleted");
33 axdefs[1] = _("New User");
36 axdefs[2] = _("Problem User");
38 /* user with normal privileges */
39 axdefs[3] = _("Local User");
41 /* a user that may access network resources */
42 axdefs[4] = _("Network User");
45 axdefs[5] = _("Preferred User");
48 axdefs[6] = _("Admin");
54 * Display the login screen
55 * mesg = the error message if last attempt failed.
57 void display_login(void)
60 output_headers(1, 0, 0, 0, 1, 0);
69 /* Initialize the session
71 * This function needs to get called whenever the session changes from
72 * not-logged-in to logged-in, either by an explicit login by the user or
73 * by a timed-out session automatically re-establishing with a little help
74 * from the browser cookie. Either way, we need to load access controls and
75 * preferences from the server.
79 * serv_response The parameters returned from a Citadel USER or NEWU command
81 void become_logged_in(const StrBuf *user, const StrBuf *pass, StrBuf *serv_response)
89 if (WCC->wc_fullname == NULL)
90 WCC->wc_fullname = NewStrBufPlain(NULL, StrLength(serv_response));
91 StrBufExtract_token(WCC->wc_fullname, serv_response, 0, '|');
92 StrBufCutLeft(WCC->wc_fullname, 4 );
94 if (WCC->wc_username == NULL)
95 WCC->wc_username = NewStrBufDup(user);
97 FlushStrBuf(WCC->wc_username);
98 StrBufAppendBuf(WCC->wc_username, user, 0);
101 if (WCC->wc_password == NULL)
102 WCC->wc_password = NewStrBufDup(pass);
104 FlushStrBuf(WCC->wc_password);
105 StrBufAppendBuf(WCC->wc_password, pass, 0);
108 WCC->axlevel = StrBufExtract_int(serv_response, 1, '|');
109 if (WCC->axlevel >= 6) {
117 StrBuf_ServGetln(Buf);
118 if (GetServerStatus(Buf, NULL) == 2) {
121 pch = ChrPtr(Buf) + 4;
122 /*WCC->new_mail =*/ StrBufExtractNext_long(Buf, &pch, '|');
123 WCC->need_regi = StrBufExtractNext_long(Buf, &pch, '|');
124 WCC->need_vali = StrBufExtractNext_long(Buf, &pch, '|');
125 if (WCC->cs_inet_email == NULL)
126 WCC->cs_inet_email = NewStrBuf();
127 StrBufExtract_NextToken(WCC->cs_inet_email, Buf, &pch, '|');
129 get_preference("floordiv_expanded", &FloorDiv);
130 WCC->floordiv_expanded = FloorDiv;
137 * modal/ajax version of 'login' (username and password)
139 void ajax_login_username_password(void) {
140 StrBuf *Buf = NewStrBuf();
142 serv_printf("USER %s", bstr("name"));
143 StrBuf_ServGetln(Buf);
144 if (GetServerStatus(Buf, NULL) == 3) {
145 serv_printf("PASS %s", bstr("pass"));
146 StrBuf_ServGetln(Buf);
147 if (GetServerStatus(Buf, NULL) == 2) {
148 become_logged_in(sbstr("name"), sbstr("pass"), Buf);
152 /* The client is expecting to read back a citadel protocol response */
153 wc_printf("%s", ChrPtr(Buf));
160 * modal/ajax version of 'new user' (username and password)
162 void ajax_login_newuser(void) {
163 StrBuf *NBuf = NewStrBuf();
164 StrBuf *SBuf = NewStrBuf();
166 serv_printf("NEWU %s", bstr("name"));
167 StrBuf_ServGetln(NBuf);
168 if (GetServerStatus(NBuf, NULL) == 2) {
169 become_logged_in(sbstr("name"), sbstr("pass"), NBuf);
170 serv_printf("SETP %s", bstr("pass"));
171 StrBuf_ServGetln(SBuf);
174 /* The client is expecting to read back a citadel protocol response */
175 wc_printf("%s", ChrPtr(NBuf));
183 * Try to create an account manually after an OpenID was verified
185 void openid_manual_create(void)
189 /* Did the user change his mind? Pack up and go home. */
190 if (havebstr("exit_action")) {
192 output_headers(1, 0, 0, 0, 1, 0);
193 do_template("authpopup_finished");
199 /* Ok, let's give this a try. Can we create the new user? */
202 serv_printf("OIDC %s", bstr("name"));
203 StrBuf_ServGetln(Buf);
204 if (GetServerStatus(Buf, NULL) == 2) {
208 serv_puts("SETP GENERATE_RANDOM_PASSWORD");
209 StrBuf_ServGetln(gpass);
210 StrBufCutLeft(gpass, 4);
211 become_logged_in(sbstr("name"), gpass, Buf);
216 /* Did we manage to log in? If so, continue with the normal flow... */
220 output_headers(1, 0, 0, 0, 1, 0);
221 do_template("authpopup_finished");
225 /* Still no good! Go back to teh dialog to select a username */
227 putbstr("__claimed_id", NewStrBufDup(sbstr("openid_url")));
229 if (StrLength(Buf) > 0)
230 putbstr("__username", NewStrBufDup(Buf));
232 output_headers(1, 0, 0, 0, 1, 0);
233 wc_printf("<html><body>");
234 do_template("openid_manual_create");
235 wc_printf("</body></html>");
243 * Perform authentication using OpenID
244 * assemble the checkid_setup request and then redirect to the user's identity provider
246 void do_openid_login(void)
250 snprintf(buf, sizeof buf,
251 "OIDS %s|%s/finalize_openid_login|%s",
258 serv_getln(buf, sizeof buf);
260 syslog(LOG_DEBUG, "OpenID server contacted; redirecting to %s\n", &buf[4]);
261 http_redirect(&buf[4]);
266 output_headers(1, 0, 0, 0, 1, 0);
267 wc_printf("<html><body>");
269 wc_printf("</body></html>");
275 * Complete the authentication using OpenID
276 * This function handles the positive or negative assertion from the user's Identity Provider
278 void finalize_openid_login(void)
283 StrBuf *result = NULL;
284 StrBuf *username = NULL;
285 StrBuf *password = NULL;
286 StrBuf *logged_in_response = NULL;
287 StrBuf *claimed_id = NULL;
289 if (havebstr("openid.mode")) {
290 if (!strcasecmp(bstr("openid.mode"), "id_res")) {
293 StrBuf_ServGetln(Buf);
294 if (GetServerStatus(Buf, NULL) == 8) {
302 Cursor = GetNewHashPos (WCC->Hdr->urlstrings, 0);
303 while (GetNextHashPos(WCC->Hdr->urlstrings, Cursor, &HKLen, &HKey, &U)) {
305 if (!strncasecmp(u->url_key, "openid.", 7)) {
306 serv_printf("%s|%s", &u->url_key[7], ChrPtr(u->url_data));
313 while (len = StrBuf_ServGetln(Buf),
315 ((len != 3) || strcmp(ChrPtr(Buf), "000") )))
317 if (linecount == 0) result = NewStrBufDup(Buf);
318 if (!strcasecmp(ChrPtr(result), "authenticate")) {
319 if (linecount == 1) {
320 username = NewStrBufDup(Buf);
322 else if (linecount == 2) {
323 password = NewStrBufDup(Buf);
325 else if (linecount == 3) {
326 logged_in_response = NewStrBufDup(Buf);
329 else if (!strcasecmp(ChrPtr(result), "verify_only")) {
330 if (linecount == 1) {
331 claimed_id = NewStrBufDup(Buf);
333 if (linecount == 2) {
334 username = NewStrBufDup(Buf);
345 * Is this an attempt to associate a new OpenID with an account that is already logged in?
347 if ( (WCC->logged_in) && (havebstr("attach_existing")) ) {
351 /* If this operation logged us in, either by connecting with an existing account or by
352 * auto-creating one using Simple Registration Extension, we're already on our way.
354 else if (!strcasecmp(ChrPtr(result), "authenticate")) {
355 become_logged_in(username, password, logged_in_response);
357 /* Did we manage to log in? If so, continue with the normal flow... */
360 output_headers(1, 0, 0, 0, 1, 0);
361 do_template("authpopup_finished");
365 output_headers(1, 0, 0, 0, 1, 0);
366 wc_printf("<html><body>");
367 wc_printf(_("An error has occurred."));
368 wc_printf("</body></html>");
373 /* The specified OpenID was verified but the desired user name was either not specified via SRE
374 * or conflicts with an existing user. Either way the user will need to specify a new name.
376 else if (!strcasecmp(ChrPtr(result), "verify_only")) {
377 putbstr("__claimed_id", claimed_id);
379 if (StrLength(username) > 0) {
380 putbstr("__username", username);
384 output_headers(1, 0, 0, 0, 1, 0);
385 wc_printf("<html><body>");
386 do_template("openid_manual_create");
387 wc_printf("</body></html>");
391 /* Something went VERY wrong if we get to this point */
393 syslog(LOG_DEBUG, "finalize_openid_login() failed to do anything. This is a code problem.\n");
395 output_headers(1, 0, 0, 0, 1, 0);
396 wc_printf("<html><body>");
397 wc_printf(_("An error has occurred."));
398 wc_printf("</body></html>");
403 FreeStrBuf(&username);
404 FreeStrBuf(&password);
405 FreeStrBuf(&claimed_id);
406 FreeStrBuf(&logged_in_response);
411 * Display a welcome screen to the user.
413 * If this is the first time login, and the web based setup is enabled,
414 * lead the user through the setup routines
416 void do_welcome(void)
419 #ifdef XXX_NOT_FINISHED_YET_XXX
424 * See if we have to run the first-time setup wizard
429 sprintf(wizard_filename, "setupwiz.%s.%s",
430 abs(HashLittle(ctdlhost, strlen(ctdlhost))),
431 abs(HashLittle(ctdlport, strlen(ctdlport)))
434 fp = fopen(wizard_filename, "r");
436 fgets(buf, sizeof buf, fp);
437 buf[strlen(buf)-1] = 0;
439 if (atoi(buf) == serv_info.serv_rev_level) {
440 setup_wizard = 1; /* already run */
446 http_redirect("setup_wizard");
452 * Go to the user's preferred start page
454 if (!get_preference("startpage", &Buf)) {
456 StrBufPrintf(Buf, "dotskip?room=_BASEROOM_");
457 set_preference("startpage", Buf, 1);
459 if (ChrPtr(Buf)[0] == '/') {
460 StrBufCutLeft(Buf, 1);
462 if (StrLength(Buf) == 0) {
463 StrBufAppendBufPlain(Buf, "dotgoto?room=_BASEROOM_", -1, 0);
465 syslog(LOG_DEBUG, "Redirecting to user's start page: %s\n", ChrPtr(Buf));
466 http_redirect(ChrPtr(Buf));
471 * Disconnect from the Citadel server, and end this WebCit session
473 void end_webcit_session(void) {
476 /* close() of citadel socket will be done by do_housekeeping() */
481 * Log out the session with the Citadel server
488 FlushStrBuf(WCC->wc_username);
489 FlushStrBuf(WCC->wc_password);
490 FlushStrBuf(WCC->wc_fullname);
494 serv_getln(buf, sizeof buf);
497 FlushStrBuf(WCC->CurRoom.name);
499 /* Calling output_headers() this way causes the cookies to be un-set */
500 output_headers(1, 1, 0, 1, 0, 0);
501 do_template("logout");
502 if ((WCC->serv_info != NULL) && WCC->serv_info->serv_supports_guest) {
503 display_default_landing_page();
508 end_webcit_session();
513 * Special page for monitoring scripts etc
517 output_headers(0, 0, 0, 0, 0, 0);
519 hprintf("Content-type: text/plain\r\n"
520 "Server: " PACKAGE_STRING "\r\n"
521 "Connection: close\r\n"
525 wc_printf("Connection to Citadel server at %s:%s : %s\r\n",
527 (WC->connected ? "SUCCESS" : "FAIL")
544 output_headers(1, 1, 1, 0, 0, 0);
546 do_template("box_begin_1");
547 StrBufAppendBufPlain(WC->WBuf, _("Validate new users"), -1, 0);
548 do_template("box_begin_2");
550 /* If the user just submitted a validation, process it... */
551 safestrncpy(buf, bstr("user"), sizeof buf);
552 if (!IsEmptyStr(buf)) {
553 if (havebstr("axlevel")) {
554 serv_printf("VALI %s|%s", buf, bstr("axlevel"));
555 serv_getln(buf, sizeof buf);
557 wc_printf("<b>%s</b><br>\n", &buf[4]);
562 /* Now see if any more users require validation. */
564 serv_getln(buf, sizeof buf);
567 wc_printf(_("No users require validation at this time."));
568 wc_printf("</b><br>\n");
573 wc_printf("<b>%s</b><br>\n", &buf[4]);
578 wc_printf("<table class=\"auth_validate\"><tr><td>\n");
579 wc_printf("<div id=\"validate\">");
581 safestrncpy(user, &buf[4], sizeof user);
582 serv_printf("GREG %s", user);
583 serv_getln(cmd, sizeof cmd);
587 serv_getln(buf, sizeof buf);
590 wc_printf("#%s<br><H1>%s</H1>",
599 while (!IsEmptyStr(pch))
603 else if (isalpha(*pch))
617 pch = _("very weak");
630 wc_printf("PW: %s<br>\n", pch);
633 wc_printf("%s<br>\n", buf);
635 wc_printf("%s<br>\n", buf);
637 wc_printf("%s, ", buf);
639 wc_printf("%s ", buf);
641 wc_printf("%s<br>\n", buf);
643 wc_printf("%s<br>\n", buf);
645 wc_printf(_("Current access level: %d (%s)\n"),
646 atoi(buf), axdefs[atoi(buf)]);
647 } while (strcmp(buf, "000"));
649 wc_printf("<H1>%s</H1>%s<br>\n", user, &cmd[4]);
653 wc_printf(_("Select access level for this user:"));
655 for (a = 0; a <= 6; ++a) {
656 wc_printf("<a href=\"validate?nonce=%d?user=", WC->nonce);
658 wc_printf("&axlevel=%d\">%s</A> \n",
663 wc_printf("</div>\n");
664 wc_printf("</td></tr></table>\n");
665 do_template("box_end");
671 * Display form for registration.
673 * (Set during_login to 1 if this registration is being performed during
674 * new user login and will require chaining to the proper screen.)
676 void display_reg(int during_login)
680 message_summary *VCMsg = NULL;
681 wc_mime_attachment *VCAtt = NULL;
685 memset(&Room, 0, sizeof(folder));
686 if (goto_config_room(Buf, &Room) != 0) {
687 syslog(LOG_WARNING, "display_reg() exiting because goto_config_room() failed\n");
701 vcard_msgnum = locate_user_vcard_in_this_room(&VCMsg, &VCAtt);
702 if (vcard_msgnum < 0L) {
703 syslog(LOG_WARNING, "display_reg() exiting because locate_user_vcard_in_this_room() failed\n");
714 do_edit_vcard(vcard_msgnum, "1", VCMsg, VCAtt, "pop", USERCONFIGROOM);
718 ReturnTo = NewStrBufPlain(HKEY("display_main_menu?go="));
719 StrBufAppendBuf(ReturnTo, WC->CurRoom.name, 0);
720 do_edit_vcard(vcard_msgnum, "1", VCMsg, VCAtt, ChrPtr(ReturnTo), USERCONFIGROOM);
721 FreeStrBuf(&ReturnTo);
728 * if passwords match, propagate it to citserver.
733 char newpass1[32], newpass2[32];
735 if (!havebstr("change_action")) {
736 AppendImportantMessage(_("Cancelled. Password was not changed."), -1);
741 safestrncpy(newpass1, bstr("newpass1"), sizeof newpass1);
742 safestrncpy(newpass2, bstr("newpass2"), sizeof newpass2);
744 if (strcasecmp(newpass1, newpass2)) {
745 AppendImportantMessage(_("They don't match. Password was not changed."), -1);
746 do_template("menu_change_pw");
750 if (IsEmptyStr(newpass1)) {
751 AppendImportantMessage(_("Blank passwords are not allowed."), -1);
752 do_template("menu_change_pw");
757 serv_printf("SETP %s", newpass1);
758 StrBuf_ServGetln(Line);
759 if (GetServerStatusMsg(Line, NULL, 1, 0) == 2) {
760 if (WC->wc_password == NULL)
761 WC->wc_password = NewStrBufPlain(
763 StrLength(Line) - 4);
765 FlushStrBuf(WC->wc_password);
766 StrBufAppendBufPlain(WC->wc_password,
768 StrLength(Line) - 4, 0);
773 do_template("menu_change_pw");
779 int ConditionalHaveAccessCreateRoom(StrBuf *Target, WCTemplputParams *TP)
785 StrBuf_ServGetln(Buf);
787 if (GetServerStatus(Buf, NULL) == 2) {
788 StrBufCutLeft(Buf, 4);
789 AppendImportantMessage(SKEY(Buf));
798 int ConditionalAide(StrBuf *Target, WCTemplputParams *TP)
801 return (WCC != NULL) ? ((WCC->logged_in == 0)||(WC->is_aide == 0)) : 0;
805 int ConditionalIsLoggedIn(StrBuf *Target, WCTemplputParams *TP)
808 return (WCC != NULL) ? (WCC->logged_in == 0) : 0;
814 * toggle the session over to a different language
816 void switch_language(void) {
817 set_selected_language(bstr("lang"));
822 void _display_reg(void) {
827 void Header_HandleAuth(StrBuf *Line, ParsedHttpHdrs *hdr)
829 if (hdr->HR.got_auth == NO_AUTH) /* don't override cookie auth... */
831 if (strncasecmp(ChrPtr(Line), "Basic", 5) == 0) {
832 StrBufCutLeft(Line, 6);
833 StrBufDecodeBase64(Line);
834 hdr->HR.plainauth = Line;
835 hdr->HR.got_auth = AUTH_BASIC;
838 syslog(LOG_WARNING, "Authentication scheme not supported! [%s]\n", ChrPtr(Line));
843 void CheckAuthBasic(ParsedHttpHdrs *hdr)
846 todo: enable this if we can have other sessions than authenticated ones.
847 if (hdr->DontNeedAuth)
850 StrBufAppendBufPlain(hdr->HR.plainauth, HKEY(":"), 0);
851 StrBufAppendBuf(hdr->HR.plainauth, hdr->HR.user_agent, 0);
855 void GetAuthBasic(ParsedHttpHdrs *hdr)
857 const char *Pos = NULL;
858 if (hdr->c_username == NULL)
859 hdr->c_username = NewStrBufPlain(HKEY(DEFAULT_HTTPAUTH_USER));
860 if (hdr->c_password == NULL)
861 hdr->c_password = NewStrBufPlain(HKEY(DEFAULT_HTTPAUTH_PASS));
862 StrBufExtract_NextToken(hdr->c_username, hdr->HR.plainauth, &Pos, ':');
863 StrBufExtract_NextToken(hdr->c_password, hdr->HR.plainauth, &Pos, ':');
867 void Header_HandleCookie(StrBuf *Line, ParsedHttpHdrs *hdr)
871 todo: enable this if we can have other sessions than authenticated ones.
872 if (hdr->DontNeedAuth)
875 pch = strstr(ChrPtr(Line), "webcit=");
880 hdr->HR.RawCookie = Line;
881 StrBufCutLeft(hdr->HR.RawCookie, (pch - ChrPtr(hdr->HR.RawCookie)) + 7);
882 StrBufDecodeHex(hdr->HR.RawCookie);
884 cookie_to_stuff(Line, &hdr->HR.desired_session,
890 hdr->HR.got_auth = AUTH_COOKIE;
896 (ParsedHttpHdrs *httpreq)
898 httpreq->c_username = NewStrBufPlain(HKEY(DEFAULT_HTTPAUTH_USER));
899 httpreq->c_password = NewStrBufPlain(HKEY(DEFAULT_HTTPAUTH_PASS));
900 httpreq->c_roomname = NewStrBuf();
901 httpreq->c_language = NewStrBuf();
906 HttpDetachModule_AUTH
907 (ParsedHttpHdrs *httpreq)
909 FLUSHStrBuf(httpreq->c_username);
910 FLUSHStrBuf(httpreq->c_password);
911 FLUSHStrBuf(httpreq->c_roomname);
912 FLUSHStrBuf(httpreq->c_language);
917 HttpDestroyModule_AUTH
918 (ParsedHttpHdrs *httpreq)
920 FreeStrBuf(&httpreq->c_username);
921 FreeStrBuf(&httpreq->c_password);
922 FreeStrBuf(&httpreq->c_roomname);
923 FreeStrBuf(&httpreq->c_language);
932 RegisterHeaderHandler(HKEY("COOKIE"), Header_HandleCookie);
933 RegisterHeaderHandler(HKEY("AUTHORIZATION"), Header_HandleAuth);
935 /* no url pattern at all? Show login. */
936 WebcitAddUrlHandler(HKEY(""), "", 0, do_welcome, ANONYMOUS|COOKIEUNNEEDED);
938 WebcitAddUrlHandler(HKEY("do_welcome"), "", 0, do_welcome, ANONYMOUS|COOKIEUNNEEDED);
939 WebcitAddUrlHandler(HKEY("openid_login"), "", 0, do_openid_login, ANONYMOUS);
940 WebcitAddUrlHandler(HKEY("finalize_openid_login"), "", 0, finalize_openid_login, ANONYMOUS);
941 WebcitAddUrlHandler(HKEY("openid_manual_create"), "", 0, openid_manual_create, ANONYMOUS);
942 WebcitAddUrlHandler(HKEY("validate"), "", 0, validate, 0);
943 WebcitAddUrlHandler(HKEY("do_welcome"), "", 0, do_welcome, 0);
944 WebcitAddUrlHandler(HKEY("display_reg"), "", 0, _display_reg, 0);
945 WebcitAddUrlHandler(HKEY("changepw"), "", 0, changepw, 0);
946 WebcitAddUrlHandler(HKEY("termquit"), "", 0, do_logout, 0);
947 WebcitAddUrlHandler(HKEY("do_logout"), "", 0, do_logout, ANONYMOUS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
948 WebcitAddUrlHandler(HKEY("monitor"), "", 0, monitor, ANONYMOUS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
949 WebcitAddUrlHandler(HKEY("ajax_login_username_password"), "", 0, ajax_login_username_password, AJAX|ANONYMOUS);
950 WebcitAddUrlHandler(HKEY("ajax_login_newuser"), "", 0, ajax_login_newuser, AJAX|ANONYMOUS);
951 WebcitAddUrlHandler(HKEY("switch_language"), "", 0, switch_language, ANONYMOUS);
952 RegisterConditional("COND:AIDE", 2, ConditionalAide, CTX_NONE);
953 RegisterConditional("COND:LOGGEDIN", 2, ConditionalIsLoggedIn, CTX_NONE);
954 RegisterConditional("COND:MAY_CREATE_ROOM", 2, ConditionalHaveAccessCreateRoom, CTX_NONE);
960 SessionDestroyModule_AUTH
963 FreeStrBuf(&sess->wc_username);
964 FreeStrBuf(&sess->wc_fullname);
965 FreeStrBuf(&sess->wc_password);
966 FreeStrBuf(&sess->httpauth_pass);
967 FreeStrBuf(&sess->cs_inet_email);