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