2 * This contains a simple multithreaded TCP server manager. It sits around
3 * waiting on the specified port for incoming HTTP connections. When a
4 * connection is established, it calls context_loop() from context_loop.c.
6 * Copyright (c) 1996-2011 by the citadel.org developers.
7 * This program is released under the terms of the GNU General Public License v3.
12 #include "webserver.h"
14 #include "modules_init.h"
16 int vsnprintf(char *buf, size_t max, const char *fmt, va_list argp);
20 extern int msock; /* master listening socket */
21 extern int verbosity; /* Logging level */
22 extern char static_icon_dir[PATH_MAX]; /* where should we find our mime icons */
23 int is_https = 0; /* Nonzero if I am an HTTPS service */
24 int follow_xff = 0; /* Follow X-Forwarded-For: header */
26 struct redirector *redir = NULL;
27 char *default_landing_page = NULL;
29 extern pthread_mutex_t SessionListMutex;
30 extern pthread_key_t MyConKey;
32 extern void *housekeeping_loop(void);
33 extern int webcit_tcp_server(char *ip_addr, int port_number, int queue_len);
34 extern int webcit_uds_server(char *sockpath, int queue_len);
35 extern void graceful_shutdown_watcher(int signum);
36 extern void graceful_shutdown(int signum);
37 extern void start_daemon(char *pid_file);
38 extern void webcit_calc_dirs_n_files(int relh, const char *basedir, int home, char *webcitdir, char *relhome);
39 extern void worker_entry(void);
40 extern void drop_root(uid_t UID);
42 char socket_dir[PATH_MAX]; /* where to talk to our citadel server */
44 char *server_cookie = NULL; /* our Cookie connection to the client */
45 int http_port = PORT_NUM; /* Port to listen on */
46 char *ctdlhost = DEFAULT_HOST; /* our name */
47 char *ctdlport = DEFAULT_PORT; /* our Port */
48 int setup_wizard = 0; /* should we run the setup wizard? \todo */
49 char wizard_filename[PATH_MAX]; /* where's the setup wizard? */
50 int running_as_daemon = 0; /* should we deamonize on startup? */
54 /* #define DBG_PRINNT_HOOKS_AT_START */
55 #ifdef DBG_PRINNT_HOOKS_AT_START
56 extern HashList *HandlerHash;
57 const char foobuf[32];
58 const char *nix(void *vptr) {snprintf(foobuf, 32, "%0x", (long) vptr); return foobuf;}
60 extern int dbg_analyze_msg;
61 extern int dbg_backtrace_template_errors;
62 extern int DumpTemplateI18NStrings;
63 extern StrBuf *I18nDump;
64 void InitTemplateCache(void);
65 extern int LoadTemplates;
72 * Handle redirects to legacy web servers
74 void handle_redir(void) {
77 const char *req = ChrPtr(WC->Hdr->this_page);
82 if (req[0] == '/') ++req;
83 syslog(9, "handle_redir() called; redirect this: %s", req);
84 for (i=0; i<num_redir; ++i) {
85 if (!strncmp(redir[i].urlpart, req, strlen(redir[i].urlpart))) {
87 snprintf(go_here, sizeof go_here, redir[i].redirect_to, req);
88 syslog(9, "redirecting to: %s", go_here);
89 http_redirect(go_here);
100 * load redirect strings (for supporting transition of legacy web servers to citadel on the same host)
102 void load_redirs(char *filename) {
104 int num_redir_alloc = num_redir;
105 FILE *fp = fopen(filename, "r");
107 syslog(1, "Cannot open %s: %s", filename, strerror(errno));
111 while (fgets(buf, sizeof buf, fp) != NULL) {
114 buf[strlen(buf)-1] = 0;
116 ch = strchr(buf, '#');
117 if (ch) strcpy(ch, "");
119 if (!IsEmptyStr(buf)) {
121 if (num_redir >= num_redir_alloc) {
122 if (num_redir_alloc == 0) {
123 num_redir_alloc = 10;
126 num_redir_alloc = num_redir_alloc * 2;
128 redir = realloc(redir, sizeof(struct redirector) * num_redir_alloc );
131 extract_token(redir[num_redir].urlpart, buf, 0, '|', sizeof(redir[num_redir].urlpart));
132 extract_token(redir[num_redir].redirect_to, buf, 1, '|', sizeof(redir[num_redir].redirect_to));
133 WebcitAddUrlHandler(redir[num_redir].urlpart, strlen(redir[num_redir].urlpart), "", 0, handle_redir, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC);
134 if (!strcasecmp(redir[num_redir].urlpart, "home")) {
135 default_landing_page = redir[num_redir].redirect_to ;
147 * Here's where it all begins.
149 int main(int argc, char **argv)
152 size_t basesize = 2; /* how big should strbufs be on creation? */
153 pthread_t SessThread; /* Thread descriptor */
154 pthread_attr_t attr; /* Thread attributes */
155 int a; /* General-purpose variable */
156 char tracefile[PATH_MAX];
157 char ip_addr[256]="*";
160 char relhome[PATH_MAX]="";
161 char webcitdir[PATH_MAX] = DATADIR;
162 char *pidfile = NULL;
164 const char *basedir = NULL;
165 char uds_listen_path[PATH_MAX]; /* listen on a unix domain socket? */
166 const char *I18nDumpFile = NULL;
170 WildFireInitBacktrace(argv[0], 2);
174 #ifdef DBG_PRINNT_HOOKS_AT_START
175 /* dbg_PrintHash(HandlerHash, nix, NULL);*/
178 /* Ensure that we are linked to the correct version of libcitadel */
179 if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
180 fprintf(stderr, " You are running libcitadel version %d.%02d\n",
181 (libcitadel_version_number() / 100), (libcitadel_version_number() % 100));
182 fprintf(stderr, "WebCit was compiled against version %d.%02d\n",
183 (LIBCITADEL_VERSION_NUMBER / 100), (LIBCITADEL_VERSION_NUMBER % 100));
187 strcpy(uds_listen_path, "");
189 /* Parse command line */
191 while ((a = getopt(argc, argv, "u:h:i:p:t:T:B:x:dD:G:r:cfsS:Z")) != EOF)
193 while ((a = getopt(argc, argv, "u:h:i:p:t:T:B:x:dD:G:r:cfZ")) != EOF)
203 hdir = strdup(optarg);
206 safestrncpy(webcitdir, hdir, sizeof webcitdir);
209 safestrncpy(relhome, relhome, sizeof relhome);
211 /* free(hdir); TODO: SHOULD WE DO THIS? */
215 running_as_daemon = 1;
218 pidfile = strdup(optarg);
219 running_as_daemon = 1;
221 case 'B': /* Basesize */
222 basesize = atoi(optarg);
224 StartLibCitadel(basesize);
227 safestrncpy(ip_addr, optarg, sizeof ip_addr);
230 http_port = atoi(optarg);
231 if (http_port == 0) {
232 safestrncpy(uds_listen_path, optarg, sizeof uds_listen_path);
236 safestrncpy(tracefile, optarg, sizeof tracefile);
237 rvfp = freopen(tracefile, "w", stdout);
238 rvfp = freopen(tracefile, "w", stderr);
239 rvfp = freopen(tracefile, "r", stdin);
242 LoadTemplates = atoi(optarg);
243 dbg_analyze_msg = (LoadTemplates && (1<<1)) != 0;
244 dbg_backtrace_template_errors = (LoadTemplates && (1<<2)) != 0;
250 verbosity = atoi(optarg);
256 server_cookie = malloc(256);
257 if (server_cookie != NULL) {
258 safestrncpy(server_cookie,
259 "Set-cookie: wcserver=",
262 (&server_cookie[strlen(server_cookie)],
264 syslog(2, "gethostname: %s", strerror(errno));
275 ssl_cipher_list = strdup(optarg);
279 DumpTemplateI18NStrings = 1;
280 I18nDump = NewStrBufPlain(HKEY("int templatestrings(void)\n{\n"));
281 I18nDumpFile = optarg;
284 fprintf(stderr, "usage: webcit "
285 "[-i ip_addr] [-p http_port] "
286 "[-t tracefile] [-c] [-f] "
287 "[-T Templatedebuglevel] "
288 "[-d] [-Z] [-G i18ndumpfile] "
290 "[-s] [-S cipher_suites]"
292 "[remotehost [remoteport]]\n");
296 /* Start the logger */
298 ( running_as_daemon ? (LOG_PID) : (LOG_PID | LOG_PERROR) ),
303 ctdlhost = argv[optind];
305 ctdlport = argv[optind];
308 /* daemonize, if we were asked to */
309 if (!DumpTemplateI18NStrings && running_as_daemon) {
310 start_daemon(pidfile);
313 signal(SIGINT, graceful_shutdown);
314 signal(SIGHUP, graceful_shutdown);
317 webcit_calc_dirs_n_files(relh, basedir, home, webcitdir, relhome);
318 LoadIconDir(static_icon_dir);
320 /* Tell 'em who's in da house */
321 syslog(1, "%s", PACKAGE_STRING);
322 syslog(1, "Copyright (C) 1996-2011 by the citadel.org team");
324 syslog(1, "This program is open source software: you can redistribute it and/or");
325 syslog(1, "modify it under the terms of the GNU General Public License as published");
326 syslog(1, "by the Free Software Foundation, either version 3 of the License, or");
327 syslog(1, "(at your option) any later version.");
329 syslog(1, "This program is distributed in the hope that it will be useful,");
330 syslog(1, "but WITHOUT ANY WARRANTY; without even the implied warranty of");
331 syslog(1, "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the");
332 syslog(1, "GNU General Public License for more details.");
334 syslog(1, "You should have received a copy of the GNU General Public License");
335 syslog(1, "along with this program. If not, see <http://www.gnu.org/licenses/>.");
339 /* initialize various subsystems */
341 initialise_modules();
343 if (DumpTemplateI18NStrings) {
345 StrBufAppendBufPlain(I18nDump, HKEY("}\n"), 0);
346 if (StrLength(I18nDump) < 50) {
347 syslog(1, "********************************************************************************\n");
348 syslog(1, "* No strings found in templates! Are you sure they're there? *\n");
349 syslog(1, "********************************************************************************\n");
352 fd = fopen(I18nDumpFile, "w");
354 syslog(1, "********************************************************************************\n");
355 syslog(1, "* unable to open I18N dumpfile [%s] *\n", I18nDumpFile);
356 syslog(1, "********************************************************************************\n");
359 rv = fwrite(ChrPtr(I18nDump), 1, StrLength(I18nDump), fd);
365 /* Tell libical to return an error instead of aborting if it sees badly formed iCalendar data. */
366 icalerror_errors_are_fatal = 0;
368 /* Use our own prefix on tzid's generated from system tzdata */
369 icaltimezone_set_tzid_prefix("/citadel.org/");
372 * Set up a place to put thread-specific data.
373 * We only need a single pointer per thread - it points to the
374 * wcsession struct to which the thread is currently bound.
376 if (pthread_key_create(&MyConKey, NULL) != 0) {
377 syslog(1, "Can't create TSD key: %s\n", strerror(errno));
379 InitialiseSemaphores ();
382 * Set up a place to put thread-specific SSL data.
383 * We don't stick this in the wcsession struct because SSL starts
384 * up before the session is bound, and it gets torn down between
388 if (pthread_key_create(&ThreadSSL, NULL) != 0) {
389 syslog(1, "Can't create TSD key: %s\n", strerror(errno));
394 * Bind the server to our favorite port.
395 * There is no need to check for errors, because webcit_tcp_server()
396 * exits if it doesn't succeed.
399 if (!IsEmptyStr(uds_listen_path)) {
400 syslog(2, "Attempting to create listener socket at %s...\n", uds_listen_path);
401 msock = webcit_uds_server(uds_listen_path, LISTEN_QUEUE_LENGTH);
404 syslog(2, "Attempting to bind to port %d...\n", http_port);
405 msock = webcit_tcp_server(ip_addr, http_port, LISTEN_QUEUE_LENGTH);
413 syslog(2, "Listening on socket %d\n", msock);
414 signal(SIGPIPE, SIG_IGN);
416 pthread_mutex_init(&SessionListMutex, NULL);
419 * Start up the housekeeping thread
421 pthread_attr_init(&attr);
422 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
423 pthread_create(&SessThread, &attr,
424 (void *(*)(void *)) housekeeping_loop, NULL);
428 * If this is an HTTPS server, fire up SSL
437 /* Become a worker thread. More worker threads will be spawned as they are needed. */
439 ShutDownLibCitadel ();