* migrate message creation to templates (citing still missing)
[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 authentication
399          */
400         if (GetHash(HTTPHeaders, HKEY("COOKIE"), &vLine) && 
401             (vLine != NULL)) {
402                 cookie_to_stuff(vLine, &desired_session,
403                                 NULL, 0, NULL, 0, NULL, 0);
404                 got_cookie = 1;
405         }
406
407         /**
408          * GroupDAV-based sessions use HTTP authentication
409          */
410         if (GetHash(HTTPHeaders, HKEY("AUTHORIZATION"), &vLine) && 
411             (vLine != NULL)) {
412                 Line = (StrBuf*)vLine;
413                 if (strncasecmp(ChrPtr(Line), "Basic ", 6)) {
414                         StrBufCutLeft(Line, 6);
415                         CtdlDecodeBase64(httpauth_string, ChrPtr(Line), StrLength(Line));
416                         extract_token(httpauth_user, httpauth_string, 0, ':', sizeof httpauth_user);
417                         extract_token(httpauth_pass, httpauth_string, 1, ':', sizeof httpauth_pass);
418                 }
419                 else 
420                         lprintf(1, "Authentication sheme not supported! [%s]\n", ChrPtr(Line));
421         }
422
423         if (GetHash(HTTPHeaders, HKEY("IF-MODIFIED-SINCE"), &vLine) && 
424             (vLine != NULL)) {
425                 if_modified_since = httpdate_to_timestamp((StrBuf*)vLine);
426         }
427
428         if (GetHash(HTTPHeaders, HKEY("ACCEPT-LANGUAGE"), &vLine) && 
429             (vLine != NULL)) {
430                 accept_language = (StrBuf*) vLine;
431         }
432
433
434         /**
435          * If the request is prefixed by "/webcit" then chop that off.  This
436          * allows a front end web server to forward all /webcit requests to us
437          * while still using the same web server port for other things.
438          */
439
440         ReqType = NewStrBuf();
441         HTTPVersion = NewStrBuf();
442         StrBufExtract_token(HTTPVersion, ReqLine, 2, ' ');
443         StrBufExtract_token(ReqType, ReqLine, 0, ' ');
444         StrBufCutLeft(ReqLine, StrLength(ReqType) + 1);
445         StrBufCutRight(ReqLine, StrLength(HTTPVersion) + 1);
446
447         if ((StrLength(ReqLine) >= 8) &&
448             (ptr = strstr(ChrPtr(ReqLine), "/webcit/"), /*< Handle "/webcit/" */
449              (ptr != NULL))) {
450                 StrBufCutLeft(ReqLine, 7);
451         }
452
453         /** Begin parsing the request. */
454 #ifdef TECH_PREVIEW
455         if ((strncmp(ChrPtr(ReqLine), "/sslg", 5) != 0) &&
456             (strncmp(ChrPtr(ReqLine), "/static/", 8) != 0) &&
457             (strncmp(ChrPtr(ReqLine), "/wholist_section", 16) != 0)) {
458 #endif
459                 lprintf(5, "HTTP: %s %s %s\n", ChrPtr(ReqType), ChrPtr(ReqLine), ChrPtr(HTTPVersion));
460 #ifdef TECH_PREVIEW
461         }
462 #endif
463
464         /** Check for bogus requests */
465         if ((StrLength(HTTPVersion) == 0) ||
466             (StrLength(ReqType) == 0) || 
467             is_bogus(ReqLine)) {
468                 StrBufPlain(ReqLine, HKEY("/404 HTTP/1.1"));
469                 StrBufPlain(ReqType, HKEY("GET"));
470         }
471         FreeStrBuf(&HTTPVersion);
472
473         /**
474          * While we're at it, gracefully handle requests for the
475          * robots.txt and favicon.ico files.
476          */
477         if (!strncasecmp(ChrPtr(ReqLine), "/robots.txt", 11)) {
478                 StrBufPlain(ReqLine, 
479                             HKEY("/static/robots.txt"
480                                  "?force_close_session=yes HTTP/1.1"));
481                 StrBufPlain(ReqType, HKEY("GET"));
482         }
483         else if (!strncasecmp(ChrPtr(ReqLine), "/favicon.ico", 12)) {
484                 StrBufPlain(ReqLine, HKEY("/static/favicon.ico"));
485                 StrBufPlain(ReqType, HKEY("GET"));
486         }
487
488         /**
489          * These are the URL's which may be executed without a
490          * session cookie already set.  If it's not one of these,
491          * force the session to close because cookies are
492          * probably disabled on the client browser.
493          */
494         else if ( (StrLength(ReqLine) > 1 )
495                 && (strncasecmp(ChrPtr(ReqLine), "/listsub", 8))
496                 && (strncasecmp(ChrPtr(ReqLine), "/freebusy", 9))
497                 && (strncasecmp(ChrPtr(ReqLine), "/do_logout", 10))
498                 && (strncasecmp(ChrPtr(ReqLine), "/groupdav", 9))
499                 && (strncasecmp(ChrPtr(ReqLine), "/static", 7))
500                 && (strncasecmp(ChrPtr(ReqLine), "/rss", 4))
501                 && (strncasecmp(ChrPtr(ReqLine), "/404", 4))
502                 && (got_cookie == 0)) {
503                 StrBufPlain(ReqLine, 
504                             HKEY("/static/nocookies.html"
505                                  "?force_close_session=yes"));
506         }
507
508         /**
509          * See if there's an existing session open with the desired ID or user/pass
510          */
511         TheSession = NULL;
512
513         if (TheSession == NULL) {
514                 pthread_mutex_lock(&SessionListMutex);
515                 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
516
517                         /** If HTTP-AUTH, look for a session with matching credentials */
518                         if ( (!IsEmptyStr(httpauth_user))
519                            &&(!strcasecmp(sptr->httpauth_user, httpauth_user))
520                            &&(!strcasecmp(sptr->httpauth_pass, httpauth_pass)) ) {
521                                 TheSession = sptr;
522                         }
523
524                         /** If cookie-session, look for a session with matching session ID */
525                         if ( (desired_session != 0) && (sptr->wc_session == desired_session)) {
526                                 TheSession = sptr;
527                         }
528
529                 }
530                 pthread_mutex_unlock(&SessionListMutex);
531         }
532
533         /**
534          * Create a new session if we have to
535          */
536         if (TheSession == NULL) {
537                 lprintf(3, "Creating a new session\n");
538                 TheSession = (struct wcsession *)
539                         malloc(sizeof(struct wcsession));
540                 memset(TheSession, 0, sizeof(struct wcsession));
541                 TheSession->serv_sock = (-1);
542                 TheSession->chat_sock = (-1);
543         
544                 /* If we're recreating a session that expired, it's best to give it the same
545                  * session number that it had before.  The client browser ought to pick up
546                  * the new session number and start using it, but in some rare situations it
547                  * doesn't, and that's a Bad Thing because it causes lots of spurious sessions
548                  * to get created.
549                  */     
550                 if (desired_session == 0) {
551                         TheSession->wc_session = GenerateSessionID();
552                 }
553                 else {
554                         TheSession->wc_session = desired_session;
555                 }
556
557                 strcpy(TheSession->httpauth_user, httpauth_user);
558                 strcpy(TheSession->httpauth_pass, httpauth_pass);
559                 TheSession->hash_prefs = NewHash(1,NULL);       /* Get a hash table for the user preferences */
560                 pthread_mutex_init(&TheSession->SessionMutex, NULL);
561                 pthread_mutex_lock(&SessionListMutex);
562                 TheSession->urlstrings = NULL;
563                 TheSession->vars = NULL;
564                 TheSession->nonce = rand();
565                 TheSession->WBuf = NULL;
566                 TheSession->CLineBuf = NewStrBuf();
567                 TheSession->next = SessionList;
568                 TheSession->is_mobile = -1;
569                 SessionList = TheSession;
570                 pthread_mutex_unlock(&SessionListMutex);
571                 session_is_new = 1;
572         }
573
574         /*
575          * A future improvement might be to check the session integrity
576          * at this point before continuing.
577          */
578
579         /*
580          * Bind to the session and perform the transaction
581          */
582         pthread_mutex_lock(&TheSession->SessionMutex);          /* bind */
583         pthread_setspecific(MyConKey, (void *)TheSession);
584         
585         TheSession->urlstrings = NewHash(1,NULL);
586         TheSession->vars = NewHash(1,NULL);
587         TheSession->http_sock = *sock;
588         TheSession->lastreq = time(NULL);                       /* log */
589         TheSession->gzip_ok = gzip_ok;
590 #ifdef ENABLE_NLS
591         if (session_is_new) {
592                 httplang_to_locale(accept_language);
593         }
594         go_selected_language();                                 /* set locale */
595 #endif
596         session_loop(HTTPHeaders, ReqLine, ReqType, Buf);                               /* do transaction */
597 #ifdef ENABLE_NLS
598         stop_selected_language();                               /* unset locale */
599 #endif
600         DeleteHash(&TheSession->summ);
601         DeleteHash(&TheSession->urlstrings);
602         DeleteHash(&TheSession->vars);
603         FreeStrBuf(&TheSession->WBuf);
604         FreeStrBuf(&TheSession->HBuf);
605         
606         
607         pthread_mutex_unlock(&TheSession->SessionMutex);        /* unbind */
608
609         /* Free the request buffer */
610         DeleteHash(&HTTPHeaders);
611         FreeStrBuf(&ReqLine);
612         FreeStrBuf(&ReqType);
613         FreeStrBuf(&Buf);
614         /*
615          * Free up any session-local substitution variables which
616          * were set during this transaction
617          */
618         
619         
620 }
621
622 void tmpl_nonce(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
623 {
624         struct wcsession *WCC = WC;
625         StrBufAppendPrintf(Target, "%ld",
626                            (WCC != NULL)? WCC->nonce:0);                   
627 }
628
629 void 
630 InitModule_CONTEXT
631 (void)
632 {
633         RegisterNamespace("NONCE", 0, 0, tmpl_nonce, 0);
634 }