]> xenbits.xensource.com Git - libvirt.git/commitdiff
remote: fix logic for known_hosts and keyfile checks
authorPino Toscano <ptoscano@redhat.com>
Tue, 10 Jan 2017 18:43:19 +0000 (19:43 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 11 Jan 2017 12:37:45 +0000 (13:37 +0100)
If any of them is specified for the libssh and libssh2 drivers, there is
no need to depend on checks based on other paths: in particular, a
specified path for known_hosts was ignored if the local config directory
could not be determined, and the path for keyfile was ignored if the
home could not be determined.

Instead, lazily determine and use these two paths only in case they are
needed.

src/rpc/virnetclient.c

index 34475d92770105857a6a093c60e444ce04b8cc13..9c781e37139c71c426fedbcab732c5a0bd5ef39c 100644 (file)
@@ -450,32 +450,34 @@ virNetClientPtr virNetClientNewLibSSH2(const char *host,
     char *nc = NULL;
     char *command = NULL;
 
-    char *homedir = virGetUserDirectory();
-    char *confdir = virGetUserConfigDirectory();
+    char *homedir = NULL;
+    char *confdir = NULL;
     char *knownhosts = NULL;
     char *privkey = NULL;
 
     /* Use default paths for known hosts an public keys if not provided */
-    if (confdir) {
-        if (!knownHostsPath) {
+    if (knownHostsPath) {
+        if (VIR_STRDUP(knownhosts, knownHostsPath) < 0)
+            goto cleanup;
+    } else {
+        confdir = virGetUserConfigDirectory();
+        if (confdir) {
             if (virFileExists(confdir)) {
                 virBufferAsprintf(&buf, "%s/known_hosts", confdir);
                 if (!(knownhosts = virBufferContentAndReset(&buf)))
                     goto no_memory;
             }
-        } else {
-            if (VIR_STRDUP(knownhosts, knownHostsPath) < 0)
-                goto cleanup;
         }
     }
 
-    if (homedir) {
-        if (!privkeyPath) {
+    if (privkeyPath) {
+        if (VIR_STRDUP(privkey, privkeyPath) < 0)
+            goto cleanup;
+    } else {
+        homedir = virGetUserDirectory();
+        if (homedir) {
             if (virNetClientFindDefaultSshKey(homedir, &privkey) < 0)
                 goto no_memory;
-        } else {
-            if (VIR_STRDUP(privkey, privkeyPath) < 0)
-                goto cleanup;
         }
     }
 
@@ -559,31 +561,33 @@ virNetClientPtr virNetClientNewLibssh(const char *host,
     char *nc = NULL;
     char *command = NULL;
 
-    char *homedir = virGetUserDirectory();
-    char *confdir = virGetUserConfigDirectory();
+    char *homedir = NULL;
+    char *confdir = NULL;
     char *knownhosts = NULL;
     char *privkey = NULL;
 
     /* Use default paths for known hosts an public keys if not provided */
-    if (confdir) {
-        if (!knownHostsPath) {
+    if (knownHostsPath) {
+        if (VIR_STRDUP(knownhosts, knownHostsPath) < 0)
+            goto cleanup;
+    } else {
+        confdir = virGetUserConfigDirectory();
+        if (confdir) {
             if (virFileExists(confdir)) {
                 if (virAsprintf(&knownhosts, "%s/known_hosts", confdir) < 0)
                     goto cleanup;
             }
-        } else {
-            if (VIR_STRDUP(knownhosts, knownHostsPath) < 0)
-                goto cleanup;
         }
     }
 
-    if (homedir) {
-        if (!privkeyPath) {
+    if (privkeyPath) {
+        if (VIR_STRDUP(privkey, privkeyPath) < 0)
+            goto cleanup;
+    } else {
+        homedir = virGetUserDirectory();
+        if (homedir) {
             if (virNetClientFindDefaultSshKey(homedir, &privkey) < 0)
                 goto no_memory;
-        } else {
-            if (VIR_STRDUP(privkey, privkeyPath) < 0)
-                goto cleanup;
         }
     }