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