1 #include <stdlib.h>
2 #include <string.h>
3 
4 #include <sepol/policydb/hashtab.h>
5 #include <sepol/policydb/policydb.h>
6 
7 #include "debug.h"
8 #include "handle.h"
9 
10 /* Check if a role exists */
sepol_role_exists(sepol_handle_t * handle,sepol_policydb_t * p,const char * role,int * response)11 int sepol_role_exists(sepol_handle_t * handle __attribute__ ((unused)),
12 		      sepol_policydb_t * p, const char *role, int *response)
13 {
14 
15 	policydb_t *policydb = &p->p;
16 	*response = (hashtab_search(policydb->p_roles.table, role) != NULL);
17 
18 	return STATUS_SUCCESS;
19 }
20 
21 /* Fill an array with all valid roles */
sepol_role_list(sepol_handle_t * handle,sepol_policydb_t * p,char *** roles,unsigned int * nroles)22 int sepol_role_list(sepol_handle_t * handle,
23 		    sepol_policydb_t * p, char ***roles, unsigned int *nroles)
24 {
25 
26 	policydb_t *policydb = &p->p;
27 	unsigned int tmp_nroles = policydb->p_roles.nprim;
28 	char **tmp_roles = (char **)malloc(tmp_nroles * sizeof(char *));
29 	char **ptr;
30 	unsigned int i;
31 	if (!tmp_roles)
32 		goto omem;
33 
34 	for (i = 0; i < tmp_nroles; i++) {
35 		tmp_roles[i] = strdup(policydb->p_role_val_to_name[i]);
36 		if (!tmp_roles[i])
37 			goto omem;
38 	}
39 
40 	*nroles = tmp_nroles;
41 	*roles = tmp_roles;
42 
43 	return STATUS_SUCCESS;
44 
45       omem:
46 	ERR(handle, "out of memory, could not list roles");
47 
48 	ptr = tmp_roles;
49 	while (ptr && *ptr)
50 		free(*ptr++);
51 	free(tmp_roles);
52 	return STATUS_ERR;
53 }
54