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() */
24 void DestroySession(wcsession **sessions_to_kill)
26 close((*sessions_to_kill)->serv_sock);
27 close((*sessions_to_kill)->chat_sock);
29 // if ((*sessions_to_kill)->preferences != NULL) {
30 // free((*sessions_to_kill)->preferences);
33 if ((*sessions_to_kill)->cache_fold != NULL) {
34 free((*sessions_to_kill)->cache_fold);
36 DeleteServInfo(&((*sessions_to_kill)->serv_info));
37 free_march_list((*sessions_to_kill));
39 session_destroy_modules(*sessions_to_kill);
41 free((*sessions_to_kill));
42 (*sessions_to_kill) = NULL;
45 void shutdown_sessions(void)
49 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
54 void do_housekeeping(void)
57 wcsession *sessions_to_kill = NULL;
59 static int num_threads = MIN_WORKER_THREADS;
62 * Lock the session list, moving any candidates for euthanasia into
65 pthread_mutex_lock(&SessionListMutex);
67 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
70 /** Kill idle sessions */
71 if ((time(NULL) - (sptr->lastreq)) >
72 (time_t) WEBCIT_TIMEOUT) {
76 /** Remove sessions flagged for kill */
79 /** remove session from linked list */
80 if (sptr == SessionList) {
81 SessionList = SessionList->next;
83 else for (ss=SessionList;ss!=NULL;ss=ss->next) {
84 if (ss->next == sptr) {
85 ss->next = ss->next->next;
89 sptr->next = sessions_to_kill;
90 sessions_to_kill = sptr;
93 pthread_mutex_unlock(&SessionListMutex);
96 * Now free up and destroy the culled sessions.
98 while (sessions_to_kill != NULL) {
99 lprintf(3, "Destroying session %d\n", sessions_to_kill->wc_session);
100 pthread_mutex_lock(&sessions_to_kill->SessionMutex);
101 pthread_mutex_unlock(&sessions_to_kill->SessionMutex);
102 sptr = sessions_to_kill->next;
104 DestroySession(&sessions_to_kill);
105 sessions_to_kill = sptr;
110 * If there are more sessions than threads, then we should spawn
111 * more threads ... up to a predefined maximum.
113 while ( (num_sessions > num_threads)
114 && (num_threads <= MAX_WORKER_THREADS) ) {
115 spawn_another_worker_thread();
117 lprintf(3, "There are %d sessions and %d threads active.\n",
118 num_sessions, num_threads);
124 * Wake up occasionally and clean house
126 void housekeeping_loop(void)
129 sleeeeeeeeeep(HOUSEKEEPING);
136 * Create a Session id
137 * Generate a unique WebCit session ID (which is not the same thing as the
138 * Citadel session ID).
140 int GenerateSessionID(void)
142 static int seq = (-1);
145 seq = (int) time(NULL);
153 * lingering_close() a`la Apache. see
154 * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
156 int lingering_close(int fd)
161 struct timeval tv, start;
163 gettimeofday(&start, NULL);
167 gettimeofday(&tv, NULL);
168 tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
169 tv.tv_usec = start.tv_usec - tv.tv_usec;
170 if (tv.tv_usec < 0) {
172 tv.tv_usec += 1000000;
176 i = select(fd + 1, &set, NULL, NULL, &tv);
177 } while (i == -1 && errno == EINTR);
182 i = read(fd, buf, sizeof buf);
183 } while (i != 0 && (i != -1 || errno == EINTR));
191 * Look for commonly-found probes of malware such as worms, viruses, trojans, and Microsoft Office.
192 * Short-circuit these requests so we don't have to send them through the full processing loop.
194 int is_bogus(StrBuf *http_cmd) {
197 const char *bogus_prefixes[] = {
198 "/scripts/root.exe", /* Worms and trojans and viruses, oh my! */
201 "/_vti", /* Broken Microsoft DAV implementation */
202 "/MSOffice", /* Stoopid MSOffice thinks everyone is IIS */
203 "/nonexistenshit" /* Exploit found in the wild January 2009 */
206 url = ChrPtr(http_cmd);
207 if (IsEmptyStr(url)) return(1);
210 max = sizeof(bogus_prefixes) / sizeof(char *);
212 for (i=0; i<max; ++i) {
213 if (!strncasecmp(url, bogus_prefixes[i], strlen(bogus_prefixes[i]))) {
218 return(0); /* probably ok */
222 /*const char *nix(void *vptr) {return ChrPtr( (StrBuf*)vptr);}*/
227 * This loop gets called once for every HTTP connection made to WebCit. At
228 * this entry point we have an HTTP socket with a browser allegedly on the
229 * other end, but we have not yet bound to a WebCit session.
231 * The job of this function is to locate the correct session and bind to it,
232 * or create a session if necessary and bind to it, then run the WebCit
233 * transaction loop. Afterwards, we unbind from the session. When this
234 * function returns, the worker thread is then free to handle another
237 void context_loop(int *sock)
239 const char *Pos = NULL;
241 int desired_session = 0;
244 wcsession *TheSession, *sptr;
245 char httpauth_string[1024];
246 char httpauth_user[1024];
247 char httpauth_pass[1024];
248 int session_is_new = 0;
252 StrBuf *Buf, *Line, *LastLine, *HeaderName, *ReqLine, *ReqType, *HTTPVersion;
253 const char *pch, *pchs, *pche;
254 HashList *HTTPHeaders;
256 strcpy(httpauth_string, "");
257 strcpy(httpauth_user, DEFAULT_HTTPAUTH_USER);
258 strcpy(httpauth_pass, DEFAULT_HTTPAUTH_PASS);
261 * Find out what it is that the web browser is asking for
263 HeaderName = NewStrBuf();
266 HTTPHeaders = NewHash(1, NULL);
269 * Read in the request
276 if (ClientGetLine(sock, Line, Buf, &Pos) < 0) return;
278 LineLen = StrLength(Line);
289 /* Do we need to Unfold? */
290 if ((LastLine != NULL) &&
291 (isspace(*ChrPtr(Line)))) {
292 pch = pchs = ChrPtr(Line);
293 pche = pchs + StrLength(Line);
294 while (isspace(*pch) && (pch < pche))
296 StrBufCutLeft(Line, pch - pchs);
297 StrBufAppendBuf(LastLine, Line, 0);
302 StrBufSanitizeAscii(Line, '§');
303 StrBufExtract_token(HeaderName, Line, 0, ':');
306 pch = pchs + StrLength(HeaderName) + 1;
307 pche = pchs + StrLength(Line);
308 while (isspace(*pch) && (pch < pche))
310 StrBufCutLeft(Line, pch - pchs);
312 StrBufUpCase(HeaderName);
313 Put(HTTPHeaders, SKEY(HeaderName), Line, HFreeStrBuf);
315 } while (LineLen > 0);
316 FreeStrBuf(&HeaderName);
318 /* dbg_PrintHash(HTTPHeaders, nix, NULL); */
324 if (GetHash(HTTPHeaders, HKEY("ACCEPT-ENCODING"), &vLine) &&
326 buf = ChrPtr((StrBuf*)vLine);
327 if (strstr(&buf[16], "gzip")) {
333 * Browser-based sessions use cookies for session authentication
335 if (GetHash(HTTPHeaders, HKEY("COOKIE"), &vLine) &&
337 cookie_to_stuff(vLine, &desired_session,
343 * GroupDAV-based sessions use HTTP authentication
345 if (GetHash(HTTPHeaders, HKEY("AUTHORIZATION"), &vLine) &&
347 Line = (StrBuf*)vLine;
348 if (strncasecmp(ChrPtr(Line), "Basic", 5) == 0) {
349 StrBufCutLeft(Line, 6);
350 CtdlDecodeBase64(httpauth_string, ChrPtr(Line), StrLength(Line));
351 extract_token(httpauth_user, httpauth_string, 0, ':', sizeof httpauth_user);
352 extract_token(httpauth_pass, httpauth_string, 1, ':', sizeof httpauth_pass);
355 lprintf(1, "Authentication scheme not supported! [%s]\n", ChrPtr(Line));
358 if (GetHash(HTTPHeaders, HKEY("IF-MODIFIED-SINCE"), &vLine) &&
360 if_modified_since = httpdate_to_timestamp((StrBuf*)vLine);
365 ReqType = NewStrBuf();
366 HTTPVersion = NewStrBuf();
367 StrBufExtract_token(HTTPVersion, ReqLine, 2, ' ');
368 StrBufExtract_token(ReqType, ReqLine, 0, ' ');
369 StrBufCutLeft(ReqLine, StrLength(ReqType) + 1);
370 StrBufCutRight(ReqLine, StrLength(HTTPVersion) + 1);
373 * If the request is prefixed by "/webcit" then chop that off. This
374 * allows a front end web server to forward all /webcit requests to us
375 * while still using the same web server port for other things.
377 if ( (StrLength(ReqLine) >= 8) && (strstr(ChrPtr(ReqLine), "/webcit/")) ) {
378 StrBufCutLeft(ReqLine, 7);
381 /* Begin parsing the request. */
383 if ((strncmp(ChrPtr(ReqLine), "/sslg", 5) != 0) &&
384 (strncmp(ChrPtr(ReqLine), "/static/", 8) != 0) &&
385 (strncmp(ChrPtr(ReqLine), "/tiny_mce/", 10) != 0) &&
386 (strncmp(ChrPtr(ReqLine), "/wholist_section", 16) != 0) &&
387 (strstr(ChrPtr(ReqLine), "wholist_section") == NULL)) {
389 lprintf(5, "HTTP: %s %s %s\n", ChrPtr(ReqType), ChrPtr(ReqLine), ChrPtr(HTTPVersion));
394 /** Check for bogus requests */
395 if ((StrLength(HTTPVersion) == 0) ||
396 (StrLength(ReqType) == 0) ||
398 StrBufPlain(ReqLine, HKEY("/404 HTTP/1.1"));
399 StrBufPlain(ReqType, HKEY("GET"));
401 FreeStrBuf(&HTTPVersion);
404 * While we're at it, gracefully handle requests for the
405 * robots.txt and favicon.ico files.
407 if (!strncasecmp(ChrPtr(ReqLine), "/robots.txt", 11)) {
409 HKEY("/static/robots.txt"
410 "?force_close_session=yes HTTP/1.1"));
411 StrBufPlain(ReqType, HKEY("GET"));
413 else if (!strncasecmp(ChrPtr(ReqLine), "/favicon.ico", 12)) {
414 StrBufPlain(ReqLine, HKEY("/static/favicon.ico"));
415 StrBufPlain(ReqType, HKEY("GET"));
419 * These are the URL's which may be executed without a
420 * session cookie already set. If it's not one of these,
421 * force the session to close because cookies are
422 * probably disabled on the client browser.
424 else if ( (StrLength(ReqLine) > 1 )
425 && (strncasecmp(ChrPtr(ReqLine), "/listsub", 8))
426 && (strncasecmp(ChrPtr(ReqLine), "/freebusy", 9))
427 && (strncasecmp(ChrPtr(ReqLine), "/do_logout", 10))
428 && (strncasecmp(ChrPtr(ReqLine), "/groupdav", 9))
429 && (strncasecmp(ChrPtr(ReqLine), "/static", 7))
430 && (strncasecmp(ChrPtr(ReqLine), "/rss", 4))
431 && (strncasecmp(ChrPtr(ReqLine), "/404", 4))
432 && (got_cookie == 0)) {
434 HKEY("/static/nocookies.html"
435 "?force_close_session=yes"));
439 * See if there's an existing session open with the desired ID or user/pass
443 if (TheSession == NULL) {
444 pthread_mutex_lock(&SessionListMutex);
445 for (sptr = SessionList;
446 ((sptr != NULL) && (TheSession == NULL));
449 /** If HTTP-AUTH, look for a session with matching credentials */
450 if ( (!IsEmptyStr(httpauth_user))
451 &&(!strcasecmp(ChrPtr(sptr->httpauth_user), httpauth_user))
452 &&(!strcasecmp(ChrPtr(sptr->httpauth_pass), httpauth_pass)) ) {
456 /** If cookie-session, look for a session with matching session ID */
457 if ( (desired_session != 0) && (sptr->wc_session == desired_session)) {
462 pthread_mutex_unlock(&SessionListMutex);
466 * Create a new session if we have to
468 if (TheSession == NULL) {
469 lprintf(3, "Creating a new session\n");
470 TheSession = (wcsession *)
471 malloc(sizeof(wcsession));
472 memset(TheSession, 0, sizeof(wcsession));
473 TheSession->headers = HTTPHeaders;
474 TheSession->serv_sock = (-1);
475 TheSession->chat_sock = (-1);
477 /* If we're recreating a session that expired, it's best to give it the same
478 * session number that it had before. The client browser ought to pick up
479 * the new session number and start using it, but in some rare situations it
480 * doesn't, and that's a Bad Thing because it causes lots of spurious sessions
483 if (desired_session == 0) {
484 TheSession->wc_session = GenerateSessionID();
487 TheSession->wc_session = desired_session;
490 TheSession->httpauth_user = NewStrBufPlain(httpauth_user, -1);
491 TheSession->httpauth_pass = NewStrBufPlain(httpauth_user, -1);
493 pthread_setspecific(MyConKey, (void *)TheSession);
494 session_new_modules(TheSession);
496 pthread_mutex_init(&TheSession->SessionMutex, NULL);
497 pthread_mutex_lock(&SessionListMutex);
498 TheSession->nonce = rand();
499 TheSession->next = SessionList;
500 TheSession->is_mobile = -1;
501 SessionList = TheSession;
502 pthread_mutex_unlock(&SessionListMutex);
505 TheSession->headers = HTTPHeaders;
508 * A future improvement might be to check the session integrity
509 * at this point before continuing.
513 * Bind to the session and perform the transaction
515 pthread_mutex_lock(&TheSession->SessionMutex); /* bind */
516 pthread_setspecific(MyConKey, (void *)TheSession);
518 TheSession->lastreq = time(NULL); /* log */
519 TheSession->http_sock = *sock;
520 TheSession->gzip_ok = gzip_ok;
522 session_attach_modules(TheSession);
524 session_loop(ReqLine, ReqType, Buf, &Pos); /* do transaction */
525 session_detach_modules(TheSession);
527 TheSession->headers = NULL;
528 pthread_mutex_unlock(&TheSession->SessionMutex); /* unbind */
530 /* Free the request buffer */
531 DeleteHash(&HTTPHeaders);
532 FreeStrBuf(&ReqLine);
533 FreeStrBuf(&ReqType);
537 void tmplput_nonce(StrBuf *Target, WCTemplputParams *TP)
540 StrBufAppendPrintf(Target, "%ld",
541 (WCC != NULL)? WCC->nonce:0);
544 void tmplput_current_user(StrBuf *Target, WCTemplputParams *TP)
546 StrBufAppendTemplate(Target, TP, WC->wc_fullname, 0);
549 void tmplput_current_room(StrBuf *Target, WCTemplputParams *TP)
551 StrBufAppendTemplate(Target, TP, WC->wc_roomname, 0);
560 RegisterNamespace("CURRENT_USER", 0, 1, tmplput_current_user, CTX_NONE);
561 RegisterNamespace("CURRENT_ROOM", 0, 1, tmplput_current_room, CTX_NONE);
562 RegisterNamespace("NONCE", 0, 0, tmplput_nonce, 0);