aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/audit/src/ausearch-avc.c
blob: 2d3b3199cf3157524f63a7b4a795e938180c04c7 (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
/*
* ausearch-avc.c - Minimal linked list library for avcs
* Copyright (c) 2006,2008,2014 Red Hat Inc., Durham, North Carolina.
* All Rights Reserved. 
*
* This software may be freely redistributed and/or modified under the
* terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2, or (at your option) any
* later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Authors:
*   Steve Grubb <sgrubb@redhat.com>
*/

#include "config.h"
#include <stdlib.h>
#include <string.h>
#include "ausearch-avc.h"


void alist_create(alist *l)
{
	l->head = NULL;
	l->cur = NULL;
	l->cnt = 0;
}

anode *alist_next(alist *l)
{
	if (l->cur == NULL)
		return NULL;
	l->cur = l->cur->next;
	return l->cur;
}

static void alist_last(alist *l)
{
	register anode* cur;

	if (l->head == NULL)
		return;

	// Start with cur in hopes that we don't start at beginning
	if (l->cur)
		cur = l->cur;
	else
		cur = l->head;

	// Loop until no next value
	while (cur->next)
		cur = cur->next;
	l->cur = cur;
}

void alist_append(alist *l, anode *node)
{
	anode* newnode;

	newnode = malloc(sizeof(anode));

	if (node->scontext)
		newnode->scontext = node->scontext;
	else
		newnode->scontext = NULL;

	if (node->tcontext)
		newnode->tcontext = node->tcontext;
	else
		newnode->tcontext = NULL;

	newnode->avc_result = node->avc_result;

	if (node->avc_perm)
		newnode->avc_perm = node->avc_perm;
	else
		newnode->avc_perm = NULL;

	if (node->avc_class)
		newnode->avc_class = node->avc_class;
	else
		newnode->avc_class = NULL;

	newnode->next = NULL;

	// Make sure cursor is at the end
	alist_last(l);

	// if we are at top, fix this up
	if (l->head == NULL)
		l->head = newnode;
	else	// Otherwise add pointer to newnode
		l->cur->next = newnode;

	// make newnode current
	l->cur = newnode;
	l->cnt++;
}

int alist_find_subj(alist *l)
{
        register anode* window = l->head;

	while (window) {
		if (window->scontext) {
			l->cur = window;
			return 1;
		}
		else
			window = window->next;
	}
	return 0;
}

anode *alist_next_subj(alist *l)
{
	if (l->cur == NULL)
		return NULL;
	while (l->cur->next) {
		l->cur=l->cur->next;
		if (l->cur->scontext)
			return l->cur;
	}
	return NULL;
}

int alist_find_obj(alist *l)
{
        register anode* window = l->head;

	while (window) {
		if (window->tcontext) {
			l->cur = window;
			return 1;
		}
		else
			window = window->next;
	}
	return 0;
}

anode *alist_next_obj(alist *l)
{
	if (l->cur == NULL)
		return NULL;
	while (l->cur->next) {
		l->cur=l->cur->next;
		if (l->cur->tcontext)
			return l->cur;
	}
	return NULL;
}

int alist_find_avc(alist *l)
{
        register anode* window = l->head;

	while (window) {
		if (window->avc_result != AVC_UNSET) {
			l->cur = window;
			return 1;
		}
		else
			window = window->next;
	}
	return 0;
}

anode *alist_next_avc(alist *l)
{
	if (l->cur == NULL)
		return NULL;
	while (l->cur->next) {
		l->cur=l->cur->next;
		if (l->cur->avc_result != AVC_UNSET)
			return l->cur;
	}
	return NULL;
}

void alist_clear(alist* l)
{
	anode* nextnode;
	register anode* current;

	current = l->head;
	while (current) {
		nextnode=current->next;
		anode_clear(current);
		free(current);
		current=nextnode;
	}
	l->head = NULL;
	l->cur = NULL;
	l->cnt = 0;
}

void anode_init(anode *an)
{
	an->scontext = NULL;
	an->tcontext = NULL;
	an->avc_result = AVC_UNSET;
	an->avc_perm = NULL;
	an->avc_class = NULL;
}

void anode_clear(anode *an)
{
	free(an->scontext);
	free(an->tcontext);
	free(an->avc_perm);
	free(an->avc_class);
}