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