libvirt/src/util/virauthconfig.c
Michal Privoznik 7c9a2d88cd virutil: Move string related functions to virstring.c
The source code base needs to be adapted as well. Some files
include virutil.h just for the string related functions (here,
the include is substituted to match the new file), some include
virutil.h without any need (here, the include is removed), and
some require both.
2013-05-02 16:56:55 +02:00

171 lines
3.9 KiB
C

/*
* virauthconfig.c: authentication config handling
*
* Copyright (C) 2012 Red Hat, Inc.
*
* 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
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*
* Author: Daniel P. Berrange <berrange@redhat.com>
*/
#include <config.h>
#include "virauthconfig.h"
#include "virkeyfile.h"
#include "viralloc.h"
#include "virlog.h"
#include "virerror.h"
#include "virstring.h"
struct _virAuthConfig {
virKeyFilePtr keyfile;
char *path;
};
#define VIR_FROM_THIS VIR_FROM_NONE
virAuthConfigPtr virAuthConfigNew(const char *path)
{
virAuthConfigPtr auth;
if (VIR_ALLOC(auth) < 0) {
virReportOOMError();
goto error;
}
if (!(auth->path = strdup(path))) {
virReportOOMError();
goto error;
}
if (!(auth->keyfile = virKeyFileNew()))
goto error;
if (virKeyFileLoadFile(auth->keyfile, path) < 0)
goto error;
return auth;
error:
virAuthConfigFree(auth);
return NULL;
}
virAuthConfigPtr virAuthConfigNewData(const char *path,
const char *data,
size_t len)
{
virAuthConfigPtr auth;
if (VIR_ALLOC(auth) < 0) {
virReportOOMError();
goto error;
}
if (!(auth->path = strdup(path))) {
virReportOOMError();
goto error;
}
if (!(auth->keyfile = virKeyFileNew()))
goto error;
if (virKeyFileLoadData(auth->keyfile, path, data, len) < 0)
goto error;
return auth;
error:
virAuthConfigFree(auth);
return NULL;
}
void virAuthConfigFree(virAuthConfigPtr auth)
{
if (!auth)
return;
virKeyFileFree(auth->keyfile);
VIR_FREE(auth->path);
VIR_FREE(auth);
}
int virAuthConfigLookup(virAuthConfigPtr auth,
const char *service,
const char *hostname,
const char *credname,
const char **value)
{
char *authgroup = NULL;
char *credgroup = NULL;
const char *authcred;
int ret = -1;
*value = NULL;
VIR_DEBUG("Lookup '%s' '%s' '%s'", service, NULLSTR(hostname), credname);
if (!hostname)
hostname = "localhost";
if (virAsprintf(&authgroup, "auth-%s-%s", service, hostname) < 0) {
virReportOOMError();
goto cleanup;
}
if (!virKeyFileHasGroup(auth->keyfile, authgroup)) {
ret = 0;
goto cleanup;
}
if (!(authcred = virKeyFileGetValueString(auth->keyfile, authgroup, "credentials"))) {
virReportError(VIR_ERR_CONF_SYNTAX,
_("Missing item 'credentials' in group '%s' in '%s'"),
authgroup, auth->path);
goto cleanup;
}
if (virAsprintf(&credgroup, "credentials-%s", authcred) < 0) {
virReportOOMError();
goto cleanup;
}
if (!virKeyFileHasGroup(auth->keyfile, credgroup)) {
virReportError(VIR_ERR_CONF_SYNTAX,
_("Missing group 'credentials-%s' referenced from group '%s' in '%s'"),
authcred, authgroup, auth->path);
goto cleanup;
}
if (!virKeyFileHasValue(auth->keyfile, credgroup, credname)) {
ret = 0;
goto cleanup;
}
*value = virKeyFileGetValueString(auth->keyfile, credgroup, credname);
ret = 0;
cleanup:
VIR_FREE(authgroup);
VIR_FREE(credgroup);
return ret;
}