1 /*
2  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 #pragma ident	"%Z%%M%	%I%	%E% SMI"
7 
8 /*
9  * Copyright 1993 by OpenVision Technologies, Inc.
10  *
11  * Permission to use, copy, modify, distribute, and sell this software
12  * and its documentation for any purpose is hereby granted without fee,
13  * provided that the above copyright notice appears in all copies and
14  * that both that copyright notice and this permission notice appear in
15  * supporting documentation, and that the name of OpenVision not be used
16  * in advertising or publicity pertaining to distribution of the software
17  * without specific, written prior permission. OpenVision makes no
18  * representations about the suitability of this software for any
19  * purpose.  It is provided "as is" without express or implied warranty.
20  *
21  * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
22  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
23  * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
24  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
25  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
26  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
27  * PERFORMANCE OF THIS SOFTWARE.
28  */
29 
30 /*
31  * $Id: util_validate.c,v 1.8 1996/08/28 21:50:37 tytso Exp $
32  */
33 
34 /*
35  * functions to validate name, credential, and context handles
36  */
37 
38 #include <gssapiP_generic.h>
39 
40 #ifdef HAVE_SYS_TYPES_H
41 #include <sys/types.h>
42 #endif
43 
44 #ifdef	_KERNEL
45 #include <sys/fcntl.h>
46 #else
47 #include <fcntl.h>
48 #include <limits.h>
49 #endif
50 
51 #ifdef HAVE_BSD_DB
52 #include <sys/file.h>
53 #include <db.h>
54 
55 static const int one = 1;
56 static const DBT dbtone = { (void *) &one, sizeof(one) };
57 
58 typedef struct _vkey {
59    int type;
60    void *ptr;
61 } vkey;
62 #endif
63 
64 #define V_NAME		1
65 #define V_CRED_ID	2
66 #define V_CTX_ID	3
67 
68 /* All these functions return 0 on failure, and non-zero on success */
69 
70 static int g_save(db, type, ptr)
71      void **db;
72      int type;
73      void *ptr;
74 {
75 #ifdef HAVE_BSD_DB
76    DB **vdb = (DB **) db;
77    vkey vk;
78    DBT key;
79 
80    if (!*vdb)
81       *vdb = dbopen(NULL, O_CREAT|O_RDWR, O_CREAT|O_RDWR, DB_HASH, NULL);
82 
83    vk.type = type;
84    vk.ptr = ptr;
85 
86    key.data = &vk;
87    key.size = sizeof(vk);
88 
89    return((*((*vdb)->put))(*vdb, &key, &dbtone, 0) == 0);
90 #else
91    g_set *gs = (g_set *) db;
92 
93    if (!*gs)
94       if (g_set_init(gs))
95 	 return(0);
96 
97    return(g_set_entry_add(gs, ptr, (void *)(intptr_t)type) == 0);
98 #endif
99 }
100 
101 static int g_validate(db, type, ptr)
102      void **db;
103      int type;
104      void *ptr;
105 {
106 #ifdef HAVE_BSD_DB
107    DB **vdb = (DB **) db;
108    vkey vk;
109    DBT key, value;
110 
111    if (!*vdb)
112       return(0);
113 
114    vk.type = type;
115    vk.ptr = ptr;
116 
117    key.data = &vk;
118    key.size = sizeof(vk);
119 
120    if ((*((*vdb)->get))(*vdb, &key, &value, 0))
121       return(0);
122 
123    return((value.size == sizeof(one)) &&
124 	  (*((int *) value.data) == one));
125 #else
126    g_set *gs = (g_set *) db;
127    void *value;
128 
129    if (!*gs)
130       return(0);
131 
132    if (g_set_entry_get(gs, ptr, (void **) &value))
133       return(0);
134 
135    return((intptr_t)value == (intptr_t)type);
136 #endif
137 }
138 
139 /*ARGSUSED*/
140 static int g_delete(db, type, ptr)
141      void **db;
142      int type;
143      void *ptr;
144 {
145 #ifdef HAVE_BSD_DB
146    DB **vdb = (DB **) db;
147    vkey vk;
148    DBT key;
149 
150    if (!*vdb)
151       return(0);
152 
153    vk.type = type;
154    vk.ptr = ptr;
155 
156    key.data = &vk;
157    key.size = sizeof(vk);
158 
159    return((*((*vdb)->del))(*vdb, &key, 0) == 0);
160 #else
161    g_set *gs = (g_set *) db;
162 
163    if (!*gs)
164       return(0);
165 
166    if (g_set_entry_delete(gs, ptr))
167       return(0);
168 
169    return(1);
170 #endif
171 }
172 
173 /* functions for each type */
174 
175 /* save */
176 
177 int g_save_name(vdb, name)
178      void **vdb;
179      gss_name_t name;
180 {
181    return(g_save(vdb, V_NAME, (void *) name));
182 }
183 int g_save_cred_id(vdb, cred)
184      void **vdb;
185      gss_cred_id_t cred;
186 {
187    return(g_save(vdb, V_CRED_ID, (void *) cred));
188 }
189 int g_save_ctx_id(vdb, ctx)
190      void **vdb;
191      gss_ctx_id_t ctx;
192 {
193    return(g_save(vdb, V_CTX_ID, (void *) ctx));
194 }
195 
196 /* validate */
197 
198 int g_validate_name(vdb, name)
199      void **vdb;
200      gss_name_t name;
201 {
202    return(g_validate(vdb, V_NAME, (void *) name));
203 }
204 int g_validate_cred_id(vdb, cred)
205      void **vdb;
206      gss_cred_id_t cred;
207 {
208    return(g_validate(vdb, V_CRED_ID, (void *) cred));
209 }
210 int g_validate_ctx_id(vdb, ctx)
211      void **vdb;
212      gss_ctx_id_t ctx;
213 {
214    return(g_validate(vdb, V_CTX_ID, (void *) ctx));
215 }
216 
217 /* delete */
218 
219 int g_delete_name(vdb, name)
220      void **vdb;
221      gss_name_t name;
222 {
223    return(g_delete(vdb, V_NAME, (void *) name));
224 }
225 int g_delete_cred_id(vdb, cred)
226      void **vdb;
227      gss_cred_id_t cred;
228 {
229    return(g_delete(vdb, V_CRED_ID, (void *) cred));
230 }
231 int g_delete_ctx_id(vdb, ctx)
232      void **vdb;
233      gss_ctx_id_t ctx;
234 {
235    return(g_delete(vdb, V_CTX_ID, (void *) ctx));
236 }
237 
238