Chris PeBenito 473ea7
#include <unistd.h>
Chris PeBenito 473ea7
#include <fcntl.h>
Chris PeBenito 473ea7
#include <string.h>
Chris PeBenito 473ea7
#include <stdlib.h>
Chris PeBenito 473ea7
#include <errno.h>
Chris PeBenito 473ea7
#include <sys/socket.h>
Chris PeBenito 473ea7
#include "selinux_internal.h"
Chris PeBenito 473ea7
#include "policy.h"
Chris PeBenito 473ea7
Chris PeBenito 473ea7
#ifndef SO_PEERSEC
Chris PeBenito 473ea7
#define SO_PEERSEC 31
Chris PeBenito 473ea7
#endif
Chris PeBenito 473ea7
Chris PeBenito 473ea7
int getpeercon_raw(int fd, security_context_t *context)
Chris PeBenito 473ea7
{
Chris PeBenito 473ea7
	char *buf;
Chris PeBenito 473ea7
	socklen_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 = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, buf, &size);
Chris PeBenito 473ea7
	if (ret < 0 && errno == ERANGE) {
Chris PeBenito 473ea7
		char *newbuf;
Chris PeBenito 473ea7
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 = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, buf, &size);
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(getpeercon_raw)
Chris PeBenito 473ea7
Chris PeBenito 473ea7
int getpeercon(int fd, 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 = getpeercon_raw(fd, &rcontext);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	if (context_translations && !ret) {
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)
Chris PeBenito 473ea7
		*context = rcontext;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	return ret;
Chris PeBenito 473ea7
}