* move stuff from Hdr into substruct that may be easily wiped by memset 0'ing them
[citadel.git] / webcit / auth.c
index 863488b8c5dd1223347ecf4bb982d8776677460f..a751b89d8137e778860537a9422a3020d01e19c3 100644 (file)
@@ -8,6 +8,7 @@
 #include "webserver.h"
 #include <ctype.h>
 
+extern uint32_t hashlittle( const void *key, size_t length, uint32_t initval);
 
 void display_reg(int during_login);
 
@@ -27,43 +28,6 @@ void initialize_axdefs(void) {
        axdefs[6] = _("Aide");          /* chief */
 }
 
-int ReEstablish_Session(void)
-{
-       StrBuf *Buf = NewStrBuf();
-       wcsession *WCC = WC;
-
-       serv_printf("USER %s", ChrPtr(WCC->Hdr->c_username));
-       StrBuf_ServGetln(Buf);
-       if (GetServerStatus(Buf, NULL) == 3) {
-               serv_printf("PASS %s", ChrPtr(WCC->Hdr->c_password));
-               StrBuf_ServGetln(Buf);
-               if (GetServerStatus(Buf, NULL) == 2) {
-                       become_logged_in(WCC->Hdr->c_username, 
-                                        WCC->Hdr->c_password, Buf);
-                       get_preference("default_header_charset", &WCC->DefaultCharset);
-               }
-       }
-       /*
-        * If we don't have a current room, but a cookie specifying the
-        * current room is supplied, make an effort to go there.
-        */
-       if ((StrLength(WCC->wc_roomname) == 0) && (StrLength(WCC->Hdr->c_roomname) > 0)) {
-               serv_printf("GOTO %s", 
-                           ChrPtr(WCC->Hdr->c_roomname));
-               StrBuf_ServGetln(Buf);
-               if (GetServerStatus(Buf, NULL) == 2) {
-                       if (WCC->wc_roomname == NULL) {
-                               WCC->wc_roomname = NewStrBufDup(WCC->Hdr->c_roomname);
-                       }
-                       else {
-                               FlushStrBuf(WCC->wc_roomname);
-                               StrBufAppendBuf(WCC->wc_roomname, WCC->Hdr->c_roomname, 0);
-                       }
-               }
-       }
-       FreeStrBuf(&Buf);
-       return 0;
-}
 
 
 /* 
@@ -152,7 +116,6 @@ void become_logged_in(const StrBuf *user, const StrBuf *pass, StrBuf *serv_respo
        wcsession *WCC = WC;
        StrBuf *Buf;
        StrBuf *FloorDiv;
-       StrBuf *Language = NULL;
 
        WCC->logged_in = 1;
 
@@ -196,15 +159,6 @@ void become_logged_in(const StrBuf *user, const StrBuf *pass, StrBuf *serv_respo
                        WCC->cs_inet_email  = NewStrBuf();
                StrBufExtract_NextToken(WCC->cs_inet_email, Buf, &pch, '|');
        }
-       if (havebstr("language"))
-               set_preference("language", NewStrBufDup(SBSTR("language")), 1);
-       else {
-               get_preference("language", &Language);
-               if (Language != NULL) {
-                       set_selected_language(ChrPtr(Language));
-                       go_selected_language();         /* set locale */
-               }
-       }
        get_preference("floordiv_expanded", &FloorDiv);
        WCC->floordiv_expanded = FloorDiv;
        FreeStrBuf(&Buf);
@@ -219,6 +173,8 @@ void do_login(void)
        wcsession *WCC = WC;
        StrBuf *Buf;
 
