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