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 */
43 char *server_cookie = NULL; /* our Cookie connection to the client */
44 int http_port = PORT_NUM; /* Port to listen on */
45 char *ctdlhost = DEFAULT_HOST; /* Host name or IP address of Citadel server */
46 char *ctdlport = DEFAULT_PORT; /* Port number of Citadel server */
47 int setup_wizard = 0; /* should we run the setup wizard? */
48 char wizard_filename[PATH_MAX]; /* location of file containing the last webcit version against which we ran setup wizard */
49 int running_as_daemon = 0; /* should we deamonize on startup? */
52 /* #define DBG_PRINNT_HOOKS_AT_START */
53 #ifdef DBG_PRINNT_HOOKS_AT_START
54 extern HashList *HandlerHash;
55 const char foobuf[32];
56 const char *nix(void *vptr) {snprintf(foobuf, 32, "%0x", (long) vptr); return foobuf;}
58 extern int dbg_analyze_msg;
59 extern int dbg_backtrace_template_errors;
60 extern int DumpTemplateI18NStrings;
61 extern StrBuf *I18nDump;
62 void InitTemplateCache(void);
63 extern int LoadTemplates;
70 * Handle redirects to legacy web servers
72 void handle_redir(void) {
75 const char *req = ChrPtr(WC->Hdr->this_page);
80 if (req[0] == '/') ++req;
81 syslog(9, "handle_redir() called; redirect this: %s", req);
82 for (i=0; i<num_redir; ++i) {
83 if (!strncmp(redir[i].urlpart, req, strlen(redir[i].urlpart))) {
85 snprintf(go_here, sizeof go_here, redir[i].redirect_to, req);
86 syslog(9, "redirecting to: %s", go_here);
87 http_redirect(go_here);
98 * load redirect strings (for supporting transition of legacy web servers to citadel on the same host)
100 void load_redirs(char *filename) {
102 int num_redir_alloc = num_redir;
103 FILE *fp = fopen(filename, "r");
105 syslog(1, "Cannot open %s: %s", filename, strerror(errno));
109 while (fgets(buf, sizeof buf, fp) != NULL) {
112 buf[strlen(buf)-1] = 0;
114 ch = strchr(buf, '#');
115 if (ch) strcpy(ch, "");
117 if (!IsEmptyStr(buf)) {
119 if (num_redir >= num_redir_alloc) {
120 if (num_redir_alloc == 0) {
121 num_redir_alloc = 10;
124 num_redir_alloc = num_redir_alloc * 2;
126 redir = realloc(redir, sizeof(struct redirector) * num_redir_alloc );
129 extract_token(redir[num_redir].urlpart, buf, 0, '|', sizeof(redir[num_redir].urlpart));
130 extract_token(redir[num_redir].redirect_to, buf, 1, '|', sizeof(redir[num_redir].redirect_to));
131 WebcitAddUrlHandler(redir[num_redir].urlpart, strlen(redir[num_redir].urlpart), "", 0, handle_redir, ANONYMOUS|COOKIEUNNEEDED|ISSTATIC);
132 if (!strcasecmp(redir[num_redir].urlpart, "home")) {
133 default_landing_page = redir[num_redir].redirect_to ;
145 * Here's where it all begins.
147 int main(int argc, char **argv)
150 size_t basesize = 2; /* how big should strbufs be on creation? */
151 pthread_t SessThread; /* Thread descriptor */
152 pthread_attr_t attr; /* Thread attributes */
153 int a; /* General-purpose variable */
154 char tracefile[PATH_MAX];
155 char ip_addr[256]="*";
158 char relhome[PATH_MAX]="";
159 char webcitdir[PATH_MAX] = DATADIR;
160 char *pidfile = NULL;
162 const char *basedir = NULL;
163 char uds_listen_path[PATH_MAX]; /* listen on a unix domain socket? */
164 const char *I18nDumpFile = NULL;
168 WildFireInitBacktrace(argv[0], 2);
172 #ifdef DBG_PRINNT_HOOKS_AT_START
173 /* dbg_PrintHash(HandlerHash, nix, NULL);*/
176 /* Ensure that we are linked to the correct version of libcitadel */
177 if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
178 fprintf(stderr, " You are running libcitadel version %d.%02d\n",
179 (libcitadel_version_number() / 100), (libcitadel_version_number() % 100));
180 fprintf(stderr, "WebCit was compiled against version %d.%02d\n",
181 (LIBCITADEL_VERSION_NUMBER / 100), (LIBCITADEL_VERSION_NUMBER % 100));
185 strcpy(uds_listen_path, "");
187 /* Parse command line */
189 while ((a = getopt(argc, argv, "u:h:i:p:t:T:B:x:dD:G:r:cfsS:Z")) != EOF)
191 while ((a = getopt(argc, argv, "u:h:i:p:t:T:B:x:dD:G:r:cfZ")) != EOF)
201 hdir = strdup(optarg);
204 safestrncpy(webcitdir, hdir, sizeof webcitdir);
207 safestrncpy(relhome, relhome, sizeof relhome);
209 /* free(hdir); TODO: SHOULD WE DO THIS? */
213 running_as_daemon = 1;
216 pidfile = strdup(optarg);
217 running_as_daemon = 1;
219 case 'B': /* Basesize */
220 basesize = atoi(optarg);
222 StartLibCitadel(basesize);
225 safestrncpy(ip_addr, optarg, sizeof ip_addr);
228 http_port = atoi(optarg);
229 if (http_port == 0) {
230 safestrncpy(uds_listen_path, optarg, sizeof uds_listen_path);
234 safestrncpy(tracefile, optarg, sizeof tracefile);
235 rvfp = freopen(tracefile, "w", stdout);
236 rvfp = freopen(tracefile, "w", stderr);
237 rvfp = freopen(tracefile, "r", stdin);
240 LoadTemplates = atoi(optarg);
241 dbg_analyze_msg = (LoadTemplates && (1<<1)) != 0;
242 dbg_backtrace_template_errors = (LoadTemplates && (1<<2)) != 0;
248 verbosity = atoi(optarg);
254 server_cookie = malloc(256);
255 if (server_cookie != NULL) {
256 safestrncpy(server_cookie,
257 "Set-cookie: wcserver=",
260 (&server_cookie[strlen(server_cookie)],
262 syslog(2, "gethostname: %s", strerror(errno));
273 ssl_cipher_list = strdup(optarg);
277 DumpTemplateI18NStrings = 1;
278 I18nDump = NewStrBufPlain(HKEY("int templatestrings(void)\n{\n"));
279 I18nDumpFile = optarg;
282 fprintf(stderr, "usage: webcit "
283 "[-i ip_addr] [-p http_port] "
284 "[-t tracefile] [-c] [-f] "
285 "[-T Templatedebuglevel] "
286 "[-d] [-Z] [-G i18ndumpfile] "
288 "[-s] [-S cipher_suites]"
290 "[remotehost [remoteport]]\n");
294 /* Start the logger */
296 ( running_as_daemon ? (LOG_PID) : (LOG_PID | LOG_PERROR) ),
301 ctdlhost = argv[optind];
303 ctdlport = argv[optind];
306 /* daemonize, if we were asked to */
307 if (!DumpTemplateI18NStrings && running_as_daemon) {
308 start_daemon(pidfile);
311 signal(SIGINT, graceful_shutdown);
312 signal(SIGHUP, graceful_shutdown);
315 webcit_calc_dirs_n_files(relh, basedir, home, webcitdir, relhome);
316 LoadIconDir(static_icon_dir);
318 /* Tell 'em who's in da house */
319 syslog(1, "%s", PACKAGE_STRING);
320 syslog(1, "Copyright (C) 1996-2011 by the citadel.org team");
322 syslog(1, "This program is open source software: you can redistribute it and/or");
323 syslog(1, "modify it under the terms of the GNU General Public License as published");
324 syslog(1, "by the Free Software Foundation, either version 3 of the License, or");
325 syslog(1, "(at your option) any later version.");
327 syslog(1, "This program is distributed in the hope that it will be useful,");
328 syslog(1, "but WITHOUT ANY WARRANTY; without even the implied warranty of");
329 syslog(1, "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the");
330 syslog(1, "GNU General Public License for more details.");
332 syslog(1, "You should have received a copy of the GNU General Public License");
333 syslog(1, "along with this program. If not, see <http://www.gnu.org/licenses/>.");
337 /* initialize various subsystems */
339 initialise_modules();
341 if (DumpTemplateI18NStrings) {
343 StrBufAppendBufPlain(I18nDump, HKEY("}\n"), 0);
344 if (StrLength(I18nDump) < 50) {
345 syslog(1, "********************************************************************************\n");
346 syslog(1, "* No strings found in templates! Are you sure they're there? *\n");
347 syslog(1, "********************************************************************************\n");
350 fd = fopen(I18nDumpFile, "w");
352 syslog(1, "********************************************************************************\n");
353 syslog(1, "* unable to open I18N dumpfile [%s] *\n", I18nDumpFile);
354 syslog(1, "********************************************************************************\n");
357 rv = fwrite(ChrPtr(I18nDump), 1, StrLength(I18nDump), fd);
363 /* Tell libical to return an error instead of aborting if it sees badly formed iCalendar data. */
364 icalerror_errors_are_fatal = 0;
366 /* Use our own prefix on tzid's generated from system tzdata */
367 icaltimezone_set_tzid_prefix("/citadel.org/");
370 * Set up a place to put thread-specific data.
371 * We only need a single pointer per thread - it points to the
372 * wcsession struct to which the thread is currently bound.
374 if (pthread_key_create(&MyConKey, NULL) != 0) {
375 syslog(1, "Can't create TSD key: %s\n", strerror(errno));
377 InitialiseSemaphores ();
380 * Set up a place to put thread-specific SSL data.
381 * We don't stick this in the wcsession struct because SSL starts
382 * up before the session is bound, and it gets torn down between
386 if (pthread_key_create(&ThreadSSL, NULL) != 0) {
387 syslog(1, "Can't create TSD key: %s\n", strerror(errno));
392 * Bind the server to our favorite port.
393 * There is no need to check for errors, because webcit_tcp_server()
394 * exits if it doesn't succeed.
397 if (!IsEmptyStr(uds_listen_path)) {
398 syslog(2, "Attempting to create listener socket at %s...\n", uds_listen_path);
399 msock = webcit_uds_server(uds_listen_path, LISTEN_QUEUE_LENGTH);
402 syslog(2, "Attempting to bind to port %d...\n", http_port);
403 msock = webcit_tcp_server(ip_addr, http_port, LISTEN_QUEUE_LENGTH);
411 syslog(2, "Listening on socket %d\n", msock);
412 signal(SIGPIPE, SIG_IGN);
414 pthread_mutex_init(&SessionListMutex, NULL);
417 * Start up the housekeeping thread
419 pthread_attr_init(&attr);
420 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
421 pthread_create(&SessThread, &attr,
422 (void *(*)(void *)) housekeeping_loop, NULL);
426 * If this is an HTTPS server, fire up SSL
435 /* Become a worker thread. More worker threads will be spawned as they are needed. */
437 ShutDownLibCitadel();