dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / krb5 / plugins / kdb / db2 / adb_policy.c
blobf6452dbe2b99386cb8774a8bc8c5ad2543a12762
2 /*
3 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
5 * Openvision retains the copyright to derivative works of
6 * this source code. Do *NOT* create a derivative of this
7 * source code before consulting with your legal department.
8 * Do *NOT* integrate *ANY* of this source code into another
9 * product before consulting with your legal department.
11 * For further information, read the top-level Openvision
12 * copyright which is contained in the top-level MIT Kerberos
13 * copyright.
15 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
21 * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved
23 * $Header$
26 static char *rcsid = "$Header$";
28 #include <sys/file.h>
29 #include <fcntl.h>
30 #include "policy_db.h"
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
35 extern caddr_t xdralloc_getdata(XDR *xdrs);
36 extern void xdralloc_create(XDR *xdrs, enum xdr_op op);
38 #define OPENLOCK(db, mode) \
39 { \
40 int olret; \
41 if (db == NULL) \
42 return EINVAL; \
43 else if (db->magic != OSA_ADB_POLICY_DB_MAGIC) \
44 return OSA_ADB_DBINIT; \
45 else if ((olret = osa_adb_open_and_lock(db, mode)) != OSA_ADB_OK) \
46 return olret; \
49 #define CLOSELOCK(db) \
50 { \
51 int cl_ret; \
52 if ((cl_ret = osa_adb_close_and_unlock(db)) != OSA_ADB_OK) \
53 return cl_ret; \
58 * Function: osa_adb_create_policy
60 * Purpose: create a policy entry in the policy db.
62 * Arguments:
63 * entry (input) pointer to the entry to be added
64 * <return value> OSA_ADB_OK on success, else error code.
66 * Requires:
67 * entry have a valid name.
69 * Effects:
70 * creates the entry in the db
72 * Modifies:
73 * the policy db.
76 krb5_error_code
77 osa_adb_create_policy(osa_adb_policy_t db, osa_policy_ent_t entry)
79 DBT dbkey;
80 DBT dbdata;
81 XDR xdrs;
82 int ret;
84 OPENLOCK(db, KRB5_DB_LOCKMODE_EXCLUSIVE);
86 if(entry->name == NULL) {
87 ret = EINVAL;
88 goto error;
90 dbkey.data = entry->name;
91 dbkey.size = (strlen(entry->name) + 1);
93 switch(db->db->get(db->db, &dbkey, &dbdata, 0)) {
94 case 0:
95 ret = OSA_ADB_DUP;
96 goto error;
97 case 1:
98 break;
99 default:
100 ret = errno;
101 goto error;
103 xdralloc_create(&xdrs, XDR_ENCODE);
104 if(!xdr_osa_policy_ent_rec(&xdrs, entry)) {
105 xdr_destroy(&xdrs);
106 ret = OSA_ADB_XDR_FAILURE;
107 goto error;
109 dbdata.data = xdralloc_getdata(&xdrs);
110 dbdata.size = xdr_getpos(&xdrs);
111 switch(db->db->put(db->db, &dbkey, &dbdata, R_NOOVERWRITE)) {
112 case 0:
113 if((db->db->sync(db->db, 0)) == -1)
114 ret = OSA_ADB_FAILURE;
115 ret = OSA_ADB_OK;
116 break;
117 case 1:
118 ret = OSA_ADB_DUP;
119 break;
120 default:
121 ret = OSA_ADB_FAILURE;
122 break;
124 xdr_destroy(&xdrs);
126 error:
127 CLOSELOCK(db);
128 return ret;
132 * Function: osa_adb_destroy_policy
134 * Purpose: destroy a policy entry
136 * Arguments:
137 * db (input) database handle
138 * name (input) name of policy
139 * <return value> OSA_ADB_OK on success, or error code.
141 * Requires:
142 * db being valid.
143 * name being non-null.
144 * Effects:
145 * deletes policy from db.
147 * Modifies:
148 * policy db.
151 krb5_error_code
152 osa_adb_destroy_policy(osa_adb_policy_t db, char *name)
154 DBT dbkey;
155 int status, ret;
157 OPENLOCK(db, KRB5_DB_LOCKMODE_EXCLUSIVE);
159 if(name == NULL) {
160 ret = EINVAL;
161 goto error;
163 dbkey.data = name;
164 dbkey.size = (strlen(name) + 1);
166 status = db->db->del(db->db, &dbkey, 0);
167 switch(status) {
168 case 1:
169 ret = OSA_ADB_NOENT;
170 goto error;
171 case 0:
172 if ((db->db->sync(db->db, 0)) == -1) {
173 ret = OSA_ADB_FAILURE;
174 goto error;
176 ret = OSA_ADB_OK;
177 break;
178 default:
179 ret = OSA_ADB_FAILURE;
180 goto error;
183 error:
184 CLOSELOCK(db);
185 return ret;
189 * Function: osa_adb_get_policy
191 * Purpose: retrieve policy
193 * Arguments:
194 * db (input) db handle
195 * name (input) name of policy
196 * entry (output) policy entry
197 * cnt (inout) Number of entries
198 * <return value> 0 on success, error code on failure.
200 * Requires:
201 * Effects:
202 * Modifies:
204 krb5_error_code
205 osa_adb_get_policy(osa_adb_policy_t db, char *name,
206 osa_policy_ent_t *entry, int *cnt)
208 DBT dbkey;
209 DBT dbdata;
210 XDR xdrs;
211 int ret;
212 char *aligned_data;
214 OPENLOCK(db, KRB5_DB_LOCKMODE_SHARED);
216 *cnt = 1;
218 if(name == NULL) {
219 ret = EINVAL;
220 goto error;
222 dbkey.data = name;
223 dbkey.size = (strlen(dbkey.data) + 1);
224 dbdata.data = NULL;
225 dbdata.size = 0;
226 switch((db->db->get(db->db, &dbkey, &dbdata, 0))) {
227 case 1:
228 ret = OSA_ADB_OK;
229 *cnt = 0;
230 goto error;
231 case 0:
232 break;
233 default:
234 ret = OSA_ADB_FAILURE;
235 goto error;
237 if (!(*(entry) = (osa_policy_ent_t)malloc(sizeof(osa_policy_ent_rec)))) {
238 ret = ENOMEM;
239 goto error;
241 if (!(aligned_data = (char *) malloc(dbdata.size))) {
242 ret = ENOMEM;
243 goto error;
245 memcpy(aligned_data, dbdata.data, dbdata.size);
246 memset(*entry, 0, sizeof(osa_policy_ent_rec));
247 xdrmem_create(&xdrs, aligned_data, dbdata.size, XDR_DECODE);
248 if (!xdr_osa_policy_ent_rec(&xdrs, *entry))
249 ret = OSA_ADB_FAILURE;
250 else ret = OSA_ADB_OK;
251 xdr_destroy(&xdrs);
252 free(aligned_data);
254 error:
255 CLOSELOCK(db);
256 return ret;
260 * Function: osa_adb_put_policy
262 * Purpose: update a policy in the dababase
264 * Arguments:
265 * db (input) db handle
266 * entry (input) policy entry
267 * <return value> 0 on success error code on failure.
269 * Requires:
270 * [requires]
272 * Effects:
273 * [effects]
275 * Modifies:
276 * [modifies]
279 krb5_error_code
280 osa_adb_put_policy(osa_adb_policy_t db, osa_policy_ent_t entry)
282 DBT dbkey;
283 DBT dbdata;
284 DBT tmpdb;
285 XDR xdrs;
286 int ret;
288 OPENLOCK(db, KRB5_DB_LOCKMODE_EXCLUSIVE);
290 if(entry->name == NULL) {
291 ret = EINVAL;
292 goto error;
294 dbkey.data = entry->name;
295 dbkey.size = (strlen(entry->name) + 1);
296 switch(db->db->get(db->db, &dbkey, &tmpdb, 0)) {
297 case 0:
298 break;
299 case 1:
300 ret = OSA_ADB_NOENT;
301 goto error;
302 default:
303 ret = OSA_ADB_FAILURE;
304 goto error;
306 xdralloc_create(&xdrs, XDR_ENCODE);
307 if(!xdr_osa_policy_ent_rec(&xdrs, entry)) {
308 xdr_destroy(&xdrs);
309 ret = OSA_ADB_XDR_FAILURE;
310 goto error;
312 dbdata.data = xdralloc_getdata(&xdrs);
313 dbdata.size = xdr_getpos(&xdrs);
314 switch(db->db->put(db->db, &dbkey, &dbdata, 0)) {
315 case 0:
316 if((db->db->sync(db->db, 0)) == -1)
317 ret = OSA_ADB_FAILURE;
318 ret = OSA_ADB_OK;
319 break;
320 default:
321 ret = OSA_ADB_FAILURE;
322 break;
324 xdr_destroy(&xdrs);
326 error:
327 CLOSELOCK(db);
328 return ret;
332 * Function: osa_adb_iter_policy
334 * Purpose: iterate over the policy database.
336 * Arguments:
337 * db (input) db handle
338 * func (input) fucntion pointer to call
339 * data opaque data type
340 * <return value> 0 on success error code on failure
342 * Requires:
343 * Effects:
344 * Modifies:
346 krb5_error_code
347 osa_adb_iter_policy(osa_adb_policy_t db, osa_adb_iter_policy_func func,
348 void *data)
350 DBT dbkey,
351 dbdata;
352 XDR xdrs;
353 int ret;
354 osa_policy_ent_t entry;
355 char *aligned_data;
357 OPENLOCK(db, KRB5_DB_LOCKMODE_EXCLUSIVE); /* hmmm */
359 if((ret = db->db->seq(db->db, &dbkey, &dbdata, R_FIRST)) == -1) {
360 ret = errno;
361 goto error;
364 while (ret == 0) {
365 if (!(entry = (osa_policy_ent_t) malloc(sizeof(osa_policy_ent_rec)))) {
366 ret = ENOMEM;
367 goto error;
370 if(!(aligned_data = (char *) malloc(dbdata.size))) {
371 ret = ENOMEM;
372 goto error;
374 memcpy(aligned_data, dbdata.data, dbdata.size);
376 memset(entry, 0, sizeof(osa_policy_ent_rec));
377 xdrmem_create(&xdrs, aligned_data, dbdata.size, XDR_DECODE);
378 if(!xdr_osa_policy_ent_rec(&xdrs, entry)) {
379 xdr_destroy(&xdrs);
380 free(aligned_data);
381 ret = OSA_ADB_FAILURE;
382 goto error;
384 (*func)(data, entry);
385 xdr_destroy(&xdrs);
386 free(aligned_data);
387 osa_free_policy_ent(entry);
388 ret = db->db->seq(db->db, &dbkey, &dbdata, R_NEXT);
390 if(ret == -1)
391 ret = errno;
392 else ret = OSA_ADB_OK;
394 error:
395 CLOSELOCK(db);
396 return ret;
399 void
400 osa_free_policy_ent(osa_policy_ent_t val)
402 XDR xdrs;
404 xdrmem_create(&xdrs, NULL, 0, XDR_FREE);
406 xdr_osa_policy_ent_rec(&xdrs, val);
408 free(val);