[libvirt] [PATCH 11/14] Add helper API for finding auth file path
Osier Yang
jyang at redhat.com
Thu Mar 22 08:34:16 UTC 2012
On 2012年03月21日 01:33, Daniel P. Berrange wrote:
> From: "Daniel P. Berrange"<berrange at 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;
virAuthGetConfigFilePath;
virAuthGetPassword;
virAuthGetUsername;
>
>
> # 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);
*path will be NULL if the last choice of authfile (e.g.
/etc/libvirt/auth.conf) is not readable, while the function
will return 0.
ACK with this fixed.
More information about the libvir-list
mailing list