52#define WHO "sslserver"
70static stralloc tcpremoteinfo;
73char iplocal[16] = { 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 };
77static stralloc localhostsa;
84static stralloc remotehostsa;
94static char strnum[FMT_ULONG];
95static char strnum2[FMT_ULONG];
96static char strnum3[FMT_ULONG];
100static stralloc addresses;
112 logmsg(
WHO,111,FATAL,
"out of memory");
123GEN_ALLOC_readyplus(child_alloc,
struct child,c,len,a,i,n,x,24,child_readyplus)
124GEN_ALLOC_append(child_alloc,
struct child,c,len,a,i,n,x,24,child_readyplus,child_append)
126child_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;
220 for (i = 0;i < 100;++i) {
223 if (ch < 33) ch =
'?';
224 if (ch > 126) ch =
'?';
225 if (ch ==
'%') ch =
'?';
231void env(
const char *s,
const char *t)
238 logmsg(
WHO,110,FATAL,B(
"unable to read: ",fnbase));
241void found(
char *data,
unsigned int datalen)
248 while ((next0 = byte_chr(data,datalen,0)) < datalen) {
255 split = str_chr(data + 1,
'=');
256 if (data[1 + split] ==
'=') {
258 env(data + 1,data + 1 + split + 1);
259 if (!str_diff(data + 1,
"MAXCONIP")) {
260 scan_ulong(data + 1 + split + 1,&
maxconip);
268 data += next0; datalen -= next0;
279 unsigned long tmp_long;
281 stralloc ssl_env = {0};
286 stralloc tlsinfo = {0};
288 if (pipe(
pi) == -1) logmsg(
WHO,111,FATAL,
"unable to create pipe");
289 if (pipe(
po) == -1) logmsg(
WHO,111,FATAL,
"unable to create pipe");
290 if (socketpair(AF_UNIX,SOCK_STREAM,0,sslctl) == -1)
291 logmsg(
WHO,111,FATAL,
"unable to create socketpair");
324 if (dns_name(&remotehostsa,
remoteip) >= 0)
325 if (remotehostsa.len) {
328 if (dns_ip6(&tmp,&remotehostsa) >= 0)
329 for (j = 0; j + 16 <= tmp.len; j += 16)
330 if (byte_equal(
remoteip,16,tmp.s + j)) {
334 if (dns_ip4(&tmp,&remotehostsa) >= 0)
335 for (j = 0; j + 4 <= tmp.len; j += 4)
336 if (byte_equal(
remoteip + 12,4,tmp.s + j)) {
351 if (!stralloc_0(&tcpremoteinfo))
drop_nomem();
371 switch (childpid = fork()) {
373 logmsg(
WHO,111,FATAL,
"unable to fork");
381 close(
pi[0]); close(
po[1]); close(sslctl[1]);
383 if ((s = env_get(
"SSL_CHROOT")))
384 if (chroot(s) == -1) {
385 kill(childpid,SIGTERM);
386 logmsg(
WHO,111,FATAL,
"unable to chroot");
389 if ((s = env_get(
"SSL_GID"))) {
390 scan_ulong(s,&tmp_long);
393 if (
gid)
if (prot_gid(
gid) == -1) {
394 kill(childpid,SIGTERM);
395 logmsg(
WHO,111,FATAL,
"unable to set gid");
398 if ((s = env_get(
"SSL_UID"))) {
399 scan_ulong(s,&tmp_long);
403 if (prot_uid(
uid) == -1) {
404 kill(childpid,SIGTERM);
405 logmsg(
WHO,111,FATAL,
"unable to set uid");
417 if (read(sslctl[0],&ssl_cmd,1) == 1) {
420 kill(childpid,SIGTERM);
421 logmsg(
WHO,111,FATAL,
"unable to create TLS instance");
423 if (ndelay_on(t) == -1) {
424 kill(childpid,SIGTERM);
425 logmsg(
WHO,111,FATAL,
"unable to set socket options");
428 strnum[fmt_ulong(strnum,childpid)] = 0;
429 kill(childpid,SIGTERM);
430 logmsg(
WHO,111,FATAL,B(
"unable to accept TLS from: ",
remoteipstr,
" for pid: ",strnum,
" ",ERR_reason_error_string(ERR_peek_last_error())));
432 }
else if (errno == EAGAIN) {
433 strnum[fmt_ulong(strnum,childpid)] = 0;
434 kill(childpid,SIGTERM);
441 strnum[fmt_ulong(strnum,childpid)] = 0;
445 kill(childpid,SIGTERM);
446 logmsg(
WHO,110,ERROR,B(
"no client certificate from: ",
remoteipstr,
" for pid: ",strnum));
448 kill(childpid,SIGTERM);
449 logmsg(
WHO,110,ERROR,B(
"missing credentials (CA) or unable to validate client certificate from: ",
remoteipstr,
" for pid: ",strnum));
451 kill(childpid,SIGTERM);
453 logmsg(
WHO,110,ERROR,B(
"client hostname does not match certificate for pid: ",strnum,
" ",
verifyhost,
" <=> ",
certfqdn.s));
456 strnum[fmt_ulong(strnum,childpid)] = 0;
457 logmsg(
WHO,0,INFO,B(
"valid client cert received from: ",
remoteipstr,
" for pid: ",strnum));
465 if (ssl_cmd ==
'Y') {
468 env(
"SSLCTL",ssl_env.s);
470 for (bytesleft = ssl_env.len; bytesleft > 0; bytesleft -= j)
471 if ((j = write(sslctl[0],ssl_env.s,bytesleft)) < 0) {
472 kill(childpid,SIGTERM);
473 logmsg(
WHO,111,FATAL,
"unable to write TLS environment");
478 strnum[fmt_ulong(strnum,childpid)] = 0;
481 if ((j = str_chr(s,
'=')))
482 if (!stralloc_copys(&tlsinfo,s + j + 1))
drop_nomem();
483 if (!stralloc_cats(&tlsinfo,
":"))
drop_nomem();
484 s = s + str_len(s) + 1;
485 s = s + str_len(s) + 1;
486 if ((j = str_chr(s,
'=')))
487 if (!stralloc_cats(&tlsinfo,s + j + 1))
drop_nomem();
489 log_who(
WHO,B(
"tls ",strnum,
" accept ",tlsinfo.s));
491 log_who(
WHO,B(
"tls ",strnum,
" accept"));
494 if (ssl_cmd ==
'Y' || ssl_cmd ==
'y') {
496 strnum[fmt_ulong(strnum,childpid)] = 0;
497 kill(childpid,SIGTERM);
498 logmsg(
WHO,111,ERROR,B(
"unable to speak TLS with: ",
remoteipstr,
" for pid: ",strnum,
" ",ERR_reason_error_string(ERR_peek_last_error())));
500 if (wait_nohang(&wstat) > 0)
501 _exit(wait_exitcode(wstat));
504 kill(childpid,SIGTERM);
520 strnum[fmt_ulong(strnum,getpid())] = 0;
525 if (dns_name(&localhostsa,
localip) >= 0)
526 if (localhostsa.len) {
561 env(
"TCP6INTERFACE",socket_getifname(
netif));
569 strnum[fmt_ulong(strnum,getpid())] = 0;
570 strnum2[fmt_ulong(strnum2,
maxconip)] = 0;
571 if (!stralloc_copys(&tmp,
"sslserver: "))
drop_nomem();
583 buffer_putflush(buffer_2,tmp.s,tmp.len);
588 if (
gid)
if (prot_gid(
gid) == -1)
589 logmsg(
WHO,111,FATAL,
"unable to set gid");
590 if (
uid)
if (prot_uid(
uid) == -1)
591 logmsg(
WHO,111,FATAL,
"unable to set uid");
593 close(
pi[1]); close(
po[0]); close(sslctl[0]);
595 sig_uncatch(sig_child);
596 sig_unblock(sig_child);
597 sig_uncatch(sig_term);
598 sig_uncatch(sig_pipe);
600 if (fcntl(sslctl[1],F_SETFD,0) == -1)
601 logmsg(
WHO,111,FATAL,
"unable to clear close-on-exec flag");
602 strnum[fmt_ulong(strnum,sslctl[1])] = 0;
603 env(
"SSLCTLFD",strnum);
605 if (fcntl(
pi[0],F_SETFD,0) == -1)
606 logmsg(
WHO,111,FATAL,
"unable to clear close-on-exec flag");
607 strnum[fmt_ulong(strnum,
pi[0])] = 0;
608 env(
"SSLREADFD",strnum);
610 if (fcntl(
po[1],F_SETFD,0) == -1)
611 logmsg(
WHO,111,FATAL,
"unable to clear close-on-exec flag");
612 strnum[fmt_ulong(strnum,
po[1])] = 0;
613 env(
"SSLWRITEFD",strnum);
616 if (fd_copy(0,t) == -1)
617 logmsg(
WHO,111,FATAL,
"unable to set up descriptor 0");
618 if (fd_copy(1,t) == -1)
619 logmsg(
WHO,111,FATAL,
"unable to set up descriptor 1");
621 if (fd_move(0,
pi[0]) == -1)
622 logmsg(
WHO,111,FATAL,
"unable to set up descriptor 0");
623 if (fd_move(1,
po[1]) == -1)
624 logmsg(
WHO,111,FATAL,
"unable to set up descriptor 1");
628 socket_ipoptionskill(t);
631 socket_tcpnodelay(t);
635 if (buffer_putsflush(&
bo,
banner) == -1)
636 logmsg(
WHO,111,FATAL,
"unable to print banner");
641 if (write(sslctl[1],&ssl_cmd,1) < 1)
642 logmsg(
WHO,111,FATAL,
"unable to start TLS");
644 while ((j = read(sslctl[1],envbuf,8192)) > 0) {
645 stralloc_catb(&ssl_env,envbuf,j);
646 if (ssl_env.len >= 2 && ssl_env.s[ssl_env.len - 2] == 0 && ssl_env.s[ssl_env.len - 1] == 0)
650 logmsg(
WHO,111,FATAL,
"unable to read TLS environment");
651 pathexec_multienv(&ssl_env);
656 logmsg(
WHO,111,FATAL,B(
"unable to run: ",*
prog));
663 logmsg(
WHO,100,USAGE,
"sslserver \
664[ -1346UXpPhHrRoOdDqQvVIeEsSnNmzZ ] \
688 strnum2[fmt_ulong(strnum2,
limit)] = 0;
689 strnum3[fmt_ulong(strnum3,
maxconip)] = 0;
690 log_who(
WHO,B(
"status: ",strnum,
"/",strnum2,
"/",strnum3));
703 while ((pid = wait_nohang(&wstat)) > 0) {
705 strnum[fmt_ulong(strnum,pid + 1)] = 0;
706 strnum2[fmt_ulong(strnum2,wstat)] = 0;
707 log_who(
WHO,B(
"ended by ",strnum,
" status ",strnum2));
720 logmsg(
WHO,111,ERROR,
"unable to read password from FD3");
726int passwd_cb(
char *buff,
int size,
int rwflag,
void *userdata)
729 logmsg(
WHO,111,ERROR,
"password too long");
735int main(
int argc,
char *
const *argv)
746 while ((opt = getoptb(argc,(
char **)argv,
"1346dDvVqQhHrRUXx:y:t:T:u:g:l:b:B:c:K:pPoOIEeSsw:nNzZm")) != opteof)
748 case 'b': scan_ulong(optarg,&
backlog);
break;
749 case 'c': scan_ulong(optarg,&
limit);
break;
751 case 'x':
fnrules = optarg;
break;
753 case 'B':
banner = optarg;
break;
768 case 't': scan_ulong(optarg,&
timeout);
break;
769 case 'T': scan_ulong(optarg,&
ssltimeout);
break;
771 case 'U': x = env_get(
"UID");
if (x) scan_ulong(x,&
uid);
772 x = env_get(
"GID");
if (x) scan_ulong(x,&
gid);
break;
773 case 'u': scan_ulong(optarg,&
uid);
break;
774 case 'g': scan_ulong(optarg,&
gid);
break;
775 case 'I':
netif = socket_getifidx(optarg);
break;
777 case 'K':
keypass = optarg;
break;
778 case '1':
flag1 = 1;
break;
779 case '3':
flag3 = 1;
break;
780 case '4': ipflag = 1;
break;
781 case '6': ipflag = 2;
break;
801 else if (str_equal((
char *)
hostname,
":0")) {
810 if (!x[scan_ulong(x,&u)])
813 se = getservbyname(x,
"tcp");
815 logmsg(
WHO,111,FATAL,B(
"unable to figure out port number for: ",x));
816 uint16_unpack_big((
char *)&se->s_port,&
localport);
819 if ((x = env_get(
"MAXCONIP"))) { scan_ulong(x,&u);
maxconip = u; }
823 if ((x = env_get(
"VERIFYDEPTH"))) { scan_ulong(x,&u);
verifydepth = u; }
825 if ((x = env_get(
"CAFILE")))
cafile = x;
828 if ((x = env_get(
"CCAFILE")))
ccafile = x;
833 if ((x = env_get(
"CADIR")))
cadir = x;
839 if ((x = env_get(
"CERTFILE")))
certfile = x;
842 if ((x = env_get(
"KEYFILE")))
keyfile = x;
845 if ((x = env_get(
"DHFILE")))
dhfile = x;
848 if ((x = env_get(
"CIPHERS")))
ciphers = x;
851 sig_block(sig_child);
854 sig_ignore(sig_pipe);
859 if (!stralloc_copyb(&addresses,(
char *)V4mappedprefix,12))
drop_nomem();
861 byte_copy(
localip,16,addresses.s);
864 byte_copy(
localip,16,addresses.s);
869 if (!addresses.len) {
871 if (dns_ip_qualify(&addresses,&fqdn,&tmp) < 0)
872 logmsg(
WHO,111,FATAL,B(
"temporarily unable to figure out IP address for: ",(
char *)
hostname));
874 byte_copy(
localip,16,addresses.s);
876 for (j = 0; j < addresses.len; j += 16) {
877 if (ipflag == 1 && !ip6_isv4mapped(addresses.s + j))
continue;
878 if (ipflag == 2 && !ip6_isv4mapped(addresses.s + j))
continue;
879 byte_copy(
localip,16,addresses.s + j);
883 if (addresses.len < 16)
884 logmsg(
WHO,111,FATAL,B(
"no IP address for: ",(
char *)
hostname));
891 logmsg(
WHO,111,FATAL,
"unable to create socket");
905 if (socket_listen(s,
backlog) == -1)
906 logmsg(
WHO,111,FATAL,
"unable to listen");
912 buffer_puts(&
bo,
" : ");
914 buffer_puts(&
bo,
"\n");
922 if (!
ctx) logmsg(
WHO,111,FATAL,
"unable to create TLS context");
926 case -1: logmsg(
WHO,111,ERROR,B(
"unable to load certificate chain file: ",
certchainfile));
927 case -2: logmsg(
WHO,111,ERROR,B(
"unable to load key: ",
keyfile));
928 case -3: logmsg(
WHO,111,ERROR,
"key does not match certificate");
933 case -1: logmsg(
WHO,111,ERROR,B(
"unable to load certificate: ",
certfile));
934 case -2: logmsg(
WHO,111,ERROR,B(
"unable to load key: ",
keyfile));
935 case -3: logmsg(
WHO,111,ERROR,
"key does not match certificate");
942 logmsg(
WHO,111,ERROR,
"unable to load CA list");
944 logmsg(
WHO,111,ERROR,
"unable to load client CA list");
947 logmsg(
WHO,111,ERROR,
"unable to set RSA parameters");
949 logmsg(
WHO,111,ERROR,
"unable to set DH parameters");
951 logmsg(
WHO,111,ERROR,
"unable to set cipher list");
954 strnum[fmt_ulong(strnum,getpid())] = 0;
955 strnum2[fmt_ulong(strnum2,
rsalen)] = 0;
956 log_who(
WHO,B(
"ciphers ",strnum,
" ",(
char *)
ciphers));
957 log_who(
WHO,B(
"cafile ",strnum,
" ",(
char *)
cafile));
958 log_who(
WHO,B(
"ccafile ",strnum,
" ",(
char *)
ccafile));
959 log_who(
WHO,B(
"cadir ",strnum,
" ",(
char *)
cadir));
961 log_who(
WHO,B(
"cert ",strnum,
" ",(
char *)
certfile));
962 log_who(
WHO,B(
"key ",strnum,
" ",(
char *)
keyfile));
963 log_who(
WHO,B(
"dhparam ",strnum,
" ",(
char *)
dhfile,
" ",strnum2));
966 close(0); open_read(
"/dev/null");
967 close(1); open_append(
"/dev/null");
975 sig_unblock(sig_child);
977 sig_block(sig_child);
978 if (t == -1)
continue;
988 strnum[fmt_ulong(strnum,
maxconip)] = 0;
989 logmsg(
WHO,100,WARN,B(
"ip connection limit of ",strnum,
" exceeded for: ",
remoteipstr));
1002 logmsg(
WHO,111,FATAL,B(
"unable to run: ",*argv));
1005 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 drop_rules(const char *fnbase)
void append(const char *ch)
void safecats(const char *s)
void ipchild_clear(char ip[16])
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)