Automatically create and delete a source Unix domain DGRAM socket.

Patch based on one by Tomas Hozza.
http://seclists.org/nmap-dev/2012/q4/334
This commit is contained in:
david 2012-11-27 21:47:26 +00:00
parent d52a6fd23c
commit 9f3fa403ef
3 changed files with 37 additions and 4 deletions

View file

@ -767,8 +767,11 @@
Both stream and datagram domain sockets are supported. Use
<option>-U</option> on its own for stream sockets, or
combine it with <option>--udp</option> for datagram sockets.
Datagram sockets require the use of the <option>--source</option>
option to specify a source socket to connect from.
Datagram sockets require a source socket to connect from. By
default, a source socket with a random filename will be created as
needed, and deleted when the program ends. Use the
<option>--source</option> with a path to use a source socket with a
specific name.
</para>
</refsect1>

View file

@ -519,10 +519,19 @@ int ncat_connect(void)
if (o.af == AF_UNIX && o.udp)
{
if (srcaddr.storage.ss_family != AF_UNIX) {
bye("A source socket filename (--source) is required when using\n"
"Unix domain sockets in --udp mode.");
char *tmp_name = NULL;
/* 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).");
srcaddr.un.sun_family = AF_UNIX;
strncpy(srcaddr.un.sun_path, tmp_name, sizeof(srcaddr.un.sun_path));
free (tmp_name);
}
nsi_set_localaddr(cs.sock_nsi, &srcaddr.storage, SUN_LEN((struct sockaddr_un *)&srcaddr.storage));
if (o.verbose)
loguser("[%s] used as source DGRAM Unix domain socket.\n", srcaddr.un.sun_path);
}
else
#endif
@ -675,6 +684,14 @@ int ncat_connect(void)
nsi_get_read_count(cs.sock_nsi), time);
}
#if HAVE_SYS_UN_H
if (o.af == AF_UNIX && o.udp) {
if (o.verbose)
loguser("Deleting source DGRAM Unix domain socket. [%s]\n", srcaddr.un.sun_path);
unlink(srcaddr.un.sun_path);
}
#endif
nsp_delete(mypool);
return rc == NSOCK_LOOP_ERROR ? 1 : 0;

View file

@ -548,6 +548,19 @@ sub {
kill_children;
unlink($UNIXSOCK);
($s_pid, $s_out, $s_in) = ncat("-l", "-U", "--udp", $UNIXSOCK);
test "Server UNIX socket listen on $UNIXSOCK --udp (DGRAM)",
sub {
my $resp;
unlink($UNIXSOCK);
my ($c_pid, $c_out, $c_in) = ncat("-U", "--udp", $UNIXSOCK);
syswrite($c_in, "abc\n");
$resp = timeout_read($s_out);
$resp eq "abc\n" or die "Server got \"$resp\", not \"abc\\n\" from client";
};
kill_children;
unlink($UNIXSOCK);
server_client_test "Connect success exit code",
[], ["--send-only"], sub {