4 * This contains a simple multithreaded TCP server manager. It sits around
5 * waiting on the specified port for incoming HTTP connections. When a
6 * connection is established, it calls context_loop() from context_loop.c.
8 * Copyright (c) 1996-2009 by the citadel.org developers.
9 * This program is released under the terms of the GNU General Public License v3.
14 #include "webserver.h"
19 #include "modules_init.h"
21 int vsnprintf(char *buf, size_t max, const char *fmt, va_list argp);
24 int verbosity = 9; /* Logging level */
25 int msock; /* master listening socket */
26 int is_https = 0; /* Nonzero if I am an HTTPS service */
27 int follow_xff = 0; /* Follow X-Forwarded-For: header */
28 int home_specified = 0; /* did the user specify a homedir? */
29 int time_to_die = 0; /* Nonzero if server is shutting down */
31 extern void *context_loop(int*);
32 extern void *housekeeping_loop(void);
33 extern pthread_mutex_t SessionListMutex;
34 extern pthread_key_t MyConKey;
36 extern int ig_tcp_server(char *ip_addr, int port_number, int queue_len);
37 extern int ig_uds_server(char *sockpath, int queue_len);
40 char ctdl_key_dir[PATH_MAX]=SSL_DIR;
41 char file_crpt_file_key[PATH_MAX]="";
42 char file_crpt_file_csr[PATH_MAX]="";
43 char file_crpt_file_cer[PATH_MAX]="";
45 char socket_dir[PATH_MAX]; /* where to talk to our citadel server */
46 const char editor_absolut_dir[PATH_MAX]=EDITORDIR; /* nailed to what configure gives us. */
47 char static_dir[PATH_MAX]; /* calculated on startup */
48 char static_local_dir[PATH_MAX]; /* calculated on startup */
49 char static_icon_dir[PATH_MAX]; /* where should we find our mime icons? */
50 char *static_dirs[]={ /* needs same sort order as the web mapping */
51 (char*)static_dir, /* our templates on disk */
52 (char*)static_local_dir, /* user provided templates disk */
53 (char*)editor_absolut_dir, /* the editor on disk */
54 (char*)static_icon_dir /* our icons... */
58 * Subdirectories from which the client may request static content
60 * (If you add more, remember to increment 'ndirs' below)
62 char *static_content_dirs[] = {
63 "static", /* static templates */
64 "static.local", /* site local static templates */
65 "tiny_mce" /* rich text editor */
71 char *server_cookie = NULL; /* our Cookie connection to the client */
72 int http_port = PORT_NUM; /* Port to listen on */
73 char *ctdlhost = DEFAULT_HOST; /* our name */
74 char *ctdlport = DEFAULT_PORT; /* our Port */
75 int setup_wizard = 0; /* should we run the setup wizard? \todo */
76 char wizard_filename[PATH_MAX]; /* where's the setup wizard? */
77 int running_as_daemon = 0; /* should we deamonize on startup? */
82 * Shut us down the regular way.
83 * signum is the signal we want to forward
86 void graceful_shutdown_watcher(int signum) {
87 lprintf (1, "bye; shutting down watcher.");
88 kill(current_child, signum);
97 * Shut us down the regular way.
98 * signum is the signal we want to forward
101 void graceful_shutdown(int signum) {
106 lprintf (1, "bye going down gracefull.[%d][%s]\n", signum, wd);
118 * Start running as a daemon.
120 void start_daemon(char *pid_file)
129 /* Close stdin/stdout/stderr and replace them with /dev/null.
130 * We don't just call close() because we don't want these fd's
131 * to be reused for other files.
135 signal(SIGHUP, SIG_IGN);
136 signal(SIGINT, SIG_IGN);
137 signal(SIGQUIT, SIG_IGN);
146 freopen("/dev/null", "r", stdin);
147 freopen("/dev/null", "w", stdout);
148 freopen("/dev/null", "w", stderr);
149 signal(SIGTERM, graceful_shutdown_watcher);
150 signal(SIGHUP, graceful_shutdown_watcher);
153 current_child = fork();
156 if (current_child < 0) {
158 ShutDownLibCitadel ();
162 else if (current_child == 0) { /* child process */
163 signal(SIGHUP, graceful_shutdown);
165 return; /* continue starting webcit. */
167 else { /* watcher process */
169 fp = fopen(pid_file, "w");
171 fprintf(fp, "%d\n", getpid());
175 waitpid(current_child, &status, 0);
180 /* Did the main process exit with an actual exit code? */
181 if (WIFEXITED(status)) {
183 /* Exit code 0 means the watcher should exit */
184 if (WEXITSTATUS(status) == 0) {
188 /* Exit code 101-109 means the watcher should exit */
189 else if ( (WEXITSTATUS(status) >= 101) && (WEXITSTATUS(status) <= 109) ) {
193 /* Any other exit code means we should restart. */
199 /* Any other type of termination (signals, etc.) should also restart. */
204 } while (do_restart);
209 ShutDownLibCitadel ();
210 exit(WEXITSTATUS(status));
214 * Spawn an additional worker thread into the pool.
216 void spawn_another_worker_thread()
218 pthread_t SessThread; /* Thread descriptor */
219 pthread_attr_t attr; /* Thread attributes */
222 lprintf(3, "Creating a new thread\n");
224 /* set attributes for the new thread */
225 pthread_attr_init(&attr);
226 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
229 * Our per-thread stacks need to be bigger than the default size, otherwise
230 * the MIME parser crashes on FreeBSD, and the IMAP service crashes on
233 if ((ret = pthread_attr_setstacksize(&attr, 1024 * 1024))) {
234 lprintf(1, "pthread_attr_setstacksize: %s\n",
236 pthread_attr_destroy(&attr);
239 /* now create the thread */
240 if (pthread_create(&SessThread, &attr,
241 (void *(*)(void *)) worker_entry, NULL)
243 lprintf(1, "Can't create thread: %s\n", strerror(errno));
246 /* free up the attributes */
247 pthread_attr_destroy(&attr);
250 /* #define DBG_PRINNT_HOOKS_AT_START */
251 #ifdef DBG_PRINNT_HOOKS_AT_START
252 const char foobuf[32];
253 const char *nix(void *vptr) {snprintf(foobuf, 32, "%0x", (long) vptr); return foobuf;}
255 extern int dbg_analyze_msg;
256 extern int dbg_bactrace_template_errors;
257 extern int DumpTemplateI18NStrings;
258 extern StrBuf *I18nDump;
259 void InitTemplateCache(void);
260 extern int LoadTemplates;
262 extern HashList *HandlerHash;
267 webcit_calc_dirs_n_files(int relh, const char *basedir, int home, char *webcitdir, char *relhome)
269 char dirbuffer[PATH_MAX]="";
270 /* calculate all our path on a central place */
271 /* where to keep our config */
273 #define COMPUTE_DIRECTORY(SUBDIR) memcpy(dirbuffer,SUBDIR, sizeof dirbuffer);\
274 snprintf(SUBDIR,sizeof SUBDIR, "%s%s%s%s%s%s%s", \
275 (home&!relh)?webcitdir:basedir, \
276 ((basedir!=webcitdir)&(home&!relh))?basedir:"/", \
277 ((basedir!=webcitdir)&(home&!relh))?"/":"", \
279 (relhome[0]!='\0')?"/":"",\
281 (dirbuffer[0]!='\0')?"/":"");
283 COMPUTE_DIRECTORY(socket_dir);
284 basedir=WWWDIR "/static";
285 COMPUTE_DIRECTORY(static_dir);
286 basedir=WWWDIR "/static/icons";
287 COMPUTE_DIRECTORY(static_icon_dir);
288 basedir=WWWDIR "/static.local";
289 COMPUTE_DIRECTORY(static_local_dir);
290 StripSlashes(static_dir, 1);
291 StripSlashes(static_icon_dir, 1);
292 StripSlashes(static_local_dir, 1);
294 snprintf(file_crpt_file_key,
295 sizeof file_crpt_file_key,
298 snprintf(file_crpt_file_csr,
299 sizeof file_crpt_file_csr,
302 snprintf(file_crpt_file_cer,
303 sizeof file_crpt_file_cer,
307 /* we should go somewhere we can leave our coredump, if enabled... */
308 lprintf(9, "Changing directory to %s\n", socket_dir);
309 if (chdir(webcitdir) != 0) {
314 * Here's where it all begins.
316 int main(int argc, char **argv)
318 size_t basesize = 2; /* how big should strbufs be on creation? */
319 pthread_t SessThread; /* Thread descriptor */
320 pthread_attr_t attr; /* Thread attributes */
321 int a, i; /* General-purpose variables */
322 char tracefile[PATH_MAX];
323 char ip_addr[256]="0.0.0.0";
326 int home_specified=0;
327 char relhome[PATH_MAX]="";
328 char webcitdir[PATH_MAX] = DATADIR;
329 char *pidfile = NULL;
335 #endif /* ENABLE_NLS */
336 char uds_listen_path[PATH_MAX]; /* listen on a unix domain socket? */
337 const char *I18nDumpFile = NULL;
339 WildFireInitBacktrace(argv[0], 2);
343 #ifdef DBG_PRINNT_HOOKS_AT_START
344 /* dbg_PrintHash(HandlerHash, nix, NULL);*/
347 /* Ensure that we are linked to the correct version of libcitadel */
348 if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
349 fprintf(stderr, " You are running libcitadel version %d.%02d\n",
350 (libcitadel_version_number() / 100), (libcitadel_version_number() % 100));
351 fprintf(stderr, "WebCit was compiled against version %d.%02d\n",
352 (LIBCITADEL_VERSION_NUMBER / 100), (LIBCITADEL_VERSION_NUMBER % 100));
356 strcpy(uds_listen_path, "");
358 /* Parse command line */
360 while ((a = getopt(argc, argv, "h:i:p:t:T:B:x:dD:G:cfsZ")) != EOF)
362 while ((a = getopt(argc, argv, "h:i:p:t:T:B:x:dD:G:cfZ")) != EOF)
366 hdir = strdup(optarg);
368 if (!relh) safestrncpy(webcitdir, hdir,
371 safestrncpy(relhome, relhome,
373 /* free(hdir); TODO: SHOULD WE DO THIS? */
378 running_as_daemon = 1;
381 pidfile = strdup(optarg);
382 running_as_daemon = 1;
384 case 'B': /* Basesize */
385 basesize = atoi(optarg);
387 StartLibCitadel(basesize);
390 safestrncpy(ip_addr, optarg, sizeof ip_addr);
393 http_port = atoi(optarg);
394 if (http_port == 0) {
395 safestrncpy(uds_listen_path, optarg, sizeof uds_listen_path);
399 safestrncpy(tracefile, optarg, sizeof tracefile);
400 freopen(tracefile, "w", stdout);
401 freopen(tracefile, "w", stderr);
402 freopen(tracefile, "r", stdin);
405 LoadTemplates = atoi(optarg);
406 dbg_analyze_msg = (LoadTemplates && (1<<1)) != 0;
407 dbg_bactrace_template_errors = (LoadTemplates && (1<<2)) != 0;
413 verbosity = atoi(optarg);
419 server_cookie = malloc(256);
420 if (server_cookie != NULL) {
421 safestrncpy(server_cookie,
422 "Set-cookie: wcserver=",
425 (&server_cookie[strlen(server_cookie)],
427 lprintf(2, "gethostname: %s\n",
437 DumpTemplateI18NStrings = 1;
438 I18nDump = NewStrBufPlain(HKEY("int templatestrings(void)\n{\n"));
439 I18nDumpFile = optarg;
442 fprintf(stderr, "usage: webcit "
443 "[-i ip_addr] [-p http_port] "
444 "[-t tracefile] [-c] [-f] "
445 "[-T Templatedebuglevel] "
446 "[-d] [-Z] [-G i18ndumpfile] "
450 "[remotehost [remoteport]]\n");
455 ctdlhost = argv[optind];
457 ctdlport = argv[optind];
460 /* daemonize, if we were asked to */
461 if (!DumpTemplateI18NStrings && running_as_daemon) {
462 start_daemon(pidfile);
465 signal(SIGHUP, graceful_shutdown);
468 webcit_calc_dirs_n_files(relh, basedir, home, webcitdir, relhome);
469 LoadIconDir(static_icon_dir);
471 /* Tell 'em who's in da house */
472 lprintf(1, PACKAGE_STRING "\n");
473 lprintf(1, "Copyright (C) 1996-2009 by the Citadel development team.\n"
474 "This software is distributed under the terms of the "
475 "GNU General Public License.\n\n"
479 /* initialize the International Bright Young Thing */
481 initialize_locales();
484 locale = setlocale(LC_ALL, "");
486 mo = malloc(strlen(webcitdir) + 20);
487 lprintf(9, "Message catalog directory: %s\n", bindtextdomain("webcit", LOCALEDIR"/locale"));
489 lprintf(9, "Text domain: %s\n", textdomain("webcit"));
490 lprintf(9, "Text domain Charset: %s\n", bind_textdomain_codeset("webcit","UTF8"));
493 initialise_modules();
494 initialize_viewdefs();
498 if (DumpTemplateI18NStrings) {
500 StrBufAppendBufPlain(I18nDump, HKEY("}\n"), 0);
501 if (StrLength(I18nDump) < 50) {
502 lprintf(1, "********************************************************************************\n");
503 lprintf(1, "* No strings found in templates! are you shure they're there? *\n");
504 lprintf(1, "********************************************************************************\n");
507 fd = fopen(I18nDumpFile, "w");
509 lprintf(1, "********************************************************************************\n");
510 lprintf(1, "* unable to open I18N dumpfile [%s] *\n", I18nDumpFile);
511 lprintf(1, "********************************************************************************\n");
514 fwrite(ChrPtr(I18nDump), 1, StrLength(I18nDump), fd);
520 /* Tell libical to return an error instead of aborting if it sees badly formed iCalendar data. */
521 icalerror_errors_are_fatal = 0;
523 /* Use our own prefix on tzid's generated from system tzdata */
524 icaltimezone_set_tzid_prefix("/citadel.org/");
527 * Set up a place to put thread-specific data.
528 * We only need a single pointer per thread - it points to the
529 * wcsession struct to which the thread is currently bound.
531 if (pthread_key_create(&MyConKey, NULL) != 0) {
532 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
534 InitialiseSemaphores ();
537 * Set up a place to put thread-specific SSL data.
538 * We don't stick this in the wcsession struct because SSL starts
539 * up before the session is bound, and it gets torn down between
543 if (pthread_key_create(&ThreadSSL, NULL) != 0) {
544 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
549 * Bind the server to our favorite port.
550 * There is no need to check for errors, because ig_tcp_server()
551 * exits if it doesn't succeed.
554 if (!IsEmptyStr(uds_listen_path)) {
555 lprintf(2, "Attempting to create listener socket at %s...\n", uds_listen_path);
556 msock = ig_uds_server(uds_listen_path, LISTEN_QUEUE_LENGTH);
559 lprintf(2, "Attempting to bind to port %d...\n", http_port);
560 msock = ig_tcp_server(ip_addr, http_port, LISTEN_QUEUE_LENGTH);
563 lprintf(2, "Listening on socket %d\n", msock);
564 signal(SIGPIPE, SIG_IGN);
566 pthread_mutex_init(&SessionListMutex, NULL);
569 * Start up the housekeeping thread
571 pthread_attr_init(&attr);
572 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
573 pthread_create(&SessThread, &attr,
574 (void *(*)(void *)) housekeeping_loop, NULL);
578 * If this is an HTTPS server, fire up SSL
586 /* Start a few initial worker threads */
587 for (i = 0; i < (MIN_WORKER_THREADS); ++i) {
588 spawn_another_worker_thread();
591 /* now the original thread becomes another worker */
593 ShutDownLibCitadel ();
598 void ShutDownWebcit(void)
600 free_zone_directory ();
601 icaltimezone_release_zone_tab ();
602 icalmemory_free_ring ();
603 ShutDownLibCitadel ();
616 * Entry point for worker threads
618 void worker_entry(void)
622 int fail_this_transaction = 0;
625 fd_set readset, tempset;
630 FD_SET(msock, &readset);
633 /* Only one thread can accept at a time */
634 fail_this_transaction = 0;
638 ret = -1; /* just one at once should select... */
639 begin_critical_section(S_SELECT);
642 if (msock > 0) FD_SET(msock, &tempset);
645 if (msock > 0) ret = select(msock+1, &tempset, NULL, NULL, &tv);
646 end_critical_section(S_SELECT);
647 if ((ret < 0) && (errno != EINTR) && (errno != EAGAIN))
648 {/* EINTR and EAGAIN are thrown but not of interest. */
649 lprintf(2, "accept() failed:%d %s\n",
650 errno, strerror(errno));
652 else if ((ret > 0) && (msock > 0) && FD_ISSET(msock, &tempset))
653 {/* Successfully selected, and still not shutting down? Accept! */
654 ssock = accept(msock, NULL, 0);
657 } while ((msock > 0) && (ssock < 0) && (time_to_die == 0));
659 if ((msock == -1)||(time_to_die))
660 {/* ok, we're going down. */
663 /* the first to come here will have to do the cleanup.
664 * make shure its realy just one.
666 begin_critical_section(S_SHUTDOWN);
672 end_critical_section(S_SHUTDOWN);
674 {/* we're the one to cleanup the mess. */
675 lprintf(2, "I'm master shutdown: tagging sessions to be killed.\n");
677 lprintf(2, "master shutdown: waiting for others\n");
678 sleeeeeeeeeep(1); /* wait so some others might finish... */
679 lprintf(2, "master shutdown: cleaning up sessions\n");
681 lprintf(2, "master shutdown: cleaning up libical\n");
685 lprintf(2, "master shutdown exiting!.\n");
690 if (ssock < 0 ) continue;
693 if (ssock > 0) close (ssock);
694 lprintf(2, "inbetween.");
696 } else { /* Got it? do some real work! */
697 /* Set the SO_REUSEADDR socket option */
699 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
702 /* If we are an HTTPS server, go crypto now. */
705 if (starttls(ssock) != 0) {
706 fail_this_transaction = 1;
713 fdflags = fcntl(ssock, F_GETFL);
715 lprintf(1, "unable to get server socket flags! %s \n",
717 fdflags = fdflags | O_NONBLOCK;
718 if (fcntl(ssock, F_SETFD, fdflags) < 0)
719 lprintf(1, "unable to set server socket nonblocking flags! %s \n",
724 if (fail_this_transaction == 0) {
726 memset(&Hdr, 0, sizeof(ParsedHttpHdrs));
727 Hdr.HR.eReqType = eGET;
728 Hdr.http_sock = ssock;
730 /* Perform an HTTP transaction... */
733 /* Shut down SSL/TLS if required... */
740 /* ...and close the socket. */
741 if (Hdr.http_sock > 0)
742 lingering_close(ssock);
747 } while (!time_to_die);
749 lprintf (1, "bye\n");
755 * logs to stderr if loglevel is lower than the verbosity set at startup
757 * loglevel level of the message
758 * format the printf like format string
759 * ... the strings to put into format
761 int lprintf(int loglevel, const char *format, ...)
765 if (loglevel <= verbosity) {
766 va_start(ap, format);
767 vfprintf(stderr, format, ap);
776 * print the actual stack frame.
778 void wc_backtrace(void)
780 #ifdef HAVE_BACKTRACE
781 void *stack_frames[50];
786 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
787 strings = backtrace_symbols(stack_frames, size);
788 for (i = 0; i < size; i++) {
790 lprintf(1, "%s\n", strings[i]);
792 lprintf(1, "%p\n", stack_frames[i]);