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