8 #include <sys/socket.h>
12 #include <arpa/inet.h>
36 #include "socket_if.h"
42 #include "dnsresolv.h"
52 #define WHO "sslserver"
70 static stralloc tcpremoteinfo;
73 char iplocal[16] = { 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 };
77 static stralloc localhostsa;
84 static stralloc remotehostsa;
91 unsigned long uid = 0;
92 unsigned long gid = 0;
94 static char strnum[FMT_ULONG];
95 static char strnum2[FMT_ULONG];
96 static char strnum3[FMT_ULONG];
100 static stralloc addresses;
112 logmsg(
WHO,111,FATAL,
"out of memory");
123 GEN_ALLOC_readyplus(child_alloc,
struct child,c,len,a,i,n,x,24,child_readyplus)
124 GEN_ALLOC_append(child_alloc,
struct child,c,len,a,i,n,x,24,child_readyplus,child_append)
126 child_alloc children = {0};
130 struct child *ipchild = 0;
133 for (i = 0; i <= n; ++i) {
134 ipchild = &children.c[i];
135 if (byte_equal(ipchild->
ipaddr,16,ip)) {
139 byte_copy(ipchild->
ipaddr,16,ip);
148 struct child *ipchild = 0;
151 for (i = 0; i <= children.len; ++i) {
152 ipchild = &children.c[i];
153 if (byte_equal(ipchild->
ipaddr,16,ip)) {
154 if (ipchild->
num) --ipchild->
num;
164 for (i = 0; i <= n; ++i)
165 if (byte_equal(children.c[i].ipaddr,16,ip))
166 return children.c[i].num;
216 for (i = 0;i < 100;++i) {
219 if (ch < 33) ch =
'?';
220 if (ch > 126) ch =
'?';
221 if (ch ==
'%') ch =
'?';
227 void env(
const char *s,
const char *t) {
232 logmsg(
WHO,110,FATAL,B(
"unable to read: ",(
char *)
fnrules));
235 void found(
char *data,
unsigned int datalen) {
241 while ((next0 = byte_chr(data,datalen,0)) < datalen) {
247 split = str_chr(data + 1,
'=');
248 if (data[1 + split] ==
'=') {
250 env(data + 1,data + 1 + split + 1);
251 if (!str_diff(data + 1,
"MAXCONIP")) {
252 scan_ulong(data + 1 + split + 1,&
maxconip);
260 data += next0; datalen -= next0;
270 unsigned long tmp_long;
272 stralloc ssl_env = {0};
277 stralloc tlsinfo = {0};
279 if (pipe(
pi) == -1) logmsg(
WHO,111,FATAL,
"unable to create pipe");
280 if (pipe(
po) == -1) logmsg(
WHO,111,FATAL,
"unable to create pipe");
281 if (socketpair(AF_UNIX,SOCK_STREAM,0,sslctl) == -1)
282 logmsg(
WHO,111,FATAL,
"unable to create socketpair");
297 if (dns_name(&remotehostsa,
remoteip) >= 0)
298 if (remotehostsa.len) {
301 if (dns_ip6(&tmp,&remotehostsa) >= 0)
302 for (j = 0; j + 16 <= tmp.len; j += 16)
303 if (byte_equal(
remoteip,16,tmp.s + j)) {
307 if (dns_ip4(&tmp,&remotehostsa) >= 0)
308 for (j = 0; j + 4 <= tmp.len; j += 4)
309 if (byte_equal(
remoteip+12,4,tmp.s + j)) {
324 if (!stralloc_0(&tcpremoteinfo))
drop_nomem();
344 switch(childpid = fork()) {
346 logmsg(
WHO,111,FATAL,
"unable to fork");
354 close(
pi[0]); close(
po[1]); close(sslctl[1]);
356 if ((s = env_get(
"SSL_CHROOT")))
357 if (chroot(s) == -1) {
358 kill(childpid,SIGTERM);
359 logmsg(
WHO,111,FATAL,
"unable to chroot");
362 if ((s = env_get(
"SSL_GID"))) {
363 scan_ulong(s,&tmp_long);
366 if (
gid)
if (prot_gid(
gid) == -1) {
367 kill(childpid,SIGTERM);
368 logmsg(
WHO,111,FATAL,
"unable to set gid");
371 if ((s = env_get(
"SSL_UID"))) {
372 scan_ulong(s,&tmp_long);
376 if (prot_uid(
uid) == -1) {
377 kill(childpid,SIGTERM);
378 logmsg(
WHO,111,FATAL,
"unable to set uid");
383 if (read(sslctl[0],&ssl_cmd,1) == 1) {
386 kill(childpid,SIGTERM);
387 logmsg(
WHO,111,FATAL,
"unable to create TLS instance");
389 if (ndelay_on(t) == -1) {
390 kill(childpid,SIGTERM);
391 logmsg(
WHO,111,FATAL,
"unable to set socket options");
394 strnum[fmt_ulong(strnum,childpid)] = 0;
395 kill(childpid,SIGTERM);
396 logmsg(
WHO,111,ERROR,B(
"unable to accept TLS for pid: ",strnum,
" ",ERR_reason_error_string(
ssl_errno)));
398 }
else if (errno == EAGAIN) {
399 strnum[fmt_ulong(strnum,childpid)] = 0;
400 kill(childpid,SIGTERM);
410 kill(childpid,SIGTERM);
411 logmsg(
WHO,110,ERROR,B(
"no client certificate for pid: ",strnum));
413 kill(childpid,SIGTERM);
414 logmsg(
WHO,110,ERROR,B(
"missing credentials (CA) or unable to validate client certificate for pid: ",strnum));
416 kill(childpid,SIGTERM);
418 logmsg(
WHO,110,ERROR,B(
"client hostname does not match certificate for pid: ",strnum,
" ",
verifyhost,
" <=> ",
certname.s));
421 strnum[fmt_ulong(strnum,childpid)] = 0;
422 logmsg(
WHO,0,INFO,B(
"valid client cert received for: ",strnum));
430 if (ssl_cmd ==
'Y') {
433 env(
"SSLCTL",ssl_env.s);
435 for (bytesleft = ssl_env.len; bytesleft > 0; bytesleft -= j)
436 if ((j = write(sslctl[0],ssl_env.s,bytesleft)) < 0) {
437 kill(childpid,SIGTERM);
438 logmsg(
WHO,111,FATAL,
"unable to write TLS environment");
443 strnum[fmt_ulong(strnum,childpid)] = 0;
446 if ((j = str_chr(s,
'=')))
447 if (!stralloc_copys(&tlsinfo,s + j + 1))
drop_nomem();
448 if (!stralloc_cats(&tlsinfo,
":"))
drop_nomem();
449 s = s + str_len(s) + 1;
450 s = s + str_len(s) + 1;
451 if ((j = str_chr(s,
'=')))
452 if (!stralloc_cats(&tlsinfo,s + j + 1))
drop_nomem();
454 log_who(
WHO,B(
"tls ",strnum,
" accept ",tlsinfo.s));
456 log_who(
WHO,B(
"tls ",strnum,
" accept"));
459 if (ssl_cmd ==
'Y' || ssl_cmd ==
'y') {
461 strnum[fmt_ulong(strnum,childpid)] = 0;
462 kill(childpid,SIGTERM);
463 logmsg(
WHO,111,ERROR,B(
"unable to speak TLS for pid: ",strnum,
" ",ERR_reason_error_string(
ssl_errno)));
465 if (wait_nohang(&wstat) > 0)
466 _exit(wait_exitcode(wstat));
469 kill(childpid,SIGTERM);
476 logmsg(
WHO,111,FATAL,
"unable to get local address");
487 strnum[fmt_ulong(strnum,getpid())] = 0;
492 if (dns_name(&localhostsa,
localip) >= 0)
493 if (localhostsa.len) {
527 env(
"TCP6INTERFACE",socket_getifname(
netif));
536 strnum[fmt_ulong(strnum,getpid())] = 0;
537 strnum2[fmt_ulong(strnum2,
maxconip)] = 0;
538 if (!stralloc_copys(&tmp,
"sslserver: "))
drop_nomem();
550 buffer_putflush(buffer_2,tmp.s,tmp.len);
555 if (
gid)
if (prot_gid(
gid) == -1)
556 logmsg(
WHO,111,FATAL,
"unable to set gid");
557 if (
uid)
if (prot_uid(
uid) == -1)
558 logmsg(
WHO,111,FATAL,
"unable to set uid");
560 close(
pi[1]); close(
po[0]); close(sslctl[0]);
562 sig_uncatch(sig_child);
563 sig_unblock(sig_child);
564 sig_uncatch(sig_term);
565 sig_uncatch(sig_pipe);
567 if (fcntl(sslctl[1],F_SETFD,0) == -1)
568 logmsg(
WHO,111,FATAL,
"unable to clear close-on-exec flag");
569 strnum[fmt_ulong(strnum,sslctl[1])] = 0;
570 env(
"SSLCTLFD",strnum);
572 if (fcntl(
pi[0],F_SETFD,0) == -1)
573 logmsg(
WHO,111,FATAL,
"unable to clear close-on-exec flag");
574 strnum[fmt_ulong(strnum,
pi[0])] = 0;
575 env(
"SSLREADFD",strnum);
577 if (fcntl(
po[1],F_SETFD,0) == -1)
578 logmsg(
WHO,111,FATAL,
"unable to clear close-on-exec flag");
579 strnum[fmt_ulong(strnum,
po[1])] = 0;
580 env(
"SSLWRITEFD",strnum);
583 if (fd_copy(0,t) == -1)
584 logmsg(
WHO,111,FATAL,
"unable to set up descriptor 0");
585 if (fd_copy(1,t) == -1)
586 logmsg(
WHO,111,FATAL,
"unable to set up descriptor 1");
588 if (fd_move(0,
pi[0]) == -1)
589 logmsg(
WHO,111,FATAL,
"unable to set up descriptor 0");
590 if (fd_move(1,
po[1]) == -1)
591 logmsg(
WHO,111,FATAL,
"unable to set up descriptor 1");
595 socket_ipoptionskill(t);
598 socket_tcpnodelay(t);
602 if (buffer_putsflush(&
bo,
banner) == -1)
603 logmsg(
WHO,111,FATAL,
"unable to print banner");
608 if (write(sslctl[1],&ssl_cmd,1) < 1)
609 logmsg(
WHO,111,FATAL,
"unable to start TLS");
611 while ((j = read(sslctl[1],envbuf,8192)) > 0) {
612 stralloc_catb(&ssl_env,envbuf,j);
613 if (ssl_env.len >= 2 && ssl_env.s[ssl_env.len-2] == 0 && ssl_env.s[ssl_env.len-1] == 0)
617 logmsg(
WHO,111,FATAL,
"unable to read TLS environment");
618 pathexec_multienv(&ssl_env);
623 logmsg(
WHO,111,FATAL,B(
"unable to run: ",*
prog));
630 logmsg(
WHO,100,USAGE,
"sslserver \
631 [ -1346UXpPhHrRoOdDqQvVIeEsSnNmzZ ] \
654 strnum2[fmt_ulong(strnum2,
limit)] = 0;
655 strnum3[fmt_ulong(strnum3,
maxconip)] = 0;
656 log_who(
WHO,B(
"status: ",strnum,
"/",strnum2,
"/",strnum3));
669 while ((pid = wait_nohang(&wstat)) > 0) {
671 strnum[fmt_ulong(strnum,pid)] = 0;
672 strnum2[fmt_ulong(strnum2,wstat)] = 0;
673 log_who(
WHO,B(
"ended by ",strnum,
" status ",strnum2));
685 logmsg(
WHO,111,ERROR,
"unable to read password");
691 int passwd_cb(
char *buff,
int size,
int rwflag,
void *userdata) {
693 logmsg(
WHO,111,ERROR,
"password too long");
699 int main(
int argc,
char *
const *argv) {
709 while ((opt = getopt(argc,argv,
"1346dDvVqQhHrRUXx:t:T:u:g:l:b:B:c:pPoOIEeSsw:nNzZm")) != opteof)
711 case 'b': scan_ulong(optarg,&
backlog);
break;
712 case 'c': scan_ulong(optarg,&
limit);
break;
714 case 'x':
fnrules = optarg;
break;
715 case 'B':
banner = optarg;
break;
730 case 't': scan_ulong(optarg,&
timeout);
break;
731 case 'T': scan_ulong(optarg,&
ssltimeout);
break;
733 case 'U': x = env_get(
"UID");
if (x) scan_ulong(x,&
uid);
734 x = env_get(
"GID");
if (x) scan_ulong(x,&
gid);
break;
735 case 'u': scan_ulong(optarg,&
uid);
break;
736 case 'g': scan_ulong(optarg,&
gid);
break;
737 case 'I':
netif = socket_getifidx(optarg);
break;
739 case '1':
flag1 = 1;
break;
740 case '3':
flag3 = 1;
break;
741 case '4': ipflag = 1;
break;
742 case '6': ipflag = 2;
break;
762 else if (str_equal((
char *)
hostname,
":0")) {
771 if (!x[scan_ulong(x,&u)])
774 se = getservbyname(x,
"tcp");
776 logmsg(
WHO,111,FATAL,B(
"unable to figure out port number for: ",x));
777 uint16_unpack_big((
char *)&se->s_port,&
localport);
780 if ((x = env_get(
"MAXCONIP"))) { scan_ulong(x,&u);
maxconip = u; }
784 if ((x = env_get(
"VERIFYDEPTH"))) { scan_ulong(x,&u);
verifydepth = u; }
786 if ((x = env_get(
"CAFILE")))
cafile = x;
789 if ((x = env_get(
"CCAFILE")))
ccafile = x;
794 if ((x = env_get(
"CADIR")))
cadir = x;
800 if ((x = env_get(
"CERTFILE")))
certfile = x;
803 if ((x = env_get(
"KEYFILE")))
keyfile = x;
806 if ((x = env_get(
"DHFILE")))
dhfile = x;
809 if ((x = env_get(
"CIPHERS")))
ciphers = x;
812 sig_block(sig_child);
815 sig_ignore(sig_pipe);
820 if (!stralloc_copyb(&addresses,(
char *)V4mappedprefix,12))
drop_nomem();
822 byte_copy(
localip,16,addresses.s);
825 byte_copy(
localip,16,addresses.s);
830 if (!addresses.len) {
832 if (dns_ip_qualify(&addresses,&fqdn,&tmp) < 0)
833 logmsg(
WHO,111,FATAL,B(
"temporarily unable to figure out IP address for: ",(
char *)
hostname));
835 byte_copy(
localip,16,addresses.s);
837 for (j = 0; j < addresses.len; j += 16) {
838 if (ipflag == 1 && !ip6_isv4mapped(addresses.s + j))
continue;
839 if (ipflag == 2 && !ip6_isv4mapped(addresses.s + j))
continue;
840 byte_copy(
localip,16,addresses.s + j);
844 if (addresses.len < 16)
845 logmsg(
WHO,111,FATAL,B(
"no IP address for: ",(
char *)
hostname));
852 logmsg(
WHO,111,FATAL,
"unable to create socket");
856 logmsg(
WHO,111,FATAL,
"unable to bind");
858 logmsg(
WHO,111,FATAL,
"unable to get local address");
859 if (socket_listen(s,
backlog) == -1)
860 logmsg(
WHO,111,FATAL,
"unable to listen");
872 buffer_puts(&
bo,
" : ");
874 buffer_puts(&
bo,
"\n");
882 if (!
ctx) logmsg(
WHO,111,FATAL,
"unable to create TLS context");
886 case -1: logmsg(
WHO,111,ERROR,
"unable to load certificate chain file");
887 case -2: logmsg(
WHO,111,ERROR,
"unable to load key");
888 case -3: logmsg(
WHO,111,ERROR,
"key does not match certificate");
893 case -1: logmsg(
WHO,111,ERROR,
"unable to load certificate");
894 case -2: logmsg(
WHO,111,ERROR,
"unable to load key");
895 case -3: logmsg(
WHO,111,ERROR,
"key does not match certificate");
901 logmsg(
WHO,111,ERROR,
"unable to load CA list");
903 logmsg(
WHO,111,ERROR,
"unable to load client CA list");
905 logmsg(
WHO,111,ERROR,
"unable to set RSA parameters");
907 logmsg(
WHO,111,ERROR,
"unable to set DH parameters");
909 logmsg(
WHO,111,ERROR,
"unable to set cipher list");
912 strnum[fmt_ulong(strnum,getpid())] = 0;
913 strnum2[fmt_ulong(strnum2,
rsalen)] = 0;
914 log_who(
WHO,B(
"ciphers ",strnum,
" ",(
char *)
ciphers));
915 log_who(
WHO,B(
"cafile ",strnum,
" ",(
char *)
cafile));
916 log_who(
WHO,B(
"ccafile ",strnum,
" ",(
char *)
ccafile));
917 log_who(
WHO,B(
"cadir ",strnum,
" ",(
char *)
cadir));
919 log_who(
WHO,B(
"cert ",strnum,
" ",(
char *)
certfile));
920 log_who(
WHO,B(
"key ",strnum,
" ",(
char *)
keyfile));
921 log_who(
WHO,B(
"dhparam ",strnum,
" ",(
char *)
dhfile,
" ",strnum2));
924 close(0); open_read(
"/dev/null");
925 close(1); open_append(
"/dev/null");
933 sig_unblock(sig_child);
935 sig_block(sig_child);
936 if (t == -1)
continue;
946 strnum[fmt_ulong(strnum,
maxconip)] = 0;
947 logmsg(
WHO,100,WARN,B(
"ip connection limit of ",strnum,
" exceeded for: ",
remoteipstr));
960 logmsg(
WHO,111,FATAL,B(
"unable to run: ",*argv));
963 logmsg(
WHO,111,FATAL,B(
"unable to fork: ",strnum));
const char auto_ccafile[]
const char auto_certchainfile[]
const char auto_certfile[]
const char auto_ciphers[]
const char auto_keyfile[]
int remoteinfo(stralloc *out, char ipremote[16], uint16 portremote, char iplocal[16], uint16 portlocal, unsigned int timeout, uint32 netif)
int rules(void(*callback)(char *, unsigned int), int fd, char *ip, char *host, char *info)
int ssl_ca(SSL_CTX *ctx, const char *certfile, const char *certdir, int d)
int ssl_cca(SSL_CTX *ctx, const char *certfile)
int ssl_certkey(SSL_CTX *ctx, const char *certfile, const char *keyfile, pem_password_cb *passwd_cb)
int ssl_chainfile(SSL_CTX *ctx, const char *certchainfile, const char *keyfile, pem_password_cb *passwd_cb)
int ssl_ciphers(SSL_CTX *ctx, const char *ciphers)
int ssl_server_env(SSL *ssl, stralloc *sa)
int ssl_io(SSL *ssl, int fdleft, int fdright, unsigned int timeout)
SSL * ssl_new(SSL_CTX *ctx, int s)
int ssl_params_dh(SSL_CTX *ctx, const char *dhfile)
int ssl_params_rsa(SSL_CTX *ctx, int len)
int ssl_timeoutaccept(SSL *ssl, unsigned int timeout)
int ssl_verify(SSL *ssl, const char *hostname, stralloc *dnsout)
void ipchild_append(char ip[16], unsigned long n)
void found(char *data, unsigned int datalen)
char remoteportstr[FMT_ULONG]
char localportstr[FMT_ULONG]
char remoteipstr[IP6_FMT]
const char * certchainfile
void env(const char *s, const char *t)
unsigned long numchildren
GEN_ALLOC_typedef(GEN_ALLOC_readyplus(child_alloc, GEN_ALLOC_readyplus(struct child, GEN_ALLOC_readyplus(c, GEN_ALLOC_readyplus(len, GEN_ALLOC_readyplus(a)
void append(const char *ch)
void safecats(const char *s)
void ipchild_clear(char ip[16])
int main(int argc, char *const *argv)
int passwd_cb(char *buff, int size, int rwflag, void *userdata)
int ipchild_limit(char ip[16], unsigned long n)
Header file to be used with sqmail; previously called ssl.h. (name clash)