diff --git a/ncat/config.h.in b/ncat/config.h.in index 3ef265ebf..be72c67ab 100644 --- a/ncat/config.h.in +++ b/ncat/config.h.in @@ -43,6 +43,9 @@ /* Define to 1 if you have the `memset' function. */ #undef HAVE_MEMSET +/* Define to 1 if you have the `mkstemp' function. */ +#undef HAVE_MKSTEMP + /* Define to 1 if you have the header file. */ #undef HAVE_NETDB_H diff --git a/ncat/configure b/ncat/configure index 3001a5e78..013bc7237 100755 --- a/ncat/configure +++ b/ncat/configure @@ -4401,7 +4401,7 @@ fi done -for ac_func in dup2 gettimeofday inet_ntoa memset select socket strcasecmp strchr strdup strerror strncasecmp strtol +for ac_func in dup2 gettimeofday inet_ntoa memset mkstemp select socket strcasecmp strchr strdup strerror strncasecmp strtol do : as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh` ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var" diff --git a/ncat/configure.ac b/ncat/configure.ac index f42e624ab..a3afade0a 100644 --- a/ncat/configure.ac +++ b/ncat/configure.ac @@ -51,7 +51,7 @@ AC_FUNC_FORK AC_FUNC_SELECT_ARGTYPES AC_TYPE_SIGNAL AC_FUNC_VPRINTF -AC_CHECK_FUNCS([dup2 gettimeofday inet_ntoa memset select socket strcasecmp strchr strdup strerror strncasecmp strtol]) +AC_CHECK_FUNCS([dup2 gettimeofday inet_ntoa memset mkstemp select socket strcasecmp strchr strdup strerror strncasecmp strtol]) AC_SEARCH_LIBS(setsockopt, socket) # Ncat does not call gethostbyname directly, but some of the libraries # it links to (such as libpcap) do. Instead it calls getaddrinfo. At diff --git a/ncat/ncat_connect.c b/ncat/ncat_connect.c index 1aea7f2a4..515b0cb39 100644 --- a/ncat/ncat_connect.c +++ b/ncat/ncat_connect.c @@ -909,9 +909,20 @@ int ncat_connect(void) { if (srcaddr.storage.ss_family != AF_UNIX) { char *tmp_name = NULL; +#if HAVE_MKSTEMP + char *tmpdir = getenv("TMPDIR"); + size_t size=0, offset=0; + strbuf_sprintf(&tmp_name, &size, &offset, "%s/ncat.XXXXXX", + tmpdir ? tmpdir : "/tmp"); + if (mkstemp(tmp_name) == -1) { + bye("Failed to create name for temporary DGRAM source Unix domain socket (mkstemp)."); + } + unlink(tmp_name); +#else /* If no source socket was specified, we have to create temporary one. */ if ((tmp_name = tempnam(NULL, "ncat.")) == NULL) bye("Failed to create name for temporary DGRAM source Unix domain socket (tempnam)."); +#endif srcaddr.un.sun_family = AF_UNIX; strncpy(srcaddr.un.sun_path, tmp_name, sizeof(srcaddr.un.sun_path));