]> code.citadel.org Git - citadel.git/blobdiff - webcit/context_loop.c
* Brought over the newer string tokenizer from Citadel
[citadel.git] / webcit / context_loop.c
index 2cdd78088557d6310d3a699126548f46ebfab970..03ec66e1a00a563360aed972325801304d42c927 100644 (file)
@@ -2,9 +2,9 @@
  * context_loop.c
  *
  * This is the other half of the webserver.  It handles the task of hooking
- * up HTTP requests with the session they belong to, using HTTP cookies to
+ * up HTTP requests with the sessions they belong to, using HTTP cookies to
  * keep track of things.  If the HTTP request doesn't belong to any currently
- * active session, a new session is spawned.
+ * active session, a new session is started.
  *
  * $Id$
  */
@@ -155,9 +155,9 @@ int req_gets(int sock, char *buf, char *hold)
  * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
  */
 
-static int lingering_close(int fd)
+int lingering_close(int fd)
 {
-       char buf[256];
+       char buf[SIZ];
        int i;
        fd_set set;
        struct timeval tv, start;
@@ -189,6 +189,22 @@ static int lingering_close(int fd)
 
 
 
+/*
+ * Check for bogus requests coming from (for example) brain-dead
+ * Windoze boxes that are infected with the latest worm-of-the-week.
+ * If we detect one of these, bail out without bothering our Citadel
+ * server.
+ */
+int is_bogus(char *http_cmd) {
+
+       if (!strncasecmp(http_cmd, "GET /scripts/root.exe", 21)) return(1);
+       if (!strncasecmp(http_cmd, "GET /c/winnt", 12)) return(2);
+       if (!strncasecmp(http_cmd, "GET /MSADC/", 11)) return(3);
+
+       return(0);      /* probably ok */
+}
+
+
 
 /*
  * This loop gets called once for every HTTP connection made to WebCit.  At
@@ -206,7 +222,7 @@ void context_loop(int sock)
        struct httprequest *req = NULL;
        struct httprequest *last = NULL;
        struct httprequest *hptr;
-       char buf[256], hold[256];
+       char buf[SIZ], hold[SIZ];
        int desired_session = 0;
        int got_cookie = 0;
        struct wcsession *TheSession, *sptr;
@@ -236,14 +252,17 @@ void context_loop(int sock)
 
        } while (strlen(buf) > 0);
 
+       strcpy(buf, req->line);
+       fprintf(stderr, "%s\n", buf);
+
+       /* Check for bogus requests */
+       if (is_bogus(buf)) goto bail;
 
        /*
         * If requesting a non-root page, there should already be a cookie
         * set.  If there isn't, the client browser has cookies turned off
         * (or doesn't support them) and we have to barf & bail.
         */
-       strcpy(buf, req->line);
-       fprintf(stderr, "%s\n", buf);
        if (!strncasecmp(buf, "GET ", 4)) strcpy(buf, &buf[4]);
        else if (!strncasecmp(buf, "HEAD ", 5)) strcpy(buf, &buf[5]);
        if (buf[1]==' ') buf[1]=0;
@@ -253,12 +272,14 @@ void context_loop(int sock)
         * robots.txt file...
         */
        if (!strncasecmp(buf, "/robots.txt", 11)) {
-               strcpy(req->line, "GET /static/robots.txt HTTP/1.0");
+               strcpy(req->line, "GET /static/robots.txt"
+                               "?force_close_session=yes HTTP/1.0");
        }
 
        /* Do the non-root-cookie check now. */
        else if ( (strcmp(buf, "/")) && (got_cookie == 0)) {
-               strcpy(req->line, "GET /static/nocookies.html HTTP/1.0");
+               strcpy(req->line, "GET /static/nocookies.html"
+                               "?force_close_session=yes HTTP/1.0");
        }
 
 
@@ -302,7 +323,6 @@ void context_loop(int sock)
         */
 
 
-
        /*
         * Bind to the session and perform the transaction
         */
@@ -314,15 +334,9 @@ void context_loop(int sock)
        pthread_mutex_unlock(&TheSession->SessionMutex);        /* unbind */
 
        /* Free the request buffer */
-       while (req != NULL) {
+bail:  while (req != NULL) {
                hptr = req->next;
                free(req);
                req = hptr;
        }
-
-       /*
-        * Now our HTTP connection is done.  Close the socket and exit this
-        * function, so the worker thread can handle a new HTTP connection.
-        */
-       lingering_close(sock);
 }