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