Ejemplo n.º 1
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 *groupname = NULL;
    char *seccontext = NULL;
    virIdentityPtr ret = NULL;
#if WITH_SELINUX
    security_context_t con;
#endif

    if (!(username = virGetUserName(getuid())))
        goto cleanup;
    if (!(groupname = virGetGroupName(getgid())))
        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 (username &&
        virIdentitySetAttr(ret,
                           VIR_IDENTITY_ATTR_UNIX_USER_NAME,
                           username) < 0)
        goto error;
    if (groupname &&
        virIdentitySetAttr(ret,
                           VIR_IDENTITY_ATTR_UNIX_GROUP_NAME,
                           groupname) < 0)
        goto error;
    if (seccontext &&
        virIdentitySetAttr(ret,
                           VIR_IDENTITY_ATTR_SELINUX_CONTEXT,
                           seccontext) < 0)
        goto error;

cleanup:
    VIR_FREE(username);
    VIR_FREE(groupname);
    VIR_FREE(seccontext);
    return ret;

error:
    virObjectUnref(ret);
    ret = NULL;
    goto cleanup;
}
Ejemplo n.º 2
0
int virIdentitySetSELinuxContext(virIdentityPtr ident,
                                 const char *context)
{
    return virIdentitySetAttr(ident,
                              VIR_IDENTITY_ATTR_SELINUX_CONTEXT,
                              context);
}
Ejemplo n.º 3
0
int virIdentitySetX509DName(virIdentityPtr ident,
                            const char *dname)
{
    return virIdentitySetAttr(ident,
                              VIR_IDENTITY_ATTR_X509_DISTINGUISHED_NAME,
                              dname);
}
Ejemplo n.º 4
0
int virIdentitySetSASLUserName(virIdentityPtr ident,
                               const char *username)
{
    return virIdentitySetAttr(ident,
                              VIR_IDENTITY_ATTR_SASL_USER_NAME,
                              username);
}
Ejemplo n.º 5
0
int virIdentitySetUNIXGroupName(virIdentityPtr ident,
                                const char *groupname)
{
    return virIdentitySetAttr(ident,
                              VIR_IDENTITY_ATTR_UNIX_GROUP_NAME,
                              groupname);
}
Ejemplo n.º 6
0
int virIdentitySetUNIXProcessTime(virIdentityPtr ident,
                                  unsigned long long timestamp)
{
    VIR_AUTOFREE(char *) val = NULL;

    if (virAsprintf(&val, "%llu", timestamp) < 0)
        return -1;

    return virIdentitySetAttr(ident,
                              VIR_IDENTITY_ATTR_UNIX_PROCESS_TIME,
                              val);
}
Ejemplo n.º 7
0
int virIdentitySetUNIXProcessID(virIdentityPtr ident,
                                pid_t pid)
{
    VIR_AUTOFREE(char *) val = NULL;

    if (virAsprintf(&val, "%lld", (long long) pid) < 0)
        return -1;

    return virIdentitySetAttr(ident,
                              VIR_IDENTITY_ATTR_UNIX_PROCESS_ID,
                              val);
}
Ejemplo n.º 8
0
int virIdentitySetUNIXGroupID(virIdentityPtr ident,
                              gid_t gid)
{
    VIR_AUTOFREE(char *) val = NULL;

    if (virAsprintf(&val, "%d", (int)gid) < 0)
        return -1;

    return virIdentitySetAttr(ident,
                              VIR_IDENTITY_ATTR_UNIX_GROUP_ID,
                              val);
}
Ejemplo n.º 9
0
int virIdentitySetUNIXUserID(virIdentityPtr ident,
                             uid_t uid)
{
    VIR_AUTOFREE(char *) val = NULL;

    if (virAsprintf(&val, "%d", (int)uid) < 0)
        return -1;

    return virIdentitySetAttr(ident,
                              VIR_IDENTITY_ATTR_UNIX_USER_ID,
                              val);
}
Ejemplo n.º 10
0
int virIdentitySetUNIXProcessTime(virIdentityPtr ident,
                                  unsigned long long timestamp)
{
    char *val;
    int ret;
    if (virAsprintf(&val, "%llu", timestamp) < 0)
        return -1;
    ret = virIdentitySetAttr(ident,
                             VIR_IDENTITY_ATTR_UNIX_PROCESS_TIME,
                             val);
    VIR_FREE(val);
    return ret;
}
Ejemplo n.º 11
0
int virIdentitySetUNIXProcessID(virIdentityPtr ident,
                                pid_t pid)
{
    char *val;
    int ret;
    if (virAsprintf(&val, "%lld", (long long) pid) < 0)
        return -1;
    ret = virIdentitySetAttr(ident,
                             VIR_IDENTITY_ATTR_UNIX_PROCESS_ID,
                             val);
    VIR_FREE(val);
    return ret;
}
Ejemplo n.º 12
0
int virIdentitySetUNIXGroupID(virIdentityPtr ident,
                              gid_t gid)
{
    char *val;
    int ret;
    if (virAsprintf(&val, "%d", (int)gid) < 0)
        return -1;
    ret = virIdentitySetAttr(ident,
                             VIR_IDENTITY_ATTR_UNIX_GROUP_ID,
                             val);
    VIR_FREE(val);
    return ret;
}
Ejemplo n.º 13
0
int virIdentitySetUNIXUserID(virIdentityPtr ident,
                             uid_t uid)
{
    char *val;
    int ret;
    if (virAsprintf(&val, "%d", (int)uid) < 0)
        return -1;
    ret = virIdentitySetAttr(ident,
                             VIR_IDENTITY_ATTR_UNIX_USER_ID,
                             val);
    VIR_FREE(val);
    return ret;
}
Ejemplo n.º 14
0
static virIdentityPtr
virNetServerClientCreateIdentity(virNetServerClientPtr client)
{
    char *processid = NULL;
    char *processtime = NULL;
    char *username = NULL;
    char *userid = NULL;
    char *groupname = NULL;
    char *groupid = NULL;
#if WITH_SASL
    char *saslname = NULL;
#endif
#if WITH_GNUTLS
    char *x509dname = NULL;
#endif
    char *seccontext = NULL;
    virIdentityPtr ret = NULL;

    if (client->sock && virNetSocketIsLocal(client->sock)) {
        gid_t gid;
        uid_t uid;
        pid_t pid;
        unsigned long long timestamp;
        if (virNetSocketGetUNIXIdentity(client->sock,
                                        &uid, &gid, &pid,
                                        &timestamp) < 0)
            goto cleanup;

        if (!(username = virGetUserName(uid)))
            goto cleanup;
        if (virAsprintf(&userid, "%d", (int)uid) < 0)
            goto cleanup;
        if (!(groupname = virGetGroupName(gid)))
            goto cleanup;
        if (virAsprintf(&groupid, "%d", (int)gid) < 0)
            goto cleanup;
        if (virAsprintf(&processid, "%llu",
                        (unsigned long long)pid) < 0)
            goto cleanup;
        if (virAsprintf(&processtime, "%llu",
                        timestamp) < 0)
            goto cleanup;
    }

#if WITH_SASL
    if (client->sasl) {
        const char *identity = virNetSASLSessionGetIdentity(client->sasl);
        if (VIR_STRDUP(saslname, identity) < 0)
            goto cleanup;
    }
#endif

#if WITH_GNUTLS
    if (client->tls) {
        const char *identity = virNetTLSSessionGetX509DName(client->tls);
        if (VIR_STRDUP(x509dname, identity) < 0)
            goto cleanup;
    }
#endif

    if (client->sock &&
        virNetSocketGetSELinuxContext(client->sock, &seccontext) < 0)
        goto cleanup;

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

    if (username &&
        virIdentitySetAttr(ret,
                           VIR_IDENTITY_ATTR_UNIX_USER_NAME,
                           username) < 0)
        goto error;
    if (userid &&
        virIdentitySetAttr(ret,
                           VIR_IDENTITY_ATTR_UNIX_USER_ID,
                           userid) < 0)
        goto error;
    if (groupname &&
        virIdentitySetAttr(ret,
                           VIR_IDENTITY_ATTR_UNIX_GROUP_NAME,
                           groupname) < 0)
        goto error;
    if (groupid &&
        virIdentitySetAttr(ret,
                           VIR_IDENTITY_ATTR_UNIX_GROUP_ID,
                           groupid) < 0)
        goto error;
    if (processid &&
        virIdentitySetAttr(ret,
                           VIR_IDENTITY_ATTR_UNIX_PROCESS_ID,
                           processid) < 0)
        goto error;
    if (processtime &&
        virIdentitySetAttr(ret,
                           VIR_IDENTITY_ATTR_UNIX_PROCESS_TIME,
                           processtime) < 0)
        goto error;
#if WITH_SASL
    if (saslname &&
        virIdentitySetAttr(ret,
                           VIR_IDENTITY_ATTR_SASL_USER_NAME,
                           saslname) < 0)
        goto error;
#endif
#if WITH_GNUTLS
    if (x509dname &&
        virIdentitySetAttr(ret,
                           VIR_IDENTITY_ATTR_X509_DISTINGUISHED_NAME,
                           x509dname) < 0)
        goto error;
#endif
    if (seccontext &&
        virIdentitySetAttr(ret,
                           VIR_IDENTITY_ATTR_SELINUX_CONTEXT,
                           seccontext) < 0)
        goto error;

cleanup:
    VIR_FREE(username);
    VIR_FREE(userid);
    VIR_FREE(groupname);
    VIR_FREE(groupid);
    VIR_FREE(processid);
    VIR_FREE(processtime);
    VIR_FREE(seccontext);
#if WITH_SASL
    VIR_FREE(saslname);
#endif
#if WITH_GNUTLS
    VIR_FREE(x509dname);
#endif
    return ret;

error:
    virObjectUnref(ret);
    ret = NULL;
    goto cleanup;
}