10 #include <arpa/inet.h>
20 #include "socket_if.h"
27 #include "timeoutconn.h"
29 #include "dnsresolv.h"
37 #define WHO "sslclient"
40 logmsg(
WHO,111,FATAL,
"out of memory");
42 void env(
const char *s,
const char *t) {
43 if (!pathexec_env(s,t))
nomem();
47 logmsg(
WHO,100,USAGE,
"sslclient \
48 [ -463hHrRdDiqQveEsSnNxX ] \
79 char iplocal[16] = { 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 };
89 static stralloc addresses;
91 static stralloc moreaddresses;
95 static char strnum[FMT_ULONG];
96 static char ipstr[IP6_FMT];
121 logmsg(
WHO,111,ERROR,
"unable to read password");
129 logmsg(
WHO,111,ERROR,
"password too long");
135 int main(
int argc,
char *
const *argv) {
147 dns_random_init(
seed);
151 sig_ignore(sig_pipe);
153 while ((opt = getopt(argc,argv,
"dDvqQhHrRimM:p:t:T:l:a:A:c:z:k:V:346eEsSnN0xXw:")) != opteof)
155 case '4': ipflag = 1;
break;
156 case '6': ipflag = 0;
break;
169 case 't': scan_ulong(optarg,&
itimeout);
break;
170 case 'T': j = scan_ulong(optarg,&
ctimeout[0]);
171 if (optarg[j] ==
'+') ++j;
172 scan_ulong(optarg + j,&
ctimeout[1]);
175 case 'i':
if (!ip6_scan(optarg,
iplocal))
usage();
break;
176 case 'I':
netif = socket_getifidx(optarg);
break;
177 case 'p': scan_ulong(optarg,&u);
portlocal = u;
break;
178 case 'a':
cafile = optarg;
break;
179 case 'A':
cadir = optarg;
break;
181 case 'z':
ciphers = optarg;
break;
182 case 'k':
keyfile = optarg;
break;
183 case 'V': scan_ulong(optarg,&u);
verifydepth = u;
break;
184 case '3':
flag3 = 1;
break;
206 if (!x[scan_ulong(x,&u)])
210 se = getservbyname(x,
"tcp");
212 logmsg(
WHO,111,FATAL,B(
"unable to figure out port number for ",x));
213 uint16_unpack_big((
char*)&se->s_port,&
portremote);
225 if (!*++argv)
usage();
228 dns_ip_qualify(&addresses,&fqdn,&tmp);
229 if (addresses.len < 16)
230 logmsg(
WHO,111,ERROR,B(
"No IP address for: ",
hostname));
232 if (addresses.len == 16) {
237 for (cloop = 0; cloop < 2; ++cloop) {
238 if (!stralloc_copys(&moreaddresses,
""))
nomem();
239 for (j = 0; j + 16 <= addresses.len; j += 16) {
240 if (ipflag == 1 || ip6_isv4mapped(addresses.s + j)) {
242 if (s == -1) logmsg(
WHO,111,FATAL,
"unable to create socket");
246 if (s == -1) logmsg(
WHO,111,FATAL,
"unable to create socket");
249 if (r == -1) logmsg(
WHO,111,FATAL,
"unable to bind socket");
253 if (!cloop &&
ctimeout[1] && (errno == ETIMEDOUT)) {
254 if (!stralloc_catb(&moreaddresses,addresses.s + j,16))
nomem();
258 if (ip6_isv4mapped(addresses.s + j))
259 ipstr[ip4_fmt(ipstr,addresses.s + j + 12)] = 0;
261 ipstr[ip6_fmt(ipstr,addresses.s + j)] = 0;
264 if (!stralloc_copy(&addresses,&moreaddresses))
nomem();
266 logmsg(
WHO,110,DROP,B(
"unable to connect to: ",ipstr,
" port: ",strnum));
276 logmsg(
WHO,111,FATAL,
"unable to get local address");
280 ipstr[ip4_fmt(ipstr,
iplocal + 12)] = 0;
284 ipstr[ip6_fmt(ipstr,
iplocal)] = 0;
287 env(
"SSLLOCALIP",ipstr);
291 env(
"SSLLOCALPORT",strnum);
296 if (dns_name(&tmp,
iplocal) >= 0) {
297 if (!stralloc_0(&tmp))
nomem();
300 env(
"SSLLOCALHOST",x);
306 logmsg(
WHO,111,FATAL,
"unable to get remote address");
309 ipstr[ip4_fmt(ipstr,
ipremote + 12)] = 0;
313 env(
"SSLREMOTEIP",ipstr);
317 env(
"SSLREMOTEPORT",strnum);
323 if (!stralloc_0(&tmp))
nomem();
327 env(
"SSLREMOTEHOST",x);
333 if (!stralloc_0(&tmp))
nomem();
336 env(
"SSLREMOTEINFO",x);
344 logmsg(
WHO,111,FATAL,
"unable to create TLS context");
347 case -1: logmsg(
WHO,111,ERROR,
"unable to load certificate");
348 case -2: logmsg(
WHO,111,ERROR,
"unable to load key pair");
349 case -3: logmsg(
WHO,111,ERROR,
"key does not match certificate");
354 logmsg(
WHO,111,ERROR,
"unable to load CA list");
357 logmsg(
WHO,111,ERROR,
"unable to set cipher list");
360 if (!ssl) logmsg(
WHO,111,FATAL,
"unable to create TLS instance");
363 if (!SSL_set_tlsext_host_name(ssl,
hostname))
364 logmsg(
WHO,111,FATAL,B(
"unable to set TLS SNI extensions for hostname: ",(
char *)
hostname));
366 for (cloop = 0; cloop < 2; ++cloop) {
368 if (!cloop &&
ctimeout[1])
continue;
369 logmsg(
WHO,111,FATAL,
"unable to TLS connect");
381 logmsg(
WHO,110,ERROR,
"no server certificate");
383 logmsg(
WHO,110,ERROR,
"missing credentials (CA) or unable to validate server certificate");
387 logmsg(
WHO,110,ERROR,B(
"server hostname does not match certificate: ",(
char *)
hostname,
" <=> ",
certname.s));
392 log_who(
WHO,B(
"tls connected to: ",ipstr,
" port: ",strnum));
395 socket_tcpnodelay(s);
397 if (pipe(
pi) == -1) logmsg(
WHO,111,FATAL,
"unable to create pipe");
398 if (pipe(
po) == -1) logmsg(
WHO,111,FATAL,
"unable to create pipe");
400 if (pipe(
pt) == -1) logmsg(
WHO,111,FATAL,
"unable to create pipe");
401 close(
pi[0]); close(
pi[1]);
405 if (pipe(
pt) == -1) logmsg(
WHO,111,FATAL,
"unable to create pipe");
406 close(
po[0]); close(
po[1]);
410 switch (opt = fork()) {
412 logmsg(
WHO,111,FATAL,
"unable to fork");
416 close(
pi[0]); close(
po[1]);
418 logmsg(
WHO,110,DROP,
"unable to speak TLS");
420 wait_pid(&wstat,opt);
424 if (wait_pid(&wstat,opt) > 0)
425 _exit(wait_exitcode(wstat));
432 if (fd_move(6,
pi[0]) == -1)
433 logmsg(
WHO,111,FATAL,
"unable to set up descriptor 6");
434 if (fd_move(7,
po[1]) == -1)
435 logmsg(
WHO,111,FATAL,
"unable to set up descriptor 7");
436 sig_uncatch(sig_pipe);
439 logmsg(
WHO,111,FATAL,B(
"unable to run: ",*argv));
const char auto_ciphers[]
int remoteinfo(stralloc *out, char ipremote[16], uint16 portremote, char iplocal[16], uint16 portlocal, unsigned int timeout, uint32 netif)
int ssl_ca(SSL_CTX *ctx, const char *certfile, const char *certdir, int d)
int ssl_certkey(SSL_CTX *ctx, const char *certfile, const char *keyfile, pem_password_cb *passwd_cb)
int ssl_ciphers(SSL_CTX *ctx, const char *ciphers)
int ssl_client_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_timeoutconn(SSL *ssl, unsigned int timeout)
int ssl_verify(SSL *ssl, const char *hostname, stralloc *dnsout)
unsigned long ctimeout[2]
void env(const char *s, const char *t)
int passwd_cb(char *buf, int size, int rwflag, void *userdata)
int main(int argc, char *const *argv)
Header file to be used with sqmail; previously called ssl.h. (name clash)