Revert "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  * 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 int verbosity;           /* Logging level */
22 extern char static_icon_dir[PATH_MAX];          /* where should we find our mime icons */
23 extern long MaxRead; 
24 int is_https = 0;               /* Nonzero if I am an HTTPS service */
25 int follow_xff = 0;             /* Follow X-Forwarded-For: header */
26 int home_specified = 0;         /* did the user specify a homedir? */
27 int DisableGzip = 0;
28 StrBuf *site_prefix = NULL;
29 extern pthread_mutex_t SessionListMutex;
30 extern pthread_key_t MyConKey;
31
32 extern void *housekeeping_loop(void);
33 extern int webcit_tcp_server(char *ip_addr, int port_number, int queue_len);
34 extern int webcit_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_backtrace_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]="*";
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, "R:u:h:i:p:t:T:B:x:dD:G:cfsS:Z")) != EOF)
117 #else
118         while ((a = getopt(argc, argv, "R: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_backtrace_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                 case 'R':
208                         MaxRead = atol(optarg);
209                         break;
210                 default:
211                         fprintf(stderr, "usage: webcit "
212                                 "[-i ip_addr] [-p http_port] "
213                                 "[-t tracefile] [-c] [-f] "
214                                 "[-T Templatedebuglevel] "
215                                 "[-d] [-Z] [-G i18ndumpfile] "
216 #ifdef HAVE_OPENSSL
217                                 "[-s] [-S cipher_suites]"
218 #endif
219                                 "[remotehost [remoteport]]\n");
220                         return 1;
221                 }
222
223         if (optind < argc) {
224                 ctdlhost = argv[optind];
225                 if (++optind < argc)
226                         ctdlport = argv[optind];
227         }
228
229         /* daemonize, if we were asked to */
230         if (!DumpTemplateI18NStrings && running_as_daemon) {
231                 start_daemon(pidfile);
232         }
233         else {
234                 signal(SIGINT, graceful_shutdown);
235                 signal(SIGHUP, graceful_shutdown);
236         }
237
238         webcit_calc_dirs_n_files(relh, basedir, home, webcitdir, relhome);
239         LoadIconDir(static_icon_dir);
240
241         /* Tell 'em who's in da house */
242         lprintf(1, PACKAGE_STRING "\n");
243         lprintf(1, "Copyright (C) 1996-2010 by the Citadel development team.\n"
244                 "This software is distributed under the terms of the "
245                 "GNU General Public License.\n\n"
246         );
247
248
249         /* initialize the International Bright Young Thing */
250
251         initialise_modules();
252
253         InitTemplateCache();
254         if (DumpTemplateI18NStrings) {
255                 FILE *fd;
256                 StrBufAppendBufPlain(I18nDump, HKEY("}\n"), 0);
257                 if (StrLength(I18nDump) < 50) {
258                         lprintf(1, "********************************************************************************\n");
259                         lprintf(1, "*        No strings found in templates!  Are you sure they're there?           *\n");
260                         lprintf(1, "********************************************************************************\n");
261                         return -1;
262                 }
263                 fd = fopen(I18nDumpFile, "w");
264                 if (fd == NULL) {
265                         lprintf(1, "********************************************************************************\n");
266                         lprintf(1, "*                  unable to open I18N dumpfile [%s]         *\n", I18nDumpFile);
267                         lprintf(1, "********************************************************************************\n");
268                         return -1;
269                 }
270                 rv = fwrite(ChrPtr(I18nDump), 1, StrLength(I18nDump), fd);
271                 fclose(fd);
272                 return 0;
273         }
274
275
276         /* Tell libical to return an error instead of aborting if it sees badly formed iCalendar data. */
277         icalerror_errors_are_fatal = 0;
278
279         /* Use our own prefix on tzid's generated from system tzdata */
280         icaltimezone_set_tzid_prefix("/citadel.org/");
281
282         /*
283          * Set up a place to put thread-specific data.
284          * We only need a single pointer per thread - it points to the
285          * wcsession struct to which the thread is currently bound.
286          */
287         if (pthread_key_create(&MyConKey, NULL) != 0) {
288                 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
289         }
290         InitialiseSemaphores ();
291
292         /*
293          * Set up a place to put thread-specific SSL data.
294          * We don't stick this in the wcsession struct because SSL starts
295          * up before the session is bound, and it gets torn down between
296          * transactions.
297          */
298 #ifdef HAVE_OPENSSL
299         if (pthread_key_create(&ThreadSSL, NULL) != 0) {
300                 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
301         }
302 #endif
303
304         /*
305          * Bind the server to our favorite port.
306          * There is no need to check for errors, because webcit_tcp_server()
307          * exits if it doesn't succeed.
308          */
309
310         if (!IsEmptyStr(uds_listen_path)) {
311                 lprintf(2, "Attempting to create listener socket at %s...\n", uds_listen_path);
312                 msock = webcit_uds_server(uds_listen_path, LISTEN_QUEUE_LENGTH);
313         }
314         else {
315                 lprintf(2, "Attempting to bind to port %d...\n", http_port);
316                 msock = webcit_tcp_server(ip_addr, http_port, LISTEN_QUEUE_LENGTH);
317         }
318         if (msock < 0)
319         {
320                 ShutDownWebcit();
321                 return -msock;
322         }
323
324         lprintf(2, "Listening on socket %d\n", msock);
325         signal(SIGPIPE, SIG_IGN);
326
327         pthread_mutex_init(&SessionListMutex, NULL);
328
329         /*
330          * Start up the housekeeping thread
331          */
332         pthread_attr_init(&attr);
333         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
334         pthread_create(&SessThread, &attr,
335                        (void *(*)(void *)) housekeeping_loop, NULL);
336
337
338         /*
339          * If this is an HTTPS server, fire up SSL
340          */
341 #ifdef HAVE_OPENSSL
342         if (is_https) {
343                 init_ssl();
344         }
345 #endif
346         drop_root(UID);
347
348         /* Become a worker thread.  More worker threads will be spawned as they are needed. */
349         worker_entry();
350         ShutDownLibCitadel ();
351         return 0;
352 }
353
354
355
356
357
358
359