Trust the compiler to do short-circuit evaluation.
[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-2008 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 #if HAVE_BACKTRACE
17 #include <execinfo.h>
18 #endif
19 #include "modules_init.h"
20 #ifndef HAVE_SNPRINTF
21 int vsnprintf(char *buf, size_t max, const char *fmt, va_list argp);
22 #endif
23
24 int verbosity = 9;              /* Logging level */
25 int msock;                      /* master listening socket */
26 int is_https = 0;               /* Nonzero if I am an HTTPS service */
27 int follow_xff = 0;             /* Follow X-Forwarded-For: header */
28 int home_specified = 0;         /* did the user specify a homedir? */
29 int time_to_die = 0;            /* Nonzero if server is shutting down */
30 extern void *context_loop(int*);
31 extern void *housekeeping_loop(void);
32 extern pthread_mutex_t SessionListMutex;
33 extern pthread_key_t MyConKey;
34
35
36 char ctdl_key_dir[PATH_MAX]=SSL_DIR;
37 char file_crpt_file_key[PATH_MAX]="";
38 char file_crpt_file_csr[PATH_MAX]="";
39 char file_crpt_file_cer[PATH_MAX]="";
40
41 char socket_dir[PATH_MAX];                      /* where to talk to our citadel server */
42 static const char editor_absolut_dir[PATH_MAX]=EDITORDIR;       /* nailed to what configure gives us. */
43 static char static_dir[PATH_MAX];               /* calculated on startup */
44 static char static_local_dir[PATH_MAX];         /* calculated on startup */
45 static char static_icon_dir[PATH_MAX];          /* where should we find our mime icons? */
46 char  *static_dirs[]={                          /* needs same sort order as the web mapping */
47         (char*)static_dir,                      /* our templates on disk */
48         (char*)static_local_dir,                /* user provided templates disk */
49         (char*)editor_absolut_dir,              /* the editor on disk */
50         (char*)static_icon_dir                  /* our icons... */
51 };
52
53 /*
54  * Subdirectories from which the client may request static content
55  *
56  * (If you add more, remember to increment 'ndirs' below)
57  */
58 char *static_content_dirs[] = {
59         "static",                     /* static templates */
60         "static.local",               /* site local static templates */
61         "tiny_mce"                    /* rich text editor */
62 };
63
64 int ndirs=3;
65
66
67 char *server_cookie = NULL;     /* our Cookie connection to the client */
68 int http_port = PORT_NUM;       /* Port to listen on */
69 char *ctdlhost = DEFAULT_HOST;  /* our name */
70 char *ctdlport = DEFAULT_PORT;  /* our Port */
71 int setup_wizard = 0;           /* should we run the setup wizard? \todo */
72 char wizard_filename[PATH_MAX]; /* where's the setup wizard? */
73 int running_as_daemon = 0;      /* should we deamonize on startup? */
74
75
76 /* 
77  * This is a generic function to set up a master socket for listening on
78  * a TCP port.  The server shuts down if the bind fails.
79  *
80  * ip_addr      IP address to bind
81  * port_number  port number to bind
82  * queue_len    number of incoming connections to allow in the queue
83  */
84 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
85 {
86         struct sockaddr_in sin;
87         int s, i;
88
89         memset(&sin, 0, sizeof(sin));
90         sin.sin_family = AF_INET;
91         if (ip_addr == NULL) {
92                 sin.sin_addr.s_addr = INADDR_ANY;
93         } else {
94                 sin.sin_addr.s_addr = inet_addr(ip_addr);
95         }
96
97         if (sin.sin_addr.s_addr == INADDR_NONE) {
98                 sin.sin_addr.s_addr = INADDR_ANY;
99         }
100
101         if (port_number == 0) {
102                 lprintf(1, "Cannot start: no port number specified.\n");
103                 exit(WC_EXIT_BIND);
104         }
105         sin.sin_port = htons((u_short) port_number);
106
107         s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
108         if (s < 0) {
109                 lprintf(1, "Can't create a socket: %s\n", strerror(errno));
110                 exit(WC_EXIT_BIND);
111         }
112         /* Set some socket options that make sense. */
113         i = 1;
114         setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
115
116         fcntl(s, F_SETFL, O_NONBLOCK); /* maide: this statement is incorrect
117                                           there should be a preceding F_GETFL
118                                           and a bitwise OR with the previous
119                                           fd flags */
120         
121         if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
122                 lprintf(1, "Can't bind: %s\n", strerror(errno));
123                 exit(WC_EXIT_BIND);
124         }
125         if (listen(s, queue_len) < 0) {
126                 lprintf(1, "Can't listen: %s\n", strerror(errno));
127                 exit(WC_EXIT_BIND);
128         }
129         return (s);
130 }
131
132
133
134 /*
135  * Create a Unix domain socket and listen on it
136  * sockpath - file name of the unix domain socket
137  * queue_len - Number of incoming connections to allow in the queue
138  */
139 int ig_uds_server(char *sockpath, int queue_len)
140 {
141         struct sockaddr_un addr;
142         int s;
143         int i;
144         int actual_queue_len;
145
146         actual_queue_len = queue_len;
147         if (actual_queue_len < 5) actual_queue_len = 5;
148
149         i = unlink(sockpath);
150         if ((i != 0) && (errno != ENOENT)) {
151                 lprintf(1, "webcit: can't unlink %s: %s\n",
152                         sockpath, strerror(errno));
153                 exit(WC_EXIT_BIND);
154         }
155
156         memset(&addr, 0, sizeof(addr));
157         addr.sun_family = AF_UNIX;
158         safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
159
160         s = socket(AF_UNIX, SOCK_STREAM, 0);
161         if (s < 0) {
162                 lprintf(1, "webcit: Can't create a socket: %s\n",
163                         strerror(errno));
164                 exit(WC_EXIT_BIND);
165         }
166
167         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
168                 lprintf(1, "webcit: Can't bind: %s\n",
169                         strerror(errno));
170                 exit(WC_EXIT_BIND);
171         }
172
173         if (listen(s, actual_queue_len) < 0) {
174                 lprintf(1, "webcit: Can't listen: %s\n",
175                         strerror(errno));
176                 exit(WC_EXIT_BIND);
177         }
178
179         chmod(sockpath, 0777);
180         return(s);
181 }
182
183
184
185
186 /*
187  * Read data from the client socket.
188  *
189  * sock         socket fd to read from
190  * buf          buffer to read into 
191  * bytes        number of bytes to read
192  * timeout      Number of seconds to wait before timing out
193  *
194  * Possible return values:
195  *      1       Requested number of bytes has been read.
196  *      0       Request timed out.
197  *      -1      Connection is broken, or other error.
198  */
199 int client_read_to(int *sock, StrBuf *Target, StrBuf *Buf, int bytes, int timeout)
200 {
201         const char *Error;
202         int retval = 0;
203
204 #ifdef HAVE_OPENSSL
205         if (is_https) {
206                 while ((StrLength(Buf) + StrLength(Target) < bytes) &&
207                        (retval >= 0))
208                         retval = client_read_sslbuffer(Buf, timeout);
209                 if (retval >= 0) {
210                         StrBufAppendBuf(Target, Buf, 0); /// todo: Buf > bytes?
211 #ifdef HTTP_TRACING
212                         write(2, "\033[32m", 5);
213                         write(2, buf, bytes);
214                         write(2, "\033[30m", 5);
215 #endif
216                         return 1;
217                 }
218                 else {
219                         lprintf(2, "client_read_ssl() failed\n");
220                         return -1;
221                 }
222         }
223 #endif
224
225         if (StrLength(Buf) > 0) {//// todo: what if Buf > bytes?
226                 StrBufAppendBuf(Target, Buf, 0);
227         }
228         retval = StrBufReadBLOB(Target, 
229                            sock, 
230                            (StrLength(Target) > 0), 
231                            bytes - StrLength(Target), 
232                                 &Error);
233         if (retval < 0) {
234                 lprintf(2, "client_read() failed: %s\n",
235                         Error);
236                 return retval;
237         }
238
239 #ifdef HTTP_TRACING
240         write(2, "\033[32m", 5);
241         write(2, buf, bytes);
242         write(2, "\033[30m", 5);
243 #endif
244         return 1;
245 }
246
247
248 /*
249  * Begin buffering HTTP output so we can transmit it all in one write operation later.
250  */
251 void begin_burst(void)
252 {
253         if (WC->WBuf == NULL)
254                 WC->WBuf = NewStrBufPlain(NULL, 32768);
255 }
256
257
258 /*
259  * Finish buffering HTTP output.  [Compress using zlib and] output with a Content-Length: header.
260  */
261 long end_burst(void)
262 {
263         struct wcsession *WCC = WC;
264         const char *ptr, *eptr;
265         long count;
266         ssize_t res;
267         fd_set wset;
268         int fdflags;
269
270 #ifdef HAVE_ZLIB
271         /* Perform gzip compression, if enabled and supported by client */
272         if ((WCC->gzip_ok) && CompressBuffer(WCC->WBuf))
273         {
274                 hprintf("Content-encoding: gzip\r\n");
275         }
276 #endif  /* HAVE_ZLIB */
277
278         hprintf("Content-length: %d\r\n\r\n", StrLength(WCC->WBuf));
279
280         ptr = ChrPtr(WCC->HBuf);
281         count = StrLength(WCC->HBuf);
282         eptr = ptr + count;
283
284 #ifdef HAVE_OPENSSL
285         if (is_https) {
286                 client_write_ssl(WCC->HBuf);
287                 client_write_ssl(WCC->WBuf);
288                 return (count);
289         }
290 #endif
291
292         
293 #ifdef HTTP_TRACING
294         
295         write(2, "\033[34m", 5);
296         write(2, ptr, StrLength(WCC->WBuf));
297         write(2, "\033[30m", 5);
298 #endif
299         fdflags = fcntl(WC->http_sock, F_GETFL);
300
301         while (ptr < eptr) {
302                 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
303                         FD_ZERO(&wset);
304                         FD_SET(WCC->http_sock, &wset);
305                         if (select(WCC->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
306                                 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
307                                 return -1;
308                         }
309                 }
310
311                 if ((res = write(WCC->http_sock, 
312                                  ptr,
313                                  count)) == -1) {
314                         lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
315                         wc_backtrace();
316                         return res;
317                 }
318                 count -= res;
319                 ptr += res;
320         }
321
322         ptr = ChrPtr(WCC->WBuf);
323         count = StrLength(WCC->WBuf);
324         eptr = ptr + count;
325
326 #ifdef HTTP_TRACING
327         
328         write(2, "\033[34m", 5);
329         write(2, ptr, StrLength(WCC->WBuf));
330         write(2, "\033[30m", 5);
331 #endif
332
333         while (ptr < eptr) {
334                 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
335                         FD_ZERO(&wset);
336                         FD_SET(WCC->http_sock, &wset);
337                         if (select(WCC->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
338                                 lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
339                                 return -1;
340                         }
341                 }
342
343                 if ((res = write(WCC->http_sock, 
344                                  ptr,
345                                  count)) == -1) {
346                         lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
347                         wc_backtrace();
348                         return res;
349                 }
350                 count -= res;
351                 ptr += res;
352         }
353
354         return StrLength(WCC->WBuf);
355 }
356
357
358
359 /*
360  * Read data from the client socket with default timeout.
361  * (This is implemented in terms of client_read_to() and could be
362  * justifiably moved out of sysdep.c)
363  *
364  * sock         the socket fd to read from
365  * buf          the buffer to write to
366  * bytes        Number of bytes to read
367  */
368 int client_read(int *sock, StrBuf *Target, StrBuf *buf, int bytes)
369 {
370         return (client_read_to(sock, Target, buf, bytes, SLEEPING));
371 }
372
373
374 /*
375  * Get a LF-terminated line of text from the client.
376  * (This is implemented in terms of client_read() and could be
377  * justifiably moved out of sysdep.c)
378  *
379  * sock         socket fd to get client line from
380  * buf          buffer to write read data to
381  * bufsiz       how many bytes to read
382  *
383  * returns the number of bytes read
384  */
385 /////int client_getln(int *sock, char *buf, int bufsiz)
386 /////{
387 /////   int i, retval;
388 /////
389 /////   /* Read one character at a time.*/
390 /////   for (i = 0; *sock > 0; i++) {
391 /////           retval = client_read(sock, &buf[i], 1);
392 /////           if (retval < 0)
393 /////                   return retval;
394 /////           if (retval != 1 || buf[i] == '\n' || i == (bufsiz-1))
395 /////                   break;
396 /////           if ( (!isspace(buf[i])) && (!isprint(buf[i])) ) {
397 /////                   /* Non printable character recieved from client */
398 /////                   return(-1);
399 /////           }
400 /////   }
401 /////
402 /////   /* If we got a long line, discard characters until the newline. */
403 /////   if (i == (bufsiz-1))
404 /////           while (buf[i] != '\n' && retval == 1)
405 /////                   retval = client_read(sock, &buf[i], 1);
406 /////
407 /////   /*
408 /////    * Strip any trailing non-printable characters.
409 /////    */
410 /////   buf[i] = 0;
411 /////   while ((i > 0) && (!isprint(buf[i - 1]))) {
412 /////           buf[--i] = 0;
413 /////   }
414 /////   return (retval);
415 /////}
416
417 /*
418  * Shut us down the regular way.
419  * signum is the signal we want to forward
420  */
421 pid_t current_child;
422 void graceful_shutdown_watcher(int signum) {
423         lprintf (1, "bye; shutting down watcher.");
424         kill(current_child, signum);
425         if (signum != SIGHUP)
426                 exit(0);
427 }
428
429
430 int ClientGetLine(int *sock, StrBuf *Target, StrBuf *CLineBuf)
431 {
432         const char *Error, *pch, *pchs;
433         int rlen, len, retval = 0;
434
435         if (is_https) {
436                 int ntries = 0;
437                 if (StrLength(CLineBuf) > 0) {
438                         pchs = ChrPtr(CLineBuf);
439                         pch = strchr(pchs, '\n');
440                         if (pch != NULL) {
441                                 rlen = 0;
442                                 len = pch - pchs;
443                                 if (len > 0 && (*(pch - 1) == '\r') )
444                                         rlen ++;
445                                 StrBufSub(Target, CLineBuf, 0, len - rlen);
446                                 StrBufCutLeft(CLineBuf, len + 1);
447                                 return len - rlen;
448                         }
449                 }
450
451                 while (retval == 0) { 
452                                 pch = NULL;
453                                 pchs = ChrPtr(CLineBuf);
454                                 if (*pchs != '\0')
455                                         pch = strchr(pchs, '\n');
456                                 if (pch == NULL) {
457                                         retval = client_read_sslbuffer(CLineBuf, SLEEPING);
458                                         pchs = ChrPtr(CLineBuf);
459                                         pch = strchr(pchs, '\n');
460                                 }
461                                 if (retval == 0) {
462                                         sleeeeeeeeeep(1);
463                                         ntries ++;
464                                 }
465                                 if (ntries > 10)
466                                         return 0;
467                 }
468                 if ((retval > 0) && (pch != NULL)) {
469                         rlen = 0;
470                         len = pch - pchs;
471                         if (len > 0 && (*(pch - 1) == '\r') )
472                                 rlen ++;
473                         StrBufSub(Target, CLineBuf, 0, len - rlen);
474                         StrBufCutLeft(CLineBuf, len + 1);
475                         return len - rlen;
476
477                 }
478                 else 
479                         return -1;
480         }
481         else 
482                 return StrBufTCP_read_buffered_line(Target, 
483                                                     CLineBuf,
484                                                     sock,
485                                                     5,
486                                                     1,
487                                                     &Error);
488 }
489
490
491
492 /*
493  * Shut us down the regular way.
494  * signum is the signal we want to forward
495  */
496 pid_t current_child;
497 void graceful_shutdown(int signum) {
498 //      kill(current_child, signum);
499         char wd[SIZ];
500         FILE *FD;
501         int fd;
502         getcwd(wd, SIZ);
503         lprintf (1, "bye going down gracefull.[%d][%s]\n", signum, wd);
504         fd = msock;
505         msock = -1;
506         time_to_die = 1;
507         FD=fdopen(fd, "a+");
508         fflush (FD);
509         fclose (FD);
510         close(fd);
511 }
512
513
514 /*
515  * Start running as a daemon.
516  */
517 void start_daemon(char *pid_file) 
518 {
519         int status = 0;
520         pid_t child = 0;
521         FILE *fp;
522         int do_restart = 0;
523
524         current_child = 0;
525
526         /* Close stdin/stdout/stderr and replace them with /dev/null.
527          * We don't just call close() because we don't want these fd's
528          * to be reused for other files.
529          */
530         chdir("/");
531
532         signal(SIGHUP, SIG_IGN);
533         signal(SIGINT, SIG_IGN);
534         signal(SIGQUIT, SIG_IGN);
535
536         child = fork();
537         if (child != 0) {
538                 exit(0);
539         }
540
541         setsid();
542         umask(0);
543         freopen("/dev/null", "r", stdin);
544         freopen("/dev/null", "w", stdout);
545         freopen("/dev/null", "w", stderr);
546         signal(SIGTERM, graceful_shutdown_watcher);
547         signal(SIGHUP, graceful_shutdown_watcher);
548
549         do {
550                 current_child = fork();
551
552         
553                 if (current_child < 0) {
554                         perror("fork");
555                         ShutDownLibCitadel ();
556                         exit(errno);
557                 }
558         
559                 else if (current_child == 0) {  // child process
560 //                      signal(SIGTERM, graceful_shutdown);
561                         signal(SIGHUP, graceful_shutdown);
562
563                         return; /* continue starting webcit. */
564                 }
565         
566                 else { // watcher process
567 //                      signal(SIGTERM, SIG_IGN);
568 //                      signal(SIGHUP, SIG_IGN);
569                         if (pid_file) {
570                                 fp = fopen(pid_file, "w");
571                                 if (fp != NULL) {
572                                         fprintf(fp, "%d\n", getpid());
573                                         fclose(fp);
574                                 }
575                         }
576                         waitpid(current_child, &status, 0);
577                 }
578
579                 do_restart = 0;
580
581                 /* Did the main process exit with an actual exit code? */
582                 if (WIFEXITED(status)) {
583
584                         /* Exit code 0 means the watcher should exit */
585                         if (WEXITSTATUS(status) == 0) {
586                                 do_restart = 0;
587                         }
588
589                         /* Exit code 101-109 means the watcher should exit */
590                         else if ( (WEXITSTATUS(status) >= 101) && (WEXITSTATUS(status) <= 109) ) {
591                                 do_restart = 0;
592                         }
593
594                         /* Any other exit code means we should restart. */
595                         else {
596                                 do_restart = 1;
597                         }
598                 }
599
600                 /* Any other type of termination (signals, etc.) should also restart. */
601                 else {
602                         do_restart = 1;
603                 }
604
605         } while (do_restart);
606
607         if (pid_file) {
608                 unlink(pid_file);
609         }
610         ShutDownLibCitadel ();
611         exit(WEXITSTATUS(status));
612 }
613
614 /*
615  * Spawn an additional worker thread into the pool.
616  */
617 void spawn_another_worker_thread()
618 {
619         pthread_t SessThread;   /* Thread descriptor */
620         pthread_attr_t attr;    /* Thread attributes */
621         int ret;
622
623         lprintf(3, "Creating a new thread\n");
624
625         /* set attributes for the new thread */
626         pthread_attr_init(&attr);
627         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
628
629         /*
630          * Our per-thread stacks need to be bigger than the default size, otherwise
631          * the MIME parser crashes on FreeBSD, and the IMAP service crashes on
632          * 64-bit Linux.
633          */
634         if ((ret = pthread_attr_setstacksize(&attr, 1024 * 1024))) {
635                 lprintf(1, "pthread_attr_setstacksize: %s\n",
636                         strerror(ret));
637                 pthread_attr_destroy(&attr);
638         }
639
640         /* now create the thread */
641         if (pthread_create(&SessThread, &attr,
642                            (void *(*)(void *)) worker_entry, NULL)
643             != 0) {
644                 lprintf(1, "Can't create thread: %s\n", strerror(errno));
645         }
646
647         /* free up the attributes */
648         pthread_attr_destroy(&attr);
649 }
650
651 //#define DBG_PRINNT_HOOKS_AT_START
652 #ifdef DBG_PRINNT_HOOKS_AT_START
653 const char foobuf[32];
654 const char *nix(void *vptr) {snprintf(foobuf, 32, "%0x", (long) vptr); return foobuf;}
655 #endif 
656 void InitTemplateCache(void);
657 extern int LoadTemplates;
658 extern void LoadZoneFiles(void);
659
660 /*
661  * Here's where it all begins.
662  */
663 int main(int argc, char **argv)
664 {
665         pthread_t SessThread;           /* Thread descriptor */
666         pthread_attr_t attr;            /* Thread attributes */
667         int a, i;                       /* General-purpose variables */
668         char tracefile[PATH_MAX];
669         char ip_addr[256]="0.0.0.0";
670         char dirbuffer[PATH_MAX]="";
671         int relh=0;
672         int home=0;
673         int home_specified=0;
674         char relhome[PATH_MAX]="";
675         char webcitdir[PATH_MAX] = DATADIR;
676         char *pidfile = NULL;
677         char *hdir;
678         const char *basedir;
679 #ifdef ENABLE_NLS
680         char *locale = NULL;
681         char *mo = NULL;
682 #endif /* ENABLE_NLS */
683         char uds_listen_path[PATH_MAX]; /* listen on a unix domain socket? */
684
685         HandlerHash = NewHash(1, NULL);
686         PreferenceHooks = NewHash(1, NULL);
687         WirelessTemplateCache = NewHash(1, NULL);
688         WirelessLocalTemplateCache = NewHash(1, NULL);
689         LocalTemplateCache = NewHash(1, NULL);
690         TemplateCache = NewHash(1, NULL);
691         GlobalNS = NewHash(1, NULL);
692         Iterators = NewHash(1, NULL);
693         Contitionals = NewHash(1, NULL);
694         LoadZoneFiles();
695
696
697 #ifdef DBG_PRINNT_HOOKS_AT_START
698         dbg_PrintHash(HandlerHash, nix, NULL);
699 #endif
700
701         /* Ensure that we are linked to the correct version of libcitadel */
702         if (libcitadel_version_number() < LIBCITADEL_VERSION_NUMBER) {
703                 fprintf(stderr, " You are running libcitadel version %d.%02d\n",
704                         (libcitadel_version_number() / 100), (libcitadel_version_number() % 100));
705                 fprintf(stderr, "WebCit was compiled against version %d.%02d\n",
706                         (LIBCITADEL_VERSION_NUMBER / 100), (LIBCITADEL_VERSION_NUMBER % 100));
707                 return(1);
708         }
709
710         strcpy(uds_listen_path, "");
711
712         /* Parse command line */
713 #ifdef HAVE_OPENSSL
714         while ((a = getopt(argc, argv, "h:i:p:t:T:x:dD:cfs")) != EOF)
715 #else
716         while ((a = getopt(argc, argv, "h:i:p:t:T:x:dD:cf")) != EOF)
717 #endif
718                 switch (a) {
719                 case 'h':
720                         hdir = strdup(optarg);
721                         relh=hdir[0]!='/';
722                         if (!relh) safestrncpy(webcitdir, hdir,
723                                                                    sizeof webcitdir);
724                         else
725                                 safestrncpy(relhome, relhome,
726                                                         sizeof relhome);
727                         /* free(hdir); TODO: SHOULD WE DO THIS? */
728                         home_specified = 1;
729                         home=1;
730                         break;
731                 case 'd':
732                         running_as_daemon = 1;
733                         break;
734                 case 'D':
735                         pidfile = strdup(optarg);
736                         running_as_daemon = 1;
737                         break;
738                 case 'i':
739                         safestrncpy(ip_addr, optarg, sizeof ip_addr);
740                         break;
741                 case 'p':
742                         http_port = atoi(optarg);
743                         if (http_port == 0) {
744                                 safestrncpy(uds_listen_path, optarg, sizeof uds_listen_path);
745                         }
746                         break;
747                 case 't':
748                         safestrncpy(tracefile, optarg, sizeof tracefile);
749                         freopen(tracefile, "w", stdout);
750                         freopen(tracefile, "w", stderr);
751                         freopen(tracefile, "r", stdin);
752                         break;
753                 case 'T':
754                         LoadTemplates = atoi(optarg);
755                         break;
756                 case 'x':
757                         verbosity = atoi(optarg);
758                         break;
759                 case 'f':
760                         follow_xff = 1;
761                         break;
762                 case 'c':
763                         server_cookie = malloc(256);
764                         if (server_cookie != NULL) {
765                                 safestrncpy(server_cookie,
766                                        "Set-cookie: wcserver=",
767                                         256);
768                                 if (gethostname
769                                     (&server_cookie[strlen(server_cookie)],
770                                      200) != 0) {
771                                         lprintf(2, "gethostname: %s\n",
772                                                 strerror(errno));
773                                         free(server_cookie);
774                                 }
775                         }
776                         break;
777                 case 's':
778                         is_https = 1;
779                         break;
780                 default:
781                         fprintf(stderr, "usage: webcit "
782                                 "[-i ip_addr] [-p http_port] "
783                                 "[-t tracefile] [-c] [-f] "
784                                 "[-T Templatedebuglevel] "
785                                 "[-d] "
786 #ifdef HAVE_OPENSSL
787                                 "[-s] "
788 #endif
789                                 "[remotehost [remoteport]]\n");
790                         return 1;
791                 }
792
793         if (optind < argc) {
794                 ctdlhost = argv[optind];
795                 if (++optind < argc)
796                         ctdlport = argv[optind];
797         }
798
799         /* daemonize, if we were asked to */
800         if (running_as_daemon) {
801                 start_daemon(pidfile);
802         }
803         else {
804 ///             signal(SIGTERM, graceful_shutdown);
805                 signal(SIGHUP, graceful_shutdown);
806         }
807
808         /* Tell 'em who's in da house */
809         lprintf(1, PACKAGE_STRING "\n");
810         lprintf(1, "Copyright (C) 1996-2008 by the Citadel development team.\n"
811                 "This software is distributed under the terms of the "
812                 "GNU General Public License.\n\n"
813         );
814
815
816         /* initialize the International Bright Young Thing */
817 #ifdef ENABLE_NLS
818         initialize_locales();
819
820         locale = setlocale(LC_ALL, "");
821
822         mo = malloc(strlen(webcitdir) + 20);
823         lprintf(9, "Message catalog directory: %s\n", bindtextdomain("webcit", LOCALEDIR"/locale"));
824         free(mo);
825         lprintf(9, "Text domain: %s\n", textdomain("webcit"));
826         lprintf(9, "Text domain Charset: %s\n", bind_textdomain_codeset("webcit","UTF8"));
827         preset_locale();
828 #endif
829
830
831         /* calculate all our path on a central place */
832     /* where to keep our config */
833         
834 #define COMPUTE_DIRECTORY(SUBDIR) memcpy(dirbuffer,SUBDIR, sizeof dirbuffer);\
835         snprintf(SUBDIR,sizeof SUBDIR,  "%s%s%s%s%s%s%s", \
836                          (home&!relh)?webcitdir:basedir, \
837              ((basedir!=webcitdir)&(home&!relh))?basedir:"/", \
838              ((basedir!=webcitdir)&(home&!relh))?"/":"", \
839                          relhome, \
840              (relhome[0]!='\0')?"/":"",\
841                          dirbuffer,\
842                          (dirbuffer[0]!='\0')?"/":"");
843         basedir=RUNDIR;
844         COMPUTE_DIRECTORY(socket_dir);
845         basedir=WWWDIR "/static";
846         COMPUTE_DIRECTORY(static_dir);
847         basedir=WWWDIR "/static/icons";
848         COMPUTE_DIRECTORY(static_icon_dir);
849         basedir=WWWDIR "/static.local";
850         COMPUTE_DIRECTORY(static_local_dir);
851
852         snprintf(file_crpt_file_key,
853                  sizeof file_crpt_file_key, 
854                  "%s/citadel.key",
855                  ctdl_key_dir);
856         snprintf(file_crpt_file_csr,
857                  sizeof file_crpt_file_csr, 
858                  "%s/citadel.csr",
859                  ctdl_key_dir);
860         snprintf(file_crpt_file_cer,
861                  sizeof file_crpt_file_cer, 
862                  "%s/citadel.cer",
863                  ctdl_key_dir);
864
865         /* we should go somewhere we can leave our coredump, if enabled... */
866         lprintf(9, "Changing directory to %s\n", socket_dir);
867         if (chdir(webcitdir) != 0) {
868                 perror("chdir");
869         }
870         LoadIconDir(static_icon_dir);
871         InitTemplateCache();
872
873         initialise_modules();
874         initialize_viewdefs();
875         initialize_axdefs();
876
877         /* Tell libical to return an error instead of aborting if it sees badly formed iCalendar data. */
878         icalerror_errors_are_fatal = 0;
879
880         /*
881          * Set up a place to put thread-specific data.
882          * We only need a single pointer per thread - it points to the
883          * wcsession struct to which the thread is currently bound.
884          */
885         if (pthread_key_create(&MyConKey, NULL) != 0) {
886                 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
887         }
888         InitialiseSemaphores ();
889
890         /*
891          * Set up a place to put thread-specific SSL data.
892          * We don't stick this in the wcsession struct because SSL starts
893          * up before the session is bound, and it gets torn down between
894          * transactions.
895          */
896 #ifdef HAVE_OPENSSL
897         if (pthread_key_create(&ThreadSSL, NULL) != 0) {
898                 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
899         }
900 #endif
901
902         /*
903          * Bind the server to our favorite port.
904          * There is no need to check for errors, because ig_tcp_server()
905          * exits if it doesn't succeed.
906          */
907
908         if (!IsEmptyStr(uds_listen_path)) {
909                 lprintf(2, "Attempting to create listener socket at %s...\n", uds_listen_path);
910                 msock = ig_uds_server(uds_listen_path, LISTEN_QUEUE_LENGTH);
911         }
912         else {
913                 lprintf(2, "Attempting to bind to port %d...\n", http_port);
914                 msock = ig_tcp_server(ip_addr, http_port, LISTEN_QUEUE_LENGTH);
915         }
916
917         lprintf(2, "Listening on socket %d\n", msock);
918         signal(SIGPIPE, SIG_IGN);
919
920         pthread_mutex_init(&SessionListMutex, NULL);
921
922         /*
923          * Start up the housekeeping thread
924          */
925         pthread_attr_init(&attr);
926         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
927         pthread_create(&SessThread, &attr,
928                        (void *(*)(void *)) housekeeping_loop, NULL);
929
930
931         /*
932          * If this is an HTTPS server, fire up SSL
933          */
934 #ifdef HAVE_OPENSSL
935         if (is_https) {
936                 init_ssl();
937         }
938 #endif
939
940         /* Start a few initial worker threads */
941         for (i = 0; i < (MIN_WORKER_THREADS); ++i) {
942                 spawn_another_worker_thread();
943         }
944
945         /* now the original thread becomes another worker */
946         worker_entry();
947         ShutDownLibCitadel ();
948         DeleteHash(&HandlerHash);
949         DeleteHash(&PreferenceHooks);
950         return 0;
951 }
952
953
954 void ShutDownWebcit(void)
955 {
956         DeleteHash(&ZoneHash);
957         free_zone_directory ();
958         icaltimezone_release_zone_tab ();
959         icalmemory_free_ring ();
960         ShutDownLibCitadel ();
961         DeleteHash(&HandlerHash);
962         DeleteHash(&PreferenceHooks);
963         DeleteHash(&GlobalNS);
964         DeleteHash(&WirelessTemplateCache);
965         DeleteHash(&WirelessLocalTemplateCache);
966         DeleteHash(&TemplateCache);
967         DeleteHash(&LocalTemplateCache);
968         DeleteHash(&Iterators);
969         DeleteHash(&Contitionals);
970 #ifdef ENABLE_NLS
971         ShutdownLocale();
972 #endif
973 #ifdef HAVE_OPENSSL
974         if (is_https) {
975                 shutdown_ssl();
976         }
977 #endif
978 }
979
980 /*
981  * Entry point for worker threads
982  */
983 void worker_entry(void)
984 {
985         int ssock;
986         int i = 0;
987         int fail_this_transaction = 0;
988         int ret;
989         struct timeval tv;
990         fd_set readset, tempset;
991
992         tv.tv_sec = 0;
993         tv.tv_usec = 10000;
994         FD_ZERO(&readset);
995         FD_SET(msock, &readset);
996
997         do {
998                 /* Only one thread can accept at a time */
999                 fail_this_transaction = 0;
1000                 ssock = -1; 
1001                 errno = EAGAIN;
1002                 do {
1003                         ret = -1; /* just one at once should select... */
1004                         begin_critical_section(S_SELECT);
1005
1006                         FD_ZERO(&tempset);
1007                         if (msock > 0) FD_SET(msock, &tempset);
1008                         tv.tv_sec = 0;
1009                         tv.tv_usec = 10000;
1010                         if (msock > 0)  ret = select(msock+1, &tempset, NULL, NULL,  &tv);
1011                         end_critical_section(S_SELECT);
1012                         if ((ret < 0) && (errno != EINTR) && (errno != EAGAIN))
1013                         {// EINTR and EAGAIN are thrown but not of interest.
1014                                 lprintf(2, "accept() failed:%d %s\n",
1015                                         errno, strerror(errno));
1016                         }
1017                         else if ((ret > 0) && (msock > 0) && FD_ISSET(msock, &tempset))
1018                         {// Successfully selected, and still not shutting down? Accept!
1019                                 ssock = accept(msock, NULL, 0);
1020                         }
1021                         
1022                 } while ((msock > 0) && (ssock < 0)  && (time_to_die == 0));
1023
1024                 if ((msock == -1)||(time_to_die))
1025                 {// ok, we're going down.
1026                         int shutdown = 0;
1027
1028                         /* the first to come here will have to do the cleanup.
1029                          * make shure its realy just one.
1030                          */
1031                         begin_critical_section(S_SHUTDOWN);
1032                         if (msock == -1)
1033                         {
1034                                 msock = -2;
1035                                 shutdown = 1;
1036                         }
1037                         end_critical_section(S_SHUTDOWN);
1038                         if (shutdown == 1)
1039                         {// we're the one to cleanup the mess.
1040                                 lprintf(2, "I'm master shutdown: tagging sessions to be killed.\n");
1041                                 shutdown_sessions();
1042                                 lprintf(2, "master shutdown: waiting for others\n");
1043                                 sleeeeeeeeeep(1); // wait so some others might finish...
1044                                 lprintf(2, "master shutdown: cleaning up sessions\n");
1045                                 do_housekeeping();
1046                                 lprintf(2, "master shutdown: cleaning up libical\n");
1047
1048                                 ShutDownWebcit();
1049
1050                                 lprintf(2, "master shutdown exiting!.\n");                              
1051                                 exit(0);
1052                         }
1053                         break;
1054                 }
1055                 if (ssock < 0 ) continue;
1056
1057                 if (msock < 0) {
1058                         if (ssock > 0) close (ssock);
1059                         lprintf(2, "inbetween.");
1060                         pthread_exit(NULL);
1061                 } else { // Got it? do some real work!
1062                         /* Set the SO_REUSEADDR socket option */
1063                         i = 1;
1064                         setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
1065                                    &i, sizeof(i));
1066
1067                         /* If we are an HTTPS server, go crypto now. */
1068 #ifdef HAVE_OPENSSL
1069                         if (is_https) {
1070                                 if (starttls(ssock) != 0) {
1071                                         fail_this_transaction = 1;
1072                                         close(ssock);
1073                                 }
1074                         }
1075 #endif
1076
1077                         if (fail_this_transaction == 0) {
1078
1079                                 /* Perform an HTTP transaction... */
1080                                 context_loop(&ssock);
1081
1082                                 /* Shut down SSL/TLS if required... */
1083 #ifdef HAVE_OPENSSL
1084                                 if (is_https) {
1085                                         endtls();
1086                                 }
1087 #endif
1088
1089                                 /* ...and close the socket. */
1090                                 if (ssock > 0)
1091                                         lingering_close(ssock);
1092                         }
1093
1094                 }
1095
1096         } while (!time_to_die);
1097
1098         lprintf (1, "bye\n");
1099         pthread_exit(NULL);
1100 }
1101
1102 /*
1103  * print log messages 
1104  * logs to stderr if loglevel is lower than the verbosity set at startup
1105  *
1106  * loglevel     level of the message
1107  * format       the printf like format string
1108  * ...          the strings to put into format
1109  */
1110 int lprintf(int loglevel, const char *format, ...)
1111 {
1112         va_list ap;
1113
1114         if (loglevel <= verbosity) {
1115                 va_start(ap, format);
1116                 vfprintf(stderr, format, ap);
1117                 va_end(ap);
1118                 fflush(stderr);
1119         }
1120         return 1;
1121 }
1122
1123
1124 /*
1125  * print the actual stack frame.
1126  */
1127 void wc_backtrace(void)
1128 {
1129 #ifdef HAVE_BACKTRACE
1130         void *stack_frames[50];
1131         size_t size, i;
1132         char **strings;
1133
1134
1135         size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
1136         strings = backtrace_symbols(stack_frames, size);
1137         for (i = 0; i < size; i++) {
1138                 if (strings != NULL)
1139                         lprintf(1, "%s\n", strings[i]);
1140                 else
1141                         lprintf(1, "%p\n", stack_frames[i]);
1142         }
1143         free(strings);
1144 #endif
1145 }
1146