Removed the obsolete -t and -x options in webcit and their documentation
[citadel.git] / webcit / webserver.c
1 /*
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.
5  *
6  * Copyright (c) 1996-2011 by the citadel.org developers.
7  * This program is released under the terms of the GNU General Public License v3.
8  *
9  */
10
11 #include "webcit.h"
12 #include "webserver.h"
13
14 #include "modules_init.h"
15 #ifndef HAVE_SNPRINTF
16 int vsnprintf(char *buf, size_t max, const char *fmt, va_list argp);
17 #endif
18
19
20 extern int msock;                               /* master listening socket */
21 extern char static_icon_dir[PATH_MAX];          /* where should we find our mime icons */
22 int is_https = 0;                               /* Nonzero if I am an HTTPS service */
23 int follow_xff = 0;                             /* Follow X-Forwarded-For: header? */
24 int DisableGzip = 0;
25 char *default_landing_page = NULL;              /* FIXME we need to populate this somehow */
26 extern pthread_mutex_t SessionListMutex;
27 extern pthread_key_t MyConKey;
28
29 extern void *housekeeping_loop(void);
30 extern int webcit_tcp_server(char *ip_addr, int port_number, int queue_len);
31 extern int webcit_uds_server(char *sockpath, int queue_len);
32 extern void graceful_shutdown_watcher(int signum);
33 extern void graceful_shutdown(int signum);
34 extern void start_daemon(char *pid_file);
35 extern void webcit_calc_dirs_n_files(int relh, const char *basedir, int home, char *webcitdir, char *relhome);
36 extern void worker_entry(void);
37 extern void drop_root(uid_t UID);
38
39 char socket_dir[PATH_MAX];      /* where to talk to our citadel server */
40 char *server_cookie = NULL;     /* our Cookie connection to the client */
41 int http_port = PORT_NUM;       /* Port to listen on */
42 char *ctdlhost = DEFAULT_HOST;  /* Host name or IP address of Citadel server */
43 char *ctdlport = DEFAULT_PORT;  /* Port number of Citadel server */
44 int setup_wizard = 0;           /* should we run the setup wizard? */
45 char wizard_filename[PATH_MAX]; /* location of file containing the last webcit version against which we ran setup wizard */
46 int running_as_daemon = 0;      /* should we deamonize on startup? */
47
48
49 /* #define DBG_PRINNT_HOOKS_AT_START */
50 #ifdef DBG_PRINNT_HOOKS_AT_START
51 extern HashList *HandlerHash;
52 const char foobuf[32];
53 const char *nix(void *vptr) {snprintf(foobuf, 32, "%0x", (long) vptr); return foobuf;}
54 #endif 
55 extern int dbg_analyze_msg;
56 extern int dbg_backtrace_template_errors;
57 extern int DumpTemplateI18NStrings;
58 extern StrBuf *I18nDump;
59 void InitTemplateCache(void);
60 extern int LoadTemplates;
61
62
63
64
65
66 /*
67  * Here's where it all begins.
68  */
69 int main(int argc, char **argv)
70 {
71         uid_t UID = -1;
72         size_t basesize = 2;            /* how big should strbufs be on creation? */
73         pthread_t SessThread;           /* Thread descriptor */
74         pthread_attr_t attr;            /* Thread attributes */
75         int a;                          /* General-purpose variable */
76         char ip_addr[256]="*";
77         int relh=0;
78         int home=0;
79         char relhome[PATH_MAX]="";
80         char webcitdir[PATH_MAX] = DATADIR;
81         char *pidfile = NULL;
82         char *hdir;
83         const char *basedir = NULL;
84         char uds_listen_path[PATH_MAX]; /* listen on a unix domain socket? */
85         const char *I18nDumpFile = NULL;
86         int rv = 0;
87
88         WildFireInitBacktrace(argv[0], 2);
89
90         start_modules ();
91
92 #ifdef DBG_PRINNT_HOOKS_AT_START
93 /*      dbg_PrintHash(HandlerHash, nix, NULL);*/
94 #endif
95
96         /* Ensure that we are linked to the correct version of libcitadel */
97         if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
98                 fprintf(stderr, " You are running libcitadel version %d.%02d\n",
99                         (libcitadel_version_number() / 100), (libcitadel_version_number() % 100));
100                 fprintf(stderr, "WebCit was compiled against version %d.%02d\n",
101                         (LIBCITADEL_VERSION_NUMBER / 100), (LIBCITADEL_VERSION_NUMBER % 100));
102                 return(1);
103         }
104
105         strcpy(uds_listen_path, "");
106
107         /* Parse command line */
108 #ifdef HAVE_OPENSSL
109         while ((a = getopt(argc, argv, "u:h:i:p:t:T:B:x:g:dD:G:cfsS:Z")) != EOF)
110 #else
111         while ((a = getopt(argc, argv, "u:h:i:p:t:T:B:x:g:dD:G:cfZ")) != EOF)
112 #endif
113                 switch (a) {
114                 case 'u':
115                         UID = atol(optarg);
116                         break;
117                 case 'h':
118                         hdir = strdup(optarg);
119                         relh=hdir[0]!='/';
120                         if (!relh) {
121                                 safestrncpy(webcitdir, hdir, sizeof webcitdir);
122                         }
123                         else {
124                                 safestrncpy(relhome, relhome, sizeof relhome);
125                         }
126                         /* free(hdir); TODO: SHOULD WE DO THIS? */
127                         home=1;
128                         break;
129                 case 'd':
130                         running_as_daemon = 1;
131                         break;
132                 case 'D':
133                         pidfile = strdup(optarg);
134                         running_as_daemon = 1;
135                         break;
136                 case 'g':
137                         default_landing_page = strdup(optarg);
138                         break;
139                 case 'B': /* Basesize */
140                         basesize = atoi(optarg);
141                         if (basesize > 2)
142                                 StartLibCitadel(basesize);
143                         break;
144                 case 'i':
145                         safestrncpy(ip_addr, optarg, sizeof ip_addr);
146                         break;
147                 case 'p':
148                         http_port = atoi(optarg);
149                         if (http_port == 0) {
150                                 safestrncpy(uds_listen_path, optarg, sizeof uds_listen_path);
151                         }
152                         break;
153                 case 't':
154                         /* no longer used, but ignored so old scripts don't break */
155                         break;
156                 case 'T':
157                         LoadTemplates = atoi(optarg);
158                         dbg_analyze_msg = (LoadTemplates && (1<<1)) != 0;
159                         dbg_backtrace_template_errors = (LoadTemplates && (1<<2)) != 0;
160                         break;
161                 case 'Z':
162                         DisableGzip = 1;
163                         break;
164                 case 'x':
165                         /* no longer used, but ignored so old scripts don't break */
166                         break;
167                 case 'f':
168                         follow_xff = 1;
169                         break;
170                 case 'c':
171                         server_cookie = malloc(256);
172                         if (server_cookie != NULL) {
173                                 safestrncpy(server_cookie,
174                                        "Set-cookie: wcserver=",
175                                         256);
176                                 if (gethostname
177                                     (&server_cookie[strlen(server_cookie)],
178                                      200) != 0) {
179                                         syslog(2, "gethostname: %s", strerror(errno));
180                                         free(server_cookie);
181                                 }
182                         }
183                         break;
184 #ifdef HAVE_OPENSSL
185                 case 's':
186                         is_https = 1;
187                         break;
188                 case 'S':
189                         is_https = 1;
190                         ssl_cipher_list = strdup(optarg);
191                         break;
192 #endif
193                 case 'G':
194                         DumpTemplateI18NStrings = 1;
195                         I18nDump = NewStrBufPlain(HKEY("int templatestrings(void)\n{\n"));
196                         I18nDumpFile = optarg;
197                         break;
198                 default:
199                         fprintf(stderr, "usage: webcit "
200                                 "[-i ip_addr] [-p http_port] "
201                                 "[-c] [-f] "
202                                 "[-T Templatedebuglevel] "
203                                 "[-d] [-Z] [-G i18ndumpfile] "
204 #ifdef HAVE_OPENSSL
205                                 "[-s] [-S cipher_suites]"
206 #endif
207                                 "[remotehost [remoteport]]\n");
208                         return 1;
209                 }
210
211         /* Start the logger */
212         openlog("webcit",
213                 ( running_as_daemon ? (LOG_PID) : (LOG_PID | LOG_PERROR) ),
214                 LOG_DAEMON
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         syslog(1, "%s", PACKAGE_STRING);
237         syslog(1, "Copyright (C) 1996-2011 by the citadel.org team");
238         syslog(1, " ");
239         syslog(1, "This program is open source software: you can redistribute it and/or");
240         syslog(1, "modify it under the terms of the GNU General Public License as published");
241         syslog(1, "by the Free Software Foundation, either version 3 of the License, or");
242         syslog(1, "(at your option) any later version.");
243         syslog(1, " ");
244         syslog(1, "This program is distributed in the hope that it will be useful,");
245         syslog(1, "but WITHOUT ANY WARRANTY; without even the implied warranty of");
246         syslog(1, "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the");
247         syslog(1, "GNU General Public License for more details.");
248         syslog(1, " ");
249         syslog(1, "You should have received a copy of the GNU General Public License");
250         syslog(1, "along with this program.  If not, see <http://www.gnu.org/licenses/>.");
251         syslog(1, " ");
252
253
254         /* initialize various subsystems */
255
256         initialise_modules();
257         InitTemplateCache();
258         if (DumpTemplateI18NStrings) {
259                 FILE *fd;
260                 StrBufAppendBufPlain(I18nDump, HKEY("}\n"), 0);
261                 if (StrLength(I18nDump) < 50) {
262                         syslog(1, "********************************************************************************\n");
263                         syslog(1, "*        No strings found in templates!  Are you sure they're there?           *\n");
264                         syslog(1, "********************************************************************************\n");
265                         return -1;
266                 }
267                 fd = fopen(I18nDumpFile, "w");
268                 if (fd == NULL) {
269                         syslog(1, "********************************************************************************\n");
270                         syslog(1, "*                  unable to open I18N dumpfile [%s]         *\n", I18nDumpFile);
271                         syslog(1, "********************************************************************************\n");
272                         return -1;
273                 }
274                 rv = fwrite(ChrPtr(I18nDump), 1, StrLength(I18nDump), fd);
275                 fclose(fd);
276                 return 0;
277         }
278
279
280         /* Tell libical to return an error instead of aborting if it sees badly formed iCalendar data. */
281         icalerror_errors_are_fatal = 0;
282
283         /* Use our own prefix on tzid's generated from system tzdata */
284         icaltimezone_set_tzid_prefix("/citadel.org/");
285
286         /*
287          * Set up a place to put thread-specific data.
288          * We only need a single pointer per thread - it points to the
289          * wcsession struct to which the thread is currently bound.
290          */
291         if (pthread_key_create(&MyConKey, NULL) != 0) {
292                 syslog(1, "Can't create TSD key: %s\n", strerror(errno));
293         }
294         InitialiseSemaphores ();
295
296         /*
297          * Set up a place to put thread-specific SSL data.
298          * We don't stick this in the wcsession struct because SSL starts
299          * up before the session is bound, and it gets torn down between
300          * transactions.
301          */
302 #ifdef HAVE_OPENSSL
303         if (pthread_key_create(&ThreadSSL, NULL) != 0) {
304                 syslog(1, "Can't create TSD key: %s\n", strerror(errno));
305         }
306 #endif
307
308         /*
309          * Bind the server to our favorite port.
310          * There is no need to check for errors, because webcit_tcp_server()
311          * exits if it doesn't succeed.
312          */
313
314         if (!IsEmptyStr(uds_listen_path)) {
315                 syslog(2, "Attempting to create listener socket at %s...\n", uds_listen_path);
316                 msock = webcit_uds_server(uds_listen_path, LISTEN_QUEUE_LENGTH);
317         }
318         else {
319                 syslog(2, "Attempting to bind to port %d...\n", http_port);
320                 msock = webcit_tcp_server(ip_addr, http_port, LISTEN_QUEUE_LENGTH);
321         }
322         if (msock < 0)
323         {
324                 ShutDownWebcit();
325                 return -msock;
326         }
327
328         syslog(2, "Listening on socket %d\n", msock);
329         signal(SIGPIPE, SIG_IGN);
330
331         pthread_mutex_init(&SessionListMutex, NULL);
332
333         /*
334          * Start up the housekeeping thread
335          */
336         pthread_attr_init(&attr);
337         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
338         pthread_create(&SessThread, &attr,
339                        (void *(*)(void *)) housekeeping_loop, NULL);
340
341
342         /*
343          * If this is an HTTPS server, fire up SSL
344          */
345 #ifdef HAVE_OPENSSL
346         if (is_https) {
347                 init_ssl();
348         }
349 #endif
350         drop_root(UID);
351
352         /* Become a worker thread.  More worker threads will be spawned as they are needed. */
353         worker_entry();
354         ShutDownLibCitadel();
355         return 0;
356 }
357
358
359
360
361
362
363