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