tzh
2024-08-22 c7d0944258c7d0943aa7b2211498fd612971ce27
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
#include <stdlib.h>
#include <string.h>
 
#include <sepol/policydb/hashtab.h>
#include <sepol/policydb/policydb.h>
 
#include "debug.h"
#include "handle.h"
 
/* Check if a role exists */
int sepol_role_exists(sepol_handle_t * handle __attribute__ ((unused)),
             sepol_policydb_t * p, const char *role, int *response)
{
 
   policydb_t *policydb = &p->p;
   *response = (hashtab_search(policydb->p_roles.table, role) != NULL);
 
   return STATUS_SUCCESS;
}
 
/* Fill an array with all valid roles */
int sepol_role_list(sepol_handle_t * handle,
           sepol_policydb_t * p, char ***roles, unsigned int *nroles)
{
 
   policydb_t *policydb = &p->p;
   unsigned int tmp_nroles = policydb->p_roles.nprim;
   char **tmp_roles = (char **)malloc(tmp_nroles * sizeof(char *));
   char **ptr;
   unsigned int i;
   if (!tmp_roles)
       goto omem;
 
   for (i = 0; i < tmp_nroles; i++) {
       tmp_roles[i] = strdup(policydb->p_role_val_to_name[i]);
       if (!tmp_roles[i])
           goto omem;
   }
 
   *nroles = tmp_nroles;
   *roles = tmp_roles;
 
   return STATUS_SUCCESS;
 
      omem:
   ERR(handle, "out of memory, could not list roles");
 
   ptr = tmp_roles;
   while (ptr && *ptr)
       free(*ptr++);
   free(tmp_roles);
   return STATUS_ERR;
}