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-2016 by the citadel.org developers.
7 * This program is released under the terms of the GNU General Public License v3.
10 #include "../webcit.h"
11 #include "../webserver.h"
12 #include "../modules_init.h"
15 #include <CUnit/CUnit.h>
16 #include <CUnit/Basic.h>
17 #include <CUnit/TestDB.h>
20 CU_EXPORT void CU_automated_run_tests(void);
21 CU_EXPORT CU_ErrorCode CU_list_tests_to_file(void);
22 CU_EXPORT void CU_set_output_filename(const char* szFilenameRoot);
24 extern int msock; /* master listening socket */
25 extern int verbosity; /* Logging level */
26 extern char static_icon_dir[PATH_MAX]; /* where should we find our mime icons */
28 int is_https = 0; /* Nonzero if I am an HTTPS service */
29 int follow_xff = 0; /* Follow X-Forwarded-For: header */
30 int home_specified = 0; /* did the user specify a homedir? */
32 extern pthread_mutex_t SessionListMutex;
33 extern pthread_key_t MyConKey;
35 extern void run_tests(void);
36 extern int ig_tcp_server(char *ip_addr, int port_number, int queue_len);
37 extern int ig_uds_server(char *sockpath, int queue_len);
38 extern void webcit_calc_dirs_n_files(int relh, const char *basedir, int home, char *webcitdir, char *relhome);
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 running_as_daemon = 0; /* should we deamonize on startup? */
48 char wizard_filename[PATH_MAX];
50 StrBuf *Username = NULL; /* the test user... */
51 StrBuf *Passvoid = NULL; /* the test user... */
54 extern int dbg_analyze_msg;
55 extern int dbg_bactrace_template_errors;
56 extern int DumpTemplateI18NStrings;
57 extern StrBuf *I18nDump;
58 void InitTemplateCache(void);
59 extern int LoadTemplates;
64 * Here's where it all begins.
66 int main(int argc, char **argv)
68 pthread_attr_t attr; /* Thread attributes */
69 int a; /* General-purpose variables */
70 char tracefile[PATH_MAX];
71 char ip_addr[256]="0.0.0.0";
75 char relhome[PATH_MAX]="";
76 char webcitdir[PATH_MAX] = DATADIR;
78 const char *basedir = NULL;
79 char uds_listen_path[PATH_MAX]; /* listen on a unix domain socket? */
82 WildFireInitBacktrace(argv[0], 2);
86 /* Ensure that we are linked to the correct version of libcitadel */
87 if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
88 fprintf(stderr, " You are running libcitadel version %d\n", (libcitadel_version_number()));
89 fprintf(stderr, "WebCit was compiled against version %d\n", LIBCITADEL_VERSION_NUMBER);
93 strcpy(uds_listen_path, "");
95 /* Parse command line */
96 while ((a = getopt(argc, argv, "h:i:p:t:T:B:x:U:P:cf:Z")) != EOF)
99 Username = NewStrBufPlain(optarg, -1);
102 Passvoid = NewStrBufPlain(optarg, -1);
105 hdir = strdup(optarg);
108 safestrncpy(webcitdir, hdir, sizeof webcitdir);
111 safestrncpy(relhome, relhome, sizeof relhome);
113 /* free(hdir); TODO: SHOULD WE DO THIS? */
117 case 'B': /* deprecated */
120 safestrncpy(ip_addr, optarg, sizeof ip_addr);
123 http_port = atoi(optarg);
124 if (http_port == 0) {
125 safestrncpy(uds_listen_path, optarg, sizeof uds_listen_path);
129 safestrncpy(tracefile, optarg, sizeof tracefile);
130 rvfp = freopen(tracefile, "w", stdout);
131 rvfp = freopen(tracefile, "w", stderr);
132 rvfp = freopen(tracefile, "r", stdin);
135 LoadTemplates = atoi(optarg);
136 dbg_analyze_msg = (LoadTemplates && (1<<1)) != 0;
137 dbg_bactrace_template_errors = (LoadTemplates && (1<<2)) != 0;
143 verbosity = atoi(optarg);
149 server_cookie = malloc(256);
150 if (server_cookie != NULL) {
151 safestrncpy(server_cookie,
152 "Set-cookie: wcserver=",
155 (&server_cookie[strlen(server_cookie)],
157 syslog(2, "gethostname: %s\n",
164 fprintf(stderr, "usage: webcit "
165 "[-i ip_addr] [-p http_port] "
166 "[-t tracefile] [-c] [-f] "
167 "[-T Templatedebuglevel] "
168 "[-Z] [-G i18ndumpfile] "
170 "[-s] [-S cipher_suites]"
172 "[-U Username -P Password]"
174 "[remotehost [remoteport]]\n");
179 ctdlhost = argv[optind];
181 ctdlport = argv[optind];
184 webcit_calc_dirs_n_files(relh, basedir, home, webcitdir, relhome);
185 LoadIconDir(static_icon_dir);
187 /* Tell 'em who's in da house */
188 syslog(1, PACKAGE_STRING "\n");
189 syslog(1, "Copyright (C) 1996-2009 by the Citadel development team.\n"
190 "This software is distributed under the terms of the "
191 "GNU General Public License.\n\n"
195 /* initialize the International Bright Young Thing */
197 initialise_modules();
198 initialize_viewdefs();
203 /* Use our own prefix on tzid's generated from system tzdata */
204 icaltimezone_set_tzid_prefix("/citadel.org/");
207 * Set up a place to put thread-specific data.
208 * We only need a single pointer per thread - it points to the
209 * wcsession struct to which the thread is currently bound.
211 if (pthread_key_create(&MyConKey, NULL) != 0) {
212 syslog(1, "Can't create TSD key: %s\n", strerror(errno));
214 InitialiseSemaphores ();
218 * Start up the housekeeping thread
220 pthread_attr_init(&attr);
221 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
226 FreeStrBuf(&Username);
227 FreeStrBuf(&Passvoid);