liyujie
2025-08-28 786ff4f4ca2374bdd9177f2e24b503d43e7a3b93
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
68
69
70
71
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "get_default_type_internal.h"
#include <errno.h>
 
static int find_default_type(FILE * fp, const char *role, char **type);
 
int get_default_type(const char *role, char **type)
{
   FILE *fp = NULL;
 
   fp = fopen(selinux_default_type_path(), "re");
   if (!fp)
       return -1;
 
   if (find_default_type(fp, role, type) < 0) {
       fclose(fp);
       return -1;
   }
 
   fclose(fp);
   return 0;
}
 
static int find_default_type(FILE * fp, const char *role, char **type)
{
   char buf[250];
   const char *ptr = "", *end;
   char *t;
   size_t len;
   int found = 0;
 
   len = strlen(role);
   while (!feof_unlocked(fp)) {
       if (!fgets_unlocked(buf, sizeof buf, fp)) {
           errno = EINVAL;
           return -1;
       }
       if (buf[strlen(buf) - 1])
           buf[strlen(buf) - 1] = 0;
 
       ptr = buf;
       while (*ptr && isspace(*ptr))
           ptr++;
       if (!(*ptr))
           continue;
 
       if (!strncmp(role, ptr, len)) {
           end = ptr + len;
           if (*end == ':') {
               found = 1;
               ptr = ++end;
               break;
           }
       }
   }
 
   if (!found) {
       errno = EINVAL;
       return -1;
   }
 
   t = malloc(strlen(buf) - len);
   if (!t)
       return -1;
   strcpy(t, ptr);
   *type = t;
   return 0;
}