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