55439dc6907ec594849b225d1ea7b62127ea494a
[citadel.git] / webcit / webserver.c
1 /*
2  * $Id$
3  */
4 /**
5  * \defgroup Webserver This contains a simple multithreaded TCP server manager.  It sits around
6  * waiting on the specified port for incoming HTTP connections.  When a
7  * connection is established, it calls context_loop() from context_loop.c.
8  * \ingroup WebcitHttpServer
9  */
10
11 /*@{*/
12 #include "webcit.h"
13 #include "webserver.h"
14
15 #if HAVE_BACKTRACE
16 #include <execinfo.h>
17 #endif
18
19 #ifndef HAVE_SNPRINTF
20 int vsnprintf(char *buf, size_t max, const char *fmt, va_list argp);
21 #endif
22
23 int verbosity = 9;              /**< Logging level */
24 int msock;                      /**< master listening socket */
25 int is_https = 0;               /**< Nonzero if I am an HTTPS service */
26 int follow_xff = 0;             /**< Follow X-Forwarded-For: header */
27 int home_specified = 0;         /**< did the user specify a homedir? */
28 int time_to_die = 0;            /**< Nonzero if server is shutting down */
29 extern void *context_loop(int);
30 extern void *housekeeping_loop(void);
31 extern pthread_mutex_t SessionListMutex;
32 extern pthread_key_t MyConKey;
33
34
35 char ctdl_key_dir[PATH_MAX]=SSL_DIR;
36 char file_crpt_file_key[PATH_MAX]="";
37 char file_crpt_file_csr[PATH_MAX]="";
38 char file_crpt_file_cer[PATH_MAX]="";
39
40 char socket_dir[PATH_MAX];                      /**< where to talk to our citadel server */
41 static const char editor_absolut_dir[PATH_MAX]=EDITORDIR;       /**< nailed to what configure gives us. */
42 static char static_dir[PATH_MAX];               /**< calculated on startup */
43 static char static_local_dir[PATH_MAX];         /**< calculated on startup */
44 char  *static_dirs[]={                          /**< needs same sort order as the web mapping */
45         (char*)static_dir,                      /** our templates on disk */
46         (char*)static_local_dir,                /** user provided templates disk */
47         (char*)editor_absolut_dir               /** the editor on disk */
48 };
49
50 /**
51  * Subdirectories from which the client may request static content
52  *
53  * (If you add more, remember to increment 'ndirs' below)
54  */
55 char *static_content_dirs[] = {
56         "static",                     /** static templates */
57         "static.local",               /** site local static templates */
58         "tiny_mce"                    /** rich text editor */
59 };
60
61 int ndirs=3;
62
63
64 char *server_cookie = NULL;     /**< our Cookie connection to the client */
65 int http_port = PORT_NUM;       /**< Port to listen on */
66 char *ctdlhost = DEFAULT_HOST;  /**< our name */
67 char *ctdlport = DEFAULT_PORT;  /**< our Port */
68 int setup_wizard = 0;           /**< should we run the setup wizard? \todo */
69 char wizard_filename[PATH_MAX]; /**< where's the setup wizard? */
70 int running_as_daemon = 0;      /**< should we deamonize on startup? */
71
72
73 /** 
74  * \brief This is a generic function to set up a master socket for listening on
75  * a TCP port.  The server shuts down if the bind fails.
76  * \param ip_addr ip to bind to
77  * \param port_number the port to bind to 
78  * \param queue_len Number of incoming connections to allow in the queue
79  */
80 int ig_tcp_server(char *ip_addr, int port_number, int queue_len)
81 {
82         struct sockaddr_in sin;
83         int s, i;
84
85         memset(&sin, 0, sizeof(sin));
86         sin.sin_family = AF_INET;
87         if (ip_addr == NULL) {
88                 sin.sin_addr.s_addr = INADDR_ANY;
89         } else {
90                 sin.sin_addr.s_addr = inet_addr(ip_addr);
91         }
92
93         if (sin.sin_addr.s_addr == INADDR_NONE) {
94                 sin.sin_addr.s_addr = INADDR_ANY;
95         }
96
97         if (port_number == 0) {
98                 lprintf(1, "Cannot start: no port number specified.\n");
99                 exit(WC_EXIT_BIND);
100         }
101         sin.sin_port = htons((u_short) port_number);
102
103         s = socket(PF_INET, SOCK_STREAM, (getprotobyname("tcp")->p_proto));
104         if (s < 0) {
105                 lprintf(1, "Can't create a socket: %s\n", strerror(errno));
106                 exit(WC_EXIT_BIND);
107         }
108         /** Set some socket options that make sense. */
109         i = 1;
110         setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
111
112         if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
113                 lprintf(1, "Can't bind: %s\n", strerror(errno));
114                 exit(WC_EXIT_BIND);
115         }
116         if (listen(s, queue_len) < 0) {
117                 lprintf(1, "Can't listen: %s\n", strerror(errno));
118                 exit(WC_EXIT_BIND);
119         }
120         return (s);
121 }
122
123
124
125 /**
126  * \brief Create a Unix domain socket and listen on it
127  * \param sockpath file name of the unix domain socket
128  * \param queue_len Number of incoming connections to allow in the queue
129  */
130 int ig_uds_server(char *sockpath, int queue_len)
131 {
132         struct sockaddr_un addr;
133         int s;
134         int i;
135         int actual_queue_len;
136
137         actual_queue_len = queue_len;
138         if (actual_queue_len < 5) actual_queue_len = 5;
139
140         i = unlink(sockpath);
141         if (i != 0) if (errno != ENOENT) {
142                 lprintf(1, "citserver: can't unlink %s: %s\n",
143                         sockpath, strerror(errno));
144                 exit(WC_EXIT_BIND);
145         }
146
147         memset(&addr, 0, sizeof(addr));
148         addr.sun_family = AF_UNIX;
149         safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
150
151         s = socket(AF_UNIX, SOCK_STREAM, 0);
152         if (s < 0) {
153                 lprintf(1, "citserver: Can't create a socket: %s\n",
154                         strerror(errno));
155                 exit(WC_EXIT_BIND);
156         }
157
158         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
159                 lprintf(1, "citserver: Can't bind: %s\n",
160                         strerror(errno));
161                 exit(WC_EXIT_BIND);
162         }
163
164         if (listen(s, actual_queue_len) < 0) {
165                 lprintf(1, "citserver: Can't listen: %s\n",
166                         strerror(errno));
167                 exit(WC_EXIT_BIND);
168         }
169
170         chmod(sockpath, 0777);
171         return(s);
172 }
173
174
175
176
177 /**
178  * \brief Read data from the client socket.
179  * \param sock socket fd to read from
180  * \param buf buffer to read into 
181  * \param bytes number of bytes to read
182  * \param timeout Number of seconds to wait before timing out
183  * \return values are\
184  *      1       Requested number of bytes has been read.\
185  *      0       Request timed out.\
186  *         -1           Connection is broken, or other error.
187  */
188 int client_read_to(int sock, char *buf, int bytes, int timeout)
189 {
190         int len, rlen;
191         fd_set rfds;
192         struct timeval tv;
193         int retval;
194
195
196 #ifdef HAVE_OPENSSL
197         if (is_https) {
198                 return (client_read_ssl(buf, bytes, timeout));
199         }
200 #endif
201
202         len = 0;
203         while (len < bytes) {
204                 FD_ZERO(&rfds);
205                 FD_SET(sock, &rfds);
206                 tv.tv_sec = timeout;
207                 tv.tv_usec = 0;
208
209                 retval = select((sock) + 1, &rfds, NULL, NULL, &tv);
210                 if (FD_ISSET(sock, &rfds) == 0) {
211                         return (0);
212                 }
213
214                 rlen = read(sock, &buf[len], bytes - len);
215
216                 if (rlen < 1) {
217                         lprintf(2, "client_read() failed: %s\n",
218                                 strerror(errno));
219                         return (-1);
220                 }
221                 len = len + rlen;
222         }
223
224 #ifdef HTTP_TRACING
225         write(2, "\033[32m", 5);
226         write(2, buf, bytes);
227         write(2, "\033[30m", 5);
228 #endif
229         return (1);
230 }
231
232 /**
233  * \brief write data to the client
234  * \param buf data to write to the client
235  * \param count size of buffer
236  */
237 ssize_t client_write(const void *buf, size_t count)
238 {
239         char *newptr;
240         size_t newalloc;
241
242         if (WC->burst != NULL) {
243                 if ((WC->burst_len + count) >= WC->burst_alloc) {
244                         newalloc = (WC->burst_alloc * 2);
245                         if ((WC->burst_len + count) >= newalloc) {
246                                 newalloc += count;
247                         }
248                         newptr = realloc(WC->burst, newalloc);
249                         if (newptr != NULL) {
250                                 WC->burst = newptr;
251                                 WC->burst_alloc = newalloc;
252                         }
253                 }
254                 if ((WC->burst_len + count) < WC->burst_alloc) {
255                         memcpy(&WC->burst[WC->burst_len], buf, count);
256                         WC->burst_len += count;
257                         return (count);
258                 }
259                 else {
260                         return(-1);
261                 }
262         }
263 #ifdef HAVE_OPENSSL
264         if (is_https) {
265                 client_write_ssl((char *) buf, count);
266                 return (count);
267         }
268 #endif
269 #ifdef HTTP_TRACING
270         write(2, "\033[34m", 5);
271         write(2, buf, count);
272         write(2, "\033[30m", 5);
273 #endif
274         return (write(WC->http_sock, buf, count));
275 }
276
277 /**
278  * \brief Begin buffering HTTP output so we can transmit it all in one write operation later.
279  */
280 void begin_burst(void)
281 {
282         if (WC->burst != NULL) {
283                 free(WC->burst);
284                 WC->burst = NULL;
285         }
286         WC->burst_len = 0;
287         WC->burst_alloc = 32768;
288         WC->burst = malloc(WC->burst_alloc);
289 }
290
291
292 /**
293  * \brief uses the same calling syntax as compress2(), but it
294  * creates a stream compatible with HTTP "Content-encoding: gzip"
295  */
296 #ifdef HAVE_ZLIB
297 #define DEF_MEM_LEVEL 8 /**< memlevel??? */
298 #define OS_CODE 0x03    /**< unix */
299 int ZEXPORT compress_gzip(Bytef * dest,         /**< compressed buffer*/
300                           uLongf * destLen,     /**< length of the compresed data */
301                           const Bytef * source, /**< source to encode */
302                           uLong sourceLen,      /**< length of source to encode */
303                           int level)            /**< compression level */
304 {
305         const int gz_magic[2] = { 0x1f, 0x8b }; /** gzip magic header */
306
307         /** write gzip header */
308         sprintf((char *) dest, "%c%c%c%c%c%c%c%c%c%c",
309                 gz_magic[0], gz_magic[1], Z_DEFLATED,
310                 0 /*flags */ , 0, 0, 0, 0 /*time */ , 0 /** xflags */ ,
311                 OS_CODE);
312
313         /* normal deflate */
314         z_stream stream;
315         int err;
316         stream.next_in = (Bytef *) source;
317         stream.avail_in = (uInt) sourceLen;
318         stream.next_out = dest + 10L;   // after header
319         stream.avail_out = (uInt) * destLen;
320         if ((uLong) stream.avail_out != *destLen)
321                 return Z_BUF_ERROR;
322
323         stream.zalloc = (alloc_func) 0;
324         stream.zfree = (free_func) 0;
325         stream.opaque = (voidpf) 0;
326
327         err = deflateInit2(&stream, level, Z_DEFLATED, -MAX_WBITS,
328                            DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
329         if (err != Z_OK)
330                 return err;
331
332         err = deflate(&stream, Z_FINISH);
333         if (err != Z_STREAM_END) {
334                 deflateEnd(&stream);
335                 return err == Z_OK ? Z_BUF_ERROR : err;
336         }
337         *destLen = stream.total_out + 10L;
338
339         /* write CRC and Length */
340         uLong crc = crc32(0L, source, sourceLen);
341         int n;
342         for (n = 0; n < 4; ++n, ++*destLen) {
343                 dest[*destLen] = (int) (crc & 0xff);
344                 crc >>= 8;
345         }
346         uLong len = stream.total_in;
347         for (n = 0; n < 4; ++n, ++*destLen) {
348                 dest[*destLen] = (int) (len & 0xff);
349                 len >>= 8;
350         }
351         err = deflateEnd(&stream);
352         return err;
353 }
354 #endif
355
356 /**
357  * \brief Finish buffering HTTP output.  [Compress using zlib and] output with a Content-Length: header.
358  */
359 void end_burst(void)
360 {
361         size_t the_len;
362         char *the_data;
363
364         if (WC->burst == NULL)
365                 return;
366
367         the_len = WC->burst_len;
368         the_data = WC->burst;
369
370         WC->burst_len = 0;
371         WC->burst_alloc = 0;
372         WC->burst = NULL;
373
374 #ifdef HAVE_ZLIB
375         /* Perform gzip compression, if enabled and supported by client */
376         if (WC->gzip_ok) {
377                 char *compressed_data = NULL;
378                 uLongf compressed_len;
379
380                 compressed_len = (uLongf) ((the_len * 101) / 100) + 100;
381                 compressed_data = malloc(compressed_len);
382
383                 if (compress_gzip((Bytef *) compressed_data,
384                                   &compressed_len,
385                                   (Bytef *) the_data,
386                                   (uLongf) the_len, Z_BEST_SPEED) == Z_OK) {
387                         wprintf("Content-encoding: gzip\r\n");
388                         free(the_data);
389                         the_data = compressed_data;
390                         the_len = compressed_len;
391                 } else {
392                         free(compressed_data);
393                 }
394         }
395 #endif  /* HAVE_ZLIB */
396
397         wprintf("Content-length: %d\r\n\r\n", the_len);
398         client_write(the_data, the_len);
399         free(the_data);
400         return;
401 }
402
403
404
405 /**
406  * \brief Read data from the client socket with default timeout.
407  * (This is implemented in terms of client_read_to() and could be
408  * justifiably moved out of sysdep.c)
409  * \param sock the socket fd to read from
410  * \param buf the buffer to write to
411  * \param bytes Number of bytes to read
412  */
413 int client_read(int sock, char *buf, int bytes)
414 {
415         return (client_read_to(sock, buf, bytes, SLEEPING));
416 }
417
418
419 /**
420  * \brief Get a LF-terminated line of text from the client.
421  * (This is implemented in terms of client_read() and could be
422  * justifiably moved out of sysdep.c)
423  * \param sock socket fd to get client line from
424  * \param buf buffer to write read data to
425  * \param bufsiz how many bytes to read
426  * \return  number of bytes read???
427  */
428 int client_getln(int sock, char *buf, int bufsiz)
429 {
430         int i, retval;
431
432         /** Read one character at a time.*/
433         for (i = 0;; i++) {
434                 retval = client_read(sock, &buf[i], 1);
435                 if (retval != 1 || buf[i] == '\n' || i == (bufsiz-1))
436                         break;
437                 if ( (!isspace(buf[i])) && (!isprint(buf[i])) ) {
438                         /** Non printable character recieved from client */
439                         return(-1);
440                 }
441         }
442
443         /** If we got a long line, discard characters until the newline. */
444         if (i == (bufsiz-1))
445                 while (buf[i] != '\n' && retval == 1)
446                         retval = client_read(sock, &buf[i], 1);
447
448         /**
449          * Strip any trailing non-printable characters.
450          */
451         buf[i] = 0;
452         while ((i > 0) && (!isprint(buf[i - 1]))) {
453                 buf[--i] = 0;
454         }
455         return (retval);
456 }
457
458 /**
459  * \brief shut us down the regular way.
460  * param signum the signal we want to forward
461  */
462 pid_t current_child;
463 void graceful_shutdown_watcher(int signum) {
464         lprintf (1, "bye; shutting down watcher.");
465         kill(current_child, signum);
466         exit(0);
467 }
468
469 /**
470  * \brief shut us down the regular way.
471  * param signum the signal we want to forward
472  */
473 pid_t current_child;
474 void graceful_shutdown(int signum) {
475 //      kill(current_child, signum);
476         char wd[SIZ];
477         getcwd(wd, SIZ);
478         lprintf (1, "bye going down gracefull.[%s]\n", wd);
479         time_to_die = 1;
480         exit(0);
481 }
482
483
484 /**
485  * \brief       Start running as a daemon.  
486  *
487  * param        do_close_stdio          Only close stdio if set.
488  */
489
490 /*
491  * Start running as a daemon.
492  */
493 void start_daemon(char *pid_file) 
494 {
495         int status = 0;
496         pid_t child = 0;
497         FILE *fp;
498         int do_restart = 0;
499
500         current_child = 0;
501
502         /* Close stdin/stdout/stderr and replace them with /dev/null.
503          * We don't just call close() because we don't want these fd's
504          * to be reused for other files.
505          */
506         chdir("/");
507
508         signal(SIGHUP, SIG_IGN);
509         signal(SIGINT, SIG_IGN);
510         signal(SIGQUIT, SIG_IGN);
511
512         child = fork();
513         if (child != 0) {
514                 exit(0);
515         }
516
517         setsid();
518         umask(0);
519         freopen("/dev/null", "r", stdin);
520         freopen("/dev/null", "w", stdout);
521         freopen("/dev/null", "w", stderr);
522         signal(SIGTERM, graceful_shutdown_watcher);
523
524         do {
525                 current_child = fork();
526
527         
528                 if (current_child < 0) {
529                         perror("fork");
530                         exit(errno);
531                 }
532         
533                 else if (current_child == 0) {
534                         signal(SIGTERM, graceful_shutdown);
535                         return; /* continue starting webcit. */
536                 }
537         
538                 else {
539                         signal(SIGTERM, SIG_IGN);
540                         if (pid_file) {
541                                 fp = fopen(pid_file, "w");
542                                 if (fp != NULL) {
543                                         fprintf(fp, "%d\n", current_child);
544                                         fclose(fp);
545                                 }
546                         }
547                         waitpid(current_child, &status, 0);
548                 }
549
550                 do_restart = 0;
551
552                 /* Did the main process exit with an actual exit code? */
553                 if (WIFEXITED(status)) {
554
555                         /* Exit code 0 means the watcher should exit */
556                         if (WEXITSTATUS(status) == 0) {
557                                 do_restart = 0;
558                         }
559
560                         /* Exit code 101-109 means the watcher should exit */
561                         else if ( (WEXITSTATUS(status) >= 101) && (WEXITSTATUS(status) <= 109) ) {
562                                 do_restart = 0;
563                         }
564
565                         /* Any other exit code means we should restart. */
566                         else {
567                                 do_restart = 1;
568                         }
569                 }
570
571                 /* Any other type of termination (signals, etc.) should also restart. */
572                 else {
573                         do_restart = 1;
574                 }
575
576         } while (do_restart);
577
578         if (pid_file) {
579                 unlink(pid_file);
580         }
581         exit(WEXITSTATUS(status));
582 }
583
584 /**
585  * \brief       Spawn an additional worker thread into the pool.
586  */
587 void spawn_another_worker_thread()
588 {
589         pthread_t SessThread;   /**< Thread descriptor */
590         pthread_attr_t attr;    /**< Thread attributes */
591         int ret;
592
593         lprintf(3, "Creating a new thread\n");
594
595         /** set attributes for the new thread */
596         pthread_attr_init(&attr);
597         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
598
599         /**
600          * Our per-thread stacks need to be bigger than the default size, otherwise
601          * the MIME parser crashes on FreeBSD, and the IMAP service crashes on
602          * 64-bit Linux.
603          */
604         if ((ret = pthread_attr_setstacksize(&attr, 1024 * 1024))) {
605                 lprintf(1, "pthread_attr_setstacksize: %s\n",
606                         strerror(ret));
607                 pthread_attr_destroy(&attr);
608         }
609
610         /** now create the thread */
611         if (pthread_create(&SessThread, &attr,
612                            (void *(*)(void *)) worker_entry, NULL)
613             != 0) {
614                 lprintf(1, "Can't create thread: %s\n", strerror(errno));
615         }
616
617         /** free up the attributes */
618         pthread_attr_destroy(&attr);
619 }
620
621 /**
622  * \brief Here's where it all begins.
623  * \param argc number of commandline args
624  * \param argv the commandline arguments
625  */
626 int main(int argc, char **argv)
627 {
628         pthread_t SessThread;   /**< Thread descriptor */
629         pthread_attr_t attr;    /**< Thread attributes */
630         int a, i;                       /**< General-purpose variables */
631         char tracefile[PATH_MAX];
632         char ip_addr[256]="0.0.0.0";
633         char dirbuffer[PATH_MAX]="";
634         int relh=0;
635         int home=0;
636         int home_specified=0;
637         char relhome[PATH_MAX]="";
638         char webcitdir[PATH_MAX] = DATADIR;
639         char *pidfile = NULL;
640         char *hdir;
641         const char *basedir;
642 #ifdef ENABLE_NLS
643         char *locale = NULL;
644         char *mo = NULL;
645 #endif /* ENABLE_NLS */
646         char uds_listen_path[PATH_MAX]; /**< listen on a unix domain socket? */
647
648         strcpy(uds_listen_path, "");
649
650         /** Parse command line */
651 #ifdef HAVE_OPENSSL
652         while ((a = getopt(argc, argv, "h:i:p:t:x:dD:cfs")) != EOF)
653 #else
654         while ((a = getopt(argc, argv, "h:i:p:t:x:dD:cf")) != EOF)
655 #endif
656                 switch (a) {
657                 case 'h':
658                         hdir = strdup(optarg);
659                         relh=hdir[0]!='/';
660                         if (!relh) safestrncpy(webcitdir, hdir,
661                                                                    sizeof webcitdir);
662                         else
663                                 safestrncpy(relhome, relhome,
664                                                         sizeof relhome);
665                         /* free(hdir); TODO: SHOULD WE DO THIS? */
666                         home_specified = 1;
667                         home=1;
668                         break;
669                 case 'd':
670                         running_as_daemon = 1;
671                         break;
672                 case 'D':
673                         pidfile = strdup(optarg);
674                         running_as_daemon = 1;
675                         break;
676                 case 'i':
677                         safestrncpy(ip_addr, optarg, sizeof ip_addr);
678                         break;
679                 case 'p':
680                         http_port = atoi(optarg);
681                         if (http_port == 0) {
682                                 safestrncpy(uds_listen_path, optarg, sizeof uds_listen_path);
683                         }
684                         break;
685                 case 't':
686                         safestrncpy(tracefile, optarg, sizeof tracefile);
687                         freopen(tracefile, "w", stdout);
688                         freopen(tracefile, "w", stderr);
689                         freopen(tracefile, "r", stdin);
690                         break;
691                 case 'x':
692                         verbosity = atoi(optarg);
693                         break;
694                 case 'f':
695                         follow_xff = 1;
696                         break;
697                 case 'c':
698                         server_cookie = malloc(256);
699                         if (server_cookie != NULL) {
700                                 safestrncpy(server_cookie,
701                                        "Set-cookie: wcserver=",
702                                         256);
703                                 if (gethostname
704                                     (&server_cookie[strlen(server_cookie)],
705                                      200) != 0) {
706                                         lprintf(2, "gethostname: %s\n",
707                                                 strerror(errno));
708                                         free(server_cookie);
709                                 }
710                         }
711                         break;
712                 case 's':
713                         is_https = 1;
714                         break;
715                 default:
716                         fprintf(stderr, "usage: webserver "
717                                 "[-i ip_addr] [-p http_port] "
718                                 "[-t tracefile] [-c] [-f] "
719                                 "[-d] "
720 #ifdef HAVE_OPENSSL
721                                 "[-s] "
722 #endif
723                                 "[remotehost [remoteport]]\n");
724                         return 1;
725                 }
726
727         if (optind < argc) {
728                 ctdlhost = argv[optind];
729                 if (++optind < argc)
730                         ctdlport = argv[optind];
731         }
732
733         /* daemonize, if we were asked to */
734         if (running_as_daemon) {
735                 start_daemon(pidfile);
736         }
737         else {
738                 signal(SIGTERM, graceful_shutdown);
739         }
740
741         /** Tell 'em who's in da house */
742         lprintf(1, PACKAGE_STRING "\n");
743         lprintf(1, "Copyright (C) 1996-2007 by the Citadel development team.\n"
744                 "This software is distributed under the terms of the "
745                 "GNU General Public License.\n\n"
746         );
747
748
749         /** initialize the International Bright Young Thing */
750 #ifdef ENABLE_NLS
751         initialize_locales();
752         locale = setlocale(LC_ALL, "");
753         mo = malloc(strlen(webcitdir) + 20);
754         lprintf(9, "Message catalog directory: %s\n", bindtextdomain("webcit", LOCALEDIR"/locale"));
755         free(mo);
756         lprintf(9, "Text domain: %s\n", textdomain("webcit"));
757         lprintf(9, "Text domain Charset: %s\n", bind_textdomain_codeset("webcit","UTF8"));
758 #endif
759
760
761         /* calculate all our path on a central place */
762     /* where to keep our config */
763         
764 #define COMPUTE_DIRECTORY(SUBDIR) memcpy(dirbuffer,SUBDIR, sizeof dirbuffer);\
765         snprintf(SUBDIR,sizeof SUBDIR,  "%s%s%s%s%s%s%s", \
766                          (home&!relh)?webcitdir:basedir, \
767              ((basedir!=webcitdir)&(home&!relh))?basedir:"/", \
768              ((basedir!=webcitdir)&(home&!relh))?"/":"", \
769                          relhome, \
770              (relhome[0]!='\0')?"/":"",\
771                          dirbuffer,\
772                          (dirbuffer[0]!='\0')?"/":"");
773         basedir=RUNDIR;
774         COMPUTE_DIRECTORY(socket_dir);
775         basedir=WWWDIR "/static";
776         COMPUTE_DIRECTORY(static_dir);
777         basedir=WWWDIR "/static.local";
778         COMPUTE_DIRECTORY(static_local_dir);
779
780         snprintf(file_crpt_file_key,
781                  sizeof file_crpt_file_key, 
782                  "%s/citadel.key",
783                  ctdl_key_dir);
784         snprintf(file_crpt_file_csr,
785                  sizeof file_crpt_file_csr, 
786                  "%s/citadel.csr",
787                  ctdl_key_dir);
788         snprintf(file_crpt_file_cer,
789                  sizeof file_crpt_file_cer, 
790                  "%s/citadel.cer",
791                  ctdl_key_dir);
792
793         /** we should go somewhere we can leave our coredump, if enabled... */
794         lprintf(9, "Changing directory to %s\n", socket_dir);
795         if (chdir(webcitdir) != 0) {
796                 perror("chdir");
797         }
798         initialize_viewdefs();
799         initialize_axdefs();
800
801         /**
802          * Set up a place to put thread-specific data.
803          * We only need a single pointer per thread - it points to the
804          * wcsession struct to which the thread is currently bound.
805          */
806         if (pthread_key_create(&MyConKey, NULL) != 0) {
807                 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
808         }
809
810         /**
811          * Set up a place to put thread-specific SSL data.
812          * We don't stick this in the wcsession struct because SSL starts
813          * up before the session is bound, and it gets torn down between
814          * transactions.
815          */
816 #ifdef HAVE_OPENSSL
817         if (pthread_key_create(&ThreadSSL, NULL) != 0) {
818                 lprintf(1, "Can't create TSD key: %s\n", strerror(errno));
819         }
820 #endif
821
822         /**
823          * Bind the server to our favorite port.
824          * There is no need to check for errors, because ig_tcp_server()
825          * exits if it doesn't succeed.
826          */
827
828         if (!IsEmptyStr(uds_listen_path)) {
829                 lprintf(2, "Attempting to create listener socket at %s...\n", uds_listen_path);
830                 msock = ig_uds_server(uds_listen_path, LISTEN_QUEUE_LENGTH);
831         }
832         else {
833                 lprintf(2, "Attempting to bind to port %d...\n", http_port);
834                 msock = ig_tcp_server(ip_addr, http_port, LISTEN_QUEUE_LENGTH);
835         }
836
837         lprintf(2, "Listening on socket %d\n", msock);
838         signal(SIGPIPE, SIG_IGN);
839
840         pthread_mutex_init(&SessionListMutex, NULL);
841
842         /**
843          * Start up the housekeeping thread
844          */
845         pthread_attr_init(&attr);
846         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
847         pthread_create(&SessThread, &attr,
848                        (void *(*)(void *)) housekeeping_loop, NULL);
849
850
851         /**
852          * If this is an HTTPS server, fire up SSL
853          */
854 #ifdef HAVE_OPENSSL
855         if (is_https) {
856                 init_ssl();
857         }
858 #endif
859
860         /** Start a few initial worker threads */
861         for (i = 0; i < (MIN_WORKER_THREADS); ++i) {
862                 spawn_another_worker_thread();
863         }
864
865         /* now the original thread becomes another worker */
866         worker_entry();
867         return 0;
868 }
869
870
871 /**
872  * Entry point for worker threads
873  */
874 void worker_entry(void)
875 {
876         int ssock;
877         int i = 0;
878         int fail_this_transaction = 0;
879
880         do {
881                 /** Only one thread can accept at a time */
882                 fail_this_transaction = 0;
883                 ssock = accept(msock, NULL, 0);
884                 if (ssock < 0) {
885                         lprintf(2, "accept() failed: %s\n",
886                                 strerror(errno));
887                 } else {
888                         /** Set the SO_REUSEADDR socket option */
889                         i = 1;
890                         setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR,
891                                    &i, sizeof(i));
892
893                         /** If we are an HTTPS server, go crypto now. */
894 #ifdef HAVE_OPENSSL
895                         if (is_https) {
896                                 if (starttls(ssock) != 0) {
897                                         fail_this_transaction = 1;
898                                         close(ssock);
899                                 }
900                         }
901 #endif
902
903                         if (fail_this_transaction == 0) {
904
905                                 /** Perform an HTTP transaction... */
906                                 context_loop(ssock);
907
908                                 /** Shut down SSL/TLS if required... */
909 #ifdef HAVE_OPENSSL
910                                 if (is_https) {
911                                         endtls();
912                                 }
913 #endif
914
915                                 /** ...and close the socket. */
916                                 lingering_close(ssock);
917                         }
918
919                 }
920
921         } while (!time_to_die);
922
923         lprintf (1, "bye");
924         pthread_exit(NULL);
925 }
926
927 /**
928  * \brief logprintf. log messages 
929  * logs to stderr if loglevel is lower than the verbosity set at startup
930  * \param loglevel level of the message
931  * \param format the printf like format string
932  * \param ... the strings to put into format
933  */
934 int lprintf(int loglevel, const char *format, ...)
935 {
936         va_list ap;
937
938         if (loglevel <= verbosity) {
939                 va_start(ap, format);
940                 vfprintf(stderr, format, ap);
941                 va_end(ap);
942                 fflush(stderr);
943         }
944         return 1;
945 }
946
947
948 /**
949  * \brief print the actual stack frame.
950  */
951 void wc_backtrace(void)
952 {
953 #ifdef HAVE_BACKTRACE
954         void *stack_frames[50];
955         size_t size, i;
956         char **strings;
957
958
959         size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
960         strings = backtrace_symbols(stack_frames, size);
961         for (i = 0; i < size; i++) {
962                 if (strings != NULL)
963                         lprintf(1, "%s\n", strings[i]);
964                 else
965                         lprintf(1, "%p\n", stack_frames[i]);
966         }
967         free(strings);
968 #endif
969 }
970
971 /*@}*/