1 // This started as a portability layer, but then everything got more standard.
2 // Most of it handles network communication.
4 // Copyright (c) 1987-2023 by the citadel.org team
6 // This program is open source software. Use, duplication, or disclosure
7 // is subject to the terms of the GNU General Public License, version 3.
17 #include <sys/syslog.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <netinet/tcp.h>
26 #include <arpa/inet.h>
27 #define SHOW_ME_VAPPEND_PRINTF
28 #include <libcitadel.h>
29 #include "citserver.h"
31 #include "ctdl_module.h"
32 #include "sysdep_decls.h"
33 #include "modules/crypto/serv_crypto.h" // Needed for init_ssl, client_write_ssl, client_read_ssl
34 #include "housekeeping.h"
37 // Signal handler to shut down the server.
38 volatile int exit_signal = 0;
39 volatile int shutdown_and_halt = 0;
40 volatile int restart_server = 0;
41 volatile int running_as_daemon = 0;
44 static RETSIGTYPE signal_cleanup(int signum) {
45 syslog(LOG_DEBUG, "sysdep: caught signal %d", signum);
46 signal(SIGINT, SIG_DFL);
47 signal(SIGHUP, SIG_DFL);
48 signal(SIGTERM, SIG_DFL);
49 //signal(SIGSEGV, SIG_DFL);
51 server_shutting_down = 1;
52 master_cleanup(signum);
56 // Some initialization stuff...
57 void init_sysdep(void) {
60 // Avoid vulnerabilities related to FD_SETSIZE if we can.
64 getrlimit(RLIMIT_NOFILE, &rl);
65 rl.rlim_cur = FD_SETSIZE;
66 rl.rlim_max = FD_SETSIZE;
67 setrlimit(RLIMIT_NOFILE, &rl);
71 // If we've got OpenSSL, we're going to use it.
76 if (pthread_key_create(&MyConKey, NULL) != 0) { // TSD for sessions
77 syslog(LOG_CRIT, "sysdep: can't create TSD key: %m");
78 exit(CTDLEXIT_THREAD);
81 // Interript, hangup, and terminate signals should cause the server to shut down.
83 sigaddset(&set, SIGINT);
84 sigaddset(&set, SIGHUP);
85 sigaddset(&set, SIGTERM);
86 //sigaddset(&set, SIGSEGV);
87 sigprocmask(SIG_UNBLOCK, &set, NULL);
89 signal(SIGINT, signal_cleanup);
90 signal(SIGHUP, signal_cleanup);
91 signal(SIGTERM, signal_cleanup);
92 //signal(SIGSEGV, signal_cleanup);
94 // Do not shut down the server on broken pipe signals, otherwise the
95 // whole Citadel service would come down whenever a single client
97 signal(SIGPIPE, SIG_IGN);
101 // This is a generic function to set up a master socket for listening on
102 // a TCP port. The server shuts down if the bind fails. (IPv4/IPv6 version)
104 // ip_addr IP address to bind
105 // port_number port number to bind
106 // queue_len number of incoming connections to allow in the queue
107 int ctdl_tcp_server(char *ip_addr, int port_number, int queue_len) {
109 struct sockaddr_in6 sin6;
110 struct sockaddr_in sin4;
114 memset(&sin6, 0, sizeof(sin6));
115 memset(&sin4, 0, sizeof(sin4));
116 sin6.sin6_family = AF_INET6;
117 sin4.sin_family = AF_INET;
119 if ( (ip_addr == NULL) // any IPv6
120 || (IsEmptyStr(ip_addr))
121 || (!strcmp(ip_addr, "*"))
122 || (!strcmp(ip_addr, "::"))
125 sin6.sin6_addr = in6addr_any;
127 else if (!strcmp(ip_addr, "0.0.0.0")) { // any IPv4
129 sin4.sin_addr.s_addr = INADDR_ANY;
131 else if ((strchr(ip_addr, '.')) && (!strchr(ip_addr, ':'))) { // specific IPv4
133 if (inet_pton(AF_INET, ip_addr, &sin4.sin_addr) <= 0) {
134 syslog(LOG_ALERT, "tcpserver: inet_pton: %m");
138 else { // specific IPv6
140 if (inet_pton(AF_INET6, ip_addr, &sin6.sin6_addr) <= 0) {
141 syslog(LOG_ALERT, "tcpserver: inet_pton: %m");
146 if (port_number == 0) {
147 syslog(LOG_ALERT, "tcpserver: no port number was specified");
150 sin6.sin6_port = htons((u_short) port_number);
151 sin4.sin_port = htons((u_short) port_number);
153 p = getprotobyname("tcp");
155 syslog(LOG_ALERT, "tcpserver: getprotobyname: %m");
159 s = socket( ((ip_version == 6) ? PF_INET6 : PF_INET), SOCK_STREAM, (p->p_proto));
161 syslog(LOG_ALERT, "tcpserver: socket: %m");
164 // Set some socket options that make sense.
166 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
168 if (ip_version == 6) {
169 b = bind(s, (struct sockaddr *) &sin6, sizeof(sin6));
172 b = bind(s, (struct sockaddr *) &sin4, sizeof(sin4));
176 syslog(LOG_ALERT, "tcpserver: bind: %m");
180 fcntl(s, F_SETFL, O_NONBLOCK);
182 if (listen(s, ((queue_len >= 5) ? queue_len : 5) ) < 0) {
183 syslog(LOG_ALERT, "tcpserver: listen: %m");
190 // Create a Unix domain socket and listen on it
191 int ctdl_uds_server(char *sockpath, int queue_len) {
192 struct sockaddr_un addr;
195 int actual_queue_len;
197 actual_queue_len = queue_len;
198 if (actual_queue_len < 5) actual_queue_len = 5;
200 i = unlink(sockpath);
201 if ((i != 0) && (errno != ENOENT)) {
202 syslog(LOG_ERR, "udsserver: %m");
206 memset(&addr, 0, sizeof(addr));
207 addr.sun_family = AF_UNIX;
208 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
210 s = socket(AF_UNIX, SOCK_STREAM, 0);
212 syslog(LOG_ERR, "udsserver: socket: %m");
216 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
217 syslog(LOG_ERR, "udsserver: bind: %m");
221 // set to nonblock - we need this for some obscure situations
222 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
223 syslog(LOG_ERR, "udsserver: fcntl: %m");
228 if (listen(s, actual_queue_len) < 0) {
229 syslog(LOG_ERR, "udsserver: listen: %m");
233 chmod(sockpath, S_ISGID|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH);
238 // The following functions implement output buffering on operating systems which
239 // support it (such as Linux and various BSD flavors).
242 # define HAVE_TCP_BUFFERING
245 # define HAVE_TCP_BUFFERING
246 # define TCP_CORK TCP_NOPUSH
249 #endif // HAVE_DARWIN
251 static unsigned on = 1, off = 0;
253 void buffer_output(void) {
254 #ifdef HAVE_TCP_BUFFERING
256 if (!CC->redirect_ssl)
258 setsockopt(CC->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
262 void unbuffer_output(void) {
263 #ifdef HAVE_TCP_BUFFERING
265 if (!CC->redirect_ssl)
267 setsockopt(CC->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
271 void flush_output(void) {
272 #ifdef HAVE_TCP_BUFFERING
273 setsockopt(CC->client_socket, IPPROTO_TCP, TCP_CORK, &off, 4);
274 setsockopt(CC->client_socket, IPPROTO_TCP, TCP_CORK, &on, 4);
279 // close the client socket
280 void client_close(void) {
282 if (CC->client_socket <= 0) return;
283 syslog(LOG_DEBUG, "sysdep: closing socket %d", CC->client_socket);
284 close(CC->client_socket);
285 CC->client_socket = -1 ;
289 // Send binary data to the client.
290 int client_write(const char *buf, int nbytes) {
291 int bytes_written = 0;
293 #ifndef HAVE_TCP_BUFFERING
294 int old_buffer_len = 0;
300 if (nbytes < 1) return(0);
303 if (Ctx->redirect_buffer != NULL) {
304 StrBufAppendBufPlain(Ctx->redirect_buffer, buf, nbytes, 0);
309 if (Ctx->redirect_ssl) {
310 client_write_ssl(buf, nbytes);
314 if (Ctx->client_socket == -1) return -1;
316 fdflags = fcntl(Ctx->client_socket, F_GETFL);
318 while ((bytes_written < nbytes) && (Ctx->client_socket != -1)){
319 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
321 FD_SET(Ctx->client_socket, &wset);
322 if (select(1, NULL, &wset, NULL, NULL) == -1) {
323 if (errno == EINTR) {
324 syslog(LOG_DEBUG, "sysdep: client_write(%d bytes) select() interrupted.", nbytes-bytes_written);
325 if (server_shutting_down) {
326 CC->kill_me = KILLME_SELECT_INTERRUPTED;
330 // can't trust fd's and stuff so we need to re-create them
335 syslog(LOG_ERR, "sysdep: client_write(%d bytes) select failed: %m", nbytes - bytes_written);
337 Ctx->kill_me = KILLME_SELECT_FAILED;
343 retval = write(Ctx->client_socket, &buf[bytes_written], nbytes - bytes_written);
345 syslog(LOG_ERR, "sysdep: client_write(%d bytes) failed: %m", nbytes - bytes_written);
347 Ctx->kill_me = KILLME_WRITE_FAILED;
350 bytes_written = bytes_written + retval;
356 void cputbuf(const StrBuf *Buf) {
357 client_write(ChrPtr(Buf), StrLength(Buf));
361 // Send formatted printable data to the client.
362 // Implemented in terms of client_write() so it's technically not sysdep...
363 void cprintf(const char *format, ...) {
368 va_start(arg_ptr, format);
369 rc = vsnprintf(buf, sizeof buf, format, arg_ptr);
371 syslog(LOG_ERR,"Console output error occured. Format: %s",format);
373 if (rc >= sizeof buf) {
374 // Output was truncated.
375 // Chop at the end of the buffer
376 buf[sizeof buf - 2] = '\n';
377 buf[sizeof buf - 1] = 0;
379 client_write(buf, strlen(buf));
385 // Read data from the client socket.
387 // sock socket fd to read from
388 // buf buffer to read into
389 // bytes number of bytes to read
390 // timeout Number of seconds to wait before timing out
392 // Possible return values:
393 // 1 Requested number of bytes has been read.
394 // 0 Request timed out.
395 // -1 Connection is broken, or other error.
396 int client_read_blob(StrBuf *Target, int bytes, int timeout) {
401 if (CC->redirect_ssl) {
402 retval = client_read_sslblob(Target, bytes, timeout);
404 syslog(LOG_ERR, "sysdep: client_read_blob() failed");
410 retval = StrBufReadBLOBBuffered(Target,
412 &CC->RecvBuf.ReadWritePointer,
420 syslog(LOG_ERR, "sysdep: client_read_blob() failed: %s", Error);
429 // to make client_read_random_blob() more efficient, increase buffer size.
430 // just use in greeting function, else your buffer may be flushed
431 void client_set_inbound_buf(long N) {
432 FlushStrBuf(CC->RecvBuf.Buf);
433 ReAdjustEmptyBuf(CC->RecvBuf.Buf, N * SIZ, N * SIZ);
437 int client_read_random_blob(StrBuf *Target, int timeout) {
440 rc = client_read_blob(Target, 1, timeout);
445 len = StrLength(CC->RecvBuf.Buf);
446 pch = ChrPtr(CC->RecvBuf.Buf);
449 if (CC->RecvBuf.ReadWritePointer != NULL) {
450 len -= CC->RecvBuf.ReadWritePointer - pch;
451 pch = CC->RecvBuf.ReadWritePointer;
453 StrBufAppendBufPlain(Target, pch, len, 0);
454 FlushStrBuf(CC->RecvBuf.Buf);
455 CC->RecvBuf.ReadWritePointer = NULL;
456 return StrLength(Target);
465 int client_read_to(char *buf, int bytes, int timeout) {
468 rc = client_read_blob(CC->MigrateBuf, bytes, timeout);
475 ChrPtr(CC->MigrateBuf),
476 StrLength(CC->MigrateBuf) + 1);
477 FlushStrBuf(CC->MigrateBuf);
483 int HaveMoreLinesWaiting(CitContext *ctx) {
484 if ( (ctx->kill_me != 0)
485 || ( (ctx->RecvBuf.ReadWritePointer == NULL)
486 && (StrLength(ctx->RecvBuf.Buf) == 0)
487 && (ctx->client_socket != -1))
495 // Read data from the client socket with default timeout.
496 // (This is implemented in terms of client_read_to() and could be
497 // justifiably moved out of sysdep.c)
498 INLINE int client_read(char *buf, int bytes) {
499 return(client_read_to(buf, bytes, CtdlGetConfigInt("c_sleeping")));
503 int CtdlClientGetLine(StrBuf *Target) {
509 if (CC->redirect_ssl) {
510 rc = client_readline_sslbuffer(Target, CC->RecvBuf.Buf, &CC->RecvBuf.ReadWritePointer, 1);
516 rc = StrBufTCP_read_buffered_line_fast(Target,
518 &CC->RecvBuf.ReadWritePointer,
529 // Get a LF-terminated line of text from the client.
530 // (This is implemented in terms of client_read() and could be
531 // justifiably moved out of sysdep.c)
532 int client_getln(char *buf, int bufsize) {
536 retval = CtdlClientGetLine(CC->MigrateBuf);
541 i = StrLength(CC->MigrateBuf);
542 pCh = ChrPtr(CC->MigrateBuf);
543 // Strip the trailing LF, and the trailing CR if present.
547 && ( (pCh[i - 1]==13)
548 || ( pCh[i - 1]==10)) ) {
554 FlushStrBuf(CC->MigrateBuf);
556 safestrncpy(&buf[i], "000", bufsize - i);
562 // Generic routine to convert a login name to a full name (gecos)
563 // Returns nonzero if a conversion took place
564 int convert_login(char NameToConvert[]) {
568 pw = getpwnam(NameToConvert);
573 strcpy(NameToConvert, pw->pw_gecos);
574 for (a=0; a<strlen(NameToConvert); ++a) {
575 if (NameToConvert[a] == ',') NameToConvert[a] = 0;
582 void HuntBadSession(void) {
587 struct ServiceFunctionHook *serviceptr;
589 // Next, add all of the client sockets
590 begin_critical_section(S_SESSION_TABLE);
591 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
592 if ((ptr->state == CON_SYS) && (ptr->client_socket == 0))
594 // Initialize the fdset.
597 tv.tv_sec = 0; // wake up every second if no input
600 // Don't select on dead sessions, only truly idle ones
601 if ( (ptr->state == CON_IDLE)
602 && (ptr->kill_me == 0)
603 && (ptr->client_socket > 0)
605 FD_SET(ptr->client_socket, &readfds);
606 if (ptr->client_socket > highest)
607 highest = ptr->client_socket;
609 if ((select(highest + 1, &readfds, NULL, NULL, &tv) < 0) && (errno == EBADF)) {
612 "sysdep: killing session CC[%d] bad FD: [%d] User[%s] Host[%s:%s]",
620 ptr->client_socket = -1;
625 end_critical_section(S_SESSION_TABLE);
627 // First, add the various master sockets to the fdset.
628 for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next ) {
630 // Initialize the fdset.
632 tv.tv_sec = 0; // wake up every second if no input
635 FD_SET(serviceptr->msock, &readfds);
636 if (serviceptr->msock > highest) {
637 highest = serviceptr->msock;
639 if ((select(highest + 1, &readfds, NULL, NULL, &tv) < 0) && (errno == EBADF)) {
640 // Gotcha! server socket dead? commit suicide!
641 syslog(LOG_ERR, "sysdep: found bad FD: %d and its a server socket! Shutting Down!", serviceptr->msock);
642 server_shutting_down = 1;
650 // select() can wake up on any of the following conditions:
651 // 1. A new client connection on a master socket
652 // 2. Received data on a client socket
654 // That's why we are blocking on select() and not accept().
655 void *worker_thread(void *blah) {
658 CitContext *bind_me = NULL;
663 struct ServiceFunctionHook *serviceptr;
664 int ssock; // Descriptor for client socket
665 CitContext *con = NULL; // Temporary context pointer
668 pthread_mutex_lock(&ThreadCountMutex);
670 pthread_mutex_unlock(&ThreadCountMutex);
672 while (!server_shutting_down) {
674 // make doubly sure we're not holding any stale db handles which might cause a deadlock
676 do_select: force_purge = 0;
677 bind_me = NULL; // Which session shall we handle?
679 // Initialize the fdset
683 // First, add the various master sockets to the fdset.
684 for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next ) {
685 FD_SET(serviceptr->msock, &readfds);
686 if (serviceptr->msock > highest) {
687 highest = serviceptr->msock;
691 // Next, add all of the client sockets.
692 begin_critical_section(S_SESSION_TABLE);
693 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
694 if ((ptr->state == CON_SYS) && (ptr->client_socket == 0))
697 // Don't select on dead sessions, only truly idle ones
698 if ( (ptr->state == CON_IDLE)
699 && (ptr->kill_me == 0)
700 && (ptr->client_socket > 0)
702 FD_SET(ptr->client_socket, &readfds);
703 if (ptr->client_socket > highest)
704 highest = ptr->client_socket;
706 if ((bind_me == NULL) && (ptr->state == CON_READY)) {
708 ptr->state = CON_EXECUTING;
711 if ((bind_me == NULL) && (ptr->state == CON_GREETING)) {
713 ptr->state = CON_STARTING;
717 end_critical_section(S_SESSION_TABLE);
719 if (bind_me) { // don't search for a session to bind to.
720 goto SKIP_SELECT; // we already found one.
723 // If we got this far, it means that there are no sessions
724 // which a previous thread marked for attention, so we go
725 // ahead and get ready to select().
727 if (!server_shutting_down) {
728 tv.tv_sec = 1; // wake up every second if no input
730 retval = select(highest + 1, &readfds, NULL, NULL, &tv);
737 // Now figure out who made this select() unblock.
738 // First, check for an error or exit condition.
740 if (errno == EBADF) {
741 syslog(LOG_ERR, "sysdep: select() failed: %m");
745 if (errno != EINTR) {
746 syslog(LOG_ERR, "sysdep: exiting: %m");
747 server_shutting_down = 1;
750 if (server_shutting_down) {
757 else if (retval == 0) {
758 if (server_shutting_down) {
764 // Next, check to see if it's a new client connecting on a master socket.
766 else if ((retval > 0) && (!server_shutting_down)) for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next) {
768 if (FD_ISSET(serviceptr->msock, &readfds)) {
769 ssock = accept(serviceptr->msock, NULL, 0);
771 syslog(LOG_DEBUG, "sysdep: new client socket %d", ssock);
773 // The master socket is non-blocking but the client
774 // sockets need to be blocking, otherwise certain
775 // operations barf on FreeBSD. Not a fatal error.
776 if (fcntl(ssock, F_SETFL, 0) < 0) {
777 syslog(LOG_ERR, "sysdep: Can't set socket to blocking: %m");
780 // New context will be created already
781 // set up in the CON_EXECUTING state.
782 con = CreateNewContext();
784 // Assign our new socket number to it.
785 con->tcp_port = serviceptr->tcp_port;
786 con->client_socket = ssock;
787 con->h_command_function = serviceptr->h_command_function;
788 con->h_async_function = serviceptr->h_async_function;
789 con->h_greeting_function = serviceptr->h_greeting_function;
790 con->ServiceName = serviceptr->ServiceName;
792 // Connections on a local client are always from the same host
793 if (serviceptr->sockpath != NULL) {
794 con->is_local_client = 1;
797 // Set the SO_REUSEADDR socket option
799 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
800 con->state = CON_GREETING;
808 // It must be a client socket. Find a context that has data
809 // waiting on its socket *and* is in the CON_IDLE state. Any
810 // active sockets other than our chosen one are marked as
811 // CON_READY so the next thread that comes around can just bind
812 // to one without having to select() again.
813 begin_critical_section(S_SESSION_TABLE);
814 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
815 int checkfd = ptr->client_socket;
816 if ((checkfd != -1) && (ptr->state == CON_IDLE)){
817 if (FD_ISSET(checkfd, &readfds)) {
818 ptr->input_waiting = 1;
820 bind_me = ptr; // I choose you!
821 bind_me->state = CON_EXECUTING;
824 ptr->state = CON_READY;
827 else if ((ptr->is_async) && (ptr->async_waiting) && (ptr->h_async_function)) {
829 bind_me = ptr; // I choose you!
830 bind_me->state = CON_EXECUTING;
833 ptr->state = CON_READY;
838 end_critical_section(S_SESSION_TABLE);
841 // We're bound to a session
842 pthread_mutex_lock(&ThreadCountMutex);
844 pthread_mutex_unlock(&ThreadCountMutex);
846 if (bind_me != NULL) {
847 become_session(bind_me);
849 if (bind_me->state == CON_STARTING) {
850 bind_me->state = CON_EXECUTING;
851 begin_session(bind_me);
852 bind_me->h_greeting_function();
854 // If the client has sent a command, execute it.
855 if (CC->input_waiting) {
856 CC->h_command_function();
858 while (HaveMoreLinesWaiting(CC))
859 CC->h_command_function();
861 CC->input_waiting = 0;
864 // If there are asynchronous messages waiting and the client supports it, do those now
865 if ((CC->is_async) && (CC->async_waiting) && (CC->h_async_function != NULL)) {
866 CC->h_async_function();
867 CC->async_waiting = 0;
870 force_purge = CC->kill_me;
871 become_session(NULL);
872 bind_me->state = CON_IDLE;
875 dead_session_purge(force_purge);
878 pthread_mutex_lock(&ThreadCountMutex);
880 pthread_mutex_unlock(&ThreadCountMutex);
883 // If control reaches this point, the server is shutting down
889 // Translate text facility name to syslog.h defined value.
890 int SyslogFacility(char *name) {
897 { LOG_KERN, "kern" },
898 { LOG_USER, "user" },
899 { LOG_MAIL, "mail" },
900 { LOG_DAEMON, "daemon" },
901 { LOG_AUTH, "auth" },
902 { LOG_SYSLOG, "syslog" },
904 { LOG_NEWS, "news" },
905 { LOG_UUCP, "uucp" },
906 { LOG_LOCAL0, "local0" },
907 { LOG_LOCAL1, "local1" },
908 { LOG_LOCAL2, "local2" },
909 { LOG_LOCAL3, "local3" },
910 { LOG_LOCAL4, "local4" },
911 { LOG_LOCAL5, "local5" },
912 { LOG_LOCAL6, "local6" },
913 { LOG_LOCAL7, "local7" },
916 for(i = 0; facTbl[i].name != NULL; i++) {
917 if(!strcasecmp(name, facTbl[i].name))
918 return facTbl[i].facility;