summaryrefslogtreecommitdiffstats
path: root/libqpol/src/mlsrule_query.c
blob: cc7e7a89addbd64317fb329407809cc38d8ae431 (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
/**
 *  @file
 *  Implementation for the public interface for searching and iterating over
 *  range transition rules.
 *
 *  @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 "iterator_internal.h"
#include <qpol/iterator.h>
#include <qpol/policy.h>
#include <qpol/mlsrule_query.h>
#include <sepol/policydb/policydb.h>
#include <sepol/policydb/avtab.h>
#include <sepol/policydb/util.h>
#include <stdlib.h>
#include "qpol_internal.h"

typedef struct range_trans_state
{
	range_trans_t *head;
	range_trans_t *cur;
} range_trans_state_t;

static int range_trans_state_end(const qpol_iterator_t * iter)
{
	range_trans_state_t *rs = NULL;

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

	return rs->cur ? 0 : 1;
}

static void *range_trans_state_get_cur(const qpol_iterator_t * iter)
{
	range_trans_state_t *rs = NULL;

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

	return rs->cur;
}

static int range_trans_state_next(qpol_iterator_t * iter)
{
	range_trans_state_t *rs = NULL;

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

	if (range_trans_state_end(iter)) {
		errno = EINVAL;
		return STATUS_ERR;
	}

	rs->cur = rs->cur->next;

	return STATUS_SUCCESS;
}

static size_t range_trans_state_size(const qpol_iterator_t * iter)
{
	range_trans_state_t *rs = NULL;
	size_t count = 0;
	range_trans_t *tmp = NULL;

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

	for (tmp = rs->head; tmp; tmp = tmp->next)
		count++;

	return count;
}

int qpol_policy_get_range_trans_iter(const qpol_policy_t * policy, qpol_iterator_t ** iter)
{
	policydb_t *db = NULL;
	range_trans_state_t *rs = NULL;
	int error = 0;

	if (iter)
		*iter = NULL;

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

	db = &policy->p->p;

	rs = calloc(1, sizeof(range_trans_state_t));
	if (!rs) {
		error = errno;
		ERR(policy, "%s", strerror(error));
		errno = error;
		return STATUS_ERR;
	}

	if (qpol_iterator_create(policy, (void *)rs, range_trans_state_get_cur,
				 range_trans_state_next, range_trans_state_end, range_trans_state_size, free, iter)) {
		error = errno;
		free(rs);
		errno = error;
		return STATUS_ERR;
	}

	rs->head = rs->cur = db->range_tr;
	return STATUS_SUCCESS;
}

int qpol_range_trans_get_source_type(const qpol_policy_t * policy, const qpol_range_trans_t * rule, const qpol_type_t ** source)
{
	policydb_t *db = NULL;
	range_trans_t *rt = NULL;

	if (source) {
		*source = NULL;
	}

	if (!policy || !rule || !source) {
		errno = EINVAL;
		ERR(policy, "%s", strerror(EINVAL));
		return STATUS_ERR;
	}

	db = &policy->p->p;
	rt = (range_trans_t *) rule;

	*source = (qpol_type_t *) db->type_val_to_struct[rt->source_type - 1];

	return STATUS_SUCCESS;
}

int qpol_range_trans_get_target_type(const qpol_policy_t * policy, const qpol_range_trans_t * rule, const qpol_type_t ** target)
{
	policydb_t *db = NULL;
	range_trans_t *rt = NULL;

	if (target) {
		*target = NULL;
	}

	if (!policy || !rule || !target) {
		ERR(policy, "%s", strerror(EINVAL));
		errno = EINVAL;
		return STATUS_ERR;
	}

	db = &policy->p->p;
	rt = (range_trans_t *) rule;

	*target = (qpol_type_t *) db->type_val_to_struct[rt->target_type - 1];

	return STATUS_SUCCESS;
}

int qpol_range_trans_get_target_class(const qpol_policy_t * policy, const qpol_range_trans_t * rule, const qpol_class_t ** target)
{
	policydb_t *db = NULL;
	range_trans_t *rt = NULL;

	if (target) {
		*target = NULL;
	}

	if (!policy || !rule || !target) {
		ERR(policy, "%s", strerror(EINVAL));
		errno = EINVAL;
		return STATUS_ERR;
	}

	db = &policy->p->p;
	rt = (range_trans_t *) rule;

	*target = (qpol_class_t *) db->class_val_to_struct[rt->target_class - 1];

	return STATUS_SUCCESS;
}

int qpol_range_trans_get_range(const qpol_policy_t * policy, const qpol_range_trans_t * rule, const qpol_mls_range_t ** range)
{
	policydb_t *db = NULL;
	range_trans_t *rt = NULL;

	if (range) {
		*range = NULL;
	}

	if (!policy || !rule || !range) {
		ERR(policy, "%s", strerror(EINVAL));
		errno = EINVAL;
		return STATUS_ERR;
	}

	db = &policy->p->p;
	rt = (range_trans_t *) rule;

	*range = (qpol_mls_range_t *) & rt->target_range;

	return STATUS_SUCCESS;
}