Chris PeBenito 473ea7
#include <unistd.h>
Chris PeBenito 473ea7
#include <fcntl.h>
Chris PeBenito 473ea7
#include <sys/stat.h>
Chris PeBenito 473ea7
#include <string.h>
Chris PeBenito 473ea7
#include "selinux_internal.h"
Chris PeBenito 473ea7
#include <stdio.h>
Chris PeBenito 473ea7
#include <stdlib.h>
Chris PeBenito 473ea7
#include <ctype.h>
Chris PeBenito 473ea7
#include <errno.h>
Chris PeBenito 473ea7
#include <limits.h>
Chris PeBenito 473ea7
#include <regex.h>
Chris PeBenito 473ea7
#include <stdarg.h>
Chris PeBenito 473ea7
Chris PeBenito 473ea7
int matchmediacon(const char *media, 
Chris PeBenito 473ea7
		 security_context_t *con)
Chris PeBenito 473ea7
{
Chris PeBenito 473ea7
	const char *path = selinux_media_context_path();
Chris PeBenito 473ea7
	FILE *infile;
Chris PeBenito 473ea7
	char *ptr, *ptr2=NULL;
Chris PeBenito 473ea7
	int found=-1;
Chris PeBenito 473ea7
	char current_line[PATH_MAX];
Chris PeBenito 473ea7
	if ((infile = fopen(path, "r")) == NULL)
Chris PeBenito 473ea7
		return -1;
Chris PeBenito 473ea7
	while (!feof_unlocked (infile)) {
Chris PeBenito 473ea7
		if (!fgets_unlocked(current_line, sizeof(current_line), infile)) {
Chris PeBenito 473ea7
			return -1;
Chris PeBenito 473ea7
		}
Chris PeBenito 473ea7
		if (current_line[strlen(current_line) - 1])
Chris PeBenito 473ea7
			current_line[strlen(current_line) - 1] = 0;
Chris PeBenito 473ea7
		/* Skip leading whitespace before the partial context. */
Chris PeBenito 473ea7
		ptr = current_line;
Chris PeBenito 473ea7
		while (*ptr && isspace(*ptr))
Chris PeBenito 473ea7
			ptr++;
Chris PeBenito 473ea7
		
Chris PeBenito 473ea7
		if (!(*ptr))
Chris PeBenito 473ea7
			continue;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
Chris PeBenito 473ea7
		/* Find the end of the media context. */
Chris PeBenito 473ea7
		ptr2 = ptr;
Chris PeBenito 473ea7
		while (*ptr2 && !isspace(*ptr2))
Chris PeBenito 473ea7
			ptr2++;
Chris PeBenito 473ea7
		if (!(*ptr2))
Chris PeBenito 473ea7
			continue;
Chris PeBenito 473ea7
		
Chris PeBenito 473ea7
		*ptr2++ = 0;
Chris PeBenito 473ea7
		if (strcmp (media, ptr) == 0) {
Chris PeBenito 473ea7
			found = 1;
Chris PeBenito 473ea7
			break;
Chris PeBenito 473ea7
		}
Chris PeBenito 473ea7
	}
Chris PeBenito 473ea7
	if (!found) 
Chris PeBenito 473ea7
		return -1;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	/* Skip whitespace. */
Chris PeBenito 473ea7
	while (*ptr2 && isspace(*ptr2))
Chris PeBenito 473ea7
		ptr2++;
Chris PeBenito 473ea7
	if (!(*ptr2)) {
Chris PeBenito 473ea7
		return -1;
Chris PeBenito 473ea7
	}
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	if (context_translations) {
Chris PeBenito 473ea7
		if (raw_to_trans_context(ptr2, con)) {
Chris PeBenito 473ea7
			*con = NULL;
Chris PeBenito 473ea7
			return -1;
Chris PeBenito 473ea7
		}
Chris PeBenito 473ea7
	} else
Chris PeBenito 473ea7
		*con = strdup(ptr2);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	return 0;
Chris PeBenito 473ea7
}