diff options
author | deraadt <> | 2015-09-12 08:38:33 +0000 |
---|---|---|
committer | deraadt <> | 2015-09-12 08:38:33 +0000 |
commit | 4c26aee9b32371e04636caf4e3188a68b04c2a0c (patch) | |
tree | 9e5890ec7d237f7093264acce8b11eb7a47640de /src | |
parent | 3c17358c0a47b0c8ad2d510ae15a2035a6c7e9f2 (diff) | |
download | openbsd-4c26aee9b32371e04636caf4e3188a68b04c2a0c.tar.gz openbsd-4c26aee9b32371e04636caf4e3188a68b04c2a0c.tar.bz2 openbsd-4c26aee9b32371e04636caf4e3188a68b04c2a0c.zip |
use SOCK_CLOEXEC instead of fnctl; ok guenther beck jsing
Diffstat (limited to 'src')
-rw-r--r-- | src/usr.bin/nc/netcat.c | 28 |
1 files changed, 16 insertions, 12 deletions
diff --git a/src/usr.bin/nc/netcat.c b/src/usr.bin/nc/netcat.c index acb97870a0..e044be4f3d 100644 --- a/src/usr.bin/nc/netcat.c +++ b/src/usr.bin/nc/netcat.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: netcat.c,v 1.135 2015/09/12 07:56:56 jmc Exp $ */ | 1 | /* $OpenBSD: netcat.c,v 1.136 2015/09/12 08:38:33 deraadt Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2001 Eric Jackson <ericj@monkey.org> | 3 | * Copyright (c) 2001 Eric Jackson <ericj@monkey.org> |
4 | * Copyright (c) 2015 Bob Beck. All rights reserved. | 4 | * Copyright (c) 2015 Bob Beck. All rights reserved. |
@@ -45,7 +45,6 @@ | |||
45 | 45 | ||
46 | #include <err.h> | 46 | #include <err.h> |
47 | #include <errno.h> | 47 | #include <errno.h> |
48 | #include <fcntl.h> | ||
49 | #include <limits.h> | 48 | #include <limits.h> |
50 | #include <netdb.h> | 49 | #include <netdb.h> |
51 | #include <poll.h> | 50 | #include <poll.h> |
@@ -129,7 +128,7 @@ int timeout_connect(int, const struct sockaddr *, socklen_t); | |||
129 | int socks_connect(const char *, const char *, struct addrinfo, | 128 | int socks_connect(const char *, const char *, struct addrinfo, |
130 | const char *, const char *, struct addrinfo, int, const char *); | 129 | const char *, const char *, struct addrinfo, int, const char *); |
131 | int udptest(int); | 130 | int udptest(int); |
132 | int unix_bind(char *); | 131 | int unix_bind(char *, int); |
133 | int unix_connect(char *); | 132 | int unix_connect(char *); |
134 | int unix_listen(char *); | 133 | int unix_listen(char *); |
135 | void set_common_sockopts(int, int); | 134 | void set_common_sockopts(int, int); |
@@ -363,7 +362,7 @@ main(int argc, char *argv[]) | |||
363 | unix_dg_tmp_socket = sflag; | 362 | unix_dg_tmp_socket = sflag; |
364 | } else { | 363 | } else { |
365 | strlcpy(unix_dg_tmp_socket_buf, "/tmp/nc.XXXXXXXXXX", | 364 | strlcpy(unix_dg_tmp_socket_buf, "/tmp/nc.XXXXXXXXXX", |
366 | UNIX_DG_TMP_SOCKET_SIZE); | 365 | UNIX_DG_TMP_SOCKET_SIZE); |
367 | if (mktemp(unix_dg_tmp_socket_buf) == NULL) | 366 | if (mktemp(unix_dg_tmp_socket_buf) == NULL) |
368 | err(1, "mktemp"); | 367 | err(1, "mktemp"); |
369 | unix_dg_tmp_socket = unix_dg_tmp_socket_buf; | 368 | unix_dg_tmp_socket = unix_dg_tmp_socket_buf; |
@@ -441,7 +440,7 @@ main(int argc, char *argv[]) | |||
441 | 440 | ||
442 | if (family == AF_UNIX) { | 441 | if (family == AF_UNIX) { |
443 | if (uflag) | 442 | if (uflag) |
444 | s = unix_bind(host); | 443 | s = unix_bind(host, 0); |
445 | else | 444 | else |
446 | s = unix_listen(host); | 445 | s = unix_listen(host); |
447 | } | 446 | } |
@@ -508,6 +507,7 @@ main(int argc, char *argv[]) | |||
508 | readwrite(connfd, NULL); | 507 | readwrite(connfd, NULL); |
509 | if (tls_cctx) { | 508 | if (tls_cctx) { |
510 | int i; | 509 | int i; |
510 | |||
511 | do { | 511 | do { |
512 | i = tls_close(tls_cctx); | 512 | i = tls_close(tls_cctx); |
513 | } while (i == TLS_WANT_POLLIN || | 513 | } while (i == TLS_WANT_POLLIN || |
@@ -602,6 +602,7 @@ main(int argc, char *argv[]) | |||
602 | readwrite(s, tls_ctx); | 602 | readwrite(s, tls_ctx); |
603 | if (tls_ctx) { | 603 | if (tls_ctx) { |
604 | int j; | 604 | int j; |
605 | |||
605 | do { | 606 | do { |
606 | j = tls_close(tls_ctx); | 607 | j = tls_close(tls_ctx); |
607 | } while (j == TLS_WANT_POLLIN || | 608 | } while (j == TLS_WANT_POLLIN || |
@@ -627,14 +628,14 @@ main(int argc, char *argv[]) | |||
627 | * Returns a unix socket bound to the given path | 628 | * Returns a unix socket bound to the given path |
628 | */ | 629 | */ |
629 | int | 630 | int |
630 | unix_bind(char *path) | 631 | unix_bind(char *path, int flags) |
631 | { | 632 | { |
632 | struct sockaddr_un sun; | 633 | struct sockaddr_un sun; |
633 | int s; | 634 | int s; |
634 | 635 | ||
635 | /* Create unix domain socket. */ | 636 | /* Create unix domain socket. */ |
636 | if ((s = socket(AF_UNIX, uflag ? SOCK_DGRAM : SOCK_STREAM, | 637 | if ((s = socket(AF_UNIX, flags | (uflag ? SOCK_DGRAM : SOCK_STREAM), |
637 | 0)) < 0) | 638 | 0)) < 0) |
638 | return (-1); | 639 | return (-1); |
639 | 640 | ||
640 | memset(&sun, 0, sizeof(struct sockaddr_un)); | 641 | memset(&sun, 0, sizeof(struct sockaddr_un)); |
@@ -659,6 +660,7 @@ tls_setup_client(struct tls *tls_ctx, int s, char *host) | |||
659 | 660 | ||
660 | { | 661 | { |
661 | int i; | 662 | int i; |
663 | |||
662 | if (tls_connect_socket(tls_ctx, s, | 664 | if (tls_connect_socket(tls_ctx, s, |
663 | tls_expectname ? tls_expectname : host) == -1) { | 665 | tls_expectname ? tls_expectname : host) == -1) { |
664 | errx(1, "tls connection failed (%s)", | 666 | errx(1, "tls connection failed (%s)", |
@@ -681,6 +683,7 @@ struct tls * | |||
681 | tls_setup_server(struct tls *tls_ctx, int connfd, char *host) | 683 | tls_setup_server(struct tls *tls_ctx, int connfd, char *host) |
682 | { | 684 | { |
683 | struct tls *tls_cctx; | 685 | struct tls *tls_cctx; |
686 | |||
684 | if (tls_accept_socket(tls_ctx, &tls_cctx, | 687 | if (tls_accept_socket(tls_ctx, &tls_cctx, |
685 | connfd) == -1) { | 688 | connfd) == -1) { |
686 | warnx("tls accept failed (%s)", | 689 | warnx("tls accept failed (%s)", |
@@ -688,6 +691,7 @@ tls_setup_server(struct tls *tls_ctx, int connfd, char *host) | |||
688 | tls_cctx = NULL; | 691 | tls_cctx = NULL; |
689 | } else { | 692 | } else { |
690 | int i; | 693 | int i; |
694 | |||
691 | do { | 695 | do { |
692 | if ((i = tls_handshake(tls_cctx)) == -1) | 696 | if ((i = tls_handshake(tls_cctx)) == -1) |
693 | warnx("tls handshake failed (%s)", | 697 | warnx("tls handshake failed (%s)", |
@@ -696,6 +700,7 @@ tls_setup_server(struct tls *tls_ctx, int connfd, char *host) | |||
696 | } | 700 | } |
697 | if (tls_cctx) { | 701 | if (tls_cctx) { |
698 | int gotcert = tls_peer_cert_provided(tls_cctx); | 702 | int gotcert = tls_peer_cert_provided(tls_cctx); |
703 | |||
699 | if (gotcert && tls_peer_cert_hash(tls_cctx, &tls_peerhash) == -1) | 704 | if (gotcert && tls_peer_cert_hash(tls_cctx, &tls_peerhash) == -1) |
700 | warn("hash of peer certificate failed"); | 705 | warn("hash of peer certificate failed"); |
701 | if (vflag && gotcert) | 706 | if (vflag && gotcert) |
@@ -726,13 +731,12 @@ unix_connect(char *path) | |||
726 | int s; | 731 | int s; |
727 | 732 | ||
728 | if (uflag) { | 733 | if (uflag) { |
729 | if ((s = unix_bind(unix_dg_tmp_socket)) < 0) | 734 | if ((s = unix_bind(unix_dg_tmp_socket, SOCK_CLOEXEC)) < 0) |
730 | return (-1); | 735 | return (-1); |
731 | } else { | 736 | } else { |
732 | if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) | 737 | if ((s = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)) < 0) |
733 | return (-1); | 738 | return (-1); |
734 | } | 739 | } |
735 | (void)fcntl(s, F_SETFD, FD_CLOEXEC); | ||
736 | 740 | ||
737 | memset(&sun, 0, sizeof(struct sockaddr_un)); | 741 | memset(&sun, 0, sizeof(struct sockaddr_un)); |
738 | sun.sun_family = AF_UNIX; | 742 | sun.sun_family = AF_UNIX; |
@@ -759,7 +763,7 @@ int | |||
759 | unix_listen(char *path) | 763 | unix_listen(char *path) |
760 | { | 764 | { |
761 | int s; | 765 | int s; |
762 | if ((s = unix_bind(path)) < 0) | 766 | if ((s = unix_bind(path, 0)) < 0) |
763 | return (-1); | 767 | return (-1); |
764 | 768 | ||
765 | if (listen(s, 5) < 0) { | 769 | if (listen(s, 5) < 0) { |