2 * Copyright (c) 1996-2021 by the citadel.org team
4 * This program is open source software. You can redistribute it and/or
5 * modify it under the terms of the GNU General Public License version 3.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
14 #include "webserver.h"
16 #include "modules_init.h"
18 extern int msock; /* master listening socket */
19 extern char static_icon_dir[PATH_MAX]; /* where should we find our mime icons */
20 int is_https = 0; /* Nonzero if I am an HTTPS service */
21 int follow_xff = 0; /* Follow X-Forwarded-For: header? */
23 char *default_landing_page = NULL;
24 extern pthread_mutex_t SessionListMutex;
26 extern void *housekeeping_loop(void);
27 extern int webcit_tcp_server(char *ip_addr, int port_number, int queue_len);
28 extern int webcit_uds_server(char *sockpath, int queue_len);
29 extern void graceful_shutdown_watcher(int signum);
30 extern void graceful_shutdown(int signum);
31 extern void start_daemon(char *pid_file);
32 extern void webcit_calc_dirs_n_files(int relh, const char *basedir, int home, char *webcitdir, char *relhome);
33 extern void worker_entry(void);
34 extern void drop_root(uid_t UID);
36 char socket_dir[PATH_MAX]; /* where to talk to our citadel server */
37 char *server_cookie = NULL; /* our Cookie connection to the client */
38 int http_port = PORT_NUM; /* Port to listen on */
39 char *ctdlhost = DEFAULT_HOST; /* Host name or IP address of Citadel server */
40 char *ctdlport = DEFAULT_PORT; /* Port number of Citadel server */
41 int setup_wizard = 0; /* should we run the setup wizard? */
42 char wizard_filename[PATH_MAX]; /* location of file containing the last webcit version against which we ran setup wizard */
43 int running_as_daemon = 0; /* should we deamonize on startup? */
45 /* #define DBG_PRINNT_HOOKS_AT_START */
46 #ifdef DBG_PRINNT_HOOKS_AT_START
47 extern HashList *HandlerHash;
48 const char foobuf[32];
49 const char *nix(void *vptr) {snprintf(foobuf, 32, "%0x", (long) vptr); return foobuf;}
52 extern int dbg_analyze_msg;
53 extern int dbg_backtrace_template_errors;
54 extern int DumpTemplateI18NStrings;
55 extern StrBuf *I18nDump;
56 void InitTemplateCache(void);
57 extern int LoadTemplates;
59 void LoadMimeBlacklist(void);
62 * Here's where it all begins.
64 int main(int argc, char **argv)
67 pthread_t SessThread; /* Thread descriptor */
68 pthread_attr_t attr; /* Thread attributes */
69 int a; /* General-purpose variable */
70 char ip_addr[256]="*";
73 char relhome[PATH_MAX]="";
74 char webcitdir[PATH_MAX] = DATADIR;
77 const char *basedir = NULL;
78 char uds_listen_path[PATH_MAX]; /* listen on a unix domain socket? */
79 const char *I18nDumpFile = NULL;
80 int max_log_level = LOG_INFO;
82 WildFireInitBacktrace(argv[0], 2);
86 #ifdef DBG_PRINNT_HOOKS_AT_START
87 /* dbg_PrintHash(HandlerHash, nix, NULL);*/
90 /* Ensure that we are linked to the correct version of libcitadel */
91 if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
92 fprintf(stderr, " You are running libcitadel version %d\n", libcitadel_version_number() );
93 fprintf(stderr, "WebCit was compiled against version %d\n", LIBCITADEL_VERSION_NUMBER );
97 strcpy(uds_listen_path, "");
99 /* Parse command line */
101 while ((a = getopt(argc, argv, "u:h:i:p:t:T:B:x:g:dD:G:cfsS:Z:v:")) != EOF)
103 while ((a = getopt(argc, argv, "u:h:i:p:t:T:B:x:g:dD:G:cfZ:v:")) != EOF)
110 hdir = strdup(optarg);
113 safestrncpy(webcitdir, hdir, sizeof webcitdir);
116 safestrncpy(relhome, relhome, sizeof relhome);
118 /* free(hdir); TODO: SHOULD WE DO THIS? */
122 running_as_daemon = 1;
125 pidfile = strdup(optarg);
126 running_as_daemon = 1;
129 default_landing_page = strdup(optarg);
131 case 'B': /* deprecated */
134 safestrncpy(ip_addr, optarg, sizeof ip_addr);
137 http_port = atoi(optarg);
138 if (http_port == 0) {
139 safestrncpy(uds_listen_path, optarg, sizeof uds_listen_path);
143 /* no longer used, but ignored so old scripts don't break */
146 LoadTemplates = atoi(optarg);
147 dbg_analyze_msg = (LoadTemplates & (1<<1)) != 0;
148 dbg_backtrace_template_errors = (LoadTemplates & (1<<2)) != 0;
154 max_log_level = atoi(optarg);
160 server_cookie = malloc(256);
161 if (server_cookie != NULL) {
162 safestrncpy(server_cookie,
163 "Set-cookie: wcserver=",
166 (&server_cookie[strlen(server_cookie)],
168 syslog(LOG_INFO, "gethostname: %s", strerror(errno));
179 ssl_cipher_list = strdup(optarg);
183 DumpTemplateI18NStrings = 1;
184 I18nDump = NewStrBufPlain(HKEY("int templatestrings(void)\n{\n"));
185 I18nDumpFile = optarg;
191 fprintf(stderr, "usage:\nwebcit "
192 "[-i ip_addr] [-p http_port] "
194 "[-T Templatedebuglevel] "
195 "[-d] [-Z] [-G i18ndumpfile] "
196 "[-u uid] [-h homedirectory] "
197 "[-D daemonizepid] [-v] "
198 "[-g defaultlandingpage] "
200 "[-s] [-S cipher_suites]"
202 "[remotehost [remoteport]]\n");
206 /* Start the logger */
207 setlogmask(LOG_UPTO(max_log_level));
209 ( running_as_daemon ? (LOG_PID) : (LOG_PID | LOG_PERROR) ),
214 ctdlhost = argv[optind];
216 ctdlport = argv[optind];
219 /* daemonize, if we were asked to */
220 if (!DumpTemplateI18NStrings && running_as_daemon) {
221 start_daemon(pidfile);
224 signal(SIGINT, graceful_shutdown);
225 signal(SIGHUP, graceful_shutdown);
228 webcit_calc_dirs_n_files(relh, basedir, home, webcitdir, relhome);
230 LoadIconDir(static_icon_dir);
232 /* Tell 'em who's in da house */
233 syslog(LOG_NOTICE, "%s", PACKAGE_STRING);
234 syslog(LOG_NOTICE, "Copyright (C) 1996-2020 by the citadel.org team");
235 syslog(LOG_NOTICE, " ");
236 syslog(LOG_NOTICE, "This program is open source software: you can redistribute it and/or");
237 syslog(LOG_NOTICE, "modify it under the terms of the GNU General Public License, version 3.");
238 syslog(LOG_NOTICE, " ");
239 syslog(LOG_NOTICE, "This program is distributed in the hope that it will be useful,");
240 syslog(LOG_NOTICE, "but WITHOUT ANY WARRANTY; without even the implied warranty of");
241 syslog(LOG_NOTICE, "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the");
242 syslog(LOG_NOTICE, "GNU General Public License for more details.");
243 syslog(LOG_NOTICE, " ");
245 /* run from the webcit home directory */
246 if (chdir(webcitdir) != 0) {
247 syslog(LOG_ERR, "webcit: %s: %m", webcitdir);
251 /* initialize various subsystems */
252 initialise_modules();
253 initialise2_modules();
255 if (DumpTemplateI18NStrings) {
257 StrBufAppendBufPlain(I18nDump, HKEY("}\n"), 0);
258 if (StrLength(I18nDump) < 50) {
259 syslog(LOG_INFO, "*******************************************************************\n");
260 syslog(LOG_INFO, "* No strings found in templates! Are you sure they're there? *\n");
261 syslog(LOG_INFO, "*******************************************************************\n");
264 fd = fopen(I18nDumpFile, "w");
266 syslog(LOG_INFO, "***********************************************\n");
267 syslog(LOG_INFO, "* unable to open I18N dumpfile [%s] *\n", I18nDumpFile);
268 syslog(LOG_INFO, "***********************************************\n");
271 fwrite(ChrPtr(I18nDump), 1, StrLength(I18nDump), fd);
276 /* Tell libical to return an error instead of aborting if it sees badly formed iCalendar data. */
278 #ifdef LIBICAL_ICAL_EXPORT // cheap and sleazy way to detect libical >=2.0
279 icalerror_set_errors_are_fatal(0);
281 icalerror_errors_are_fatal = 0;
284 /* Use our own prefix on tzid's generated from system tzdata */
285 icaltimezone_set_tzid_prefix("/citadel.org/");
288 * Set up a place to put thread-specific data.
289 * We only need a single pointer per thread - it points to the
290 * wcsession struct to which the thread is currently bound.
292 if (pthread_key_create(&MyConKey, NULL) != 0) {
293 syslog(LOG_ERR, "Can't create TSD key: %s", strerror(errno));
295 InitialiseSemaphores();
298 * Set up a place to put thread-specific SSL data.
299 * We don't stick this in the wcsession struct because SSL starts
300 * up before the session is bound, and it gets torn down between
304 if (pthread_key_create(&ThreadSSL, NULL) != 0) {
305 syslog(LOG_ERR, "Can't create TSD key: %s", strerror(errno));
310 * Bind the server to our favorite port.
311 * There is no need to check for errors, because webcit_tcp_server()
312 * exits if it doesn't succeed.
315 if (!IsEmptyStr(uds_listen_path)) {
316 syslog(LOG_DEBUG, "Attempting to create listener socket at %s...", uds_listen_path);
317 msock = webcit_uds_server(uds_listen_path, LISTEN_QUEUE_LENGTH);
320 syslog(LOG_DEBUG, "Attempting to bind to port %d...", http_port);
321 msock = webcit_tcp_server(ip_addr, http_port, LISTEN_QUEUE_LENGTH);
329 syslog(LOG_INFO, "Listening on socket %d", msock);
330 signal(SIGPIPE, SIG_IGN);
332 pthread_mutex_init(&SessionListMutex, NULL);
335 * Start up the housekeeping thread
337 pthread_attr_init(&attr);
338 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
339 pthread_create(&SessThread, &attr, (void *(*)(void *)) housekeeping_loop, NULL);
342 * If this is an HTTPS server, fire up SSL
351 /* Become a worker thread. More worker threads will be spawned as they are needed. */
353 ShutDownLibCitadel();