netcat: user-provided socket path is unlinked

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

netcat: user-provided socket path is unlinked

astian
For unix datagram client sockets netcat will either generate a temporary
socket path or use the one provided by the user.  However it unlinks the
file in both cases.  Since the path for a listening unix sockets (which
is always provided by the user) is not unlinked, for consistency this
one should probably not be either.  Consider the patch below (applies
to CVS 1.206).

Note: you might have issues testing this, see other netcat-related
emails I sent.

Cheers.

diff --git netcat.c netcat.c
index c04298f..45501de 100644
--- netcat.c
+++ netcat.c
@@ -114,7 +114,7 @@ int recvcount, recvlimit;
 int timeout = -1;
 int family = AF_UNSPEC;
 char *portlist[PORT_MAX+1];
-char *unix_dg_tmp_socket;
+char *unix_dg_client_socket;
 int ttl = -1;
 int minttl = -1;
 
@@ -446,13 +446,13 @@ main(int argc, char *argv[])
  /* Get name of temporary socket for unix datagram client */
  if ((family == AF_UNIX) && uflag && !lflag) {
  if (sflag) {
- unix_dg_tmp_socket = sflag;
+ unix_dg_client_socket = sflag;
  } else {
  strlcpy(unix_dg_tmp_socket_buf, "/tmp/nc.XXXXXXXXXX",
     UNIX_DG_TMP_SOCKET_SIZE);
  if (mktemp(unix_dg_tmp_socket_buf) == NULL)
  err(1, "mktemp");
- unix_dg_tmp_socket = unix_dg_tmp_socket_buf;
+ unix_dg_client_socket = unix_dg_tmp_socket_buf;
  }
  }
 
@@ -648,8 +648,8 @@ main(int argc, char *argv[])
  ret = 1;
  }
 
- if (uflag)
- unlink(unix_dg_tmp_socket);
+ if (uflag && !sflag)
+ unlink(unix_dg_client_socket);
  return ret;
 
  } else {
@@ -863,7 +863,7 @@ unix_connect(char *path)
  int s, save_errno;
 
  if (uflag) {
- if ((s = unix_bind(unix_dg_tmp_socket, SOCK_CLOEXEC)) == -1)
+ if ((s = unix_bind(unix_dg_client_socket, SOCK_CLOEXEC)) == -1)
  return -1;
  } else {
  if ((s = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)) == -1)