New server option -b to specify the name of a file to which backtrace should be writt...
[citadel.git] / citadel / sysdep.c
1 // Citadel "system dependent" stuff.
2 //
3 // Here's where we (hopefully) have most parts of the Citadel server that
4 // might need tweaking when run on different operating system variants.
5 //
6 // Copyright (c) 1987-2021 by the citadel.org team
7 //
8 // This program is open source software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License, version 3.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 #include "sysdep.h"
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <sys/stat.h>
20 #include <errno.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include <syslog.h>
24 #include <sys/syslog.h>
25 #include <execinfo.h>
26 #include <netdb.h>
27 #include <sys/un.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <netinet/tcp.h>
33 #include <arpa/inet.h>
34 #define SHOW_ME_VAPPEND_PRINTF
35 #include <libcitadel.h>
36 #include "citserver.h"
37 #include "config.h"
38 #include "ctdl_module.h"
39 #include "sysdep_decls.h"
40 #include "modules/crypto/serv_crypto.h" // Needed for init_ssl, client_write_ssl, client_read_ssl, destruct_ssl
41 #include "housekeeping.h"
42 #include "context.h"
43
44 // Signal handler to shut down the server.
45 volatile int exit_signal = 0;
46 volatile int shutdown_and_halt = 0;
47 volatile int restart_server = 0;
48 volatile int running_as_daemon = 0;
49
50
51 static RETSIGTYPE signal_cleanup(int signum) {
52         syslog(LOG_DEBUG, "sysdep: caught signal %d - backtrace follows:", signum);
53
54         void *bt[1024];
55         int bt_size;
56         char **bt_syms;
57         int i;
58         FILE *backtrace_fp = NULL;
59
60         if (backtrace_filename != NULL) {
61                 backtrace_fp = fopen(backtrace_filename, "w");
62         }
63
64         bt_size = backtrace(bt, 1024);
65         bt_syms = backtrace_symbols(bt, bt_size);
66         for (i = 1; i < bt_size; i++) {
67                 syslog(LOG_DEBUG, "%s", bt_syms[i]);
68                 if (backtrace_fp) {
69                         fprintf(backtrace_fp, "%s\n", bt_syms[i]);
70                 }
71         }
72         free(bt_syms);
73
74         if (backtrace_fp) {
75                 fclose(backtrace_fp);
76         }
77
78         exit_signal = signum;
79         server_shutting_down = 1;
80 }
81
82
83 // Some initialization stuff...
84 void init_sysdep(void) {
85         sigset_t set;
86
87         // Avoid vulnerabilities related to FD_SETSIZE if we can.
88 #ifdef FD_SETSIZE
89 #ifdef RLIMIT_NOFILE
90         struct rlimit rl;
91         getrlimit(RLIMIT_NOFILE, &rl);
92         rl.rlim_cur = FD_SETSIZE;
93         rl.rlim_max = FD_SETSIZE;
94         setrlimit(RLIMIT_NOFILE, &rl);
95 #endif
96 #endif
97
98         // If we've got OpenSSL, we're going to use it.
99 #ifdef HAVE_OPENSSL
100         init_ssl();
101 #endif
102
103         if (pthread_key_create(&ThreadKey, NULL) != 0) {                        // TSD for threads
104                 syslog(LOG_ERR, "pthread_key_create() : %m");
105                 abort();
106         }
107         
108         if (pthread_key_create(&MyConKey, NULL) != 0) {                         // TSD for sessions
109                 syslog(LOG_CRIT, "sysdep: can't create TSD key: %m");
110                 abort();
111         }
112
113         // Interript, hangup, and terminate signals should cause the server to shut down.
114         sigemptyset(&set);
115         sigaddset(&set, SIGINT);
116         sigaddset(&set, SIGHUP);
117         sigaddset(&set, SIGTERM);
118         sigaddset(&set, SIGSEGV);
119         sigprocmask(SIG_UNBLOCK, &set, NULL);
120
121         signal(SIGINT, signal_cleanup);
122         signal(SIGHUP, signal_cleanup);
123         signal(SIGTERM, signal_cleanup);
124         signal(SIGSEGV, signal_cleanup);
125
126         // Do not shut down the server on broken pipe signals, otherwise the
127         // whole Citadel service would come down whenever a single client
128         // socket breaks.
129         signal(SIGPIPE, SIG_IGN);
130 }
131
132
133 // This is a generic function to set up a master socket for listening on
134 // a TCP port.  The server shuts down if the bind fails.  (IPv4/IPv6 version)
135 //
136 // ip_addr      IP address to bind
137 // port_number  port number to bind
138 // queue_len    number of incoming connections to allow in the queue
139 int ctdl_tcp_server(char *ip_addr, int port_number, int queue_len) {
140         struct protoent *p;
141         struct sockaddr_in6 sin6;
142         struct sockaddr_in sin4;
143         int s, i, b;
144         int ip_version = 6;
145
146         memset(&sin6, 0, sizeof(sin6));
147         memset(&sin4, 0, sizeof(sin4));
148         sin6.sin6_family = AF_INET6;
149         sin4.sin_family = AF_INET;
150
151         if (    (ip_addr == NULL)                                                       // any IPv6
152                 || (IsEmptyStr(ip_addr))
153                 || (!strcmp(ip_addr, "*"))
154         ) {
155                 ip_version = 6;
156                 sin6.sin6_addr = in6addr_any;
157         }
158         else if (!strcmp(ip_addr, "0.0.0.0"))                                           // any IPv4
159         {
160                 ip_version = 4;
161                 sin4.sin_addr.s_addr = INADDR_ANY;
162         }
163         else if ((strchr(ip_addr, '.')) && (!strchr(ip_addr, ':')))                     // specific IPv4
164         {
165                 ip_version = 4;
166                 if (inet_pton(AF_INET, ip_addr, &sin4.sin_addr) <= 0) {
167                         syslog(LOG_ALERT, "tcpserver: inet_pton: %m");
168                         return (-1);
169                 }
170         }
171         else                                                                            // specific IPv6
172         {
173                 ip_version = 6;
174                 if (inet_pton(AF_INET6, ip_addr, &sin6.sin6_addr) <= 0) {
175                         syslog(LOG_ALERT, "tcpserver: inet_pton: %m");
176                         return (-1);
177                 }
178         }
179
180         if (port_number == 0) {
181                 syslog(LOG_ALERT, "tcpserver: no port number was specified");
182                 return (-1);
183         }
184         sin6.sin6_port = htons((u_short) port_number);
185         sin4.sin_port = htons((u_short) port_number);
186
187         p = getprotobyname("tcp");
188         if (p == NULL) {
189                 syslog(LOG_ALERT, "tcpserver: getprotobyname: %m");
190                 return (-1);
191         }
192
193         s = socket( ((ip_version == 6) ? PF_INET6 : PF_INET), SOCK_STREAM, (p->p_proto));
194         if (s < 0) {
195                 syslog(LOG_ALERT, "tcpserver: socket: %m");
196                 return (-1);
197         }
198         // Set some socket options that make sense.
199         i = 1;
200         setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
201
202         if (ip_version == 6) {
203                 b = bind(s, (struct sockaddr *) &sin6, sizeof(sin6));
204         }
205         else {
206                 b = bind(s, (struct sockaddr *) &sin4, sizeof(sin4));
207         }
208
209         if (b < 0) {
210                 syslog(LOG_ALERT, "tcpserver: bind: %m");
211                 return (-1);
212         }
213
214         fcntl(s, F_SETFL, O_NONBLOCK);
215
216         if (listen(s, ((queue_len >= 5) ? queue_len : 5) ) < 0) {
217                 syslog(LOG_ALERT, "tcpserver: listen: %m");
218                 return (-1);
219         }
220         return (s);
221 }
222
223
224 // Create a Unix domain socket and listen on it
225 int ctdl_uds_server(char *sockpath, int queue_len) {
226         struct sockaddr_un addr;
227         int s;
228         int i;
229         int actual_queue_len;
230 #ifdef HAVE_STRUCT_UCRED
231         int passcred = 1;
232 #endif
233
234         actual_queue_len = queue_len;
235         if (actual_queue_len < 5) actual_queue_len = 5;
236
237         i = unlink(sockpath);
238         if ((i != 0) && (errno != ENOENT)) {
239                 syslog(LOG_ERR, "udsserver: %m");
240                 return(-1);
241         }
242
243         memset(&addr, 0, sizeof(addr));
244         addr.sun_family = AF_UNIX;
245         safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
246
247         s = socket(AF_UNIX, SOCK_STREAM, 0);
248         if (s < 0) {
249                 syslog(LOG_ERR, "udsserver: socket: %m");
250                 return(-1);
251         }
252
253         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
254                 syslog(LOG_ERR, "udsserver: bind: %m");
255                 return(-1);
256         }
257
258         // set to nonblock - we need this for some obscure situations
259         if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
260                 syslog(LOG_ERR, "udsserver: fcntl: %m");
261                 close(s);
262                 return(-1);
263         }
264
265         if (listen(s, actual_queue_len) < 0) {
266                 syslog(LOG_ERR, "udsserver: listen: %m");
267                 return(-1);
268         }
269
270 #ifdef HAVE_STRUCT_UCRED
271         setsockopt(s, SOL_SOCKET, SO_PASSCRED, &passcred, sizeof(passcred));
272 #endif
273
274         chmod(sockpath, S_ISGID|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH);
275         return(s);
276 }
277
278
279 // The following functions implement output buffering on operating systems which
280 // support it (such as Linux and various BSD flavors).
281 #ifndef HAVE_DARWIN
282 #ifdef TCP_CORK
283 #       define HAVE_TCP_BUFFERING
284 #else
285 #       ifdef TCP_NOPUSH
286 #               define HAVE_TCP_BUFFERING
287 #               define TCP_CORK TCP_NOPUSH
288 #       endif
289 #endif // TCP_CORK
290 #endif // HAVE_DARWIN
291
292 static unsigned on = 1, off = 0;
293
294 void buffer_output(void) {
295 #ifdef HAVE_TCP_BUFFERING
296 #ifdef HAVE_OPENSSL
297         if (!CC->redirect_ssl)
298 #endif
299                 setsockopt(CC->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
300 #endif
301 }
302
303 void unbuffer_output(void) {
304 #ifdef HAVE_TCP_BUFFERING
305 #ifdef HAVE_OPENSSL
306         if (!CC->redirect_ssl)
307 #endif
308                 setsockopt(CC->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
309 #endif
310 }
311
312 void flush_output(void) {
313 #ifdef HAVE_TCP_BUFFERING
314         struct CitContext *CCC = CC;
315         setsockopt(CCC->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
316         setsockopt(CCC->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
317 #endif
318 }
319
320
321 // close the client socket
322 void client_close(void) {
323         CitContext *CCC = CC;
324
325         if (!CCC) return;
326         if (CCC->client_socket <= 0) return;
327         syslog(LOG_DEBUG, "sysdep: closing socket %d", CCC->client_socket);
328         close(CCC->client_socket);
329         CCC->client_socket = -1 ;
330 }
331
332
333 // Send binary data to the client.
334 int client_write(const char *buf, int nbytes)
335 {
336         int bytes_written = 0;
337         int retval;
338 #ifndef HAVE_TCP_BUFFERING
339         int old_buffer_len = 0;
340 #endif
341         fd_set wset;
342         CitContext *Ctx;
343         int fdflags;
344
345         if (nbytes < 1) return(0);
346
347         Ctx = CC;
348
349         if (Ctx->redirect_buffer != NULL) {
350                 StrBufAppendBufPlain(Ctx->redirect_buffer,
351                                      buf, nbytes, 0);
352                 return 0;
353         }
354
355 #ifdef HAVE_OPENSSL
356         if (Ctx->redirect_ssl) {
357                 client_write_ssl(buf, nbytes);
358                 return 0;
359         }
360 #endif
361         if (Ctx->client_socket == -1) return -1;
362
363         fdflags = fcntl(Ctx->client_socket, F_GETFL);
364
365         while ((bytes_written < nbytes) && (Ctx->client_socket != -1)){
366                 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
367                         FD_ZERO(&wset);
368                         FD_SET(Ctx->client_socket, &wset);
369                         if (select(1, NULL, &wset, NULL, NULL) == -1) {
370                                 if (errno == EINTR)
371                                 {
372                                         syslog(LOG_DEBUG, "sysdep: client_write(%d bytes) select() interrupted.", nbytes-bytes_written);
373                                         if (server_shutting_down) {
374                                                 CC->kill_me = KILLME_SELECT_INTERRUPTED;
375                                                 return (-1);
376                                         } else {
377                                                 // can't trust fd's and stuff so we need to re-create them
378                                                 continue;
379                                         }
380                                 } else {
381                                         syslog(LOG_ERR, "sysdep: client_write(%d bytes) select failed: %m", nbytes - bytes_written);
382                                         client_close();
383                                         Ctx->kill_me = KILLME_SELECT_FAILED;
384                                         return -1;
385                                 }
386                         }
387                 }
388
389                 retval = write(Ctx->client_socket, &buf[bytes_written], nbytes - bytes_written);
390                 if (retval < 1) {
391                         syslog(LOG_ERR, "sysdep: client_write(%d bytes) failed: %m", nbytes - bytes_written);
392                         client_close();
393                         Ctx->kill_me = KILLME_WRITE_FAILED;
394                         return -1;
395                 }
396                 bytes_written = bytes_written + retval;
397         }
398         return 0;
399 }
400
401 void cputbuf(const StrBuf *Buf) {   
402         client_write(ChrPtr(Buf), StrLength(Buf)); 
403 }   
404
405
406 // Send formatted printable data to the client.
407 // Implemented in terms of client_write() so it's technically not sysdep...
408 void cprintf(const char *format, ...) {   
409         va_list arg_ptr;   
410         char buf[1024];
411    
412         va_start(arg_ptr, format);   
413         if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
414                 buf[sizeof buf - 2] = '\n';
415         client_write(buf, strlen(buf)); 
416         va_end(arg_ptr);
417 }
418
419
420 // Read data from the client socket.
421 //
422 // sock         socket fd to read from
423 // buf          buffer to read into 
424 // bytes        number of bytes to read
425 // timeout      Number of seconds to wait before timing out
426 //
427 // Possible return values:
428 //      1       Requested number of bytes has been read.
429 //      0       Request timed out.
430 //      -1      Connection is broken, or other error.
431 int client_read_blob(StrBuf *Target, int bytes, int timeout) {
432         CitContext *CCC=CC;
433         const char *Error;
434         int retval = 0;
435
436 #ifdef HAVE_OPENSSL
437         if (CCC->redirect_ssl) {
438                 retval = client_read_sslblob(Target, bytes, timeout);
439                 if (retval < 0) {
440                         syslog(LOG_ERR, "sysdep: client_read_blob() failed");
441                 }
442         }
443         else 
444 #endif
445         {
446                 retval = StrBufReadBLOBBuffered(Target, 
447                                                 CCC->RecvBuf.Buf,
448                                                 &CCC->RecvBuf.ReadWritePointer,
449                                                 &CCC->client_socket,
450                                                 1, 
451                                                 bytes,
452                                                 O_TERM,
453                                                 &Error
454                 );
455                 if (retval < 0) {
456                         syslog(LOG_ERR, "sysdep: client_read_blob() failed: %s", Error);
457                         client_close();
458                         return retval;
459                 }
460         }
461         return retval;
462 }
463
464
465 // to make client_read_random_blob() more efficient, increase buffer size.
466 // just use in greeting function, else your buffer may be flushed
467 void client_set_inbound_buf(long N)
468 {
469         CitContext *CCC=CC;
470         FlushStrBuf(CCC->RecvBuf.Buf);
471         ReAdjustEmptyBuf(CCC->RecvBuf.Buf, N * SIZ, N * SIZ);
472 }
473
474 int client_read_random_blob(StrBuf *Target, int timeout)
475 {
476         CitContext *CCC=CC;
477         int rc;
478
479         rc =  client_read_blob(Target, 1, timeout);
480         if (rc > 0)
481         {
482                 long len;
483                 const char *pch;
484                 
485                 len = StrLength(CCC->RecvBuf.Buf);
486                 pch = ChrPtr(CCC->RecvBuf.Buf);
487
488                 if (len > 0)
489                 {
490                         if (CCC->RecvBuf.ReadWritePointer != NULL) {
491                                 len -= CCC->RecvBuf.ReadWritePointer - pch;
492                                 pch = CCC->RecvBuf.ReadWritePointer;
493                         }
494                         StrBufAppendBufPlain(Target, pch, len, 0);
495                         FlushStrBuf(CCC->RecvBuf.Buf);
496                         CCC->RecvBuf.ReadWritePointer = NULL;
497                         return StrLength(Target);
498                 }
499                 return rc;
500         }
501         else
502                 return rc;
503 }
504
505 int client_read_to(char *buf, int bytes, int timeout)
506 {
507         CitContext *CCC=CC;
508         int rc;
509
510         rc = client_read_blob(CCC->MigrateBuf, bytes, timeout);
511         if (rc < 0)
512         {
513                 *buf = '\0';
514                 return rc;
515         }
516         else
517         {
518                 memcpy(buf, 
519                        ChrPtr(CCC->MigrateBuf),
520                        StrLength(CCC->MigrateBuf) + 1);
521                 FlushStrBuf(CCC->MigrateBuf);
522                 return rc;
523         }
524 }
525
526
527 int HaveMoreLinesWaiting(CitContext *CCC) {
528         if ((CCC->kill_me != 0) ||
529             ( (CCC->RecvBuf.ReadWritePointer == NULL) && 
530               (StrLength(CCC->RecvBuf.Buf) == 0) && 
531               (CCC->client_socket != -1)) )
532                 return 0;
533         else
534                 return 1;
535 }
536
537
538 // Read data from the client socket with default timeout.
539 // (This is implemented in terms of client_read_to() and could be
540 // justifiably moved out of sysdep.c)
541 INLINE int client_read(char *buf, int bytes) {
542         return(client_read_to(buf, bytes, CtdlGetConfigInt("c_sleeping")));
543 }
544
545 int CtdlClientGetLine(StrBuf *Target) {
546         CitContext *CCC=CC;
547         const char *Error;
548         int rc;
549
550         FlushStrBuf(Target);
551 #ifdef HAVE_OPENSSL
552         if (CCC->redirect_ssl) {
553                 rc = client_readline_sslbuffer(Target, CCC->RecvBuf.Buf, &CCC->RecvBuf.ReadWritePointer, 1);
554                 return rc;
555         }
556         else 
557 #endif
558         {
559                 rc = StrBufTCP_read_buffered_line_fast(Target, 
560                                                        CCC->RecvBuf.Buf,
561                                                        &CCC->RecvBuf.ReadWritePointer,
562                                                        &CCC->client_socket,
563                                                        5,
564                                                        1,
565                                                        &Error
566                 );
567                 return rc;
568         }
569 }
570
571
572 // Get a LF-terminated line of text from the client.
573 // (This is implemented in terms of client_read() and could be
574 // justifiably moved out of sysdep.c)
575 int client_getln(char *buf, int bufsize) {
576         int i, retval;
577         CitContext *CCC=CC;
578         const char *pCh;
579
580         retval = CtdlClientGetLine(CCC->MigrateBuf);
581         if (retval < 0)
582           return(retval >= 0);
583
584
585         i = StrLength(CCC->MigrateBuf);
586         pCh = ChrPtr(CCC->MigrateBuf);
587         // Strip the trailing LF, and the trailing CR if present.
588         if (bufsize <= i)
589                 i = bufsize - 1;
590         while ( (i > 0)
591                 && ( (pCh[i - 1]==13)
592                      || ( pCh[i - 1]==10)) ) {
593                 i--;
594         }
595         memcpy(buf, pCh, i);
596         buf[i] = 0;
597
598         FlushStrBuf(CCC->MigrateBuf);
599         if (retval < 0) {
600                 safestrncpy(&buf[i], "000", bufsize - i);
601         }
602         return(retval >= 0);
603 }
604
605
606 // Cleanup any contexts that are left lying around
607 void close_masters(void) {
608         struct ServiceFunctionHook *serviceptr;
609         const char *Text;
610
611         // close all protocol master sockets
612         for (serviceptr = ServiceHookTable; serviceptr != NULL;
613             serviceptr = serviceptr->next ) {
614
615                 if (serviceptr->tcp_port > 0) {
616                         if (serviceptr->msock == -1) {
617                                 Text = "not closing again";
618                         }
619                         else {
620                                 Text = "Closing";
621                         }
622                         syslog(LOG_INFO, "sysdep: %s %d listener on port %d",
623                                Text,
624                                serviceptr->msock,
625                                serviceptr->tcp_port
626                         );
627                         serviceptr->tcp_port = 0;
628                 }
629                 
630                 if (serviceptr->sockpath != NULL) {
631                         if (serviceptr->msock == -1) {
632                                 Text = "not closing again";
633                         }
634                         else {
635                                 Text = "Closing";
636                         }
637                         syslog(LOG_INFO, "sysdep: %s %d listener on '%s'",
638                                Text,
639                                serviceptr->msock,
640                                serviceptr->sockpath
641                         );
642                 }
643
644                 if (serviceptr->msock != -1) {
645                         close(serviceptr->msock);
646                         serviceptr->msock = -1;
647                 }
648
649                 // If it's a Unix domain socket, remove the file.
650                 if (serviceptr->sockpath != NULL) {
651                         unlink(serviceptr->sockpath);
652                         serviceptr->sockpath = NULL;
653                 }
654         }
655 }
656
657
658 // The system-dependent part of master_cleanup() - close the master socket.
659 void sysdep_master_cleanup(void) {
660         close_masters();
661         context_cleanup();
662 #ifdef HAVE_OPENSSL
663         destruct_ssl();
664 #endif
665 }
666
667
668
669 pid_t current_child;
670 void graceful_shutdown(int signum) {
671         kill(current_child, signum);
672         unlink(file_pid_file);
673         exit(0);
674 }
675
676 int nFireUps = 0;
677 int nFireUpsNonRestart = 0;
678 pid_t ForkedPid = 1;
679
680 // Start running as a daemon.
681 void start_daemon(int unused) {
682         int status = 0;
683         pid_t child = 0;
684         FILE *fp;
685         int do_restart = 0;
686         current_child = 0;
687
688         // Close stdin/stdout/stderr and replace them with /dev/null.
689         // We don't just call close() because we don't want these fd's
690         // to be reused for other files.
691         child = fork();
692         if (child != 0) {
693                 exit(0);
694         }
695         
696         signal(SIGHUP, SIG_IGN);
697         signal(SIGINT, SIG_IGN);
698         signal(SIGQUIT, SIG_IGN);
699
700         setsid();
701         umask(0);
702         if (    (freopen("/dev/null", "r", stdin) != stdin) || 
703                 (freopen("/dev/null", "w", stdout) != stdout) || 
704                 (freopen("/dev/null", "w", stderr) != stderr)
705         ) {
706                 syslog(LOG_ERR, "sysdep: unable to reopen stdio: %m");
707         }
708
709         do {
710                 current_child = fork();
711                 signal(SIGTERM, graceful_shutdown);
712                 if (current_child < 0) {
713                         perror("fork");
714                         exit(errno);
715                 }
716                 else if (current_child == 0) {
717                         return; // continue starting citadel.
718                 }
719                 else {
720                         fp = fopen(file_pid_file, "w");
721                         if (fp != NULL) {
722                                 fprintf(fp, ""F_PID_T"\n", getpid());
723                                 fclose(fp);
724                         }
725                         waitpid(current_child, &status, 0);
726                 }
727                 nFireUpsNonRestart = nFireUps;
728                 
729                 // Exit code 0 means the watcher should exit
730                 if (WIFEXITED(status) && (WEXITSTATUS(status) == CTDLEXIT_SHUTDOWN)) {
731                         do_restart = 0;
732                 }
733
734                 // Exit code 101-109 means the watcher should exit
735                 else if (WIFEXITED(status) && (WEXITSTATUS(status) >= 101) && (WEXITSTATUS(status) <= 109)) {
736                         do_restart = 0;
737                 }
738
739                 // Any other exit code, or no exit code, means we should restart.
740                 else {
741                         do_restart = 1;
742                         nFireUps++;
743                         ForkedPid = current_child;
744                 }
745
746         } while (do_restart);
747
748         unlink(file_pid_file);
749         exit(WEXITSTATUS(status));
750 }
751
752
753 void checkcrash(void) {
754         if (nFireUpsNonRestart != nFireUps) {
755                 StrBuf *CrashMail;
756                 CrashMail = NewStrBuf();
757                 syslog(LOG_ALERT, "sysdep: posting crash message");
758                 StrBufPrintf(CrashMail, 
759                         " \n"
760                         " The Citadel server process (citserver) terminated unexpectedly."
761                         "\n \n"
762                         " This could be the result of a bug in the server program, or some external "
763                         "factor.\n \n"
764                         " You can obtain more information about this by enabling core dumps.\n \n"
765                         " For more information, please see:\n \n"
766                         " http://citadel.org/doku.php?id=faq:mastering_your_os:gdb#how.do.i.make.my.system.produce.core-files"
767                         "\n \n"
768
769                         " If you have already done this, the core dump is likely to be found at %score.%d\n"
770                         ,
771                         ctdl_run_dir, ForkedPid);
772                 CtdlAideMessage(ChrPtr(CrashMail), "Citadel server process terminated unexpectedly");
773                 FreeStrBuf(&CrashMail);
774         }
775 }
776
777
778 // Generic routine to convert a login name to a full name (gecos)
779 // Returns nonzero if a conversion took place
780 int convert_login(char NameToConvert[]) {
781         struct passwd *pw;
782         unsigned int a;
783
784         pw = getpwnam(NameToConvert);
785         if (pw == NULL) {
786                 return(0);
787         }
788         else {
789                 strcpy(NameToConvert, pw->pw_gecos);
790                 for (a=0; a<strlen(NameToConvert); ++a) {
791                         if (NameToConvert[a] == ',') NameToConvert[a] = 0;
792                 }
793                 return(1);
794         }
795 }
796
797
798 void HuntBadSession(void) {
799         int highest;
800         CitContext *ptr;
801         fd_set readfds;
802         struct timeval tv;
803         struct ServiceFunctionHook *serviceptr;
804
805         // Next, add all of the client sockets
806         begin_critical_section(S_SESSION_TABLE);
807         for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
808                 if ((ptr->state == CON_SYS) && (ptr->client_socket == 0))
809                         continue;
810                 // Initialize the fdset.
811                 FD_ZERO(&readfds);
812                 highest = 0;
813                 tv.tv_sec = 0;          // wake up every second if no input
814                 tv.tv_usec = 0;
815
816                 // Don't select on dead sessions, only truly idle ones
817                 if (    (ptr->state == CON_IDLE)
818                         && (ptr->kill_me == 0)
819                         && (ptr->client_socket > 0)
820                 ) {
821                         FD_SET(ptr->client_socket, &readfds);
822                         if (ptr->client_socket > highest)
823                                 highest = ptr->client_socket;
824                         
825                         if ((select(highest + 1, &readfds, NULL, NULL, &tv) < 0) && (errno == EBADF))
826                         {
827                                 // Gotcha!
828                                 syslog(LOG_ERR,
829                                        "sysdep: killing session CC[%d] bad FD: [%d] User[%s] Host[%s:%s]",
830                                         ptr->cs_pid,
831                                         ptr->client_socket,
832                                         ptr->curr_user,
833                                         ptr->cs_host,
834                                         ptr->cs_addr
835                                 );
836                                 ptr->kill_me = 1;
837                                 ptr->client_socket = -1;
838                                 break;
839                         }
840                 }
841         }
842         end_critical_section(S_SESSION_TABLE);
843
844         // First, add the various master sockets to the fdset.
845         for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next ) {
846
847                 // Initialize the fdset.
848                 highest = 0;
849                 tv.tv_sec = 0;          // wake up every second if no input
850                 tv.tv_usec = 0;
851
852                 FD_SET(serviceptr->msock, &readfds);
853                 if (serviceptr->msock > highest) {
854                         highest = serviceptr->msock;
855                 }
856                 if ((select(highest + 1, &readfds, NULL, NULL, &tv) < 0) &&
857                     (errno == EBADF))
858                 {
859                         // Gotcha! server socket dead? commit suicide!
860                         syslog(LOG_ERR, "sysdep: found bad FD: %d and its a server socket! Shutting Down!", serviceptr->msock);
861                         server_shutting_down = 1;
862                         break;
863                 }
864         }
865 }
866
867
868 // This loop just keeps going and going and going...
869 void *worker_thread(void *blah) {
870         int highest;
871         CitContext *ptr;
872         CitContext *bind_me = NULL;
873         fd_set readfds;
874         int retval = 0;
875         struct timeval tv;
876         int force_purge = 0;
877         struct ServiceFunctionHook *serviceptr;
878         int ssock;                      // Descriptor for client socket
879         CitContext *con = NULL;         // Temporary context pointer
880         int i;
881
882         pthread_mutex_lock(&ThreadCountMutex);
883         ++num_workers;
884         pthread_mutex_unlock(&ThreadCountMutex);
885
886         while (!server_shutting_down) {
887
888                 // make doubly sure we're not holding any stale db handles which might cause a deadlock
889                 cdb_check_handles();
890 do_select:      force_purge = 0;
891                 bind_me = NULL;         // Which session shall we handle?
892
893                 // Initialize the fdset
894                 FD_ZERO(&readfds);
895                 highest = 0;
896
897                 // First, add the various master sockets to the fdset.
898                 for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next ) {
899                         FD_SET(serviceptr->msock, &readfds);
900                         if (serviceptr->msock > highest) {
901                                 highest = serviceptr->msock;
902                         }
903                 }
904
905                 // Next, add all of the client sockets.
906                 begin_critical_section(S_SESSION_TABLE);
907                 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
908                         if ((ptr->state == CON_SYS) && (ptr->client_socket == 0))
909                             continue;
910
911                         // Don't select on dead sessions, only truly idle ones
912                         if (    (ptr->state == CON_IDLE)
913                                 && (ptr->kill_me == 0)
914                                 && (ptr->client_socket > 0)
915                         ) {
916                                 FD_SET(ptr->client_socket, &readfds);
917                                 if (ptr->client_socket > highest)
918                                         highest = ptr->client_socket;
919                         }
920                         if ((bind_me == NULL) && (ptr->state == CON_READY)) {
921                                 bind_me = ptr;
922                                 ptr->state = CON_EXECUTING;
923                                 break;
924                         }
925                         if ((bind_me == NULL) && (ptr->state == CON_GREETING)) {
926                                 bind_me = ptr;
927                                 ptr->state = CON_STARTING;
928                                 break;
929                         }
930                 }
931                 end_critical_section(S_SESSION_TABLE);
932
933                 if (bind_me) {
934                         goto SKIP_SELECT;
935                 }
936
937                 // If we got this far, it means that there are no sessions
938                 // which a previous thread marked for attention, so we go
939                 // ahead and get ready to select().
940
941                 if (!server_shutting_down) {
942                         tv.tv_sec = 1;          // wake up every second if no input
943                         tv.tv_usec = 0;
944                         retval = select(highest + 1, &readfds, NULL, NULL, &tv);
945                 }
946                 else {
947                         --num_workers;
948                         return NULL;
949                 }
950
951                 // Now figure out who made this select() unblock.
952                 // First, check for an error or exit condition.
953                 if (retval < 0) {
954                         if (errno == EBADF) {
955                                 syslog(LOG_ERR, "sysdep: select() failed: %m");
956                                 HuntBadSession();
957                                 goto do_select;
958                         }
959                         if (errno != EINTR) {
960                                 syslog(LOG_ERR, "sysdep: exiting: %m");
961                                 server_shutting_down = 1;
962                                 continue;
963                         } else {
964                                 if (server_shutting_down) {
965                                         --num_workers;
966                                         return(NULL);
967                                 }
968                                 goto do_select;
969                         }
970                 }
971                 else if (retval == 0) {
972                         if (server_shutting_down) {
973                                 --num_workers;
974                                 return(NULL);
975                         }
976                 }
977
978                 // Next, check to see if it's a new client connecting on a master socket.
979
980                 else if ((retval > 0) && (!server_shutting_down)) for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next) {
981
982                         if (FD_ISSET(serviceptr->msock, &readfds)) {
983                                 ssock = accept(serviceptr->msock, NULL, 0);
984                                 if (ssock >= 0) {
985                                         syslog(LOG_DEBUG, "sysdep: new client socket %d", ssock);
986
987                                         // The master socket is non-blocking but the client
988                                         // sockets need to be blocking, otherwise certain
989                                         // operations barf on FreeBSD.  Not a fatal error.
990                                         if (fcntl(ssock, F_SETFL, 0) < 0) {
991                                                 syslog(LOG_ERR, "sysdep: Can't set socket to blocking: %m");
992                                         }
993
994                                         // New context will be created already
995                                         // set up in the CON_EXECUTING state.
996                                         con = CreateNewContext();
997
998                                         // Assign our new socket number to it.
999                                         con->tcp_port = serviceptr->tcp_port;
1000                                         con->client_socket = ssock;
1001                                         con->h_command_function = serviceptr->h_command_function;
1002                                         con->h_async_function = serviceptr->h_async_function;
1003                                         con->h_greeting_function = serviceptr->h_greeting_function;
1004                                         con->ServiceName = serviceptr->ServiceName;
1005                                         
1006                                         // Connections on a local client are always from the same host
1007                                         if (serviceptr->sockpath != NULL) {
1008                                                 con->is_local_client = 1;
1009                                         }
1010         
1011                                         // Set the SO_REUSEADDR socket option
1012                                         i = 1;
1013                                         setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
1014                                         con->state = CON_GREETING;
1015                                         retval--;
1016                                         if (retval == 0)
1017                                                 break;
1018                                 }
1019                         }
1020                 }
1021
1022                 // It must be a client socket.  Find a context that has data
1023                 // waiting on its socket *and* is in the CON_IDLE state.  Any
1024                 // active sockets other than our chosen one are marked as
1025                 // CON_READY so the next thread that comes around can just bind
1026                 // to one without having to select() again.
1027                 begin_critical_section(S_SESSION_TABLE);
1028                 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
1029                         int checkfd = ptr->client_socket;
1030                         if ((checkfd != -1) && (ptr->state == CON_IDLE) ){
1031                                 if (FD_ISSET(checkfd, &readfds)) {
1032                                         ptr->input_waiting = 1;
1033                                         if (!bind_me) {
1034                                                 bind_me = ptr;  // I choose you!
1035                                                 bind_me->state = CON_EXECUTING;
1036                                         }
1037                                         else {
1038                                                 ptr->state = CON_READY;
1039                                         }
1040                                 } else if ((ptr->is_async) && (ptr->async_waiting) && (ptr->h_async_function)) {
1041                                         if (!bind_me) {
1042                                                 bind_me = ptr;  // I choose you!
1043                                                 bind_me->state = CON_EXECUTING;
1044                                         }
1045                                         else {
1046                                                 ptr->state = CON_READY;
1047                                         }
1048                                 }
1049                         }
1050                 }
1051                 end_critical_section(S_SESSION_TABLE);
1052
1053 SKIP_SELECT:
1054                 // We're bound to a session
1055                 pthread_mutex_lock(&ThreadCountMutex);
1056                 ++active_workers;
1057                 pthread_mutex_unlock(&ThreadCountMutex);
1058
1059                 if (bind_me != NULL) {
1060                         become_session(bind_me);
1061
1062                         if (bind_me->state == CON_STARTING) {
1063                                 bind_me->state = CON_EXECUTING;
1064                                 begin_session(bind_me);
1065                                 bind_me->h_greeting_function();
1066                         }
1067                         // If the client has sent a command, execute it.
1068                         if (CC->input_waiting) {
1069                                 CC->h_command_function();
1070
1071                                 while (HaveMoreLinesWaiting(CC))
1072                                        CC->h_command_function();
1073
1074                                 CC->input_waiting = 0;
1075                         }
1076
1077                         // If there are asynchronous messages waiting and the client supports it, do those now
1078                         if ((CC->is_async) && (CC->async_waiting) && (CC->h_async_function != NULL)) {
1079                                 CC->h_async_function();
1080                                 CC->async_waiting = 0;
1081                         }
1082
1083                         force_purge = CC->kill_me;
1084                         become_session(NULL);
1085                         bind_me->state = CON_IDLE;
1086                 }
1087
1088                 dead_session_purge(force_purge);
1089                 do_housekeeping();
1090
1091                 pthread_mutex_lock(&ThreadCountMutex);
1092                 --active_workers;
1093                 if ((active_workers + CtdlGetConfigInt("c_min_workers") < num_workers) &&
1094                     (num_workers > CtdlGetConfigInt("c_min_workers")))
1095                 {
1096                         num_workers--;
1097                         pthread_mutex_unlock(&ThreadCountMutex);
1098                         return (NULL);
1099                 }
1100                 pthread_mutex_unlock(&ThreadCountMutex);
1101         }
1102
1103         // If control reaches this point, the server is shutting down
1104         pthread_mutex_lock(&ThreadCountMutex);
1105         --num_workers;
1106         pthread_mutex_unlock(&ThreadCountMutex);
1107         return(NULL);
1108 }
1109
1110
1111 // SyslogFacility()
1112 // Translate text facility name to syslog.h defined value.
1113 int SyslogFacility(char *name)
1114 {
1115         int i;
1116         struct
1117         {
1118                 int facility;
1119                 char *name;
1120         }   facTbl[] =
1121         {
1122                 {   LOG_KERN,   "kern"          },
1123                 {   LOG_USER,   "user"          },
1124                 {   LOG_MAIL,   "mail"          },
1125                 {   LOG_DAEMON, "daemon"        },
1126                 {   LOG_AUTH,   "auth"          },
1127                 {   LOG_SYSLOG, "syslog"        },
1128                 {   LOG_LPR,    "lpr"           },
1129                 {   LOG_NEWS,   "news"          },
1130                 {   LOG_UUCP,   "uucp"          },
1131                 {   LOG_LOCAL0, "local0"        },
1132                 {   LOG_LOCAL1, "local1"        },
1133                 {   LOG_LOCAL2, "local2"        },
1134                 {   LOG_LOCAL3, "local3"        },
1135                 {   LOG_LOCAL4, "local4"        },
1136                 {   LOG_LOCAL5, "local5"        },
1137                 {   LOG_LOCAL6, "local6"        },
1138                 {   LOG_LOCAL7, "local7"        },
1139                 {   0,            NULL          }
1140         };
1141         for(i = 0; facTbl[i].name != NULL; i++) {
1142                 if(!strcasecmp(name, facTbl[i].name))
1143                         return facTbl[i].facility;
1144         }
1145         return LOG_DAEMON;
1146 }