[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH] virNetSocketNewConnectSSH: Escape netcat path too



What holds for netcat is also true for the socket path since it can be
part of the connection URI as well. Make both subject to the same amount
of shell parsing.
Cheers,
 -- Guido

---
 src/rpc/virnetsocket.c   |   30 +++++++++++++++++++++---------
 tests/virnetsockettest.c |   14 +++++++-------
 2 files changed, 28 insertions(+), 16 deletions(-)

diff --git a/src/rpc/virnetsocket.c b/src/rpc/virnetsocket.c
index e4eff49..d7a055f 100644
--- a/src/rpc/virnetsocket.c
+++ b/src/rpc/virnetsocket.c
@@ -612,9 +612,10 @@ int virNetSocketNewConnectSSH(const char *nodename,
                               const char *path,
                               virNetSocketPtr *retsock)
 {
-    char *quoted;
+    char *qnetcat, *qpath;
     virCommandPtr cmd;
-    virBuffer buf = VIR_BUFFER_INITIALIZER;
+    virBuffer ncbuf = VIR_BUFFER_INITIALIZER;
+    virBuffer pathbuf = VIR_BUFFER_INITIALIZER;
 
     *retsock = NULL;
 
@@ -643,13 +644,23 @@ int virNetSocketNewConnectSSH(const char *nodename,
 
     virCommandAddArgList(cmd, nodename, "sh", "-c", NULL);
 
-    virBufferEscapeShell(&buf, netcat);
-    if (virBufferError(&buf)) {
-        virBufferFreeAndReset(&buf);
+    virBufferEscapeShell(&ncbuf, netcat);
+    if (virBufferError(&ncbuf)) {
+        virBufferFreeAndReset(&ncbuf);
         virReportOOMError();
         return -1;
     }
-    quoted = virBufferContentAndReset(&buf);
+
+    virBufferEscapeShell(&pathbuf, path);
+    if (virBufferError(&pathbuf)) {
+        virBufferFreeAndReset(&ncbuf);
+        virBufferFreeAndReset(&pathbuf);
+        virReportOOMError();
+        return -1;
+    }
+
+    qnetcat = virBufferContentAndReset(&ncbuf);
+    qpath = virBufferContentAndReset(&pathbuf);
     /*
      * This ugly thing is a shell script to detect availability of
      * the -q option for 'nc': debian and suse based distros need this
@@ -666,10 +677,11 @@ int virNetSocketNewConnectSSH(const char *nodename,
          "else "
              "ARG=;"
          "fi;"
-         "'%s' $ARG -U %s'",
-         quoted, quoted, path);
+         "'%s' $ARG -U '%s",
+         qnetcat, qnetcat, qpath);
 
-    VIR_FREE(quoted);
+    VIR_FREE(qnetcat);
+    VIR_FREE(qpath);
     return virNetSocketNewConnectCommand(cmd, retsock);
 }
 
diff --git a/tests/virnetsockettest.c b/tests/virnetsockettest.c
index 6320ce0..5c93f0f 100644
--- a/tests/virnetsockettest.c
+++ b/tests/virnetsockettest.c
@@ -501,7 +501,7 @@ mymain(void)
                                      "else "
                                          "ARG=;"
                                      "fi;"
-                                     "'nc' $ARG -U /tmp/socket'\n",
+                                     "'nc' $ARG -U '/tmp/socket\n",
     };
     if (virtTestRun("SSH test 1", 1, testSocketSSH, &sshData1) < 0)
         ret = -1;
@@ -520,7 +520,7 @@ mymain(void)
                      "else "
                          "ARG=;"
                      "fi;"
-                     "'netcat' $ARG -U /tmp/socket'\n",
+                     "'netcat' $ARG -U '/tmp/socket\n",
     };
     if (virtTestRun("SSH test 2", 1, testSocketSSH, &sshData2) < 0)
         ret = -1;
@@ -539,7 +539,7 @@ mymain(void)
                      "else "
                          "ARG=;"
                      "fi;"
-                     "'netcat' $ARG -U /tmp/socket'\n",
+                     "'netcat' $ARG -U '/tmp/socket\n",
     };
     if (virtTestRun("SSH test 3", 1, testSocketSSH, &sshData3) < 0)
         ret = -1;
@@ -561,7 +561,7 @@ mymain(void)
                      "else "
                          "ARG=;"
                      "fi;"
-                     "'nc' $ARG -U /tmp/socket'\n",
+                     "'nc' $ARG -U '/tmp/socket\n",
         .dieEarly = true,
     };
     if (virtTestRun("SSH test 5", 1, testSocketSSH, &sshData5) < 0)
@@ -578,7 +578,7 @@ mymain(void)
                      "else "
                          "ARG=;"
                      "fi;"
-                     "'nc' $ARG -U /tmp/socket'\n",
+                     "'nc' $ARG -U '/tmp/socket\n",
     };
     if (virtTestRun("SSH test 6", 1, testSocketSSH, &sshData6) < 0)
         ret = -1;
@@ -586,13 +586,13 @@ mymain(void)
     struct testSSHData sshData7 = {
         .nodename = "somehost",
         .netcat = "nc -4",
-        .path = "/tmp/socket",
+        .path = "/tmp/so cket",
         .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",
+                                     "''nc -4'' $ARG -U ''/tmp/so cket'\n",
     };
     if (virtTestRun("SSH test 7", 1, testSocketSSH, &sshData7) < 0)
         ret = -1;
-- 
1.7.6.3


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]