4 * This is the other half of the webserver. It handles the task of hooking
5 * up HTTP requests with the sessions they belong to, using HTTP cookies to
6 * keep track of things. If the HTTP request doesn't belong to any currently
7 * active session, a new session is started.
12 #include "webserver.h"
13 #include "modules_init.h"
15 /* Only one thread may manipulate SessionList at a time... */
16 pthread_mutex_t SessionListMutex;
18 wcsession *SessionList = NULL; /**< our sessions ????*/
20 pthread_key_t MyConKey; /**< TSD key for MySession() */
21 HashList *HttpReqTypes = NULL;
22 HashList *HttpHeaderHandler = NULL;
23 extern HashList *HandlerHash;
25 void DestroyHttpHeaderHandler(void *V)
28 pHdr = (OneHttpHeader*) V;
29 FreeStrBuf(&pHdr->Val);
33 void shutdown_sessions(void)
37 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
42 void do_housekeeping(void)
45 wcsession *sessions_to_kill = NULL;
47 static int num_threads = MIN_WORKER_THREADS;
50 * Lock the session list, moving any candidates for euthanasia into
53 pthread_mutex_lock(&SessionListMutex);
55 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
58 /** Kill idle sessions */
59 if ((time(NULL) - (sptr->lastreq)) >
60 (time_t) WEBCIT_TIMEOUT) {
64 /** Remove sessions flagged for kill */
67 /** remove session from linked list */
68 if (sptr == SessionList) {
69 SessionList = SessionList->next;
71 else for (ss=SessionList;ss!=NULL;ss=ss->next) {
72 if (ss->next == sptr) {
73 ss->next = ss->next->next;
77 sptr->next = sessions_to_kill;
78 sessions_to_kill = sptr;
81 pthread_mutex_unlock(&SessionListMutex);
84 * Now free up and destroy the culled sessions.
86 while (sessions_to_kill != NULL) {
87 lprintf(3, "Destroying session %d\n", sessions_to_kill->wc_session);
88 pthread_mutex_lock(&sessions_to_kill->SessionMutex);
89 pthread_mutex_unlock(&sessions_to_kill->SessionMutex);
90 sptr = sessions_to_kill->next;
92 session_destroy_modules(&sessions_to_kill);
93 sessions_to_kill = sptr;
98 * If there are more sessions than threads, then we should spawn
99 * more threads ... up to a predefined maximum.
101 while ( (num_sessions > num_threads)
102 && (num_threads <= MAX_WORKER_THREADS) ) {
103 spawn_another_worker_thread();
105 lprintf(3, "There are %d sessions and %d threads active.\n",
106 num_sessions, num_threads);
112 * Wake up occasionally and clean house
114 void housekeeping_loop(void)
117 sleeeeeeeeeep(HOUSEKEEPING);
124 * Create a Session id
125 * Generate a unique WebCit session ID (which is not the same thing as the
126 * Citadel session ID).
128 int GenerateSessionID(void)
130 static int seq = (-1);
133 seq = (int) time(NULL);
141 * lingering_close() a`la Apache. see
142 * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
144 int lingering_close(int fd)
149 struct timeval tv, start;
151 gettimeofday(&start, NULL);
155 gettimeofday(&tv, NULL);
156 tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
157 tv.tv_usec = start.tv_usec - tv.tv_usec;
158 if (tv.tv_usec < 0) {
160 tv.tv_usec += 1000000;
164 i = select(fd + 1, &set, NULL, NULL, &tv);
165 } while (i == -1 && errno == EINTR);
170 i = read(fd, buf, sizeof buf);
171 } while (i != 0 && (i != -1 || errno == EINTR));
180 * \brief Detects a 'mobile' user agent
182 int is_mobile_ua(char *user_agent) {
183 if (strstr(user_agent,"iPhone OS") != NULL) {
185 } else if (strstr(user_agent,"Windows CE") != NULL) {
187 } else if (strstr(user_agent,"SymbianOS") != NULL) {
189 } else if (strstr(user_agent, "Opera Mobi") != NULL) {
191 } else if (strstr(user_agent, "Firefox/2.0.0 Opera 9.51 Beta") != NULL) {
192 /* For some reason a new install of Opera 9.51beta decided to spoof. */
201 * Look for commonly-found probes of malware such as worms, viruses, trojans, and Microsoft Office.
202 * Short-circuit these requests so we don't have to send them through the full processing loop.
204 int is_bogus(StrBuf *http_cmd) {////TODO!
207 const char *bogus_prefixes[] = {
208 "/scripts/root.exe", /* Worms and trojans and viruses, oh my! */
211 "/_vti", /* Broken Microsoft DAV implementation */
212 "/MSOffice", /* Stoopid MSOffice thinks everyone is IIS */
213 "/nonexistenshit" /* Exploit found in the wild January 2009 */
216 url = ChrPtr(http_cmd);
217 if (IsEmptyStr(url)) return(1);
220 max = sizeof(bogus_prefixes) / sizeof(char *);
222 for (i=0; i<max; ++i) {
223 if (!strncasecmp(url, bogus_prefixes[i], strlen(bogus_prefixes[i]))) {
228 return(0); /* probably ok */
232 int ReadHttpSubject(ParsedHttpHdrs *Hdr, StrBuf *Line, StrBuf *Buf)
235 void *vLine, *vHandler;
236 const char *Pos = NULL;
240 /* The requesttype... GET, POST... */
241 StrBufExtract_token(Buf, Hdr->ReqLine, 0, ' ');
242 if (GetHash(HttpReqTypes, SKEY(Buf), &vLine) &&
245 Hdr->eReqType = *(long*)vLine;
248 Hdr->eReqType = eGET;
251 StrBufCutLeft(Hdr->ReqLine, StrLength(Buf) + 1);
253 /* the HTTP Version... */
254 StrBufExtract_token(Buf, Hdr->ReqLine, 1, ' ');
255 StrBufCutRight(Hdr->ReqLine, StrLength(Buf) + 1);
256 if ((StrLength(Buf) == 0) ||
257 is_bogus(Hdr->ReqLine)) {
258 Hdr->eReqType = eGET;
262 Hdr->this_page = NewStrBufDup(Hdr->ReqLine);
263 /* chop Filename / query arguments */
264 Args = strchr(ChrPtr(Hdr->ReqLine), '?');
265 if (Args == NULL) /* whe're not that picky about params... TODO: this will spoil '&' in filenames.*/
266 Args = strchr(ChrPtr(Hdr->ReqLine), '&');
268 Args ++; /* skip the ? */
269 Hdr->PlainArgs = NewStrBufPlain(
271 StrLength(Hdr->ReqLine) -
272 (Args - ChrPtr(Hdr->ReqLine)));
273 StrBufCutAt(Hdr->ReqLine, 0, Args - 1);
274 } /* don't parse them yet, maybe we don't even care... */
276 /* now lookup what we are going to do with this... */
277 /* skip first slash */
278 StrBufExtract_NextToken(Buf, Hdr->ReqLine, &Pos, '/');
280 StrBufExtract_NextToken(Buf, Hdr->ReqLine, &Pos, '/');
282 GetHash(HandlerHash, SKEY(Buf), &vHandler),
283 Hdr->Handler = (WebcitHandler*) vHandler;
284 if (Hdr->Handler == NULL)
286 /* are we about to ignore some prefix like webcit/ ? */
287 if ((Hdr->Handler->Flags & URLNAMESPACE) == 0)
290 /* remove the handlername from the URL */
292 StrBufCutLeft(Hdr->ReqLine,
293 Pos - ChrPtr(Hdr->ReqLine));
296 if (Hdr->Handler == NULL)
299 Hdr->HTTPHeaders = NewHash(1, NULL);
304 int AnalyseHeaders(ParsedHttpHdrs *Hdr)
310 HashPos *at = GetNewHashPos(Hdr->HTTPHeaders, 0);
312 while (GetNextHashPos(Hdr->HTTPHeaders, at, &HKLen, &HashKey, &vHdr) &&
314 pHdr = (OneHttpHeader *)vHdr;
315 if (pHdr->HaveEvaluator)
316 pHdr->H(pHdr->Val, Hdr);
323 /*const char *nix(void *vptr) {return ChrPtr( (StrBuf*)vptr);}*/
326 * Read in the request
328 int ReadHTTPRequset (ParsedHttpHdrs *Hdr)
330 const char *pch, *pchs, *pche;
332 StrBuf *Line, *LastLine, *HeaderName;
337 HeaderName = NewStrBuf();
338 Hdr->ReadBuf = NewStrBuf();
344 if (ClientGetLine(&Hdr->http_sock, Line, Hdr->ReadBuf, &Hdr->Pos) < 0) return 1;
346 if (StrLength(Line) == 0) {
351 isbogus = ReadHttpSubject(Hdr, Line, HeaderName);
356 /* Do we need to Unfold? */
357 if ((LastLine != NULL) &&
358 (isspace(*ChrPtr(Line)))) {
359 pch = pchs = ChrPtr(Line);
360 pche = pchs + StrLength(Line);
361 while (isspace(*pch) && (pch < pche))
363 StrBufCutLeft(Line, pch - pchs);
364 StrBufAppendBuf(LastLine, Line, 0);
370 StrBufSanitizeAscii(Line, '§');
371 StrBufExtract_token(HeaderName, Line, 0, ':');
374 pch = pchs + StrLength(HeaderName) + 1;
375 pche = pchs + StrLength(Line);
376 while (isspace(*pch) && (pch < pche))
378 StrBufCutLeft(Line, pch - pchs);
380 StrBufUpCase(HeaderName);
382 pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
383 memset(pHdr, 0, sizeof(OneHttpHeader));
386 if (GetHash(HttpHeaderHandler, SKEY(HeaderName), &vF) &&
389 OneHttpHeader *FHdr = (OneHttpHeader*) vF;
391 pHdr->HaveEvaluator = 1;
393 Put(Hdr->HTTPHeaders, SKEY(HeaderName), pHdr, DestroyHttpHeaderHandler);
395 } while (Line != NULL);
397 FreeStrBuf(&HeaderName);
407 * This loop gets called once for every HTTP connection made to WebCit. At
408 * this entry point we have an HTTP socket with a browser allegedly on the
409 * other end, but we have not yet bound to a WebCit session.
411 * The job of this function is to locate the correct session and bind to it,
412 * or create a session if necessary and bind to it, then run the WebCit
413 * transaction loop. Afterwards, we unbind from the session. When this
414 * function returns, the worker thread is then free to handle another
417 void context_loop(int *sock)
421 wcsession *TheSession, *sptr;
422 struct timeval tx_start;
423 struct timeval tx_finish;
425 gettimeofday(&tx_start, NULL); /* start a stopwatch for performance timing */
427 memset(&Hdr, 0, sizeof(ParsedHttpHdrs));
429 Hdr.http_sock = *sock;
431 * Find out what it is that the web browser is asking for
433 isbogus = ReadHTTPRequset(&Hdr);
436 isbogus = AnalyseHeaders(&Hdr);
439 StrBufPlain(ReqLine, HKEY("/404"));
442 /* dbg_PrintHash(HTTPHeaders, nix, NULL); */
445 * If the request is prefixed by "/webcit" then chop that off. This
446 * allows a front end web server to forward all /webcit requests to us
447 * while still using the same web server port for other things.
450 (StrLength(ReqLine) >= 8) &&
451 (strstr(ChrPtr(ReqLine), "/webcit/")) ) {
452 StrBufCutLeft(ReqLine, 7);
455 /* Begin parsing the request. * /
457 if ((strncmp(ChrPtr(ReqLine), "/sslg", 5) != 0) &&
458 (strncmp(ChrPtr(ReqLine), "/static/", 8) != 0) &&
459 (strncmp(ChrPtr(ReqLine), "/tiny_mce/", 10) != 0) &&
460 (strncmp(ChrPtr(ReqLine), "/wholist_section", 16) != 0) &&
461 (strstr(ChrPtr(ReqLine), "wholist_section") == NULL)) {
463 lprintf(5, "HTTP: %s %s\n", ReqStrs[Hdr.eReqType], ChrPtr(ReqLine));
471 * See if there's an existing session open with the desired ID or user/pass
475 if (TheSession == NULL) {
476 pthread_mutex_lock(&SessionListMutex);
477 for (sptr = SessionList;
478 ((sptr != NULL) && (TheSession == NULL));
481 /** If HTTP-AUTH, look for a session with matching credentials * /
482 if ( (////TODO check auth type here...
483 &&(!strcasecmp(ChrPtr(sptr->httpauth_user), httpauth_user))
484 &&(!strcasecmp(ChrPtr(sptr->httpauth_pass), httpauth_pass)) ) {
488 /** If cookie-session, look for a session with matching session ID */
489 if ( (Hdr.desired_session != 0) && (sptr->wc_session == Hdr.desired_session)) {
494 pthread_mutex_unlock(&SessionListMutex);
498 * Create a new session if we have to
500 if (TheSession == NULL) {
501 lprintf(3, "Creating a new session\n");
502 TheSession = (wcsession *)
503 malloc(sizeof(wcsession));
504 memset(TheSession, 0, sizeof(wcsession));
505 TheSession->Hdr = &Hdr;
506 TheSession->serv_sock = (-1);
507 TheSession->chat_sock = (-1);
509 /* If we're recreating a session that expired, it's best to give it the same
510 * session number that it had before. The client browser ought to pick up
511 * the new session number and start using it, but in some rare situations it
512 * doesn't, and that's a Bad Thing because it causes lots of spurious sessions
515 if (Hdr.desired_session == 0) {
516 TheSession->wc_session = GenerateSessionID();
519 TheSession->wc_session = Hdr.desired_session;
522 TheSession->httpauth_user = NewStrBufPlain(httpauth_user, -1);
523 TheSession->httpauth_pass = NewStrBufPlain(httpauth_user, -1);
525 pthread_setspecific(MyConKey, (void *)TheSession);
526 session_new_modules(TheSession);
528 pthread_mutex_init(&TheSession->SessionMutex, NULL);
529 pthread_mutex_lock(&SessionListMutex);
530 TheSession->nonce = rand();
531 TheSession->next = SessionList;
532 TheSession->is_mobile = -1;
533 SessionList = TheSession;
534 pthread_mutex_unlock(&SessionListMutex);
538 * A future improvement might be to check the session integrity
539 * at this point before continuing.
543 * Bind to the session and perform the transaction
545 pthread_mutex_lock(&TheSession->SessionMutex); /* bind */
546 pthread_setspecific(MyConKey, (void *)TheSession);
548 TheSession->lastreq = time(NULL); /* log */
549 TheSession->Hdr = &Hdr;
551 session_attach_modules(TheSession);
552 session_loop(); /* do transaction */
555 /* How long did this transaction take? */
556 gettimeofday(&tx_finish, NULL);
558 lprintf(9, "Transaction [%s] completed in %ld.%06ld seconds.\n",
559 ChrPtr(Hdr.this_page),
560 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
561 ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000
564 session_detach_modules(TheSession);
566 TheSession->Hdr = NULL;
567 pthread_mutex_unlock(&TheSession->SessionMutex); /* unbind */
570 http_destroy_modules(&Hdr);
573 FreeStrBuf(&c_username);
574 FreeStrBuf(&c_password);
575 FreeStrBuf(&c_roomname);
576 FreeStrBuf(&c_httpauth_user);
577 FreeStrBuf(&c_httpauth_pass);
579 /* Free the request buffer */
580 ///FreeStrBuf(&ReqLine);
584 void tmplput_nonce(StrBuf *Target, WCTemplputParams *TP)
587 StrBufAppendPrintf(Target, "%ld",
588 (WCC != NULL)? WCC->nonce:0);
591 void tmplput_current_user(StrBuf *Target, WCTemplputParams *TP)
593 StrBufAppendTemplate(Target, TP, WC->wc_fullname, 0);
596 void tmplput_current_room(StrBuf *Target, WCTemplputParams *TP)
598 StrBufAppendTemplate(Target, TP, WC->wc_roomname, 0);
602 void Header_HandleCookie(StrBuf *Line, ParsedHttpHdrs *hdr)
604 hdr->RawCookie = Line;
605 if (hdr->DontNeedAuth)
608 c_username = NewStrBuf();
609 c_password = NewStrBuf();
610 c_roomname = NewStrBuf();
611 safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
612 c_httpauth_user = NewStrBufPlain(HKEY(DEFAULT_HTTPAUTH_USER));
613 c_httpauth_pass = NewStrBufPlain(HKEY(DEFAULT_HTTPAUTH_PASS));
615 cookie_to_stuff(Line, &hdr->desired_session,
624 * Browser-based sessions use cookies for session authentication
627 GetHash(HTTPHeaders, HKEY("COOKIE"), &vLine) &&
629 cookie_to_stuff(vLine, &desired_session,
635 * GroupDAV-based sessions use HTTP authentication
639 GetHash(HTTPHeaders, HKEY("AUTHORIZATION"), &vLine) &&
641 Line = (StrBuf*)vLine;
642 if (strncasecmp(ChrPtr(Line), "Basic", 5) == 0) {
643 StrBufCutLeft(Line, 6);
644 CtdlDecodeBase64(httpauth_string, ChrPtr(Line), StrLength(Line));
645 extract_token(httpauth_user, httpauth_string, 0, ':', sizeof httpauth_user);
646 extract_token(httpauth_pass, httpauth_string, 1, ':', sizeof httpauth_pass);
649 lprintf(1, "Authentication scheme not supported! [%s]\n", ChrPtr(Line));
653 void Header_HandleAuth(StrBuf *Line, ParsedHttpHdrs *hdr)
655 const char *Pos = NULL;
656 StrBufDecodeBase64(Line);
657 StrBufExtract_NextToken(hdr->c_username, Line, &Pos, ':');
658 StrBufExtract_NextToken(hdr->c_password, Line, &Pos, ':');
661 void Header_HandleContentLength(StrBuf *Line, ParsedHttpHdrs *hdr)
663 hdr->ContentLength = StrToi(Line);
666 void Header_HandleContentType(StrBuf *Line, ParsedHttpHdrs *hdr)
668 hdr->ContentType = Line;
671 void Header_HandleUserAgent(StrBuf *Line, ParsedHttpHdrs *hdr)
673 hdr->user_agent = Line;
675 /* TODO: do this later on session creating
676 if ((WCC->is_mobile < 0) && is_mobile_ua(&buf[12])) {
687 void Header_HandleHost(StrBuf *Line, ParsedHttpHdrs *hdr)
689 if ((follow_xff) && (hdr->http_host != NULL))
692 hdr->http_host = Line;
695 void Header_HandleXFFHost(StrBuf *Line, ParsedHttpHdrs *hdr)
698 hdr->http_host = Line;
702 void Header_HandleXFF(StrBuf *Line, ParsedHttpHdrs *hdr)
704 hdr->browser_host = Line;
706 while (StrBufNum_tokens(hdr->browser_host, ',') > 1) {
707 StrBufRemove_token(hdr->browser_host, 0, ',');
709 StrBufTrim(hdr->browser_host);
712 void Header_HandleIfModSince(StrBuf *Line, ParsedHttpHdrs *hdr)
714 hdr->if_modified_since = httpdate_to_timestamp(Line);
717 void Header_HandleAcceptEncoding(StrBuf *Line, ParsedHttpHdrs *hdr)
722 if (strstr(&ChrPtr(Line)[16], "gzip")) {
729 c_username = NewStrBuf();
730 c_password = NewStrBuf();
731 c_roomname = NewStrBuf();
732 safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
733 c_httpauth_user = NewStrBufPlain(HKEY(DEFAULT_HTTPAUTH_USER));
734 c_httpauth_pass = NewStrBufPlain(HKEY(DEFAULT_HTTPAUTH_PASS));
738 * These are the URL's which may be executed without a
739 * session cookie already set. If it's not one of these,
740 * force the session to close because cookies are
741 * probably disabled on the client browser.
743 else if ( (StrLength(ReqLine) > 1 )
744 && (strncasecmp(ChrPtr(ReqLine), "/404", 4))
745 && (Hdr.got_cookie == 0)) {
747 HKEY("/static/nocookies.html"
748 "?force_close_session=yes"));
751 const char *ReqStrs[eNONE] = {
764 ServerStartModule_CONTEXT
768 HttpReqTypes = NewHash(1, NULL);
769 HttpHeaderHandler = NewHash(1, NULL);
771 v = malloc(sizeof(long));
773 Put(HttpReqTypes, HKEY("GET"), v, NULL);
775 v = malloc(sizeof(long));
777 Put(HttpReqTypes, HKEY("POST"), v, NULL);
779 v = malloc(sizeof(long));
781 Put(HttpReqTypes, HKEY("OPTIONS"), v, NULL);
783 v = malloc(sizeof(long));
785 Put(HttpReqTypes, HKEY("PROPFIND"), v, NULL);
787 v = malloc(sizeof(long));
789 Put(HttpReqTypes, HKEY("PUT"), v, NULL);
791 v = malloc(sizeof(long));
793 Put(HttpReqTypes, HKEY("DELETE"), v, NULL);
795 v = malloc(sizeof(long));
797 Put(HttpReqTypes, HKEY("HEAD"), v, NULL);
799 v = malloc(sizeof(long));
801 Put(HttpReqTypes, HKEY("MOVE"), v, NULL);
803 v = malloc(sizeof(long));
805 Put(HttpReqTypes, HKEY("COPY"), v, NULL);
809 ServerShutdownModule_CONTEXT
812 DeleteHash(&HttpReqTypes);
813 DeleteHash(&HttpHeaderHandler);
816 void RegisterHeaderHandler(const char *Name, long Len, Header_Evaluator F)
819 pHdr = (OneHttpHeader*) malloc(sizeof(OneHttpHeader));
820 memset(pHdr, 0, sizeof(OneHttpHeader));
822 Put(HttpHeaderHandler, Name, Len, pHdr, DestroyHttpHeaderHandler);
824 extern void blank_page(void); ///TODO: remove me
829 RegisterHeaderHandler(HKEY("COOKIE"), Header_HandleCookie);
830 RegisterHeaderHandler(HKEY("AUTHORIZATION"), Header_HandleAuth);
831 RegisterHeaderHandler(HKEY("CONTENT-LENGTH"), Header_HandleContentLength);
832 RegisterHeaderHandler(HKEY("CONTENT-TYPE"), Header_HandleContentType);
833 RegisterHeaderHandler(HKEY("USER-AGENT"), Header_HandleUserAgent);
834 RegisterHeaderHandler(HKEY("X-FORWARDED-HOST"), Header_HandleXFFHost);
835 RegisterHeaderHandler(HKEY("HOST"), Header_HandleHost);
836 RegisterHeaderHandler(HKEY("X-FORWARDED-FOR"), Header_HandleXFF);
837 RegisterHeaderHandler(HKEY("ACCEPT-ENCODING"), Header_HandleAcceptEncoding);
838 RegisterHeaderHandler(HKEY("IF-MODIFIED-SINCE"), Header_HandleIfModSince);
840 RegisterNamespace("CURRENT_USER", 0, 1, tmplput_current_user, CTX_NONE);
841 RegisterNamespace("CURRENT_ROOM", 0, 1, tmplput_current_room, CTX_NONE);
842 RegisterNamespace("NONCE", 0, 0, tmplput_nonce, 0);
846 WebcitAddUrlHandler(HKEY("blank"), blank_page, ANONYMOUS|BOGUS);
848 WebcitAddUrlHandler(HKEY("webcit"), blank_page, URLNAMESPACE);
854 HttpDestroyModule_CONTEXT
855 (ParsedHttpHdrs *httpreq)
857 FreeStrBuf(&httpreq->ReqLine);
858 FreeStrBuf(&httpreq->ReadBuf);
859 FreeStrBuf(&httpreq->PlainArgs);
860 FreeStrBuf(&httpreq->this_page);
861 DeleteHash(&httpreq->HTTPHeaders);