1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /*
3*7f2fe78bSCy Schubert * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved
4*7f2fe78bSCy Schubert *
5*7f2fe78bSCy Schubert * $Header$
6*7f2fe78bSCy Schubert */
7*7f2fe78bSCy Schubert
8*7f2fe78bSCy Schubert #include "k5-int.h"
9*7f2fe78bSCy Schubert
10*7f2fe78bSCy Schubert #include <sys/file.h>
11*7f2fe78bSCy Schubert #include "policy_db.h"
12*7f2fe78bSCy Schubert
13*7f2fe78bSCy Schubert #define OPENLOCK(db, mode) \
14*7f2fe78bSCy Schubert { \
15*7f2fe78bSCy Schubert int olret; \
16*7f2fe78bSCy Schubert if (db == NULL) \
17*7f2fe78bSCy Schubert return EINVAL; \
18*7f2fe78bSCy Schubert else if (db->magic != OSA_ADB_POLICY_DB_MAGIC) \
19*7f2fe78bSCy Schubert return OSA_ADB_DBINIT; \
20*7f2fe78bSCy Schubert else if ((olret = osa_adb_open_and_lock(db, mode)) != OSA_ADB_OK) \
21*7f2fe78bSCy Schubert return olret; \
22*7f2fe78bSCy Schubert }
23*7f2fe78bSCy Schubert
24*7f2fe78bSCy Schubert #define CLOSELOCK(db) \
25*7f2fe78bSCy Schubert { \
26*7f2fe78bSCy Schubert int cl_ret; \
27*7f2fe78bSCy Schubert if ((cl_ret = osa_adb_close_and_unlock(db)) != OSA_ADB_OK) \
28*7f2fe78bSCy Schubert return cl_ret; \
29*7f2fe78bSCy Schubert }
30*7f2fe78bSCy Schubert
31*7f2fe78bSCy Schubert
32*7f2fe78bSCy Schubert /*
33*7f2fe78bSCy Schubert * Function: osa_adb_create_policy
34*7f2fe78bSCy Schubert *
35*7f2fe78bSCy Schubert * Purpose: create a policy entry in the policy db.
36*7f2fe78bSCy Schubert *
37*7f2fe78bSCy Schubert * Arguments:
38*7f2fe78bSCy Schubert * entry (input) pointer to the entry to be added
39*7f2fe78bSCy Schubert * <return value> OSA_ADB_OK on success, else error code.
40*7f2fe78bSCy Schubert *
41*7f2fe78bSCy Schubert * Requires:
42*7f2fe78bSCy Schubert * entry have a valid name.
43*7f2fe78bSCy Schubert *
44*7f2fe78bSCy Schubert * Effects:
45*7f2fe78bSCy Schubert * creates the entry in the db
46*7f2fe78bSCy Schubert *
47*7f2fe78bSCy Schubert * Modifies:
48*7f2fe78bSCy Schubert * the policy db.
49*7f2fe78bSCy Schubert *
50*7f2fe78bSCy Schubert */
51*7f2fe78bSCy Schubert krb5_error_code
osa_adb_create_policy(osa_adb_policy_t db,osa_policy_ent_t entry)52*7f2fe78bSCy Schubert osa_adb_create_policy(osa_adb_policy_t db, osa_policy_ent_t entry)
53*7f2fe78bSCy Schubert {
54*7f2fe78bSCy Schubert DBT dbkey;
55*7f2fe78bSCy Schubert DBT dbdata;
56*7f2fe78bSCy Schubert XDR xdrs;
57*7f2fe78bSCy Schubert int ret;
58*7f2fe78bSCy Schubert
59*7f2fe78bSCy Schubert OPENLOCK(db, KRB5_DB_LOCKMODE_EXCLUSIVE);
60*7f2fe78bSCy Schubert
61*7f2fe78bSCy Schubert if(entry->name == NULL) {
62*7f2fe78bSCy Schubert ret = EINVAL;
63*7f2fe78bSCy Schubert goto error;
64*7f2fe78bSCy Schubert }
65*7f2fe78bSCy Schubert dbkey.data = entry->name;
66*7f2fe78bSCy Schubert dbkey.size = (strlen(entry->name) + 1);
67*7f2fe78bSCy Schubert
68*7f2fe78bSCy Schubert switch(db->db->get(db->db, &dbkey, &dbdata, 0)) {
69*7f2fe78bSCy Schubert case 0:
70*7f2fe78bSCy Schubert ret = OSA_ADB_DUP;
71*7f2fe78bSCy Schubert goto error;
72*7f2fe78bSCy Schubert case 1:
73*7f2fe78bSCy Schubert break;
74*7f2fe78bSCy Schubert default:
75*7f2fe78bSCy Schubert ret = errno;
76*7f2fe78bSCy Schubert goto error;
77*7f2fe78bSCy Schubert }
78*7f2fe78bSCy Schubert xdralloc_create(&xdrs, XDR_ENCODE);
79*7f2fe78bSCy Schubert if(!xdr_osa_policy_ent_rec(&xdrs, entry)) {
80*7f2fe78bSCy Schubert xdr_destroy(&xdrs);
81*7f2fe78bSCy Schubert ret = OSA_ADB_XDR_FAILURE;
82*7f2fe78bSCy Schubert goto error;
83*7f2fe78bSCy Schubert }
84*7f2fe78bSCy Schubert dbdata.data = xdralloc_getdata(&xdrs);
85*7f2fe78bSCy Schubert dbdata.size = xdr_getpos(&xdrs);
86*7f2fe78bSCy Schubert switch(db->db->put(db->db, &dbkey, &dbdata, R_NOOVERWRITE)) {
87*7f2fe78bSCy Schubert case 0:
88*7f2fe78bSCy Schubert if((db->db->sync(db->db, 0)) == -1)
89*7f2fe78bSCy Schubert ret = OSA_ADB_FAILURE;
90*7f2fe78bSCy Schubert ret = OSA_ADB_OK;
91*7f2fe78bSCy Schubert break;
92*7f2fe78bSCy Schubert case 1:
93*7f2fe78bSCy Schubert ret = OSA_ADB_DUP;
94*7f2fe78bSCy Schubert break;
95*7f2fe78bSCy Schubert default:
96*7f2fe78bSCy Schubert ret = OSA_ADB_FAILURE;
97*7f2fe78bSCy Schubert break;
98*7f2fe78bSCy Schubert }
99*7f2fe78bSCy Schubert xdr_destroy(&xdrs);
100*7f2fe78bSCy Schubert
101*7f2fe78bSCy Schubert error:
102*7f2fe78bSCy Schubert CLOSELOCK(db);
103*7f2fe78bSCy Schubert return ret;
104*7f2fe78bSCy Schubert }
105*7f2fe78bSCy Schubert
106*7f2fe78bSCy Schubert /*
107*7f2fe78bSCy Schubert * Function: osa_adb_destroy_policy
108*7f2fe78bSCy Schubert *
109*7f2fe78bSCy Schubert * Purpose: destroy a policy entry
110*7f2fe78bSCy Schubert *
111*7f2fe78bSCy Schubert * Arguments:
112*7f2fe78bSCy Schubert * db (input) database handle
113*7f2fe78bSCy Schubert * name (input) name of policy
114*7f2fe78bSCy Schubert * <return value> OSA_ADB_OK on success, or error code.
115*7f2fe78bSCy Schubert *
116*7f2fe78bSCy Schubert * Requires:
117*7f2fe78bSCy Schubert * db being valid.
118*7f2fe78bSCy Schubert * name being non-null.
119*7f2fe78bSCy Schubert * Effects:
120*7f2fe78bSCy Schubert * deletes policy from db.
121*7f2fe78bSCy Schubert *
122*7f2fe78bSCy Schubert * Modifies:
123*7f2fe78bSCy Schubert * policy db.
124*7f2fe78bSCy Schubert *
125*7f2fe78bSCy Schubert */
126*7f2fe78bSCy Schubert krb5_error_code
osa_adb_destroy_policy(osa_adb_policy_t db,char * name)127*7f2fe78bSCy Schubert osa_adb_destroy_policy(osa_adb_policy_t db, char *name)
128*7f2fe78bSCy Schubert {
129*7f2fe78bSCy Schubert DBT dbkey;
130*7f2fe78bSCy Schubert int status, ret;
131*7f2fe78bSCy Schubert
132*7f2fe78bSCy Schubert OPENLOCK(db, KRB5_DB_LOCKMODE_EXCLUSIVE);
133*7f2fe78bSCy Schubert
134*7f2fe78bSCy Schubert if(name == NULL) {
135*7f2fe78bSCy Schubert ret = EINVAL;
136*7f2fe78bSCy Schubert goto error;
137*7f2fe78bSCy Schubert }
138*7f2fe78bSCy Schubert dbkey.data = name;
139*7f2fe78bSCy Schubert dbkey.size = (strlen(name) + 1);
140*7f2fe78bSCy Schubert
141*7f2fe78bSCy Schubert status = db->db->del(db->db, &dbkey, 0);
142*7f2fe78bSCy Schubert switch(status) {
143*7f2fe78bSCy Schubert case 1:
144*7f2fe78bSCy Schubert ret = OSA_ADB_NOENT;
145*7f2fe78bSCy Schubert goto error;
146*7f2fe78bSCy Schubert case 0:
147*7f2fe78bSCy Schubert if ((db->db->sync(db->db, 0)) == -1) {
148*7f2fe78bSCy Schubert ret = OSA_ADB_FAILURE;
149*7f2fe78bSCy Schubert goto error;
150*7f2fe78bSCy Schubert }
151*7f2fe78bSCy Schubert ret = OSA_ADB_OK;
152*7f2fe78bSCy Schubert break;
153*7f2fe78bSCy Schubert default:
154*7f2fe78bSCy Schubert ret = OSA_ADB_FAILURE;
155*7f2fe78bSCy Schubert goto error;
156*7f2fe78bSCy Schubert }
157*7f2fe78bSCy Schubert
158*7f2fe78bSCy Schubert error:
159*7f2fe78bSCy Schubert CLOSELOCK(db);
160*7f2fe78bSCy Schubert return ret;
161*7f2fe78bSCy Schubert }
162*7f2fe78bSCy Schubert
163*7f2fe78bSCy Schubert /*
164*7f2fe78bSCy Schubert * Function: osa_adb_get_policy
165*7f2fe78bSCy Schubert *
166*7f2fe78bSCy Schubert * Purpose: retrieve policy
167*7f2fe78bSCy Schubert *
168*7f2fe78bSCy Schubert * Arguments:
169*7f2fe78bSCy Schubert * db (input) db handle
170*7f2fe78bSCy Schubert * name (input) name of policy
171*7f2fe78bSCy Schubert * entry (output) policy entry
172*7f2fe78bSCy Schubert * cnt (inout) Number of entries
173*7f2fe78bSCy Schubert * <return value> 0 on success, error code on failure.
174*7f2fe78bSCy Schubert *
175*7f2fe78bSCy Schubert * Requires:
176*7f2fe78bSCy Schubert * Effects:
177*7f2fe78bSCy Schubert * Modifies:
178*7f2fe78bSCy Schubert */
179*7f2fe78bSCy Schubert krb5_error_code
osa_adb_get_policy(osa_adb_policy_t db,char * name,osa_policy_ent_t * entry_ptr)180*7f2fe78bSCy Schubert osa_adb_get_policy(osa_adb_policy_t db, char *name,
181*7f2fe78bSCy Schubert osa_policy_ent_t *entry_ptr)
182*7f2fe78bSCy Schubert {
183*7f2fe78bSCy Schubert DBT dbkey;
184*7f2fe78bSCy Schubert DBT dbdata;
185*7f2fe78bSCy Schubert XDR xdrs;
186*7f2fe78bSCy Schubert int ret;
187*7f2fe78bSCy Schubert char *aligned_data = NULL;
188*7f2fe78bSCy Schubert osa_policy_ent_t entry = NULL;
189*7f2fe78bSCy Schubert
190*7f2fe78bSCy Schubert *entry_ptr = NULL;
191*7f2fe78bSCy Schubert OPENLOCK(db, KRB5_DB_LOCKMODE_SHARED);
192*7f2fe78bSCy Schubert
193*7f2fe78bSCy Schubert if(name == NULL) {
194*7f2fe78bSCy Schubert ret = EINVAL;
195*7f2fe78bSCy Schubert goto error;
196*7f2fe78bSCy Schubert }
197*7f2fe78bSCy Schubert dbkey.data = name;
198*7f2fe78bSCy Schubert dbkey.size = (strlen(dbkey.data) + 1);
199*7f2fe78bSCy Schubert dbdata.data = NULL;
200*7f2fe78bSCy Schubert dbdata.size = 0;
201*7f2fe78bSCy Schubert switch((db->db->get(db->db, &dbkey, &dbdata, 0))) {
202*7f2fe78bSCy Schubert case 1:
203*7f2fe78bSCy Schubert ret = KRB5_KDB_NOENTRY;
204*7f2fe78bSCy Schubert goto error;
205*7f2fe78bSCy Schubert case 0:
206*7f2fe78bSCy Schubert break;
207*7f2fe78bSCy Schubert default:
208*7f2fe78bSCy Schubert ret = OSA_ADB_FAILURE;
209*7f2fe78bSCy Schubert goto error;
210*7f2fe78bSCy Schubert }
211*7f2fe78bSCy Schubert entry = k5alloc(sizeof(*entry), &ret);
212*7f2fe78bSCy Schubert if (entry == NULL)
213*7f2fe78bSCy Schubert goto error;
214*7f2fe78bSCy Schubert aligned_data = k5memdup(dbdata.data, dbdata.size, &ret);
215*7f2fe78bSCy Schubert if (aligned_data == NULL)
216*7f2fe78bSCy Schubert goto error;
217*7f2fe78bSCy Schubert xdrmem_create(&xdrs, aligned_data, dbdata.size, XDR_DECODE);
218*7f2fe78bSCy Schubert if (!xdr_osa_policy_ent_rec(&xdrs, entry)) {
219*7f2fe78bSCy Schubert ret = OSA_ADB_FAILURE;
220*7f2fe78bSCy Schubert goto error;
221*7f2fe78bSCy Schubert }
222*7f2fe78bSCy Schubert ret = OSA_ADB_OK;
223*7f2fe78bSCy Schubert xdr_destroy(&xdrs);
224*7f2fe78bSCy Schubert *entry_ptr = entry;
225*7f2fe78bSCy Schubert entry = NULL;
226*7f2fe78bSCy Schubert
227*7f2fe78bSCy Schubert error:
228*7f2fe78bSCy Schubert free(aligned_data);
229*7f2fe78bSCy Schubert free(entry);
230*7f2fe78bSCy Schubert CLOSELOCK(db);
231*7f2fe78bSCy Schubert return ret;
232*7f2fe78bSCy Schubert }
233*7f2fe78bSCy Schubert
234*7f2fe78bSCy Schubert /*
235*7f2fe78bSCy Schubert * Function: osa_adb_put_policy
236*7f2fe78bSCy Schubert *
237*7f2fe78bSCy Schubert * Purpose: update a policy in the dababase
238*7f2fe78bSCy Schubert *
239*7f2fe78bSCy Schubert * Arguments:
240*7f2fe78bSCy Schubert * db (input) db handle
241*7f2fe78bSCy Schubert * entry (input) policy entry
242*7f2fe78bSCy Schubert * <return value> 0 on success error code on failure.
243*7f2fe78bSCy Schubert *
244*7f2fe78bSCy Schubert * Requires:
245*7f2fe78bSCy Schubert * [requires]
246*7f2fe78bSCy Schubert *
247*7f2fe78bSCy Schubert * Effects:
248*7f2fe78bSCy Schubert * [effects]
249*7f2fe78bSCy Schubert *
250*7f2fe78bSCy Schubert * Modifies:
251*7f2fe78bSCy Schubert * [modifies]
252*7f2fe78bSCy Schubert *
253*7f2fe78bSCy Schubert */
254*7f2fe78bSCy Schubert krb5_error_code
osa_adb_put_policy(osa_adb_policy_t db,osa_policy_ent_t entry)255*7f2fe78bSCy Schubert osa_adb_put_policy(osa_adb_policy_t db, osa_policy_ent_t entry)
256*7f2fe78bSCy Schubert {
257*7f2fe78bSCy Schubert DBT dbkey;
258*7f2fe78bSCy Schubert DBT dbdata;
259*7f2fe78bSCy Schubert DBT tmpdb;
260*7f2fe78bSCy Schubert XDR xdrs;
261*7f2fe78bSCy Schubert int ret;
262*7f2fe78bSCy Schubert
263*7f2fe78bSCy Schubert OPENLOCK(db, KRB5_DB_LOCKMODE_EXCLUSIVE);
264*7f2fe78bSCy Schubert
265*7f2fe78bSCy Schubert if(entry->name == NULL) {
266*7f2fe78bSCy Schubert ret = EINVAL;
267*7f2fe78bSCy Schubert goto error;
268*7f2fe78bSCy Schubert }
269*7f2fe78bSCy Schubert dbkey.data = entry->name;
270*7f2fe78bSCy Schubert dbkey.size = (strlen(entry->name) + 1);
271*7f2fe78bSCy Schubert switch(db->db->get(db->db, &dbkey, &tmpdb, 0)) {
272*7f2fe78bSCy Schubert case 0:
273*7f2fe78bSCy Schubert break;
274*7f2fe78bSCy Schubert case 1:
275*7f2fe78bSCy Schubert ret = OSA_ADB_NOENT;
276*7f2fe78bSCy Schubert goto error;
277*7f2fe78bSCy Schubert default:
278*7f2fe78bSCy Schubert ret = OSA_ADB_FAILURE;
279*7f2fe78bSCy Schubert goto error;
280*7f2fe78bSCy Schubert }
281*7f2fe78bSCy Schubert xdralloc_create(&xdrs, XDR_ENCODE);
282*7f2fe78bSCy Schubert if(!xdr_osa_policy_ent_rec(&xdrs, entry)) {
283*7f2fe78bSCy Schubert xdr_destroy(&xdrs);
284*7f2fe78bSCy Schubert ret = OSA_ADB_XDR_FAILURE;
285*7f2fe78bSCy Schubert goto error;
286*7f2fe78bSCy Schubert }
287*7f2fe78bSCy Schubert dbdata.data = xdralloc_getdata(&xdrs);
288*7f2fe78bSCy Schubert dbdata.size = xdr_getpos(&xdrs);
289*7f2fe78bSCy Schubert switch(db->db->put(db->db, &dbkey, &dbdata, 0)) {
290*7f2fe78bSCy Schubert case 0:
291*7f2fe78bSCy Schubert if((db->db->sync(db->db, 0)) == -1)
292*7f2fe78bSCy Schubert ret = OSA_ADB_FAILURE;
293*7f2fe78bSCy Schubert ret = OSA_ADB_OK;
294*7f2fe78bSCy Schubert break;
295*7f2fe78bSCy Schubert default:
296*7f2fe78bSCy Schubert ret = OSA_ADB_FAILURE;
297*7f2fe78bSCy Schubert break;
298*7f2fe78bSCy Schubert }
299*7f2fe78bSCy Schubert xdr_destroy(&xdrs);
300*7f2fe78bSCy Schubert
301*7f2fe78bSCy Schubert error:
302*7f2fe78bSCy Schubert CLOSELOCK(db);
303*7f2fe78bSCy Schubert return ret;
304*7f2fe78bSCy Schubert }
305*7f2fe78bSCy Schubert
306*7f2fe78bSCy Schubert /*
307*7f2fe78bSCy Schubert * Function: osa_adb_iter_policy
308*7f2fe78bSCy Schubert *
309*7f2fe78bSCy Schubert * Purpose: iterate over the policy database.
310*7f2fe78bSCy Schubert *
311*7f2fe78bSCy Schubert * Arguments:
312*7f2fe78bSCy Schubert * db (input) db handle
313*7f2fe78bSCy Schubert * func (input) function pointer to call
314*7f2fe78bSCy Schubert * data opaque data type
315*7f2fe78bSCy Schubert * <return value> 0 on success error code on failure
316*7f2fe78bSCy Schubert *
317*7f2fe78bSCy Schubert * Requires:
318*7f2fe78bSCy Schubert * Effects:
319*7f2fe78bSCy Schubert * Modifies:
320*7f2fe78bSCy Schubert */
321*7f2fe78bSCy Schubert krb5_error_code
osa_adb_iter_policy(osa_adb_policy_t db,osa_adb_iter_policy_func func,void * data)322*7f2fe78bSCy Schubert osa_adb_iter_policy(osa_adb_policy_t db, osa_adb_iter_policy_func func,
323*7f2fe78bSCy Schubert void *data)
324*7f2fe78bSCy Schubert {
325*7f2fe78bSCy Schubert DBT dbkey,
326*7f2fe78bSCy Schubert dbdata;
327*7f2fe78bSCy Schubert XDR xdrs;
328*7f2fe78bSCy Schubert int ret;
329*7f2fe78bSCy Schubert osa_policy_ent_t entry;
330*7f2fe78bSCy Schubert char *aligned_data;
331*7f2fe78bSCy Schubert
332*7f2fe78bSCy Schubert OPENLOCK(db, KRB5_DB_LOCKMODE_EXCLUSIVE); /* hmmm */
333*7f2fe78bSCy Schubert
334*7f2fe78bSCy Schubert if((ret = db->db->seq(db->db, &dbkey, &dbdata, R_FIRST)) == -1) {
335*7f2fe78bSCy Schubert ret = errno;
336*7f2fe78bSCy Schubert goto error;
337*7f2fe78bSCy Schubert }
338*7f2fe78bSCy Schubert
339*7f2fe78bSCy Schubert while (ret == 0) {
340*7f2fe78bSCy Schubert entry = k5alloc(sizeof(osa_policy_ent_rec), &ret);
341*7f2fe78bSCy Schubert if (entry == NULL)
342*7f2fe78bSCy Schubert goto error;
343*7f2fe78bSCy Schubert
344*7f2fe78bSCy Schubert aligned_data = k5memdup(dbdata.data, dbdata.size, &ret);
345*7f2fe78bSCy Schubert if (aligned_data == NULL) {
346*7f2fe78bSCy Schubert free(entry);
347*7f2fe78bSCy Schubert goto error;
348*7f2fe78bSCy Schubert }
349*7f2fe78bSCy Schubert
350*7f2fe78bSCy Schubert xdrmem_create(&xdrs, aligned_data, dbdata.size, XDR_DECODE);
351*7f2fe78bSCy Schubert if(!xdr_osa_policy_ent_rec(&xdrs, entry)) {
352*7f2fe78bSCy Schubert xdr_destroy(&xdrs);
353*7f2fe78bSCy Schubert free(aligned_data);
354*7f2fe78bSCy Schubert osa_free_policy_ent(entry);
355*7f2fe78bSCy Schubert ret = OSA_ADB_FAILURE;
356*7f2fe78bSCy Schubert goto error;
357*7f2fe78bSCy Schubert }
358*7f2fe78bSCy Schubert (*func)(data, entry);
359*7f2fe78bSCy Schubert xdr_destroy(&xdrs);
360*7f2fe78bSCy Schubert free(aligned_data);
361*7f2fe78bSCy Schubert osa_free_policy_ent(entry);
362*7f2fe78bSCy Schubert ret = db->db->seq(db->db, &dbkey, &dbdata, R_NEXT);
363*7f2fe78bSCy Schubert }
364*7f2fe78bSCy Schubert if(ret == -1)
365*7f2fe78bSCy Schubert ret = errno;
366*7f2fe78bSCy Schubert else ret = OSA_ADB_OK;
367*7f2fe78bSCy Schubert
368*7f2fe78bSCy Schubert error:
369*7f2fe78bSCy Schubert CLOSELOCK(db);
370*7f2fe78bSCy Schubert return ret;
371*7f2fe78bSCy Schubert }
372*7f2fe78bSCy Schubert
373*7f2fe78bSCy Schubert void
osa_free_policy_ent(osa_policy_ent_t val)374*7f2fe78bSCy Schubert osa_free_policy_ent(osa_policy_ent_t val)
375*7f2fe78bSCy Schubert {
376*7f2fe78bSCy Schubert XDR xdrs;
377*7f2fe78bSCy Schubert
378*7f2fe78bSCy Schubert xdrmem_create(&xdrs, NULL, 0, XDR_FREE);
379*7f2fe78bSCy Schubert
380*7f2fe78bSCy Schubert xdr_osa_policy_ent_rec(&xdrs, val);
381*7f2fe78bSCy Schubert
382*7f2fe78bSCy Schubert free(val);
383*7f2fe78bSCy Schubert }
384