* --pedantic cleanup.
[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         DeleteHash(&((*sessions_to_kill)->attachments));
36         free_march_list((*sessions_to_kill));
37         DeleteHash(&((*sessions_to_kill)->hash_prefs));
38         DeleteHash(&((*sessions_to_kill)->IconBarSetttings));
39         DeleteHash(&((*sessions_to_kill)->ServCfg));
40         FreeStrBuf(&((*sessions_to_kill)->UrlFragment1));
41         FreeStrBuf(&((*sessions_to_kill)->UrlFragment2));
42         FreeStrBuf(&((*sessions_to_kill)->UrlFragment3));
43         FreeStrBuf(&((*sessions_to_kill)->WBuf));
44         FreeStrBuf(&((*sessions_to_kill)->HBuf));
45         FreeStrBuf(&((*sessions_to_kill)->CLineBuf));
46         free((*sessions_to_kill));
47         (*sessions_to_kill) = NULL;
48 }
49
50 void shutdown_sessions(void)
51 {
52         wcsession *sptr;
53         
54         for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
55                         sptr->killthis = 1;
56         }
57 }
58
59 void do_housekeeping(void)
60 {
61         wcsession *sptr, *ss;
62         wcsession *sessions_to_kill = NULL;
63         int num_sessions = 0;
64         static int num_threads = MIN_WORKER_THREADS;
65
66         /**
67          * Lock the session list, moving any candidates for euthanasia into
68          * a separate list.
69          */
70         pthread_mutex_lock(&SessionListMutex);
71         num_sessions = 0;
72         for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
73                 ++num_sessions;
74
75                 /** Kill idle sessions */
76                 if ((time(NULL) - (sptr->lastreq)) >
77                    (time_t) WEBCIT_TIMEOUT) {
78                         sptr->killthis = 1;
79                 }
80
81                 /** Remove sessions flagged for kill */
82                 if (sptr->killthis) {
83
84                         /** remove session from linked list */
85                         if (sptr == SessionList) {
86                                 SessionList = SessionList->next;
87                         }
88                         else for (ss=SessionList;ss!=NULL;ss=ss->next) {
89                                 if (ss->next == sptr) {
90                                         ss->next = ss->next->next;
91                                 }
92                         }
93
94                         sptr->next = sessions_to_kill;
95                         sessions_to_kill = sptr;
96                 }
97         }
98         pthread_mutex_unlock(&SessionListMutex);
99
100         /**
101          * Now free up and destroy the culled sessions.
102          */
103         while (sessions_to_kill != NULL) {
104                 lprintf(3, "Destroying session %d\n", sessions_to_kill->wc_session);
105                 pthread_mutex_lock(&sessions_to_kill->SessionMutex);
106                 pthread_mutex_unlock(&sessions_to_kill->SessionMutex);
107                 sptr = sessions_to_kill->next;
108
109                 DestroySession(&sessions_to_kill);
110                 sessions_to_kill = sptr;
111                 --num_sessions;
112         }
113
114         /**
115          * If there are more sessions than threads, then we should spawn
116          * more threads ... up to a predefined maximum.
117          */
118         while ( (num_sessions > num_threads)
119               && (num_threads <= MAX_WORKER_THREADS) ) {
120                 spawn_another_worker_thread();
121                 ++num_threads;
122                 lprintf(3, "There are %d sessions and %d threads active.\n",
123                         num_sessions, num_threads);
124         }
125 }
126
127
128 /**
129  * \brief Wake up occasionally and clean house
130  */
131 void housekeeping_loop(void)
132 {
133         while (1) {
134                 sleeeeeeeeeep(HOUSEKEEPING);
135                 do_housekeeping();
136         }
137 }
138
139
140 /**
141  * \brief Create a Session id
142  * Generate a unique WebCit session ID (which is not the same thing as the
143  * Citadel session ID).
144  *
145  * \todo FIXME ... ensure that session number is truly unique
146  *
147  */
148 int GenerateSessionID(void)
149 {
150         static int seq = (-1);
151
152         if (seq < 0) {
153                 seq = (int) time(NULL);
154         }
155                 
156         return ++seq;
157 }
158
159 /*
160  * Collapse multiple cookies on one line
161  */
162 int ReqGetStrBuf(int *sock, StrBuf *Target, StrBuf *buf)
163 {
164         
165         return ClientGetLine(sock, Target, buf);
166 }
167
168
169
170 /*
171  * lingering_close() a`la Apache. see
172  * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
173  */
174 int lingering_close(int fd)
175 {
176         char buf[SIZ];
177         int i;
178         fd_set set;
179         struct timeval tv, start;
180
181         gettimeofday(&start, NULL);
182         shutdown(fd, 1);
183         do {
184                 do {
185                         gettimeofday(&tv, NULL);
186                         tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
187                         tv.tv_usec = start.tv_usec - tv.tv_usec;
188                         if (tv.tv_usec < 0) {
189                                 tv.tv_sec--;
190                                 tv.tv_usec += 1000000;
191                         }
192                         FD_ZERO(&set);
193                         FD_SET(fd, &set);
194                         i = select(fd + 1, &set, NULL, NULL, &tv);
195                 } while (i == -1 && errno == EINTR);
196
197                 if (i <= 0)
198                         break;
199
200                 i = read(fd, buf, sizeof buf);
201         } while (i != 0 && (i != -1 || errno == EINTR));
202
203         return close(fd);
204 }
205
206
207
208 /**
209  * \brief       sanity requests
210  *              Check for bogus requests coming from brain-dead Windows boxes.
211  *
212  * \param       http_cmd        The HTTP request to check
213  */
214 int is_bogus(StrBuf *http_cmd) {
215         const char *url;
216         int i, max;
217         const char *bogus_prefixes[] = {
218                 "/scripts/root.exe",    /**< Worms and trojans and viruses, oh my! */
219                 "/c/winnt",
220                 "/MSADC/",
221                 "/_vti",                /**< Broken Microsoft DAV implementation */
222                 "/MSOffice"             /**< Stoopid MSOffice thinks everyone is IIS */
223         };
224
225         url = ChrPtr(http_cmd);
226         if (IsEmptyStr(url)) return(1);
227         ++url;
228
229
230         max = sizeof(bogus_prefixes) / sizeof(char *);
231
232         for (i=0; i<max; ++i) {
233                 if (!strncasecmp(url, bogus_prefixes[i], strlen(bogus_prefixes[i]))) {
234                         return(2);
235                 }
236         }
237
238         return(0);      /* probably ok */
239 }
240
241
242 const char *nix(void *vptr) {return ChrPtr( (StrBuf*)vptr);}
243
244 /**
245  * \brief handle one request
246  * This loop gets called once for every HTTP connection made to WebCit.  At
247  * this entry point we have an HTTP socket with a browser allegedly on the
248  * other end, but we have not yet bound to a WebCit session.
249  *
250  * The job of this function is to locate the correct session and bind to it,
251  * or create a session if necessary and bind to it, then run the WebCit
252  * transaction loop.  Afterwards, we unbind from the session.  When this
253  * function returns, the worker thread is then free to handle another
254  * transaction.
255  * \param sock the socket we will put our answer to
256  */
257 void context_loop(int *sock)
258 {
259         const char *buf;
260         int desired_session = 0;
261         int got_cookie = 0;
262         int gzip_ok = 0;
263         wcsession *TheSession, *sptr;
264         char httpauth_string[1024];
265         char httpauth_user[1024];
266         char httpauth_pass[1024];
267         char *ptr = NULL;
268         int session_is_new = 0;
269         int nLine = 0;
270         int LineLen;
271         void *vLine;
272         StrBuf *Buf, *Line, *LastLine, *HeaderName, *ReqLine, *ReqType, *HTTPVersion;
273         StrBuf *accept_language = NULL;
274         const char *pch, *pchs, *pche;
275         HashList *HTTPHeaders;
276
277         strcpy(httpauth_string, "");
278         strcpy(httpauth_user, DEFAULT_HTTPAUTH_USER);
279         strcpy(httpauth_pass, DEFAULT_HTTPAUTH_PASS);
280
281         /**
282          * Find out what it is that the web browser is asking for
283          */
284         HeaderName = NewStrBuf();
285         Buf = NewStrBuf();
286         LastLine = NULL;
287         HTTPHeaders = NewHash(1, NULL);
288         /**
289          * Read in the request
290          */
291         do {
292                 nLine ++;
293                 Line = NewStrBuf();
294                 if (ReqGetStrBuf(sock, Line, Buf) < 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                 StrBufExtract_token(HeaderName, Line, 0, ':');
321                 /*/// TODO: filter bad chars! */
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 
352
353 authentication
354          */
355         if (GetHash(HTTPHeaders, HKEY("COOKIE"), &vLine) && 
356             (vLine != NULL)) {
357                 cookie_to_stuff(vLine, &desired_session,
358                                 NULL, 0, NULL, 0, NULL, 0);
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         /**
390          * If the request is prefixed by "/webcit" then chop that off.  This
391          * allows a front end web server to forward all /webcit requests to us
392          * while still using the same web server port for other things.
393          */
394
395         ReqType = NewStrBuf();
396         HTTPVersion = NewStrBuf();
397         StrBufExtract_token(HTTPVersion, ReqLine, 2, ' ');
398         StrBufExtract_token(ReqType, ReqLine, 0, ' ');
399         StrBufCutLeft(ReqLine, StrLength(ReqType) + 1);
400         StrBufCutRight(ReqLine, StrLength(HTTPVersion) + 1);
401
402         if ((follow_xff == 1) && (StrLength(ReqLine) >= 8) &&
403             (ptr = strstr(ChrPtr(ReqLine), "/webcit/"), /*< Handle "/webcit/" */
404              (ptr != NULL))) {
405                 StrBufCutLeft(ReqLine, 7);
406         }
407
408         /** Begin parsing the request. */
409 #ifdef TECH_PREVIEW
410         if ((strncmp(ChrPtr(ReqLine), "/sslg", 5) != 0) &&
411             (strncmp(ChrPtr(ReqLine), "/static/", 8) != 0) &&
412             (strncmp(ChrPtr(ReqLine), "/tiny_mce/", 10) != 0) &&
413             (strncmp(ChrPtr(ReqLine), "/wholist_section", 16) != 0) &&
414             (strstr(ChrPtr(ReqLine), "wholist_section") == NULL)) {
415 #endif
416                 lprintf(5, "HTTP: %s %s %s\n", ChrPtr(ReqType), ChrPtr(ReqLine), ChrPtr(HTTPVersion));
417 #ifdef TECH_PREVIEW
418         }
419 #endif
420
421         /** Check for bogus requests */
422         if ((StrLength(HTTPVersion) == 0) ||
423             (StrLength(ReqType) == 0) || 
424             is_bogus(ReqLine)) {
425                 StrBufPlain(ReqLine, HKEY("/404 HTTP/1.1"));
426                 StrBufPlain(ReqType, HKEY("GET"));
427         }
428         FreeStrBuf(&HTTPVersion);
429
430         /**
431          * While we're at it, gracefully handle requests for the
432          * robots.txt and favicon.ico files.
433          */
434         if (!strncasecmp(ChrPtr(ReqLine), "/robots.txt", 11)) {
435                 StrBufPlain(ReqLine, 
436                             HKEY("/static/robots.txt"
437                                  "?force_close_session=yes HTTP/1.1"));
438                 StrBufPlain(ReqType, HKEY("GET"));
439         }
440         else if (!strncasecmp(ChrPtr(ReqLine), "/favicon.ico", 12)) {
441                 StrBufPlain(ReqLine, HKEY("/static/favicon.ico"));
442                 StrBufPlain(ReqType, HKEY("GET"));
443         }
444
445         /**
446          * These are the URL's which may be executed without a
447          * session cookie already set.  If it's not one of these,
448          * force the session to close because cookies are
449          * probably disabled on the client browser.
450          */
451         else if ( (StrLength(ReqLine) > 1 )
452                 && (strncasecmp(ChrPtr(ReqLine), "/listsub", 8))
453                 && (strncasecmp(ChrPtr(ReqLine), "/freebusy", 9))
454                 && (strncasecmp(ChrPtr(ReqLine), "/do_logout", 10))
455                 && (strncasecmp(ChrPtr(ReqLine), "/groupdav", 9))
456                 && (strncasecmp(ChrPtr(ReqLine), "/static", 7))
457                 && (strncasecmp(ChrPtr(ReqLine), "/rss", 4))
458                 && (strncasecmp(ChrPtr(ReqLine), "/404", 4))
459                 && (got_cookie == 0)) {
460                 StrBufPlain(ReqLine, 
461                             HKEY("/static/nocookies.html"
462                                  "?force_close_session=yes"));
463         }
464
465         /**
466          * See if there's an existing session open with the desired ID or user/pass
467          */
468         TheSession = NULL;
469
470         if (TheSession == NULL) {
471                 pthread_mutex_lock(&SessionListMutex);
472                 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
473
474                         /** If HTTP-AUTH, look for a session with matching credentials */
475                         if ( (!IsEmptyStr(httpauth_user))
476                            &&(!strcasecmp(sptr->httpauth_user, httpauth_user))
477                            &&(!strcasecmp(sptr->httpauth_pass, httpauth_pass)) ) {
478                                 TheSession = sptr;
479                         }
480
481                         /** If cookie-session, look for a session with matching session ID */
482                         if ( (desired_session != 0) && (sptr->wc_session == desired_session)) {
483                                 TheSession = sptr;
484                         }
485
486                 }
487                 pthread_mutex_unlock(&SessionListMutex);
488         }
489
490         /**
491          * Create a new session if we have to
492          */
493         if (TheSession == NULL) {
494                 lprintf(3, "Creating a new session\n");
495                 TheSession = (wcsession *)
496                         malloc(sizeof(wcsession));
497                 memset(TheSession, 0, sizeof(wcsession));
498                 TheSession->serv_sock = (-1);
499                 TheSession->chat_sock = (-1);
500         
501                 /* If we're recreating a session that expired, it's best to give it the same
502                  * session number that it had before.  The client browser ought to pick up
503                  * the new session number and start using it, but in some rare situations it
504                  * doesn't, and that's a Bad Thing because it causes lots of spurious sessions
505                  * to get created.
506                  */     
507                 if (desired_session == 0) {
508                         TheSession->wc_session = GenerateSessionID();
509                 }
510                 else {
511                         TheSession->wc_session = desired_session;
512                 }
513
514                 strcpy(TheSession->httpauth_user, httpauth_user);
515                 strcpy(TheSession->httpauth_pass, httpauth_pass);
516                 TheSession->hash_prefs = NewHash(1,NULL);       /* Get a hash table for the user preferences */
517                 pthread_mutex_init(&TheSession->SessionMutex, NULL);
518                 pthread_mutex_lock(&SessionListMutex);
519                 TheSession->urlstrings = NULL;
520                 TheSession->vars = NULL;
521                 TheSession->nonce = rand();
522                 TheSession->WBuf = NULL;
523                 TheSession->CLineBuf = NewStrBuf();
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 tmpl_nonce(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
580 {
581         wcsession *WCC = WC;
582         StrBufAppendPrintf(Target, "%ld",
583                            (WCC != NULL)? WCC->nonce:0);                   
584 }
585
586 void 
587 InitModule_CONTEXT
588 (void)
589 {
590         RegisterNamespace("NONCE", 0, 0, tmpl_nonce, 0);
591 }