viridentity.c 8.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * viridentity.c: helper APIs for managing user identities
 *
 * Copyright (C) 2012-2013 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/>.
 *
 */

#include <config.h>

24
#include <unistd.h>
25
#if WITH_SELINUX
26 27 28
# include <selinux/selinux.h>
#endif

29 30 31 32 33 34 35
#include "internal.h"
#include "viralloc.h"
#include "virerror.h"
#include "viridentity.h"
#include "virlog.h"
#include "virobject.h"
#include "virthread.h"
36
#include "virutil.h"
37
#include "virstring.h"
38
#include "virprocess.h"
39 40 41

#define VIR_FROM_THIS VIR_FROM_IDENTITY

42
VIR_LOG_INIT("util.identity");
43 44 45 46 47 48 49 50

struct _virIdentity {
    virObject parent;

    char *attrs[VIR_IDENTITY_ATTR_LAST];
};

static virClassPtr virIdentityClass;
51
static virThreadLocal virIdentityCurrent;
52 53 54 55 56 57 58 59 60 61 62

static void virIdentityDispose(void *obj);

static int virIdentityOnceInit(void)
{
    if (!(virIdentityClass = virClassNew(virClassForObject(),
                                         "virIdentity",
                                         sizeof(virIdentity),
                                         virIdentityDispose)))
        return -1;

63 64 65 66 67 68 69
    if (virThreadLocalInit(&virIdentityCurrent,
                           (virThreadLocalCleanup)virObjectUnref) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Cannot initialize thread local for current identity"));
        return -1;
    }

70 71 72 73 74
    return 0;
}

VIR_ONCE_GLOBAL_INIT(virIdentity)

75 76 77 78 79 80 81 82 83 84 85 86 87 88
/**
 * virIdentityGetCurrent:
 *
 * Get the current identity associated with this thread. The
 * caller will own a reference to the returned identity, but
 * must not modify the object in any way, other than to
 * release the reference when done with virObjectUnref
 *
 * Returns: a reference to the current identity, or NULL
 */
virIdentityPtr virIdentityGetCurrent(void)
{
    virIdentityPtr ident;

89
    if (virIdentityInitialize() < 0)
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
        return NULL;

    ident = virThreadLocalGet(&virIdentityCurrent);
    return virObjectRef(ident);
}


/**
 * virIdentitySetCurrent:
 *
 * Set the new identity to be associated with this thread.
 * The caller should not modify the passed identity after
 * it has been set, other than to release its own reference.
 *
 * Returns 0 on success, or -1 on error
 */
int virIdentitySetCurrent(virIdentityPtr ident)
{
    virIdentityPtr old;

110
    if (virIdentityInitialize() < 0)
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
        return -1;

    old = virThreadLocalGet(&virIdentityCurrent);
    virObjectUnref(old);

    if (virThreadLocalSet(&virIdentityCurrent,
                          virObjectRef(ident)) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Unable to set thread local identity"));
        return -1;
    }

    return 0;
}

126

127 128 129 130 131 132 133 134 135 136 137
/**
 * virIdentityGetSystem:
 *
 * Returns an identity that represents the system itself.
 * This is the identity that the process is running as
 *
 * Returns a reference to the system identity, or NULL
 */
