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