File: sidtab.c

package info (click to toggle)
oskit 0.97.20000202-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 58,008 kB
  • ctags: 172,612
  • sloc: ansic: 832,827; asm: 7,640; sh: 3,920; yacc: 3,664; perl: 1,457; lex: 427; makefile: 337; csh: 141; awk: 78
file content (265 lines) | stat: -rw-r--r-- 5,158 bytes parent folder | download
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
/*
 * Copyright (c) 1999 The University of Utah and the Flux Group.
 * All rights reserved.
 * 
 * Contributed by the Computer Security Research division,
 * INFOSEC Research and Technology Office, NSA.
 * 
 * This file is part of the Flux OSKit.  The OSKit is free software, also known
 * as "open source;" you can redistribute it and/or modify it under the terms
 * of the GNU General Public License (GPL), version 2, as published by the Free
 * Software Foundation (FSF).  To explore alternate licensing terms, contact
 * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
 * 
 * The OSKit 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 GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */
/* FLASK */

#include "sidtab.h"
#include "extension.h"
#include "services.h"

#define SIDTAB_HASH(key) \
(key % SIDTAB_SIZE)

#define SIDTAB_EQ(key1,key2) \
(key1 == key2)

static void 
sid_datum_destroy(sid_datum_t *datum)
{
	hashtab_map(datum->children.table, invalidate_child, 0);
	hashtab_destroy(datum->children.table);
	datum->children.table = 0;
	if (datum->child_val_to_name) {
		FREE(datum->child_val_to_name,
		     (datum->children.nprim / CVTN_NPRIM_INCR + 1) * CVTN_NPRIM_INCR * sizeof(char *));
	}
	context_destroy(&datum->context);
}


int 
sidtab_insert(sidtab_t * h, security_id_t key, sid_datum_t * datum)
{
	int             hvalue;
	sidtab_ptr_t     cur, newnode;


	if (!h)
		return -ENOMEM;

	hvalue = SIDTAB_HASH(key);
	cur = h->htable[hvalue];
	while (cur != NULL && !SIDTAB_EQ(cur->key, key))
		cur = cur->next;

	if (cur != NULL)
		return -EEXIST;

	newnode = (sidtab_ptr_t) malloc(sizeof(struct sidtab_node_t));
	if (newnode == NULL)
		return -ENOMEM;
	newnode->key = key;
	newnode->datum = *datum;
	newnode->next = h->htable[hvalue];
	h->htable[hvalue] = newnode;

	h->nel++;
	return 0;
}


int 
sidtab_remove(sidtab_t *h, security_id_t key)
{
	int             hvalue;
	sidtab_ptr_t   cur, last;


	if (!h)
		return -ENOENT;

	hvalue = SIDTAB_HASH(key);
	last = NULL;
	cur = h->htable[hvalue];
	while (cur != NULL && !SIDTAB_EQ(cur->key, key)) {
		last = cur;
		cur = cur->next;
	}

	if (cur == NULL)
		return -ENOENT;

	if (last == NULL)
		h->htable[hvalue] = cur->next;
	else
		last->next = cur->next;

	sid_datum_destroy(&cur->datum);

	free(cur);
	return 0;
}


sid_datum_t  *
sidtab_search(sidtab_t * h, security_id_t key)
{
	int             hvalue;
	sidtab_ptr_t     cur;


	if (!h)
		return NULL;

	hvalue = SIDTAB_HASH(key);
	cur = h->htable[hvalue];
	while (cur != NULL && !SIDTAB_EQ(cur->key, key))
		cur = cur->next;

	if (cur == NULL)
		return NULL;

	return &cur->datum;
}


int 
sidtab_map(sidtab_t * h,
	     int (*apply) (security_id_t key,
			sid_datum_t * d,
			void *args),
	  void *args)
{
	int             i, ret;
	sidtab_ptr_t     cur;


	if (!h)
		return 0;

	for (i = 0; i < SIDTAB_SIZE; i++) {
		cur = h->htable[i];
		while (cur != NULL) {
			ret = apply(cur->key, &cur->datum, args);
			if (ret)
				return ret;
			cur = cur->next;
		}
	}
	return 0;
}


void 
sidtab_map_remove_on_error(sidtab_t *h,
			   int (*apply) (security_id_t sid,
					 sid_datum_t *d,
					 void *args),
			   void *args)
{
	int             i, ret;
	sidtab_ptr_t   last, cur, temp;


	if (!h)
		return;

	for (i = 0; i < SIDTAB_SIZE; i++) {
		last = NULL;
		cur = h->htable[i];
		while (cur != NULL) {
			ret = apply(cur->key, &cur->datum, args);
			if (ret) {
				if (last) {
					last->next = cur->next;
				} else {
					h->htable[i] = cur->next;
				}

				temp = cur;
				cur = cur->next;
				sid_datum_destroy(&temp->datum);
				free(temp);
			} else {
				last = cur;
				cur = cur->next;
			}
		}
	}

	return;
}


int 
sidtab_init(sidtab_t * h)
{
	int             i;


	for (i = 0; i < SIDTAB_SIZE; i++)
		h->htable[i] = (sidtab_ptr_t) NULL;
	h->nel = 0;
	return 0;
}


int 
sidtab_context_to_sid(sidtab_t * s,
		      ss_context_t * context,
		      security_id_t * out_sid)
{
	security_id_t	key;
	sid_datum_t	*datum;
	sidtab_ptr_t     cur;
	int             i, ret;


	*out_sid = SECSID_NULL;

	/* check to see if there is already a sid for this context */
	for (i = 0; i < SIDTAB_SIZE; i++) {
		cur = s->htable[i];
		while (cur != NULL) {
			if (context_cmp(&cur->datum.context, context)) {
				*out_sid = cur->key;
				return 0;
			}
			cur = cur->next;
		}
	}

	/* no sid exists; need to create a new entry in the sid table */
	key = s->nel + 1;
	if (key == 0)
		return -1;
	datum = (sid_datum_t *) MALLOC(sizeof(sid_datum_t));
	if (!datum) {
		return -1;
	}
	if (!context_cpy(&datum->context, context)) {
		FREE(datum, sizeof(sid_datum_t));
		return -1;
	}
	datum->children.table = 0;
	datum->children.nprim = 0;
	datum->child_val_to_name = 0;

	ret = sidtab_insert(s, key, datum);
	if (ret) {
		context_destroy(&datum->context);
		FREE(datum, sizeof(sid_datum_t));
		return -1;
	}
 
	*out_sid = key;
	return 0;
}

/* FLASK */