208521386eb318cefe76e92a27da39b1a5a6462c
[citadel.git] / webcit / context_loop.c
1 /*
2  * $Id$
3  *
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.
8  *
9  */
10
11 #include "webcit.h"
12 #include "webserver.h"
13 #include "modules_init.h"
14
15 /* Only one thread may manipulate SessionList at a time... */
16 pthread_mutex_t SessionListMutex;
17
18 wcsession *SessionList = NULL; /**< our sessions ????*/
19
20 pthread_key_t MyConKey;         /**< TSD key for MySession() */
21
22
23
24 void DestroySession(wcsession **sessions_to_kill)
25 {
26         close((*sessions_to_kill)->serv_sock);
27         close((*sessions_to_kill)->chat_sock);
28 /*
29 //              if ((*sessions_to_kill)->preferences != NULL) {
30 //                      free((*sessions_to_kill)->preferences);
31 //              }
32 */
33         if ((*sessions_to_kill)->cache_fold != NULL) {
34                 free((*sessions_to_kill)->cache_fold);
35         }
36         DeleteServInfo(&((*sessions_to_kill)->serv_info));
37         free_march_list((*sessions_to_kill));
38         
39         session_destroy_modules(*sessions_to_kill);
40
41         free((*sessions_to_kill));
42         (*sessions_to_kill) = NULL;
43 }
44
45 void shutdown_sessions(void)
46 {
47         wcsession *sptr;
48         
49         for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
50                         sptr->killthis = 1;
51         }
52 }
53
54 void do_housekeeping(void)
55 {
56         wcsession *sptr, *ss;
57         wcsession *sessions_to_kill = NULL;
58         int num_sessions = 0;
59         static int num_threads = MIN_WORKER_THREADS;
60
61         /**
62          * Lock the session list, moving any candidates for euthanasia into
63          * a separate list.
64          */
65         pthread_mutex_lock(&SessionListMutex);
66         num_sessions = 0;
67         for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
68                 ++num_sessions;
69
70                 /** Kill idle sessions */
71                 if ((time(NULL) - (sptr->lastreq)) >
72                    (time_t) WEBCIT_TIMEOUT) {
73                         sptr->killthis = 1;
74                 }
75
76                 /** Remove sessions flagged for kill */
77                 if (sptr->killthis) {
78
79                         /** remove session from linked list */
80                         if (sptr == SessionList) {
81                                 SessionList = SessionList->next;
82                         }
83                         else for (ss=SessionList;ss!=NULL;ss=ss->next) {
84                                 if (ss->next == sptr) {
85                                         ss->next = ss->next->next;
86                                 }
87                         }
88
89                         sptr->next = sessions_to_kill;
90                         sessions_to_kill = sptr;
91                 }
92         }
93         pthread_mutex_unlock(&SessionListMutex);
94
95         /**
96          * Now free up and destroy the culled sessions.
97          */
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;
103
104                 DestroySession(&sessions_to_kill);
105                 sessions_to_kill = sptr;
106                 --num_sessions;
107         }
108
109         /**
110          * If there are more sessions than threads, then we should spawn
111          * more threads ... up to a predefined maximum.
112          */
113         while ( (num_sessions > num_threads)
114               && (num_threads <= MAX_WORKER_THREADS) ) {
115                 spawn_another_worker_thread();
116                 ++num_threads;
117                 lprintf(3, "There are %d sessions and %d threads active.\n",
118                         num_sessions, num_threads);
119         }
120 }
121
122
123 /*
124  * Wake up occasionally and clean house
125  */
126 void housekeeping_loop(void)
127 {
128         while (1) {
129                 sleeeeeeeeeep(HOUSEKEEPING);
130                 do_housekeeping();
131         }
132 }
133
134
135 /*
136  * Create a Session id
137  * Generate a unique WebCit session ID (which is not the same thing as the
138  * Citadel session ID).
139  */
140 int GenerateSessionID(void)
141 {
142         static int seq = (-1);
143
144         if (seq < 0) {
145                 seq = (int) time(NULL);
146         }
147                 
148         return ++seq;
149 }
150
151
152 /*
153  * lingering_close() a`la Apache. see
154  * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
155  */
156 int lingering_close(int fd)
157 {
158         char buf[SIZ];
159         int i;
160         fd_set set;
161         struct timeval tv, start;
162
163         gettimeofday(&start, NULL);
164         shutdown(fd, 1);
165         do {
166                 do {
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) {
171                                 tv.tv_sec--;
172                                 tv.tv_usec += 1000000;
173                         }
174                         FD_ZERO(&set);
175                         FD_SET(fd, &set);
176                         i = select(fd + 1, &set, NULL, NULL, &tv);
177                 } while (i == -1 && errno == EINTR);
178
179                 if (i <= 0)
180                         break;
181
182                 i = read(fd, buf, sizeof buf);
183         } while (i != 0 && (i != -1 || errno == EINTR));
184
185         return close(fd);
186 }
187
188
189
190 /*
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.
193  */
194 int is_bogus(StrBuf *http_cmd) {
195         const char *url;
196         int i, max;
197         const char *bogus_prefixes[] = {
198                 "/scripts/root.exe",    /* Worms and trojans and viruses, oh my! */
199                 "/c/winnt",
200                 "/MSADC/",
201                 "/_vti",                /* Broken Microsoft DAV implementation */
202                 "/MSOffice",            /* Stoopid MSOffice thinks everyone is IIS */
203                 "/nonexistenshit"       /* Exploit found in the wild January 2009 */
204         };
205
206         url = ChrPtr(http_cmd);
207         if (IsEmptyStr(url)) return(1);
208         ++url;
209
210         max = sizeof(bogus_prefixes) / sizeof(char *);
211
212         for (i=0; i<max; ++i) {
213                 if (!strncasecmp(url, bogus_prefixes[i], strlen(bogus_prefixes[i]))) {
214                         return(2);
215                 }
216         }
217
218         return(0);      /* probably ok */
219 }
220
221
222 /*const char *nix(void *vptr) {return ChrPtr( (StrBuf*)vptr);}*/
223
224 /*
225  * handle one request
226  *
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.
230  *
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
235  * transaction.
236  */
237 void context_loop(int *sock)
238 {
239         const char *Pos = NULL;
240         const char *buf;
241         int desired_session = 0;
242         int got_cookie = 0;
243         int gzip_ok = 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;
249         int nLine = 0;
250         int LineLen;
251         void *vLine;
252         StrBuf *Buf, *Line, *LastLine, *HeaderName, *ReqLine, *ReqType, *HTTPVersion;
253         const char *pch, *pchs, *pche;
254         HashList *HTTPHeaders;
255
256         strcpy(httpauth_string, "");
257         strcpy(httpauth_user, DEFAULT_HTTPAUTH_USER);
258         strcpy(httpauth_pass, DEFAULT_HTTPAUTH_PASS);
259
260         /*
261          * Find out what it is that the web browser is asking for
262          */
263         HeaderName = NewStrBuf();
264         Buf = NewStrBuf();
265         LastLine = NULL;
266         HTTPHeaders = NewHash(1, NULL);
267
268         /*
269          * Read in the request
270          */
271         do {
272                 nLine ++;
273                 Line = NewStrBuf();
274
275
276                 if (ClientGetLine(sock, Line, Buf, &Pos) < 0) return;
277
278                 LineLen = StrLength(Line);
279
280                 if (nLine == 1) {
281                         ReqLine = Line;
282                         continue;
283                 }
284                 if (LineLen == 0) {
285                         FreeStrBuf(&Line);
286                         continue;
287                 }
288
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))
295                                 pch ++;
296                         StrBufCutLeft(Line, pch - pchs);
297                         StrBufAppendBuf(LastLine, Line, 0);
298                         FreeStrBuf(&Line);
299                         continue;
300                 }
301
302                 StrBufSanitizeAscii(Line, '§');
303                 StrBufExtract_token(HeaderName, Line, 0, ':');
304
305                 pchs = ChrPtr(Line);
306                 pch = pchs + StrLength(HeaderName) + 1;
307                 pche = pchs + StrLength(Line);
308                 while (isspace(*pch) && (pch < pche))
309                         pch ++;
310                 StrBufCutLeft(Line, pch - pchs);
311
312                 StrBufUpCase(HeaderName);
313                 Put(HTTPHeaders, SKEY(HeaderName), Line, HFreeStrBuf);
314                 LastLine = Line;
315         } while (LineLen > 0);
316         FreeStrBuf(&HeaderName);
317
318 /*      dbg_PrintHash(HTTPHeaders, nix, NULL);  */
319
320
321         /*
322          * Can we compress?
323          */
324         if (GetHash(HTTPHeaders, HKEY("ACCEPT-ENCODING"), &vLine) && 
325             (vLine != NULL)) {
326                 buf = ChrPtr((StrBuf*)vLine);
327                 if (strstr(&buf[16], "gzip")) {
328                         gzip_ok = 1;
329                 }
330         }
331
332         /*
333          * Browser-based sessions use cookies for session authentication
334          */
335         if (GetHash(HTTPHeaders, HKEY("COOKIE"), &vLine) && 
336             (vLine != NULL)) {
337                 cookie_to_stuff(vLine, &desired_session,
338                                 NULL, NULL, NULL);
339                 got_cookie = 1;
340         }
341
342         /*
343          * GroupDAV-based sessions use HTTP authentication
344          */
345         if (GetHash(HTTPHeaders, HKEY("AUTHORIZATION"), &vLine) && 
346             (vLine != NULL)) {
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);
353                 }
354                 else 
355                         lprintf(1, "Authentication scheme not supported! [%s]\n", ChrPtr(Line));
356         }
357
358         if (GetHash(HTTPHeaders, HKEY("IF-MODIFIED-SINCE"), &vLine) && 
359             (vLine != NULL)) {
360                 if_modified_since = httpdate_to_timestamp((StrBuf*)vLine);
361         }
362
363
364
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);
371
372         /*
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.
376          */
377         if ( (StrLength(ReqLine) >= 8) && (strstr(ChrPtr(ReqLine), "/webcit/")) ) {
378                 StrBufCutLeft(ReqLine, 7);
379         }
380
381         /* Begin parsing the request. */
382 #ifdef TECH_PREVIEW
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)) {
388 #endif
389                 lprintf(5, "HTTP: %s %s %s\n", ChrPtr(ReqType), ChrPtr(ReqLine), ChrPtr(HTTPVersion));
390 #ifdef TECH_PREVIEW
391         }
392 #endif
393
394         /** Check for bogus requests */
395         if ((StrLength(HTTPVersion) == 0) ||
396             (StrLength(ReqType) == 0) || 
397             is_bogus(ReqLine)) {
398                 StrBufPlain(ReqLine, HKEY("/404 HTTP/1.1"));
399                 StrBufPlain(ReqType, HKEY("GET"));
400         }
401         FreeStrBuf(&HTTPVersion);
402
403         /**
404          * While we're at it, gracefully handle requests for the
405          * robots.txt and favicon.ico files.
406          */
407         if (!strncasecmp(ChrPtr(ReqLine), "/robots.txt", 11)) {
408                 StrBufPlain(ReqLine, 
409                             HKEY("/static/robots.txt"
410                                  "?force_close_session=yes HTTP/1.1"));
411                 StrBufPlain(ReqType, HKEY("GET"));
412         }
413         else if (!strncasecmp(ChrPtr(ReqLine), "/favicon.ico", 12)) {
414                 StrBufPlain(ReqLine, HKEY("/static/favicon.ico"));
415                 StrBufPlain(ReqType, HKEY("GET"));
416         }
417
418         /**
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.
423          */
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)) {
433                 StrBufPlain(ReqLine, 
434                             HKEY("/static/nocookies.html"
435                                  "?force_close_session=yes"));
436         }
437
438         /**
439          * See if there's an existing session open with the desired ID or user/pass
440          */
441         TheSession = NULL;
442
443         if (TheSession == NULL) {
444                 pthread_mutex_lock(&SessionListMutex);
445                 for (sptr = SessionList; 
446                      ((sptr != NULL) && (TheSession == NULL)); 
447                       sptr = sptr->next) {
448
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)) ) {
453                                 TheSession = sptr;
454                         }
455
456                         /** If cookie-session, look for a session with matching session ID */
457                         if ( (desired_session != 0) && (sptr->wc_session == desired_session)) {
458                                 TheSession = sptr;
459                         }
460
461                 }
462                 pthread_mutex_unlock(&SessionListMutex);
463         }
464
465         /**
466          * Create a new session if we have to
467          */
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);
476         
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
481                  * to get created.
482                  */     
483                 if (desired_session == 0) {
484                         TheSession->wc_session = GenerateSessionID();
485                 }
486                 else {
487                         TheSession->wc_session = desired_session;
488                 }
489
490                 TheSession->httpauth_user = NewStrBufPlain(httpauth_user, -1);
491                 TheSession->httpauth_pass = NewStrBufPlain(httpauth_user, -1);
492
493                 pthread_setspecific(MyConKey, (void *)TheSession);
494                 session_new_modules(TheSession);
495
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);
503                 session_is_new = 1;
504         }
505         TheSession->headers = HTTPHeaders;
506
507         /*
508          * A future improvement might be to check the session integrity
509          * at this point before continuing.
510          */
511
512         /*
513          * Bind to the session and perform the transaction
514          */
515         pthread_mutex_lock(&TheSession->SessionMutex);          /* bind */
516         pthread_setspecific(MyConKey, (void *)TheSession);
517         
518         TheSession->lastreq = time(NULL);                       /* log */
519         TheSession->http_sock = *sock;
520         TheSession->gzip_ok = gzip_ok;
521
522         session_attach_modules(TheSession);
523
524         session_loop(ReqLine, ReqType, Buf, &Pos);                              /* do transaction */
525         session_detach_modules(TheSession);
526
527         TheSession->headers = NULL;
528         pthread_mutex_unlock(&TheSession->SessionMutex);        /* unbind */
529
530         /* Free the request buffer */
531         DeleteHash(&HTTPHeaders);
532         FreeStrBuf(&ReqLine);
533         FreeStrBuf(&ReqType);
534         FreeStrBuf(&Buf);
535 }
536
537 void tmplput_nonce(StrBuf *Target, WCTemplputParams *TP)
538 {
539         wcsession *WCC = WC;
540         StrBufAppendPrintf(Target, "%ld",
541                            (WCC != NULL)? WCC->nonce:0);                   
542 }
543
544 void tmplput_current_user(StrBuf *Target, WCTemplputParams *TP)
545 {
546         StrBufAppendTemplate(Target, TP, WC->wc_fullname, 0);
547 }
548
549 void tmplput_current_room(StrBuf *Target, WCTemplputParams *TP)
550 {
551         StrBufAppendTemplate(Target, TP, WC->wc_roomname, 0); 
552 }
553
554
555
556 void 
557 InitModule_CONTEXT
558 (void)
559 {
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);
563 }