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