<commit_before>/*
 * 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>

#include <unistd.h>
#if WITH_SELINUX
# include <selinux/selinux.h>
#endif

#include "internal.h"
#include "viralloc.h"
#include "virerror.h"
#include "viridentity.h"
#include "virlog.h"
#include "virobject.h"
#include "virthread.h"
#include "virutil.h"
#include "virstring.h"
#include "virprocess.h"

#define VIR_FROM_THIS VIR_FROM_IDENTITY


struct _virIdentity {
    virObject parent;

    char *attrs[VIR_IDENTITY_ATTR_LAST];
};

static virClassPtr virIdentityClass;
static virThreadLocal virIdentityCurrent;

static void virIdentityDispose(void *obj);

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

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

    return 0;
}

VIR_ONCE_GLOBAL_INIT(virIdentity)

/**
 * 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;

    if (virIdentityInitialize() < 0)
        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;

    if (virIdentityInitialize() < 0)
        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;
}


/**
 * 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;
    char *userid = NULL;
    char *groupname = NULL;
    char *groupid = NULL;
    char *seccontext = NULL;
    virIdentityPtr ret = NULL;
#if WITH_SELINUX
    security_context_t con;
#endif
    char *processid = NULL;
    unsigned long long timestamp;
    char *processtime = NULL;

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

    if (virProcessGetStartTime(getpid(), &timestamp) < 0)
        goto cleanup;

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

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

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

#if WITH_SELINUX
    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;
    }
    freecon(con);
#endif

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

    if (virIdentitySetAttr(ret,
                           VIR_IDENTITY_ATTR_UNIX_USER_NAME,
                           username) < 0)
        goto error;
    if (virIdentitySetAttr(ret,<commit_msg>virIdentityGetSystem: don't fail if SELinux is disabled<commit_after>/*
 * 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>

#include <unistd.h>
#if WITH_SELINUX
# include <selinux/selinux.h>
#endif

#include "internal.h"
#include "viralloc.h"
#include "virerror.h"
#include "viridentity.h"
#include "virlog.h"
#include "virobject.h"
#include "virthread.h"
#include "virutil.h"
#include "virstring.h"
#include "virprocess.h"

#define VIR_FROM_THIS VIR_FROM_IDENTITY


struct _virIdentity {
    virObject parent;

    char *attrs[VIR_IDENTITY_ATTR_LAST];
};

static virClassPtr virIdentityClass;
static virThreadLocal virIdentityCurrent;

static void virIdentityDispose(void *obj);

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

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

    return 0;
}

VIR_ONCE_GLOBAL_INIT(virIdentity)

/**
 * 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;

    if (virIdentityInitialize() < 0)
        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;

    if (virIdentityInitialize() < 0)
        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;
}


/**
 * 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;
    char *userid = NULL;
    char *groupname = NULL;
    char *groupid = NULL;
    char *seccontext = NULL;
    virIdentityPtr ret = NULL;
#if WITH_SELINUX
    security_context_t con;
#endif
    char *processid = NULL;
    unsigned long long timestamp;
    char *processtime = NULL;

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

    if (virProcessGetStartTime(getpid(), &timestamp) < 0)
        goto cleanup;

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

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

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

#if WITH_SELINUX
    if (is_selinux_enabled()) {
        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;
        }
        freecon(con);
    }
#endif

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

    if (virIdentitySetAttr(ret,
                           VIR_IDENTITY_ATTR_UNIX_USER_NAME,
                           username) < 0)
        goto error;
    if (virIdentitySetAttr(ret,
{'fraction_non_alphanumeric': 0.0944062806673209, 'fraction_numerical': 0.006084396467124632, 'mean_word_length': 2.7071662422699165, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5966501', 'n_tokens_mistral': 2990, 'n_tokens_neox': 2902, 'n_words': 949}