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