2008-08-19 19:30:36 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "selinux_internal.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <regex.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2014-02-19 14:16:17 +00:00
|
|
|
int matchmediacon(const char *media, char ** con)
|
2008-08-19 19:30:36 +00:00
|
|
|
{
|
|
|
|
const char *path = selinux_media_context_path();
|
|
|
|
FILE *infile;
|
|
|
|
char *ptr, *ptr2 = NULL;
|
|
|
|
int found = 0;
|
|
|
|
char current_line[PATH_MAX];
|
2016-12-11 17:30:16 +00:00
|
|
|
if ((infile = fopen(path, "re")) == NULL)
|
2008-08-19 19:30:36 +00:00
|
|
|
return -1;
|
|
|
|
while (!feof_unlocked(infile)) {
|
|
|
|
if (!fgets_unlocked(current_line, sizeof(current_line), infile)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (current_line[strlen(current_line) - 1])
|
|
|
|
current_line[strlen(current_line) - 1] = 0;
|
|
|
|
/* Skip leading whitespace before the partial context. */
|
|
|
|
ptr = current_line;
|
|
|
|
while (*ptr && isspace(*ptr))
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
if (!(*ptr))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Find the end of the media context. */
|
|
|
|
ptr2 = ptr;
|
|
|
|
while (*ptr2 && !isspace(*ptr2))
|
|
|
|
ptr2++;
|
|
|
|
if (!(*ptr2))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
*ptr2++ = 0;
|
|
|
|
if (strcmp(media, ptr) == 0) {
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-02-06 00:30:42 +00:00
|
|
|
fclose(infile);
|
2008-08-19 19:30:36 +00:00
|
|
|
if (!found)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Skip whitespace. */
|
|
|
|
while (*ptr2 && isspace(*ptr2))
|
|
|
|
ptr2++;
|
|
|
|
if (!(*ptr2)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (selinux_raw_to_trans_context(ptr2, con)) {
|
|
|
|
*con = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|