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 */
25 int home_specified = 0; /* did the user specify a homedir? */
27 StrBuf *site_prefix = NULL;
28 extern pthread_mutex_t SessionListMutex;
29 extern pthread_key_t MyConKey;
31 extern void *housekeeping_loop(void);
32 extern int webcit_tcp_server(char *ip_addr, int port_number, int queue_len);
33 extern int webcit_uds_server(char *sockpath, int queue_len);
34 extern void graceful_shutdown_watcher(int signum);
35 extern void graceful_shutdown(int signum);
36 extern void start_daemon(char *pid_file);
37 extern void webcit_calc_dirs_n_files(int relh, const char *basedir, int home, char *webcitdir, char *relhome);
39 extern void drop_root(uid_t UID);
41 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; /* our name */
46 char *ctdlport = DEFAULT_PORT; /* our Port */
47 int setup_wizard = 0; /* should we run the setup wizard? \todo */
48 char wizard_filename[PATH_MAX]; /* where's the setup wizard? */
49 int running_as_daemon = 0; /* should we deamonize on startup? */
53 /* #define DBG_PRINNT_HOOKS_AT_START */
54 #ifdef DBG_PRINNT_HOOKS_AT_START
55 extern HashList *HandlerHash;
56 const char foobuf[32];
57 const char *nix(void *vptr) {snprintf(foobuf, 32, "%0x", (long) vptr); return foobuf;}
59 extern int dbg_analyze_msg;
60 extern int dbg_backtrace_template_errors;
61 extern int DumpTemplateI18NStrings;
62 extern StrBuf *I18nDump;
63 void InitTemplateCache(void);
64 extern int LoadTemplates;
70 * Here's where it all begins.
72 int main(int argc, char **argv)
75 size_t basesize = 2; /* how big should strbufs be on creation? */
76 pthread_t SessThread; /* Thread descriptor */
77 pthread_attr_t attr; /* Thread attributes */
78 int a; /* General-purpose variable */
79 char tracefile[PATH_MAX];
80 char ip_addr[256]="*";
84 char relhome[PATH_MAX]="";
85 char webcitdir[PATH_MAX] = DATADIR;
88 const char *basedir = NULL;
89 char uds_listen_path[PATH_MAX]; /* listen on a unix domain socket? */
90 const char *I18nDumpFile = NULL;
94 WildFireInitBacktrace(argv[0], 2);
98 #ifdef DBG_PRINNT_HOOKS_AT_START
99 /* dbg_PrintHash(HandlerHash, nix, NULL);*/
102 /* Ensure that we are linked to the correct version of libcitadel */
103 if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
104 fprintf(stderr, " You are running libcitadel version %d.%02d\n",
105 (libcitadel_version_number() / 100), (libcitadel_version_number() % 100));
106 fprintf(stderr, "WebCit was compiled against version %d.%02d\n",
107 (LIBCITADEL_VERSION_NUMBER / 100), (LIBCITADEL_VERSION_NUMBER % 100));
111 strcpy(uds_listen_path, "");
113 /* Parse command line */
115 while ((a = getopt(argc, argv, "u:h:i:p:t:T:B:x:dD:G:cfsS:Z")) != EOF)
117 while ((a = getopt(argc, argv, "u:h:i:p:t:T:B:x:dD:G:cfZ")) != EOF)
124 hdir = strdup(optarg);
127 safestrncpy(webcitdir, hdir, sizeof webcitdir);
130 safestrncpy(relhome, relhome, sizeof relhome);
132 /* free(hdir); TODO: SHOULD WE DO THIS? */
137 running_as_daemon = 1;
140 pidfile = strdup(optarg);
141 running_as_daemon = 1;
143 case 'B': /* Basesize */
144 basesize = atoi(optarg);
146 StartLibCitadel(basesize);
149 safestrncpy(ip_addr, optarg, sizeof ip_addr);
152 http_port = atoi(optarg);
153 if (http_port == 0) {
154 safestrncpy(uds_listen_path, optarg, sizeof uds_listen_path);
158 safestrncpy(tracefile, optarg, sizeof tracefile);
159 rvfp = freopen(tracefile, "w", stdout);
160 rvfp = freopen(tracefile, "w", stderr);
161 rvfp = freopen(tracefile, "r", stdin);
164 LoadTemplates = atoi(optarg);
165 dbg_analyze_msg = (LoadTemplates && (1<<1)) != 0;
166 dbg_backtrace_template_errors = (LoadTemplates && (1<<2)) != 0;
172 verbosity = atoi(optarg);
178 server_cookie = malloc(256);
179 if (server_cookie != NULL) {
180 safestrncpy(server_cookie,
181 "Set-cookie: wcserver=",
184 (&server_cookie[strlen(server_cookie)],
186 lprintf(2, "gethostname: %s\n",
198 ssl_cipher_list = strdup(optarg);
202 DumpTemplateI18NStrings = 1;
203 I18nDump = NewStrBufPlain(HKEY("int templatestrings(void)\n{\n"));
204 I18nDumpFile = optarg;
207 fprintf(stderr, "usage: webcit "
208 "[-i ip_addr] [-p http_port] "
209 "[-t tracefile] [-c] [-f] "
210 "[-T Templatedebuglevel] "
211 "[-d] [-Z] [-G i18ndumpfile] "
213 "[-s] [-S cipher_suites]"
215 "[remotehost [remoteport]]\n");
219 /* Start the logger */
221 ( running_as_daemon ? (LOG_PID) : (LOG_PID | LOG_PERROR) ),
226 ctdlhost = argv[optind];
228 ctdlport = argv[optind];
231 /* daemonize, if we were asked to */
232 if (!DumpTemplateI18NStrings && running_as_daemon) {
233 start_daemon(pidfile);
236 signal(SIGINT, graceful_shutdown);
237 signal(SIGHUP, graceful_shutdown);
240 webcit_calc_dirs_n_files(relh, basedir, home, webcitdir, relhome);
241 LoadIconDir(static_icon_dir);
243 /* Tell 'em who's in da house */
244 lprintf(1, PACKAGE_STRING "\n");
245 lprintf(1, "Copyright (C) 1996-2011 by the citadel.org team\n");
247 lprintf(1, "This program is open source software: you can redistribute it and/or\n");
248 lprintf(1, "modify it under the terms of the GNU General Public License as published\n");
249 lprintf(1, "by the Free Software Foundation, either version 3 of the License, or\n");
250 lprintf(1, "(at your option) any later version.\n");
252 lprintf(1, "This program is distributed in the hope that it will be useful,\n");
253 lprintf(1, "but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
254 lprintf(1, "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
255 lprintf(1, "GNU General Public License for more details.\n");
257 lprintf(1, "You should have received a copy of the GNU General Public License\n");
258 lprintf(1, "along with this program. If not, see <http://www.gnu.org/licenses/>.\n");
262 /* initialize various subsystems */
264 initialise_modules();
266 if (DumpTemplateI18NStrings) {
268 StrBufAppendBufPlain(I18nDump, HKEY("}\n"), 0);
269 if (StrLength(I18nDump) < 50) {
270 lprintf(1, "********************************************************************************\n");
271 lprintf(1, "* No strings found in templates! Are you sure they're there? *\n");
272 lprintf(1, "********************************************************************************\n");
275 fd = fopen(I18nDumpFile, "w");
277 lprintf(1, "********************************************************************************\n");
278 lprintf(1, "* unable to open I18N dumpfile [%s] *\n", I18nDumpFile);
279 lprintf(1, "********************************************************************************\n");
282 rv = fwrite(ChrPtr(I18nDump), 1, StrLength(I18nDump), fd);
288 /* Tell libical to return an error instead of aborting if it sees badly formed iCalendar data. */
289 icalerror_errors_are_fatal = 0;
291 /* Use our own prefix on tzid's generated from system tzdata */
292 icaltimezone_set_tzid_prefix("/citadel.org/");
295 * Set up a place to put thread-specific data.
296 * We only need a single pointer per thread - it points to the
297 * wcsession struct to which the thread is currently bound.
299 if (pthread_key_create(&MyConKey, NULL) != 0) {
300 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
302 InitialiseSemaphores ();
305 * Set up a place to put thread-specific SSL data.
306 * We don't stick this in the wcsession struct because SSL starts
307 * up before the session is bound, and it gets torn down between
311 if (pthread_key_create(&ThreadSSL, NULL) != 0) {
312 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
317 * Bind the server to our favorite port.
318 * There is no need to check for errors, because webcit_tcp_server()
319 * exits if it doesn't succeed.
322 if (!IsEmptyStr(uds_listen_path)) {
323 lprintf(2, "Attempting to create listener socket at %s...\n", uds_listen_path);
324 msock = webcit_uds_server(uds_listen_path, LISTEN_QUEUE_LENGTH);
327 lprintf(2, "Attempting to bind to port %d...\n", http_port);
328 msock = webcit_tcp_server(ip_addr, http_port, LISTEN_QUEUE_LENGTH);
336 lprintf(2, "Listening on socket %d\n", msock);
337 signal(SIGPIPE, SIG_IGN);
339 pthread_mutex_init(&SessionListMutex, NULL);
342 * Start up the housekeeping thread
344 pthread_attr_init(&attr);
345 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
346 pthread_create(&SessThread, &attr,
347 (void *(*)(void *)) housekeeping_loop, NULL);
351 * If this is an HTTPS server, fire up SSL
360 /* Become a worker thread. More worker threads will be spawned as they are needed. */
362 ShutDownLibCitadel ();