f4bd27372292839ce03a41b755a219d8d0f1ffb2
[citadel.git] / webcit / context_loop.c
1 /*
2  * context_loop.c
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  * $Id$
10  */
11
12 #include <ctype.h>
13 #include <stdlib.h>
14 #ifdef HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <stdio.h>
18 #ifdef HAVE_FCNTL_H
19 #include <fcntl.h>
20 #endif
21 #include <signal.h>
22 #include <sys/types.h>
23 #include <sys/wait.h>
24 #include <sys/socket.h>
25 #ifdef HAVE_SYS_TIME_H
26 #include <sys/time.h>
27 #endif
28 #ifdef HAVE_LIMITS_H
29 #include <limits.h>
30 #endif
31 #include <netinet/in.h>
32 #include <netdb.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <stdarg.h>
36 #include <pthread.h>
37 #include <signal.h>
38 #include "webcit.h"
39 #include "webserver.h"
40
41 /* Only one thread may manipulate SessionList at a time... */
42 pthread_mutex_t SessionListMutex;
43
44 struct wcsession *SessionList = NULL;
45
46 pthread_key_t MyConKey;                         /* TSD key for MySession() */
47
48
49 void free_attachments(struct wcsession *sess) {
50         struct wc_attachment *att;
51
52         while (sess->first_attachment != NULL) {
53                 att = sess->first_attachment;
54                 sess->first_attachment = sess->first_attachment->next;
55                 free(att->data);
56                 free(att);
57         }
58 }
59
60
61 void do_housekeeping(void)
62 {
63         struct wcsession *sptr, *ss, *session_to_kill;
64         int num_sessions = 0;
65         static int num_threads = MIN_WORKER_THREADS;
66
67         do {
68                 session_to_kill = NULL;
69                 pthread_mutex_lock(&SessionListMutex);
70                 num_sessions = 0;
71                 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
72                         ++num_sessions;
73
74                         /* Kill idle sessions */
75                         if ((time(NULL) - (sptr->lastreq)) >
76                            (time_t) WEBCIT_TIMEOUT) {
77                                 sptr->killthis = 1;
78                         }
79
80                         /* Remove sessions flagged for kill */
81                         if (sptr->killthis) {
82
83                                 lprintf(3, "Destroying session %d\n",
84                                         sptr->wc_session);
85
86                                 /* remove session from linked list */
87                                 if (sptr == SessionList) {
88                                         SessionList = SessionList->next;
89                                 }
90                                 else for (ss=SessionList;ss!=NULL;ss=ss->next) {
91                                         if (ss->next == sptr) {
92                                                 ss->next = ss->next->next;
93                                         }
94                                 }
95
96                                 session_to_kill = sptr;
97                                 goto BREAKOUT;
98                         }
99                 }
100 BREAKOUT:       pthread_mutex_unlock(&SessionListMutex);
101
102                 if (session_to_kill != NULL) {
103                         pthread_mutex_lock(&session_to_kill->SessionMutex);
104                         close(session_to_kill->serv_sock);
105                         close(session_to_kill->chat_sock);
106                         if (session_to_kill->preferences != NULL) {
107                                 free(session_to_kill->preferences);
108                         }
109                         free_attachments(session_to_kill);
110                         pthread_mutex_unlock(&session_to_kill->SessionMutex);
111                         free(session_to_kill);
112                 }
113
114         } while (session_to_kill != NULL);
115
116         /*
117          * See if we need more worker threads
118          */
119         while ( (num_sessions > num_threads)
120               && (num_threads <= MAX_WORKER_THREADS) ) {
121                 spawn_another_worker_thread();
122                 ++num_threads;
123                 lprintf(3, "There are %d sessions and %d threads active.\n",
124                         num_sessions, num_threads);
125         }
126 }
127
128
129 /* 
130  * Wake up occasionally and clean house
131  */
132 void housekeeping_loop(void)
133 {
134         while (1) {
135                 sleeeeeeeeeep(HOUSEKEEPING);
136                 do_housekeeping();
137         }
138 }
139
140
141 /*
142  * Generate a unique WebCit session ID (which is not the same thing as the
143  * Citadel session ID).
144  *
145  * FIXME ... ensure that session number is truly unique
146  *
147  */
148 int GenerateSessionID(void)
149 {
150         static int seq = (-1);
151
152         if (seq < 0) {
153                 seq = (int) time(NULL);
154         }
155                 
156         return ++seq;
157 }
158
159
160 /*
161  * Collapse multiple cookies on one line
162  */
163 int req_gets(int sock, char *buf, char *hold)
164 {
165         int a;
166
167         if (strlen(hold) == 0) {
168                 strcpy(buf, "");
169                 a = client_gets(sock, buf);
170                 if (a<1) return(-1);
171         } else {
172                 safestrncpy(buf, hold, SIZ);
173         }
174         strcpy(hold, "");
175
176         if (!strncasecmp(buf, "Cookie: ", 8)) {
177                 for (a = 0; a < strlen(buf); ++a)
178                         if (buf[a] == ';') {
179                                 sprintf(hold, "Cookie: %s", &buf[a + 1]);
180                                 buf[a] = 0;
181                                 while (isspace(hold[8]))
182                                         strcpy(&hold[8], &hold[9]);
183                                 return(0);
184                         }
185         }
186
187         return(0);
188 }
189
190 /*
191  * lingering_close() a`la Apache. see
192  * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
193  */
194
195 int lingering_close(int fd)
196 {
197         char buf[SIZ];
198         int i;
199         fd_set set;
200         struct timeval tv, start;
201
202         gettimeofday(&start, NULL);
203         shutdown(fd, 1);
204         do {
205                 do {
206                         gettimeofday(&tv, NULL);
207                         tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
208                         tv.tv_usec = start.tv_usec - tv.tv_usec;
209                         if (tv.tv_usec < 0) {
210                                 tv.tv_sec--;
211                                 tv.tv_usec += 1000000;
212                         }
213                         FD_ZERO(&set);
214                         FD_SET(fd, &set);
215                         i = select(fd + 1, &set, NULL, NULL, &tv);
216                 } while (i == -1 && errno == EINTR);
217
218                 if (i <= 0)
219                         break;
220
221                 i = read(fd, buf, sizeof buf);
222         } while (i != 0 && (i != -1 || errno == EINTR));
223
224         return close(fd);
225 }
226
227
228
229 /*
230  * Check for bogus requests coming from (for example) brain-dead
231  * Windoze boxes that are infected with the latest worm-of-the-week.
232  * If we detect one of these, bail out without bothering our Citadel
233  * server.
234  */
235 int is_bogus(char *http_cmd) {
236
237         if (!strncasecmp(http_cmd, "GET /scripts/root.exe", 21)) return(1);
238         if (!strncasecmp(http_cmd, "GET /c/winnt", 12)) return(2);
239         if (!strncasecmp(http_cmd, "GET /MSADC/", 11)) return(3);
240
241         return(0);      /* probably ok */
242 }
243
244
245
246 /*
247  * This loop gets called once for every HTTP connection made to WebCit.  At
248  * this entry point we have an HTTP socket with a browser allegedly on the
249  * other end, but we have not yet bound to a WebCit session.
250  *
251  * The job of this function is to locate the correct session and bind to it,
252  * or create a session if necessary and bind to it, then run the WebCit
253  * transaction loop.  Afterwards, we unbind from the session.  When this
254  * function returns, the worker thread is then free to handle another
255  * transaction.
256  */
257 void context_loop(int sock)
258 {
259         struct httprequest *req = NULL;
260         struct httprequest *last = NULL;
261         struct httprequest *hptr;
262         char buf[SIZ], hold[SIZ];
263         int desired_session = 0;
264         int got_cookie = 0;
265         struct wcsession *TheSession, *sptr;
266         char httpauth_string[SIZ];
267         char httpauth_user[SIZ];
268         char httpauth_pass[SIZ];
269
270         strcpy(httpauth_string, "");
271         strcpy(httpauth_user, "");
272         strcpy(httpauth_pass, "");
273
274         /*
275          * Find out what it is that the web browser is asking for
276          */
277         memset(hold, 0, sizeof(hold));
278         do {
279                 if (req_gets(sock, buf, hold) < 0) return;
280
281                 /*
282                  * Browser-based sessions use cookies for session authentication
283                  */
284                 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
285                         cookie_to_stuff(&buf[15], &desired_session,
286                                 NULL, NULL, NULL);
287                         got_cookie = 1;
288                 }
289
290                 /*
291                  * GroupDAV-based sessions use HTTP authentication
292                  */
293                 if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
294                         CtdlDecodeBase64(httpauth_string, &buf[21], strlen(&buf[21]));
295                         extract_token(httpauth_user, httpauth_string, 0, ':');
296                         extract_token(httpauth_pass, httpauth_string, 1, ':');
297                 }
298
299                 /*
300                  * Read in the request
301                  */
302                 hptr = (struct httprequest *)
303                         malloc(sizeof(struct httprequest));
304                 if (req == NULL)
305                         req = hptr;
306                 else
307                         last->next = hptr;
308                 hptr->next = NULL;
309                 last = hptr;
310
311                 safestrncpy(hptr->line, buf, sizeof hptr->line);
312
313         } while (strlen(buf) > 0);
314
315         safestrncpy(buf, req->line, sizeof buf);
316         lprintf(5, "HTTP: %s\n", buf);
317
318         /* Check for bogus requests */
319         if (is_bogus(buf)) goto bail;
320
321         /*
322          * If requesting a non-root page, there should already be a cookie
323          * set.  If there isn't, the client browser has cookies turned off
324          * (or doesn't support them) and we have to barf & bail.
325          */
326         remove_token(buf, 0, ' ');
327         if (buf[1]==' ') buf[1]=0;
328
329         /*
330          * While we're at it, gracefully handle requests for the
331          * robots.txt and favicon.ico files.
332          */
333         if (!strncasecmp(buf, "/robots.txt", 11)) {
334                 strcpy(req->line, "GET /static/robots.txt"
335                                 "?force_close_session=yes HTTP/1.0");
336         }
337         else if (!strncasecmp(buf, "/favicon.ico", 12)) {
338                 strcpy(req->line, "GET /static/favicon.ico");
339         }
340
341         /* These are the URL's which may be executed without a
342          * session cookie already set.  If it's not one of these,
343          * force the session to close because cookies are
344          * probably disabled on the client browser.
345          */
346         else if ( (strcmp(buf, "/"))
347                 && (strncasecmp(buf, "/listsub", 8))
348                 && (strncasecmp(buf, "/freebusy", 9))
349                 && (strncasecmp(buf, "/do_logout", 10))
350                 && (strncasecmp(buf, "/groupdav", 9))
351                 && (got_cookie == 0)) {
352                 strcpy(req->line, "GET /static/nocookies.html"
353                                 "?force_close_session=yes HTTP/1.0");
354         }
355
356         /*
357          * See if there's an existing session open with the desired ID or user/pass
358          */
359         TheSession = NULL;
360
361         if ( (TheSession == NULL) && (strlen(httpauth_user) > 0) ) {
362                 pthread_mutex_lock(&SessionListMutex);
363                 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
364                         if ( (!strcasecmp(sptr->httpauth_user, httpauth_user))
365                            &&(!strcasecmp(sptr->httpauth_pass, httpauth_pass)) ) {
366                                 TheSession = sptr;
367                         }
368                 }
369                 pthread_mutex_unlock(&SessionListMutex);
370         }
371
372         if ( (TheSession == NULL) && (desired_session != 0) ) {
373                 pthread_mutex_lock(&SessionListMutex);
374                 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
375                         if (sptr->wc_session == desired_session) {
376                                 TheSession = sptr;
377                         }
378                 }
379                 pthread_mutex_unlock(&SessionListMutex);
380         }
381
382         /*
383          * Create a new session if we have to
384          */
385         if (TheSession == NULL) {
386                 lprintf(3, "Creating a new session\n");
387                 TheSession = (struct wcsession *)
388                         malloc(sizeof(struct wcsession));
389                 memset(TheSession, 0, sizeof(struct wcsession));
390                 TheSession->serv_sock = (-1);
391                 TheSession->chat_sock = (-1);
392                 TheSession->wc_session = GenerateSessionID();
393                 strcpy(TheSession->httpauth_user, httpauth_user);
394                 strcpy(TheSession->httpauth_pass, httpauth_pass);
395                 pthread_mutex_init(&TheSession->SessionMutex, NULL);
396
397                 pthread_mutex_lock(&SessionListMutex);
398                 TheSession->next = SessionList;
399                 SessionList = TheSession;
400                 pthread_mutex_unlock(&SessionListMutex);
401         }
402
403         /*
404          * A future improvement might be to check the session integrity
405          * at this point before continuing.
406          */
407
408         /*
409          * Bind to the session and perform the transaction
410          */
411         pthread_mutex_lock(&TheSession->SessionMutex);          /* bind */
412         pthread_setspecific(MyConKey, (void *)TheSession);
413         TheSession->http_sock = sock;
414         TheSession->lastreq = time(NULL);                       /* log */
415         session_loop(req);                              /* do transaction */
416         pthread_mutex_unlock(&TheSession->SessionMutex);        /* unbind */
417
418         /* Free the request buffer */
419 bail:   while (req != NULL) {
420                 hptr = req->next;
421                 free(req);
422                 req = hptr;
423         }
424
425         /* Free up any session-local substitution variables which
426          * were set during this transaction
427          */
428         clear_local_substs();
429 }