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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
/*
* Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved
*
* $Header$
*/
#if !defined(lint) && !defined(__CODECENTER__)
static char *rcsid = "$Header$";
#endif
#include <sys/file.h>
#include <fcntl.h>
#include "policy_db.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define OPENLOCK(db, mode) \
{ \
int olret; \
if (db == NULL) \
return EINVAL; \
else if (db->magic != OSA_ADB_POLICY_DB_MAGIC) \
return OSA_ADB_DBINIT; \
else if ((olret = osa_adb_open_and_lock(db, mode)) != OSA_ADB_OK) \
return olret; \
}
#define CLOSELOCK(db) \
{ \
int cl_ret; \
if ((cl_ret = osa_adb_close_and_unlock(db)) != OSA_ADB_OK) \
return cl_ret; \
}
/*
* Function: osa_adb_create_policy
*
* Purpose: create a policy entry in the policy db.
*
* Arguments:
* entry (input) pointer to the entry to be added
* <return value> OSA_ADB_OK on success, else error code.
*
* Requires:
* entry have a valid name.
*
* Effects:
* creates the entry in the db
*
* Modifies:
* the policy db.
*
*/
krb5_error_code
osa_adb_create_policy(osa_adb_policy_t db, osa_policy_ent_t entry)
{
DBT dbkey;
DBT dbdata;
XDR xdrs;
int ret;
OPENLOCK(db, KRB5_DB_LOCKMODE_EXCLUSIVE);
if(entry->name == NULL) {
ret = EINVAL;
goto error;
}
dbkey.data = entry->name;
dbkey.size = (strlen(entry->name) + 1);
switch(db->db->get(db->db, &dbkey, &dbdata, 0)) {
case 0:
ret = OSA_ADB_DUP;
goto error;
case 1:
break;
default:
ret = errno;
goto error;
}
xdralloc_create(&xdrs, XDR_ENCODE);
if(!xdr_osa_policy_ent_rec(&xdrs, entry)) {
xdr_destroy(&xdrs);
ret = OSA_ADB_XDR_FAILURE;
goto error;
}
dbdata.data = xdralloc_getdata(&xdrs);
dbdata.size = xdr_getpos(&xdrs);
switch(db->db->put(db->db, &dbkey, &dbdata, R_NOOVERWRITE)) {
case 0:
if((db->db->sync(db->db, 0)) == -1)
ret = OSA_ADB_FAILURE;
ret = OSA_ADB_OK;
break;
case 1:
ret = OSA_ADB_DUP;
break;
default:
ret = OSA_ADB_FAILURE;
break;
}
xdr_destroy(&xdrs);
error:
CLOSELOCK(db);
return ret;
}
/*
* Function: osa_adb_destroy_policy
*
* Purpose: destroy a policy entry
*
* Arguments:
* db (input) database handle
* name (input) name of policy
* <return value> OSA_ADB_OK on success, or error code.
*
* Requires:
* db being valid.
* name being non-null.
* Effects:
* deletes policy from db.
*
* Modifies:
* policy db.
*
*/
krb5_error_code
osa_adb_destroy_policy(osa_adb_policy_t db, char *name)
{
DBT dbkey;
int status, ret;
OPENLOCK(db, KRB5_DB_LOCKMODE_EXCLUSIVE);
if(name == NULL) {
ret = EINVAL;
goto error;
}
dbkey.data = name;
dbkey.size = (strlen(name) + 1);
status = db->db->del(db->db, &dbkey, 0);
switch(status) {
case 1:
ret = OSA_ADB_NOENT;
goto error;
case 0:
if ((db->db->sync(db->db, 0)) == -1) {
ret = OSA_ADB_FAILURE;
goto error;
}
ret = OSA_ADB_OK;
break;
default:
ret = OSA_ADB_FAILURE;
goto error;
}
error:
CLOSELOCK(db);
return ret;
}
/*
* Function: osa_adb_get_policy
*
* Purpose: retrieve policy
*
* Arguments:
* db (input) db handle
* name (input) name of policy
* entry (output) policy entry
* cnt (inout) Number of entries
* <return value> 0 on success, error code on failure.
*
* Requires:
* Effects:
* Modifies:
*/
krb5_error_code
osa_adb_get_policy(osa_adb_policy_t db, char *name,
osa_policy_ent_t *entry, int *cnt)
{
DBT dbkey;
DBT dbdata;
XDR xdrs;
int ret;
char *aligned_data;
OPENLOCK(db, KRB5_DB_LOCKMODE_SHARED);
*cnt = 1;
if(name == NULL) {
ret = EINVAL;
goto error;
}
dbkey.data = name;
dbkey.size = (strlen(dbkey.data) + 1);
dbdata.data = NULL;
dbdata.size = 0;
switch((db->db->get(db->db, &dbkey, &dbdata, 0))) {
case 1:
ret = 0;
*cnt = 0;
goto error;
case 0:
break;
default:
ret = OSA_ADB_FAILURE;
goto error;
}
if (!(*(entry) = (osa_policy_ent_t)malloc(sizeof(osa_policy_ent_rec)))) {
ret = ENOMEM;
goto error;
}
if (!(aligned_data = (char *) malloc(dbdata.size))) {
ret = ENOMEM;
goto error;
}
memcpy(aligned_data, dbdata.data, dbdata.size);
memset(*entry, 0, sizeof(osa_policy_ent_rec));
xdrmem_create(&xdrs, aligned_data, dbdata.size, XDR_DECODE);
if (!xdr_osa_policy_ent_rec(&xdrs, *entry))
ret = OSA_ADB_FAILURE;
else ret = OSA_ADB_OK;
xdr_destroy(&xdrs);
free(aligned_data);
error:
CLOSELOCK(db);
return ret;
}
/*
* Function: osa_adb_put_policy
*
* Purpose: update a policy in the dababase
*
* Arguments:
* db (input) db handle
* entry (input) policy entry
* <return value> 0 on success error code on failure.
*
* Requires:
* [requires]
*
* Effects:
* [effects]
*
* Modifies:
* [modifies]
*
*/
krb5_error_code
osa_adb_put_policy(osa_adb_policy_t db, osa_policy_ent_t entry)
{
DBT dbkey;
DBT dbdata;
DBT tmpdb;
XDR xdrs;
int ret;
OPENLOCK(db, KRB5_DB_LOCKMODE_EXCLUSIVE);
if(entry->name == NULL) {
ret = EINVAL;
goto error;
}
dbkey.data = entry->name;
dbkey.size = (strlen(entry->name) + 1);
switch(db->db->get(db->db, &dbkey, &tmpdb, 0)) {
case 0:
break;
case 1:
ret = OSA_ADB_NOENT;
goto error;
default:
ret = OSA_ADB_FAILURE;
goto error;
}
xdralloc_create(&xdrs, XDR_ENCODE);
if(!xdr_osa_policy_ent_rec(&xdrs, entry)) {
xdr_destroy(&xdrs);
ret = OSA_ADB_XDR_FAILURE;
goto error;
}
dbdata.data = xdralloc_getdata(&xdrs);
dbdata.size = xdr_getpos(&xdrs);
switch(db->db->put(db->db, &dbkey, &dbdata, 0)) {
case 0:
if((db->db->sync(db->db, 0)) == -1)
ret = OSA_ADB_FAILURE;
ret = OSA_ADB_OK;
break;
default:
ret = OSA_ADB_FAILURE;
break;
}
xdr_destroy(&xdrs);
error:
CLOSELOCK(db);
return ret;
}
/*
* Function: osa_adb_iter_policy
*
* Purpose: iterate over the policy database.
*
* Arguments:
* db (input) db handle
* func (input) fucntion pointer to call
* data opaque data type
* <return value> 0 on success error code on failure
*
* Requires:
* Effects:
* Modifies:
*/
krb5_error_code
osa_adb_iter_policy(osa_adb_policy_t db, osa_adb_iter_policy_func func,
void *data)
{
DBT dbkey,
dbdata;
XDR xdrs;
int ret;
osa_policy_ent_t entry;
char *aligned_data;
OPENLOCK(db, KRB5_DB_LOCKMODE_EXCLUSIVE); /* hmmm */
if((ret = db->db->seq(db->db, &dbkey, &dbdata, R_FIRST)) == -1) {
ret = errno;
goto error;
}
while (ret == 0) {
if (!(entry = (osa_policy_ent_t) malloc(sizeof(osa_policy_ent_rec)))) {
ret = ENOMEM;
goto error;
}
if(!(aligned_data = (char *) malloc(dbdata.size))) {
ret = ENOMEM;
goto error;
}
memcpy(aligned_data, dbdata.data, dbdata.size);
memset(entry, 0, sizeof(osa_policy_ent_rec));
xdrmem_create(&xdrs, aligned_data, dbdata.size, XDR_DECODE);
if(!xdr_osa_policy_ent_rec(&xdrs, entry)) {
xdr_destroy(&xdrs);
free(aligned_data);
osa_free_policy_ent(entry);
ret = OSA_ADB_FAILURE;
goto error;
}
(*func)(data, entry);
xdr_destroy(&xdrs);
free(aligned_data);
osa_free_policy_ent(entry);
ret = db->db->seq(db->db, &dbkey, &dbdata, R_NEXT);
}
if(ret == -1)
ret = errno;
else ret = OSA_ADB_OK;
error:
CLOSELOCK(db);
return ret;
}
void
osa_free_policy_ent(osa_policy_ent_t val)
{
XDR xdrs;
xdrmem_create(&xdrs, NULL, 0, XDR_FREE);
xdr_osa_policy_ent_rec(&xdrs, val);
free(val);
}
|