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