1255e72915d4cbddceb435e13d81601755714e9fSE Android#include <stdio.h>
2b1db49d77789525ac1f4e73e978e35694f21ea1aStephen Smalley
3255e72915d4cbddceb435e13d81601755714e9fSE Android#include <stdlib.h>
4255e72915d4cbddceb435e13d81601755714e9fSE Android#include <ctype.h>
5255e72915d4cbddceb435e13d81601755714e9fSE Android#include <errno.h>
6255e72915d4cbddceb435e13d81601755714e9fSE Android#include <limits.h>
7255e72915d4cbddceb435e13d81601755714e9fSE Android
8255e72915d4cbddceb435e13d81601755714e9fSE Android#include <sepol/policydb/policydb.h>
9b1db49d77789525ac1f4e73e978e35694f21ea1aStephen Smalley
10b1db49d77789525ac1f4e73e978e35694f21ea1aStephen Smalley#ifndef DARWIN
11b1db49d77789525ac1f4e73e978e35694f21ea1aStephen Smalley#include <stdio_ext.h>
12b1db49d77789525ac1f4e73e978e35694f21ea1aStephen Smalley#endif
13b1db49d77789525ac1f4e73e978e35694f21ea1aStephen Smalley
14255e72915d4cbddceb435e13d81601755714e9fSE Android#include <stdarg.h>
15255e72915d4cbddceb435e13d81601755714e9fSE Android
16255e72915d4cbddceb435e13d81601755714e9fSE Android#include "debug.h"
17255e72915d4cbddceb435e13d81601755714e9fSE Android#include "private.h"
18255e72915d4cbddceb435e13d81601755714e9fSE Android#include "dso.h"
19255e72915d4cbddceb435e13d81601755714e9fSE Android#include "mls.h"
20255e72915d4cbddceb435e13d81601755714e9fSE Android
21255e72915d4cbddceb435e13d81601755714e9fSE Android/* -- Deprecated -- */
22255e72915d4cbddceb435e13d81601755714e9fSE Android
23255e72915d4cbddceb435e13d81601755714e9fSE Androidvoid sepol_set_delusers(int on __attribute((unused)))
24255e72915d4cbddceb435e13d81601755714e9fSE Android{
25255e72915d4cbddceb435e13d81601755714e9fSE Android	WARN(NULL, "Deprecated interface");
26255e72915d4cbddceb435e13d81601755714e9fSE Android}
27255e72915d4cbddceb435e13d81601755714e9fSE Android
28255e72915d4cbddceb435e13d81601755714e9fSE Android#undef BADLINE
29255e72915d4cbddceb435e13d81601755714e9fSE Android#define BADLINE() { \
30255e72915d4cbddceb435e13d81601755714e9fSE Android	ERR(NULL, "invalid entry %s (%s:%u)", \
31255e72915d4cbddceb435e13d81601755714e9fSE Android		buffer, path, lineno); \
32255e72915d4cbddceb435e13d81601755714e9fSE Android	continue; \
33255e72915d4cbddceb435e13d81601755714e9fSE Android}
34255e72915d4cbddceb435e13d81601755714e9fSE Android
35255e72915d4cbddceb435e13d81601755714e9fSE Androidstatic int load_users(struct policydb *policydb, const char *path)
36255e72915d4cbddceb435e13d81601755714e9fSE Android{
37255e72915d4cbddceb435e13d81601755714e9fSE Android	FILE *fp;
38255e72915d4cbddceb435e13d81601755714e9fSE Android	char *buffer = NULL, *p, *q, oldc;
39255e72915d4cbddceb435e13d81601755714e9fSE Android	size_t len = 0;
40255e72915d4cbddceb435e13d81601755714e9fSE Android	ssize_t nread;
41255e72915d4cbddceb435e13d81601755714e9fSE Android	unsigned lineno = 0, islist = 0, bit;
42255e72915d4cbddceb435e13d81601755714e9fSE Android	user_datum_t *usrdatum;
43255e72915d4cbddceb435e13d81601755714e9fSE Android	role_datum_t *roldatum;
44255e72915d4cbddceb435e13d81601755714e9fSE Android	ebitmap_node_t *rnode;
45255e72915d4cbddceb435e13d81601755714e9fSE Android
46255e72915d4cbddceb435e13d81601755714e9fSE Android	fp = fopen(path, "r");
47255e72915d4cbddceb435e13d81601755714e9fSE Android	if (fp == NULL)
48255e72915d4cbddceb435e13d81601755714e9fSE Android		return -1;
49255e72915d4cbddceb435e13d81601755714e9fSE Android
50b1db49d77789525ac1f4e73e978e35694f21ea1aStephen Smalley#ifdef DARWIN
51b1db49d77789525ac1f4e73e978e35694f21ea1aStephen Smalley	if ((buffer = (char *)malloc(255 * sizeof(char))) == NULL) {
52b1db49d77789525ac1f4e73e978e35694f21ea1aStephen Smalley	  ERR(NULL, "out of memory");
53b1db49d77789525ac1f4e73e978e35694f21ea1aStephen Smalley	  return -1;
54b1db49d77789525ac1f4e73e978e35694f21ea1aStephen Smalley	}
55b1db49d77789525ac1f4e73e978e35694f21ea1aStephen Smalley
56b1db49d77789525ac1f4e73e978e35694f21ea1aStephen Smalley	while(fgets(buffer, 255, fp) != NULL) {
57b1db49d77789525ac1f4e73e978e35694f21ea1aStephen Smalley#else
58b1db49d77789525ac1f4e73e978e35694f21ea1aStephen Smalley	__fsetlocking(fp, FSETLOCKING_BYCALLER);
59255e72915d4cbddceb435e13d81601755714e9fSE Android	while ((nread = getline(&buffer, &len, fp)) > 0) {
60b1db49d77789525ac1f4e73e978e35694f21ea1aStephen Smalley#endif
61b1db49d77789525ac1f4e73e978e35694f21ea1aStephen Smalley
62255e72915d4cbddceb435e13d81601755714e9fSE Android		lineno++;
63255e72915d4cbddceb435e13d81601755714e9fSE Android		if (buffer[nread - 1] == '\n')
64255e72915d4cbddceb435e13d81601755714e9fSE Android			buffer[nread - 1] = 0;
65255e72915d4cbddceb435e13d81601755714e9fSE Android		p = buffer;
66255e72915d4cbddceb435e13d81601755714e9fSE Android		while (*p && isspace(*p))
67255e72915d4cbddceb435e13d81601755714e9fSE Android			p++;
68255e72915d4cbddceb435e13d81601755714e9fSE Android		if (!(*p) || *p == '#')
69255e72915d4cbddceb435e13d81601755714e9fSE Android			continue;
70255e72915d4cbddceb435e13d81601755714e9fSE Android
71255e72915d4cbddceb435e13d81601755714e9fSE Android		if (strncasecmp(p, "user", 4))
72255e72915d4cbddceb435e13d81601755714e9fSE Android			BADLINE();
73255e72915d4cbddceb435e13d81601755714e9fSE Android		p += 4;
74255e72915d4cbddceb435e13d81601755714e9fSE Android		if (!isspace(*p))
75255e72915d4cbddceb435e13d81601755714e9fSE Android			BADLINE();
76255e72915d4cbddceb435e13d81601755714e9fSE Android		while (*p && isspace(*p))
77255e72915d4cbddceb435e13d81601755714e9fSE Android			p++;
78255e72915d4cbddceb435e13d81601755714e9fSE Android		if (!(*p))
79255e72915d4cbddceb435e13d81601755714e9fSE Android			BADLINE();
80255e72915d4cbddceb435e13d81601755714e9fSE Android		q = p;
81255e72915d4cbddceb435e13d81601755714e9fSE Android		while (*p && !isspace(*p))
82255e72915d4cbddceb435e13d81601755714e9fSE Android			p++;
83255e72915d4cbddceb435e13d81601755714e9fSE Android		if (!(*p))
84255e72915d4cbddceb435e13d81601755714e9fSE Android			BADLINE();
85255e72915d4cbddceb435e13d81601755714e9fSE Android		*p++ = 0;
86255e72915d4cbddceb435e13d81601755714e9fSE Android
87255e72915d4cbddceb435e13d81601755714e9fSE Android		usrdatum = hashtab_search(policydb->p_users.table, q);
88255e72915d4cbddceb435e13d81601755714e9fSE Android		if (usrdatum) {
89255e72915d4cbddceb435e13d81601755714e9fSE Android			/* Replacing an existing user definition. */
90255e72915d4cbddceb435e13d81601755714e9fSE Android			ebitmap_destroy(&usrdatum->roles.roles);
91255e72915d4cbddceb435e13d81601755714e9fSE Android			ebitmap_init(&usrdatum->roles.roles);
92255e72915d4cbddceb435e13d81601755714e9fSE Android		} else {
93255e72915d4cbddceb435e13d81601755714e9fSE Android			char *id = strdup(q);
94255e72915d4cbddceb435e13d81601755714e9fSE Android
95255e72915d4cbddceb435e13d81601755714e9fSE Android			/* Adding a new user definition. */
96255e72915d4cbddceb435e13d81601755714e9fSE Android			usrdatum =
97255e72915d4cbddceb435e13d81601755714e9fSE Android			    (user_datum_t *) malloc(sizeof(user_datum_t));
98255e72915d4cbddceb435e13d81601755714e9fSE Android			if (!id || !usrdatum) {
99255e72915d4cbddceb435e13d81601755714e9fSE Android				ERR(NULL, "out of memory");
100255e72915d4cbddceb435e13d81601755714e9fSE Android				free(buffer);
101255e72915d4cbddceb435e13d81601755714e9fSE Android				fclose(fp);
102255e72915d4cbddceb435e13d81601755714e9fSE Android				return -1;
103255e72915d4cbddceb435e13d81601755714e9fSE Android			}
104255e72915d4cbddceb435e13d81601755714e9fSE Android			memset(usrdatum, 0, sizeof(user_datum_t));
105255e72915d4cbddceb435e13d81601755714e9fSE Android			usrdatum->s.value = ++policydb->p_users.nprim;
106255e72915d4cbddceb435e13d81601755714e9fSE Android			ebitmap_init(&usrdatum->roles.roles);
107255e72915d4cbddceb435e13d81601755714e9fSE Android			if (hashtab_insert(policydb->p_users.table,
108255e72915d4cbddceb435e13d81601755714e9fSE Android					   id, (hashtab_datum_t) usrdatum)) {
109255e72915d4cbddceb435e13d81601755714e9fSE Android				ERR(NULL, "out of memory");
110255e72915d4cbddceb435e13d81601755714e9fSE Android				free(buffer);
111255e72915d4cbddceb435e13d81601755714e9fSE Android				fclose(fp);
112255e72915d4cbddceb435e13d81601755714e9fSE Android				return -1;
113255e72915d4cbddceb435e13d81601755714e9fSE Android			}
114255e72915d4cbddceb435e13d81601755714e9fSE Android		}
115255e72915d4cbddceb435e13d81601755714e9fSE Android
116255e72915d4cbddceb435e13d81601755714e9fSE Android		while (*p && isspace(*p))
117255e72915d4cbddceb435e13d81601755714e9fSE Android			p++;
118255e72915d4cbddceb435e13d81601755714e9fSE Android		if (!(*p))
119255e72915d4cbddceb435e13d81601755714e9fSE Android			BADLINE();
120255e72915d4cbddceb435e13d81601755714e9fSE Android		if (strncasecmp(p, "roles", 5))
121255e72915d4cbddceb435e13d81601755714e9fSE Android			BADLINE();
122255e72915d4cbddceb435e13d81601755714e9fSE Android		p += 5;
123255e72915d4cbddceb435e13d81601755714e9fSE Android		if (!isspace(*p))
124255e72915d4cbddceb435e13d81601755714e9fSE Android			BADLINE();
125255e72915d4cbddceb435e13d81601755714e9fSE Android		while (*p && isspace(*p))
126255e72915d4cbddceb435e13d81601755714e9fSE Android			p++;
127255e72915d4cbddceb435e13d81601755714e9fSE Android		if (!(*p))
128255e72915d4cbddceb435e13d81601755714e9fSE Android			BADLINE();
129255e72915d4cbddceb435e13d81601755714e9fSE Android		if (*p == '{') {
130255e72915d4cbddceb435e13d81601755714e9fSE Android			islist = 1;
131255e72915d4cbddceb435e13d81601755714e9fSE Android			p++;
132255e72915d4cbddceb435e13d81601755714e9fSE Android		} else
133255e72915d4cbddceb435e13d81601755714e9fSE Android			islist = 0;
134255e72915d4cbddceb435e13d81601755714e9fSE Android
135255e72915d4cbddceb435e13d81601755714e9fSE Android		oldc = 0;
136255e72915d4cbddceb435e13d81601755714e9fSE Android		do {
137255e72915d4cbddceb435e13d81601755714e9fSE Android			while (*p && isspace(*p))
138255e72915d4cbddceb435e13d81601755714e9fSE Android				p++;
139255e72915d4cbddceb435e13d81601755714e9fSE Android			if (!(*p))
140255e72915d4cbddceb435e13d81601755714e9fSE Android				break;
141255e72915d4cbddceb435e13d81601755714e9fSE Android
142255e72915d4cbddceb435e13d81601755714e9fSE Android			q = p;
143255e72915d4cbddceb435e13d81601755714e9fSE Android			while (*p && *p != ';' && *p != '}' && !isspace(*p))
144255e72915d4cbddceb435e13d81601755714e9fSE Android				p++;
145255e72915d4cbddceb435e13d81601755714e9fSE Android			if (!(*p))
146255e72915d4cbddceb435e13d81601755714e9fSE Android				break;
147255e72915d4cbddceb435e13d81601755714e9fSE Android			if (*p == '}')
148255e72915d4cbddceb435e13d81601755714e9fSE Android				islist = 0;
149255e72915d4cbddceb435e13d81601755714e9fSE Android			oldc = *p;
150255e72915d4cbddceb435e13d81601755714e9fSE Android			*p++ = 0;
151255e72915d4cbddceb435e13d81601755714e9fSE Android			if (!q[0])
152255e72915d4cbddceb435e13d81601755714e9fSE Android				break;
153255e72915d4cbddceb435e13d81601755714e9fSE Android
154255e72915d4cbddceb435e13d81601755714e9fSE Android			roldatum = hashtab_search(policydb->p_roles.table, q);
155255e72915d4cbddceb435e13d81601755714e9fSE Android			if (!roldatum) {
156255e72915d4cbddceb435e13d81601755714e9fSE Android				ERR(NULL, "undefined role %s (%s:%u)",
157255e72915d4cbddceb435e13d81601755714e9fSE Android				    q, path, lineno);
158255e72915d4cbddceb435e13d81601755714e9fSE Android				continue;
159255e72915d4cbddceb435e13d81601755714e9fSE Android			}
160255e72915d4cbddceb435e13d81601755714e9fSE Android			/* Set the role and every role it dominates */
161255e72915d4cbddceb435e13d81601755714e9fSE Android			ebitmap_for_each_bit(&roldatum->dominates, rnode, bit) {
162255e72915d4cbddceb435e13d81601755714e9fSE Android				if (ebitmap_node_get_bit(rnode, bit))
163255e72915d4cbddceb435e13d81601755714e9fSE Android					if (ebitmap_set_bit
164255e72915d4cbddceb435e13d81601755714e9fSE Android					    (&usrdatum->roles.roles, bit, 1)) {
165255e72915d4cbddceb435e13d81601755714e9fSE Android						ERR(NULL, "out of memory");
166255e72915d4cbddceb435e13d81601755714e9fSE Android						free(buffer);
167255e72915d4cbddceb435e13d81601755714e9fSE Android						fclose(fp);
168255e72915d4cbddceb435e13d81601755714e9fSE Android						return -1;
169255e72915d4cbddceb435e13d81601755714e9fSE Android					}
170255e72915d4cbddceb435e13d81601755714e9fSE Android			}
171255e72915d4cbddceb435e13d81601755714e9fSE Android		} while (islist);
172255e72915d4cbddceb435e13d81601755714e9fSE Android		if (oldc == 0)
173255e72915d4cbddceb435e13d81601755714e9fSE Android			BADLINE();
174255e72915d4cbddceb435e13d81601755714e9fSE Android
175255e72915d4cbddceb435e13d81601755714e9fSE Android		if (policydb->mls) {
176255e72915d4cbddceb435e13d81601755714e9fSE Android			context_struct_t context;
177255e72915d4cbddceb435e13d81601755714e9fSE Android			char *scontext, *r, *s;
178255e72915d4cbddceb435e13d81601755714e9fSE Android
179255e72915d4cbddceb435e13d81601755714e9fSE Android			while (*p && isspace(*p))
180255e72915d4cbddceb435e13d81601755714e9fSE Android				p++;
181255e72915d4cbddceb435e13d81601755714e9fSE Android			if (!(*p))
182255e72915d4cbddceb435e13d81601755714e9fSE Android				BADLINE();
183255e72915d4cbddceb435e13d81601755714e9fSE Android			if (strncasecmp(p, "level", 5))
184255e72915d4cbddceb435e13d81601755714e9fSE Android				BADLINE();
185255e72915d4cbddceb435e13d81601755714e9fSE Android			p += 5;
186255e72915d4cbddceb435e13d81601755714e9fSE Android			if (!isspace(*p))
187255e72915d4cbddceb435e13d81601755714e9fSE Android				BADLINE();
188255e72915d4cbddceb435e13d81601755714e9fSE Android			while (*p && isspace(*p))
189255e72915d4cbddceb435e13d81601755714e9fSE Android				p++;
190255e72915d4cbddceb435e13d81601755714e9fSE Android			if (!(*p))
191255e72915d4cbddceb435e13d81601755714e9fSE Android				BADLINE();
192255e72915d4cbddceb435e13d81601755714e9fSE Android			q = p;
193255e72915d4cbddceb435e13d81601755714e9fSE Android			while (*p && strncasecmp(p, "range", 5))
194255e72915d4cbddceb435e13d81601755714e9fSE Android				p++;
195255e72915d4cbddceb435e13d81601755714e9fSE Android			if (!(*p))
196255e72915d4cbddceb435e13d81601755714e9fSE Android				BADLINE();
197255e72915d4cbddceb435e13d81601755714e9fSE Android			*--p = 0;
198255e72915d4cbddceb435e13d81601755714e9fSE Android			p++;
199255e72915d4cbddceb435e13d81601755714e9fSE Android
200255e72915d4cbddceb435e13d81601755714e9fSE Android			scontext = malloc(p - q);
201255e72915d4cbddceb435e13d81601755714e9fSE Android			if (!scontext) {
202255e72915d4cbddceb435e13d81601755714e9fSE Android				ERR(NULL, "out of memory");
203255e72915d4cbddceb435e13d81601755714e9fSE Android				free(buffer);
204255e72915d4cbddceb435e13d81601755714e9fSE Android				fclose(fp);
205255e72915d4cbddceb435e13d81601755714e9fSE Android				return -1;
206255e72915d4cbddceb435e13d81601755714e9fSE Android			}
207255e72915d4cbddceb435e13d81601755714e9fSE Android			r = scontext;
208255e72915d4cbddceb435e13d81601755714e9fSE Android			s = q;
209255e72915d4cbddceb435e13d81601755714e9fSE Android			while (*s) {
210255e72915d4cbddceb435e13d81601755714e9fSE Android				if (!isspace(*s))
211255e72915d4cbddceb435e13d81601755714e9fSE Android					*r++ = *s;
212255e72915d4cbddceb435e13d81601755714e9fSE Android				s++;
213255e72915d4cbddceb435e13d81601755714e9fSE Android			}
214255e72915d4cbddceb435e13d81601755714e9fSE Android			*r = 0;
215255e72915d4cbddceb435e13d81601755714e9fSE Android			r = scontext;
216255e72915d4cbddceb435e13d81601755714e9fSE Android
217255e72915d4cbddceb435e13d81601755714e9fSE Android			context_init(&context);
218255e72915d4cbddceb435e13d81601755714e9fSE Android			if (mls_context_to_sid(policydb, oldc, &r, &context) <
219255e72915d4cbddceb435e13d81601755714e9fSE Android			    0) {
220255e72915d4cbddceb435e13d81601755714e9fSE Android				ERR(NULL, "invalid level %s (%s:%u)", scontext,
221255e72915d4cbddceb435e13d81601755714e9fSE Android				    path, lineno);
222255e72915d4cbddceb435e13d81601755714e9fSE Android				free(scontext);
223255e72915d4cbddceb435e13d81601755714e9fSE Android				continue;
224255e72915d4cbddceb435e13d81601755714e9fSE Android
225255e72915d4cbddceb435e13d81601755714e9fSE Android			}
226255e72915d4cbddceb435e13d81601755714e9fSE Android			free(scontext);
227255e72915d4cbddceb435e13d81601755714e9fSE Android			memcpy(&usrdatum->dfltlevel, &context.range.level[0],
228255e72915d4cbddceb435e13d81601755714e9fSE Android			       sizeof(usrdatum->dfltlevel));
229255e72915d4cbddceb435e13d81601755714e9fSE Android
230255e72915d4cbddceb435e13d81601755714e9fSE Android			if (strncasecmp(p, "range", 5))
231255e72915d4cbddceb435e13d81601755714e9fSE Android				BADLINE();
232255e72915d4cbddceb435e13d81601755714e9fSE Android			p += 5;
233255e72915d4cbddceb435e13d81601755714e9fSE Android			if (!isspace(*p))
234255e72915d4cbddceb435e13d81601755714e9fSE Android				BADLINE();
235255e72915d4cbddceb435e13d81601755714e9fSE Android			while (*p && isspace(*p))
236255e72915d4cbddceb435e13d81601755714e9fSE Android				p++;
237255e72915d4cbddceb435e13d81601755714e9fSE Android			if (!(*p))
238255e72915d4cbddceb435e13d81601755714e9fSE Android				BADLINE();
239255e72915d4cbddceb435e13d81601755714e9fSE Android			q = p;
240255e72915d4cbddceb435e13d81601755714e9fSE Android			while (*p && *p != ';')
241255e72915d4cbddceb435e13d81601755714e9fSE Android				p++;
242255e72915d4cbddceb435e13d81601755714e9fSE Android			if (!(*p))
243255e72915d4cbddceb435e13d81601755714e9fSE Android				BADLINE();
244255e72915d4cbddceb435e13d81601755714e9fSE Android			*p++ = 0;
245255e72915d4cbddceb435e13d81601755714e9fSE Android
246255e72915d4cbddceb435e13d81601755714e9fSE Android			scontext = malloc(p - q);
247255e72915d4cbddceb435e13d81601755714e9fSE Android			if (!scontext) {
248255e72915d4cbddceb435e13d81601755714e9fSE Android				ERR(NULL, "out of memory");
249255e72915d4cbddceb435e13d81601755714e9fSE Android				free(buffer);
250255e72915d4cbddceb435e13d81601755714e9fSE Android				fclose(fp);
251255e72915d4cbddceb435e13d81601755714e9fSE Android				return -1;
252255e72915d4cbddceb435e13d81601755714e9fSE Android			}
253255e72915d4cbddceb435e13d81601755714e9fSE Android			r = scontext;
254255e72915d4cbddceb435e13d81601755714e9fSE Android			s = q;
255255e72915d4cbddceb435e13d81601755714e9fSE Android			while (*s) {
256255e72915d4cbddceb435e13d81601755714e9fSE Android				if (!isspace(*s))
257255e72915d4cbddceb435e13d81601755714e9fSE Android					*r++ = *s;
258255e72915d4cbddceb435e13d81601755714e9fSE Android				s++;
259255e72915d4cbddceb435e13d81601755714e9fSE Android			}
260255e72915d4cbddceb435e13d81601755714e9fSE Android			*r = 0;
261255e72915d4cbddceb435e13d81601755714e9fSE Android			r = scontext;
262255e72915d4cbddceb435e13d81601755714e9fSE Android
263255e72915d4cbddceb435e13d81601755714e9fSE Android			context_init(&context);
264255e72915d4cbddceb435e13d81601755714e9fSE Android			if (mls_context_to_sid(policydb, oldc, &r, &context) <
265255e72915d4cbddceb435e13d81601755714e9fSE Android			    0) {
266255e72915d4cbddceb435e13d81601755714e9fSE Android				ERR(NULL, "invalid range %s (%s:%u)", scontext,
267255e72915d4cbddceb435e13d81601755714e9fSE Android				    path, lineno);
268255e72915d4cbddceb435e13d81601755714e9fSE Android				free(scontext);
269255e72915d4cbddceb435e13d81601755714e9fSE Android				continue;
270255e72915d4cbddceb435e13d81601755714e9fSE Android			}
271255e72915d4cbddceb435e13d81601755714e9fSE Android			free(scontext);
272255e72915d4cbddceb435e13d81601755714e9fSE Android			memcpy(&usrdatum->range, &context.range,
273255e72915d4cbddceb435e13d81601755714e9fSE Android			       sizeof(usrdatum->range));
274255e72915d4cbddceb435e13d81601755714e9fSE Android		}
275255e72915d4cbddceb435e13d81601755714e9fSE Android	}
276255e72915d4cbddceb435e13d81601755714e9fSE Android
277255e72915d4cbddceb435e13d81601755714e9fSE Android	free(buffer);
278255e72915d4cbddceb435e13d81601755714e9fSE Android	fclose(fp);
279255e72915d4cbddceb435e13d81601755714e9fSE Android	return 0;
280255e72915d4cbddceb435e13d81601755714e9fSE Android}
281255e72915d4cbddceb435e13d81601755714e9fSE Android
282255e72915d4cbddceb435e13d81601755714e9fSE Androidint sepol_genusers(void *data, size_t len,
283255e72915d4cbddceb435e13d81601755714e9fSE Android		   const char *usersdir, void **newdata, size_t * newlen)
284255e72915d4cbddceb435e13d81601755714e9fSE Android{
285255e72915d4cbddceb435e13d81601755714e9fSE Android	struct policydb policydb;
286255e72915d4cbddceb435e13d81601755714e9fSE Android	char path[PATH_MAX];
287255e72915d4cbddceb435e13d81601755714e9fSE Android
288255e72915d4cbddceb435e13d81601755714e9fSE Android	/* Construct policy database */
289255e72915d4cbddceb435e13d81601755714e9fSE Android	if (policydb_init(&policydb))
290255e72915d4cbddceb435e13d81601755714e9fSE Android		goto err;
291255e72915d4cbddceb435e13d81601755714e9fSE Android	if (policydb_from_image(NULL, data, len, &policydb) < 0)
292255e72915d4cbddceb435e13d81601755714e9fSE Android		goto err;
293255e72915d4cbddceb435e13d81601755714e9fSE Android
294255e72915d4cbddceb435e13d81601755714e9fSE Android	/* Load locally defined users. */
295255e72915d4cbddceb435e13d81601755714e9fSE Android	snprintf(path, sizeof path, "%s/local.users", usersdir);
296255e72915d4cbddceb435e13d81601755714e9fSE Android	if (load_users(&policydb, path) < 0)
297255e72915d4cbddceb435e13d81601755714e9fSE Android		goto err_destroy;
298255e72915d4cbddceb435e13d81601755714e9fSE Android
299255e72915d4cbddceb435e13d81601755714e9fSE Android	/* Write policy database */
300255e72915d4cbddceb435e13d81601755714e9fSE Android	if (policydb_to_image(NULL, &policydb, newdata, newlen) < 0)
301255e72915d4cbddceb435e13d81601755714e9fSE Android		goto err_destroy;
302255e72915d4cbddceb435e13d81601755714e9fSE Android
303255e72915d4cbddceb435e13d81601755714e9fSE Android	policydb_destroy(&policydb);
304255e72915d4cbddceb435e13d81601755714e9fSE Android	return 0;
305255e72915d4cbddceb435e13d81601755714e9fSE Android
306255e72915d4cbddceb435e13d81601755714e9fSE Android      err_destroy:
307255e72915d4cbddceb435e13d81601755714e9fSE Android	policydb_destroy(&policydb);
308255e72915d4cbddceb435e13d81601755714e9fSE Android
309255e72915d4cbddceb435e13d81601755714e9fSE Android      err:
310255e72915d4cbddceb435e13d81601755714e9fSE Android	return -1;
311255e72915d4cbddceb435e13d81601755714e9fSE Android}
312255e72915d4cbddceb435e13d81601755714e9fSE Android
313255e72915d4cbddceb435e13d81601755714e9fSE Androidint hidden sepol_genusers_policydb(policydb_t * policydb, const char *usersdir)
314255e72915d4cbddceb435e13d81601755714e9fSE Android{
315255e72915d4cbddceb435e13d81601755714e9fSE Android	char path[PATH_MAX];
316255e72915d4cbddceb435e13d81601755714e9fSE Android
317255e72915d4cbddceb435e13d81601755714e9fSE Android	/* Load locally defined users. */
318255e72915d4cbddceb435e13d81601755714e9fSE Android	snprintf(path, sizeof path, "%s/local.users", usersdir);
319255e72915d4cbddceb435e13d81601755714e9fSE Android	if (load_users(policydb, path) < 0) {
320255e72915d4cbddceb435e13d81601755714e9fSE Android		ERR(NULL, "unable to load local.users: %s", strerror(errno));
321255e72915d4cbddceb435e13d81601755714e9fSE Android		return -1;
322255e72915d4cbddceb435e13d81601755714e9fSE Android	}
323255e72915d4cbddceb435e13d81601755714e9fSE Android
324255e72915d4cbddceb435e13d81601755714e9fSE Android	if (policydb_reindex_users(policydb) < 0) {
325255e72915d4cbddceb435e13d81601755714e9fSE Android		ERR(NULL, "unable to reindex users: %s", strerror(errno));
326255e72915d4cbddceb435e13d81601755714e9fSE Android		return -1;
327255e72915d4cbddceb435e13d81601755714e9fSE Android
328255e72915d4cbddceb435e13d81601755714e9fSE Android	}
329255e72915d4cbddceb435e13d81601755714e9fSE Android
330255e72915d4cbddceb435e13d81601755714e9fSE Android	return 0;
331255e72915d4cbddceb435e13d81601755714e9fSE Android}
332255e72915d4cbddceb435e13d81601755714e9fSE Android
333255e72915d4cbddceb435e13d81601755714e9fSE Android/* -- End Deprecated -- */
334