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;
219 for (i = 0;i < 100;++i) {
222 if (ch < 33) ch =
'?';
223 if (ch > 126) ch =
'?';
224 if (ch ==
'%') ch =
'?';
230void env(
const char *s,
const char *t)
237 logmsg(
WHO,110,FATAL,B(
"unable to read: ",fnbase));
240void found(
char *data,
unsigned int datalen)
247 while ((next0 = byte_chr(data,datalen,0)) < datalen) {
254 split = str_chr(data + 1,
'=');
255 if (data[1 + split] ==
'=') {
257 env(data + 1,data + 1 + split + 1);
258 if (!str_diff(data + 1,
"MAXCONIP")) {
259 scan_ulong(data + 1 + split + 1,&
maxconip);
267 data += next0; datalen -= next0;
278 unsigned long tmp_long;
280 stralloc ssl_env = {0};
285 stralloc tlsinfo = {0};
287 if (pipe(
pi) == -1) logmsg(
WHO,111,FATAL,
"unable to create pipe");
288 if (pipe(
po) == -1) logmsg(
WHO,111,FATAL,
"unable to create pipe");
289 if (socketpair(AF_UNIX,SOCK_STREAM,0,sslctl) == -1)
290 logmsg(
WHO,111,FATAL,
"unable to create socketpair");
323 if (dns_name(&remotehostsa,
remoteip) >= 0)
324 if (remotehostsa.len) {
327 if (dns_ip6(&tmp,&remotehostsa) >= 0)
328 for (j = 0; j + 16 <= tmp.len; j += 16)
329 if (byte_equal(
remoteip,16,tmp.s + j)) {
333 if (dns_ip4(&tmp,&remotehostsa) >= 0)
334 for (j = 0; j + 4 <= tmp.len; j += 4)
335 if (byte_equal(
remoteip + 12,4,tmp.s + j)) {
350 if (!stralloc_0(&tcpremoteinfo))
drop_nomem();
370 switch (childpid = fork()) {
372 logmsg(
WHO,111,FATAL,
"unable to fork");
380 close(
pi[0]); close(
po[1]); close(sslctl[1]);
382 if ((s = env_get(
"SSL_CHROOT")))
383 if (chroot(s) == -1) {
384 kill(childpid,SIGTERM);
385 logmsg(
WHO,111,FATAL,
"unable to chroot");
388 if ((s = env_get(
"SSL_GID"))) {
389 scan_ulong(s,&tmp_long);
392 if (
gid)
if (prot_gid(
gid) == -1) {
393 kill(childpid,SIGTERM);
394 logmsg(
WHO,111,FATAL,
"unable to set gid");
397 if ((s = env_get(
"SSL_UID"))) {
398 scan_ulong(s,&tmp_long);
402 if (prot_uid(
uid) == -1) {
403 kill(childpid,SIGTERM);
404 logmsg(
WHO,111,FATAL,
"unable to set uid");
416 if (read(sslctl[0],&ssl_cmd,1) == 1) {
419 kill(childpid,SIGTERM);
420 logmsg(
WHO,111,FATAL,
"unable to create TLS instance");
422 if (ndelay_on(t) == -1) {
423 kill(childpid,SIGTERM);
424 logmsg(
WHO,111,FATAL,
"unable to set socket options");
427 strnum[fmt_ulong(strnum,childpid)] = 0;
428 kill(childpid,SIGTERM);
429 logmsg(
WHO,111,FATAL,B(
"unable to accept TLS from: ",
remoteipstr,
" for pid: ",strnum,
" ",ERR_reason_error_string(ERR_peek_last_error())));
431 }
else if (errno == EAGAIN) {
432 strnum[fmt_ulong(strnum,childpid)] = 0;
433 kill(childpid,SIGTERM);
439 if (ssl_cmd ==
'Y') {
442 env(
"SSLCTL",ssl_env.s);
444 for (bytesleft = ssl_env.len; bytesleft > 0; bytesleft -= j)
445 if ((j = write(sslctl[0],ssl_env.s,bytesleft)) < 0) {
446 kill(childpid,SIGTERM);
447 logmsg(
WHO,111,FATAL,
"unable to write TLS environment");
454 strnum[fmt_ulong(strnum,childpid)] = 0;
458 kill(childpid,SIGTERM);
459 logmsg(
WHO,110,ERROR,B(
"no client certificate from: ",
remoteipstr,
" for pid: ",strnum));
461 kill(childpid,SIGTERM);
462 logmsg(
WHO,110,ERROR,B(
"missing credentials (CA) or unable to validate client certificate from: ",
remoteipstr,
" for pid: ",strnum));
464 kill(childpid,SIGTERM);
466 logmsg(
WHO,110,ERROR,B(
"client hostname does not match certificate for pid: ",strnum,
" ",
verifyhost,
" <=> ",
certname.s));
469 strnum[fmt_ulong(strnum,childpid)] = 0;
470 logmsg(
WHO,0,INFO,B(
"valid client cert received from: ",
remoteipstr,
" for pid: ",strnum));
477 strnum[fmt_ulong(strnum,childpid)] = 0;
480 if ((j = str_chr(s,
'=')))
481 if (!stralloc_copys(&tlsinfo,s + j + 1))
drop_nomem();
482 if (!stralloc_cats(&tlsinfo,
":"))
drop_nomem();
483 s = s + str_len(s) + 1;
484 s = s + str_len(s) + 1;
485 if ((j = str_chr(s,
'=')))
486 if (!stralloc_cats(&tlsinfo,s + j + 1))
drop_nomem();
488 log_who(
WHO,B(
"tls ",strnum,
" accept ",tlsinfo.s));
490 log_who(
WHO,B(
"tls ",strnum,
" accept"));
493 if (ssl_cmd ==
'Y' || ssl_cmd ==
'y') {
495 strnum[fmt_ulong(strnum,childpid)] = 0;
496 kill(childpid,SIGTERM);
497 logmsg(
WHO,111,ERROR,B(
"unable to speak TLS with: ",
remoteipstr,
" for pid: ",strnum,
" ",ERR_reason_error_string(ERR_peek_last_error())));
499 if (wait_nohang(&wstat) > 0)
500 _exit(wait_exitcode(wstat));
503 kill(childpid,SIGTERM);
519 strnum[fmt_ulong(strnum,getpid())] = 0;
524 if (dns_name(&localhostsa,
localip) >= 0)
525 if (localhostsa.len) {
560 env(
"TCP6INTERFACE",socket_getifname(
netif));
568 strnum[fmt_ulong(strnum,getpid())] = 0;
569 strnum2[fmt_ulong(strnum2,
maxconip)] = 0;
570 if (!stralloc_copys(&tmp,
"sslserver: "))
drop_nomem();
582 buffer_putflush(buffer_2,tmp.s,tmp.len);
587 if (
gid)
if (prot_gid(
gid) == -1)
588 logmsg(
WHO,111,FATAL,
"unable to set gid");
589 if (
uid)
if (prot_uid(
uid) == -1)
590 logmsg(
WHO,111,FATAL,
"unable to set uid");
592 close(
pi[1]); close(
po[0]); close(sslctl[0]);
594 sig_uncatch(sig_child);
595 sig_unblock(sig_child);
596 sig_uncatch(sig_term);
597 sig_uncatch(sig_pipe);
599 if (fcntl(sslctl[1],F_SETFD,0) == -1)
600 logmsg(
WHO,111,FATAL,
"unable to clear close-on-exec flag");
601 strnum[fmt_ulong(strnum,sslctl[1])] = 0;
602 env(
"SSLCTLFD",strnum);
604 if (fcntl(
pi[0],F_SETFD,0) == -1)
605 logmsg(
WHO,111,FATAL,
"unable to clear close-on-exec flag");
606 strnum[fmt_ulong(strnum,
pi[0])] = 0;
607 env(
"SSLREADFD",strnum);
609 if (fcntl(
po[1],F_SETFD,0) == -1)
610 logmsg(
WHO,111,FATAL,
"unable to clear close-on-exec flag");
611 strnum[fmt_ulong(strnum,
po[1])] = 0;
612 env(
"SSLWRITEFD",strnum);
615 if (fd_copy(0,t) == -1)
616 logmsg(
WHO,111,FATAL,
"unable to set up descriptor 0");
617 if (fd_copy(1,t) == -1)
618 logmsg(
WHO,111,FATAL,
"unable to set up descriptor 1");
620 if (fd_move(0,
pi[0]) == -1)
621 logmsg(
WHO,111,FATAL,
"unable to set up descriptor 0");
622 if (fd_move(1,
po[1]) == -1)
623 logmsg(
WHO,111,FATAL,
"unable to set up descriptor 1");
627 socket_ipoptionskill(t);
630 socket_tcpnodelay(t);
634 if (buffer_putsflush(&
bo,
banner) == -1)
635 logmsg(
WHO,111,FATAL,
"unable to print banner");
640 if (write(sslctl[1],&ssl_cmd,1) < 1)
641 logmsg(
WHO,111,FATAL,
"unable to start TLS");
643 while ((j = read(sslctl[1],envbuf,8192)) > 0) {
644 stralloc_catb(&ssl_env,envbuf,j);
645 if (ssl_env.len >= 2 && ssl_env.s[ssl_env.len - 2] == 0 && ssl_env.s[ssl_env.len - 1] == 0)
649 logmsg(
WHO,111,FATAL,
"unable to read TLS environment");
650 pathexec_multienv(&ssl_env);
655 logmsg(
WHO,111,FATAL,B(
"unable to run: ",*
prog));
662 logmsg(
WHO,100,USAGE,
"sslserver \
663[ -1346UXpPhHrRoOdDqQvVIeEsSnNmzZ ] \
687 strnum2[fmt_ulong(strnum2,
limit)] = 0;
688 strnum3[fmt_ulong(strnum3,
maxconip)] = 0;
689 log_who(
WHO,B(
"status: ",strnum,
"/",strnum2,
"/",strnum3));
702 while ((pid = wait_nohang(&wstat)) > 0) {
704 strnum[fmt_ulong(strnum,pid + 1)] = 0;
705 strnum2[fmt_ulong(strnum2,wstat)] = 0;
706 log_who(
WHO,B(
"ended by ",strnum,
" status ",strnum2));
719 logmsg(
WHO,111,ERROR,
"unable to read password");
725int passwd_cb(
char *buff,
int size,
int rwflag,
void *userdata)
728 logmsg(
WHO,111,ERROR,
"password too long");
734int main(
int argc,
const char **argv)
745 while ((opt = getoptb(argc,argv,
"1346dDvVqQhHrRUXx:y:t:T:u:g:l:b:B:c:pPoOIEeSsw:nNzZm")) != opteof)
747 case 'b': scan_ulong(optarg,&
backlog);
break;
748 case 'c': scan_ulong(optarg,&
limit);
break;
750 case 'x':
fnrules = optarg;
break;
752 case 'B':
banner = optarg;
break;
767 case 't': scan_ulong(optarg,&
timeout);
break;
768 case 'T': scan_ulong(optarg,&
ssltimeout);
break;
770 case 'U': x = env_get(
"UID");
if (x) scan_ulong(x,&
uid);
771 x = env_get(
"GID");
if (x) scan_ulong(x,&
gid);
break;
772 case 'u': scan_ulong(optarg,&
uid);
break;
773 case 'g': scan_ulong(optarg,&
gid);
break;
774 case 'I':
netif = socket_getifidx(optarg);
break;
776 case '1':
flag1 = 1;
break;
777 case '3':
flag3 = 1;
break;
778 case '4': ipflag = 1;
break;
779 case '6': ipflag = 2;
break;
799 else if (str_equal((
char *)
hostname,
":0")) {
808 if (!x[scan_ulong(x,&u)])
811 se = getservbyname(x,
"tcp");
813 logmsg(
WHO,111,FATAL,B(
"unable to figure out port number for: ",x));
814 uint16_unpack_big((
char *)&se->s_port,&
localport);
817 if ((x = env_get(
"MAXCONIP"))) { scan_ulong(x,&u);
maxconip = u; }
821 if ((x = env_get(
"VERIFYDEPTH"))) { scan_ulong(x,&u);
verifydepth = u; }
823 if ((x = env_get(
"CAFILE")))
cafile = x;
826 if ((x = env_get(
"CCAFILE")))
ccafile = x;
831 if ((x = env_get(
"CADIR")))
cadir = x;
837 if ((x = env_get(
"CERTFILE")))
certfile = x;
840 if ((x = env_get(
"KEYFILE")))
keyfile = x;
843 if ((x = env_get(
"DHFILE")))
dhfile = x;
846 if ((x = env_get(
"CIPHERS")))
ciphers = x;
849 sig_block(sig_child);
852 sig_ignore(sig_pipe);
857 if (!stralloc_copyb(&addresses,(
char *)V4mappedprefix,12))
drop_nomem();
859 byte_copy(
localip,16,addresses.s);
862 byte_copy(
localip,16,addresses.s);
867 if (!addresses.len) {
869 if (dns_ip_qualify(&addresses,&fqdn,&tmp) < 0)
870 logmsg(
WHO,111,FATAL,B(
"temporarily unable to figure out IP address for: ",(
char *)
hostname));
872 byte_copy(
localip,16,addresses.s);
874 for (j = 0; j < addresses.len; j += 16) {
875 if (ipflag == 1 && !ip6_isv4mapped(addresses.s + j))
continue;
876 if (ipflag == 2 && !ip6_isv4mapped(addresses.s + j))
continue;
877 byte_copy(
localip,16,addresses.s + j);
881 if (addresses.len < 16)
882 logmsg(
WHO,111,FATAL,B(
"no IP address for: ",(
char *)
hostname));
889 logmsg(
WHO,111,FATAL,
"unable to create socket");
903 if (socket_listen(s,
backlog) == -1)
904 logmsg(
WHO,111,FATAL,
"unable to listen");
910 buffer_puts(&
bo,
" : ");
912 buffer_puts(&
bo,
"\n");
920 if (!
ctx) logmsg(
WHO,111,FATAL,
"unable to create TLS context");
924 case -1: logmsg(
WHO,111,ERROR,B(
"unable to load certificate chain file: ",
certchainfile));
925 case -2: logmsg(
WHO,111,ERROR,B(
"unable to load key: ",
keyfile));
926 case -3: logmsg(
WHO,111,ERROR,
"key does not match certificate");
931 case -1: logmsg(
WHO,111,ERROR,B(
"unable to load certificate: ",
certfile));
932 case -2: logmsg(
WHO,111,ERROR,B(
"unable to load key: ",
keyfile));
933 case -3: logmsg(
WHO,111,ERROR,
"key does not match certificate");
940 logmsg(
WHO,111,ERROR,
"unable to load CA list");
942 logmsg(
WHO,111,ERROR,
"unable to load client CA list");
945 logmsg(
WHO,111,ERROR,
"unable to set RSA parameters");
947 logmsg(
WHO,111,ERROR,
"unable to set DH parameters");
949 logmsg(
WHO,111,ERROR,
"unable to set cipher list");
952 strnum[fmt_ulong(strnum,getpid())] = 0;
953 strnum2[fmt_ulong(strnum2,
rsalen)] = 0;
954 log_who(
WHO,B(
"ciphers ",strnum,
" ",(
char *)
ciphers));
955 log_who(
WHO,B(
"cafile ",strnum,
" ",(
char *)
cafile));
956 log_who(
WHO,B(
"ccafile ",strnum,
" ",(
char *)
ccafile));
957 log_who(
WHO,B(
"cadir ",strnum,
" ",(
char *)
cadir));
959 log_who(
WHO,B(
"cert ",strnum,
" ",(
char *)
certfile));
960 log_who(
WHO,B(
"key ",strnum,
" ",(
char *)
keyfile));
961 log_who(
WHO,B(
"dhparam ",strnum,
" ",(
char *)
dhfile,
" ",strnum2));
964 close(0); open_read(
"/dev/null");
965 close(1); open_append(
"/dev/null");
973 sig_unblock(sig_child);
975 sig_block(sig_child);
976 if (t == -1)
continue;
986 strnum[fmt_ulong(strnum,
maxconip)] = 0;
987 logmsg(
WHO,100,WARN,B(
"ip connection limit of ",strnum,
" exceeded for: ",
remoteipstr));
1000 logmsg(
WHO,111,FATAL,B(
"unable to run: ",*argv));
1003 logmsg(
WHO,111,FATAL,B(
"unable to fork: ",strnum));
const char auto_ciphers[]
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)
SSL * ssl_new(SSL_CTX *ctx, int s)
Header file to be used with sqmail; previously called ssl.h. (name clash)
int ssl_io(SSL *ssl, int fdleft, int fdright, unsigned int timeout)
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_ca(SSL_CTX *ctx, const char *certfile, const char *certdir, int d)
int remoteinfo(stralloc *out, char ipremote[16], uint16 portremote, char iplocal[16], uint16 portlocal, unsigned int timeout, uint32 netif)
int ssl_server_env(SSL *ssl, stralloc *sa)
const char auto_keyfile[]
const char auto_certchainfile[]
int ssl_ciphers(SSL_CTX *ctx, const char *ciphers)
int ssl_params_dh(SSL_CTX *ctx, const char *dhfile)
int ssl_params_rsa(SSL_CTX *ctx, int len)
int ssl_cca(SSL_CTX *ctx, const char *certfile)
int ssl_verify(SSL *ssl, const char *hostname, stralloc *dnsout)
int rules(void(*callback)(char *, unsigned int), int fd, char *ip, char *host, char *info)
const char auto_ccafile[]
int ssl_timeoutaccept(SSL *ssl, unsigned int timeout)
const char auto_certfile[]