* shrinked the rest of these shutdown stuff into the module-handler.
[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 void shutdown_sessions(void)
24 {
25         wcsession *sptr;
26         
27         for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
28                         sptr->killthis = 1;
29         }
30 }
31
32 void do_housekeeping(void)
33 {
34         wcsession *sptr, *ss;
35         wcsession *sessions_to_kill = NULL;
36         int num_sessions = 0;
37         static int num_threads = MIN_WORKER_THREADS;
38
39         /**
40          * Lock the session list, moving any candidates for euthanasia into
41          * a separate list.
42          */
43         pthread_mutex_lock(&SessionListMutex);
44         num_sessions = 0;
45         for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
46                 ++num_sessions;
47
48                 /** Kill idle sessions */
49                 if ((time(NULL) - (sptr->lastreq)) >
50                    (time_t) WEBCIT_TIMEOUT) {
51                         sptr->killthis = 1;
52                 }
53
54                 /** Remove sessions flagged for kill */
55                 if (sptr->killthis) {
56
57                         /** remove session from linked list */
58                         if (sptr == SessionList) {
59                                 SessionList = SessionList->next;
60                         }
61                         else for (ss=SessionList;ss!=NULL;ss=ss->next) {
62                                 if (ss->next == sptr) {
63                                         ss->next = ss->next->next;
64                                 }
65                         }
66
67                         sptr->next = sessions_to_kill;
68                         sessions_to_kill = sptr;
69                 }
70         }
71         pthread_mutex_unlock(&SessionListMutex);
72
73         /**
74          * Now free up and destroy the culled sessions.
75          */
76         while (sessions_to_kill != NULL) {
77                 lprintf(3, "Destroying session %d\n", sessions_to_kill->wc_session);
78                 pthread_mutex_lock(&sessions_to_kill->SessionMutex);
79                 pthread_mutex_unlock(&sessions_to_kill->SessionMutex);
80                 sptr = sessions_to_kill->next;
81
82                 session_destroy_modules(&sessions_to_kill);
83                 sessions_to_kill = sptr;
84                 --num_sessions;
85         }
86
87         /**
88          * If there are more sessions than threads, then we should spawn
89          * more threads ... up to a predefined maximum.
90          */
91         while ( (num_sessions > num_threads)
92               && (num_threads <= MAX_WORKER_THREADS) ) {
93                 spawn_another_worker_thread();
94                 ++num_threads;
95                 lprintf(3, "There are %d sessions and %d threads active.\n",
96                         num_sessions, num_threads);
97         }
98 }
99
100
101 /*
102  * Wake up occasionally and clean house
103  */
104 void housekeeping_loop(void)
105 {
106         while (1) {
107                 sleeeeeeeeeep(HOUSEKEEPING);
108                 do_housekeeping();
109         }
110 }
111
112
113 /*
114  * Create a Session id
115  * Generate a unique WebCit session ID (which is not the same thing as the
116  * Citadel session ID).
117  */
118 int GenerateSessionID(void)
119 {
120         static int seq = (-1);
121
122         if (seq < 0) {
123                 seq = (int) time(NULL);
124         }
125                 
126         return ++seq;
127 }
128
129
130 /*
131  * lingering_close() a`la Apache. see
132  * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
133  */
134 int lingering_close(int fd)
135 {
136         char buf[SIZ];
137         int i;
138         fd_set set;
139         struct timeval tv, start;
140
141         gettimeofday(&start, NULL);
142         shutdown(fd, 1);
143         do {
144                 do {
145                         gettimeofday(&tv, NULL);
146                         tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
147                         tv.tv_usec = start.tv_usec - tv.tv_usec;
148                         if (tv.tv_usec < 0) {
149                                 tv.tv_sec--;
150                                 tv.tv_usec += 1000000;
151                         }
152                         FD_ZERO(&set);
153                         FD_SET(fd, &set);
154                         i = select(fd + 1, &set, NULL, NULL, &tv);
155                 } while (i == -1 && errno == EINTR);
156
157                 if (i <= 0)
158                         break;
159
160                 i = read(fd, buf, sizeof buf);
161         } while (i != 0 && (i != -1 || errno == EINTR));
162
163         return close(fd);
164 }
165
166
167
168 /*
169  * Look for commonly-found probes of malware such as worms, viruses, trojans, and Microsoft Office.
170  * Short-circuit these requests so we don't have to send them through the full processing loop.
171  */
172 int is_bogus(StrBuf *http_cmd) {
173         const char *url;
174         int i, max;
175         const char *bogus_prefixes[] = {
176                 "/scripts/root.exe",    /* Worms and trojans and viruses, oh my! */
177                 "/c/winnt",
178                 "/MSADC/",
179                 "/_vti",                /* Broken Microsoft DAV implementation */
180                 "/MSOffice",            /* Stoopid MSOffice thinks everyone is IIS */
181                 "/nonexistenshit"       /* Exploit found in the wild January 2009 */
182         };
183
184         url = ChrPtr(http_cmd);
185         if (IsEmptyStr(url)) return(1);
186         ++url;
187
188         max = sizeof(bogus_prefixes) / sizeof(char *);
189
190         for (i=0; i<max; ++i) {
191                 if (!strncasecmp(url, bogus_prefixes[i], strlen(bogus_prefixes[i]))) {
192                         return(2);
193                 }
194         }
195
196         return(0);      /* probably ok */
197 }
198
199
200 /*const char *nix(void *vptr) {return ChrPtr( (StrBuf*)vptr);}*/
201
202 /*
203  * handle one request
204  *
205  * This loop gets called once for every HTTP connection made to WebCit.  At
206  * this entry point we have an HTTP socket with a browser allegedly on the
207  * other end, but we have not yet bound to a WebCit session.
208  *
209  * The job of this function is to locate the correct session and bind to it,
210  * or create a session if necessary and bind to it, then run the WebCit
211  * transaction loop.  Afterwards, we unbind from the session.  When this
212  * function returns, the worker thread is then free to handle another
213  * transaction.
214  */
215 void context_loop(int *sock)
216 {
217         const char *Pos = NULL;
218         const char *buf;
219         int desired_session = 0;
220         int got_cookie = 0;
221         int gzip_ok = 0;
222         wcsession *TheSession, *sptr;
223         char httpauth_string[1024];
224         char httpauth_user[1024];
225         char httpauth_pass[1024];
226         int session_is_new = 0;
227         int nLine = 0;
228         int LineLen;
229         void *vLine;
230         StrBuf *Buf, *Line, *LastLine, *HeaderName, *ReqLine, *ReqType, *HTTPVersion;
231         const char *pch, *pchs, *pche;
232         HashList *HTTPHeaders;
233
234         strcpy(httpauth_string, "");
235         strcpy(httpauth_user, DEFAULT_HTTPAUTH_USER);
236         strcpy(httpauth_pass, DEFAULT_HTTPAUTH_PASS);
237
238         /*
239          * Find out what it is that the web browser is asking for
240          */
241         HeaderName = NewStrBuf();
242         Buf = NewStrBuf();
243         LastLine = NULL;
244         HTTPHeaders = NewHash(1, NULL);
245
246         /*
247          * Read in the request
248          */
249         do {
250                 nLine ++;
251                 Line = NewStrBuf();
252
253
254                 if (ClientGetLine(sock, Line, Buf, &Pos) < 0) return;
255
256                 LineLen = StrLength(Line);
257
258                 if (nLine == 1) {
259                         ReqLine = Line;
260                         continue;
261                 }
262                 if (LineLen == 0) {
263                         FreeStrBuf(&Line);
264                         continue;
265                 }
266
267                 /* Do we need to Unfold? */
268                 if ((LastLine != NULL) && 
269                     (isspace(*ChrPtr(Line)))) {
270                         pch = pchs = ChrPtr(Line);
271                         pche = pchs + StrLength(Line);
272                         while (isspace(*pch) && (pch < pche))
273                                 pch ++;
274                         StrBufCutLeft(Line, pch - pchs);
275                         StrBufAppendBuf(LastLine, Line, 0);
276                         FreeStrBuf(&Line);
277                         continue;
278                 }
279
280                 StrBufSanitizeAscii(Line, '§');
281                 StrBufExtract_token(HeaderName, Line, 0, ':');
282
283                 pchs = ChrPtr(Line);
284                 pch = pchs + StrLength(HeaderName) + 1;
285                 pche = pchs + StrLength(Line);
286                 while (isspace(*pch) && (pch < pche))
287                         pch ++;
288                 StrBufCutLeft(Line, pch - pchs);
289
290                 StrBufUpCase(HeaderName);
291                 Put(HTTPHeaders, SKEY(HeaderName), Line, HFreeStrBuf);
292                 LastLine = Line;
293         } while (LineLen > 0);
294         FreeStrBuf(&HeaderName);
295
296 /*      dbg_PrintHash(HTTPHeaders, nix, NULL);  */
297
298
299         /*
300          * Can we compress?
301          */
302         if (GetHash(HTTPHeaders, HKEY("ACCEPT-ENCODING"), &vLine) && 
303             (vLine != NULL)) {
304                 buf = ChrPtr((StrBuf*)vLine);
305                 if (strstr(&buf[16], "gzip")) {
306                         gzip_ok = 1;
307                 }
308         }
309
310         /*
311          * Browser-based sessions use cookies for session authentication
312          */
313         if (GetHash(HTTPHeaders, HKEY("COOKIE"), &vLine) && 
314             (vLine != NULL)) {
315                 cookie_to_stuff(vLine, &desired_session,
316                                 NULL, NULL, NULL);
317                 got_cookie = 1;
318         }
319
320         /*
321          * GroupDAV-based sessions use HTTP authentication
322          */
323         if (GetHash(HTTPHeaders, HKEY("AUTHORIZATION"), &vLine) && 
324             (vLine != NULL)) {
325                 Line = (StrBuf*)vLine;
326                 if (strncasecmp(ChrPtr(Line), "Basic", 5) == 0) {
327                         StrBufCutLeft(Line, 6);
328                         CtdlDecodeBase64(httpauth_string, ChrPtr(Line), StrLength(Line));
329                         extract_token(httpauth_user, httpauth_string, 0, ':', sizeof httpauth_user);
330                         extract_token(httpauth_pass, httpauth_string, 1, ':', sizeof httpauth_pass);
331                 }
332                 else 
333                         lprintf(1, "Authentication scheme not supported! [%s]\n", ChrPtr(Line));
334         }
335
336         if (GetHash(HTTPHeaders, HKEY("IF-MODIFIED-SINCE"), &vLine) && 
337             (vLine != NULL)) {
338                 if_modified_since = httpdate_to_timestamp((StrBuf*)vLine);
339         }
340
341
342
343         ReqType = NewStrBuf();
344         HTTPVersion = NewStrBuf();
345         StrBufExtract_token(HTTPVersion, ReqLine, 2, ' ');
346         StrBufExtract_token(ReqType, ReqLine, 0, ' ');
347         StrBufCutLeft(ReqLine, StrLength(ReqType) + 1);
348         StrBufCutRight(ReqLine, StrLength(HTTPVersion) + 1);
349
350         /*
351          * If the request is prefixed by "/webcit" then chop that off.  This
352          * allows a front end web server to forward all /webcit requests to us
353          * while still using the same web server port for other things.
354          */
355         if ( (StrLength(ReqLine) >= 8) && (strstr(ChrPtr(ReqLine), "/webcit/")) ) {
356                 StrBufCutLeft(ReqLine, 7);
357         }
358
359         /* Begin parsing the request. */
360 #ifdef TECH_PREVIEW
361         if ((strncmp(ChrPtr(ReqLine), "/sslg", 5) != 0) &&
362             (strncmp(ChrPtr(ReqLine), "/static/", 8) != 0) &&
363             (strncmp(ChrPtr(ReqLine), "/tiny_mce/", 10) != 0) &&
364             (strncmp(ChrPtr(ReqLine), "/wholist_section", 16) != 0) &&
365             (strstr(ChrPtr(ReqLine), "wholist_section") == NULL)) {
366 #endif
367                 lprintf(5, "HTTP: %s %s %s\n", ChrPtr(ReqType), ChrPtr(ReqLine), ChrPtr(HTTPVersion));
368 #ifdef TECH_PREVIEW
369         }
370 #endif
371
372         /** Check for bogus requests */
373         if ((StrLength(HTTPVersion) == 0) ||
374             (StrLength(ReqType) == 0) || 
375             is_bogus(ReqLine)) {
376                 StrBufPlain(ReqLine, HKEY("/404 HTTP/1.1"));
377                 StrBufPlain(ReqType, HKEY("GET"));
378         }
379         FreeStrBuf(&HTTPVersion);
380
381         /**
382          * While we're at it, gracefully handle requests for the
383          * robots.txt and favicon.ico files.
384          */
385         if (!strncasecmp(ChrPtr(ReqLine), "/robots.txt", 11)) {
386                 StrBufPlain(ReqLine, 
387                             HKEY("/static/robots.txt"
388                                  "?force_close_session=yes HTTP/1.1"));
389                 StrBufPlain(ReqType, HKEY("GET"));
390         }
391         else if (!strncasecmp(ChrPtr(ReqLine), "/favicon.ico", 12)) {
392                 StrBufPlain(ReqLine, HKEY("/static/favicon.ico"));
393                 StrBufPlain(ReqType, HKEY("GET"));
394         }
395
396         /**
397          * These are the URL's which may be executed without a
398          * session cookie already set.  If it's not one of these,
399          * force the session to close because cookies are
400          * probably disabled on the client browser.
401          */
402         else if ( (StrLength(ReqLine) > 1 )
403                 && (strncasecmp(ChrPtr(ReqLine), "/listsub", 8))
404                 && (strncasecmp(ChrPtr(ReqLine), "/freebusy", 9))
405                 && (strncasecmp(ChrPtr(ReqLine), "/do_logout", 10))
406                 && (strncasecmp(ChrPtr(ReqLine), "/groupdav", 9))
407                 && (strncasecmp(ChrPtr(ReqLine), "/static", 7))
408                 && (strncasecmp(ChrPtr(ReqLine), "/rss", 4))
409                 && (strncasecmp(ChrPtr(ReqLine), "/404", 4))
410                 && (got_cookie == 0)) {
411                 StrBufPlain(ReqLine, 
412                             HKEY("/static/nocookies.html"
413                                  "?force_close_session=yes"));
414         }
415
416         /**
417          * See if there's an existing session open with the desired ID or user/pass
418          */
419         TheSession = NULL;
420
421         if (TheSession == NULL) {
422                 pthread_mutex_lock(&SessionListMutex);
423                 for (sptr = SessionList; 
424                      ((sptr != NULL) && (TheSession == NULL)); 
425                       sptr = sptr->next) {
426
427                         /** If HTTP-AUTH, look for a session with matching credentials */
428                         if ( (!IsEmptyStr(httpauth_user))
429                              &&(!strcasecmp(ChrPtr(sptr->httpauth_user), httpauth_user))
430                              &&(!strcasecmp(ChrPtr(sptr->httpauth_pass), httpauth_pass)) ) {
431                                 TheSession = sptr;
432                         }
433
434                         /** If cookie-session, look for a session with matching session ID */
435                         if ( (desired_session != 0) && (sptr->wc_session == desired_session)) {
436                                 TheSession = sptr;
437                         }
438
439                 }
440                 pthread_mutex_unlock(&SessionListMutex);
441         }
442
443         /**
444          * Create a new session if we have to
445          */
446         if (TheSession == NULL) {
447                 lprintf(3, "Creating a new session\n");
448                 TheSession = (wcsession *)
449                         malloc(sizeof(wcsession));
450                 memset(TheSession, 0, sizeof(wcsession));
451                 TheSession->headers = HTTPHeaders;
452                 TheSession->serv_sock = (-1);
453                 TheSession->chat_sock = (-1);
454         
455                 /* If we're recreating a session that expired, it's best to give it the same
456                  * session number that it had before.  The client browser ought to pick up
457                  * the new session number and start using it, but in some rare situations it
458                  * doesn't, and that's a Bad Thing because it causes lots of spurious sessions
459                  * to get created.
460                  */     
461                 if (desired_session == 0) {
462                         TheSession->wc_session = GenerateSessionID();
463                 }
464                 else {
465                         TheSession->wc_session = desired_session;
466                 }
467
468                 TheSession->httpauth_user = NewStrBufPlain(httpauth_user, -1);
469                 TheSession->httpauth_pass = NewStrBufPlain(httpauth_user, -1);
470
471                 pthread_setspecific(MyConKey, (void *)TheSession);
472                 session_new_modules(TheSession);
473
474                 pthread_mutex_init(&TheSession->SessionMutex, NULL);
475                 pthread_mutex_lock(&SessionListMutex);
476                 TheSession->nonce = rand();
477                 TheSession->next = SessionList;
478                 TheSession->is_mobile = -1;
479                 SessionList = TheSession;
480                 pthread_mutex_unlock(&SessionListMutex);
481                 session_is_new = 1;
482         }
483         TheSession->headers = HTTPHeaders;
484
485         /*
486          * A future improvement might be to check the session integrity
487          * at this point before continuing.
488          */
489
490         /*
491          * Bind to the session and perform the transaction
492          */
493         pthread_mutex_lock(&TheSession->SessionMutex);          /* bind */
494         pthread_setspecific(MyConKey, (void *)TheSession);
495         
496         TheSession->lastreq = time(NULL);                       /* log */
497         TheSession->http_sock = *sock;
498         TheSession->gzip_ok = gzip_ok;
499
500         session_attach_modules(TheSession);
501
502         session_loop(ReqLine, ReqType, Buf, &Pos);                              /* do transaction */
503         session_detach_modules(TheSession);
504
505         TheSession->headers = NULL;
506         pthread_mutex_unlock(&TheSession->SessionMutex);        /* unbind */
507
508         /* Free the request buffer */
509         DeleteHash(&HTTPHeaders);
510         FreeStrBuf(&ReqLine);
511         FreeStrBuf(&ReqType);
512         FreeStrBuf(&Buf);
513 }
514
515 void tmplput_nonce(StrBuf *Target, WCTemplputParams *TP)
516 {
517         wcsession *WCC = WC;
518         StrBufAppendPrintf(Target, "%ld",
519                            (WCC != NULL)? WCC->nonce:0);                   
520 }
521
522 void tmplput_current_user(StrBuf *Target, WCTemplputParams *TP)
523 {
524         StrBufAppendTemplate(Target, TP, WC->wc_fullname, 0);
525 }
526
527 void tmplput_current_room(StrBuf *Target, WCTemplputParams *TP)
528 {
529         StrBufAppendTemplate(Target, TP, WC->wc_roomname, 0); 
530 }
531
532
533
534 void 
535 InitModule_CONTEXT
536 (void)
537 {
538         RegisterNamespace("CURRENT_USER", 0, 1, tmplput_current_user, CTX_NONE);
539         RegisterNamespace("CURRENT_ROOM", 0, 1, tmplput_current_room, CTX_NONE);
540         RegisterNamespace("NONCE", 0, 0, tmplput_nonce, 0);
541 }