File: isid-query.c

package info (click to toggle)
setools 3.3.7-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 9,840 kB
  • sloc: ansic: 82,247; tcl: 13,145; cpp: 4,885; makefile: 1,603; yacc: 779; lex: 296; python: 57; sh: 50
file content (123 lines) | stat: -rw-r--r-- 3,435 bytes parent folder | download | duplicates (4)
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
/**
 * @file
 *
 * Provides a way for setools to make queries about initial SIDs
 * within a policy.  The caller obtains a query object, fills in its
 * parameters, and then runs the query; it obtains a vector of
 * results.  Searches are conjunctive -- all fields of the search
 * query must match for a datum to be added to the results query.
 *
 * @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 "policy-query-internal.h"

#include <errno.h>

struct apol_isid_query
{
	char *name;
	apol_context_t *context;
	unsigned int flags;
};

/******************** genfscon queries ********************/

int apol_isid_get_by_query(const apol_policy_t * p, const apol_isid_query_t * i, apol_vector_t ** v)
{
	qpol_iterator_t *iter;
	int retval = -1, retval2;
	const qpol_isid_t *isid = NULL;
	*v = NULL;
	if (qpol_policy_get_isid_iter(p->p, &iter) < 0) {
		return -1;
	}
	if ((*v = apol_vector_create(NULL)) == NULL) {
		ERR(p, "%s", strerror(errno));
		goto cleanup;
	}
	for (; !qpol_iterator_end(iter); qpol_iterator_next(iter)) {
		if (qpol_iterator_get_item(iter, (void **)&isid) < 0) {
			goto cleanup;
		}
		if (i != NULL) {
			const char *name;
			const qpol_context_t *context;
			if (qpol_isid_get_name(p->p, isid, &name) < 0 || qpol_isid_get_context(p->p, isid, &context) < 0) {
				goto cleanup;
			}
			retval2 = apol_compare(p, name, i->name, 0, NULL);
			if (retval2 < 0) {
				goto cleanup;
			} else if (retval2 == 0) {
				continue;
			}
			retval2 = apol_compare_context(p, context, i->context, i->flags);
			if (retval2 < 0) {
				goto cleanup;
			} else if (retval2 == 0) {
				continue;
			}
		}
		if (apol_vector_append(*v, (void *)isid)) {
			ERR(p, "%s", strerror(ENOMEM));
			goto cleanup;
		}
	}

	retval = 0;
      cleanup:
	if (retval != 0) {
		apol_vector_destroy(v);
	}
	qpol_iterator_destroy(&iter);
	return retval;
}

apol_isid_query_t *apol_isid_query_create(void)
{
	return calloc(1, sizeof(apol_isid_query_t));
}

void apol_isid_query_destroy(apol_isid_query_t ** i)
{
	if (*i != NULL) {
		free((*i)->name);
		apol_context_destroy(&((*i)->context));
		free(*i);
		*i = NULL;
	}
}

int apol_isid_query_set_name(const apol_policy_t * p, apol_isid_query_t * i, const char *name)
{
	return apol_query_set(p, &i->name, NULL, name);
}

int apol_isid_query_set_context(const apol_policy_t * p __attribute__ ((unused)),
				apol_isid_query_t * i, apol_context_t * context, unsigned int range_match)
{
	if (i->context != NULL) {
		apol_context_destroy(&i->context);
	}
	i->context = context;
	i->flags = (i->flags & ~APOL_QUERY_FLAGS) | range_match;
	return 0;
}