]> code.citadel.org Git - citadel.git/blob - webcit/webserver.c
fix various incidents reported by CLANG Static analyzer:
[citadel.git] / webcit / webserver.c
1 /*
2  * $Id$
3  *
4  * This contains a simple multithreaded TCP server manager.  It sits around
5  * waiting on the specified port for incoming HTTP connections.  When a
6  * connection is established, it calls context_loop() from context_loop.c.
7  *
8  * Copyright (c) 1996-2010 by the citadel.org developers.
9  * This program is released under the terms of the GNU General Public License v3.
10  *
11  */
12
13 #include "webcit.h"
14 #include "webserver.h"
15
16 #include "modules_init.h"
17 #ifndef HAVE_SNPRINTF
18 int vsnprintf(char *buf, size_t max, const char *fmt, va_list argp);
19 #endif
20
21
22 extern int msock;                       /* master listening socket */
23 extern int verbosity;           /* Logging level */
24 extern char static_icon_dir[PATH_MAX];          /* where should we find our mime icons */
25 int is_https = 0;               /* Nonzero if I am an HTTPS service */
26 int follow_xff = 0;             /* Follow X-Forwarded-For: header */
27 int DisableGzip = 0;
28 extern pthread_mutex_t SessionListMutex;
29 extern pthread_key_t MyConKey;
30
31 extern void *housekeeping_loop(void);
32 extern int ig_tcp_server(char *ip_addr, int port_number, int queue_len);
33 extern int ig_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);
38
39 extern void drop_root(uid_t UID);
40
41 char socket_dir[PATH_MAX];                      /* where to talk to our citadel server */
42
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? */
50
51
52
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;}
58 #endif 
59 extern int dbg_analyze_msg;
60 extern int dbg_bactrace_template_errors;
61 extern int DumpTemplateI18NStrings;
62 extern StrBuf *I18nDump;
63 void InitTemplateCache(void);
64 extern int LoadTemplates;
65
66
67
68
69 /*
70  * Here's where it all begins.
71  */
72 int main(int argc, char **argv)
73 {
74         uid_t UID = -1;
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]="0.0.0.0";
81         int relh=0;
82         int home=0;
83         char relhome[PATH_MAX]="";
84         char webcitdir[PATH_MAX] = DATADIR;
85         char *pidfile = NULL;
86         char *hdir;
87         const char *basedir = NULL;
88         char uds_listen_path[PATH_MAX]; /* listen on a unix domain socket? */
89         const char *I18nDumpFile = NULL;
90         FILE *rvfp = NULL;
91         int rv = 0;
92
93         WildFireInitBacktrace(argv[0], 2);
94
95         start_modules ();
96
97 #ifdef DBG_PRINNT_HOOKS_AT_START
98 /*      dbg_PrintHash(HandlerHash, nix, NULL);*/
99 #endif
100
101         /* Ensure that we are linked to the correct version of libcitadel */
102         if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
103                 fprintf(stderr, " You are running libcitadel version %d.%02d\n",
104                         (libcitadel_version_number() / 100), (libcitadel_version_number() % 100));
105                 fprintf(stderr, "WebCit was compiled against version %d.%02d\n",
106                         (LIBCITADEL_VERSION_NUMBER / 100), (LIBCITADEL_VERSION_NUMBER % 100));
107                 return(1);
108         }
109
110         strcpy(uds_listen_path, "");
111
112         /* Parse command line */
113 #ifdef HAVE_OPENSSL
114         while ((a = getopt(argc, argv, "u:h:i:p:t:T:B:x:dD:G:cfsS:Z")) != EOF)
115 #else
116         while ((a = getopt(argc, argv, "u:h:i:p:t:T:B:x:dD:G:cfZ")) != EOF)
117 #endif
118                 switch (a) {
119                 case 'u':
120                         UID = atol(optarg);
121                         break;
122                 case 'h':
123                         hdir = strdup(optarg);
124                         relh=hdir[0]!='/';
125                         if (!relh) {
126                                 safestrncpy(webcitdir, hdir, sizeof webcitdir);
127                         }
128                         else {
129                                 safestrncpy(relhome, relhome, sizeof relhome);
130                         }
131                         /* free(hdir); TODO: SHOULD WE DO THIS? */
132                         home=1;
133                         break;
134                 case 'd':
135                         running_as_daemon = 1;
136                         break;
137                 case 'D':
138                         pidfile = strdup(optarg);
139                         running_as_daemon = 1;
140                         break;
141                 case 'B': /* Basesize */
142                         basesize = atoi(optarg);
143                         if (basesize > 2)
144                                 StartLibCitadel(basesize);
145                         break;
146                 case 'i':
147                         safestrncpy(ip_addr, optarg, sizeof ip_addr);
148                         break;
149                 case 'p':
150                         http_port = atoi(optarg);
151                         if (http_port == 0) {
152                                 safestrncpy(uds_listen_path, optarg, sizeof uds_listen_path);
153                         }
154                         break;
155                 case 't':
156                         safestrncpy(tracefile, optarg, sizeof tracefile);
157                         rvfp = freopen(tracefile, "w", stdout);
158                         rvfp = freopen(tracefile, "w", stderr);
159                         rvfp = freopen(tracefile, "r", stdin);
160                         break;
161                 case 'T':
162                         LoadTemplates = atoi(optarg);
163                         dbg_analyze_msg = (LoadTemplates && (1<<1)) != 0;
164                         dbg_bactrace_template_errors = (LoadTemplates && (1<<2)) != 0;
165                         break;
166                 case 'Z':
167                         DisableGzip = 1;
168                         break;
169                 case 'x':
170                         verbosity = atoi(optarg);
171                         break;
172                 case 'f':
173                         follow_xff = 1;
174                         break;
175                 case 'c':
176                         server_cookie = malloc(256);
177                         if (server_cookie != NULL) {
178                                 safestrncpy(server_cookie,
179                                        "Set-cookie: wcserver=",
180                                         256);
181                                 if (gethostname
182                                     (&server_cookie[strlen(server_cookie)],
183                                      200) != 0) {
184                                         lprintf(2, "gethostname: %s\n",
185                                                 strerror(errno));
186                                         free(server_cookie);
187                                 }
188                         }
189                         break;
190 #ifdef HAVE_OPENSSL
191                 case 's':
192                         is_https = 1;
193                         break;
194                 case 'S':
195                         is_https = 1;
196                         ssl_cipher_list = strdup(optarg);
197                         break;
198 #endif
199                 case 'G':
200                         DumpTemplateI18NStrings = 1;
201                         I18nDump = NewStrBufPlain(HKEY("int templatestrings(void)\n{\n"));
202                         I18nDumpFile = optarg;
203                         break;
204                 default:
205                         fprintf(stderr, "usage: webcit "
206                                 "[-i ip_addr] [-p http_port] "
207                                 "[-t tracefile] [-c] [-f] "
208                                 "[-T Templatedebuglevel] "
209                                 "[-d] [-Z] [-G i18ndumpfile] "
210 #ifdef HAVE_OPENSSL
211                                 "[-s] [-S cipher_suites]"
212 #endif
213                                 "[remotehost [remoteport]]\n");
214                         return 1;
215                 }
216
217         if (optind < argc) {
218                 ctdlhost = argv[optind];
219                 if (++optind < argc)
220                         ctdlport = argv[optind];
221         }
222
223         /* daemonize, if we were asked to */
224         if (!DumpTemplateI18NStrings && running_as_daemon) {
225                 start_daemon(pidfile);
226         }
227         else {
228                 signal(SIGINT, graceful_shutdown);
229                 signal(SIGHUP, graceful_shutdown);
230         }
231
232         webcit_calc_dirs_n_files(relh, basedir, home, webcitdir, relhome);
233         LoadIconDir(static_icon_dir);
234
235         /* Tell 'em who's in da house */
236         lprintf(1, PACKAGE_STRING "\n");
237         lprintf(1, "Copyright (C) 1996-2010 by the Citadel development team.\n"
238                 "This software is distributed under the terms of the "
239                 "GNU General Public License.\n\n"
240         );
241
242
243         /* initialize the International Bright Young Thing */
244
245         initialise_modules();
246         initialize_viewdefs();
247         initialize_axdefs();
248
249         InitTemplateCache();
250         if (DumpTemplateI18NStrings) {
251                 FILE *fd;
252                 StrBufAppendBufPlain(I18nDump, HKEY("}\n"), 0);
253                 if (StrLength(I18nDump) < 50) {
254                         lprintf(1, "********************************************************************************\n");
255                         lprintf(1, "*        No strings found in templates!  Are you sure they're there?           *\n");
256                         lprintf(1, "********************************************************************************\n");
257                         return -1;
258                 }
259                 fd = fopen(I18nDumpFile, "w");
260                 if (fd == NULL) {
261                         lprintf(1, "********************************************************************************\n");
262                         lprintf(1, "*                  unable to open I18N dumpfile [%s]         *\n", I18nDumpFile);
263                         lprintf(1, "********************************************************************************\n");
264                         return -1;
265                 }
266                 rv = fwrite(ChrPtr(I18nDump), 1, StrLength(I18nDump), fd);
267                 fclose(fd);
268                 return 0;
269         }
270
271
272         /* Tell libical to return an error instead of aborting if it sees badly formed iCalendar data. */
273         icalerror_errors_are_fatal = 0;
274
275         /* Use our own prefix on tzid's generated from system tzdata */
276         icaltimezone_set_tzid_prefix("/citadel.org/");
277
278         /*
279          * Set up a place to put thread-specific data.
280          * We only need a single pointer per thread - it points to the
281          * wcsession struct to which the thread is currently bound.
282          */
283         if (pthread_key_create(&MyConKey, NULL) != 0) {
284                 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
285         }
286         InitialiseSemaphores ();
287
288         /*
289          * Set up a place to put thread-specific SSL data.
290          * We don't stick this in the wcsession struct because SSL starts
291          * up before the session is bound, and it gets torn down between
292          * transactions.
293          */
294 #ifdef HAVE_OPENSSL
295         if (pthread_key_create(&ThreadSSL, NULL) != 0) {
296                 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
297         }
298 #endif
299
300         /*
301          * Bind the server to our favorite port.
302          * There is no need to check for errors, because ig_tcp_server()
303          * exits if it doesn't succeed.
304          */
305
306         if (!IsEmptyStr(uds_listen_path)) {
307                 lprintf(2, "Attempting to create listener socket at %s...\n", uds_listen_path);
308                 msock = ig_uds_server(uds_listen_path, LISTEN_QUEUE_LENGTH);
309         }
310         else {
311                 lprintf(2, "Attempting to bind to port %d...\n", http_port);
312                 msock = ig_tcp_server(ip_addr, http_port, LISTEN_QUEUE_LENGTH);
313         }
314         if (msock < 0)
315         {
316                 ShutDownWebcit();
317                 return -msock;
318         }
319
320         lprintf(2, "Listening on socket %d\n", msock);
321         signal(SIGPIPE, SIG_IGN);
322
323         pthread_mutex_init(&SessionListMutex, NULL);
324
325         /*
326          * Start up the housekeeping thread
327          */
328         pthread_attr_init(&attr);
329         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
330         pthread_create(&SessThread, &attr,
331                        (void *(*)(void *)) housekeeping_loop, NULL);
332
333
334         /*
335          * If this is an HTTPS server, fire up SSL
336          */
337 #ifdef HAVE_OPENSSL
338         if (is_https) {
339                 init_ssl();
340         }
341 #endif
342         drop_root(UID);
343
344         /* Become a worker thread.  More worker threads will be spawned as they are needed. */
345         worker_entry();
346         ShutDownLibCitadel ();
347         return 0;
348 }
349
350
351
352
353
354
355