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.
11 #include "webserver.h"
16 #include "modules_init.h"
18 int vsnprintf(char *buf, size_t max, const char *fmt, va_list argp);
21 int verbosity = 9; /* Logging level */
22 int msock; /* master listening socket */
23 int is_https = 0; /* Nonzero if I am an HTTPS service */
24 int follow_xff = 0; /* Follow X-Forwarded-For: header */
25 int home_specified = 0; /* did the user specify a homedir? */
26 int time_to_die = 0; /* Nonzero if server is shutting down */
27 extern void *context_loop(int);
28 extern void *housekeeping_loop(void);
29 extern pthread_mutex_t SessionListMutex;
30 extern pthread_key_t MyConKey;
33 char ctdl_key_dir[PATH_MAX]=SSL_DIR;
34 char file_crpt_file_key[PATH_MAX]="";
35 char file_crpt_file_csr[PATH_MAX]="";
36 char file_crpt_file_cer[PATH_MAX]="";
38 char socket_dir[PATH_MAX]; /* where to talk to our citadel server */
39 static const char editor_absolut_dir[PATH_MAX]=EDITORDIR; /* nailed to what configure gives us. */
40 static char static_dir[PATH_MAX]; /* calculated on startup */
41 static char static_local_dir[PATH_MAX]; /* calculated on startup */
42 static char static_icon_dir[PATH_MAX]; /* where should we find our mime icons? */
43 char *static_dirs[]={ /* needs same sort order as the web mapping */
44 (char*)static_dir, /* our templates on disk */
45 (char*)static_local_dir, /* user provided templates disk */
46 (char*)editor_absolut_dir, /* the editor on disk */
47 (char*)static_icon_dir /* our icons... */
51 * Subdirectories from which the client may request static content
53 * (If you add more, remember to increment 'ndirs' below)
55 char *static_content_dirs[] = {
56 "static", /* static templates */
57 "static.local", /* site local static templates */
58 "tiny_mce" /* rich text editor */
64 char *server_cookie = NULL; /* our Cookie connection to the client */
65 int http_port = PORT_NUM; /* Port to listen on */
66 char *ctdlhost = DEFAULT_HOST; /* our name */
67 char *ctdlport = DEFAULT_PORT; /* our Port */
68 int setup_wizard = 0; /* should we run the setup wizard? \todo */
69 char wizard_filename[PATH_MAX]; /* where's the setup wizard? */
70 int running_as_daemon = 0; /* should we deamonize on startup? */
74 * This is a generic function to set up a master socket for listening on
75 * a TCP port. The server shuts down if the bind fails.
77 * ip_addr IP address to bind
78 * port_number port number to bind
79 * queue_len number of incoming connections to allow in the queue
81 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
83 struct sockaddr_in sin;
86 memset(&sin, 0, sizeof(sin));
87 sin.sin_family = AF_INET;
88 if (ip_addr == NULL) {
89 sin.sin_addr.s_addr = INADDR_ANY;
91 sin.sin_addr.s_addr = inet_addr(ip_addr);
94 if (sin.sin_addr.s_addr == INADDR_NONE) {
95 sin.sin_addr.s_addr = INADDR_ANY;
98 if (port_number == 0) {
99 lprintf(1, "Cannot start: no port number specified.\n");
102 sin.sin_port = htons((u_short) port_number);
104 s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
106 lprintf(1, "Can't create a socket: %s\n", strerror(errno));
109 /* Set some socket options that make sense. */
111 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
113 fcntl(s, F_SETFL, O_NONBLOCK); /* maide: this statement is incorrect
114 there should be a preceding F_GETFL
115 and a bitwise OR with the previous
118 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
119 lprintf(1, "Can't bind: %s\n", strerror(errno));
122 if (listen(s, queue_len) < 0) {
123 lprintf(1, "Can't listen: %s\n", strerror(errno));
132 * Create a Unix domain socket and listen on it
133 * sockpath - file name of the unix domain socket
134 * queue_len - Number of incoming connections to allow in the queue
136 int ig_uds_server(char *sockpath, int queue_len)
138 struct sockaddr_un addr;
141 int actual_queue_len;
143 actual_queue_len = queue_len;
144 if (actual_queue_len < 5) actual_queue_len = 5;
146 i = unlink(sockpath);
147 if (i != 0) if (errno != ENOENT) {
148 lprintf(1, "webcit: can't unlink %s: %s\n",
149 sockpath, strerror(errno));
153 memset(&addr, 0, sizeof(addr));
154 addr.sun_family = AF_UNIX;
155 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
157 s = socket(AF_UNIX, SOCK_STREAM, 0);
159 lprintf(1, "webcit: Can't create a socket: %s\n",
164 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
165 lprintf(1, "webcit: Can't bind: %s\n",
170 if (listen(s, actual_queue_len) < 0) {
171 lprintf(1, "webcit: Can't listen: %s\n",
176 chmod(sockpath, 0777);
184 * Read data from the client socket.
186 * sock socket fd to read from
187 * buf buffer to read into
188 * bytes number of bytes to read
189 * timeout Number of seconds to wait before timing out
191 * Possible return values:
192 * 1 Requested number of bytes has been read.
193 * 0 Request timed out.
194 * -1 Connection is broken, or other error.
196 int client_read_to(int sock, char *buf, int bytes, int timeout)
206 return (client_read_ssl(buf, bytes, timeout));
211 while (len < bytes) {
217 retval = select((sock) + 1, &rfds, NULL, NULL, &tv);
218 if (FD_ISSET(sock, &rfds) == 0) {
222 rlen = read(sock, &buf[len], bytes - len);
225 lprintf(2, "client_read() failed: %s\n",
233 write(2, "\033[32m", 5);
234 write(2, buf, bytes);
235 write(2, "\033[30m", 5);
241 * \brief Begin buffering HTTP output so we can transmit it all in one write operation later.
243 void begin_burst(void)
245 WC->WBuf = NewStrBufPlain(NULL, 32768);
250 * \brief Finish buffering HTTP output. [Compress using zlib and] output with a Content-Length: header.
254 struct wcsession *WCC = WC;
255 const char *ptr, *eptr;
262 /* Perform gzip compression, if enabled and supported by client */
263 if ((WCC->gzip_ok) && CompressBuffer(WCC->WBuf))
265 hprintf("Content-encoding: gzip\r\n");
267 #endif /* HAVE_ZLIB */
269 hprintf("Content-length: %d\r\n\r\n", StrLength(WCC->WBuf));
271 ptr = ChrPtr(WCC->HBuf);
272 count = StrLength(WCC->HBuf);
277 client_write_ssl(ptr, StrLength(WCC->HBuf));
285 write(2, "\033[34m", 5);
286 write(2, ptr, StrLength(WCC->WBuf));
287 write(2, "\033[30m", 5);
289 fdflags = fcntl(WC->http_sock, F_GETFL);
292 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
294 FD_SET(WCC->http_sock, &wset);
295 if (select(WCC->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
296 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
301 if ((res = write(WCC->http_sock,
304 lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
311 ptr = ChrPtr(WCC->WBuf);
312 count = StrLength(WCC->WBuf);
317 client_write_ssl(ptr, StrLength(WCC->HBuf));
324 write(2, "\033[34m", 5);
325 write(2, ptr, StrLength(WCC->WBuf));
326 write(2, "\033[30m", 5);
330 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
332 FD_SET(WCC->http_sock, &wset);
333 if (select(WCC->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
334 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
339 if ((res = write(WCC->http_sock,
342 lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
349 return StrLength(WCC->WBuf);
355 * \brief Read data from the client socket with default timeout.
356 * (This is implemented in terms of client_read_to() and could be
357 * justifiably moved out of sysdep.c)
358 * \param sock the socket fd to read from
359 * \param buf the buffer to write to
360 * \param bytes Number of bytes to read
362 int client_read(int sock, char *buf, int bytes)
364 return (client_read_to(sock, buf, bytes, SLEEPING));
369 * \brief Get a LF-terminated line of text from the client.
370 * (This is implemented in terms of client_read() and could be
371 * justifiably moved out of sysdep.c)
372 * \param sock socket fd to get client line from
373 * \param buf buffer to write read data to
374 * \param bufsiz how many bytes to read
375 * \return number of bytes read???
377 int client_getln(int sock, char *buf, int bufsiz)
381 /* Read one character at a time.*/
383 retval = client_read(sock, &buf[i], 1);
384 if (retval != 1 || buf[i] == '\n' || i == (bufsiz-1))
386 if ( (!isspace(buf[i])) && (!isprint(buf[i])) ) {
387 /* Non printable character recieved from client */
392 /* If we got a long line, discard characters until the newline. */
394 while (buf[i] != '\n' && retval == 1)
395 retval = client_read(sock, &buf[i], 1);
398 * Strip any trailing non-printable characters.
401 while ((i > 0) && (!isprint(buf[i - 1]))) {
408 * \brief Shut us down the regular way.
409 * \param signum the signal we want to forward
412 void graceful_shutdown_watcher(int signum) {
413 lprintf (1, "bye; shutting down watcher.");
414 kill(current_child, signum);
415 if (signum != SIGHUP)
420 * \brief shut us down the regular way.
421 * \param signum the signal we want to forward
424 void graceful_shutdown(int signum) {
425 // kill(current_child, signum);
430 lprintf (1, "bye going down gracefull.[%d][%s]\n", signum, wd);
442 * \brief Start running as a daemon.
444 * param do_close_stdio Only close stdio if set.
448 * Start running as a daemon.
450 void start_daemon(char *pid_file)
459 /* Close stdin/stdout/stderr and replace them with /dev/null.
460 * We don't just call close() because we don't want these fd's
461 * to be reused for other files.
465 signal(SIGHUP, SIG_IGN);
466 signal(SIGINT, SIG_IGN);
467 signal(SIGQUIT, SIG_IGN);
476 freopen("/dev/null", "r", stdin);
477 freopen("/dev/null", "w", stdout);
478 freopen("/dev/null", "w", stderr);
479 signal(SIGTERM, graceful_shutdown_watcher);
480 signal(SIGHUP, graceful_shutdown_watcher);
483 current_child = fork();
486 if (current_child < 0) {
488 ShutDownLibCitadel ();
492 else if (current_child == 0) { // child process
493 // signal(SIGTERM, graceful_shutdown);
494 signal(SIGHUP, graceful_shutdown);
496 return; /* continue starting webcit. */
499 else { // watcher process
500 // signal(SIGTERM, SIG_IGN);
501 // signal(SIGHUP, SIG_IGN);
503 fp = fopen(pid_file, "w");
505 fprintf(fp, "%d\n", getpid());
509 waitpid(current_child, &status, 0);
514 /* Did the main process exit with an actual exit code? */
515 if (WIFEXITED(status)) {
517 /* Exit code 0 means the watcher should exit */
518 if (WEXITSTATUS(status) == 0) {
522 /* Exit code 101-109 means the watcher should exit */
523 else if ( (WEXITSTATUS(status) >= 101) && (WEXITSTATUS(status) <= 109) ) {
527 /* Any other exit code means we should restart. */
533 /* Any other type of termination (signals, etc.) should also restart. */
538 } while (do_restart);
543 ShutDownLibCitadel ();
544 exit(WEXITSTATUS(status));
548 * \brief Spawn an additional worker thread into the pool.
550 void spawn_another_worker_thread()
552 pthread_t SessThread; /*< Thread descriptor */
553 pthread_attr_t attr; /*< Thread attributes */
556 lprintf(3, "Creating a new thread\n");
558 /* set attributes for the new thread */
559 pthread_attr_init(&attr);
560 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
563 * Our per-thread stacks need to be bigger than the default size, otherwise
564 * the MIME parser crashes on FreeBSD, and the IMAP service crashes on
567 if ((ret = pthread_attr_setstacksize(&attr, 1024 * 1024))) {
568 lprintf(1, "pthread_attr_setstacksize: %s\n",
570 pthread_attr_destroy(&attr);
573 /* now create the thread */
574 if (pthread_create(&SessThread, &attr,
575 (void *(*)(void *)) worker_entry, NULL)
577 lprintf(1, "Can't create thread: %s\n", strerror(errno));
580 /* free up the attributes */
581 pthread_attr_destroy(&attr);
584 const char foobuf[32];
585 const char *nix(void *vptr) {snprintf(foobuf, 32, "%0x", (long) vptr); return foobuf;}
587 * \brief Here's where it all begins.
588 * \param argc number of commandline args
589 * \param argv the commandline arguments
591 int main(int argc, char **argv)
593 pthread_t SessThread; /*< Thread descriptor */
594 pthread_attr_t attr; /*< Thread attributes */
595 int a, i; /*< General-purpose variables */
596 char tracefile[PATH_MAX];
597 char ip_addr[256]="0.0.0.0";
598 char dirbuffer[PATH_MAX]="";
601 int home_specified=0;
602 char relhome[PATH_MAX]="";
603 char webcitdir[PATH_MAX] = DATADIR;
604 char *pidfile = NULL;
610 #endif /* ENABLE_NLS */
611 char uds_listen_path[PATH_MAX]; /*< listen on a unix domain socket? */
613 HandlerHash = NewHash(1, NULL);
614 initialise_modules();
615 dbg_PrintHash(HandlerHash, nix, NULL);
617 /* Ensure that we are linked to the correct version of libcitadel */
618 if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
619 fprintf(stderr, " You are running libcitadel version %d.%02d\n",
620 (libcitadel_version_number() / 100), (libcitadel_version_number() % 100));
621 fprintf(stderr, "WebCit was compiled against version %d.%02d\n",
622 (LIBCITADEL_VERSION_NUMBER / 100), (LIBCITADEL_VERSION_NUMBER % 100));
626 strcpy(uds_listen_path, "");
628 /* Parse command line */
630 while ((a = getopt(argc, argv, "h:i:p:t:x:dD:cfs")) != EOF)
632 while ((a = getopt(argc, argv, "h:i:p:t:x:dD:cf")) != EOF)
636 hdir = strdup(optarg);
638 if (!relh) safestrncpy(webcitdir, hdir,
641 safestrncpy(relhome, relhome,
643 /* free(hdir); TODO: SHOULD WE DO THIS? */
648 running_as_daemon = 1;
651 pidfile = strdup(optarg);
652 running_as_daemon = 1;
655 safestrncpy(ip_addr, optarg, sizeof ip_addr);
658 http_port = atoi(optarg);
659 if (http_port == 0) {
660 safestrncpy(uds_listen_path, optarg, sizeof uds_listen_path);
664 safestrncpy(tracefile, optarg, sizeof tracefile);
665 freopen(tracefile, "w", stdout);
666 freopen(tracefile, "w", stderr);
667 freopen(tracefile, "r", stdin);
670 verbosity = atoi(optarg);
676 server_cookie = malloc(256);
677 if (server_cookie != NULL) {
678 safestrncpy(server_cookie,
679 "Set-cookie: wcserver=",
682 (&server_cookie[strlen(server_cookie)],
684 lprintf(2, "gethostname: %s\n",
694 fprintf(stderr, "usage: webcit "
695 "[-i ip_addr] [-p http_port] "
696 "[-t tracefile] [-c] [-f] "
701 "[remotehost [remoteport]]\n");
706 ctdlhost = argv[optind];
708 ctdlport = argv[optind];
711 /* daemonize, if we were asked to */
712 if (running_as_daemon) {
713 start_daemon(pidfile);
716 /// signal(SIGTERM, graceful_shutdown);
717 signal(SIGHUP, graceful_shutdown);
720 /* Tell 'em who's in da house */
721 lprintf(1, PACKAGE_STRING "\n");
722 lprintf(1, "Copyright (C) 1996-2008 by the Citadel development team.\n"
723 "This software is distributed under the terms of the "
724 "GNU General Public License.\n\n"
728 /* initialize the International Bright Young Thing */
730 initialize_locales();
732 locale = setlocale(LC_ALL, "");
734 mo = malloc(strlen(webcitdir) + 20);
735 lprintf(9, "Message catalog directory: %s\n", bindtextdomain("webcit", LOCALEDIR"/locale"));
737 lprintf(9, "Text domain: %s\n", textdomain("webcit"));
738 lprintf(9, "Text domain Charset: %s\n", bind_textdomain_codeset("webcit","UTF8"));
743 /* calculate all our path on a central place */
744 /* where to keep our config */
746 #define COMPUTE_DIRECTORY(SUBDIR) memcpy(dirbuffer,SUBDIR, sizeof dirbuffer);\
747 snprintf(SUBDIR,sizeof SUBDIR, "%s%s%s%s%s%s%s", \
748 (home&!relh)?webcitdir:basedir, \
749 ((basedir!=webcitdir)&(home&!relh))?basedir:"/", \
750 ((basedir!=webcitdir)&(home&!relh))?"/":"", \
752 (relhome[0]!='\0')?"/":"",\
754 (dirbuffer[0]!='\0')?"/":"");
756 COMPUTE_DIRECTORY(socket_dir);
757 basedir=WWWDIR "/static";
758 COMPUTE_DIRECTORY(static_dir);
759 basedir=WWWDIR "/static/icons";
760 COMPUTE_DIRECTORY(static_icon_dir);
761 basedir=WWWDIR "/static.local";
762 COMPUTE_DIRECTORY(static_local_dir);
764 snprintf(file_crpt_file_key,
765 sizeof file_crpt_file_key,
768 snprintf(file_crpt_file_csr,
769 sizeof file_crpt_file_csr,
772 snprintf(file_crpt_file_cer,
773 sizeof file_crpt_file_cer,
777 /* we should go somewhere we can leave our coredump, if enabled... */
778 lprintf(9, "Changing directory to %s\n", socket_dir);
779 if (chdir(webcitdir) != 0) {
782 LoadIconDir(static_icon_dir);
783 initialize_viewdefs();
787 * Set up a place to put thread-specific data.
788 * We only need a single pointer per thread - it points to the
789 * wcsession struct to which the thread is currently bound.
791 if (pthread_key_create(&MyConKey, NULL) != 0) {
792 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
794 InitialiseSemaphores ();
797 * Set up a place to put thread-specific SSL data.
798 * We don't stick this in the wcsession struct because SSL starts
799 * up before the session is bound, and it gets torn down between
803 if (pthread_key_create(&ThreadSSL, NULL) != 0) {
804 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
809 * Bind the server to our favorite port.
810 * There is no need to check for errors, because ig_tcp_server()
811 * exits if it doesn't succeed.
814 if (!IsEmptyStr(uds_listen_path)) {
815 lprintf(2, "Attempting to create listener socket at %s...\n", uds_listen_path);
816 msock = ig_uds_server(uds_listen_path, LISTEN_QUEUE_LENGTH);
819 lprintf(2, "Attempting to bind to port %d...\n", http_port);
820 msock = ig_tcp_server(ip_addr, http_port, LISTEN_QUEUE_LENGTH);
823 lprintf(2, "Listening on socket %d\n", msock);
824 signal(SIGPIPE, SIG_IGN);
826 pthread_mutex_init(&SessionListMutex, NULL);
829 * Start up the housekeeping thread
831 pthread_attr_init(&attr);
832 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
833 pthread_create(&SessThread, &attr,
834 (void *(*)(void *)) housekeeping_loop, NULL);
838 * If this is an HTTPS server, fire up SSL
846 /* Start a few initial worker threads */
847 for (i = 0; i < (MIN_WORKER_THREADS); ++i) {
848 spawn_another_worker_thread();
851 /* now the original thread becomes another worker */
853 ShutDownLibCitadel ();
859 * Entry point for worker threads
861 void worker_entry(void)
865 int fail_this_transaction = 0;
868 fd_set readset, tempset;
873 FD_SET(msock, &readset);
876 /* Only one thread can accept at a time */
877 fail_this_transaction = 0;
881 ret = -1; /* just one at once should select... */
882 begin_critical_section(S_SELECT);
885 if (msock > 0) FD_SET(msock, &tempset);
888 if (msock > 0) ret = select(msock+1, &tempset, NULL, NULL, &tv);
889 end_critical_section(S_SELECT);
890 if ((ret < 0) && (errno != EINTR) && (errno != EAGAIN))
891 {// EINTR and EAGAIN are thrown but not of interest.
892 lprintf(2, "accept() failed:%d %s\n",
893 errno, strerror(errno));
895 else if ((ret > 0) && (msock > 0) && FD_ISSET(msock, &tempset))
896 {// Successfully selected, and still not shutting down? Accept!
897 ssock = accept(msock, NULL, 0);
900 } while ((msock > 0) && (ssock < 0) && (time_to_die == 0));
902 if ((msock == -1)||(time_to_die))
903 {// ok, we're going down.
906 /* the first to come here will have to do the cleanup.
907 * make shure its realy just one.
909 begin_critical_section(S_SHUTDOWN);
915 end_critical_section(S_SHUTDOWN);
917 {// we're the one to cleanup the mess.
918 lprintf(2, "I'm master shutdown: tagging sessions to be killed.\n");
920 lprintf(2, "master shutdown: waiting for others\n");
921 sleeeeeeeeeep(1); // wait so some others might finish...
922 lprintf(2, "master shutdown: cleaning up sessions\n");
924 lprintf(2, "master shutdown: cleaning up libical\n");
925 free_zone_directory ();
926 icaltimezone_release_zone_tab ();
927 icalmemory_free_ring ();
928 ShutDownLibCitadel ();
929 lprintf(2, "master shutdown exiting!.\n");
934 if (ssock < 0 ) continue;
937 if (ssock > 0) close (ssock);
938 lprintf(2, "inbetween.");
940 } else { // Got it? do some real work!
941 /* Set the SO_REUSEADDR socket option */
943 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
946 /* If we are an HTTPS server, go crypto now. */
949 if (starttls(ssock) != 0) {
950 fail_this_transaction = 1;
956 if (fail_this_transaction == 0) {
958 /* Perform an HTTP transaction... */
961 /* Shut down SSL/TLS if required... */
968 /* ...and close the socket. */
969 lingering_close(ssock);
974 } while (!time_to_die);
976 lprintf (1, "bye\n");
981 * \brief print log messages
982 * logs to stderr if loglevel is lower than the verbosity set at startup
983 * \param loglevel level of the message
984 * \param format the printf like format string
985 * \param ... the strings to put into format
987 int lprintf(int loglevel, const char *format, ...)
991 if (loglevel <= verbosity) {
992 va_start(ap, format);
993 vfprintf(stderr, format, ap);
1002 * \brief print the actual stack frame.
1004 void wc_backtrace(void)
1006 #ifdef HAVE_BACKTRACE
1007 void *stack_frames[50];
1012 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
1013 strings = backtrace_symbols(stack_frames, size);
1014 for (i = 0; i < size; i++) {
1015 if (strings != NULL)
1016 lprintf(1, "%s\n", strings[i]);
1018 lprintf(1, "%p\n", stack_frames[i]);