1 /*
2 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
4 */
5
6
7 /*
8 * Copyright 1993 by OpenVision Technologies, Inc.
9 *
10 * Permission to use, copy, modify, distribute, and sell this software
11 * and its documentation for any purpose is hereby granted without fee,
12 * provided that the above copyright notice appears in all copies and
13 * that both that copyright notice and this permission notice appear in
14 * supporting documentation, and that the name of OpenVision not be used
15 * in advertising or publicity pertaining to distribution of the software
16 * without specific, written prior permission. OpenVision makes no
17 * representations about the suitability of this software for any
18 * purpose. It is provided "as is" without express or implied warranty.
19 *
20 * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
21 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
22 * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
23 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
24 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
25 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
26 * PERFORMANCE OF THIS SOFTWARE.
27 */
28
29 /*
30 * $Id: util_validate.c 18721 2006-10-16 16:18:29Z epeisach $
31 */
32
33 /*
34 * functions to validate name, credential, and context handles
35 */
36
37 #include "gssapiP_generic.h"
38 #ifndef _KERNEL
39 #include "gss_libinit.h"
40 #endif
41
42 #ifdef HAVE_SYS_TYPES_H
43 #include <sys/types.h>
44 #endif
45
46 #ifdef _KERNEL
47 #include <sys/fcntl.h>
48 #else
49 #include <fcntl.h>
50 #include <limits.h>
51 #endif
52
53 #ifdef HAVE_BSD_DB
54 #include <sys/file.h>
55 #include <db.h>
56
57 static const int one = 1;
58 static const DBT dbtone = { (void *) &one, sizeof(one) };
59
60 typedef struct _vkey {
61 int type;
62 void *ptr;
63 } vkey;
64 #endif
65
66 #define V_NAME 1
67 #define V_CRED_ID 2
68 #define V_CTX_ID 3
69 #define V_LCTX_ID 4
70
71 /* SUNW15resync
72 beware some of the uses below of type look dubious but seem
73 to have been working in Solaris for a long time */
74
75 /* All these functions return 0 on failure, and non-zero on success */
76
g_save(db,type,ptr)77 static int g_save(db, type, ptr)
78 g_set *db;
79 int type;
80 void *ptr;
81 {
82 int ret;
83 #ifdef HAVE_BSD_DB
84 DB **vdb;
85 vkey vk;
86 DBT key;
87
88 #ifndef _KERNEL
89 ret = gssint_initialize_library();
90 if (ret)
91 return 0;
92 #endif
93 ret = k5_mutex_lock(&db->mutex);
94 if (ret)
95 return 0;
96
97 vdb = (DB **) &db->data;
98
99 if (!*vdb)
100 *vdb = dbopen(NULL, O_CREAT|O_RDWR, O_CREAT|O_RDWR, DB_HASH, NULL);
101
102 vk.type = type;
103 vk.ptr = ptr;
104
105 key.data = &vk;
106 key.size = sizeof(vk);
107
108 ret = ((*((*vdb)->put))(*vdb, &key, &dbtone, 0) == 0);
109 (void) k5_mutex_unlock(&db->mutex);
110 return ret;
111 #else
112 g_set_elt *gs;
113
114 #ifndef _KERNEL
115 ret = gssint_initialize_library();
116 if (ret)
117 return 0;
118 #endif
119 ret = k5_mutex_lock(&db->mutex);
120 if (ret)
121 return 0;
122
123 gs = (g_set_elt *) &db->data;
124
125 if (!*gs)
126 if (g_set_init(gs)) {
127 (void) k5_mutex_unlock(&db->mutex);
128 return(0);
129 }
130
131 /* SUNW15resync */
132 ret = (g_set_entry_add(gs, ptr, (void *)(intptr_t)type) == 0);
133 (void) k5_mutex_unlock(&db->mutex);
134 return ret;
135 #endif
136 }
137
g_validate(db,type,ptr)138 static int g_validate(db, type, ptr)
139 g_set *db;
140 int type;
141 void *ptr;
142 {
143 int ret;
144 #ifdef HAVE_BSD_DB
145 DB **vdb;
146 vkey vk;
147 DBT key, value;
148
149 ret = k5_mutex_lock(&db->mutex);
150 if (ret)
151 return 0;
152
153 vdb = (DB **) &db->data;
154 if (!*vdb) {
155 (void) k5_mutex_unlock(&db->mutex);
156 return(0);
157 }
158
159 vk.type = type;
160 vk.ptr = ptr;
161
162 key.data = &vk;
163 key.size = sizeof(vk);
164
165 if ((*((*vdb)->get))(*vdb, &key, &value, 0)) {
166 (void) k5_mutex_unlock(&db->mutex);
167 return(0);
168 }
169
170 (void) k5_mutex_unlock(&db->mutex);
171 return((value.size == sizeof(one)) &&
172 (*((int *) value.data) == one));
173 #else
174 g_set_elt *gs;
175 void *value;
176
177 ret = k5_mutex_lock(&db->mutex);
178 if (ret)
179 return 0;
180
181 gs = (g_set_elt *) &db->data;
182 if (!*gs) {
183 (void) k5_mutex_unlock(&db->mutex);
184 return(0);
185 }
186
187 if (g_set_entry_get(gs, ptr, (void **) &value)) {
188 (void) k5_mutex_unlock(&db->mutex);
189 return(0);
190 }
191 (void) k5_mutex_unlock(&db->mutex);
192 return((intptr_t)value == (intptr_t)type); /* SUNW15resync */
193 #endif
194 }
195
196 /*ARGSUSED*/
g_delete(db,type,ptr)197 static int g_delete(db, type, ptr)
198 g_set *db;
199 int type;
200 void *ptr;
201 {
202 int ret;
203 #ifdef HAVE_BSD_DB
204 DB **vdb;
205 vkey vk;
206 DBT key;
207
208 ret = k5_mutex_lock(&db->mutex);
209 if (ret)
210 return 0;
211
212 vdb = (DB **) &db->data;
213 if (!*vdb) {
214 (void) k5_mutex_unlock(&db->mutex);
215 return(0);
216 }
217
218 vk.type = type;
219 vk.ptr = ptr;
220
221 key.data = &vk;
222 key.size = sizeof(vk);
223
224 ret = ((*((*vdb)->del))(*vdb, &key, 0) == 0);
225 (void) k5_mutex_unlock(&db->mutex);
226 return ret;
227 #else
228 g_set_elt *gs;
229
230 ret = k5_mutex_lock(&db->mutex);
231 if (ret)
232 return 0;
233
234 gs = (g_set_elt *) &db->data;
235 if (!*gs) {
236 (void) k5_mutex_unlock(&db->mutex);
237 return(0);
238 }
239
240 if (g_set_entry_delete(gs, ptr)) {
241 (void) k5_mutex_unlock(&db->mutex);
242 return(0);
243 }
244 (void) k5_mutex_unlock(&db->mutex);
245 return(1);
246 #endif
247 }
248
249 /* functions for each type */
250
251 /* save */
252
g_save_name(vdb,name)253 int g_save_name(vdb, name)
254 g_set *vdb;
255 gss_name_t name;
256 {
257 return(g_save(vdb, V_NAME, (void *) name));
258 }
g_save_cred_id(vdb,cred)259 int g_save_cred_id(vdb, cred)
260 g_set *vdb;
261 gss_cred_id_t cred;
262 {
263 return(g_save(vdb, V_CRED_ID, (void *) cred));
264 }
g_save_ctx_id(vdb,ctx)265 int g_save_ctx_id(vdb, ctx)
266 g_set *vdb;
267 gss_ctx_id_t ctx;
268 {
269 return(g_save(vdb, V_CTX_ID, (void *) ctx));
270 }
g_save_lucidctx_id(vdb,lctx)271 int g_save_lucidctx_id(vdb, lctx)
272 g_set *vdb;
273 void *lctx;
274 {
275 return(g_save(vdb, V_LCTX_ID, (void *) lctx));
276 }
277
278
279 /* validate */
280
g_validate_name(vdb,name)281 int g_validate_name(vdb, name)
282 g_set *vdb;
283 gss_name_t name;
284 {
285 return(g_validate(vdb, V_NAME, (void *) name));
286 }
g_validate_cred_id(vdb,cred)287 int g_validate_cred_id(vdb, cred)
288 g_set *vdb;
289 gss_cred_id_t cred;
290 {
291 return(g_validate(vdb, V_CRED_ID, (void *) cred));
292 }
g_validate_ctx_id(vdb,ctx)293 int g_validate_ctx_id(vdb, ctx)
294 g_set *vdb;
295 gss_ctx_id_t ctx;
296 {
297 return(g_validate(vdb, V_CTX_ID, (void *) ctx));
298 }
g_validate_lucidctx_id(vdb,lctx)299 int g_validate_lucidctx_id(vdb, lctx)
300 g_set *vdb;
301 void *lctx;
302 {
303 return(g_validate(vdb, V_LCTX_ID, (void *) lctx));
304 }
305
306 /* delete */
307
g_delete_name(vdb,name)308 int g_delete_name(vdb, name)
309 g_set *vdb;
310 gss_name_t name;
311 {
312 return(g_delete(vdb, V_NAME, (void *) name));
313 }
g_delete_cred_id(vdb,cred)314 int g_delete_cred_id(vdb, cred)
315 g_set *vdb;
316 gss_cred_id_t cred;
317 {
318 return(g_delete(vdb, V_CRED_ID, (void *) cred));
319 }
g_delete_ctx_id(vdb,ctx)320 int g_delete_ctx_id(vdb, ctx)
321 g_set *vdb;
322 gss_ctx_id_t ctx;
323 {
324 return(g_delete(vdb, V_CTX_ID, (void *) ctx));
325 }
g_delete_lucidctx_id(vdb,lctx)326 int g_delete_lucidctx_id(vdb, lctx)
327 g_set *vdb;
328 void *lctx;
329 {
330 return(g_delete(vdb, V_LCTX_ID, (void *) lctx));
331 }
332
333