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