Chris PeBenito 473ea7
#include <unistd.h>
Chris PeBenito 473ea7
#include <fcntl.h>
Chris PeBenito 473ea7
#include <string.h>
Chris PeBenito 473ea7
#include "selinux_internal.h"
Chris PeBenito 473ea7
#include <stdlib.h>
Chris PeBenito 473ea7
#include <errno.h>
Chris PeBenito 473ea7
#include <sys/xattr.h>
Chris PeBenito 473ea7
#include "policy.h"
Chris PeBenito 473ea7
Chris PeBenito 473ea7
int getfilecon_raw(const char *path, security_context_t *context)
Chris PeBenito 473ea7
{
Chris PeBenito 473ea7
	char *buf;
Chris PeBenito 473ea7
	ssize_t size;
Chris PeBenito 473ea7
	ssize_t ret;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	size = INITCONTEXTLEN+1;
Chris PeBenito 473ea7
	buf = malloc(size);
Chris PeBenito 473ea7
	if (!buf) 
Chris PeBenito 473ea7
		return -1;
Chris PeBenito 473ea7
	memset(buf, 0, size);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	ret = getxattr(path, XATTR_NAME_SELINUX, buf, size-1);
Chris PeBenito 473ea7
	if (ret < 0 && errno == ERANGE) {
Chris PeBenito 473ea7
		char *newbuf;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
		size = getxattr(path, XATTR_NAME_SELINUX, NULL, 0);
Chris PeBenito 473ea7
		if (size < 0)
Chris PeBenito 473ea7
			goto out;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
		size++;
Chris PeBenito 473ea7
		newbuf = realloc(buf, size);
Chris PeBenito 473ea7
		if (!newbuf)
Chris PeBenito 473ea7
			goto out;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
		buf = newbuf;
Chris PeBenito 473ea7
		memset(buf, 0, size);
Chris PeBenito 473ea7
		ret = getxattr(path, XATTR_NAME_SELINUX, buf, size-1); 
Chris PeBenito 473ea7
	}
Chris PeBenito 473ea7
out:			
Chris PeBenito 473ea7
	if (ret < 0)
Chris PeBenito 473ea7
		free(buf);
Chris PeBenito 473ea7
	else
Chris PeBenito 473ea7
		*context = buf;
Chris PeBenito 473ea7
	return ret;
Chris PeBenito 473ea7
}
Chris PeBenito 473ea7
hidden_def(getfilecon_raw)
Chris PeBenito 473ea7
Chris PeBenito 473ea7
int getfilecon(const char *path, security_context_t *context)
Chris PeBenito 473ea7
{
Chris PeBenito 473ea7
	int ret;
Chris PeBenito 473ea7
	security_context_t rcontext;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
 	ret = getfilecon_raw(path, &rcontext);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	if (context_translations && ret > 0) {
Chris PeBenito 473ea7
		if (raw_to_trans_context(rcontext, context)) {
Chris PeBenito 473ea7
			*context = NULL;
Chris PeBenito 473ea7
			ret = -1;
Chris PeBenito 473ea7
		}
Chris PeBenito 473ea7
		freecon(rcontext);
Chris PeBenito 473ea7
	} else if (ret > 0)
Chris PeBenito 473ea7
		*context = rcontext;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	return ret;
Chris PeBenito 473ea7
}
Chris PeBenito 473ea7
hidden_def(getfilecon)