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
15 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
21 * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved
26 static char *rcsid
= "$Header$";
30 #include "policy_db.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) \
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) \
49 #define CLOSELOCK(db) \
52 if ((cl_ret = osa_adb_close_and_unlock(db)) != OSA_ADB_OK) \
58 * Function: osa_adb_create_policy
60 * Purpose: create a policy entry in the policy db.
63 * entry (input) pointer to the entry to be added
64 * <return value> OSA_ADB_OK on success, else error code.
67 * entry have a valid name.
70 * creates the entry in the db
77 osa_adb_create_policy(osa_adb_policy_t db
, osa_policy_ent_t entry
)
84 OPENLOCK(db
, KRB5_DB_LOCKMODE_EXCLUSIVE
);
86 if(entry
->name
== NULL
) {
90 dbkey
.data
= entry
->name
;
91 dbkey
.size
= (strlen(entry
->name
) + 1);
93 switch(db
->db
->get(db
->db
, &dbkey
, &dbdata
, 0)) {
103 xdralloc_create(&xdrs
, XDR_ENCODE
);
104 if(!xdr_osa_policy_ent_rec(&xdrs
, entry
)) {
106 ret
= OSA_ADB_XDR_FAILURE
;
109 dbdata
.data
= xdralloc_getdata(&xdrs
);
110 dbdata
.size
= xdr_getpos(&xdrs
);
111 switch(db
->db
->put(db
->db
, &dbkey
, &dbdata
, R_NOOVERWRITE
)) {
113 if((db
->db
->sync(db
->db
, 0)) == -1)
114 ret
= OSA_ADB_FAILURE
;
121 ret
= OSA_ADB_FAILURE
;
132 * Function: osa_adb_destroy_policy
134 * Purpose: destroy a policy entry
137 * db (input) database handle
138 * name (input) name of policy
139 * <return value> OSA_ADB_OK on success, or error code.
143 * name being non-null.
145 * deletes policy from db.
152 osa_adb_destroy_policy(osa_adb_policy_t db
, char *name
)
157 OPENLOCK(db
, KRB5_DB_LOCKMODE_EXCLUSIVE
);
164 dbkey
.size
= (strlen(name
) + 1);
166 status
= db
->db
->del(db
->db
, &dbkey
, 0);
172 if ((db
->db
->sync(db
->db
, 0)) == -1) {
173 ret
= OSA_ADB_FAILURE
;
179 ret
= OSA_ADB_FAILURE
;
189 * Function: osa_adb_get_policy
191 * Purpose: retrieve policy
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.
205 osa_adb_get_policy(osa_adb_policy_t db
, char *name
,
206 osa_policy_ent_t
*entry
, int *cnt
)
214 OPENLOCK(db
, KRB5_DB_LOCKMODE_SHARED
);
223 dbkey
.size
= (strlen(dbkey
.data
) + 1);
226 switch((db
->db
->get(db
->db
, &dbkey
, &dbdata
, 0))) {
234 ret
= OSA_ADB_FAILURE
;
237 if (!(*(entry
) = (osa_policy_ent_t
)malloc(sizeof(osa_policy_ent_rec
)))) {
241 if (!(aligned_data
= (char *) malloc(dbdata
.size
))) {
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
;
260 * Function: osa_adb_put_policy
262 * Purpose: update a policy in the dababase
265 * db (input) db handle
266 * entry (input) policy entry
267 * <return value> 0 on success error code on failure.
280 osa_adb_put_policy(osa_adb_policy_t db
, osa_policy_ent_t entry
)
288 OPENLOCK(db
, KRB5_DB_LOCKMODE_EXCLUSIVE
);
290 if(entry
->name
== NULL
) {
294 dbkey
.data
= entry
->name
;
295 dbkey
.size
= (strlen(entry
->name
) + 1);
296 switch(db
->db
->get(db
->db
, &dbkey
, &tmpdb
, 0)) {
303 ret
= OSA_ADB_FAILURE
;
306 xdralloc_create(&xdrs
, XDR_ENCODE
);
307 if(!xdr_osa_policy_ent_rec(&xdrs
, entry
)) {
309 ret
= OSA_ADB_XDR_FAILURE
;
312 dbdata
.data
= xdralloc_getdata(&xdrs
);
313 dbdata
.size
= xdr_getpos(&xdrs
);
314 switch(db
->db
->put(db
->db
, &dbkey
, &dbdata
, 0)) {
316 if((db
->db
->sync(db
->db
, 0)) == -1)
317 ret
= OSA_ADB_FAILURE
;
321 ret
= OSA_ADB_FAILURE
;
332 * Function: osa_adb_iter_policy
334 * Purpose: iterate over the policy database.
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
347 osa_adb_iter_policy(osa_adb_policy_t db
, osa_adb_iter_policy_func func
,
354 osa_policy_ent_t entry
;
357 OPENLOCK(db
, KRB5_DB_LOCKMODE_EXCLUSIVE
); /* hmmm */
359 if((ret
= db
->db
->seq(db
->db
, &dbkey
, &dbdata
, R_FIRST
)) == -1) {
365 if (!(entry
= (osa_policy_ent_t
) malloc(sizeof(osa_policy_ent_rec
)))) {
370 if(!(aligned_data
= (char *) malloc(dbdata
.size
))) {
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
)) {
381 ret
= OSA_ADB_FAILURE
;
384 (*func
)(data
, entry
);
387 osa_free_policy_ent(entry
);
388 ret
= db
->db
->seq(db
->db
, &dbkey
, &dbdata
, R_NEXT
);
392 else ret
= OSA_ADB_OK
;
400 osa_free_policy_ent(osa_policy_ent_t val
)
404 xdrmem_create(&xdrs
, NULL
, 0, XDR_FREE
);
406 xdr_osa_policy_ent_rec(&xdrs
, val
);