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.
12 #include "webserver.h"
14 /* Only one thread may manipulate SessionList at a time... */
15 pthread_mutex_t SessionListMutex;
17 struct wcsession *SessionList = NULL;
19 pthread_key_t MyConKey; /* TSD key for MySession() */
22 void free_attachments(struct wcsession *sess) {
23 struct wc_attachment *att;
25 while (sess->first_attachment != NULL) {
26 att = sess->first_attachment;
27 sess->first_attachment = sess->first_attachment->next;
34 void do_housekeeping(void)
36 struct wcsession *sptr, *ss;
37 struct wcsession *sessions_to_kill = NULL;
39 static int num_threads = MIN_WORKER_THREADS;
42 * Lock the session list, moving any candidates for euthanasia into
45 pthread_mutex_lock(&SessionListMutex);
47 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
50 /* Kill idle sessions */
51 if ((time(NULL) - (sptr->lastreq)) >
52 (time_t) WEBCIT_TIMEOUT) {
56 /* Remove sessions flagged for kill */
59 /* remove session from linked list */
60 if (sptr == SessionList) {
61 SessionList = SessionList->next;
63 else for (ss=SessionList;ss!=NULL;ss=ss->next) {
64 if (ss->next == sptr) {
65 ss->next = ss->next->next;
69 sptr->next = sessions_to_kill;
70 sessions_to_kill = sptr;
73 pthread_mutex_unlock(&SessionListMutex);
76 * Now free up and destroy the culled sessions.
78 while (sessions_to_kill != NULL) {
79 lprintf(3, "Destroying session %d\n", sessions_to_kill->wc_session);
80 pthread_mutex_lock(&sessions_to_kill->SessionMutex);
81 close(sessions_to_kill->serv_sock);
82 close(sessions_to_kill->chat_sock);
83 if (sessions_to_kill->preferences != NULL) {
84 free(sessions_to_kill->preferences);
86 free_attachments(sessions_to_kill);
87 pthread_mutex_unlock(&sessions_to_kill->SessionMutex);
88 sptr = sessions_to_kill->next;
89 free(sessions_to_kill);
90 sessions_to_kill = sptr;
95 * If there are more sessions than threads, then we should spawn
96 * more threads ... up to a predefined maximum.
98 while ( (num_sessions > num_threads)
99 && (num_threads <= MAX_WORKER_THREADS) ) {
100 spawn_another_worker_thread();
102 lprintf(3, "There are %d sessions and %d threads active.\n",
103 num_sessions, num_threads);
109 * Wake up occasionally and clean house
111 void housekeeping_loop(void)
114 sleeeeeeeeeep(HOUSEKEEPING);
121 * Generate a unique WebCit session ID (which is not the same thing as the
122 * Citadel session ID).
124 * FIXME ... ensure that session number is truly unique
127 int GenerateSessionID(void)
129 static int seq = (-1);
132 seq = (int) time(NULL);
140 * Collapse multiple cookies on one line
142 int req_gets(int sock, char *buf, char *hold)
146 if (strlen(hold) == 0) {
148 a = client_gets(sock, buf);
151 safestrncpy(buf, hold, SIZ);
155 if (!strncasecmp(buf, "Cookie: ", 8)) {
156 for (a = 0; a < strlen(buf); ++a)
158 sprintf(hold, "Cookie: %s", &buf[a + 1]);
160 while (isspace(hold[8]))
161 strcpy(&hold[8], &hold[9]);
170 * lingering_close() a`la Apache. see
171 * http://www.apache.org/docs/misc/fin_wait_2.html for rationale
174 int lingering_close(int fd)
179 struct timeval tv, start;
181 gettimeofday(&start, NULL);
185 gettimeofday(&tv, NULL);
186 tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
187 tv.tv_usec = start.tv_usec - tv.tv_usec;
188 if (tv.tv_usec < 0) {
190 tv.tv_usec += 1000000;
194 i = select(fd + 1, &set, NULL, NULL, &tv);
195 } while (i == -1 && errno == EINTR);
200 i = read(fd, buf, sizeof buf);
201 } while (i != 0 && (i != -1 || errno == EINTR));
209 * Check for bogus requests coming from (for example) brain-dead
210 * Windoze boxes that are infected with the latest worm-of-the-week.
211 * If we detect one of these, bail out without bothering our Citadel
214 int is_bogus(char *http_cmd) {
216 if (!strncasecmp(http_cmd, "GET /scripts/root.exe", 21)) return(1);
217 if (!strncasecmp(http_cmd, "GET /c/winnt", 12)) return(2);
218 if (!strncasecmp(http_cmd, "GET /MSADC/", 11)) return(3);
220 return(0); /* probably ok */
226 * This loop gets called once for every HTTP connection made to WebCit. At
227 * this entry point we have an HTTP socket with a browser allegedly on the
228 * other end, but we have not yet bound to a WebCit session.
230 * The job of this function is to locate the correct session and bind to it,
231 * or create a session if necessary and bind to it, then run the WebCit
232 * transaction loop. Afterwards, we unbind from the session. When this
233 * function returns, the worker thread is then free to handle another
236 void context_loop(int sock)
238 struct httprequest *req = NULL;
239 struct httprequest *last = NULL;
240 struct httprequest *hptr;
241 char buf[SIZ], hold[SIZ];
242 int desired_session = 0;
245 struct wcsession *TheSession, *sptr;
246 char httpauth_string[SIZ];
247 char httpauth_user[SIZ];
248 char httpauth_pass[SIZ];
250 strcpy(httpauth_string, "");
251 strcpy(httpauth_user, DEFAULT_HTTPAUTH_USER);
252 strcpy(httpauth_pass, DEFAULT_HTTPAUTH_PASS);
255 * Find out what it is that the web browser is asking for
257 memset(hold, 0, sizeof(hold));
259 if (req_gets(sock, buf, hold) < 0) return;
264 if (!strncasecmp(buf, "Accept-encoding:", 16)) {
265 if (strstr(&buf[16], "gzip")) {
271 * Browser-based sessions use cookies for session authentication
273 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
274 cookie_to_stuff(&buf[15], &desired_session,
275 NULL, 0, NULL, 0, NULL, 0);
280 * GroupDAV-based sessions use HTTP authentication
282 if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
283 CtdlDecodeBase64(httpauth_string, &buf[21], strlen(&buf[21]));
284 extract_token(httpauth_user, httpauth_string, 0, ':', sizeof httpauth_user);
285 extract_token(httpauth_pass, httpauth_string, 1, ':', sizeof httpauth_pass);
288 if (!strncasecmp(buf, "If-Modified-Since: ", 19)) {
289 if_modified_since = httpdate_to_timestamp(&buf[19]);
293 * Read in the request
295 hptr = (struct httprequest *)
296 malloc(sizeof(struct httprequest));
304 safestrncpy(hptr->line, buf, sizeof hptr->line);
306 } while (strlen(buf) > 0);
308 safestrncpy(buf, req->line, sizeof buf);
309 lprintf(5, "HTTP: %s\n", buf);
311 /* Check for bogus requests */
312 if (is_bogus(buf)) goto bail;
315 * If requesting a non-root page, there should already be a cookie
316 * set. If there isn't, the client browser has cookies turned off
317 * (or doesn't support them) and we have to barf & bail.
319 remove_token(buf, 0, ' ');
320 if (buf[1]==' ') buf[1]=0;
323 * While we're at it, gracefully handle requests for the
324 * robots.txt and favicon.ico files.
326 if (!strncasecmp(buf, "/robots.txt", 11)) {
327 strcpy(req->line, "GET /static/robots.txt"
328 "?force_close_session=yes HTTP/1.1");
330 else if (!strncasecmp(buf, "/favicon.ico", 12)) {
331 strcpy(req->line, "GET /static/favicon.ico");
334 /* These are the URL's which may be executed without a
335 * session cookie already set. If it's not one of these,
336 * force the session to close because cookies are
337 * probably disabled on the client browser.
339 else if ( (strcmp(buf, "/"))
340 && (strncasecmp(buf, "/listsub", 8))
341 && (strncasecmp(buf, "/freebusy", 9))
342 && (strncasecmp(buf, "/do_logout", 10))
343 && (strncasecmp(buf, "/groupdav", 9))
344 && (strncasecmp(buf, "/rss", 4))
345 && (got_cookie == 0)) {
346 strcpy(req->line, "GET /static/nocookies.html"
347 "?force_close_session=yes HTTP/1.1");
351 * See if there's an existing session open with the desired ID or user/pass
355 if (TheSession == NULL) {
356 pthread_mutex_lock(&SessionListMutex);
357 for (sptr = SessionList; sptr != NULL; sptr = sptr->next) {
359 /* If HTTP-AUTH, look for a session with matching credentials */
360 if ( (strlen(httpauth_user) > 0)
361 &&(!strcasecmp(sptr->httpauth_user, httpauth_user))
362 &&(!strcasecmp(sptr->httpauth_pass, httpauth_pass)) ) {
366 /* If cookie-session, look for a session with matching session ID */
367 if ( (desired_session != 0) && (sptr->wc_session == desired_session)) {
372 pthread_mutex_unlock(&SessionListMutex);
376 * Create a new session if we have to
378 if (TheSession == NULL) {
379 lprintf(3, "Creating a new session\n");
380 TheSession = (struct wcsession *)
381 malloc(sizeof(struct wcsession));
382 memset(TheSession, 0, sizeof(struct wcsession));
383 TheSession->serv_sock = (-1);
384 TheSession->chat_sock = (-1);
385 TheSession->wc_session = GenerateSessionID();
386 strcpy(TheSession->httpauth_user, httpauth_user);
387 strcpy(TheSession->httpauth_pass, httpauth_pass);
388 pthread_mutex_init(&TheSession->SessionMutex, NULL);
390 pthread_mutex_lock(&SessionListMutex);
391 TheSession->next = SessionList;
392 SessionList = TheSession;
393 pthread_mutex_unlock(&SessionListMutex);
397 * A future improvement might be to check the session integrity
398 * at this point before continuing.
402 * Bind to the session and perform the transaction
404 pthread_mutex_lock(&TheSession->SessionMutex); /* bind */
405 pthread_setspecific(MyConKey, (void *)TheSession);
406 TheSession->http_sock = sock;
407 TheSession->lastreq = time(NULL); /* log */
408 TheSession->gzip_ok = gzip_ok;
409 session_loop(req); /* do transaction */
410 pthread_mutex_unlock(&TheSession->SessionMutex); /* unbind */
412 /* Free the request buffer */
413 bail: while (req != NULL) {
419 /* Free up any session-local substitution variables which
420 * were set during this transaction
422 clear_local_substs();