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

[libvirt] [PATCH 1/3] virAuth: Don't require virConnectPtr to retrieve authentication creds



Previously a connection object was required to retrieve the auth
credentials. This patch adds the option to call the retrieval functions
only using the connection URI or path to the configuration file. This
will allow to use this toolkit to request passwords for ssh
authentication in the libssh2 connection driver.

Changes:
*virAuthGetConfigFilePathURI(): use URI to retrieve the config file path
*virAuthGetCredential(): Remove the need to propagate conn object

virAuthGetPasswordPath():
*virAuthGetUsernamePath(): New functions, that use config file path
                           instead of conn object
---
 src/util/virauth.c | 107 +++++++++++++++++++++++++++++++++++++----------------
 src/util/virauth.h |  17 ++++++++-
 2 files changed, 91 insertions(+), 33 deletions(-)

diff --git a/src/util/virauth.c b/src/util/virauth.c
index cd22e89..a19f55d 100644
--- a/src/util/virauth.c
+++ b/src/util/virauth.c
@@ -36,9 +36,9 @@

 #define VIR_FROM_THIS VIR_FROM_AUTH

-
-int virAuthGetConfigFilePath(virConnectPtr conn,
-                             char **path)
+int
+virAuthGetConfigFilePathURI(virURIPtr uri,
+                            char **path)
 {
     int ret = -1;
     size_t i;
@@ -56,13 +56,12 @@ int virAuthGetConfigFilePath(virConnectPtr conn,
         return 0;
     }

-    if (conn && conn->uri) {
-        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 (VIR_STRDUP(*path, conn->uri->params[i].value) < 0)
+    if (uri) {
+        for (i = 0; i < uri->paramsCount; i++) {
+            if (STREQ_NULLABLE(uri->params[i].name, "authfile") &&
+                uri->params[i].value) {
+                VIR_DEBUG("Using path from URI '%s'", uri->params[i].value);
+                if (VIR_STRDUP(*path, uri->params[i].value) < 0)
                     goto cleanup;
                 return 0;
             }
@@ -105,33 +104,36 @@ no_memory:
 }


+int
+virAuthGetConfigFilePath(virConnectPtr conn,
+                         char **path)
+{
+    return virAuthGetConfigFilePathURI(conn ? conn->uri : NULL, path);
+}
+
+
 static int
-virAuthGetCredential(virConnectPtr conn,
-                     const char *servicename,
+virAuthGetCredential(const char *servicename,
+                     const char *hostname,
                      const char *credname,
+                     const char *path,
                      char **value)
 {
     int ret = -1;
-    char *path = NULL;
     virAuthConfigPtr config = NULL;
     const char *tmp;

     *value = NULL;

-    if (virAuthGetConfigFilePath(conn, &path) < 0)
-        goto cleanup;
-
-    if (path == NULL) {
-        ret = 0;
-        goto cleanup;
-    }
+    if (path == NULL)
+        return 0;

     if (!(config = virAuthConfigNew(path)))
         goto cleanup;

     if (virAuthConfigLookup(config,
                             servicename,
-                            VIR_URI_SERVER(conn->uri),
+                            hostname,
                             credname,
                             &tmp) < 0)
         goto cleanup;
@@ -143,24 +145,23 @@ virAuthGetCredential(virConnectPtr conn,

 cleanup:
     virAuthConfigFree(config);
-    VIR_FREE(path);
     return ret;
 }


 char *
-virAuthGetUsername(virConnectPtr conn,
-                   virConnectAuthPtr auth,
-                   const char *servicename,
-                   const char *defaultUsername,
-                   const char *hostname)
+virAuthGetUsernamePath(const char *path,
+                       virConnectAuthPtr auth,
+                       const char *servicename,
+                       const char *defaultUsername,
+                       const char *hostname)
 {
     unsigned int ncred;
     virConnectCredential cred;
     char *prompt;
     char *ret = NULL;

-    if (virAuthGetCredential(conn, servicename, "username", &ret) < 0)
+    if (virAuthGetCredential(servicename, hostname, "username", path, &ret) < 0)
         return NULL;
     if (ret != NULL)
         return ret;
@@ -203,20 +204,41 @@ virAuthGetUsername(virConnectPtr conn,
 }


-
 char *
-virAuthGetPassword(virConnectPtr conn,
+virAuthGetUsername(virConnectPtr conn,
                    virConnectAuthPtr auth,
                    const char *servicename,
-                   const char *username,
+                   const char *defaultUsername,
                    const char *hostname)
 {
+    char *ret;
+    char *path;
+
+    if (virAuthGetConfigFilePath(conn, &path) < 0)
+        return NULL;
+
+    ret = virAuthGetUsernamePath(path, auth, servicename,
+                                 defaultUsername, hostname);
+
+    VIR_FREE(path);
+
+    return ret;
+}
+
+
+char *
+virAuthGetPasswordPath(const char *path,
+                       virConnectAuthPtr auth,
+                       const char *servicename,
+                       const char *username,
+                       const char *hostname)
+{
     unsigned int ncred;
     virConnectCredential cred;
     char *prompt;
     char *ret = NULL;

-    if (virAuthGetCredential(conn, servicename, "password", &ret) < 0)
+    if (virAuthGetCredential(servicename, hostname, "password", path, &ret) < 0)
         return NULL;
     if (ret != NULL)
         return ret;
@@ -252,3 +274,24 @@ virAuthGetPassword(virConnectPtr conn,

     return cred.result;
 }
+
+
+char *
+virAuthGetPassword(virConnectPtr conn,
+                   virConnectAuthPtr auth,
+                   const char *servicename,
+                   const char *username,
+                   const char *hostname)
+{
+    char *ret;
+    char *path;
+
+    if (virAuthGetConfigFilePath(conn, &path) < 0)
+        return NULL;
+
+    ret = virAuthGetPasswordPath(path, auth, servicename, username, hostname);
+
+    VIR_FREE(path);
+
+    return ret;
+}
diff --git a/src/util/virauth.h b/src/util/virauth.h
index a24aef7..268eb34 100644
--- a/src/util/virauth.h
+++ b/src/util/virauth.h
@@ -1,6 +1,7 @@
 /*
  * virauth.h: authentication related utility functions
  *
+ * Copyright (C) 2013 Red Hat, Inc.
  * Copyright (C) 2010 Matthias Bolte <matthias bolte googlemail com>
  *
  * This library is free software; you can redistribute it and/or
@@ -23,10 +24,15 @@
 # define __VIR_AUTH_H__

 # include "internal.h"
+# include "viruri.h"

 int virAuthGetConfigFilePath(virConnectPtr conn,
                              char **path);

+int virAuthGetConfigFilePathURI(virURIPtr uri,
+                                char **path);
+
+
 char *virAuthGetUsername(virConnectPtr conn,
                          virConnectAuthPtr auth,
                          const char *servicename,
@@ -37,5 +43,14 @@ char *virAuthGetPassword(virConnectPtr conn,
                          const char *servicename,
                          const char *username,
                          const char *hostname);
-
+char * virAuthGetUsernamePath(const char *path,
+                              virConnectAuthPtr auth,
+                              const char *servicename,
+                              const char *defaultUsername,
+                              const char *hostname);
+char * virAuthGetPasswordPath(const char *path,
+                              virConnectAuthPtr auth,
+                              const char *servicename,
+                              const char *username,
+                              const char *hostname);
 #endif /* __VIR_AUTH_H__ */
-- 
1.8.2.1


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