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