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 */
11int 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,
17				    (const hashtab_key_t)role) != NULL);
18
19	handle = NULL;
20	return STATUS_SUCCESS;
21}
22
23/* Fill an array with all valid roles */
24int sepol_role_list(sepol_handle_t * handle,
25		    sepol_policydb_t * p, char ***roles, unsigned int *nroles)
26{
27
28	policydb_t *policydb = &p->p;
29	unsigned int tmp_nroles = policydb->p_roles.nprim;
30	char **tmp_roles = (char **)malloc(tmp_nroles * sizeof(char *));
31	char **ptr;
32	unsigned int i;
33	if (!tmp_roles)
34		goto omem;
35
36	for (i = 0; i < tmp_nroles; i++) {
37		tmp_roles[i] = strdup(policydb->p_role_val_to_name[i]);
38		if (!tmp_roles[i])
39			goto omem;
40	}
41
42	*nroles = tmp_nroles;
43	*roles = tmp_roles;
44
45	return STATUS_SUCCESS;
46
47      omem:
48	ERR(handle, "out of memory, could not list roles");
49
50	ptr = tmp_roles;
51	while (ptr && *ptr)
52		free(*ptr++);
53	free(tmp_roles);
54	return STATUS_ERR;
55}
56