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"
|
|
|
|
|
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 {
|
2020-03-23 13:19:12 +00:00
|
|
|
GKeyFile *keyfile;
|
2012-03-20 15:40:05 +00:00
|
|
|
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;
|
|
|
|
|
2020-10-05 17:12:37 +00:00
|
|
|
auth = g_new0(virAuthConfig, 1);
|
2012-03-20 15:40:05 +00:00
|
|
|
|
2019-10-20 11:49:46 +00:00
|
|
|
auth->path = g_strdup(path);
|
2012-03-20 15:40:05 +00:00
|
|
|
|
2020-03-23 13:19:12 +00:00
|
|
|
if (!(auth->keyfile = g_key_file_new()))
|
2012-03-20 15:40:05 +00:00
|
|
|
goto error;
|
|
|
|
|
2020-03-23 13:19:12 +00:00
|
|
|
if (!g_key_file_load_from_file(auth->keyfile, path, 0, NULL))
|
2012-03-20 15:40:05 +00:00
|
|
|
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;
|
|
|
|
|
2020-10-05 17:12:37 +00:00
|
|
|
auth = g_new0(virAuthConfig, 1);
|
2012-03-20 15:40:05 +00:00
|
|
|
|
2019-10-20 11:49:46 +00:00
|
|
|
auth->path = g_strdup(path);
|
2012-03-20 15:40:05 +00:00
|
|
|
|
2020-03-23 13:19:12 +00:00
|
|
|
if (!(auth->keyfile = g_key_file_new()))
|
2012-03-20 15:40:05 +00:00
|
|
|
goto error;
|
|
|
|
|
2020-03-23 13:19:12 +00:00
|
|
|
if (!g_key_file_load_from_data(auth->keyfile, data, len, 0, NULL))
|
2012-03-20 15:40:05 +00:00
|
|
|
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;
|
|
|
|
|
2020-03-23 13:19:12 +00:00
|
|
|
g_key_file_free(auth->keyfile);
|
2012-03-20 15:40:05 +00:00
|
|
|
VIR_FREE(auth->path);
|
|
|
|
VIR_FREE(auth);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int virAuthConfigLookup(virAuthConfigPtr auth,
|
|
|
|
const char *service,
|
|
|
|
const char *hostname,
|
|
|
|
const char *credname,
|
2020-06-16 12:07:02 +00:00
|
|
|
char **value)
|
2012-03-20 15:40:05 +00:00
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *authgroup = NULL;
|
|
|
|
g_autofree char *credgroup = NULL;
|
2020-06-16 12:07:03 +00:00
|
|
|
g_autofree char *authcred = NULL;
|
2012-03-20 15:40:05 +00:00
|
|
|
|
|
|
|
*value = NULL;
|
|
|
|
|
|
|
|
VIR_DEBUG("Lookup '%s' '%s' '%s'", service, NULLSTR(hostname), credname);
|
|
|
|
|
|
|
|
if (!hostname)
|
|
|
|
hostname = "localhost";
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
authgroup = g_strdup_printf("auth-%s-%s", service, hostname);
|
2012-03-20 15:40:05 +00:00
|
|
|
|
2020-03-23 13:19:12 +00:00
|
|
|
if (!g_key_file_has_group(auth->keyfile, authgroup)) {
|
2016-06-13 09:29:57 +00:00
|
|
|
VIR_FREE(authgroup);
|
2019-10-22 13:26:14 +00:00
|
|
|
authgroup = g_strdup_printf("auth-%s-%s", service, "default");
|
2016-06-13 09:29:57 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 13:19:12 +00:00
|
|
|
if (!g_key_file_has_group(auth->keyfile, authgroup))
|
2018-07-13 17:54:52 +00:00
|
|
|
return 0;
|
2012-03-20 15:40:05 +00:00
|
|
|
|
2020-03-23 13:19:12 +00:00
|
|
|
if (!(authcred = g_key_file_get_string(auth->keyfile, authgroup, "credentials", NULL))) {
|
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
|
|
|
}
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
credgroup = g_strdup_printf("credentials-%s", authcred);
|
2012-03-20 15:40:05 +00:00
|
|
|
|
2020-03-23 13:19:12 +00:00
|
|
|
if (!g_key_file_has_group(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
|
|
|
}
|
|
|
|
|
2020-03-23 13:19:12 +00:00
|
|
|
*value = g_key_file_get_string(auth->keyfile, credgroup, credname, NULL);
|
2012-03-20 15:40:05 +00:00
|
|
|
|
2018-07-13 17:54:52 +00:00
|
|
|
return 0;
|
2012-03-20 15:40:05 +00:00
|
|
|
}
|