* move some more vars from the session context to strbuf (the use of StrBufAppendTemp...
[citadel.git] / webcit / context_loop.c
index 128487dc04663c1ffce5b34652bf773b96a68421..e0a816e1d351bb3b3e605fa09783807bf8d3b843 100644 (file)
 /* Only one thread may manipulate SessionList at a time... */
 pthread_mutex_t SessionListMutex;
 
-struct wcsession *SessionList = NULL; /**< our sessions ????*/
+wcsession *SessionList = NULL; /**< our sessions ????*/
 
 pthread_key_t MyConKey;         /**< TSD key for MySession() */
 
 
 
-void DestroySession(struct wcsession **sessions_to_kill)
+void DestroySession(wcsession **sessions_to_kill)
 {
        close((*sessions_to_kill)->serv_sock);
        close((*sessions_to_kill)->chat_sock);
+/*
 //             if ((*sessions_to_kill)->preferences != NULL) {
 //                     free((*sessions_to_kill)->preferences);
 //             }
+*/
        if ((*sessions_to_kill)->cache_fold != NULL) {
                free((*sessions_to_kill)->cache_fold);
        }
@@ -41,13 +43,19 @@ void DestroySession(struct wcsession **sessions_to_kill)
        FreeStrBuf(&((*sessions_to_kill)->WBuf));
        FreeStrBuf(&((*sessions_to_kill)->HBuf));
        FreeStrBuf(&((*sessions_to_kill)->CLineBuf));
+       FreeStrBuf(&((*sessions_to_kill)->wc_username));
+       FreeStrBuf(&((*sessions_to_kill)->wc_fullname));
+       FreeStrBuf(&((*sessions_to_kill)->wc_password));
+       FreeStrBuf(&((*sessions_to_kill)->wc_roomname));
+       FreeStrBuf(&((*sessions_to_kill)->httpauth_user));
+       FreeStrBuf(&((*sessions_to_kill)->httpauth_pass));
        free((*sessions_to_kill));
        (*sessions_to_kill) = NULL;
 }
 
 void shutdown_sessions(void)
 {
-       struct wcsession *sptr;
+       wcsession *sptr;
        
        for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
                        sptr->killthis = 1;
@@ -56,8 +64,8 @@ void shutdown_sessions(void)
 
 void do_housekeeping(void)
 {
-       struct wcsession *sptr, *ss;
-       struct wcsession *sessions_to_kill = NULL;
+       wcsession *sptr, *ss;
+       wcsession *sessions_to_kill = NULL;
        int num_sessions = 0;
        static int num_threads = MIN_WORKER_THREADS;
 
@@ -154,45 +162,6 @@ int GenerateSessionID(void)
        return ++seq;
 }
 
-
-/*
- * Collapse multiple cookies on one line
- */
-////int req_gets(int *sock, char *buf, char *hold, size_t hlen)
-////{
-////   int a, b;
-////
-////   if (IsEmptyStr(hold)) {
-////           strcpy(buf, "");
-////           a = client_getln(sock, buf, SIZ);
-////           if (a<1) return(-1);
-////   } else {
-////           safestrncpy(buf, hold, SIZ);
-////   }
-////   strcpy(hold, "");
-////
-////   if (!strncasecmp(buf, "Cookie: ", 8)) {
-////           int len;
-////           len = strlen(buf);
-////           for (a = 0; a < len; ++a)
-////                   if (buf[a] == ';') {
-////                           // we don't refresh len, because of we 
-////                           // only exit from here.
-////                           snprintf(hold, hlen, "Cookie: %s", &buf[a + 1]);
-////                           buf[a] = 0;
-////                           b = 8;
-////                           while (isspace(hold[b]))
-////                                   b++;
-////                           
-////                           memmove(&hold[8], &hold[b], len - b + 1);
-////                           return(0);
-////                   }
-////   }
-////
-////   return(0);
-////}
-
-
 /*
  * Collapse multiple cookies on one line
  */
@@ -242,28 +211,26 @@ int lingering_close(int fd)
 
 
 
-/**
- * \brief      sanity requests
- *             Check for bogus requests coming from brain-dead Windows boxes.
- *
- * \param      http_cmd        The HTTP request to check
+/*
+ * Look for commonly-found probes of malware such as worms, viruses, trojans, and Microsoft Office.
+ * Short-circuit these requests so we don't have to send them through the full processing loop.
  */
 int is_bogus(StrBuf *http_cmd) {
        const char *url;
        int i, max;
+       const char *bogus_prefixes[] = {
+               "/scripts/root.exe",    /* Worms and trojans and viruses, oh my! */
+               "/c/winnt",
+               "/MSADC/",
+               "/_vti",                /* Broken Microsoft DAV implementation */
+               "/MSOffice",            /* Stoopid MSOffice thinks everyone is IIS */
+               "/nonexistenshit"       /* Exploit found in the wild January 2009 */
+       };
 
        url = ChrPtr(http_cmd);
        if (IsEmptyStr(url)) return(1);
        ++url;
 
-       char *bogus_prefixes[] = {
-               "/scripts/root.exe",    /**< Worms and trojans and viruses, oh my! */
-               "/c/winnt",
-               "/MSADC/",
-               "/_vti",                /**< Broken Microsoft DAV implementation */
-               "/MSOffice"             /**< Stoopid MSOffice thinks everyone is IIS */
-       };
-
        max = sizeof(bogus_prefixes) / sizeof(char *);
 
        for (i=0; i<max; ++i) {
@@ -297,7 +264,7 @@ void context_loop(int *sock)
        int desired_session = 0;
        int got_cookie = 0;
        int gzip_ok = 0;
-       struct wcsession *TheSession, *sptr;
+       wcsession *TheSession, *sptr;
        char httpauth_string[1024];
        char httpauth_user[1024];
        char httpauth_pass[1024];
@@ -355,7 +322,7 @@ void context_loop(int *sock)
                }
 
                StrBufExtract_token(HeaderName, Line, 0, ':');
-       //// TODO: filter bad chars!
+               /*/// TODO: filter bad chars! */
 
                pchs = ChrPtr(Line);
                pch = pchs + StrLength(HeaderName) + 1;
@@ -370,7 +337,7 @@ void context_loop(int *sock)
        } while (LineLen > 0);
        FreeStrBuf(&HeaderName);
 
-////   dbg_PrintHash(HTTPHeaders, nix, NULL);
+/*///  dbg_PrintHash(HTTPHeaders, nix, NULL); */
 
 
        /**
@@ -392,7 +359,7 @@ authentication
        if (GetHash(HTTPHeaders, HKEY("COOKIE"), &vLine) && 
            (vLine != NULL)) {
                cookie_to_stuff(vLine, &desired_session,
-                               NULL, 0, NULL, 0, NULL, 0);
+                               NULL, NULL, NULL);
                got_cookie = 1;
        }
 
@@ -510,8 +477,8 @@ authentication
 
                        /** If HTTP-AUTH, look for a session with matching credentials */
                        if ( (!IsEmptyStr(httpauth_user))
-                          &&(!strcasecmp(sptr->httpauth_user, httpauth_user))
-                          &&(!strcasecmp(sptr->httpauth_pass, httpauth_pass)) ) {
+                            &&(!strcasecmp(ChrPtr(sptr->httpauth_user), httpauth_user))
+                            &&(!strcasecmp(ChrPtr(sptr->httpauth_pass), httpauth_pass)) ) {
                                TheSession = sptr;
                        }
 
@@ -529,9 +496,9 @@ authentication
         */
        if (TheSession == NULL) {
                lprintf(3, "Creating a new session\n");
-               TheSession = (struct wcsession *)
-                       malloc(sizeof(struct wcsession));
-               memset(TheSession, 0, sizeof(struct wcsession));
+               TheSession = (wcsession *)
+                       malloc(sizeof(wcsession));
+               memset(TheSession, 0, sizeof(wcsession));
                TheSession->serv_sock = (-1);
                TheSession->chat_sock = (-1);
        
@@ -548,8 +515,17 @@ authentication
                        TheSession->wc_session = desired_session;
                }
 
-               strcpy(TheSession->httpauth_user, httpauth_user);
-               strcpy(TheSession->httpauth_pass, httpauth_pass);
+               if (TheSession->httpauth_user != NULL){
+                       FlushStrBuf(TheSession->httpauth_user);
+                       StrBufAppendBufPlain(TheSession->httpauth_user, httpauth_user, -1, 0);
+               }
+               else TheSession->httpauth_user = NewStrBufPlain(httpauth_user, -1);
+               if (TheSession->httpauth_user != NULL){
+                       FlushStrBuf(TheSession->httpauth_pass);
+                       StrBufAppendBufPlain(TheSession->httpauth_pass, httpauth_user, -1, 0);
+               }
+               else TheSession->httpauth_pass = NewStrBufPlain(httpauth_user, -1);
+
                TheSession->hash_prefs = NewHash(1,NULL);       /* Get a hash table for the user preferences */
                pthread_mutex_init(&TheSession->SessionMutex, NULL);
                pthread_mutex_lock(&SessionListMutex);
@@ -613,16 +589,30 @@ authentication
        
 }
 
-void tmpl_nonce(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
+void tmplput_nonce(StrBuf *Target, WCTemplputParams *TP)
 {
-       struct wcsession *WCC = WC;
+       wcsession *WCC = WC;
        StrBufAppendPrintf(Target, "%ld",
                           (WCC != NULL)? WCC->nonce:0);                   
 }
 
+void tmplput_current_user(StrBuf *Target, WCTemplputParams *TP)
+{
+       StrBufAppendTemplate(Target, TP, WC->wc_fullname, 0);
+}
+
+void tmplput_current_room(StrBuf *Target, WCTemplputParams *TP)
+{
+       StrBufAppendTemplate(Target, TP, WC->wc_roomname, 0); 
+}
+
+
+
 void 
 InitModule_CONTEXT
 (void)
 {
-       RegisterNamespace("NONCE", 0, 0, tmpl_nonce, 0);
+       RegisterNamespace("CURRENT_USER", 0, 1, tmplput_current_user, CTX_NONE);
+       RegisterNamespace("CURRENT_ROOM", 0, 1, tmplput_current_room, CTX_NONE);
+       RegisterNamespace("NONCE", 0, 0, tmplput_nonce, 0);
 }