Chris PeBenito 473ea7
#include <unistd.h>
Chris PeBenito 473ea7
#include <sys/types.h>
Chris PeBenito 473ea7
#include <fcntl.h>
Chris PeBenito 473ea7
#include <stdlib.h>
Chris PeBenito 473ea7
#include <stdio.h>
Chris PeBenito 473ea7
#include <errno.h>
Chris PeBenito 473ea7
#include <string.h>
Chris PeBenito 473ea7
#include <asm/page.h>
Chris PeBenito 473ea7
#include "selinux_internal.h"
Chris PeBenito 473ea7
#include "policy.h"
Chris PeBenito 473ea7
#include <limits.h>
Chris PeBenito 473ea7
Chris PeBenito 473ea7
int security_compute_user_raw(security_context_t scon,
Chris PeBenito 473ea7
                              const char *user,
Chris PeBenito 473ea7
                              security_context_t **con)
Chris PeBenito 473ea7
{
Chris PeBenito 473ea7
	char path[PATH_MAX];
Chris PeBenito 473ea7
	char **ary;
Chris PeBenito 473ea7
	char *buf, *ptr;
Chris PeBenito 473ea7
	size_t size;
Chris PeBenito 473ea7
	int fd, ret;
Chris PeBenito 473ea7
	unsigned int i, nel;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	snprintf(path, sizeof path, "%s/user", selinux_mnt);
Chris PeBenito 473ea7
	fd = open(path, O_RDWR);
Chris PeBenito 473ea7
	if (fd < 0)
Chris PeBenito 473ea7
		return -1;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	size = PAGE_SIZE;
Chris PeBenito 473ea7
	buf = malloc(size);
Chris PeBenito 473ea7
	if (!buf) {
Chris PeBenito 473ea7
		ret = -1;
Chris PeBenito 473ea7
		goto out;
Chris PeBenito 473ea7
	}
Chris PeBenito 473ea7
	snprintf(buf, size, "%s %s", scon, user);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	ret = write(fd, buf, strlen(buf));
Chris PeBenito 473ea7
	if (ret < 0) 
Chris PeBenito 473ea7
		goto out2;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	memset(buf, 0, size);
Chris PeBenito 473ea7
	ret = read(fd, buf, size-1);
Chris PeBenito 473ea7
	if (ret < 0)
Chris PeBenito 473ea7
		goto out2;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	if (sscanf(buf, "%u", &nel) != 1) {
Chris PeBenito 473ea7
		ret = -1;
Chris PeBenito 473ea7
		goto out2;
Chris PeBenito 473ea7
	}
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	ary = malloc((nel+1)*sizeof(char*));
Chris PeBenito 473ea7
	if (!ary) {
Chris PeBenito 473ea7
		ret = -1;
Chris PeBenito 473ea7
		goto out2;
Chris PeBenito 473ea7
	}
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	ptr = buf + strlen(buf) + 1;
Chris PeBenito 473ea7
	for (i = 0; i < nel; i++) {
Chris PeBenito 473ea7
		ary[i] = strdup(ptr);
Chris PeBenito 473ea7
		if (!ary[i]) {
Chris PeBenito 473ea7
			freeconary(ary);
Chris PeBenito 473ea7
			ret = -1;
Chris PeBenito 473ea7
			goto out2;
Chris PeBenito 473ea7
		}
Chris PeBenito 473ea7
		ptr += strlen(ptr) + 1;
Chris PeBenito 473ea7
	}
Chris PeBenito 473ea7
	ary[nel] = NULL;
Chris PeBenito 473ea7
	*con = ary;
Chris PeBenito 473ea7
	ret = 0;
Chris PeBenito 473ea7
out2:
Chris PeBenito 473ea7
	free(buf);
Chris PeBenito 473ea7
out:
Chris PeBenito 473ea7
	close(fd);
Chris PeBenito 473ea7
	return ret;
Chris PeBenito 473ea7
}
Chris PeBenito 473ea7
hidden_def(security_compute_user_raw)
Chris PeBenito 473ea7
Chris PeBenito 473ea7
int security_compute_user(security_context_t scon,
Chris PeBenito 473ea7
                          const char *user,
Chris PeBenito 473ea7
                          security_context_t **con)
Chris PeBenito 473ea7
{
Chris PeBenito 473ea7
	int ret;
Chris PeBenito 473ea7
	security_context_t rscon = scon;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	if (context_translations && trans_to_raw_context(scon, &rscon))
Chris PeBenito 473ea7
		return -1;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
 	ret = security_compute_user_raw(rscon, user, con);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	if (context_translations) {
Chris PeBenito 473ea7
		freecon(rscon);
Chris PeBenito 473ea7
		if (!ret) {
Chris PeBenito 473ea7
			security_context_t *ptr, tmpcon;
Chris PeBenito 473ea7
			for (ptr = *con; *ptr; ptr++) {
Chris PeBenito 473ea7
				if (raw_to_trans_context(*ptr, &tmpcon)) {
Chris PeBenito 473ea7
					freeconary(*con);
Chris PeBenito 473ea7
					*con = NULL;
Chris PeBenito 473ea7
					return -1;
Chris PeBenito 473ea7
				}
Chris PeBenito 473ea7
				freecon(*ptr);
Chris PeBenito 473ea7
				*ptr = tmpcon;
Chris PeBenito 473ea7
			}
Chris PeBenito 473ea7
		}
Chris PeBenito 473ea7
	}
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	return ret;
Chris PeBenito 473ea7
}
Chris PeBenito 473ea7
hidden_def(security_compute_user)