* fix endless loop; respect kill_me
[citadel.git] / citadel / sysdep.c
1 /*
2  * $Id$
3  *
4  * Citadel "system dependent" stuff.
5  * See COPYING for copyright information.
6  *
7  * Here's where we (hopefully) have most parts of the Citadel server that
8  * would need to be altered to run the server in a non-POSIX environment.
9  * 
10  * If we ever port to a different platform and either have multiple
11  * variants of this file or simply load it up with #ifdefs.
12  *
13  */
14
15 #include "sysdep.h"
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <stdio.h>
19 #include <fcntl.h>
20 #include <ctype.h>
21 #include <signal.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <sys/wait.h>
25 #include <sys/socket.h>
26 #include <syslog.h>
27 #include <sys/syslog.h>
28
29 #if TIME_WITH_SYS_TIME
30 # include <sys/time.h>
31 # include <time.h>
32 #else
33 # if HAVE_SYS_TIME_H
34 #  include <sys/time.h>
35 # else
36 #  include <time.h>
37 # endif
38 #endif
39
40 #include <limits.h>
41 #include <sys/resource.h>
42 #include <netinet/in.h>
43 #include <netinet/tcp.h>
44 #include <arpa/inet.h>
45 #include <netdb.h>
46 #include <sys/un.h>
47 #include <string.h>
48 #include <pwd.h>
49 #include <errno.h>
50 #include <stdarg.h>
51 #include <grp.h>
52 #include <libcitadel.h>
53 #include "citadel.h"
54 #include "server.h"
55 #include "sysdep_decls.h"
56 #include "citserver.h"
57 #include "support.h"
58 #include "config.h"
59 #include "database.h"
60 #include "housekeeping.h"
61 #include "modules/crypto/serv_crypto.h" /* Needed for init_ssl, client_write_ssl, client_read_ssl, destruct_ssl */
62 #include "ecrash.h"
63 #include "context.h"
64
65 #ifdef HAVE_SYS_SELECT_H
66 #include <sys/select.h>
67 #endif
68
69 #ifndef HAVE_SNPRINTF
70 #include "snprintf.h"
71 #endif
72
73 #include "ctdl_module.h"
74 #include "threads.h"
75 #include "user_ops.h"
76 #include "control.h"
77
78
79 #ifdef DEBUG_MEMORY_LEAKS
80 struct igheap {
81         struct igheap *next;
82         char file[32];
83         int line;
84         void *block;
85 };
86
87 struct igheap *igheap = NULL;
88 #endif
89
90
91 int verbosity = DEFAULT_VERBOSITY;              /* Logging level */
92
93 int syslog_facility = LOG_DAEMON;
94 int enable_syslog = 0;
95
96
97 /*
98  * CtdlLogPrintf()  ...   Write logging information
99  */
100 void CtdlLogPrintf(enum LogLevel loglevel, const char *format, ...) {   
101         va_list arg_ptr;
102         va_start(arg_ptr, format);
103         vCtdlLogPrintf(loglevel, format, arg_ptr);
104         va_end(arg_ptr);
105 }
106
107 void vCtdlLogPrintf(enum LogLevel loglevel, const char *format, va_list arg_ptr)
108 {
109         char buf[SIZ], buf2[SIZ];
110
111         if (enable_syslog) {
112                 vsyslog((syslog_facility | loglevel), format, arg_ptr);
113         }
114
115         /* stderr output code */
116         if (enable_syslog || running_as_daemon) return;
117
118         /* if we run in forground and syslog is disabled, log to terminal */
119         if (loglevel <= verbosity) { 
120                 struct timeval tv;
121                 struct tm tim;
122                 time_t unixtime;
123                 CitContext *CCC = CC;
124
125                 gettimeofday(&tv, NULL);
126                 /* Promote to time_t; types differ on some OSes (like darwin) */
127                 unixtime = tv.tv_sec;
128                 localtime_r(&unixtime, &tim);
129                 if ((CCC != NULL) && (CCC->cs_pid != 0)) {
130                         sprintf(buf,
131                                 "%04d/%02d/%02d %2d:%02d:%02d.%06ld [%3d] ",
132                                 tim.tm_year + 1900, tim.tm_mon + 1,
133                                 tim.tm_mday, tim.tm_hour, tim.tm_min,
134                                 tim.tm_sec, (long)tv.tv_usec,
135                                 CCC->cs_pid);
136                 } else {
137                         sprintf(buf,
138                                 "%04d/%02d/%02d %2d:%02d:%02d.%06ld ",
139                                 tim.tm_year + 1900, tim.tm_mon + 1,
140                                 tim.tm_mday, tim.tm_hour, tim.tm_min,
141                                 tim.tm_sec, (long)tv.tv_usec);
142                 }
143                 vsnprintf(buf2, SIZ, format, arg_ptr);   
144
145                 fprintf(stderr, "%s%s", buf, buf2);
146                 fflush(stderr);
147         }
148 }   
149
150
151
152 /*
153  * Signal handler to shut down the server.
154  */
155
156 volatile int exit_signal = 0;
157 volatile int shutdown_and_halt = 0;
158 volatile int restart_server = 0;
159 volatile int running_as_daemon = 0;
160
161 static RETSIGTYPE signal_cleanup(int signum) {
162
163         if (CT)
164                 CT->signal = signum;
165         else
166         {
167                 CtdlLogPrintf(CTDL_DEBUG, "Caught signal %d; shutting down.\n", signum);
168                 exit_signal = signum;
169         }
170 }
171
172
173
174 /*
175  * Some initialization stuff...
176  */
177 void init_sysdep(void) {
178         sigset_t set;
179
180         /* Avoid vulnerabilities related to FD_SETSIZE if we can. */
181 #ifdef FD_SETSIZE
182 #ifdef RLIMIT_NOFILE
183         struct rlimit rl;
184         getrlimit(RLIMIT_NOFILE, &rl);
185         rl.rlim_cur = FD_SETSIZE;
186         rl.rlim_max = FD_SETSIZE;
187         setrlimit(RLIMIT_NOFILE, &rl);
188 #endif
189 #endif
190
191         /* If we've got OpenSSL, we're going to use it. */
192 #ifdef HAVE_OPENSSL
193         init_ssl();
194 #endif
195
196         /*
197          * Set up a place to put thread-specific data.
198          * We only need a single pointer per thread - it points to the
199          * CitContext structure (in the ContextList linked list) of the
200          * session to which the calling thread is currently bound.
201          */
202         if (citthread_key_create(&MyConKey, NULL) != 0) {
203                 CtdlLogPrintf(CTDL_CRIT, "Can't create TSD key: %s\n",
204                         strerror(errno));
205         }
206
207         /*
208          * The action for unexpected signals and exceptions should be to
209          * call signal_cleanup() to gracefully shut down the server.
210          */
211         sigemptyset(&set);
212         sigaddset(&set, SIGINT);
213         sigaddset(&set, SIGQUIT);
214         sigaddset(&set, SIGHUP);
215         sigaddset(&set, SIGTERM);
216         // sigaddset(&set, SIGSEGV);    commented out because
217         // sigaddset(&set, SIGILL);     we want core dumps
218         // sigaddset(&set, SIGBUS);
219         sigprocmask(SIG_UNBLOCK, &set, NULL);
220
221         signal(SIGINT, signal_cleanup);
222         signal(SIGQUIT, signal_cleanup);
223         signal(SIGHUP, signal_cleanup);
224         signal(SIGTERM, signal_cleanup);
225         // signal(SIGSEGV, signal_cleanup);     commented out because
226         // signal(SIGILL, signal_cleanup);      we want core dumps
227         // signal(SIGBUS, signal_cleanup);
228
229         /*
230          * Do not shut down the server on broken pipe signals, otherwise the
231          * whole Citadel service would come down whenever a single client
232          * socket breaks.
233          */
234         signal(SIGPIPE, SIG_IGN);
235 }
236
237
238
239
240 /*
241  * This is a generic function to set up a master socket for listening on
242  * a TCP port.  The server shuts down if the bind fails.
243  *
244  */
245 int ig_tcp_server(char *ip_addr, int port_number, int queue_len, char **errormessage)
246 {
247         struct sockaddr_in sin;
248         int s, i;
249         int actual_queue_len;
250
251         actual_queue_len = queue_len;
252         if (actual_queue_len < 5) actual_queue_len = 5;
253
254         memset(&sin, 0, sizeof(sin));
255         sin.sin_family = AF_INET;
256         sin.sin_port = htons((u_short)port_number);
257         if (ip_addr == NULL) {
258                 sin.sin_addr.s_addr = INADDR_ANY;
259         }
260         else {
261                 sin.sin_addr.s_addr = inet_addr(ip_addr);
262         }
263                                                                                 
264         if (sin.sin_addr.s_addr == !INADDR_ANY) {
265                 sin.sin_addr.s_addr = INADDR_ANY;
266         }
267
268         s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
269
270         if (s < 0) {
271                 *errormessage = (char*) malloc(SIZ + 1);
272                 snprintf(*errormessage, SIZ, 
273                                  "citserver: Can't create a socket: %s",
274                                  strerror(errno));
275                 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
276                 return(-1);
277         }
278
279         i = 1;
280         setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
281
282         if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
283                 *errormessage = (char*) malloc(SIZ + 1);
284                 snprintf(*errormessage, SIZ, 
285                                  "citserver: Can't bind: %s",
286                                  strerror(errno));
287                 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
288                 close(s);
289                 return(-1);
290         }
291
292         /* set to nonblock - we need this for some obscure situations */
293         if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
294                 *errormessage = (char*) malloc(SIZ + 1);
295                 snprintf(*errormessage, SIZ, 
296                                  "citserver: Can't set socket to non-blocking: %s",
297                                  strerror(errno));
298                 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
299                 close(s);
300                 return(-1);
301         }
302
303         if (listen(s, actual_queue_len) < 0) {
304                 *errormessage = (char*) malloc(SIZ + 1);
305                 snprintf(*errormessage, SIZ, 
306                                  "citserver: Can't listen: %s",
307                                  strerror(errno));
308                 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
309                 close(s);
310                 return(-1);
311         }
312
313         return(s);
314 }
315
316
317
318 /*
319  * Create a Unix domain socket and listen on it
320  */
321 int ig_uds_server(char *sockpath, int queue_len, char **errormessage)
322 {
323         struct sockaddr_un addr;
324         int s;
325         int i;
326         int actual_queue_len;
327 #ifdef HAVE_STRUCT_UCRED
328         int passcred = 1;
329 #endif
330
331         actual_queue_len = queue_len;
332         if (actual_queue_len < 5) actual_queue_len = 5;
333
334         i = unlink(sockpath);
335         if ((i != 0) && (errno != ENOENT)) {
336                 *errormessage = (char*) malloc(SIZ + 1);
337                 snprintf(*errormessage, SIZ, "citserver: can't unlink %s: %s",
338                         sockpath, strerror(errno));
339                 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
340                 return(-1);
341         }
342
343         memset(&addr, 0, sizeof(addr));
344         addr.sun_family = AF_UNIX;
345         safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
346
347         s = socket(AF_UNIX, SOCK_STREAM, 0);
348         if (s < 0) {
349                 *errormessage = (char*) malloc(SIZ + 1);
350                 snprintf(*errormessage, SIZ, 
351                          "citserver: Can't create a socket: %s",
352                          strerror(errno));
353                 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
354                 return(-1);
355         }
356
357         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
358                 *errormessage = (char*) malloc(SIZ + 1);
359                 snprintf(*errormessage, SIZ, 
360                          "citserver: Can't bind: %s",
361                          strerror(errno));
362                 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
363                 return(-1);
364         }
365
366         /* set to nonblock - we need this for some obscure situations */
367         if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
368                 *errormessage = (char*) malloc(SIZ + 1);
369                 snprintf(*errormessage, SIZ, 
370                          "citserver: Can't set socket to non-blocking: %s",
371                          strerror(errno));
372                 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
373                 close(s);
374                 return(-1);
375         }
376
377         if (listen(s, actual_queue_len) < 0) {
378                 *errormessage = (char*) malloc(SIZ + 1);
379                 snprintf(*errormessage, SIZ, 
380                          "citserver: Can't listen: %s",
381                          strerror(errno));
382                 CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
383                 return(-1);
384         }
385
386 #ifdef HAVE_STRUCT_UCRED
387         setsockopt(s, SOL_SOCKET, SO_PASSCRED, &passcred, sizeof(passcred));
388 #endif
389
390         chmod(sockpath, S_ISGID|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH);
391         return(s);
392 }
393
394
395
396 /*
397  * The following functions implement output buffering on operating systems which
398  * support it (such as Linux and various BSD flavors).
399  */
400 #ifndef HAVE_DARWIN
401 #ifdef TCP_CORK
402 #       define HAVE_TCP_BUFFERING
403 #else
404 #       ifdef TCP_NOPUSH
405 #               define HAVE_TCP_BUFFERING
406 #               define TCP_CORK TCP_NOPUSH
407 #       endif
408 #endif /* TCP_CORK */
409 #endif /* HAVE_DARWIN */
410
411 static unsigned on = 1, off = 0;
412
413 void buffer_output(void) {
414 #ifdef HAVE_TCP_BUFFERING
415 #ifdef HAVE_OPENSSL
416         if (!CC->redirect_ssl)
417 #endif
418                 setsockopt(CC->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
419 #endif
420 }
421
422 void unbuffer_output(void) {
423 #ifdef HAVE_TCP_BUFFERING
424 #ifdef HAVE_OPENSSL
425         if (!CC->redirect_ssl)
426 #endif
427                 setsockopt(CC->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
428 #endif
429 }
430
431 void flush_output(void) {
432 #ifdef HAVE_TCP_BUFFERING
433         struct CitContext *CCC = CC;
434         setsockopt(CCC->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
435         setsockopt(CCC->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
436 #endif
437 }
438
439 static void flush_client_inbuf(void)
440 {
441         CitContext *CCC=CC;
442
443         FlushStrBuf(CCC->ReadBuf);
444         CCC->Pos = NULL;
445
446 }
447
448 /*
449  * client_write()   ...    Send binary data to the client.
450  */
451 int client_write(const char *buf, int nbytes)
452 {
453         int bytes_written = 0;
454         int retval;
455 #ifndef HAVE_TCP_BUFFERING
456         int old_buffer_len = 0;
457 #endif
458         fd_set wset;
459         CitContext *Ctx;
460         int fdflags;
461
462 //      flush_client_inbuf();
463         Ctx = CC;
464         if (Ctx->redirect_buffer != NULL) {
465                 if ((Ctx->redirect_len + nbytes + 2) >= Ctx->redirect_alloc) {
466                         Ctx->redirect_alloc = (Ctx->redirect_alloc * 2) + nbytes;
467                         Ctx->redirect_buffer = realloc(Ctx->redirect_buffer,
468                                                 Ctx->redirect_alloc);
469                 }
470                 memcpy(&Ctx->redirect_buffer[Ctx->redirect_len], buf, nbytes);
471                 Ctx->redirect_len += nbytes;
472                 Ctx->redirect_buffer[Ctx->redirect_len] = 0;
473                 return 0;
474         }
475
476 #ifdef HAVE_OPENSSL
477         if (Ctx->redirect_ssl) {
478                 client_write_ssl(buf, nbytes);
479                 return 0;
480         }
481 #endif
482         if (Ctx->client_socket == -1) return -1;
483
484         fdflags = fcntl(Ctx->client_socket, F_GETFL);
485
486         while ((bytes_written < nbytes) && (Ctx->client_socket != -1)){
487                 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
488                         FD_ZERO(&wset);
489                         FD_SET(Ctx->client_socket, &wset);
490                         if (select(1, NULL, &wset, NULL, NULL) == -1) {
491                                 if (errno == EINTR)
492                                 {
493                                         CtdlLogPrintf(CTDL_DEBUG, "client_write(%d bytes) select() interrupted.\n", nbytes-bytes_written);
494                                         if (CtdlThreadCheckStop()) {
495                                                 CC->kill_me = 1;
496                                                 return (-1);
497                                         } else {
498                                                 /* can't trust fd's and stuff so we need to re-create them */
499                                                 continue;
500                                         }
501                                 } else {
502                                         CtdlLogPrintf(CTDL_ERR,
503                                                 "client_write(%d bytes) select failed: %s (%d)\n",
504                                                 nbytes - bytes_written,
505                                                 strerror(errno), errno);
506                                         cit_backtrace();
507                                         Ctx->kill_me = 1;
508                                         return -1;
509                                 }
510                         }
511                 }
512
513                 retval = write(Ctx->client_socket, &buf[bytes_written],
514                         nbytes - bytes_written);
515                 if (retval < 1) {
516                         CtdlLogPrintf(CTDL_ERR,
517                                 "client_write(%d bytes) failed: %s (%d)\n",
518                                 nbytes - bytes_written,
519                                 strerror(errno), errno);
520                         cit_backtrace();
521                         // CtdlLogPrintf(CTDL_DEBUG, "Tried to send: %s",  &buf[bytes_written]);
522                         Ctx->kill_me = 1;
523                         return -1;
524                 }
525                 bytes_written = bytes_written + retval;
526         }
527         return 0;
528 }
529
530 void cputbuf(const StrBuf *Buf) {   
531         client_write(ChrPtr(Buf), StrLength(Buf)); 
532 }   
533
534
535 /*
536  * cprintf()    Send formatted printable data to the client.
537  *              Implemented in terms of client_write() so it's technically not sysdep...
538  */
539 void cprintf(const char *format, ...) {   
540         va_list arg_ptr;   
541         char buf[1024];
542    
543         va_start(arg_ptr, format);   
544         if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
545                 buf[sizeof buf - 2] = '\n';
546         client_write(buf, strlen(buf)); 
547         va_end(arg_ptr);
548 }   
549
550
551 /*
552  * Read data from the client socket.
553  *
554  * sock         socket fd to read from
555  * buf          buffer to read into 
556  * bytes        number of bytes to read
557  * timeout      Number of seconds to wait before timing out
558  *
559  * Possible return values:
560  *      1       Requested number of bytes has been read.
561  *      0       Request timed out.
562  *      -1      Connection is broken, or other error.
563  */
564 int client_read_blob(StrBuf *Target, int bytes, int timeout)
565 {
566         CitContext *CCC=CC;
567         const char *Error;
568         int retval = 0;
569
570 #ifdef HAVE_OPENSSL
571         if (CCC->redirect_ssl) {
572                 retval = client_read_sslblob(Target, bytes, timeout);
573         }
574         else 
575 #endif
576
577                 retval = StrBufReadBLOBBuffered(Target, 
578                                                 CCC->ReadBuf,
579                                                 &CCC->Pos,
580                                                 &CCC->client_socket,
581                                                 1, 
582                                                 bytes,
583                                                 O_TERM,
584                                                 &Error);
585         if (retval < 0) {
586                 CtdlLogPrintf(CTDL_CRIT, 
587                               "%s failed: %s\n",
588                               __FUNCTION__,
589                               Error);
590         }
591         else
592         {
593                 char fn [SIZ];
594                 FILE *fd;
595
596                 snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
597
598                 fd = fopen(fn, "a+");
599                 fprintf(fd, "Read: BufSize: %d BufContent: [",
600                         StrLength(Target));
601                 fwrite(ChrPtr(Target), StrLength(Target), 1, fd);
602                 fprintf(fd, "]\n");
603                 
604                         
605                 fclose(fd);
606
607         }
608         return retval == bytes;
609 }
610
611 int client_read_to(char *buf, int bytes, int timeout)
612 {
613         CitContext *CCC=CC;
614         int rc;
615
616         rc = client_read_blob(CCC->MigrateBuf, bytes, timeout);
617         if (rc < 0)
618         {
619                 *buf = '\0';
620                 return rc;
621         }
622         else
623         {
624                 memcpy(buf, 
625                        ChrPtr(CCC->MigrateBuf),
626                        StrLength(CCC->MigrateBuf) + 1);
627                 FlushStrBuf(CCC->MigrateBuf);
628                 return rc;
629         }
630 }
631
632
633 int HaveMoreLinesWaiting(CitContext *CCC)
634 {
635         if ((CCC->kill_me == 1) &&
636             (CCC->Pos == NULL) && 
637             (StrLength(CCC->ReadBuf) == 0) && 
638             (CCC->client_socket != -1))
639                 return 0;
640         else
641                 return 1;
642 }
643
644
645 /*
646  * Read data from the client socket with default timeout.
647  * (This is implemented in terms of client_read_to() and could be
648  * justifiably moved out of sysdep.c)
649  */
650 INLINE int client_read(char *buf, int bytes)
651 {
652         return(client_read_to(buf, bytes, config.c_sleeping));
653 }
654
655 int CtdlClientGetLine(StrBuf *Target)
656 {
657         CitContext *CCC=CC;
658         const char *Error;
659         int rc;
660
661         FlushStrBuf(Target);
662 #ifdef HAVE_OPENSSL
663         if (CCC->redirect_ssl) {
664                 return client_readline_sslbuffer(Target,
665                                                  CCC->ReadBuf,
666                                                  1);
667         }
668         else 
669 #endif
670         {
671                 char fn [SIZ];
672                 FILE *fd;
673                 int len, rlen, nlen, nrlen;
674                 const char *pch;
675
676                 snprintf(fn, SIZ, "/tmp/foolog_%s.%d", CCC->ServiceName, CCC->cs_pid);
677
678                 fd = fopen(fn, "a+");
679                 pch = ChrPtr(CCC->ReadBuf);
680                 len = StrLength(CCC->ReadBuf);
681                 if (CCC->Pos != NULL)
682                         rlen = CC->Pos - pch;
683                 else
684                         rlen = 0;
685
686 /*              fprintf(fd, "\n\n\nBufSize: %d BufPos: %d \nBufContent: [%s]\n\n_____________________\n",
687                         len, rlen, pch);
688 */
689                 fprintf(fd, "\n\n\nBufSize: %d BufPos: %d \n_____________________\n",
690                         len, rlen);
691                 rc = StrBufTCP_read_buffered_line_fast(Target, 
692                                                        CCC->ReadBuf,
693                                                        &CCC->Pos,
694                                                        &CCC->client_socket,
695                                                        5,
696                                                        1,
697                                                        &Error);
698
699                 pch = ChrPtr(CCC->ReadBuf);
700                 nlen = StrLength(CCC->ReadBuf);
701                 if (CCC->Pos != NULL)
702                         nrlen = CC->Pos - pch;
703                 else
704                         nrlen = 0;
705 /*
706                 fprintf(fd, "\n\n\nBufSize: was: %d is: %d BufPos: was: %d is: %d \nBufContent: [%s]\n\n_____________________\n",
707                         len, nlen, rlen, nrlen, pch);
708 */
709                 fprintf(fd, "\n\n\nBufSize: was: %d is: %d BufPos: was: %d is: %d \n",
710                         len, nlen, rlen, nrlen);
711
712                 fprintf(fd, "Read: BufSize: %d BufContent: [%s]\n\n*************\n",
713                         StrLength(Target), ChrPtr(Target));
714                 fclose(fd);
715
716                 if ((rc < 0) && (Error != NULL))
717                         CtdlLogPrintf(CTDL_CRIT, 
718                                       "%s failed: %s\n",
719                                       __FUNCTION__,
720                                       Error);
721                 return rc;
722         }
723 }
724
725
726 /*
727  * client_getln()   ...   Get a LF-terminated line of text from the client.
728  * (This is implemented in terms of client_read() and could be
729  * justifiably moved out of sysdep.c)
730  */
731 int client_getln(char *buf, int bufsize)
732 {
733         int i, retval;
734         CitContext *CCC=CC;
735         const char *pCh;
736
737         retval = CtdlClientGetLine(CCC->MigrateBuf);
738         if (retval < 0)
739           return(retval >= 0);
740
741
742         i = StrLength(CCC->MigrateBuf);
743         pCh = ChrPtr(CCC->MigrateBuf);
744         /* Strip the trailing LF, and the trailing CR if present.
745          */
746         if (bufsize <= i)
747                 i = bufsize - 1;
748         while ( (i > 0)
749                 && ( (pCh[i - 1]==13)
750                      || ( pCh[i - 1]==10)) ) {
751                 i--;
752         }
753         memcpy(buf, pCh, i);
754         buf[i] = 0;
755
756         FlushStrBuf(CCC->MigrateBuf);
757         if (retval < 0) {
758                 safestrncpy(&buf[i], "000", bufsize - i);
759         }
760         return(retval >= 0);
761 }
762
763
764 /*
765  * Cleanup any contexts that are left lying around
766  */
767
768
769 void close_masters (void)
770 {
771         struct ServiceFunctionHook *serviceptr;
772         
773         /*
774          * close all protocol master sockets
775          */
776         for (serviceptr = ServiceHookTable; serviceptr != NULL;
777             serviceptr = serviceptr->next ) {
778
779                 if (serviceptr->tcp_port > 0)
780                 {
781                         CtdlLogPrintf(CTDL_INFO, "Closing listener on port %d\n",
782                                 serviceptr->tcp_port);
783                         serviceptr->tcp_port = 0;
784                 }
785                 
786                 if (serviceptr->sockpath != NULL)
787                         CtdlLogPrintf(CTDL_INFO, "Closing listener on '%s'\n",
788                                 serviceptr->sockpath);
789
790                 close(serviceptr->msock);
791                 /* If it's a Unix domain socket, remove the file. */
792                 if (serviceptr->sockpath != NULL) {
793                         unlink(serviceptr->sockpath);
794                         serviceptr->sockpath = NULL;
795                 }
796         }
797 }
798
799
800 /*
801  * The system-dependent part of master_cleanup() - close the master socket.
802  */
803 void sysdep_master_cleanup(void) {
804         
805         close_masters();
806         
807         context_cleanup();
808         
809 #ifdef HAVE_OPENSSL
810         destruct_ssl();
811 #endif
812         CtdlDestroyProtoHooks();
813         CtdlDestroyDeleteHooks();
814         CtdlDestroyXmsgHooks();
815         CtdlDestroyNetprocHooks();
816         CtdlDestroyUserHooks();
817         CtdlDestroyMessageHook();
818         CtdlDestroyCleanupHooks();
819         CtdlDestroyFixedOutputHooks();  
820         CtdlDestroySessionHooks();
821         CtdlDestroyServiceHook();
822         CtdlDestroyRoomHooks();
823         #ifdef HAVE_BACKTRACE
824 ///     eCrash_Uninit();
825         #endif
826 }
827
828
829
830 pid_t current_child;
831 void graceful_shutdown(int signum) {
832         kill(current_child, signum);
833         unlink(file_pid_file);
834         exit(0);
835 }
836
837 int nFireUps = 0;
838 int nFireUpsNonRestart = 0;
839 pid_t ForkedPid = 1;
840
841 /*
842  * Start running as a daemon.
843  */
844 void start_daemon(int unused) {
845         int status = 0;
846         pid_t child = 0;
847         FILE *fp;
848         int do_restart = 0;
849
850         current_child = 0;
851
852         /* Close stdin/stdout/stderr and replace them with /dev/null.
853          * We don't just call close() because we don't want these fd's
854          * to be reused for other files.
855          */
856         if (chdir(ctdl_run_dir) != 0)
857                 CtdlLogPrintf(CTDL_EMERG, 
858                               "unable to change into directory [%s]: %s", 
859                               ctdl_run_dir, strerror(errno));
860
861         child = fork();
862         if (child != 0) {
863                 exit(0);
864         }
865         
866         signal(SIGHUP, SIG_IGN);
867         signal(SIGINT, SIG_IGN);
868         signal(SIGQUIT, SIG_IGN);
869
870         setsid();
871         umask(0);
872         if ((freopen("/dev/null", "r", stdin) != stdin) || 
873             (freopen("/dev/null", "w", stdout) != stdout) || 
874             (freopen("/dev/null", "w", stderr) != stderr))
875                 CtdlLogPrintf(CTDL_EMERG, 
876                               "unable to reopen stdin/out/err %s", 
877                               strerror(errno));
878                 
879
880         do {
881                 current_child = fork();
882
883                 signal(SIGTERM, graceful_shutdown);
884         
885                 if (current_child < 0) {
886                         perror("fork");
887                         exit(errno);
888                 }
889         
890                 else if (current_child == 0) {
891                         return; /* continue starting citadel. */
892                 }
893         
894                 else {
895                         fp = fopen(file_pid_file, "w");
896                         if (fp != NULL) {
897                                 fprintf(fp, ""F_PID_T"\n", getpid());
898                                 fclose(fp);
899                         }
900                         waitpid(current_child, &status, 0);
901                 }
902                 do_restart = 0;
903                 nFireUpsNonRestart = nFireUps;
904                 
905                 /* Exit code 0 means the watcher should exit */
906                 if (WIFEXITED(status) && (WEXITSTATUS(status) == CTDLEXIT_SHUTDOWN)) {
907                         do_restart = 0;
908                 }
909
910                 /* Exit code 101-109 means the watcher should exit */
911                 else if (WIFEXITED(status) && (WEXITSTATUS(status) >= 101) && (WEXITSTATUS(status) <= 109)) {
912                         do_restart = 0;
913                 }
914
915                 /* Any other exit code, or no exit code, means we should restart. */
916                 else {
917                         do_restart = 1;
918                         nFireUps++;
919                         ForkedPid = current_child;
920                 }
921
922         } while (do_restart);
923
924         unlink(file_pid_file);
925         exit(WEXITSTATUS(status));
926 }
927
928
929
930 void checkcrash(void)
931 {
932         if (nFireUpsNonRestart != nFireUps)
933         {
934                 StrBuf *CrashMail;
935
936                 CrashMail = NewStrBuf();
937                 CtdlLogPrintf(CTDL_ALERT, "Posting crash message\n");
938                 StrBufPrintf(CrashMail, 
939                         " \n"
940                         " The Citadel server process (citserver) terminated unexpectedly."
941                         "\n \n"
942                         " This could be the result of a bug in the server program, or some external "
943                         "factor.\n \n"
944                         " You can obtain more information about this by enabling core dumps.\n \n"
945                         " For more information, please see:\n \n"
946                         " http://citadel.org/doku.php/faq:mastering_your_os:gdb#how.do.i.make.my.system.produce.core-files"
947                         "\n \n"
948
949                         " If you have already done this, the core dump is likely to be found at %score.%d\n"
950                         ,
951                         ctdl_run_dir, ForkedPid);
952                 CtdlAideMessage(ChrPtr(CrashMail), "Citadel server process terminated unexpectedly");
953                 FreeStrBuf(&CrashMail);
954         }
955 }
956
957
958 /*
959  * Generic routine to convert a login name to a full name (gecos)
960  * Returns nonzero if a conversion took place
961  */
962 int convert_login(char NameToConvert[]) {
963         struct passwd *pw;
964         int a;
965
966         pw = getpwnam(NameToConvert);
967         if (pw == NULL) {
968                 return(0);
969         }
970         else {
971                 strcpy(NameToConvert, pw->pw_gecos);
972                 for (a=0; a<strlen(NameToConvert); ++a) {
973                         if (NameToConvert[a] == ',') NameToConvert[a] = 0;
974                 }
975                 return(1);
976         }
977 }
978
979
980
981 /* 
982  * This loop just keeps going and going and going...
983  */
984 /*
985  * FIXME:
986  * This current implimentation of worker_thread creates a bottle neck in several situations
987  * The first thing to remember is that a single thread can handle more than one connection at a time.
988  * More threads mean less memory for the system to run in.
989  * So for efficiency we want every thread to be doing something useful or waiting in the main loop for
990  * something to happen anywhere.
991  * This current implimentation requires worker threads to wait in other locations, after it has
992  * been committed to a single connection which is very wasteful.
993  * As an extreme case consider this:
994  * A slow client connects and this slow client sends only one character each second.
995  * With this current implimentation a single worker thread is dispatched to handle that connection
996  * until such times as the client timeout expires, an error occurs on the socket or the client
997  * completes its transmission.
998  * THIS IS VERY BAD since that thread could have handled a read from many more clients in each one
999  * second interval between chars.
1000  *
1001  * It is my intention to re-write this code and the associated client_getln, client_read functions
1002  * to allow any thread to read data on behalf of any connection (context).
1003  * To do this I intend to have this main loop read chars into a buffer stored in the context.
1004  * Once the correct criteria for a full buffer is met then we will dispatch a thread to 
1005  * process it.
1006  * This worker thread loop also needs to be able to handle binary data.
1007  */
1008  
1009 void *worker_thread(void *arg) {
1010         int highest;
1011         CitContext *ptr;
1012         CitContext *bind_me = NULL;
1013         fd_set readfds;
1014         int retval = 0;
1015         struct timeval tv;
1016         int force_purge = 0;
1017         
1018
1019         while (!CtdlThreadCheckStop()) {
1020
1021                 /* make doubly sure we're not holding any stale db handles
1022                  * which might cause a deadlock.
1023                  */
1024                 cdb_check_handles();
1025 do_select:      force_purge = 0;
1026                 bind_me = NULL;         /* Which session shall we handle? */
1027
1028                 /* Initialize the fdset. */
1029                 FD_ZERO(&readfds);
1030                 highest = 0;
1031
1032                 begin_critical_section(S_SESSION_TABLE);
1033                 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1034                         int client_socket;
1035                         client_socket = ptr->client_socket;
1036                         /* Dont select on dead sessions only truly idle ones */
1037                         if ((ptr->state == CON_IDLE) && 
1038                             (CC->kill_me == 0) &&
1039                             (client_socket != -1))
1040                         {
1041                                 FD_SET(client_socket, &readfds);
1042                                 if (client_socket > highest)
1043                                         highest = client_socket;
1044                         }
1045                         if ((bind_me == NULL) && (ptr->state == CON_READY)) {
1046                                 bind_me = ptr;
1047                                 ptr->state = CON_EXECUTING;
1048                                 break;
1049                         }
1050                         if ((bind_me == NULL) && (ptr->state == CON_GREETING)) {
1051                                 bind_me = ptr;
1052                                 ptr->state = CON_STARTING;
1053                                 break;
1054                         }
1055                 }
1056                 end_critical_section(S_SESSION_TABLE);
1057
1058                 if (bind_me) {
1059                         goto SKIP_SELECT;
1060                 }
1061
1062                 /* If we got this far, it means that there are no sessions
1063                  * which a previous thread marked for attention, so we go
1064                  * ahead and get ready to select().
1065                  */
1066
1067                 if (!CtdlThreadCheckStop()) {
1068                         tv.tv_sec = 1;          /* wake up every second if no input */
1069                         tv.tv_usec = 0;
1070                         retval = CtdlThreadSelect(highest + 1, &readfds, NULL, NULL, &tv);
1071                 }
1072                 else
1073                         return NULL;
1074
1075                 /* Now figure out who made this select() unblock.
1076                  * First, check for an error or exit condition.
1077                  */
1078                 if (retval < 0) {
1079                         if (errno == EBADF) {
1080                                 CtdlLogPrintf(CTDL_NOTICE, "select() failed: (%s)\n",
1081                                         strerror(errno));
1082                                 goto do_select;
1083                         }
1084                         if (errno != EINTR) {
1085                                 CtdlLogPrintf(CTDL_EMERG, "Exiting (%s)\n", strerror(errno));
1086                                 CtdlThreadStopAll();
1087                                 continue;
1088                         } else {
1089                                 CtdlLogPrintf(CTDL_DEBUG, "Interrupted CtdlThreadSelect.\n");
1090                                 if (CtdlThreadCheckStop()) return(NULL);
1091                                 goto do_select;
1092                         }
1093                 }
1094                 else if(retval == 0) {
1095                         if (CtdlThreadCheckStop()) return(NULL);
1096                 }
1097
1098                 /* It must be a client socket.  Find a context that has data
1099                  * waiting on its socket *and* is in the CON_IDLE state.  Any
1100                  * active sockets other than our chosen one are marked as
1101                  * CON_READY so the next thread that comes around can just bind
1102                  * to one without having to select() again.
1103                  */
1104                 begin_critical_section(S_SESSION_TABLE);
1105                 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1106                         int checkfd = ptr->client_socket;
1107                         if ((checkfd != -1) && 
1108                             (FD_ISSET(checkfd, &readfds)) &&
1109                             (ptr->state == CON_IDLE) ) 
1110                         {
1111                                 ptr->input_waiting = 1;
1112                                 if (!bind_me) {
1113                                         bind_me = ptr;  /* I choose you! */
1114                                         bind_me->state = CON_EXECUTING;
1115                                 }
1116                                 else {
1117                                         ptr->state = CON_READY;
1118                                 }
1119                         }
1120                 }
1121                 end_critical_section(S_SESSION_TABLE);
1122
1123 SKIP_SELECT:
1124                 /* We're bound to a session */
1125                 if (bind_me != NULL) {
1126                         become_session(bind_me);
1127
1128                         if (bind_me->state == CON_STARTING) {
1129                                 bind_me->state = CON_EXECUTING;
1130                                 begin_session(bind_me);
1131                                 bind_me->h_greeting_function();
1132                         }
1133                         /* If the client has sent a command, execute it. */
1134                         if (CC->input_waiting) {
1135                                 CC->h_command_function();
1136
1137                                 while (HaveMoreLinesWaiting(CC))
1138                                        CC->h_command_function();
1139
1140                                 CC->input_waiting = 0;
1141                         }
1142
1143                         /* If there are asynchronous messages waiting and the
1144                          * client supports it, do those now */
1145                         if ((CC->is_async) && (CC->async_waiting)
1146                            && (CC->h_async_function != NULL)) {
1147                                 CC->h_async_function();
1148                                 CC->async_waiting = 0;
1149                         }
1150                         
1151                         force_purge = CC->kill_me;
1152                         become_session(NULL);
1153                         bind_me->state = CON_IDLE;
1154                 }
1155
1156                 dead_session_purge(force_purge);
1157                 do_housekeeping();
1158         }
1159         /* If control reaches this point, the server is shutting down */        
1160         return(NULL);
1161 }
1162
1163
1164
1165
1166 /*
1167  * A function to handle selecting on master sockets.
1168  * In other words it handles new connections.
1169  * It is a thread.
1170  */
1171 void *select_on_master (void *arg)
1172 {
1173         struct ServiceFunctionHook *serviceptr;
1174         fd_set master_fds;
1175         int highest;
1176         struct timeval tv;
1177         int ssock;                      /* Descriptor for client socket */
1178         CitContext *con= NULL;  /* Temporary context pointer */
1179         int m;
1180         int i;
1181         int retval;
1182
1183         while (!CtdlThreadCheckStop()) {
1184                 /* Initialize the fdset. */
1185                 FD_ZERO(&master_fds);
1186                 highest = 0;
1187
1188                 /* First, add the various master sockets to the fdset. */
1189                 for (serviceptr = ServiceHookTable; serviceptr != NULL;
1190                 serviceptr = serviceptr->next ) {
1191                         m = serviceptr->msock;
1192                         FD_SET(m, &master_fds);
1193                         if (m > highest) {
1194                                 highest = m;
1195                         }
1196                 }
1197
1198                 if (!CtdlThreadCheckStop()) {
1199                         tv.tv_sec = 60;         /* wake up every second if no input */
1200                         tv.tv_usec = 0;
1201                         retval = CtdlThreadSelect(highest + 1, &master_fds, NULL, NULL, &tv);
1202                 }
1203                 else
1204                         return NULL;
1205
1206                 /* Now figure out who made this select() unblock.
1207                  * First, check for an error or exit condition.
1208                  */
1209                 if (retval < 0) {
1210                         if (errno == EBADF) {
1211                                 CtdlLogPrintf(CTDL_NOTICE, "select() failed: (%s)\n",
1212                                         strerror(errno));
1213                                 continue;
1214                         }
1215                         if (errno != EINTR) {
1216                                 CtdlLogPrintf(CTDL_EMERG, "Exiting (%s)\n", strerror(errno));
1217                                 CtdlThreadStopAll();
1218                         } else {
1219                                 CtdlLogPrintf(CTDL_DEBUG, "Interrupted CtdlThreadSelect.\n");
1220                                 if (CtdlThreadCheckStop()) return(NULL);
1221                                 continue;
1222                         }
1223                 }
1224                 else if(retval == 0) {
1225                         if (CtdlThreadCheckStop()) return(NULL);
1226                         continue;
1227                 }
1228                 /* Next, check to see if it's a new client connecting
1229                  * on a master socket.
1230                  */
1231                 else for (serviceptr = ServiceHookTable; serviceptr != NULL;
1232                      serviceptr = serviceptr->next ) {
1233
1234                         if (FD_ISSET(serviceptr->msock, &master_fds)) {
1235                                 ssock = accept(serviceptr->msock, NULL, 0);
1236                                 if (ssock >= 0) {
1237                                         CtdlLogPrintf(CTDL_DEBUG,
1238                                                 "New client socket %d\n",
1239                                                 ssock);
1240
1241                                         /* The master socket is non-blocking but the client
1242                                          * sockets need to be blocking, otherwise certain
1243                                          * operations barf on FreeBSD.  Not a fatal error.
1244                                          */
1245                                         if (fcntl(ssock, F_SETFL, 0) < 0) {
1246                                                 CtdlLogPrintf(CTDL_EMERG,
1247                                                         "citserver: Can't set socket to blocking: %s\n",
1248                                                         strerror(errno));
1249                                         }
1250
1251                                         /* New context will be created already
1252                                          * set up in the CON_EXECUTING state.
1253                                          */
1254                                         con = CreateNewContext();
1255
1256                                         /* Assign our new socket number to it. */
1257                                         con->client_socket = ssock;
1258                                         con->h_command_function =
1259                                                 serviceptr->h_command_function;
1260                                         con->h_async_function =
1261                                                 serviceptr->h_async_function;
1262                                         con->h_greeting_function = serviceptr->h_greeting_function;
1263                                         con->ServiceName =
1264                                                 serviceptr->ServiceName;
1265                                         
1266                                         /* Determine whether it's a local socket */
1267                                         if (serviceptr->sockpath != NULL)
1268                                                 con->is_local_socket = 1;
1269         
1270                                         /* Set the SO_REUSEADDR socket option */
1271                                         i = 1;
1272                                         setsockopt(ssock, SOL_SOCKET,
1273                                                 SO_REUSEADDR,
1274                                                 &i, sizeof(i));
1275
1276                                         con->state = CON_GREETING;
1277
1278                                         retval--;
1279                                         if (retval == 0)
1280                                                 break;
1281                                 }
1282                         }
1283                 }
1284         }
1285         return NULL;
1286 }
1287
1288
1289
1290 /*
1291  * SyslogFacility()
1292  * Translate text facility name to syslog.h defined value.
1293  */
1294 int SyslogFacility(char *name)
1295 {
1296         int i;
1297         struct
1298         {
1299                 int facility;
1300                 char *name;
1301         }   facTbl[] =
1302         {
1303                 {   LOG_KERN,   "kern"          },
1304                 {   LOG_USER,   "user"          },
1305                 {   LOG_MAIL,   "mail"          },
1306                 {   LOG_DAEMON, "daemon"        },
1307                 {   LOG_AUTH,   "auth"          },
1308                 {   LOG_SYSLOG, "syslog"        },
1309                 {   LOG_LPR,    "lpr"           },
1310                 {   LOG_NEWS,   "news"          },
1311                 {   LOG_UUCP,   "uucp"          },
1312                 {   LOG_LOCAL0, "local0"        },
1313                 {   LOG_LOCAL1, "local1"        },
1314                 {   LOG_LOCAL2, "local2"        },
1315                 {   LOG_LOCAL3, "local3"        },
1316                 {   LOG_LOCAL4, "local4"        },
1317                 {   LOG_LOCAL5, "local5"        },
1318                 {   LOG_LOCAL6, "local6"        },
1319                 {   LOG_LOCAL7, "local7"        },
1320                 {   0,            NULL          }
1321         };
1322         for(i = 0; facTbl[i].name != NULL; i++) {
1323                 if(!strcasecmp(name, facTbl[i].name))
1324                         return facTbl[i].facility;
1325         }
1326         enable_syslog = 0;
1327         return LOG_DAEMON;
1328 }
1329
1330
1331 /********** MEM CHEQQER ***********/
1332
1333 #ifdef DEBUG_MEMORY_LEAKS
1334
1335 #undef malloc
1336 #undef realloc
1337 #undef strdup
1338 #undef free
1339
1340 void *tracked_malloc(size_t size, char *file, int line) {
1341         struct igheap *thisheap;
1342         void *block;
1343
1344         block = malloc(size);
1345         if (block == NULL) return(block);
1346
1347         thisheap = malloc(sizeof(struct igheap));
1348         if (thisheap == NULL) {
1349                 free(block);
1350                 return(NULL);
1351         }
1352
1353         thisheap->block = block;
1354         strcpy(thisheap->file, file);
1355         thisheap->line = line;
1356         
1357         begin_critical_section(S_DEBUGMEMLEAKS);
1358         thisheap->next = igheap;
1359         igheap = thisheap;
1360         end_critical_section(S_DEBUGMEMLEAKS);
1361
1362         return(block);
1363 }
1364
1365
1366 void *tracked_realloc(void *ptr, size_t size, char *file, int line) {
1367         struct igheap *thisheap;
1368         void *block;
1369
1370         block = realloc(ptr, size);
1371         if (block == NULL) return(block);
1372
1373         thisheap = malloc(sizeof(struct igheap));
1374         if (thisheap == NULL) {
1375                 free(block);
1376                 return(NULL);
1377         }
1378
1379         thisheap->block = block;
1380         strcpy(thisheap->file, file);
1381         thisheap->line = line;
1382         
1383         begin_critical_section(S_DEBUGMEMLEAKS);
1384         thisheap->next = igheap;
1385         igheap = thisheap;
1386         end_critical_section(S_DEBUGMEMLEAKS);
1387
1388         return(block);
1389 }
1390
1391
1392
1393 void tracked_free(void *ptr) {
1394         struct igheap *thisheap;
1395         struct igheap *trash;
1396
1397         free(ptr);
1398
1399         if (igheap == NULL) return;
1400         begin_critical_section(S_DEBUGMEMLEAKS);
1401         for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
1402                 if (thisheap->next != NULL) {
1403                         if (thisheap->next->block == ptr) {
1404                                 trash = thisheap->next;
1405                                 thisheap->next = thisheap->next->next;
1406                                 free(trash);
1407                         }
1408                 }
1409         }
1410         if (igheap->block == ptr) {
1411                 trash = igheap;
1412                 igheap = igheap->next;
1413                 free(trash);
1414         }
1415         end_critical_section(S_DEBUGMEMLEAKS);
1416 }
1417
1418 char *tracked_strdup(const char *s, char *file, int line) {
1419         char *ptr;
1420
1421         if (s == NULL) return(NULL);
1422         ptr = tracked_malloc(strlen(s) + 1, file, line);
1423         if (ptr == NULL) return(NULL);
1424         strncpy(ptr, s, strlen(s));
1425         return(ptr);
1426 }
1427
1428 void dump_heap(void) {
1429         struct igheap *thisheap;
1430
1431         for (thisheap = igheap; thisheap != NULL; thisheap = thisheap->next) {
1432                 CtdlLogPrintf(CTDL_CRIT, "UNFREED: %30s : %d\n",
1433                         thisheap->file, thisheap->line);
1434         }
1435 }
1436
1437 #endif /*  DEBUG_MEMORY_LEAKS */