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