summaryrefslogtreecommitdiffstats
path: root/libqpol/src/nodecon_query.c
blob: 4a91a1f8c2068a746484e1939269ba4e3f38329b (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/**
*  @file
*  Defines the public interface for searching and iterating over nodecon statements.
*
*  @author Kevin Carr kcarr@tresys.com
*  @author Jeremy A. Mowery jmowery@tresys.com
*  @author Jason Tang jtang@tresys.com
*
*  Copyright (C) 2006-2007 Tresys Technology, LLC
*
*  This library is free software; you can redistribute it and/or
*  modify it under the terms of the GNU Lesser General Public
*  License as published by the Free Software Foundation; either
*  version 2.1 of the License, or (at your option) any later version.
*
*  This library is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
*  Lesser General Public License for more details.
*
*  You should have received a copy of the GNU Lesser General Public
*  License along with this library; if not, write to the Free Software
*  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <stddef.h>
#include <stdlib.h>
#include <stdint.h>
#include <qpol/iterator.h>
#include <qpol/policy.h>
#include <qpol/context_query.h>
#include <qpol/nodecon_query.h>
#include <sepol/policydb/policydb.h>
#include "qpol_internal.h"
#include "iterator_internal.h"

struct qpol_nodecon
{
	ocontext_t *ocon;
	unsigned char protocol;
};

int qpol_policy_get_nodecon_by_node(const qpol_policy_t * policy, uint32_t addr[4], uint32_t mask[4], unsigned char protocol,
				    qpol_nodecon_t ** ocon)
{
	policydb_t *db = NULL;
	ocontext_t *tmp = NULL;
	int error = 0;

	if (ocon != NULL)
		*ocon = NULL;

	if (policy == NULL || ocon == NULL) {
		ERR(policy, "%s", strerror(EINVAL));
		errno = EINVAL;
		return STATUS_ERR;
	}

	db = &policy->p->p;

	for (tmp = db->ocontexts[(protocol == QPOL_IPV4 ? OCON_NODE : OCON_NODE6)]; tmp; tmp = tmp->next) {
		if (protocol == QPOL_IPV4) {
			if (addr[0] != tmp->u.node.addr || mask[0] != tmp->u.node.mask)
				continue;
		} else {
			if (memcmp(addr, tmp->u.node6.addr, 16) || memcmp(mask, tmp->u.node6.mask, 16))
				continue;
		}
		*ocon = calloc(1, sizeof(qpol_nodecon_t));
		if (*ocon == NULL) {
			error = errno;
			ERR(policy, "%s", strerror(error));
			errno = error;
			return STATUS_ERR;
		}

		(*ocon)->protocol = protocol == QPOL_IPV4 ? QPOL_IPV4 : QPOL_IPV6;
		(*ocon)->ocon = tmp;
	}

	if (*ocon == NULL) {
		ERR(policy, "%s", "could not find nodecon statement for node");
		errno = ENOENT;
		return STATUS_ERR;
	}

	return STATUS_SUCCESS;
}

typedef struct node_state
{
	ocon_state_t *v4state;
	ocon_state_t *v6state;
} node_state_t;

static void node_state_free(void *ns)
{
	node_state_t *ins = (node_state_t *) ns;

	if (!ns)
		return;

	free(ins->v4state);
	free(ins->v6state);
	free(ns);
}

static int node_state_end(const qpol_iterator_t * iter)
{
	node_state_t *ns = NULL;

	if (iter == NULL || qpol_iterator_state(iter) == NULL) {
		errno = EINVAL;
		return STATUS_ERR;
	}

	ns = (node_state_t *) qpol_iterator_state(iter);

	return (ns->v4state->cur == NULL && ns->v6state->cur == NULL);
}

static void *node_state_get_cur(const qpol_iterator_t * iter)
{
	node_state_t *ns = NULL;
	qpol_nodecon_t *node = NULL;

	if (iter == NULL || qpol_iterator_state(iter) == NULL || node_state_end(iter)) {
		errno = EINVAL;
		return NULL;
	}

	ns = (node_state_t *) qpol_iterator_state(iter);

	node = calloc(1, sizeof(qpol_nodecon_t));
	if (!node) {
		return NULL;
	}

	node->ocon = ns->v4state->cur ? ns->v4state->cur : ns->v6state->cur;
	node->protocol = ns->v4state->cur ? QPOL_IPV4 : QPOL_IPV6;

	return node;
}

static size_t node_state_size(const qpol_iterator_t * iter)
{
	node_state_t *ns = NULL;
	size_t count = 0;
	ocontext_t *ocon = NULL;

	if (iter == NULL || qpol_iterator_state(iter) == NULL) {
		errno = EINVAL;
		return 0;
	}

	ns = (node_state_t *) qpol_iterator_state(iter);

	if (ns->v4state)
		for (ocon = ns->v4state->head; ocon; ocon = ocon->next)
			count++;

	if (ns->v6state)
		for (ocon = ns->v6state->head; ocon; ocon = ocon->next)
			count++;

	return count;
}

static int node_state_next(qpol_iterator_t * iter)
{
	node_state_t *ns = NULL;

	if (iter == NULL || qpol_iterator_state(iter) == NULL) {
		errno = EINVAL;
		return STATUS_ERR;
	}

	ns = (node_state_t *) qpol_iterator_state(iter);

	if (ns->v4state->cur == NULL && ns->v6state->cur == NULL) {
		errno = ERANGE;
		return STATUS_ERR;
	}

	if (ns->v4state->cur)
		ns->v4state->cur = ns->v4state->cur->next;
	else
		ns->v6state->cur = ns->v6state->cur->next;

	return STATUS_SUCCESS;
}

int qpol_policy_get_nodecon_iter(const qpol_policy_t * policy, qpol_iterator_t ** iter)
{
	policydb_t *db = NULL;
	int error = 0;
	ocon_state_t *v4os = NULL, *v6os = NULL;
	node_state_t *ns = NULL;

	if (iter != NULL)
		*iter = NULL;

	if (policy == NULL || iter == NULL) {
		ERR(policy, "%s", strerror(EINVAL));
		errno = EINVAL;
		return STATUS_ERR;
	}

	db = &policy->p->p;

	v4os = calloc(1, sizeof(ocon_state_t));
	if (v4os == NULL) {
		error = errno;
		ERR(policy, "%s", strerror(ENOMEM));
		errno = error;
		return STATUS_ERR;
	}
	v4os->head = v4os->cur = db->ocontexts[OCON_NODE];

	v6os = calloc(1, sizeof(ocon_state_t));
	if (v6os == NULL) {
		error = errno;
		ERR(policy, "%s", strerror(ENOMEM));
		free(v4os);
		errno = error;
		return STATUS_ERR;
	}
	v6os->head = v6os->cur = db->ocontexts[OCON_NODE6];

	ns = calloc(1, sizeof(node_state_t));
	if (ns == NULL) {
		error = errno;
		ERR(policy, "%s", strerror(ENOMEM));
		free(v4os);
		free(v6os);
		errno = error;
		return STATUS_ERR;
	}
	ns->v4state = v4os;
	ns->v6state = v6os;

	if (qpol_iterator_create(policy, (void *)ns, node_state_get_cur,
				 node_state_next, node_state_end, node_state_size, node_state_free, iter)) {
		node_state_free(ns);
		return STATUS_ERR;
	}

	return STATUS_SUCCESS;
}

int qpol_nodecon_get_addr(const qpol_policy_t * policy, const qpol_nodecon_t * ocon, uint32_t ** addr, unsigned char *protocol)
{
	if (addr != NULL)
		*addr = NULL;
	if (protocol != NULL)
		*protocol = 0;

	if (policy == NULL || ocon == NULL || addr == NULL || protocol == NULL) {
		ERR(policy, "%s", strerror(EINVAL));
		errno = EINVAL;
		return STATUS_ERR;
	}

	*protocol = ocon->protocol;

	if (ocon->protocol == QPOL_IPV4) {
		*addr = &(ocon->ocon->u.node.addr);
	} else {
		*addr = ocon->ocon->u.node6.addr;
	}

	return STATUS_SUCCESS;
}

int qpol_nodecon_get_mask(const qpol_policy_t * policy, const qpol_nodecon_t * ocon, uint32_t ** mask, unsigned char *protocol)
{
	if (mask != NULL)
		*mask = NULL;
	if (protocol != NULL)
		*protocol = 0;

	if (policy == NULL || ocon == NULL || mask == NULL || protocol == NULL) {
		ERR(policy, "%s", strerror(EINVAL));
		errno = EINVAL;
		return STATUS_ERR;
	}

	*protocol = ocon->protocol;

	if (ocon->protocol == QPOL_IPV4) {
		*mask = &(ocon->ocon->u.node.mask);
	} else {
		*mask = ocon->ocon->u.node6.mask;
	}

	return STATUS_SUCCESS;
}

int qpol_nodecon_get_protocol(const qpol_policy_t * policy, const qpol_nodecon_t * ocon, unsigned char *protocol)
{
	if (protocol != NULL)
		*protocol = 0;

	if (policy == NULL || ocon == NULL || protocol == NULL) {
		ERR(policy, "%s", strerror(EINVAL));
		errno = EINVAL;
		return STATUS_ERR;
	}

	*protocol = ocon->protocol;

	return STATUS_SUCCESS;
}

int qpol_nodecon_get_context(const qpol_policy_t * policy, const qpol_nodecon_t * ocon, const qpol_context_t ** context)
{
	if (context != NULL)
		*context = NULL;

	if (policy == NULL || ocon == NULL || context == NULL) {
		ERR(policy, "%s", strerror(EINVAL));
		errno = EINVAL;
		return STATUS_ERR;
	}

	*context = (qpol_context_t *) & (ocon->ocon->context[0]);

	return STATUS_SUCCESS;
}