1 #include <unistd.h> 2 #include <fcntl.h> 3 #include <sys/stat.h> 4 #include <string.h> 5 #include "selinux_internal.h" 6 #include <stdio.h> 7 #include <stdlib.h> 8 #include <ctype.h> 9 #include <errno.h> 10 #include <limits.h> 11 #include <regex.h> 12 #include <stdarg.h> 13 matchmediacon(const char * media,char ** con)14int matchmediacon(const char *media, char ** con) 15 { 16 const char *path = selinux_media_context_path(); 17 FILE *infile; 18 char *ptr, *ptr2 = NULL; 19 int found = 0; 20 char current_line[PATH_MAX]; 21 if ((infile = fopen(path, "re")) == NULL) 22 return -1; 23 while (!feof_unlocked(infile)) { 24 if (!fgets_unlocked(current_line, sizeof(current_line), infile)) { 25 fclose(infile); 26 return -1; 27 } 28 if (current_line[strlen(current_line) - 1]) 29 current_line[strlen(current_line) - 1] = 0; 30 /* Skip leading whitespace before the partial context. */ 31 ptr = current_line; 32 while (*ptr && isspace(*ptr)) 33 ptr++; 34 35 if (!(*ptr)) 36 continue; 37 38 /* Find the end of the media context. */ 39 ptr2 = ptr; 40 while (*ptr2 && !isspace(*ptr2)) 41 ptr2++; 42 if (!(*ptr2)) 43 continue; 44 45 *ptr2++ = 0; 46 if (strcmp(media, ptr) == 0) { 47 found = 1; 48 break; 49 } 50 } 51 fclose(infile); 52 if (!found) 53 return -1; 54 55 /* Skip whitespace. */ 56 while (*ptr2 && isspace(*ptr2)) 57 ptr2++; 58 if (!(*ptr2)) { 59 return -1; 60 } 61 62 if (selinux_raw_to_trans_context(ptr2, con)) { 63 *con = NULL; 64 return -1; 65 } 66 67 return 0; 68 } 69