52extern void server(
int argcs,
char *
const *argvs);
74static stralloc tcpremoteinfo = {0};
78char localip[16] = { 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 };
80static stralloc localhostsa;
89static stralloc remotehostsa;
95static char strnum[FMT_ULONG];
96static char strnum2[FMT_ULONG];
100static stralloc addresses;
101static stralloc certname;
143 logmsg(
who,111,FATAL,
"out of memory");
147 logmsg(
who,111,FATAL,
"out of timestamps");
162 for (i = 0;i < 100;++i) {
165 if (ch < 33) ch =
'?';
166 if (ch > 126) ch =
'?';
167 if (ch ==
'%') ch =
'?';
172void env(
const char *s,
const char *t)
183static void env_def() {
190 if (!stralloc_cats(&
envplus,
""))
return;
193 for (i = 0; environ[i]; ++i)
195 for (i = 0; i <
envplus.len; ++i)
199 e = (
char **) alloc((elen + 1) *
sizeof(
char *));
203 for (i = 0; environ[i]; ++i)
204 e[elen++] = environ[i];
207 for (i = 0; i <
envplus.len; ++i)
209 split = str_chr(
envplus.s + j,
'=');
210 for (t = 0;t < elen;++t)
211 if (byte_equal(
envplus.s + j,split,
e[t]))
212 if (
e[t][split] ==
'=') {
230 alloc_free((
char *)
e);
231 logmsg(
who,111,FATAL,
"environ changed");
246 logmsg(
who,111,FATAL,B(
"unable to read: ",fnbase));
249void found(
char *data,
unsigned int datalen)
256 while ((next0 = byte_chr(data,datalen,0)) < datalen) {
263 split = str_chr(data + 1,
'=');
264 if (data[1 + split] ==
'=') {
266 env(data + 1,data + 1 + split + 1);
271 data += next0; datalen -= next0;
290 strnum[fmt_ulong(strnum,getpid())] = 0;
295 logmsg(
who,111,FATAL,
"unable to get local address");
300 if (dns_name(&localhostsa,
localip) >= 0)
301 if (localhostsa.len) {
325 if (dns_name(&remotehostsa,
remoteip) >= 0)
326 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,4,tmp.s + j)) {
350 if (!stralloc_0(&tcpremoteinfo))
drop_nomem();
386 if (!ip6_isv4mapped(
localip)) {
395 env(
"TCP6INTERFACE",socket_getifname(
netif));
403 strnum[fmt_ulong(strnum,getpid())] = 0;
416 buffer_putflush(buffer_2,tmp.s,tmp.len);
424 if (pipe(
pi) == -1) logmsg(
who,111,FATAL,
"unable to create pipe");
425 if (pipe(
po) == -1) logmsg(
who,111,FATAL,
"unable to create pipe");
428 if (!ssl) logmsg(
who,111,FATAL,
"unable to create SSL instance");
429 if (ndelay_on(t) == -1)
430 logmsg(
who,111,FATAL,
"unable to set socket options");
432 strnum[fmt_ulong(strnum,getpid())] = 0;
433 logmsg(
who,110,DROP,B(
"unable to TLS accept for pid:",strnum));
438 strnum[fmt_ulong(strnum,getpid())] = 0;
439 log_who(
who,B(
"tls ",strnum,
" accept "));
445 logmsg(
who,110,ERROR,
"no client certificate");
447 logmsg(
who,110,ERROR,
"missing credentials (CA) or unable to validate client certificate");
450 logmsg(
who,110,ERROR,B(
"client hostname name does not match certificate: ",
remotehost,
" <=> ",certname.s));
458 logmsg(
who,111,FATAL,
"unable to fork ");
460 close(
pi[0]); close(
po[1]);
461 sig_uncatch(sig_child);
462 sig_unblock(sig_child);
464 strnum[fmt_ulong(strnum,getpid())] = 0;
465 logmsg(
who,-99,WARN,B(
"unable to speak TLS for pid: ",strnum));
471 close(
pi[1]); close(
po[0]);
476 if (fd_move(0,
pi[0]) == -1)
477 logmsg(
who,111,FATAL,
"unable to set up descriptor 0");
478 if (fd_move(1,
po[1]) == -1)
479 logmsg(
who,111,FATAL,
"unable to set up descriptor 1");
482 socket_ipoptionskill(t);
485 socket_tcpnodelay(t);
489 if (buffer_putsflush(&
b,
banner) == -1)
490 logmsg(
who,111,FATAL,
"unable to print banner");
500 strnum[fmt_ulong(strnum,getpid())] = 0;
504 buffer_putflush(buffer_2,tmp.s,tmp.len);
513 logmsg(
who,100,USAGE,B(
who,
"\
514[ -1346UXpPhHrRoOdDqQviIeEsS ] \
543 strnum2[fmt_ulong(strnum2,
limit)] = 0;
544 log_who(
who,B(
"status: ",strnum,
"/",strnum2));
558 if (pid < 0) logmsg(
who,111,FATAL,
"cannot get pid");
568 while ((pid = wait_nohang(&wstat)) > 0) {
570 strnum[fmt_ulong(strnum,pid)] = 0;
571 strnum2[fmt_ulong(strnum2,wstat)] = 0;
572 log_who(
who,B(
"end ",strnum,
" status ",strnum2));
585 logmsg(
who,111,ERROR,
"unable to read password");
591int passwd_cb(
char *buff,
int size,
int rwflag,
void *userdata)
594 logmsg(
who,111,ERROR,
"password too long");
600void spawn(
int s,
int argc,
char *
const *argv)
610 sig_uncatch(sig_child);
611 sig_unblock(sig_child);
612 sig_uncatch(sig_term);
613 sig_uncatch(sig_pipe);
616 if (lock_ex(
fdlock) == -1)
617 logmsg(
who,111,FATAL,B(
"unable to lock: ",(
char *)
lockfile));
630 if (t == -1)
continue;
631 if (!
doit(t))
continue;
639 logmsg(
who,111,FATAL,
"unable to fork");
645int main(
int argc,
char *
const *argv)
655 struct taia deadline;
660 while ((opt = getoptb(argc,(
char **)argv,
"dDvqQhHrRUXx:y:t:T:u:g:l:b:B:c:pPoO1346I:EeSsaAf:w:K:zZ")) != opteof)
662 case 'b': scan_ulong(optarg,&
backlog);
break;
663 case 'c': scan_ulong(optarg,&
limit);
break;
665 case 'x':
fnrules = optarg;
break;
667 case 'B':
banner = optarg;
break;
681 case 't': scan_ulong(optarg,&
timeout);
break;
682 case 'T': scan_ulong(optarg,&
ssltimeout);
break;
683 case 'U': x = env_get(
"UID");
if (x) scan_ulong(x,&
uid);
684 x = env_get(
"GID");
if (x) scan_ulong(x,&
gid);
break;
685 case 'u': scan_ulong(optarg,&
uid);
break;
686 case 'g': scan_ulong(optarg,&
gid);
break;
688 case 'I':
netif = socket_getifidx(optarg);
break;
689 case 'K':
keypass = optarg;
break;
690 case '1':
flag1 = 1;
break;
691 case '3':
flag3 = 1;
break;
692 case '4': ipflag = 1;
break;
693 case '6': ipflag = 2;
break;
714 else if (str_equal((
char *)
hostname,
":0")) {
721 if (!x[scan_ulong(x,&u)])
724 se = getservbyname(x,
"tcp");
726 logmsg(
who,111,FATAL,B(
"unable to figure out port number for: ",x));
727 uint16_unpack_big((
char*)&se->s_port,&
localport);
730 if ((x = env_get(
"VERIFYDEPTH"))) {
735 if ((x = env_get(
"CAFILE")))
cafile = x;
738 if ((x = env_get(
"CCAFILE")))
ccafile = x;
742 if ((x = env_get(
"CADIR")))
cadir = x;
748 if ((x = env_get(
"CERTFILE")))
certfile = x;
751 if ((x = env_get(
"KEYFILE")))
keyfile = x;
754 if ((x = env_get(
"DHFILE")))
dhfile = x;
757 if ((x = env_get(
"CIPHERS")))
ciphers = x;
761 if (getpgrp() != getpid())
762 logmsg(
who,111,FATAL,
"unable to create process group");
767 logmsg(
who,111,FATAL,B(
"unable to open: ",(
char *)
lockfile));
771 logmsg(
who,111,FATAL,
"unable to create pipe");
778 sig_block(sig_child);
781 sig_ignore(sig_pipe);
786 if (!stralloc_copyb(&addresses,(
char *)V4mappedprefix,12))
drop_nomem();
788 byte_copy(
localip,16,addresses.s);
791 byte_copy(
localip,16,addresses.s);
796 if (!addresses.len) {
798 if (dns_ip_qualify(&addresses,&fqdn,&tmp) < 0)
799 logmsg(
who,111,FATAL,B(
"temporarily unable to figure out IP address for: ",(
char *)
hostname));
801 byte_copy(
localip,16,addresses.s);
803 for (j = 0; j < addresses.len; j += 16) {
804 if (ipflag == 1 && !ip6_isv4mapped(addresses.s + j))
continue;
805 if (ipflag == 2 && !ip6_isv4mapped(addresses.s + j))
continue;
806 byte_copy(
localip,16,addresses.s + j);
810 if (addresses.len < 16)
811 logmsg(
who,111,FATAL,B(
"no IP address for: ",(
char *)
hostname));
818 logmsg(
who,111,FATAL,
"unable to create socket");
823 logmsg(
who,111,FATAL,
"unable to bind");
825 logmsg(
who,111,FATAL,
"unable to get local address");
826 if (socket_listen(s,
backlog) == -1)
827 logmsg(
who,111,FATAL,
"unable to listen");
831 if (
gid)
if (prot_gid(
gid) == -1)
832 logmsg(
who,111,FATAL,
"unable to set gid");
833 if (
uid)
if (prot_uid(
uid) == -1)
834 logmsg(
who,111,FATAL,
"unable to set uid");
847 buffer_puts(&
b,
" : ");
849 buffer_puts(&
b,
"\n");
857 if (!
ctx) logmsg(
who,111,FATAL,
"unable to create TLS context");
861 case -1: logmsg(
who,111,ERROR,
"unable to load certificate chain file");
862 case -2: logmsg(
who,111,ERROR,
"unable to load key");
863 case -3: logmsg(
who,111,ERROR,
"key does not match certificate");
868 case -1: logmsg(
who,111,ERROR,
"unable to load certificate");
869 case -2: logmsg(
who,111,ERROR,
"unable to load key");
870 case -3: logmsg(
who,111,ERROR,
"key does not match certificate");
877 logmsg(
who,111,ERROR,
"unable to load CA list");
879 logmsg(
who,111,ERROR,
"unable to load client CA list");
882 logmsg(
who,111,ERROR,
"unable to set RSA parameters");
884 logmsg(
who,111,ERROR,
"unable to set DH parameters");
887 if (
gid)
if (prot_gid(
gid) == -1)
888 logmsg(
who,111,FATAL,
"unable to set gid");
889 if (
uid)
if (prot_uid(
uid) == -1)
890 logmsg(
who,111,FATAL,
"unable to set uid");
894 logmsg(
who,111,ERROR,
"unable to set cipher list");
897 strnum[fmt_ulong(strnum,getpid())] = 0;
898 strnum2[fmt_ulong(strnum2,
rsalen)] = 0;
899 log_who(
who,B(
"ciphers ",strnum,
" ",(
char *)
ciphers));
900 log_who(
who,B(
"cafile ",strnum,
" ",(
char *)
cafile));
901 log_who(
who,B(
"ccafile ",strnum,
" ",(
char *)
ccafile));
902 log_who(
who,B(
"cadir ",strnum,
" ",(
char *)
cadir));
904 log_who(
who,B(
"cert ",strnum,
" ",(
char *)
certfile));
905 log_who(
who,B(
"key ",strnum,
" ",(
char *)
keyfile));
907 log_who(
who,B(
"dhparam ",strnum,
" ",(
char *)
dhfile,
" ",strnum2));
915 int pause_ret, read_ret;
918 sig_unblock(sig_child);
920 io[0].events = IOPAUSE_READ;
922 taia_uint(&deadline,3600);
923 taia_add(&deadline,&stamp,&deadline);
924 pause_ret = iopause(io,1,&deadline,&stamp);
925 sig_block(sig_child);
928 while ((read_ret = buffer_unixread(
selfpipe[0],&ch,1)) == 1)
930 if ((pause_ret > 0) && (read_ret == 0)) {
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_error(int(*op)(const char *))
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 server(int argcs, char *const *argvs)
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
void drop_rules(const char *fnbase)
void append(const char *ch)
void safecats(const char *s)
int passwd_cb(char *buff, int size, int rwflag, void *userdata)
void spawn(int s, int argc, char *const *argv)
int error_warn(const char *x)
Header file to be used with sqmail; previously called ssl.h. (name clash)