2012-03-20 15:40:05 +00:00
|
|
|
/*
|
|
|
|
* 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
|
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/>.
|
2012-03-20 15:40:05 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "virauthconfig.h"
|
|
|
|
|
|
|
|
#include "virkeyfile.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2013-04-03 10:36:23 +00:00
|
|
|
#include "virstring.h"
|
2019-04-01 14:28:05 +00:00
|
|
|
#include "viralloc.h"
|
2012-03-20 15:40:05 +00:00
|
|
|
|
|
|
|
struct _virAuthConfig {
|
|
|
|
virKeyFilePtr keyfile;
|
|
|
|
char *path;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("util.authconfig");
|
2012-03-20 15:40:05 +00:00
|
|
|
|
|
|
|
virAuthConfigPtr virAuthConfigNew(const char *path)
|
|
|
|
{
|
|
|
|
virAuthConfigPtr auth;
|
|
|
|
|
2013-07-04 10:17:18 +00:00
|
|
|
if (VIR_ALLOC(auth) < 0)
|
2012-03-20 15:40:05 +00:00
|
|
|
goto error;
|
|
|
|
|
2013-05-24 07:19:51 +00:00
|
|
|
if (VIR_STRDUP(auth->path, path) < 0)
|
2012-03-20 15:40:05 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!(auth->keyfile = virKeyFileNew()))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (virKeyFileLoadFile(auth->keyfile, path) < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
return auth;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
error:
|
2012-03-20 15:40:05 +00:00
|
|
|
virAuthConfigFree(auth);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virAuthConfigPtr virAuthConfigNewData(const char *path,
|
|
|
|
const char *data,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
virAuthConfigPtr auth;
|
|
|
|
|
2013-07-04 10:17:18 +00:00
|
|
|
if (VIR_ALLOC(auth) < 0)
|
2012-03-20 15:40:05 +00:00
|
|
|
goto error;
|
|
|
|
|
2013-05-24 07:19:51 +00:00
|
|
|
if (VIR_STRDUP(auth->path, path) < 0)
|
2012-03-20 15:40:05 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!(auth->keyfile = virKeyFileNew()))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (virKeyFileLoadData(auth->keyfile, path, data, len) < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
return auth;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
error:
|
2012-03-20 15:40:05 +00:00
|
|
|
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)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *authgroup = NULL;
|
|
|
|
g_autofree char *credgroup = NULL;
|
2012-03-20 15:40:05 +00:00
|
|
|
const char *authcred;
|
|
|
|
|
|
|
|
*value = NULL;
|
|
|
|
|
|
|
|
VIR_DEBUG("Lookup '%s' '%s' '%s'", service, NULLSTR(hostname), credname);
|
|
|
|
|
|
|
|
if (!hostname)
|
|
|
|
hostname = "localhost";
|
|
|
|
|
2013-07-04 10:17:18 +00:00
|
|
|
if (virAsprintf(&authgroup, "auth-%s-%s", service, hostname) < 0)
|
2018-07-13 17:54:52 +00:00
|
|
|
return -1;
|
2012-03-20 15:40:05 +00:00
|
|
|
|
|
|
|
if (!virKeyFileHasGroup(auth->keyfile, authgroup)) {
|
2016-06-13 09:29:57 +00:00
|
|
|
VIR_FREE(authgroup);
|
2016-06-16 10:17:58 +00:00
|
|
|
if (virAsprintf(&authgroup, "auth-%s-%s", service, "default") < 0)
|
2018-07-13 17:54:52 +00:00
|
|
|
return -1;
|
2016-06-13 09:29:57 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 17:54:52 +00:00
|
|
|
if (!virKeyFileHasGroup(auth->keyfile, authgroup))
|
|
|
|
return 0;
|
2012-03-20 15:40:05 +00:00
|
|
|
|
|
|
|
if (!(authcred = virKeyFileGetValueString(auth->keyfile, authgroup, "credentials"))) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_CONF_SYNTAX,
|
|
|
|
_("Missing item 'credentials' in group '%s' in '%s'"),
|
|
|
|
authgroup, auth->path);
|
2018-07-13 17:54:52 +00:00
|
|
|
return -1;
|
2012-03-20 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
2013-07-04 10:17:18 +00:00
|
|
|
if (virAsprintf(&credgroup, "credentials-%s", authcred) < 0)
|
2018-07-13 17:54:52 +00:00
|
|
|
return -1;
|
2012-03-20 15:40:05 +00:00
|
|
|
|
|
|
|
if (!virKeyFileHasGroup(auth->keyfile, credgroup)) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_CONF_SYNTAX,
|
|
|
|
_("Missing group 'credentials-%s' referenced from group '%s' in '%s'"),
|
|
|
|
authcred, authgroup, auth->path);
|
2018-07-13 17:54:52 +00:00
|
|
|
return -1;
|
2012-03-20 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 17:54:52 +00:00
|
|
|
if (!virKeyFileHasValue(auth->keyfile, credgroup, credname))
|
|
|
|
return 0;
|
2012-03-20 15:40:05 +00:00
|
|
|
|
|
|
|
*value = virKeyFileGetValueString(auth->keyfile, credgroup, credname);
|
|
|
|
|
2018-07-13 17:54:52 +00:00
|
|
|
return 0;
|
2012-03-20 15:40:05 +00:00
|
|
|
}
|