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