2 * These functions handle authentication of users to a Citadel server.
4 * Copyright (c) 1996-2021 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"
21 * Access level definitions. This is initialized from a function rather than a
22 * static array so that the strings may be localized.
26 void initialize_axdefs(void) {
29 axdefs[0] = _("Deleted");
32 axdefs[1] = _("New User");
35 axdefs[2] = _("Problem User");
37 /* user with normal privileges */
38 axdefs[3] = _("Local User");
40 /* a user that may access network resources */
41 axdefs[4] = _("Network User");
44 axdefs[5] = _("Preferred User");
47 axdefs[6] = _("Admin");
53 * Display the login screen
54 * mesg = the error message if last attempt failed.
56 void display_login(void)
59 output_headers(1, 0, 0, 0, 1, 0);
68 /* Initialize the session
70 * This function needs to get called whenever the session changes from
71 * not-logged-in to logged-in, either by an explicit login by the user or
72 * by a timed-out session automatically re-establishing with a little help
73 * from the browser cookie. Either way, we need to load access controls and
74 * preferences from the server.
78 * serv_response The parameters returned from a Citadel USER or NEWU command
80 void become_logged_in(const StrBuf *user, const StrBuf *pass, StrBuf *serv_response)
88 if (WCC->wc_fullname == NULL)
89 WCC->wc_fullname = NewStrBufPlain(NULL, StrLength(serv_response));
90 StrBufExtract_token(WCC->wc_fullname, serv_response, 0, '|');
91 StrBufCutLeft(WCC->wc_fullname, 4 );
93 if (WCC->wc_username == NULL)
94 WCC->wc_username = NewStrBufDup(user);
96 FlushStrBuf(WCC->wc_username);
97 StrBufAppendBuf(WCC->wc_username, user, 0);
100 if (WCC->wc_password == NULL)
101 WCC->wc_password = NewStrBufDup(pass);
103 FlushStrBuf(WCC->wc_password);
104 StrBufAppendBuf(WCC->wc_password, pass, 0);
107 WCC->axlevel = StrBufExtract_int(serv_response, 1, '|');
108 if (WCC->axlevel >= 6) {
116 StrBuf_ServGetln(Buf);
117 if (GetServerStatus(Buf, NULL) == 2) {
120 pch = ChrPtr(Buf) + 4;
121 /*WCC->new_mail =*/ StrBufExtractNext_long(Buf, &pch, '|');
122 WCC->need_regi = StrBufExtractNext_long(Buf, &pch, '|');
123 WCC->need_vali = StrBufExtractNext_long(Buf, &pch, '|');
124 if (WCC->cs_inet_email == NULL)
125 WCC->cs_inet_email = NewStrBuf();
126 StrBufExtract_NextToken(WCC->cs_inet_email, Buf, &pch, '|');
128 get_preference("floordiv_expanded", &FloorDiv);
129 WCC->floordiv_expanded = FloorDiv;
136 * modal/ajax version of 'login' (username and password)
138 void ajax_login_username_password(void) {
139 StrBuf *Buf = NewStrBuf();
141 serv_printf("USER %s", bstr("name"));
142 StrBuf_ServGetln(Buf);
143 if (GetServerStatus(Buf, NULL) == 3) {
144 serv_printf("PASS %s", bstr("pass"));
145 StrBuf_ServGetln(Buf);
146 if (GetServerStatus(Buf, NULL) == 2) {
147 become_logged_in(sbstr("name"), sbstr("pass"), Buf);
151 /* The client is expecting to read back a citadel protocol response */
152 wc_printf("%s", ChrPtr(Buf));
159 * modal/ajax version of 'new user' (username and password)
161 void ajax_login_newuser(void) {
162 StrBuf *NBuf = NewStrBuf();
163 StrBuf *SBuf = NewStrBuf();
165 serv_printf("NEWU %s", bstr("name"));
166 StrBuf_ServGetln(NBuf);
167 if (GetServerStatus(NBuf, NULL) == 2) {
168 become_logged_in(sbstr("name"), sbstr("pass"), NBuf);
169 serv_printf("SETP %s", bstr("pass"));
170 StrBuf_ServGetln(SBuf);
173 /* The client is expecting to read back a citadel protocol response */
174 wc_printf("%s", ChrPtr(NBuf));
182 * Try to create an account manually after an OpenID was verified
184 void openid_manual_create(void)
188 /* Did the user change his mind? Pack up and go home. */
189 if (havebstr("exit_action")) {
191 output_headers(1, 0, 0, 0, 1, 0);
192 do_template("authpopup_finished");
198 /* Ok, let's give this a try. Can we create the new user? */
201 serv_printf("OIDC %s", bstr("name"));
202 StrBuf_ServGetln(Buf);
203 if (GetServerStatus(Buf, NULL) == 2) {
207 serv_puts("SETP GENERATE_RANDOM_PASSWORD");
208 StrBuf_ServGetln(gpass);
209 StrBufCutLeft(gpass, 4);
210 become_logged_in(sbstr("name"), gpass, Buf);
215 /* Did we manage to log in? If so, continue with the normal flow... */
219 output_headers(1, 0, 0, 0, 1, 0);
220 do_template("authpopup_finished");
224 /* Still no good! Go back to teh dialog to select a username */
226 putbstr("__claimed_id", NewStrBufDup(sbstr("openid_url")));
228 if (StrLength(Buf) > 0)
229 putbstr("__username", NewStrBufDup(Buf));
231 output_headers(1, 0, 0, 0, 1, 0);
232 wc_printf("<html><body>");
233 do_template("openid_manual_create");
234 wc_printf("</body></html>");
242 * Perform authentication using OpenID
243 * assemble the checkid_setup request and then redirect to the user's identity provider
245 void do_openid_login(void)
249 snprintf(buf, sizeof buf,
250 "OIDS %s|%s/finalize_openid_login|%s",
257 serv_getln(buf, sizeof buf);
259 syslog(LOG_DEBUG, "OpenID server contacted; redirecting to %s\n", &buf[4]);
260 http_redirect(&buf[4]);
265 output_headers(1, 0, 0, 0, 1, 0);
266 wc_printf("<html><body>");
268 wc_printf("</body></html>");
274 * Complete the authentication using OpenID
275 * This function handles the positive or negative assertion from the user's Identity Provider
277 void finalize_openid_login(void)
282 StrBuf *result = NULL;
283 StrBuf *username = NULL;
284 StrBuf *password = NULL;
285 StrBuf *logged_in_response = NULL;
286 StrBuf *claimed_id = NULL;
288 if (havebstr("openid.mode")) {
289 if (!strcasecmp(bstr("openid.mode"), "id_res")) {
292 StrBuf_ServGetln(Buf);
293 if (GetServerStatus(Buf, NULL) == 8) {
301 Cursor = GetNewHashPos (WCC->Hdr->urlstrings, 0);
302 while (GetNextHashPos(WCC->Hdr->urlstrings, Cursor, &HKLen, &HKey, &U)) {
304 if (!strncasecmp(u->url_key, "openid.", 7)) {
305 serv_printf("%s|%s", &u->url_key[7], ChrPtr(u->url_data));
312 while (len = StrBuf_ServGetln(Buf),
314 ((len != 3) || strcmp(ChrPtr(Buf), "000") )))
316 if (linecount == 0) result = NewStrBufDup(Buf);
317 if (!strcasecmp(ChrPtr(result), "authenticate")) {
318 if (linecount == 1) {
319 username = NewStrBufDup(Buf);
321 else if (linecount == 2) {
322 password = NewStrBufDup(Buf);
324 else if (linecount == 3) {
325 logged_in_response = NewStrBufDup(Buf);
328 else if (!strcasecmp(ChrPtr(result), "verify_only")) {
329 if (linecount == 1) {
330 claimed_id = NewStrBufDup(Buf);
332 if (linecount == 2) {
333 username = NewStrBufDup(Buf);
344 * Is this an attempt to associate a new OpenID with an account that is already logged in?
346 if ( (WCC->logged_in) && (havebstr("attach_existing")) ) {
350 /* If this operation logged us in, either by connecting with an existing account or by
351 * auto-creating one using Simple Registration Extension, we're already on our way.
353 else if (!strcasecmp(ChrPtr(result), "authenticate")) {
354 become_logged_in(username, password, logged_in_response);
356 /* Did we manage to log in? If so, continue with the normal flow... */
359 output_headers(1, 0, 0, 0, 1, 0);
360 do_template("authpopup_finished");
364 output_headers(1, 0, 0, 0, 1, 0);
365 wc_printf("<html><body>");
366 wc_printf(_("An error has occurred."));
367 wc_printf("</body></html>");
372 /* The specified OpenID was verified but the desired user name was either not specified via SRE
373 * or conflicts with an existing user. Either way the user will need to specify a new name.
375 else if (!strcasecmp(ChrPtr(result), "verify_only")) {
376 putbstr("__claimed_id", claimed_id);
378 if (StrLength(username) > 0) {
379 putbstr("__username", username);
383 output_headers(1, 0, 0, 0, 1, 0);
384 wc_printf("<html><body>");
385 do_template("openid_manual_create");
386 wc_printf("</body></html>");
390 /* Something went VERY wrong if we get to this point */
392 syslog(LOG_DEBUG, "finalize_openid_login() failed to do anything. This is a code problem.\n");
394 output_headers(1, 0, 0, 0, 1, 0);
395 wc_printf("<html><body>");
396 wc_printf(_("An error has occurred."));
397 wc_printf("</body></html>");
402 FreeStrBuf(&username);
403 FreeStrBuf(&password);
404 FreeStrBuf(&claimed_id);
405 FreeStrBuf(&logged_in_response);
410 * Display a welcome screen to the user.
412 void do_welcome(void) {
415 * Go to the user's preferred start page
417 if (!get_preference("startpage", &Buf)) {
419 StrBufPrintf(Buf, "dotskip?room=_BASEROOM_");
420 set_preference("startpage", Buf, 1);
422 if (ChrPtr(Buf)[0] == '/') {
423 StrBufCutLeft(Buf, 1);
425 if (StrLength(Buf) == 0) {
426 StrBufAppendBufPlain(Buf, "dotgoto?room=_BASEROOM_", -1, 0);
428 syslog(LOG_DEBUG, "Redirecting to user's start page: %s\n", ChrPtr(Buf));
429 http_redirect(ChrPtr(Buf));
434 * Disconnect from the Citadel server, and end this WebCit session
436 void end_webcit_session(void) {
439 /* close() of citadel socket will be done by do_housekeeping() */
444 * Log out the session with the Citadel server
451 FlushStrBuf(WCC->wc_username);
452 FlushStrBuf(WCC->wc_password);
453 FlushStrBuf(WCC->wc_fullname);
457 serv_getln(buf, sizeof buf);
460 FlushStrBuf(WCC->CurRoom.name);
462 /* Calling output_headers() this way causes the cookies to be un-set */
463 output_headers(1, 1, 0, 1, 0, 0);
464 do_template("logout");
465 if ((WCC->serv_info != NULL) && WCC->serv_info->serv_supports_guest) {
466 display_default_landing_page();
471 end_webcit_session();
476 * Special page for monitoring scripts etc
480 output_headers(0, 0, 0, 0, 0, 0);
482 hprintf("Content-type: text/plain\r\n"
483 "Server: " PACKAGE_STRING "\r\n"
484 "Connection: close\r\n"
488 wc_printf("Connection to Citadel server in %s : %s\r\n", ctdl_dir,
489 (WC->connected ? "SUCCESS" : "FAIL")
506 output_headers(1, 1, 1, 0, 0, 0);
508 do_template("box_begin_1");
509 StrBufAppendBufPlain(WC->WBuf, _("Validate new users"), -1, 0);
510 do_template("box_begin_2");
512 /* If the user just submitted a validation, process it... */
513 safestrncpy(buf, bstr("user"), sizeof buf);
514 if (!IsEmptyStr(buf)) {
515 if (havebstr("axlevel")) {
516 serv_printf("VALI %s|%s", buf, bstr("axlevel"));
517 serv_getln(buf, sizeof buf);
519 wc_printf("<b>%s</b><br>\n", &buf[4]);
524 /* Now see if any more users require validation. */
526 serv_getln(buf, sizeof buf);
529 wc_printf(_("No users require validation at this time."));
530 wc_printf("</b><br>\n");
535 wc_printf("<b>%s</b><br>\n", &buf[4]);
540 wc_printf("<table class=\"auth_validate\"><tr><td>\n");
541 wc_printf("<div id=\"validate\">");
543 safestrncpy(user, &buf[4], sizeof user);
544 serv_printf("GREG %s", user);
545 serv_getln(cmd, sizeof cmd);
549 serv_getln(buf, sizeof buf);
552 wc_printf("#%s<br><H1>%s</H1>",
561 while (!IsEmptyStr(pch))
565 else if (isalpha(*pch))
579 pch = _("very weak");
592 wc_printf("PW: %s<br>\n", pch);
595 wc_printf("%s<br>\n", buf);
597 wc_printf("%s<br>\n", buf);
599 wc_printf("%s, ", buf);
601 wc_printf("%s ", buf);
603 wc_printf("%s<br>\n", buf);
605 wc_printf("%s<br>\n", buf);
607 wc_printf(_("Current access level: %d (%s)\n"),
608 atoi(buf), axdefs[atoi(buf)]);
609 } while (strcmp(buf, "000"));
611 wc_printf("<H1>%s</H1>%s<br>\n", user, &cmd[4]);
615 wc_printf(_("Select access level for this user:"));
617 for (a = 0; a <= 6; ++a) {
618 wc_printf("<a href=\"validate?nonce=%d?user=", WC->nonce);
620 wc_printf("&axlevel=%d\">%s</A> \n",
625 wc_printf("</div>\n");
626 wc_printf("</td></tr></table>\n");
627 do_template("box_end");
633 * Display form for registration.
635 * (Set during_login to 1 if this registration is being performed during
636 * new user login and will require chaining to the proper screen.)
638 void display_reg(int during_login)
642 message_summary *VCMsg = NULL;
643 wc_mime_attachment *VCAtt = NULL;
647 memset(&Room, 0, sizeof(folder));
648 if (goto_config_room(Buf, &Room) != 0) {
649 syslog(LOG_WARNING, "display_reg() exiting because goto_config_room() failed\n");
663 vcard_msgnum = locate_user_vcard_in_this_room(&VCMsg, &VCAtt);
664 if (vcard_msgnum < 0L) {
665 syslog(LOG_WARNING, "display_reg() exiting because locate_user_vcard_in_this_room() failed\n");
676 do_edit_vcard(vcard_msgnum, "1", VCMsg, VCAtt, "pop", USERCONFIGROOM);
680 ReturnTo = NewStrBufPlain(HKEY("display_main_menu?go="));
681 StrBufAppendBuf(ReturnTo, WC->CurRoom.name, 0);
682 do_edit_vcard(vcard_msgnum, "1", VCMsg, VCAtt, ChrPtr(ReturnTo), USERCONFIGROOM);
683 FreeStrBuf(&ReturnTo);
690 * if passwords match, propagate it to citserver.
695 char newpass1[32], newpass2[32];
697 if (!havebstr("change_action")) {
698 AppendImportantMessage(_("Cancelled. Password was not changed."), -1);
703 safestrncpy(newpass1, bstr("newpass1"), sizeof newpass1);
704 safestrncpy(newpass2, bstr("newpass2"), sizeof newpass2);
706 if (strcasecmp(newpass1, newpass2)) {
707 AppendImportantMessage(_("They don't match. Password was not changed."), -1);
708 do_template("menu_change_pw");
712 if (IsEmptyStr(newpass1)) {
713 AppendImportantMessage(_("Blank passwords are not allowed."), -1);
714 do_template("menu_change_pw");
719 serv_printf("SETP %s", newpass1);
720 StrBuf_ServGetln(Line);
721 if (GetServerStatusMsg(Line, NULL, 1, 0) == 2) {
722 if (WC->wc_password == NULL)
723 WC->wc_password = NewStrBufPlain(
725 StrLength(Line) - 4);
727 FlushStrBuf(WC->wc_password);
728 StrBufAppendBufPlain(WC->wc_password,
730 StrLength(Line) - 4, 0);
735 do_template("menu_change_pw");
741 int ConditionalHaveAccessCreateRoom(StrBuf *Target, WCTemplputParams *TP)
747 StrBuf_ServGetln(Buf);
749 if (GetServerStatus(Buf, NULL) == 2) {
750 StrBufCutLeft(Buf, 4);
751 AppendImportantMessage(SKEY(Buf));
760 int ConditionalAide(StrBuf *Target, WCTemplputParams *TP)
763 return (WCC != NULL) ? ((WCC->logged_in == 0)||(WC->is_aide == 0)) : 0;
767 int ConditionalIsLoggedIn(StrBuf *Target, WCTemplputParams *TP)
770 return (WCC != NULL) ? (WCC->logged_in == 0) : 0;
776 * toggle the session over to a different language
778 void switch_language(void) {
779 set_selected_language(bstr("lang"));
784 void _display_reg(void) {
789 void Header_HandleAuth(StrBuf *Line, ParsedHttpHdrs *hdr)
791 if (hdr->HR.got_auth == NO_AUTH) /* don't override cookie auth... */
793 if (strncasecmp(ChrPtr(Line), "Basic", 5) == 0) {
794 StrBufCutLeft(Line, 6);
795 StrBufDecodeBase64(Line);
796 hdr->HR.plainauth = Line;
797 hdr->HR.got_auth = AUTH_BASIC;
800 syslog(LOG_WARNING, "Authentication scheme not supported! [%s]\n", ChrPtr(Line));
805 void CheckAuthBasic(ParsedHttpHdrs *hdr)
808 todo: enable this if we can have other sessions than authenticated ones.
809 if (hdr->DontNeedAuth)
812 StrBufAppendBufPlain(hdr->HR.plainauth, HKEY(":"), 0);
813 StrBufAppendBuf(hdr->HR.plainauth, hdr->HR.user_agent, 0);
817 void GetAuthBasic(ParsedHttpHdrs *hdr)
819 const char *Pos = NULL;
820 if (hdr->c_username == NULL)
821 hdr->c_username = NewStrBufPlain(HKEY(DEFAULT_HTTPAUTH_USER));
822 if (hdr->c_password == NULL)
823 hdr->c_password = NewStrBufPlain(HKEY(DEFAULT_HTTPAUTH_PASS));
824 StrBufExtract_NextToken(hdr->c_username, hdr->HR.plainauth, &Pos, ':');
825 StrBufExtract_NextToken(hdr->c_password, hdr->HR.plainauth, &Pos, ':');
829 void Header_HandleCookie(StrBuf *Line, ParsedHttpHdrs *hdr)
833 todo: enable this if we can have other sessions than authenticated ones.
834 if (hdr->DontNeedAuth)
837 pch = strstr(ChrPtr(Line), "webcit=");
842 hdr->HR.RawCookie = Line;
843 StrBufCutLeft(hdr->HR.RawCookie, (pch - ChrPtr(hdr->HR.RawCookie)) + 7);
844 StrBufDecodeHex(hdr->HR.RawCookie);
846 cookie_to_stuff(Line, &hdr->HR.desired_session,
852 hdr->HR.got_auth = AUTH_COOKIE;
858 (ParsedHttpHdrs *httpreq)
860 httpreq->c_username = NewStrBufPlain(HKEY(DEFAULT_HTTPAUTH_USER));
861 httpreq->c_password = NewStrBufPlain(HKEY(DEFAULT_HTTPAUTH_PASS));
862 httpreq->c_roomname = NewStrBuf();
863 httpreq->c_language = NewStrBuf();
868 HttpDetachModule_AUTH
869 (ParsedHttpHdrs *httpreq)
871 FLUSHStrBuf(httpreq->c_username);
872 FLUSHStrBuf(httpreq->c_password);
873 FLUSHStrBuf(httpreq->c_roomname);
874 FLUSHStrBuf(httpreq->c_language);
879 HttpDestroyModule_AUTH
880 (ParsedHttpHdrs *httpreq)
882 FreeStrBuf(&httpreq->c_username);
883 FreeStrBuf(&httpreq->c_password);
884 FreeStrBuf(&httpreq->c_roomname);
885 FreeStrBuf(&httpreq->c_language);
894 RegisterHeaderHandler(HKEY("COOKIE"), Header_HandleCookie);
895 RegisterHeaderHandler(HKEY("AUTHORIZATION"), Header_HandleAuth);
897 /* no url pattern at all? Show login. */
898 WebcitAddUrlHandler(HKEY(""), "", 0, do_welcome, ANONYMOUS|COOKIEUNNEEDED);
900 WebcitAddUrlHandler(HKEY("do_welcome"), "", 0, do_welcome, ANONYMOUS|COOKIEUNNEEDED);
901 WebcitAddUrlHandler(HKEY("openid_login"), "", 0, do_openid_login, ANONYMOUS);
902 WebcitAddUrlHandler(HKEY("finalize_openid_login"), "", 0, finalize_openid_login, ANONYMOUS);
903 WebcitAddUrlHandler(HKEY("openid_manual_create"), "", 0, openid_manual_create, ANONYMOUS);
904 WebcitAddUrlHandler(HKEY("validate"), "", 0, validate, 0);
905 WebcitAddUrlHandler(HKEY("do_welcome"), "", 0, do_welcome, 0);
906 WebcitAddUrlHandler(HKEY("display_reg"), "", 0, _display_reg, 0);
907 WebcitAddUrlHandler(HKEY("changepw"), "", 0, changepw, 0);
908 WebcitAddUrlHandler(HKEY("termquit"), "", 0, do_logout, 0);
909 WebcitAddUrlHandler(HKEY("do_logout"), "", 0, do_logout, ANONYMOUS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
910 WebcitAddUrlHandler(HKEY("monitor"), "", 0, monitor, ANONYMOUS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
911 WebcitAddUrlHandler(HKEY("ajax_login_username_password"), "", 0, ajax_login_username_password, AJAX|ANONYMOUS);
912 WebcitAddUrlHandler(HKEY("ajax_login_newuser"), "", 0, ajax_login_newuser, AJAX|ANONYMOUS);
913 WebcitAddUrlHandler(HKEY("switch_language"), "", 0, switch_language, ANONYMOUS);
914 RegisterConditional("COND:AIDE", 2, ConditionalAide, CTX_NONE);
915 RegisterConditional("COND:LOGGEDIN", 2, ConditionalIsLoggedIn, CTX_NONE);
916 RegisterConditional("COND:MAY_CREATE_ROOM", 2, ConditionalHaveAccessCreateRoom, CTX_NONE);
922 SessionDestroyModule_AUTH
925 FreeStrBuf(&sess->wc_username);
926 FreeStrBuf(&sess->wc_fullname);
927 FreeStrBuf(&sess->wc_password);
928 FreeStrBuf(&sess->httpauth_pass);
929 FreeStrBuf(&sess->cs_inet_email);