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 size_t basesize = 2; /* how big should strbufs be on creation? */
69 pthread_attr_t attr; /* Thread attributes */
70 int a; /* General-purpose variables */
71 char tracefile[PATH_MAX];
72 char ip_addr[256]="0.0.0.0";
76 char relhome[PATH_MAX]="";
77 char webcitdir[PATH_MAX] = DATADIR;
79 const char *basedir = NULL;
80 char uds_listen_path[PATH_MAX]; /* listen on a unix domain socket? */
83 WildFireInitBacktrace(argv[0], 2);
87 /* Ensure that we are linked to the correct version of libcitadel */
88 if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
89 fprintf(stderr, " You are running libcitadel version %d\n", (libcitadel_version_number()));
90 fprintf(stderr, "WebCit was compiled against version %d\n", LIBCITADEL_VERSION_NUMBER);
94 strcpy(uds_listen_path, "");
96 /* Parse command line */
97 while ((a = getopt(argc, argv, "h:i:p:t:T:B:x:U:P:cf:Z")) != EOF)
100 Username = NewStrBufPlain(optarg, -1);
103 Passvoid = NewStrBufPlain(optarg, -1);
106 hdir = strdup(optarg);
109 safestrncpy(webcitdir, hdir, sizeof webcitdir);
112 safestrncpy(relhome, relhome, sizeof relhome);
114 /* free(hdir); TODO: SHOULD WE DO THIS? */
118 case 'B': /* Basesize */
119 basesize = atoi(optarg);
121 StartLibCitadel(basesize);
124 safestrncpy(ip_addr, optarg, sizeof ip_addr);
127 http_port = atoi(optarg);
128 if (http_port == 0) {
129 safestrncpy(uds_listen_path, optarg, sizeof uds_listen_path);
133 safestrncpy(tracefile, optarg, sizeof tracefile);
134 rvfp = freopen(tracefile, "w", stdout);
135 rvfp = freopen(tracefile, "w", stderr);
136 rvfp = freopen(tracefile, "r", stdin);
139 LoadTemplates = atoi(optarg);
140 dbg_analyze_msg = (LoadTemplates && (1<<1)) != 0;
141 dbg_bactrace_template_errors = (LoadTemplates && (1<<2)) != 0;
147 verbosity = atoi(optarg);
153 server_cookie = malloc(256);
154 if (server_cookie != NULL) {
155 safestrncpy(server_cookie,
156 "Set-cookie: wcserver=",
159 (&server_cookie[strlen(server_cookie)],
161 syslog(2, "gethostname: %s\n",
168 fprintf(stderr, "usage: webcit "
169 "[-i ip_addr] [-p http_port] "
170 "[-t tracefile] [-c] [-f] "
171 "[-T Templatedebuglevel] "
172 "[-Z] [-G i18ndumpfile] "
174 "[-s] [-S cipher_suites]"
176 "[-U Username -P Password]"
178 "[remotehost [remoteport]]\n");
183 ctdlhost = argv[optind];
185 ctdlport = argv[optind];
188 webcit_calc_dirs_n_files(relh, basedir, home, webcitdir, relhome);
189 LoadIconDir(static_icon_dir);
191 /* Tell 'em who's in da house */
192 syslog(1, PACKAGE_STRING "\n");
193 syslog(1, "Copyright (C) 1996-2009 by the Citadel development team.\n"
194 "This software is distributed under the terms of the "
195 "GNU General Public License.\n\n"
199 /* initialize the International Bright Young Thing */
201 initialise_modules();
202 initialize_viewdefs();
207 /* Use our own prefix on tzid's generated from system tzdata */
208 icaltimezone_set_tzid_prefix("/citadel.org/");
211 * Set up a place to put thread-specific data.
212 * We only need a single pointer per thread - it points to the
213 * wcsession struct to which the thread is currently bound.
215 if (pthread_key_create(&MyConKey, NULL) != 0) {
216 syslog(1, "Can't create TSD key: %s\n", strerror(errno));
218 InitialiseSemaphores ();
222 * Start up the housekeeping thread
224 pthread_attr_init(&attr);
225 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
230 FreeStrBuf(&Username);
231 FreeStrBuf(&Passvoid);