From f94e8cb1dd847cd2efa0211e711707fc705f11eb Mon Sep 17 00:00:00 2001 From: beck <> Date: Thu, 17 Oct 2019 14:29:24 +0000 Subject: Fix -N flag to actually shut down the (entire) socket when the input goes away. This allows for using nc in cases where the network server will no longer expect anything after eof, instead of hanging waiting for more input from our end. Additionaly, shut down if tls is in use if either side of the socket goes away, since we higher level TLS operations (tls_read and write) will require the socket to be both readable and writable as we can get TLS_WANT_POLLIN or TLS_WANT_POLLOUT on either operation. deraadt@ buying it. found by sthen@ --- src/usr.bin/nc/netcat.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/usr.bin/nc/netcat.c b/src/usr.bin/nc/netcat.c index c04298f64c..795592f0b7 100644 --- a/src/usr.bin/nc/netcat.c +++ b/src/usr.bin/nc/netcat.c @@ -1,4 +1,4 @@ -/* $OpenBSD: netcat.c,v 1.206 2019/08/08 16:49:35 mestre Exp $ */ +/* $OpenBSD: netcat.c,v 1.207 2019/10/17 14:29:24 beck Exp $ */ /* * Copyright (c) 2001 Eric Jackson * Copyright (c) 2015 Bob Beck. All rights reserved. @@ -1259,6 +1259,23 @@ readwrite(int net_fd, struct tls *tls_ctx) if (pfd[POLL_NETIN].fd == -1 && netinbufpos == 0) { pfd[POLL_STDOUT].fd = -1; } + + if (((usetls || Nflag) && (pfd[POLL_NETIN].fd == -1)) || + (usetls && pfd[POLL_NETOUT].fd == -1)) { + /* + * -N says: shutdown(2) the 'network socket' + * after EOF on the input + * + * for TLS we need to die if either end is + * toast, since both reading and writing to + * the socket may be necessary for any higher + * level tls operation + */ + shutdown(pfd[POLL_NETOUT].fd, SHUT_WR); + shutdown(pfd[POLL_NETIN].fd, SHUT_RD); + pfd[POLL_NETOUT].fd = -1; + pfd[POLL_NETIN].fd = -1; + } } } -- cgit v1.2.3-55-g6feb