lin
2025-07-30 fcd736bf35fd93b563e9bbf594f2aa7b62028cc9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#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>
 
int matchmediacon(const char *media, char ** con)
{
   const char *path = selinux_media_context_path();
   FILE *infile;
   char *ptr, *ptr2 = NULL;
   int found = 0;
   char current_line[PATH_MAX];
   if ((infile = fopen(path, "re")) == NULL)
       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;
       }
   }
   fclose(infile);
   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;
}