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