/*
- * 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.
+ *
+ * Copyright (c) 1996-2011 by the citadel.org team
+ *
+ * This program is open source software. You can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
*
- * $Id$
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
-#include <ctype.h>
-#include <stdlib.h>
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-#include <stdio.h>
-#ifdef HAVE_FCNTL_H
-#include <fcntl.h>
-#endif
-#include <signal.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <sys/socket.h>
-#ifdef HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
-#ifdef HAVE_LIMITS_H
-#include <limits.h>
-#endif
-#include <netinet/in.h>
-#include <netdb.h>
-#include <string.h>
-#include <errno.h>
-#include <stdarg.h>
-#include <pthread.h>
-#include <signal.h>
#include "webcit.h"
#include "webserver.h"
+#include "modules_init.h"
-/*
- * We keep one of these around for each active session
- */
-struct wc_session {
- struct wc_session *next; /* Next session in list */
- int session_id; /* Session ID */
- pid_t webcit_pid; /* PID of the webcit process */
- int inpipe[2]; /* Data from webserver to session */
- int outpipe[2]; /* Data from session to webserver */
- pthread_mutex_t critter; /* Critical section uses pipes */
- time_t lastreq; /* Timestamp of most recent http */
-};
+/* Only one thread may manipulate SessionList at a time... */
+pthread_mutex_t SessionListMutex;
-struct wc_session *SessionList = NULL;
-extern const char *defaulthost;
-extern const char *defaultport;
+wcsession *SessionList = NULL; /* Linked list of all webcit sessions */
-/* Only one thread may manipulate SessionList at a time... */
-pthread_mutex_t MasterCritter;
+pthread_key_t MyConKey; /* TSD key for MySession() */
+HashList *HttpReqTypes = NULL;
+HashList *HttpHeaderHandler = NULL;
+extern HashList *HandlerHash;
+/* the following two values start at 1 because the initial parent thread counts as one. */
+int num_threads_existing = 1; /* Number of worker threads which exist. */
+int num_threads_executing = 1; /* Number of worker threads currently executing. */
-/*
- * Grab a lock on the session, so other threads don't try to access
- * the pipes at the same time.
- */
-static void lock_session(struct wc_session *session)
+extern void session_loop(void);
+void spawn_another_worker_thread(void);
+
+
+void DestroyHttpHeaderHandler(void *V)
{
- printf("Locking session %d...\n", session->session_id);
- pthread_mutex_lock(&session->critter);
- printf(" ...got lock\n");
+ OneHttpHeader *pHdr;
+ pHdr = (OneHttpHeader*) V;
+ FreeStrBuf(&pHdr->Val);
+ free(pHdr);
}
-/*
- * Let go of the lock.
- */
-static void unlock_session(struct wc_session *session)
+void shutdown_sessions(void)
{
- printf("Unlocking.\n");
- pthread_mutex_unlock(&session->critter);
+ wcsession *sptr;
+
+ for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
+ sptr->killthis = 1;
+ }
}
-/*
- * Remove a session context from the list.
- * Set do_lock to 1, to lock the list while manipulating it. The ONLY
- * situation in which this should _not_ be done is when it's already locked
- * by the caller.
- */
-void remove_session(struct wc_session *TheSession, int do_lock)
+void do_housekeeping(void)
{
- struct wc_session *sptr;
+ wcsession *sptr, *ss;
+ wcsession *sessions_to_kill = NULL;
- printf("Removing session.\n");
+ /*
+ * Lock the session list, moving any candidates for euthanasia into
+ * a separate list.
+ */
+ CtdlLogResult(pthread_mutex_lock(&SessionListMutex));
+ for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
+
+ /* Kill idle sessions */
+ if ((time(NULL) - (sptr->lastreq)) > (time_t) WEBCIT_TIMEOUT) {
+ syslog(3, "Timeout session %d", sptr->wc_session);
+ sptr->killthis = 1;
+ }
- /* Lock the list while manipulating it */
- if (do_lock)
- pthread_mutex_lock(&MasterCritter);
+ /* Remove sessions flagged for kill */
+ if (sptr->killthis) {
- if (SessionList == TheSession) {
- SessionList = SessionList->next;
- } else {
- for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
- if (sptr->next == TheSession) {
- sptr->next = TheSession->next;
+ /* remove session from linked list */
+ if (sptr == SessionList) {
+ SessionList = SessionList->next;
+ }
+ else for (ss=SessionList;ss!=NULL;ss=ss->next) {
+ if (ss->next == sptr) {
+ ss->next = ss->next->next;
+ }
}
+
+ sptr->next = sessions_to_kill;
+ sessions_to_kill = sptr;
}
}
+ CtdlLogResult(pthread_mutex_unlock(&SessionListMutex));
- if (do_lock)
- pthread_mutex_unlock(&MasterCritter);
-
- /* Now finish destroying the session */
- close(TheSession->inpipe[1]);
- close(TheSession->outpipe[0]);
- if (do_lock)
- unlock_session(TheSession);
- free(TheSession);
+ /*
+ * Now free up and destroy the culled sessions.
+ */
+ while (sessions_to_kill != NULL) {
+ syslog(3, "Destroying session %d", sessions_to_kill->wc_session);
+ sptr = sessions_to_kill->next;
+ session_destroy_modules(&sessions_to_kill);
+ sessions_to_kill = sptr;
+ }
+}
+/*
+ * Check the size of our thread pool. If all threads are executing, spawn another.
+ */
+void check_thread_pool_size(void)
+{
+ if (time_to_die) return; /* don't expand the thread pool during shutdown */
+
+ begin_critical_section(S_SPAWNER); /* only one of these should run at a time */
+ if (
+ (num_threads_executing >= num_threads_existing)
+ && (num_threads_existing < MAX_WORKER_THREADS)
+ ) {
+ syslog(3, "%d of %d threads are executing. Adding another worker thread.",
+ num_threads_executing,
+ num_threads_existing
+ );
+ spawn_another_worker_thread();
+ }
+ end_critical_section(S_SPAWNER);
}
+/*
+ * Wake up occasionally and clean house
+ */
+void housekeeping_loop(void)
+{
+ while (1) {
+ sleeeeeeeeeep(HOUSEKEEPING);
+ do_housekeeping();
+ }
+}
-void do_housekeeping(void)
+/*
+ * Create a Session id
+ * Generate a unique WebCit session ID (which is not the same thing as the
+ * Citadel session ID).
+ */
+int GenerateSessionID(void)
{
- struct wc_session *sptr;
+ static int seq = (-1);
- pthread_mutex_lock(&MasterCritter);
+ if (seq < 0) {
+ seq = (int) time(NULL);
+ }
+
+ return ++seq;
+}
- /* Kill idle sessions */
- for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
- if ((time(NULL) - (sptr->lastreq)) > (time_t) WEBCIT_TIMEOUT) {
- kill(sptr->webcit_pid, 15);
- }
+wcsession *FindSession(wcsession **wclist, ParsedHttpHdrs *Hdr, pthread_mutex_t *ListMutex)
+{
+ wcsession *sptr = NULL;
+ wcsession *TheSession = NULL;
+
+ if (Hdr->HR.got_auth == AUTH_BASIC) {
+ GetAuthBasic(Hdr);
}
- /* Remove dead sessions */
- for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
- if (kill(sptr->webcit_pid, 0)) {
- remove_session(sptr, 0);
+ CtdlLogResult(pthread_mutex_lock(ListMutex));
+ for (sptr = *wclist; ((sptr != NULL) && (TheSession == NULL)); sptr = sptr->next) {
+
+ /* If HTTP-AUTH, look for a session with matching credentials */
+ switch (Hdr->HR.got_auth)
+ {
+ case AUTH_BASIC:
+ if ( (!strcasecmp(ChrPtr(Hdr->c_username), ChrPtr(sptr->wc_username)))
+ && (!strcasecmp(ChrPtr(Hdr->c_password), ChrPtr(sptr->wc_password)))
+ && (sptr->killthis == 0)
+ ) {
+ syslog(LOG_DEBUG, "\033[32m-- matched a session with the same http-auth\033[0m");
+ TheSession = sptr;
+ }
+ break;
+ case AUTH_COOKIE:
+ /* If cookie-session, look for a session with matching session ID */
+ if ( (Hdr->HR.desired_session != 0)
+ && (sptr->wc_session == Hdr->HR.desired_session)
+ ) {
+ syslog(LOG_DEBUG, "\033[32m-- matched a session with the same cookie\033[0m");
+ TheSession = sptr;
+ }
+ break;
+ case NO_AUTH:
+ /* Any unbound session is a candidate */
+ if ( (sptr->wc_session == 0) && (sptr->inuse == 0) ) {
+ syslog(LOG_DEBUG, "\033[32m-- reusing an unbound session\033[0m");
+ TheSession = sptr;
+ }
+ break;
}
}
-
- pthread_mutex_unlock(&MasterCritter);
+ CtdlLogResult(pthread_mutex_unlock(ListMutex));
+ if (TheSession == NULL) {
+ syslog(LOG_DEBUG, "\033[32m-- no existing session was matched\033[0m");
+ }
+ return TheSession;
}
-
-/*
- * Wake up occasionally and clean house
- */
-void housekeeping_loop(void)
+wcsession *CreateSession(int Lockable, int Static, wcsession **wclist, ParsedHttpHdrs *Hdr, pthread_mutex_t *ListMutex)
{
- while (1) {
- sleep(HOUSEKEEPING);
- do_housekeeping();
+ wcsession *TheSession;
+ TheSession = (wcsession *) malloc(sizeof(wcsession));
+ memset(TheSession, 0, sizeof(wcsession));
+ TheSession->Hdr = Hdr;
+ TheSession->serv_sock = (-1);
+
+ pthread_setspecific(MyConKey, (void *)TheSession);
+
+ /* If we're recreating a session that expired, it's best to give it the same
+ * session number that it had before. The client browser ought to pick up
+ * the new session number and start using it, but in some rare situations it
+ * doesn't, and that's a Bad Thing because it causes lots of spurious sessions
+ * to get created.
+ */
+ if (Hdr->HR.desired_session == 0) {
+ TheSession->wc_session = GenerateSessionID();
+ syslog(3, "Created new session %d", TheSession->wc_session);
}
-}
+ else {
+ TheSession->wc_session = Hdr->HR.desired_session;
+ syslog(3, "Re-created session %d", TheSession->wc_session);
+ }
+ Hdr->HR.Static = Static;
+ session_new_modules(TheSession);
+ if (Lockable) {
+ pthread_mutex_init(&TheSession->SessionMutex, NULL);
+ if (ListMutex != NULL)
+ CtdlLogResult(pthread_mutex_lock(ListMutex));
+ if (wclist != NULL) {
+ TheSession->nonce = rand();
+ TheSession->next = *wclist;
+ *wclist = TheSession;
+ }
+ if (ListMutex != NULL)
+ CtdlLogResult(pthread_mutex_unlock(ListMutex));
+ }
+ return TheSession;
+}
-int GenerateSessionID(void)
+/* If it's a "force 404" situation then display the error and bail. */
+void do_404(void)
{
- return getpid();
+ hprintf("HTTP/1.1 404 Not found\r\n");
+ hprintf("Content-Type: text/plain\r\n");
+ wc_printf("Not found\r\n");
+ end_burst();
}
-
-void gets0(int fd, char buf[])
+int ReadHttpSubject(ParsedHttpHdrs *Hdr, StrBuf *Line, StrBuf *Buf)
{
+ const char *Args;
+ void *vLine, *vHandler;
+ const char *Pos = NULL;
+
+
+ Hdr->HR.ReqLine = Line;
+ /* The requesttype... GET, POST... */
+ StrBufExtract_token(Buf, Hdr->HR.ReqLine, 0, ' ');
+ if (GetHash(HttpReqTypes, SKEY(Buf), &vLine) &&
+ (vLine != NULL))
+ {
+ Hdr->HR.eReqType = *(long*)vLine;
+ }
+ else {
+ Hdr->HR.eReqType = eGET;
+ return 1;
+ }
+ StrBufCutLeft(Hdr->HR.ReqLine, StrLength(Buf) + 1);
+
+ /* the HTTP Version... */
+ StrBufExtract_token(Buf, Hdr->HR.ReqLine, 1, ' ');
+ StrBufCutRight(Hdr->HR.ReqLine, StrLength(Buf) + 1);
+
+ if (StrLength(Buf) == 0) {
+ Hdr->HR.eReqType = eGET;
+ return 1;
+ }
- buf[0] = 0;
+ StrBufAppendBuf(Hdr->this_page, Hdr->HR.ReqLine, 0);
+
+ /* chop Filename / query arguments */
+ Args = strchr(ChrPtr(Hdr->HR.ReqLine), '?');
+ if (Args == NULL) /* whe're not that picky about params... TODO: this will spoil '&' in filenames.*/
+ Args = strchr(ChrPtr(Hdr->HR.ReqLine), '&');
+ if (Args != NULL) {
+ Args ++; /* skip the ? */
+ StrBufPlain(Hdr->PlainArgs,
+ Args,
+ StrLength(Hdr->HR.ReqLine) -
+ (Args - ChrPtr(Hdr->HR.ReqLine)));
+ StrBufCutAt(Hdr->HR.ReqLine, 0, Args - 1);
+ } /* don't parse them yet, maybe we don't even care... */
+
+ /* now lookup what we are going to do with this... */
+ /* skip first slash */
+ StrBufExtract_NextToken(Buf, Hdr->HR.ReqLine, &Pos, '/');
do {
- buf[strlen(buf) + 1] = 0;
- read(fd, &buf[strlen(buf)], 1);
- } while (buf[strlen(buf) - 1] >= 32);
- buf[strlen(buf) - 1] = 0;
+ StrBufExtract_NextToken(Buf, Hdr->HR.ReqLine, &Pos, '/');
+
+ GetHash(HandlerHash, SKEY(Buf), &vHandler),
+ Hdr->HR.Handler = (WebcitHandler*) vHandler;
+ if (Hdr->HR.Handler == NULL)
+ break;
+ /*
+ * If the request is prefixed by "/webcit" then chop that off. This
+ * allows a front end web server to forward all /webcit requests to us
+ * while still using the same web server port for other things.
+ */
+ if ((Hdr->HR.Handler->Flags & URLNAMESPACE) != 0)
+ continue;
+ break;
+ } while (1);
+ /* remove the handlername from the URL */
+ if ((Pos != NULL) && (Pos != StrBufNOTNULL)){
+ StrBufCutLeft(Hdr->HR.ReqLine,
+ Pos - ChrPtr(Hdr->HR.ReqLine));
+ }
+
+ if (Hdr->HR.Handler != NULL) {
+ if ((Hdr->HR.Handler->Flags & BOGUS) != 0)
+ return 1;
+ Hdr->HR.DontNeedAuth = (
+ ((Hdr->HR.Handler->Flags & ISSTATIC) != 0) ||
+ ((Hdr->HR.Handler->Flags & ANONYMOUS) != 0)
+ );
+ }
+ else {
+ Hdr->HR.DontNeedAuth = 1; /* Flat request? show him the login screen... */
+ }
+
+ return 0;
}
-/*
- * Collapse multiple cookies on one line
- */
-void req_gets(int sock, char *buf, char *hold)
+int AnalyseHeaders(ParsedHttpHdrs *Hdr)
{
- int a;
-
- if (strlen(hold) == 0) {
- client_gets(sock, buf);
- } else {
- strcpy(buf, hold);
- }
- strcpy(hold, "");
-
- if (!strncasecmp(buf, "Cookie: ", 8)) {
- for (a = 0; a < strlen(buf); ++a)
- if (buf[a] == ';') {
- sprintf(hold, "Cookie: %s", &buf[a + 1]);
- buf[a] = 0;
- while (isspace(hold[8]))
- strcpy(&hold[8], &hold[9]);
- return;
- }
+ OneHttpHeader *pHdr;
+ void *vHdr;
+ long HKLen;
+ const char *HashKey;
+ HashPos *at = GetNewHashPos(Hdr->HTTPHeaders, 0);
+
+ while (GetNextHashPos(Hdr->HTTPHeaders, at, &HKLen, &HashKey, &vHdr) &&
+ (vHdr != NULL)) {
+ pHdr = (OneHttpHeader *)vHdr;
+ if (pHdr->HaveEvaluator)
+ pHdr->H(pHdr->Val, Hdr);
+
}
+ DeleteHashPos(&at);
+ return 0;
}
+/*const char *nix(void *vptr) {return ChrPtr( (StrBuf*)vptr);}*/
+
/*
- * lingering_close() a`la Apache. see
- * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
+ * Read in the request
*/
-
-static int lingering_close(int fd)
+int ReadHTTPRequest (ParsedHttpHdrs *Hdr)
{
- char buf[256];
- int i;
- fd_set set;
- struct timeval tv, start;
-
- gettimeofday(&start, NULL);
- shutdown(fd, 1);
+ const char *pch, *pchs, *pche;
+ OneHttpHeader *pHdr;
+ StrBuf *Line, *LastLine, *HeaderName;
+ int nLine = 0;
+ void *vF;
+ int isbogus = 0;
+
+ HeaderName = NewStrBuf();
+ LastLine = NULL;
do {
- do {
- gettimeofday(&tv, NULL);
- tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
- tv.tv_usec = start.tv_usec - tv.tv_usec;
- if (tv.tv_usec < 0) {
- tv.tv_sec--;
- tv.tv_usec += 1000000;
- }
- FD_ZERO(&set);
- FD_SET(fd, &set);
- i = select(fd + 1, &set, NULL, NULL, &tv);
- } while (i == -1 && errno == EINTR);
+ nLine ++;
+ Line = NewStrBufPlain(NULL, SIZ / 4);
- if (i <= 0)
- break;
+ if (ClientGetLine(Hdr, Line) < 0) return 1;
- i = read(fd, buf, sizeof buf);
- } while (i != 0 && (i != -1 || errno == EINTR));
+ if (StrLength(Line) == 0) {
+ FreeStrBuf(&Line);
+ continue;
+ }
+ if (nLine == 1) {
+ Hdr->HTTPHeaders = NewHash(1, NULL);
+ pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
+ memset(pHdr, 0, sizeof(OneHttpHeader));
+ pHdr->Val = Line;
+ Put(Hdr->HTTPHeaders, HKEY("GET /"), pHdr, DestroyHttpHeaderHandler);
+ syslog(9, "%s", ChrPtr(Line));
+ isbogus = ReadHttpSubject(Hdr, Line, HeaderName);
+ if (isbogus) break;
+ continue;
+ }
+
+ /* Do we need to Unfold? */
+ if ((LastLine != NULL) &&
+ (isspace(*ChrPtr(Line)))) {
+ pch = pchs = ChrPtr(Line);
+ pche = pchs + StrLength(Line);
+ while (isspace(*pch) && (pch < pche))
+ pch ++;
+ StrBufCutLeft(Line, pch - pchs);
+ StrBufAppendBuf(LastLine, Line, 0);
+
+ FreeStrBuf(&Line);
+ continue;
+ }
+
+ StrBufSanitizeAscii(Line, '§');
+ StrBufExtract_token(HeaderName, Line, 0, ':');
+
+ pchs = ChrPtr(Line);
+ pche = pchs + StrLength(Line);
+ pch = pchs + StrLength(HeaderName) + 1;
+ pche = pchs + StrLength(Line);
+ while ((pch < pche) && isspace(*pch))
+ pch ++;
+ StrBufCutLeft(Line, pch - pchs);
+
+ StrBufUpCase(HeaderName);
+
+ pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
+ memset(pHdr, 0, sizeof(OneHttpHeader));
+ pHdr->Val = Line;
+
+ if (GetHash(HttpHeaderHandler, SKEY(HeaderName), &vF) &&
+ (vF != NULL))
+ {
+ OneHttpHeader *FHdr = (OneHttpHeader*) vF;
+ pHdr->H = FHdr->H;
+ pHdr->HaveEvaluator = 1;
+ }
+ Put(Hdr->HTTPHeaders, SKEY(HeaderName), pHdr, DestroyHttpHeaderHandler);
+ LastLine = Line;
+ } while (Line != NULL);
- return close(fd);
+ FreeStrBuf(&HeaderName);
+
+ return isbogus;
+}
+
+void OverrideRequest(ParsedHttpHdrs *Hdr, const char *Line, long len)
+{
+ StrBuf *Buf = NewStrBuf();
+
+ if (Hdr->HR.ReqLine != NULL) {
+ FlushStrBuf(Hdr->HR.ReqLine);
+ StrBufPlain(Hdr->HR.ReqLine, Line, len);
+ }
+ else {
+ Hdr->HR.ReqLine = NewStrBufPlain(Line, len);
+ }
+ ReadHttpSubject(Hdr, Hdr->HR.ReqLine, Buf);
+
+ FreeStrBuf(&Buf);
}
/*
- * This loop gets called once for every HTTP connection made to WebCit.
+ * handle one request
+ *
+ * This loop gets called once for every HTTP connection made to WebCit. At
+ * this entry point we have an HTTP socket with a browser allegedly on the
+ * other end, but we have not yet bound to a WebCit session.
+ *
+ * The job of this function is to locate the correct session and bind to it,
+ * or create a session if necessary and bind to it, then run the WebCit
+ * transaction loop. Afterwards, we unbind from the session. When this
+ * function returns, the worker thread is then free to handle another
+ * transaction.
*/
-void *context_loop(int sock)
+void context_loop(ParsedHttpHdrs *Hdr)
{
- char (*req)[256];
- char buf[256], hold[256];
- char browser_host[256];
- char browser[256];
- int num_lines = 0;
- int a;
- int f;
- int desired_session = 0;
- int got_cookie = 0;
- char str_session[256];
- struct wc_session *sptr;
- struct wc_session *TheSession;
- int ContentLength;
- int CloseSession = 0;
-
- if ((req = malloc((long) sizeof(char[256][256]))) == NULL) {
- sprintf(buf, "Can't malloc buffers; dropping connection.\n");
- fprintf(stderr, "%s", buf);
- write(sock, buf, strlen(buf));
- close(sock);
- pthread_exit(NULL);
- }
- bzero(req, sizeof(char[256][256])); /* clear it out */
- strcpy(browser, "unknown");
-
- printf("Reading request from socket %d\n", sock);
+ int isbogus = 0;
+ wcsession *TheSession;
+ struct timeval tx_start;
+ struct timeval tx_finish;
+ int session_may_be_reused = 1;
+
+ gettimeofday(&tx_start, NULL); /* start a stopwatch for performance timing */
/*
* Find out what it is that the web browser is asking for
*/
- ContentLength = 0;
- do {
- req_gets(sock, buf, hold);
- if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
- cookie_to_stuff(&buf[15], &desired_session,
- NULL, NULL, NULL);
- got_cookie = 1;
- }
- else if (!strncasecmp(buf, "Content-length: ", 16)) {
- ContentLength = atoi(&buf[16]);
- }
- else if (!strncasecmp(buf, "User-agent: ", 12)) {
- strcpy(browser, &buf[12]);
- }
- strcpy(&req[num_lines++][0], buf);
- } while (strlen(buf) > 0);
+ isbogus = ReadHTTPRequest(Hdr);
+ Hdr->HR.dav_depth = 32767; /* TODO: find a general way to have non-0 defaults */
- /*
- * 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[0][0]);
- 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;
+ if (!isbogus) {
+ isbogus = AnalyseHeaders(Hdr);
+ }
- /*
- * While we're at it, gracefully handle requests for the
- * robots.txt file...
- */
- if (!strncasecmp(buf, "/robots.txt", 11)) {
- strcpy(&req[0][0], "GET /static/robots.txt HTTP/1.0");
+ if ( (isbogus)
+ || ((Hdr->HR.Handler != NULL)
+ && ((Hdr->HR.Handler->Flags & BOGUS) != 0))
+ ) {
+ wcsession *Bogus;
+ Bogus = CreateSession(0, 1, NULL, Hdr, NULL);
+ do_404();
+ syslog(9, "HTTP: 404 [%ld.%06ld] %s %s",
+ ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
+ ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
+ ReqStrs[Hdr->HR.eReqType],
+ ChrPtr(Hdr->this_page)
+ );
+ session_detach_modules(Bogus);
+ session_destroy_modules(&Bogus);
+ return;
}
- /* Do the non-root-cookie check now. */
- else if ( (strcmp(buf, "/")) && (got_cookie == 0)) {
- strcpy(&req[0][0], "GET /static/nocookies.html HTTP/1.0");
+ if ((Hdr->HR.Handler != NULL) && ((Hdr->HR.Handler->Flags & ISSTATIC) != 0))
+ {
+ wcsession *Static;
+ Static = CreateSession(0, 1, NULL, Hdr, NULL);
+
+ Hdr->HR.Handler->F();
+
+ /* How long did this transaction take? */
+ gettimeofday(&tx_finish, NULL);
+
+ syslog(9, "HTTP: 200 [%ld.%06ld] %s %s",
+ ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
+ ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
+ ReqStrs[Hdr->HR.eReqType],
+ ChrPtr(Hdr->this_page)
+ );
+ session_detach_modules(Static);
+ session_destroy_modules(&Static);
+ return;
}
+ if (Hdr->HR.got_auth == AUTH_BASIC) {
+ CheckAuthBasic(Hdr);
+ }
+ if (Hdr->HR.got_auth) {
+ session_may_be_reused = 0;
+ }
/*
- * See if there's an existing session open with the desired ID
+ * See if there's an existing session open with any of:
+ * - The desired Session ID
+ * - A matching http-auth username and password
+ * - An unbound session flagged as reusable
*/
- TheSession = NULL;
- if (desired_session != 0) {
- pthread_mutex_lock(&MasterCritter);
- for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
- if (sptr->session_id == desired_session) {
- TheSession = sptr;
- }
- }
- pthread_mutex_unlock(&MasterCritter);
- }
+ TheSession = FindSession(&SessionList, Hdr, &SessionListMutex);
/*
- * Before we trumpet to the universe that the session we're looking
- * for actually exists, check first to make sure it's still there.
+ * If there were no qualifying sessions, then create a new one.
*/
- if (TheSession != NULL) {
- if (kill(TheSession->webcit_pid, 0)) {
- printf(" Session is *DEAD* !!\n");
- remove_session(TheSession, 1);
- TheSession = NULL;
- }
+ if (TheSession == NULL) {
+ TheSession = CreateSession(1, 0, &SessionList, Hdr, &SessionListMutex);
}
/*
- * Create a new session if we have to
+ * If a language was requested via a cookie, select that language now.
*/
- if (TheSession == NULL) {
- printf("Creating a new session\n");
- locate_host(browser_host, sock);
- TheSession = (struct wc_session *)
- malloc(sizeof(struct wc_session));
- TheSession->session_id = GenerateSessionID();
- pipe(TheSession->inpipe);
- pipe(TheSession->outpipe);
- pthread_mutex_init(&TheSession->critter, NULL);
-
- pthread_mutex_lock(&MasterCritter);
- TheSession->next = SessionList;
- SessionList = TheSession;
- pthread_mutex_unlock(&MasterCritter);
-
- sprintf(str_session, "%d", TheSession->session_id);
- f = fork();
- if (f > 0)
- TheSession->webcit_pid = f;
-
- fflush(stdout);
- fflush(stdin);
- if (f == 0) {
-
- /* Hook stdio to the ends of the pipe we're using */
- dup2(TheSession->inpipe[0], 0);
- dup2(TheSession->outpipe[1], 1);
-
- /* Close the ends of the pipes that we're not using */
- close(TheSession->inpipe[1]);
- close(TheSession->outpipe[0]);
-
- /* Close the HTTP socket in this pid; don't need it */
- close(sock);
-
- /* Run the actual WebCit session */
- execlp("./webcit", "webcit", str_session, defaulthost,
- defaultport, browser_host, browser, NULL);
-
- /* Simple page to display if exec fails */
- printf("HTTP/1.0 404 WebCit Failure\n\n");
- printf("Server: %s\n", SERVER);
- printf("X-WebCit-Session: close\n");
- printf("Content-type: text/html\n");
- printf("Content-length: 76\n");
- printf("\n");
- printf("<HTML><HEAD><TITLE>Error</TITLE></HEAD><BODY>\n");
- printf("execlp() failed: %s</BODY></HTML>\n", strerror(errno));
- exit(0);
- } else {
- /* Close the ends of the pipes that we're not using */
- close(TheSession->inpipe[0]);
- close(TheSession->outpipe[1]);
- }
+ if (StrLength(Hdr->c_language) > 0) {
+ syslog(9, "Session cookie requests language '%s'", ChrPtr(Hdr->c_language));
+ set_selected_language(ChrPtr(Hdr->c_language));
+ go_selected_language();
}
- /*
- * Send the request to the appropriate session...
+ /*
+ * Reject transactions which require http-auth, if http-auth was not provided
*/
- lock_session(TheSession);
- TheSession->lastreq = time(NULL);
- printf(" Writing %d lines of command\n", num_lines);
- printf("%s\n", &req[0][0]);
- for (a = 0; a < num_lines; ++a) {
- write(TheSession->inpipe[1], &req[a][0], strlen(&req[a][0]));
- write(TheSession->inpipe[1], "\n", 1);
- }
- printf(" Writing %d bytes of content\n", ContentLength);
- while (ContentLength > 0) {
- a = ContentLength;
- if (a > sizeof buf)
- a = sizeof buf;
- if (!client_read(sock, buf, a))
- goto end;
- if (write(TheSession->inpipe[1], buf, a) != a)
- goto end;
- ContentLength -= a;
+ if ( (StrLength(Hdr->c_username) == 0)
+ && (!Hdr->HR.DontNeedAuth)
+ && (Hdr->HR.Handler != NULL)
+ && ((XHTTP_COMMANDS & Hdr->HR.Handler->Flags) == XHTTP_COMMANDS)
+ ) {
+ syslog(LOG_DEBUG, "\033[35m -- http-auth required but not provided\033[0m");
+ OverrideRequest(Hdr, HKEY("GET /401 HTTP/1.0"));
+ Hdr->HR.prohibit_caching = 1;
+ /* FIXME -- we have to clear the cookie here */
}
/*
- * ...and get the response.
+ * A future improvement might be to check the session integrity
+ * at this point before continuing.
*/
- printf(" Reading response\n");
- ContentLength = 0;
- do {
- gets0(TheSession->outpipe[0], buf);
- write(sock, buf, strlen(buf));
- write(sock, "\n", 1);
- if (!strncasecmp(buf, "Content-length: ", 16))
- ContentLength = atoi(&buf[16]);
- if (!strcasecmp(buf, "X-WebCit-Session: close")) {
- CloseSession = 1;
- }
- } while (strlen(buf) > 0);
- printf(" Reading %d bytes of content\n", ContentLength);
+ /*
+ * Bind to the session and perform the transaction
+ */
+ CtdlLogResult(pthread_mutex_lock(&TheSession->SessionMutex));
+ pthread_setspecific(MyConKey, (void *)TheSession);
+
+ TheSession->inuse = 1; /* mark the session as bound */
+ TheSession->lastreq = time(NULL); /* log */
+ TheSession->Hdr = Hdr;
+
+ session_attach_modules(TheSession);
+ session_loop(); /* do transaction */
+
+ /* How long did this transaction take? */
+ gettimeofday(&tx_finish, NULL);
+
+ syslog(9, "HTTP: 200 [%ld.%06ld] %s %s",
+ ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
+ ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000,
+ ReqStrs[Hdr->HR.eReqType],
+ ChrPtr(Hdr->this_page)
+ );
+
+ session_detach_modules(TheSession);
+
+ /* If *this* very transaction did not explicitly specify a session cookie,
+ * and it did not log in, we want to flag the session as a candidate for
+ * re-use by the next unbound client that comes along. This keeps our session
+ * table from getting bombarded with new sessions when, for example, a web
+ * spider crawls the site without using cookies.
+ */
+ if ((session_may_be_reused) && (!WC->logged_in)) {
+ WC->wc_session = 0; /* flag as available for re-use */
+ TheSession->selected_language = 0; /* clear any non-default language setting */
+ }
+
+ TheSession->Hdr = NULL;
+ TheSession->inuse = 0; /* mark the session as unbound */
+ CtdlLogResult(pthread_mutex_unlock(&TheSession->SessionMutex));
+}
+
+void tmplput_nonce(StrBuf *Target, WCTemplputParams *TP)
+{
+ 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 Header_HandleContentLength(StrBuf *Line, ParsedHttpHdrs *hdr)
+{
+ hdr->HR.ContentLength = StrToi(Line);
+}
- while (ContentLength--) {
- read(TheSession->outpipe[0], buf, 1);
- write(sock, buf, 1);
+void Header_HandleContentType(StrBuf *Line, ParsedHttpHdrs *hdr)
+{
+ hdr->HR.ContentType = Line;
+}
+
+
+void Header_HandleHost(StrBuf *Line, ParsedHttpHdrs *hdr)
+{
+ if (hdr->HostHeader != NULL) {
+ FreeStrBuf(&hdr->HostHeader);
}
+ hdr->HostHeader = NewStrBuf();
+ StrBufAppendPrintf(hdr->HostHeader, "%s://", (is_https ? "https" : "http") );
+ StrBufAppendBuf(hdr->HostHeader, Line, 0);
+}
- /*
- * If the last response included a "close session" directive,
- * remove the context now.
- */
- if (CloseSession) {
- remove_session(TheSession, 1);
- } else {
-end: unlock_session(TheSession);
+void Header_HandleXFFHost(StrBuf *Line, ParsedHttpHdrs *hdr)
+{
+ if (!follow_xff) return;
+
+ if (hdr->HostHeader != NULL) {
+ FreeStrBuf(&hdr->HostHeader);
}
- free(req);
+ hdr->HostHeader = NewStrBuf();
+ StrBufAppendPrintf(hdr->HostHeader, "http://"); /* this is naive; do something about it */
+ StrBufAppendBuf(hdr->HostHeader, Line, 0);
+}
- /*
- * Now our HTTP connection is done. It would be relatively easy
- * to support HTTP/1.1 "persistent" connections by looping back to
- * the top of this function. For now, we'll just close.
- */
- printf(" Closing socket %d ... ret=%d\n", sock,
- lingering_close(sock));
+void Header_HandleXFF(StrBuf *Line, ParsedHttpHdrs *hdr)
+{
+ hdr->HR.browser_host = Line;
+
+ while (StrBufNum_tokens(hdr->HR.browser_host, ',') > 1) {
+ StrBufRemove_token(hdr->HR.browser_host, 0, ',');
+ }
+ StrBufTrim(hdr->HR.browser_host);
+}
+
+void Header_HandleIfModSince(StrBuf *Line, ParsedHttpHdrs *hdr)
+{
+ hdr->HR.if_modified_since = httpdate_to_timestamp(Line);
+}
+
+void Header_HandleAcceptEncoding(StrBuf *Line, ParsedHttpHdrs *hdr)
+{
/*
- * The thread handling this HTTP connection is now finished.
- * Instead of calling pthread_exit(), just return. It does the same
- * thing, and supresses a compiler warning.
+ * Can we compress?
*/
- return NULL;
+ if (strstr(&ChrPtr(Line)[16], "gzip")) {
+ hdr->HR.gzip_ok = 1;
+ }
+}
+const char *ReqStrs[eNONE] = {
+ "GET",
+ "POST",
+ "OPTIONS",
+ "PROPFIND",
+ "PUT",
+ "DELETE",
+ "HEAD",
+ "MOVE",
+ "COPY"
+};
+
+void
+ServerStartModule_CONTEXT
+(void)
+{
+ long *v;
+ HttpReqTypes = NewHash(1, NULL);
+ HttpHeaderHandler = NewHash(1, NULL);
+
+ v = malloc(sizeof(long));
+ *v = eGET;
+ Put(HttpReqTypes, HKEY("GET"), v, NULL);
+
+ v = malloc(sizeof(long));
+ *v = ePOST;
+ Put(HttpReqTypes, HKEY("POST"), v, NULL);
+
+ v = malloc(sizeof(long));
+ *v = eOPTIONS;
+ Put(HttpReqTypes, HKEY("OPTIONS"), v, NULL);
+
+ v = malloc(sizeof(long));
+ *v = ePROPFIND;
+ Put(HttpReqTypes, HKEY("PROPFIND"), v, NULL);
+
+ v = malloc(sizeof(long));
+ *v = ePUT;
+ Put(HttpReqTypes, HKEY("PUT"), v, NULL);
+
+ v = malloc(sizeof(long));
+ *v = eDELETE;
+ Put(HttpReqTypes, HKEY("DELETE"), v, NULL);
+
+ v = malloc(sizeof(long));
+ *v = eHEAD;
+ Put(HttpReqTypes, HKEY("HEAD"), v, NULL);
+
+ v = malloc(sizeof(long));
+ *v = eMOVE;
+ Put(HttpReqTypes, HKEY("MOVE"), v, NULL);
+
+ v = malloc(sizeof(long));
+ *v = eCOPY;
+ Put(HttpReqTypes, HKEY("COPY"), v, NULL);
+}
+
+void
+ServerShutdownModule_CONTEXT
+(void)
+{
+ DeleteHash(&HttpReqTypes);
+ DeleteHash(&HttpHeaderHandler);
+}
+
+void RegisterHeaderHandler(const char *Name, long Len, Header_Evaluator F)
+{
+ OneHttpHeader *pHdr;
+ pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
+ memset(pHdr, 0, sizeof(OneHttpHeader));
+ pHdr->H = F;
+ Put(HttpHeaderHandler, Name, Len, pHdr, DestroyHttpHeaderHandler);
+}
+
+
+void
+InitModule_CONTEXT
+(void)
+{
+ RegisterHeaderHandler(HKEY("CONTENT-LENGTH"), Header_HandleContentLength);
+ RegisterHeaderHandler(HKEY("CONTENT-TYPE"), Header_HandleContentType);
+ RegisterHeaderHandler(HKEY("X-FORWARDED-HOST"), Header_HandleXFFHost); /* Apache way... */
+ RegisterHeaderHandler(HKEY("X-REAL-IP"), Header_HandleXFFHost); /* NGinX way... */
+ RegisterHeaderHandler(HKEY("HOST"), Header_HandleHost);
+ RegisterHeaderHandler(HKEY("X-FORWARDED-FOR"), Header_HandleXFF);
+ RegisterHeaderHandler(HKEY("ACCEPT-ENCODING"), Header_HandleAcceptEncoding);
+ RegisterHeaderHandler(HKEY("IF-MODIFIED-SINCE"), Header_HandleIfModSince);
+
+ RegisterNamespace("CURRENT_USER", 0, 1, tmplput_current_user, NULL, CTX_NONE);
+ RegisterNamespace("NONCE", 0, 0, tmplput_nonce, NULL, 0);
+
+ WebcitAddUrlHandler(HKEY("404"), "", 0, do_404, ANONYMOUS|COOKIEUNNEEDED);
+/*
+ * 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.
+ */
+ WebcitAddUrlHandler(HKEY("scripts"), "", 0, do_404, ANONYMOUS|BOGUS); /* /root.exe - Worms and trojans and viruses, oh my! */
+ WebcitAddUrlHandler(HKEY("c"), "", 0, do_404, ANONYMOUS|BOGUS); /* /winnt */
+ WebcitAddUrlHandler(HKEY("MSADC"), "", 0, do_404, ANONYMOUS|BOGUS);
+ WebcitAddUrlHandler(HKEY("_vti"), "", 0, do_404, ANONYMOUS|BOGUS); /* Broken Microsoft DAV implementation */
+ WebcitAddUrlHandler(HKEY("MSOffice"), "", 0, do_404, ANONYMOUS|BOGUS); /* Stoopid MSOffice thinks everyone is IIS */
+ WebcitAddUrlHandler(HKEY("nonexistenshit"), "", 0, do_404, ANONYMOUS|BOGUS); /* Exploit found in the wild January 2009 */
+}
+
+
+void
+HttpNewModule_CONTEXT
+(ParsedHttpHdrs *httpreq)
+{
+ httpreq->PlainArgs = NewStrBufPlain(NULL, SIZ);
+ httpreq->this_page = NewStrBufPlain(NULL, SIZ);
+}
+
+void
+HttpDetachModule_CONTEXT
+(ParsedHttpHdrs *httpreq)
+{
+ FlushStrBuf(httpreq->PlainArgs);
+ FlushStrBuf(httpreq->HostHeader);
+ FlushStrBuf(httpreq->this_page);
+ FlushStrBuf(httpreq->PlainArgs);
+ DeleteHash(&httpreq->HTTPHeaders);
+ memset(&httpreq->HR, 0, sizeof(HdrRefs));
+}
+
+void
+HttpDestroyModule_CONTEXT
+(ParsedHttpHdrs *httpreq)
+{
+ FreeStrBuf(&httpreq->this_page);
+ FreeStrBuf(&httpreq->PlainArgs);
+ FreeStrBuf(&httpreq->this_page);
+ FreeStrBuf(&httpreq->PlainArgs);
+ FreeStrBuf(&httpreq->HostHeader);
+ DeleteHash(&httpreq->HTTPHeaders);
+
}