ALT Linux repositórios
S: | 4.0.20100725-alt3 |
5.0: | 4.0.20061122-alt2 |
4.1: | 4.0.20061122-alt1 |
4.0: | 4.0.20061122-alt1 |
3.0: | 3.6-alt2 |
Group :: Rede/Outros
RPM: netcat
Main Changelog Spec Patches Sources Download Gear Bugs e FR Repocop
Patch: netcat-4.0.20100725-alt-execcmd.patch
Download
Download
--- src/usr.bin/nc/nc.1
+++ src/usr.bin/nc/nc.1
@@ -35,6 +35,7 @@
.Nm nc
.Bk -words
.Op Fl 46DdhklnrStUuvz
+.Op Fl e Ar command
.Op Fl I Ar length
.Op Fl i Ar interval
.Op Fl O Ar length
@@ -100,6 +101,9 @@ to use IPv6 addresses only.
Enable debugging on the socket.
.It Fl d
Do not attempt to read from stdin.
+.It Fl e Ar command
+Starts specified command for communicating with remote side
+after connection is established.
.It Fl h
Prints out
.Nm
@@ -415,6 +419,11 @@ The same example again, this time enabling proxy authentication with username
if the proxy requires it:
.Pp
.Dl $ nc -x10.2.3.4:8080 -Xconnect -Pruser host.example.com 42
+.Pp
+Listen on port 1234, send system name to connected client and exit:
+.Pp
+.Dl $ nc -l -v -e 'cat /etc/altlinux-release; uname -a' 1234
+.Pp
.Sh SEE ALSO
.Xr cat 1 ,
.Xr ssh 1
--- src/usr.bin/nc/netcat.c
+++ src/usr.bin/nc/netcat.c
@@ -104,6 +104,7 @@ int Tflag = -1; /* IP Type of Service */
#ifdef HAVE_SO_RTABLE
u_int rtableid;
#endif
+char *opt_exec = NULL; /* Program to execute after connecting */
int timeout = -1;
int family = AF_INET;
@@ -111,6 +112,7 @@ char *portlist[PORT_MAX+1];
void atelnet(int, unsigned char *, unsigned int);
void build_ports(char *);
+void execcmd(int fd);
void help(void);
int local_listen(char *, char *, struct addrinfo);
void readwrite(int);
@@ -168,7 +170,7 @@ main(int argc, char *argv[])
usage(1);
while ((ch = getopt(argc, argv,
- "46DdhI:i:jklnO:P:p:rSs:tT:UuV:vw:X:x:z")) != -1) {
+ "46Dde:hI:i:jklnO:P:p:rSs:tT:UuV:vw:X:x:z")) != -1) {
switch (ch) {
case '4':
family = AF_INET;
@@ -192,6 +194,11 @@ main(int argc, char *argv[])
case 'd':
dflag = 1;
break;
+ case 'e': /* prog to exec */
+ if (opt_exec)
+ errx(1, "cannot specify `-e' option double");
+ opt_exec = strdup(optarg);
+ break;
case 'h':
help();
break;
@@ -317,6 +324,8 @@ main(int argc, char *argv[])
errx(1, "cannot use -p and -l");
if (lflag && zflag)
errx(1, "cannot use -z and -l");
+ if (zflag && opt_exec)
+ errx(1, "cannot use -z and -e");
if (!lflag && kflag)
errx(1, "must use -l with -k");
@@ -490,6 +499,43 @@ main(int argc, char *argv[])
}
/*
+ * execcmd()
+ * Execute an external file making its stdin/stdout/stderr
+ * the actual socket. Returns never.
+ */
+void
+execcmd(int fd)
+{
+ int saved_stderr;
+ char *p;
+
+ /* save the stderr fd because we may need it later */
+ saved_stderr = dup(STDERR_FILENO);
+
+ /* duplicate the socket for the child program */
+ dup2(fd, STDIN_FILENO); /* the precise order of fiddlage */
+ close(fd); /* is apparently crucial; this is */
+ dup2(STDIN_FILENO, STDOUT_FILENO); /* swiped directly out of "inetd". */
+ dup2(STDIN_FILENO, STDERR_FILENO); /* also duplicate the stderr channel */
+
+ /* change the label for the executed program */
+ if ((p = strrchr(opt_exec, '/'))) {
+ p++; /* shorter argv[0] */
+ } else {
+ p = opt_exec;
+ }
+
+ /* replace this process with the new one */
+#ifndef USE_OLD_COMPAT
+ execl("/bin/sh", p, "-c", opt_exec, NULL);
+#else
+ execl(opt_exec, p, NULL);
+#endif
+ dup2(saved_stderr, STDERR_FILENO);
+ err(1, "couldn't execute %s ", opt_exec);
+}
+
+/*
* unix_connect()
* Returns a socket connected to a local unix socket. Returns -1 on failure.
*/
@@ -705,6 +751,12 @@ readwrite(int nfd)
int lfd = fileno(stdout);
int plen;
+ if (opt_exec) {
+ if (vflag)
+ printf("Call sub-process %s\n", opt_exec);
+ execcmd(nfd);
+ }
+
#ifdef HAVE_SO_JUMBO
plen = jflag ? 16384 : 2048;
#else
@@ -968,7 +1020,7 @@ void
usage(int ret)
{
fprintf(stderr,
- "usage: netcat [-46DdhklnrtUuvz] [-I length] [-i interval] [-O length]\n"
+ "usage: netcat [-46DdhklnrtUuvz] [-e cmd] [-I length] [-i interval] [-O length]\n"
" [-P proxy_username] [-p source_port] [-s source_ip_address] [-T ToS]\n"
" [-w timeout] [-X proxy_protocol]\n"
" [-x proxy_address[:port]] [hostname] [port]\n");