+       lprintf(9, "SELECTED LANGUAGE: '%s'\n", bstr("language"));
+
        if (havebstr("language")) {
                set_selected_language(bstr("language"));
                go_selected_language();
@@ -366,8 +322,8 @@ void do_openid_login(void)
                snprintf(buf, sizeof buf,
                        "OIDS %s|%s://%s/finalize_openid_login|%s://%s",
                        bstr("openid_url"),
-                        (is_https ? "https" : "http"), ChrPtr(WCC->Hdr->http_host),
-                        (is_https ? "https" : "http"), ChrPtr(WCC->Hdr->http_host)
+                        (is_https ? "https" : "http"), ChrPtr(WCC->Hdr->HR.http_host),
+                        (is_https ? "https" : "http"), ChrPtr(WCC->Hdr->HR.http_host)
                );
 
                serv_puts(buf);
@@ -950,36 +906,86 @@ void _display_reg(void) {display_reg(0);}
 
 void Header_HandleAuth(StrBuf *Line, ParsedHttpHdrs *hdr)
 {
-       const char *Pos = NULL;
-       if (strncasecmp(ChrPtr(Line), "Basic", 5) == 0) {
-               StrBufCutLeft(Line, 6);
-               StrBufDecodeBase64(Line);
-               StrBufExtract_NextToken(hdr->c_username, Line, &Pos, ':');
-               StrBufExtract_NextToken(hdr->c_password, Line, &Pos, ':');
-               hdr->got_auth = AUTH_BASIC;
+       if (hdr->HR.got_auth == NO_AUTH) /* don't override cookie auth... */
+       {
+               if (strncasecmp(ChrPtr(Line), "Basic", 5) == 0) {
+                       StrBufCutLeft(Line, 6);
+                       StrBufDecodeBase64(Line);
+                       hdr->HR.plainauth = Line;
+                       hdr->HR.got_auth = AUTH_BASIC;
+               }
+               else 
+                       lprintf(1, "Authentication scheme not supported! [%s]\n", ChrPtr(Line));
        }
-       else 
-               lprintf(1, "Authentication scheme not supported! [%s]\n", ChrPtr(Line));
 }
 
-void Header_HandleCookie(StrBuf *Line, ParsedHttpHdrs *hdr)
+void CheckAuthBasic(ParsedHttpHdrs *hdr)
 {
-       hdr->RawCookie = Line;
+/*
+  todo: enable this if we can have other sessions than authenticated ones.
        if (hdr->DontNeedAuth)
                return;
+*/
+       StrBufAppendBufPlain(hdr->HR.plainauth, HKEY(":"), 0);
+       StrBufAppendBuf(hdr->HR.plainauth, hdr->HR.user_agent, 0);
+       hdr->HR.SessionKey = hashlittle(SKEY(hdr->HR.plainauth), 89479832);
+       
+}
+
+void GetAuthBasic(ParsedHttpHdrs *hdr)
+{
+       const char *Pos = NULL;
+       if (hdr->c_username == NULL)
+               hdr->c_username = NewStrBufPlain(HKEY(DEFAULT_HTTPAUTH_USER));
+       if (hdr->c_password == NULL)
+               hdr->c_password = NewStrBufPlain(HKEY(DEFAULT_HTTPAUTH_PASS));
+       StrBufExtract_NextToken(hdr->c_username, hdr->HR.plainauth, &Pos, ':');
+       StrBufExtract_NextToken(hdr->c_password, hdr->HR.plainauth, &Pos, ':');
+}
+
+void Header_HandleCookie(StrBuf *Line, ParsedHttpHdrs *hdr)
+{
+       const char *pch;
 /*
-       safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
-       c_httpauth_user = NewStrBufPlain(HKEY(DEFAULT_HTTPAUTH_USER));
-       c_httpauth_pass = NewStrBufPlain(HKEY(DEFAULT_HTTPAUTH_PASS));
+  todo: enable this if we can have other sessions than authenticated ones.
+       if (hdr->DontNeedAuth)
+               return;
 */
-       cookie_to_stuff(Line, &hdr->desired_session,
+       pch = strstr(ChrPtr(Line), "webcit=");
+       if (pch == NULL) {
+               return;
+       }
+
+       hdr->HR.RawCookie = Line;
+       StrBufCutLeft(hdr->HR.RawCookie, (pch - ChrPtr(hdr->HR.RawCookie)) + 7);
+       StrBufDecodeHex(hdr->HR.RawCookie);
+
+       if (hdr->c_username == NULL)
+               hdr->c_username = NewStrBufPlain(HKEY(DEFAULT_HTTPAUTH_USER));
+       if (hdr->c_password == NULL)
+               hdr->c_password = NewStrBufPlain(HKEY(DEFAULT_HTTPAUTH_PASS));
+       if (hdr->c_roomname == NULL)
+               hdr->c_roomname = NewStrBuf();
+       if (hdr->c_language == NULL)
+               hdr->c_language = NewStrBuf();
+       cookie_to_stuff(Line, &hdr->HR.desired_session,
                        hdr->c_username,
                        hdr->c_password,
-                       hdr->c_roomname);
-       hdr->got_auth = AUTH_COOKIE;
+                       hdr->c_roomname,
+                       hdr->c_language
+       );
+       hdr->HR.got_auth = AUTH_COOKIE;
 }
 
-
+void 
+HttpDestroyModule_AUTH
+(ParsedHttpHdrs *httpreq)
+{
+       FreeStrBuf(&httpreq->c_username);
+       FreeStrBuf(&httpreq->c_password);
+       FreeStrBuf(&httpreq->c_roomname);
+       FreeStrBuf(&httpreq->c_language);
+}
 
 void 
 InitModule_AUTH
@@ -1019,7 +1025,6 @@ SessionDestroyModule_AUTH
        FreeStrBuf(&sess->wc_fullname);
        FreeStrBuf(&sess->wc_password);
        FreeStrBuf(&sess->wc_roomname);
-       FreeStrBuf(&sess->httpauth_user);
        FreeStrBuf(&sess->httpauth_pass);
        FreeStrBuf(&sess->cs_inet_email);
 }