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