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