Use virBufferEscapeShell in virNetSocketNewConnectSSH

to escape the netcat command since it's passed to the shell. Adjust
expected test case output accordingly.
This commit is contained in:
Guido Günther 2011-10-13 21:49:01 +02:00
parent 920487b36d
commit 6ac6238de3
2 changed files with 39 additions and 13 deletions

View File

@ -612,7 +612,10 @@ int virNetSocketNewConnectSSH(const char *nodename,
const char *path, const char *path,
virNetSocketPtr *retsock) virNetSocketPtr *retsock)
{ {
char *quoted;
virCommandPtr cmd; virCommandPtr cmd;
virBuffer buf = VIR_BUFFER_INITIALIZER;
*retsock = NULL; *retsock = NULL;
cmd = virCommandNew(binary ? binary : "ssh"); cmd = virCommandNew(binary ? binary : "ssh");
@ -639,6 +642,14 @@ int virNetSocketNewConnectSSH(const char *nodename,
netcat = "nc"; netcat = "nc";
virCommandAddArgList(cmd, nodename, "sh", "-c", NULL); virCommandAddArgList(cmd, nodename, "sh", "-c", NULL);
virBufferEscapeShell(&buf, netcat);
if (virBufferError(&buf)) {
virBufferFreeAndReset(&buf);
virReportOOMError();
return -1;
}
quoted = virBufferContentAndReset(&buf);
/* /*
* This ugly thing is a shell script to detect availability of * This ugly thing is a shell script to detect availability of
* the -q option for 'nc': debian and suse based distros need this * the -q option for 'nc': debian and suse based distros need this
@ -650,14 +661,15 @@ int virNetSocketNewConnectSSH(const char *nodename,
* behavior. * behavior.
*/ */
virCommandAddArgFormat(cmd, virCommandAddArgFormat(cmd,
"'if %s -q 2>&1 | grep \"requires an argument\" >/dev/null 2>&1; then " "'if '%s' -q 2>&1 | grep \"requires an argument\" >/dev/null 2>&1; then "
"ARG=-q0;" "ARG=-q0;"
"else " "else "
"ARG=;" "ARG=;"
"fi;" "fi;"
"%s $ARG -U %s'", "'%s' $ARG -U %s'",
netcat, netcat, path); quoted, quoted, path);
VIR_FREE(quoted);
return virNetSocketNewConnectCommand(cmd, retsock); return virNetSocketNewConnectCommand(cmd, retsock);
} }

View File

@ -496,12 +496,12 @@ mymain(void)
struct testSSHData sshData1 = { struct testSSHData sshData1 = {
.nodename = "somehost", .nodename = "somehost",
.path = "/tmp/socket", .path = "/tmp/socket",
.expectOut = "somehost sh -c 'if nc -q 2>&1 | grep \"requires an argument\" >/dev/null 2>&1; then " .expectOut = "somehost sh -c 'if 'nc' -q 2>&1 | grep \"requires an argument\" >/dev/null 2>&1; then "
"ARG=-q0;" "ARG=-q0;"
"else " "else "
"ARG=;" "ARG=;"
"fi;" "fi;"
"nc $ARG -U /tmp/socket'\n", "'nc' $ARG -U /tmp/socket'\n",
}; };
if (virtTestRun("SSH test 1", 1, testSocketSSH, &sshData1) < 0) if (virtTestRun("SSH test 1", 1, testSocketSSH, &sshData1) < 0)
ret = -1; ret = -1;
@ -515,12 +515,12 @@ mymain(void)
.noVerify = false, .noVerify = false,
.path = "/tmp/socket", .path = "/tmp/socket",
.expectOut = "-p 9000 -l fred -T -o BatchMode=yes -e none somehost sh -c '" .expectOut = "-p 9000 -l fred -T -o BatchMode=yes -e none somehost sh -c '"
"if netcat -q 2>&1 | grep \"requires an argument\" >/dev/null 2>&1; then " "if 'netcat' -q 2>&1 | grep \"requires an argument\" >/dev/null 2>&1; then "
"ARG=-q0;" "ARG=-q0;"
"else " "else "
"ARG=;" "ARG=;"
"fi;" "fi;"
"netcat $ARG -U /tmp/socket'\n", "'netcat' $ARG -U /tmp/socket'\n",
}; };
if (virtTestRun("SSH test 2", 1, testSocketSSH, &sshData2) < 0) if (virtTestRun("SSH test 2", 1, testSocketSSH, &sshData2) < 0)
ret = -1; ret = -1;
@ -534,12 +534,12 @@ mymain(void)
.noVerify = true, .noVerify = true,
.path = "/tmp/socket", .path = "/tmp/socket",
.expectOut = "-p 9000 -l fred -o StrictHostKeyChecking=no somehost sh -c '" .expectOut = "-p 9000 -l fred -o StrictHostKeyChecking=no somehost sh -c '"
"if netcat -q 2>&1 | grep \"requires an argument\" >/dev/null 2>&1; then " "if 'netcat' -q 2>&1 | grep \"requires an argument\" >/dev/null 2>&1; then "
"ARG=-q0;" "ARG=-q0;"
"else " "else "
"ARG=;" "ARG=;"
"fi;" "fi;"
"netcat $ARG -U /tmp/socket'\n", "'netcat' $ARG -U /tmp/socket'\n",
}; };
if (virtTestRun("SSH test 3", 1, testSocketSSH, &sshData3) < 0) if (virtTestRun("SSH test 3", 1, testSocketSSH, &sshData3) < 0)
ret = -1; ret = -1;
@ -556,12 +556,12 @@ mymain(void)
.nodename = "crashyhost", .nodename = "crashyhost",
.path = "/tmp/socket", .path = "/tmp/socket",
.expectOut = "crashyhost sh -c " .expectOut = "crashyhost sh -c "
"'if nc -q 2>&1 | grep \"requires an argument\" >/dev/null 2>&1; then " "'if 'nc' -q 2>&1 | grep \"requires an argument\" >/dev/null 2>&1; then "
"ARG=-q0;" "ARG=-q0;"
"else " "else "
"ARG=;" "ARG=;"
"fi;" "fi;"
"nc $ARG -U /tmp/socket'\n", "'nc' $ARG -U /tmp/socket'\n",
.dieEarly = true, .dieEarly = true,
}; };
if (virtTestRun("SSH test 5", 1, testSocketSSH, &sshData5) < 0) if (virtTestRun("SSH test 5", 1, testSocketSSH, &sshData5) < 0)
@ -573,16 +573,30 @@ mymain(void)
.keyfile = "/root/.ssh/example_key", .keyfile = "/root/.ssh/example_key",
.noVerify = true, .noVerify = true,
.expectOut = "-i /root/.ssh/example_key -o StrictHostKeyChecking=no example.com sh -c '" .expectOut = "-i /root/.ssh/example_key -o StrictHostKeyChecking=no example.com sh -c '"
"if nc -q 2>&1 | grep \"requires an argument\" >/dev/null 2>&1; then " "if 'nc' -q 2>&1 | grep \"requires an argument\" >/dev/null 2>&1; then "
"ARG=-q0;" "ARG=-q0;"
"else " "else "
"ARG=;" "ARG=;"
"fi;" "fi;"
"nc $ARG -U /tmp/socket'\n", "'nc' $ARG -U /tmp/socket'\n",
}; };
if (virtTestRun("SSH test 6", 1, testSocketSSH, &sshData6) < 0) if (virtTestRun("SSH test 6", 1, testSocketSSH, &sshData6) < 0)
ret = -1; ret = -1;
struct testSSHData sshData7 = {
.nodename = "somehost",
.netcat = "nc -4",
.path = "/tmp/socket",
.expectOut = "somehost sh -c 'if ''nc -4'' -q 2>&1 | grep \"requires an argument\" >/dev/null 2>&1; then "
"ARG=-q0;"
"else "
"ARG=;"
"fi;"
"''nc -4'' $ARG -U /tmp/socket'\n",
};
if (virtTestRun("SSH test 7", 1, testSocketSSH, &sshData7) < 0)
ret = -1;
#endif #endif
return (ret==0 ? EXIT_SUCCESS : EXIT_FAILURE); return (ret==0 ? EXIT_SUCCESS : EXIT_FAILURE);