virauth.c 6.1 KB
Newer Older
1
/*
2
 * virauth.c: authentication related utility functions
3
 *
M
Martin Kletzander 已提交
4
 * Copyright (C) 2012 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17
 * 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
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24
 *
 */

#include <config.h>

25 26
#include <stdlib.h>

27
#include "virauth.h"
28 29
#include "util.h"
#include "memory.h"
30 31 32 33
#include "logging.h"
#include "datatypes.h"
#include "virterror_internal.h"
#include "configmake.h"
34
#include "virauthconfig.h"
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

#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;
    }

M
Martin Kletzander 已提交
58 59 60 61 62 63 64 65 66 67
    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;
            }
68 69 70
        }
    }

71
    if (!(userdir = virGetUserConfigDirectory()))
72 73
        goto cleanup;

74
    if (virAsprintf(path, "%s/auth.conf", userdir) < 0)
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
        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;
}
105 106


107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
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,
133
                            VIR_URI_SERVER(conn->uri),
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
                            credname,
                            &tmp) < 0)
        goto cleanup;

    if (tmp &&
        !(*value = strdup(tmp))) {
        virReportOOMError();
        goto cleanup;
    }

    ret = 0;

cleanup:
    virAuthConfigFree(config);
    VIR_FREE(path);
    return ret;
}


153
char *
154 155 156 157
virAuthGetUsername(virConnectPtr conn,
                   virConnectAuthPtr auth,
                   const char *servicename,
                   const char *defaultUsername,
158 159 160 161 162
                   const char *hostname)
{
    unsigned int ncred;
    virConnectCredential cred;
    char *prompt;
163 164 165 166 167 168
    char *ret = NULL;

    if (virAuthGetCredential(conn, servicename, "username", &ret) < 0)
        return NULL;
    if (ret != NULL)
        return ret;
169

170
    memset(&cred, 0, sizeof(virConnectCredential));
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

    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 *
210 211 212 213
virAuthGetPassword(virConnectPtr conn,
                   virConnectAuthPtr auth,
                   const char *servicename,
                   const char *username,
214 215 216 217 218
                   const char *hostname)
{
    unsigned int ncred;
    virConnectCredential cred;
    char *prompt;
219 220 221 222 223 224
    char *ret = NULL;

    if (virAuthGetCredential(conn, servicename, "password", &ret) < 0)
        return NULL;
    if (ret != NULL)
        return ret;
225

226
    memset(&cred, 0, sizeof(virConnectCredential));
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

    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;
}