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