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 <ctype.h>
Chris PeBenito 473ea7
#include <asm/page.h>
Chris PeBenito 473ea7
#include <stdio.h>
Chris PeBenito 473ea7
#include <dlfcn.h>
Chris PeBenito 473ea7
Chris PeBenito 473ea7
#include "dso.h"
Chris PeBenito 473ea7
#include "policy.h"
Chris PeBenito 473ea7
#include "selinux_internal.h"
Chris PeBenito 473ea7
Chris PeBenito 473ea7
char *selinux_mnt = NULL;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
static void init_selinuxmnt(void)
Chris PeBenito 473ea7
{
Chris PeBenito 473ea7
	char *buf, *bufp, *p;
Chris PeBenito 473ea7
	size_t size;
Chris PeBenito 473ea7
	FILE *fp;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	if (selinux_mnt)
Chris PeBenito 473ea7
		return;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	fp = fopen("/proc/mounts", "r");
Chris PeBenito 473ea7
	if (!fp)
Chris PeBenito 473ea7
		return;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	size = PAGE_SIZE;
Chris PeBenito 473ea7
	buf = malloc(size);
Chris PeBenito 473ea7
	if (!buf)
Chris PeBenito 473ea7
		goto out;
Chris PeBenito 473ea7
		
Chris PeBenito 473ea7
	memset(buf, 0, size);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	while(( bufp = fgets_unlocked(buf, size, fp)))
Chris PeBenito 473ea7
	{
Chris PeBenito 473ea7
		char *tmp;
Chris PeBenito 473ea7
		p = strchr(buf, ' ');
Chris PeBenito 473ea7
		if (!p)
Chris PeBenito 473ea7
			goto out2;
Chris PeBenito 473ea7
		p++;
Chris PeBenito 473ea7
		tmp = strchr(p, ' ');
Chris PeBenito 473ea7
		if (!tmp)
Chris PeBenito 473ea7
			goto out2;
Chris PeBenito 473ea7
		if(!strncmp(tmp + 1, "selinuxfs ", 10)) {
Chris PeBenito 473ea7
			*tmp = '\0';
Chris PeBenito 473ea7
			break;
Chris PeBenito 473ea7
		}
Chris PeBenito 473ea7
	}
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	if (!bufp)
Chris PeBenito 473ea7
		goto out2;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	selinux_mnt = strdup(p);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
out2:
Chris PeBenito 473ea7
	free(buf);
Chris PeBenito 473ea7
out:
Chris PeBenito 473ea7
	fclose(fp);
Chris PeBenito 473ea7
	return;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
}
Chris PeBenito 473ea7
Chris PeBenito 473ea7
static void fini_selinuxmnt(void)
Chris PeBenito 473ea7
{
Chris PeBenito 473ea7
	free(selinux_mnt);
Chris PeBenito 473ea7
	selinux_mnt = NULL;
Chris PeBenito 473ea7
}
Chris PeBenito 473ea7
Chris PeBenito 473ea7
void set_selinuxmnt(char *mnt)
Chris PeBenito 473ea7
{
Chris PeBenito 473ea7
	selinux_mnt = strdup(mnt);
Chris PeBenito 473ea7
}
Chris PeBenito 473ea7
hidden_def(set_selinuxmnt)
Chris PeBenito 473ea7
 
Chris PeBenito 473ea7
int context_translations hidden;
Chris PeBenito 473ea7
void *translation_lib_handle hidden;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
/* from libsetrans.c */
Chris PeBenito 473ea7
extern int hidden (*lib_trans_to_raw_context)(char *trans, char **rawp);
Chris PeBenito 473ea7
extern int hidden (*lib_raw_to_trans_context)(char *raw, char **transp);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
Chris PeBenito 473ea7
static void init_translations(void)
Chris PeBenito 473ea7
{
Chris PeBenito 473ea7
#ifdef SHARED
Chris PeBenito 473ea7
	int (*lib_trans_init)(void) = NULL;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	translation_lib_handle = dlopen("libsetrans.so.0", RTLD_NOW);
Chris PeBenito 473ea7
	if (!translation_lib_handle)
Chris PeBenito 473ea7
		return;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	dlerror();
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	lib_trans_init = dlsym(translation_lib_handle,
Chris PeBenito 473ea7
	                       "init_context_translations");
Chris PeBenito 473ea7
	if (dlerror() || lib_trans_init())
Chris PeBenito 473ea7
		return;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	lib_raw_to_trans_context = dlsym(translation_lib_handle,
Chris PeBenito 473ea7
	                                 "translate_context");
Chris PeBenito 473ea7
	if (dlerror())
Chris PeBenito 473ea7
		return;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	lib_trans_to_raw_context = dlsym(translation_lib_handle,
Chris PeBenito 473ea7
	                                 "untranslate_context");
Chris PeBenito 473ea7
	if (dlerror())
Chris PeBenito 473ea7
		return;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	context_translations = 1;
Chris PeBenito 473ea7
#endif
Chris PeBenito 473ea7
}
Chris PeBenito 473ea7
Chris PeBenito 473ea7
static void fini_translations(void)
Chris PeBenito 473ea7
{
Chris PeBenito 473ea7
#ifdef SHARED
Chris PeBenito 473ea7
	context_translations = 0;
Chris PeBenito 473ea7
	if (translation_lib_handle) {
Chris PeBenito 473ea7
		dlclose(translation_lib_handle);
Chris PeBenito 473ea7
		translation_lib_handle = NULL;
Chris PeBenito 473ea7
	}
Chris PeBenito 473ea7
#endif
Chris PeBenito 473ea7
}
Chris PeBenito 473ea7
Chris PeBenito 473ea7
static void init_lib(void) __attribute__ ((constructor));
Chris PeBenito 473ea7
static void init_lib(void)
Chris PeBenito 473ea7
{
Chris PeBenito 473ea7
	init_selinuxmnt();
Chris PeBenito 473ea7
	init_translations();
Chris PeBenito 473ea7
}
Chris PeBenito 473ea7
Chris PeBenito 473ea7
static void fini_lib(void) __attribute__ ((destructor));
Chris PeBenito 473ea7
static void fini_lib(void)
Chris PeBenito 473ea7
{
Chris PeBenito 473ea7
	fini_translations();
Chris PeBenito 473ea7
	fini_selinuxmnt();
Chris PeBenito 473ea7
}