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

[libvirt] [PATCH 11/14] Add helper API for finding auth file path



From: "Daniel P. Berrange" <berrange redhat com>

* src/util/virauth.c, src/util/virauth.h: Add virAuthGetConfigFilePath
* include/libvirt/virterror.h, src/util/virterror.c: Add
  VIR_FROM_AUTH error domain
---
 include/libvirt/virterror.h |    1 +
 src/libvirt_private.syms    |    1 +
 src/util/virauth.c          |   74 +++++++++++++++++++++++++++++++++++++++++++
 src/util/virauth.h          |    3 ++
 src/util/virterror.c        |    3 ++
 5 files changed, 82 insertions(+), 0 deletions(-)

diff --git a/include/libvirt/virterror.h b/include/libvirt/virterror.h
index c8ec8d4..e04d29e 100644
--- a/include/libvirt/virterror.h
+++ b/include/libvirt/virterror.h
@@ -86,6 +86,7 @@ typedef enum {
     VIR_FROM_HYPERV = 43,	/* Error from Hyper-V driver */
     VIR_FROM_CAPABILITIES = 44, /* Error from capabilities */
     VIR_FROM_URI = 45,          /* Error from URI handling */
+    VIR_FROM_AUTH = 46,         /* Error from auth handling */
 } virErrorDomain;
 
 
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 07302cd..6f53aa8 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1168,6 +1168,7 @@ virUUIDParse;
 # virauth.h
 virAuthGetUsername;
 virAuthGetPassword;
+virAuthGetConfigFilePath;
 
 
 # viraudit.h
diff --git a/src/util/virauth.c b/src/util/virauth.c
index d7375e9..150b8e7 100644
--- a/src/util/virauth.c
+++ b/src/util/virauth.c
@@ -21,9 +21,83 @@
 
 #include <config.h>
 
+#include <stdlib.h>
+
 #include "virauth.h"
 #include "util.h"
 #include "memory.h"
+#include "logging.h"
+#include "datatypes.h"
+#include "virterror_internal.h"
+#include "configmake.h"
+
+#define VIR_FROM_THIS VIR_FROM_AUTH
+
+
+int virAuthGetConfigFilePath(virConnectPtr conn,
+                             char **path)
+{
+    int ret = -1;
+    size_t i;
+    const char *authenv = getenv("LIBVIRT_AUTH_FILE");
+    char *userdir = NULL;
+
+    *path = NULL;
+
+    VIR_DEBUG("Determining auth config file path");
+
+    if (authenv) {
+        VIR_DEBUG("Using path from env '%s'", authenv);
+        if (!(*path = strdup(authenv)))
+            goto no_memory;
+        return 0;
+    }
+
+    for (i = 0 ; i < conn->uri->paramsCount ; i++) {
+        if (STREQ_NULLABLE(conn->uri->params[i].name, "authfile") &&
+            conn->uri->params[i].value) {
+            VIR_DEBUG("Using path from URI '%s'",
+                      conn->uri->params[i].value);
+            if (!(*path = strdup(conn->uri->params[i].value)))
+                goto no_memory;
+            return 0;
+        }
+    }
+
+    if (!(userdir = virGetUserDirectory(geteuid())))
+        goto cleanup;
+
+    if (virAsprintf(path, "%s/.libvirt/auth.conf", userdir) < 0)
+        goto no_memory;
+
+    VIR_DEBUG("Checking for readability of '%s'", *path);
+    if (access(*path, R_OK) == 0)
+        goto done;
+
+    VIR_FREE(*path);
+
+    if (!(*path = strdup(SYSCONFDIR "/libvirt/auth.conf")))
+        goto no_memory;
+
+    VIR_DEBUG("Checking for readability of '%s'", *path);
+    if (access(*path, R_OK) == 0)
+        goto done;
+
+    VIR_FREE(*path);
+
+done:
+    ret = 0;
+
+    VIR_DEBUG("Using auth file '%s'", NULLSTR(*path));
+cleanup:
+    VIR_FREE(userdir);
+
+    return ret;
+
+no_memory:
+    virReportOOMError();
+    goto cleanup;
+}
 
 
 char *
diff --git a/src/util/virauth.h b/src/util/virauth.h
index 8856701..7f43bee 100644
--- a/src/util/virauth.h
+++ b/src/util/virauth.h
@@ -24,6 +24,9 @@
 
 # include "internal.h"
 
+int virAuthGetConfigFilePath(virConnectPtr conn,
+                             char **path);
+
 char *virAuthGetUsername(virConnectAuthPtr auth, const char *defaultUsername,
                          const char *hostname);
 char *virAuthGetPassword(virConnectAuthPtr auth, const char *username,
diff --git a/src/util/virterror.c b/src/util/virterror.c
index e1fe522..9dc40a8 100644
--- a/src/util/virterror.c
+++ b/src/util/virterror.c
@@ -181,6 +181,9 @@ static const char *virErrorDomainName(virErrorDomain domain) {
         case VIR_FROM_URI:
             dom = "URI ";
             break;
+        case VIR_FROM_AUTH:
+            dom = "Auth ";
+            break;
     }
     return(dom);
 }
-- 
1.7.7.6


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