virIdentityPtr virIdentityGetSystem(void)
{
    char *username = NULL;
138
    char *userid = NULL;
139
    char *groupname = NULL;
140
    char *groupid = NULL;
141 142
    char *seccontext = NULL;
    virIdentityPtr ret = NULL;
143
#if WITH_SELINUX
144 145
    security_context_t con;
#endif
146
    char *processid = NULL;
147 148
    unsigned long long timestamp;
    char *processtime = NULL;
149 150

    if (virAsprintf(&processid, "%llu",
151
                    (unsigned long long)getpid()) < 0)
152
        goto cleanup;
153

154 155 156 157 158 159 160
    if (virProcessGetStartTime(getpid(), &timestamp) < 0)
        goto cleanup;

    if (timestamp != 0 &&
        virAsprintf(&processtime, "%llu", timestamp) < 0)
        goto cleanup;

161
    if (!(username = virGetUserName(geteuid())))
162
        goto cleanup;
163
    if (virAsprintf(&userid, "%d", (int)geteuid()) < 0)
164 165
        goto cleanup;

166
    if (!(groupname = virGetGroupName(getegid())))
167
        goto cleanup;
168
    if (virAsprintf(&groupid, "%d", (int)getegid()) < 0)
169
        goto cleanup;
170

171
#if WITH_SELINUX
172
    if (is_selinux_enabled() > 0) {
173 174 175 176 177 178 179 180 181
        if (getcon(&con) < 0) {
            virReportSystemError(errno, "%s",
                                 _("Unable to lookup SELinux process context"));
            goto cleanup;
        }
        if (VIR_STRDUP(seccontext, con) < 0) {
            freecon(con);
            goto cleanup;
        }
182
        freecon(con);
183 184 185 186 187 188
    }
#endif

    if (!(ret = virIdentityNew()))
        goto cleanup;

189
    if (virIdentitySetAttr(ret,
190 191 192
                           VIR_IDENTITY_ATTR_UNIX_USER_NAME,
                           username) < 0)
        goto error;
193 194 195 196 197
    if (virIdentitySetAttr(ret,
                           VIR_IDENTITY_ATTR_UNIX_USER_ID,
                           userid) < 0)
        goto error;
    if (virIdentitySetAttr(ret,
198 199 200
                           VIR_IDENTITY_ATTR_UNIX_GROUP_NAME,
                           groupname) < 0)
        goto error;
201 202 203 204
    if (virIdentitySetAttr(ret,
                           VIR_IDENTITY_ATTR_UNIX_GROUP_ID,
                           groupid) < 0)
        goto error;
205 206
    if (seccontext &&
        virIdentitySetAttr(ret,
207
                           VIR_IDENTITY_ATTR_SELINUX_CONTEXT,
208 209
                           seccontext) < 0)
        goto error;
210 211 212 213
    if (virIdentitySetAttr(ret,
                           VIR_IDENTITY_ATTR_UNIX_PROCESS_ID,
                           processid) < 0)
        goto error;
214 215 216 217 218
    if (processtime &&
        virIdentitySetAttr(ret,
                           VIR_IDENTITY_ATTR_UNIX_PROCESS_TIME,
                           processtime) < 0)
        goto error;
219

220
 cleanup:
221
    VIR_FREE(username);
222
    VIR_FREE(userid);
223
    VIR_FREE(groupname);
224
    VIR_FREE(groupid);
225
    VIR_FREE(seccontext);
226
    VIR_FREE(processid);
227
    VIR_FREE(processtime);
228 229
    return ret;

230
 error:
231 232 233 234 235 236
    virObjectUnref(ret);
    ret = NULL;
    goto cleanup;
}


237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
/**
 * virIdentityNew:
 *
 * Creates a new empty identity object. After creating, one or
 * more identifying attributes should be set on the identity.
 *
 * Returns: a new empty identity
 */
virIdentityPtr virIdentityNew(void)
{
    virIdentityPtr ident;

    if (virIdentityInitialize() < 0)
        return NULL;

    if (!(ident = virObjectNew(virIdentityClass)))
        return NULL;

    return ident;
}


static void virIdentityDispose(void *object)
{
    virIdentityPtr ident = object;
    size_t i;

264
    for (i = 0; i < VIR_IDENTITY_ATTR_LAST; i++)
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
        VIR_FREE(ident->attrs[i]);
}


/**
 * virIdentitySetAttr:
 * @ident: the identity to modify
 * @attr: the attribute type to set
 * @value: the identifying value to associate with @attr
 *
 * Sets an identifying attribute @attr on @ident. Each
 * @attr type can only be set once.
 *
 * Returns: 0 on success, or -1 on error
 */
int virIdentitySetAttr(virIdentityPtr ident,
                       unsigned int attr,
                       const char *value)
{
    int ret = -1;
    VIR_DEBUG("ident=%p attribute=%u value=%s", ident, attr, value);

    if (ident->attrs[attr]) {
        virReportError(VIR_ERR_OPERATION_DENIED, "%s",
                        _("Identity attribute is already set"));
        goto cleanup;
    }

293
    if (VIR_STRDUP(ident->attrs[attr], value) < 0)
294 295 296 297
        goto cleanup;

    ret = 0;

298
 cleanup:
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
    return ret;
}


/**
 * virIdentityGetAttr:
 * @ident: the identity to query
 * @attr: the attribute to read
 * @value: filled with the attribute value
 *
 * Fills @value with a pointer to the value associated
 * with the identifying attribute @attr in @ident. If
 * @attr is not set, then it will simply be initialized
 * to NULL and considered as a successful read
 *
 * Returns 0 on success, -1 on error
 */
int virIdentityGetAttr(virIdentityPtr ident,
                       unsigned int attr,
                       const char **value)
{
    VIR_DEBUG("ident=%p attribute=%d value=%p", ident, attr, value);

    *value = ident->attrs[attr];

    return 0;
}


/**
 * virIdentityIsEqual:
 * @identA: the first identity
 * @identB: the second identity
 *
 * Compares every attribute in @identA and @identB
 * to determine if they refer to the same identity
 *
 * Returns true if they are equal, false if not equal
 */
bool virIdentityIsEqual(virIdentityPtr identA,
                        virIdentityPtr identB)
{
    bool ret = false;
    size_t i;
    VIR_DEBUG("identA=%p identB=%p", identA, identB);

345
    for (i = 0; i < VIR_IDENTITY_ATTR_LAST; i++) {
346 347 348 349 350 351
        if (STRNEQ_NULLABLE(identA->attrs[i],
                            identB->attrs[i]))
            goto cleanup;
    }

    ret = true;
352
 cleanup:
353 354
    return ret;
}