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-2010 by the citadel.org developers.
9 * This program is released under the terms of the GNU General Public License v3.
14 #include "webserver.h"
16 #include "modules_init.h"
18 int vsnprintf(char *buf, size_t max, const char *fmt, va_list argp);
22 extern int msock; /* master listening socket */
23 extern int verbosity; /* Logging level */
24 extern char static_icon_dir[PATH_MAX]; /* where should we find our mime icons */
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? */
30 extern pthread_mutex_t SessionListMutex;
31 extern pthread_key_t MyConKey;
33 extern void *housekeeping_loop(void);
34 extern int ig_tcp_server(char *ip_addr, int port_number, int queue_len);
35 extern int ig_uds_server(char *sockpath, int queue_len);
36 extern void graceful_shutdown_watcher(int signum);
37 extern void graceful_shutdown(int signum);
38 extern void start_daemon(char *pid_file);
39 extern void webcit_calc_dirs_n_files(int relh, const char *basedir, int home, char *webcitdir, char *relhome);
41 extern void drop_root(uid_t UID);
43 char socket_dir[PATH_MAX]; /* where to talk to our citadel server */
45 char *server_cookie = NULL; /* our Cookie connection to the client */
46 int http_port = PORT_NUM; /* Port to listen on */
47 char *ctdlhost = DEFAULT_HOST; /* our name */
48 char *ctdlport = DEFAULT_PORT; /* our Port */
49 int setup_wizard = 0; /* should we run the setup wizard? \todo */
50 char wizard_filename[PATH_MAX]; /* where's the setup wizard? */
51 int running_as_daemon = 0; /* should we deamonize on startup? */
55 /* #define DBG_PRINNT_HOOKS_AT_START */
56 #ifdef DBG_PRINNT_HOOKS_AT_START
57 extern HashList *HandlerHash;
58 const char foobuf[32];
59 const char *nix(void *vptr) {snprintf(foobuf, 32, "%0x", (long) vptr); return foobuf;}
61 extern int dbg_analyze_msg;
62 extern int dbg_bactrace_template_errors;
63 extern int DumpTemplateI18NStrings;
64 extern StrBuf *I18nDump;
65 void InitTemplateCache(void);
66 extern int LoadTemplates;
72 * Here's where it all begins.
74 int main(int argc, char **argv)
77 size_t basesize = 2; /* how big should strbufs be on creation? */
78 pthread_t SessThread; /* Thread descriptor */
79 pthread_attr_t attr; /* Thread attributes */
80 int a; /* General-purpose variable */
81 char tracefile[PATH_MAX];
82 char ip_addr[256]="0.0.0.0";
86 char relhome[PATH_MAX]="";
87 char webcitdir[PATH_MAX] = DATADIR;
90 const char *basedir = NULL;
91 char uds_listen_path[PATH_MAX]; /* listen on a unix domain socket? */
92 const char *I18nDumpFile = NULL;
96 WildFireInitBacktrace(argv[0], 2);
100 #ifdef DBG_PRINNT_HOOKS_AT_START
101 /* dbg_PrintHash(HandlerHash, nix, NULL);*/
104 /* Ensure that we are linked to the correct version of libcitadel */
105 if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
106 fprintf(stderr, " You are running libcitadel version %d.%02d\n",
107 (libcitadel_version_number() / 100), (libcitadel_version_number() % 100));
108 fprintf(stderr, "WebCit was compiled against version %d.%02d\n",
109 (LIBCITADEL_VERSION_NUMBER / 100), (LIBCITADEL_VERSION_NUMBER % 100));
113 strcpy(uds_listen_path, "");
115 /* Parse command line */
117 while ((a = getopt(argc, argv, "R:u:h:i:p:t:T:B:x:dD:G:cfsS:Z")) != EOF)
119 while ((a = getopt(argc, argv, "R:u:h:i:p:t:T:B:x:dD:G:cfZ")) != EOF)
126 hdir = strdup(optarg);
129 safestrncpy(webcitdir, hdir, sizeof webcitdir);
132 safestrncpy(relhome, relhome, sizeof relhome);
134 /* free(hdir); TODO: SHOULD WE DO THIS? */
139 running_as_daemon = 1;
142 pidfile = strdup(optarg);
143 running_as_daemon = 1;
145 case 'B': /* Basesize */
146 basesize = atoi(optarg);
148 StartLibCitadel(basesize);
151 safestrncpy(ip_addr, optarg, sizeof ip_addr);
154 http_port = atoi(optarg);
155 if (http_port == 0) {
156 safestrncpy(uds_listen_path, optarg, sizeof uds_listen_path);
160 safestrncpy(tracefile, optarg, sizeof tracefile);
161 rvfp = freopen(tracefile, "w", stdout);
162 rvfp = freopen(tracefile, "w", stderr);
163 rvfp = freopen(tracefile, "r", stdin);
166 LoadTemplates = atoi(optarg);
167 dbg_analyze_msg = (LoadTemplates && (1<<1)) != 0;
168 dbg_bactrace_template_errors = (LoadTemplates && (1<<2)) != 0;
174 verbosity = atoi(optarg);
180 server_cookie = malloc(256);
181 if (server_cookie != NULL) {
182 safestrncpy(server_cookie,
183 "Set-cookie: wcserver=",
186 (&server_cookie[strlen(server_cookie)],
188 lprintf(2, "gethostname: %s\n",
200 ssl_cipher_list = strdup(optarg);
204 DumpTemplateI18NStrings = 1;
205 I18nDump = NewStrBufPlain(HKEY("int templatestrings(void)\n{\n"));
206 I18nDumpFile = optarg;
209 MaxRead = atol(optarg);
212 fprintf(stderr, "usage: webcit "
213 "[-i ip_addr] [-p http_port] "
214 "[-t tracefile] [-c] [-f] "
215 "[-T Templatedebuglevel] "
216 "[-d] [-Z] [-G i18ndumpfile] "
218 "[-s] [-S cipher_suites]"
220 "[remotehost [remoteport]]\n");
225 ctdlhost = argv[optind];
227 ctdlport = argv[optind];
230 /* daemonize, if we were asked to */
231 if (!DumpTemplateI18NStrings && running_as_daemon) {
232 start_daemon(pidfile);
235 signal(SIGINT, graceful_shutdown);
236 signal(SIGHUP, graceful_shutdown);
239 webcit_calc_dirs_n_files(relh, basedir, home, webcitdir, relhome);
240 LoadIconDir(static_icon_dir);
242 /* Tell 'em who's in da house */
243 lprintf(1, PACKAGE_STRING "\n");
244 lprintf(1, "Copyright (C) 1996-2010 by the Citadel development team.\n"
245 "This software is distributed under the terms of the "
246 "GNU General Public License.\n\n"
250 /* initialize the International Bright Young Thing */
252 initialise_modules();
255 if (DumpTemplateI18NStrings) {
257 StrBufAppendBufPlain(I18nDump, HKEY("}\n"), 0);
258 if (StrLength(I18nDump) < 50) {
259 lprintf(1, "********************************************************************************\n");
260 lprintf(1, "* No strings found in templates! Are you sure they're there? *\n");
261 lprintf(1, "********************************************************************************\n");
264 fd = fopen(I18nDumpFile, "w");
266 lprintf(1, "********************************************************************************\n");
267 lprintf(1, "* unable to open I18N dumpfile [%s] *\n", I18nDumpFile);
268 lprintf(1, "********************************************************************************\n");
271 rv = fwrite(ChrPtr(I18nDump), 1, StrLength(I18nDump), fd);
277 /* Tell libical to return an error instead of aborting if it sees badly formed iCalendar data. */
278 icalerror_errors_are_fatal = 0;
280 /* Use our own prefix on tzid's generated from system tzdata */
281 icaltimezone_set_tzid_prefix("/citadel.org/");
284 * Set up a place to put thread-specific data.
285 * We only need a single pointer per thread - it points to the
286 * wcsession struct to which the thread is currently bound.
288 if (pthread_key_create(&MyConKey, NULL) != 0) {
289 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
291 InitialiseSemaphores ();
294 * Set up a place to put thread-specific SSL data.
295 * We don't stick this in the wcsession struct because SSL starts
296 * up before the session is bound, and it gets torn down between
300 if (pthread_key_create(&ThreadSSL, NULL) != 0) {
301 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
306 * Bind the server to our favorite port.
307 * There is no need to check for errors, because ig_tcp_server()
308 * exits if it doesn't succeed.
311 if (!IsEmptyStr(uds_listen_path)) {
312 lprintf(2, "Attempting to create listener socket at %s...\n", uds_listen_path);
313 msock = ig_uds_server(uds_listen_path, LISTEN_QUEUE_LENGTH);
316 lprintf(2, "Attempting to bind to port %d...\n", http_port);
317 msock = ig_tcp_server(ip_addr, http_port, LISTEN_QUEUE_LENGTH);
325 lprintf(2, "Listening on socket %d\n", msock);
326 signal(SIGPIPE, SIG_IGN);
328 pthread_mutex_init(&SessionListMutex, NULL);
331 * Start up the housekeeping thread
333 pthread_attr_init(&attr);
334 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
335 pthread_create(&SessThread, &attr,
336 (void *(*)(void *)) housekeeping_loop, NULL);
340 * If this is an HTTPS server, fire up SSL
349 /* Become a worker thread. More worker threads will be spawned as they are needed. */
351 ShutDownLibCitadel ();