* free the new 4th url fragment
[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  * 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  * handle one request
250  *
251  * This loop gets called once for every HTTP connection made to WebCit.  At
252  * this entry point we have an HTTP socket with a browser allegedly on the
253  * other end, but we have not yet bound to a WebCit session.
254  *
255  * The job of this function is to locate the correct session and bind to it,
256  * or create a session if necessary and bind to it, then run the WebCit
257  * transaction loop.  Afterwards, we unbind from the session.  When this
258  * function returns, the worker thread is then free to handle another
259  * transaction.
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         int session_is_new = 0;
272         int nLine = 0;
273         int LineLen;
274         void *vLine;
275         StrBuf *Buf, *Line, *LastLine, *HeaderName, *ReqLine, *ReqType, *HTTPVersion;
276         StrBuf *accept_language = NULL;
277         const char *pch, *pchs, *pche;
278         HashList *HTTPHeaders;
279
280         strcpy(httpauth_string, "");
281         strcpy(httpauth_user, DEFAULT_HTTPAUTH_USER);
282         strcpy(httpauth_pass, DEFAULT_HTTPAUTH_PASS);
283
284         /*
285          * Find out what it is that the web browser is asking for
286          */
287         HeaderName = NewStrBuf();
288         Buf = NewStrBuf();
289         LastLine = NULL;
290         HTTPHeaders = NewHash(1, NULL);
291
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                 StrBufSanitizeAscii(Line, '§');
325                 StrBufExtract_token(HeaderName, Line, 0, ':');
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 authentication
356          */
357         if (GetHash(HTTPHeaders, HKEY("COOKIE"), &vLine) && 
358             (vLine != NULL)) {
359                 cookie_to_stuff(vLine, &desired_session,
360                                 NULL, NULL, NULL);
361                 got_cookie = 1;
362         }
363
364         /*
365          * GroupDAV-based sessions use HTTP authentication
366          */
367         if (GetHash(HTTPHeaders, HKEY("AUTHORIZATION"), &vLine) && 
368             (vLine != NULL)) {
369                 Line = (StrBuf*)vLine;
370                 if (strncasecmp(ChrPtr(Line), "Basic", 5) == 0) {
371                         StrBufCutLeft(Line, 6);
372                         CtdlDecodeBase64(httpauth_string, ChrPtr(Line), StrLength(Line));
373                         extract_token(httpauth_user, httpauth_string, 0, ':', sizeof httpauth_user);
374                         extract_token(httpauth_pass, httpauth_string, 1, ':', sizeof httpauth_pass);
375                 }
376                 else 
377                         lprintf(1, "Authentication scheme not supported! [%s]\n", ChrPtr(Line));
378         }
379
380         if (GetHash(HTTPHeaders, HKEY("IF-MODIFIED-SINCE"), &vLine) && 
381             (vLine != NULL)) {
382                 if_modified_since = httpdate_to_timestamp((StrBuf*)vLine);
383         }
384
385         if (GetHash(HTTPHeaders, HKEY("ACCEPT-LANGUAGE"), &vLine) && 
386             (vLine != NULL)) {
387                 accept_language = (StrBuf*) vLine;
388         }
389
390
391         ReqType = NewStrBuf();
392         HTTPVersion = NewStrBuf();
393         StrBufExtract_token(HTTPVersion, ReqLine, 2, ' ');
394         StrBufExtract_token(ReqType, ReqLine, 0, ' ');
395         StrBufCutLeft(ReqLine, StrLength(ReqType) + 1);
396         StrBufCutRight(ReqLine, StrLength(HTTPVersion) + 1);
397
398         /*
399          * If the request is prefixed by "/webcit" then chop that off.  This
400          * allows a front end web server to forward all /webcit requests to us
401          * while still using the same web server port for other things.
402          */
403         if ( (StrLength(ReqLine) >= 8) && (strstr(ChrPtr(ReqLine), "/webcit/")) ) {
404                 StrBufCutLeft(ReqLine, 7);
405         }
406
407         /* Begin parsing the request. */
408 #ifdef TECH_PREVIEW
409         if ((strncmp(ChrPtr(ReqLine), "/sslg", 5) != 0) &&
410             (strncmp(ChrPtr(ReqLine), "/static/", 8) != 0) &&
411             (strncmp(ChrPtr(ReqLine), "/tiny_mce/", 10) != 0) &&
412             (strncmp(ChrPtr(ReqLine), "/wholist_section", 16) != 0) &&
413             (strstr(ChrPtr(ReqLine), "wholist_section") == NULL)) {
414 #endif
415                 lprintf(5, "HTTP: %s %s %s\n", ChrPtr(ReqType), ChrPtr(ReqLine), ChrPtr(HTTPVersion));
416 #ifdef TECH_PREVIEW
417         }
418 #endif
419
420         /** Check for bogus requests */
421         if ((StrLength(HTTPVersion) == 0) ||
422             (StrLength(ReqType) == 0) || 
423             is_bogus(ReqLine)) {
424                 StrBufPlain(ReqLine, HKEY("/404 HTTP/1.1"));
425                 StrBufPlain(ReqType, HKEY("GET"));
426         }
427         FreeStrBuf(&HTTPVersion);
428
429         /**
430          * While we're at it, gracefully handle requests for the
431          * robots.txt and favicon.ico files.
432          */
433         if (!strncasecmp(ChrPtr(ReqLine), "/robots.txt", 11)) {
434                 StrBufPlain(ReqLine, 
435                             HKEY("/static/robots.txt"
436                                  "?force_close_session=yes HTTP/1.1"));
437                 StrBufPlain(ReqType, HKEY("GET"));
438         }
439         else if (!strncasecmp(ChrPtr(ReqLine), "/favicon.ico", 12)) {
440                 StrBufPlain(ReqLine, HKEY("/static/favicon.ico"));
441                 StrBufPlain(ReqType, HKEY("GET"));
442         }
443
444         /**
445          * These are the URL's which may be executed without a
446          * session cookie already set.  If it's not one of these,
447          * force the session to close because cookies are
448          * probably disabled on the client browser.
449          */
450         else if ( (StrLength(ReqLine) > 1 )
451                 && (strncasecmp(ChrPtr(ReqLine), "/listsub", 8))
452                 && (strncasecmp(ChrPtr(ReqLine), "/freebusy", 9))
453                 && (strncasecmp(ChrPtr(ReqLine), "/do_logout", 10))
454                 && (strncasecmp(ChrPtr(ReqLine), "/groupdav", 9))
455                 && (strncasecmp(ChrPtr(ReqLine), "/static", 7))
456                 && (strncasecmp(ChrPtr(ReqLine), "/rss", 4))
457                 && (strncasecmp(ChrPtr(ReqLine), "/404", 4))
458                 && (got_cookie == 0)) {
459                 StrBufPlain(ReqLine, 
460                             HKEY("/static/nocookies.html"
461                                  "?force_close_session=yes"));
462         }
463
464         /**
465          * See if there's an existing session open with the desired ID or user/pass
466          */
467         TheSession = NULL;
468
469         if (TheSession == NULL) {
470                 pthread_mutex_lock(&SessionListMutex);
471                 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
472
473                         /** If HTTP-AUTH, look for a session with matching credentials */
474                         if ( (!IsEmptyStr(httpauth_user))
475                              &&(!strcasecmp(ChrPtr(sptr->httpauth_user), httpauth_user))
476                              &&(!strcasecmp(ChrPtr(sptr->httpauth_pass), httpauth_pass)) ) {
477                                 TheSession = sptr;
478                         }
479
480                         /** If cookie-session, look for a session with matching session ID */
481                         if ( (desired_session != 0) && (sptr->wc_session == desired_session)) {
482                                 TheSession = sptr;
483                         }
484
485                 }
486                 pthread_mutex_unlock(&SessionListMutex);
487         }
488
489         /**
490          * Create a new session if we have to
491          */
492         if (TheSession == NULL) {
493                 lprintf(3, "Creating a new session\n");
494                 TheSession = (wcsession *)
495                         malloc(sizeof(wcsession));
496                 memset(TheSession, 0, sizeof(wcsession));
497                 TheSession->serv_sock = (-1);
498                 TheSession->chat_sock = (-1);
499         
500                 /* If we're recreating a session that expired, it's best to give it the same
501                  * session number that it had before.  The client browser ought to pick up
502                  * the new session number and start using it, but in some rare situations it
503                  * doesn't, and that's a Bad Thing because it causes lots of spurious sessions
504                  * to get created.
505                  */     
506                 if (desired_session == 0) {
507                         TheSession->wc_session = GenerateSessionID();
508                 }
509                 else {
510                         TheSession->wc_session = desired_session;
511                 }
512
513                 if (TheSession->httpauth_user != NULL){
514                         FlushStrBuf(TheSession->httpauth_user);
515                         StrBufAppendBufPlain(TheSession->httpauth_user, httpauth_user, -1, 0);
516                 }
517                 else TheSession->httpauth_user = NewStrBufPlain(httpauth_user, -1);
518                 if (TheSession->httpauth_user != NULL){
519                         FlushStrBuf(TheSession->httpauth_pass);
520                         StrBufAppendBufPlain(TheSession->httpauth_pass, httpauth_user, -1, 0);
521                 }
522                 else TheSession->httpauth_pass = NewStrBufPlain(httpauth_user, -1);
523
524                 TheSession->CLineBuf = NewStrBuf();
525                 TheSession->hash_prefs = NewHash(1,NULL);       /* Get a hash table for the user preferences */
526                 pthread_mutex_init(&TheSession->SessionMutex, NULL);
527                 pthread_mutex_lock(&SessionListMutex);
528                 TheSession->nonce = rand();
529                 TheSession->next = SessionList;
530                 TheSession->is_mobile = -1;
531                 SessionList = TheSession;
532                 pthread_mutex_unlock(&SessionListMutex);
533                 session_is_new = 1;
534         }
535
536         /*
537          * A future improvement might be to check the session integrity
538          * at this point before continuing.
539          */
540
541         /*
542          * Bind to the session and perform the transaction
543          */
544         pthread_mutex_lock(&TheSession->SessionMutex);          /* bind */
545         pthread_setspecific(MyConKey, (void *)TheSession);
546         
547         TheSession->urlstrings = NewHash(1,NULL);
548         TheSession->vars = NewHash(1,NULL);
549         TheSession->http_sock = *sock;
550         TheSession->lastreq = time(NULL);                       /* log */
551         TheSession->gzip_ok = gzip_ok;
552 #ifdef ENABLE_NLS
553         if (session_is_new) {
554                 httplang_to_locale(accept_language);
555         }
556         go_selected_language();                                 /* set locale */
557 #endif
558         session_loop(HTTPHeaders, ReqLine, ReqType, Buf);                               /* do transaction */
559 #ifdef ENABLE_NLS
560         stop_selected_language();                               /* unset locale */
561 #endif
562         DeleteHash(&TheSession->summ);
563         DeleteHash(&TheSession->urlstrings);
564         DeleteHash(&TheSession->vars);
565         FreeStrBuf(&TheSession->WBuf);
566         FreeStrBuf(&TheSession->HBuf);
567         
568         
569         pthread_mutex_unlock(&TheSession->SessionMutex);        /* unbind */
570
571         /* Free the request buffer */
572         DeleteHash(&HTTPHeaders);
573         FreeStrBuf(&ReqLine);
574         FreeStrBuf(&ReqType);
575         FreeStrBuf(&Buf);
576         /*
577          * Free up any session-local substitution variables which
578          * were set during this transaction
579          */
580         
581         
582 }
583
584 void tmplput_nonce(StrBuf *Target, WCTemplputParams *TP)
585 {
586         wcsession *WCC = WC;
587         StrBufAppendPrintf(Target, "%ld",
588                            (WCC != NULL)? WCC->nonce:0);                   
589 }
590
591 void tmplput_current_user(StrBuf *Target, WCTemplputParams *TP)
592 {
593         StrBufAppendTemplate(Target, TP, WC->wc_fullname, 0);
594 }
595
596 void tmplput_current_room(StrBuf *Target, WCTemplputParams *TP)
597 {
598         StrBufAppendTemplate(Target, TP, WC->wc_roomname, 0); 
599 }
600
601
602
603 void 
604 InitModule_CONTEXT
605 (void)
606 {
607         RegisterNamespace("CURRENT_USER", 0, 1, tmplput_current_user, CTX_NONE);
608         RegisterNamespace("CURRENT_ROOM", 0, 1, tmplput_current_room, CTX_NONE);
609         RegisterNamespace("NONCE", 0, 0, tmplput_nonce, 0);
610 }