2010-03-14 19:50:14 +00:00
|
|
|
/*
|
2012-03-19 16:21:12 +00:00
|
|
|
* virauth.c: authentication related utility functions
|
2010-03-14 19:50:14 +00:00
|
|
|
*
|
2012-07-24 14:08:46 +00:00
|
|
|
* Copyright (C) 2012 Red Hat, Inc.
|
2010-03-14 19:50:14 +00:00
|
|
|
* Copyright (C) 2010 Matthias Bolte <matthias.bolte@googlemail.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2010-03-14 19:50:14 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2012-03-20 15:40:05 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2012-03-19 16:21:12 +00:00
|
|
|
#include "virauth.h"
|
2012-12-13 17:44:57 +00:00
|
|
|
#include "virutil.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2012-03-20 15:40:05 +00:00
|
|
|
#include "datatypes.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2012-03-20 15:40:05 +00:00
|
|
|
#include "configmake.h"
|
2012-03-20 11:11:10 +00:00
|
|
|
#include "virauthconfig.h"
|
2013-04-03 10:36:23 +00:00
|
|
|
#include "virstring.h"
|
2012-03-20 15:40:05 +00:00
|
|
|
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2012-07-24 14:08:46 +00:00
|
|
|
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 (!(*path = strdup(conn->uri->params[i].value)))
|
|
|
|
goto no_memory;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-03-20 15:40:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-24 12:29:42 +00:00
|
|
|
if (!(userdir = virGetUserConfigDirectory()))
|
2012-03-20 15:40:05 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2012-05-03 16:36:27 +00:00
|
|
|
if (virAsprintf(path, "%s/auth.conf", userdir) < 0)
|
2012-03-20 15:40:05 +00:00
|
|
|
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;
|
|
|
|
}
|
2010-03-14 19:50:14 +00:00
|
|
|
|
|
|
|
|
2012-03-20 11:11:10 +00:00
|
|
|
static int
|
|
|
|
virAuthGetCredential(virConnectPtr conn,
|
|
|
|
const char *servicename,
|
|
|
|
const char *credname,
|
|
|
|
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 (!(config = virAuthConfigNew(path)))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virAuthConfigLookup(config,
|
|
|
|
servicename,
|
2012-09-12 15:40:08 +00:00
|
|
|
VIR_URI_SERVER(conn->uri),
|
2012-03-20 11:11:10 +00:00
|
|
|
credname,
|
|
|
|
&tmp) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (tmp &&
|
|
|
|
!(*value = strdup(tmp))) {
|
|
|
|
virReportOOMError();
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
virAuthConfigFree(config);
|
|
|
|
VIR_FREE(path);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-14 19:50:14 +00:00
|
|
|
char *
|
2012-03-20 11:11:10 +00:00
|
|
|
virAuthGetUsername(virConnectPtr conn,
|
|
|
|
virConnectAuthPtr auth,
|
|
|
|
const char *servicename,
|
|
|
|
const char *defaultUsername,
|
2010-03-14 19:50:14 +00:00
|
|
|
const char *hostname)
|
|
|
|
{
|
|
|
|
unsigned int ncred;
|
|
|
|
virConnectCredential cred;
|
|
|
|
char *prompt;
|
2012-03-20 11:11:10 +00:00
|
|
|
char *ret = NULL;
|
|
|
|
|
|
|
|
if (virAuthGetCredential(conn, servicename, "username", &ret) < 0)
|
|
|
|
return NULL;
|
|
|
|
if (ret != NULL)
|
|
|
|
return ret;
|
2010-03-14 19:50:14 +00:00
|
|
|
|
2012-03-29 09:52:04 +00:00
|
|
|
memset(&cred, 0, sizeof(virConnectCredential));
|
2010-03-14 19:50:14 +00:00
|
|
|
|
|
|
|
if (defaultUsername != NULL) {
|
|
|
|
if (virAsprintf(&prompt, _("Enter username for %s [%s]"), hostname,
|
|
|
|
defaultUsername) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (virAsprintf(&prompt, _("Enter username for %s"), hostname) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ncred = 0; ncred < auth->ncredtype; ncred++) {
|
|
|
|
if (auth->credtype[ncred] != VIR_CRED_AUTHNAME) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
cred.type = VIR_CRED_AUTHNAME;
|
|
|
|
cred.prompt = prompt;
|
|
|
|
cred.challenge = hostname;
|
|
|
|
cred.defresult = defaultUsername;
|
|
|
|
cred.result = NULL;
|
|
|
|
cred.resultlen = 0;
|
|
|
|
|
|
|
|
if ((*(auth->cb))(&cred, 1, auth->cbdata) < 0) {
|
|
|
|
VIR_FREE(cred.result);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FREE(prompt);
|
|
|
|
|
|
|
|
return cred.result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char *
|
2012-03-20 11:11:10 +00:00
|
|
|
virAuthGetPassword(virConnectPtr conn,
|
|
|
|
virConnectAuthPtr auth,
|
|
|
|
const char *servicename,
|
|
|
|
const char *username,
|
2010-03-14 19:50:14 +00:00
|
|
|
const char *hostname)
|
|
|
|
{
|
|
|
|
unsigned int ncred;
|
|
|
|
virConnectCredential cred;
|
|
|
|
char *prompt;
|
2012-03-20 11:11:10 +00:00
|
|
|
char *ret = NULL;
|
|
|
|
|
|
|
|
if (virAuthGetCredential(conn, servicename, "password", &ret) < 0)
|
|
|
|
return NULL;
|
|
|
|
if (ret != NULL)
|
|
|
|
return ret;
|
2010-03-14 19:50:14 +00:00
|
|
|
|
2012-03-29 09:52:04 +00:00
|
|
|
memset(&cred, 0, sizeof(virConnectCredential));
|
2010-03-14 19:50:14 +00:00
|
|
|
|
|
|
|
if (virAsprintf(&prompt, _("Enter %s's password for %s"), username,
|
|
|
|
hostname) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ncred = 0; ncred < auth->ncredtype; ncred++) {
|
|
|
|
if (auth->credtype[ncred] != VIR_CRED_PASSPHRASE &&
|
|
|
|
auth->credtype[ncred] != VIR_CRED_NOECHOPROMPT) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
cred.type = auth->credtype[ncred];
|
|
|
|
cred.prompt = prompt;
|
|
|
|
cred.challenge = hostname;
|
|
|
|
cred.defresult = NULL;
|
|
|
|
cred.result = NULL;
|
|
|
|
cred.resultlen = 0;
|
|
|
|
|
|
|
|
if ((*(auth->cb))(&cred, 1, auth->cbdata) < 0) {
|
|
|
|
VIR_FREE(cred.result);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FREE(prompt);
|
|
|
|
|
|
|
|
return cred.result;
|
|
|
|
}
|