1 /* 2 * Copyright 2007 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 16475 2004-06-17 02:23:21Z raeburn $ 32 */ 33 34 /* 35 * functions to validate name, credential, and context handles 36 */ 37 38 #include "gssapiP_generic.h" 39 #ifndef _KERNEL 40 #include "gss_libinit.h" 41 #endif 42 43 #ifdef HAVE_SYS_TYPES_H 44 #include <sys/types.h> 45 #endif 46 47 #ifdef _KERNEL 48 #include <sys/fcntl.h> 49 #else 50 #include <fcntl.h> 51 #include <limits.h> 52 #endif 53 54 55 #ifdef HAVE_BSD_DB 56 #include <sys/file.h> 57 #include <db.h> 58 59 60 static const int one = 1; 61 static const DBT dbtone = { (void *) &one, sizeof(one) }; 62 63 typedef struct _vkey { 64 int type; 65 void *ptr; 66 } vkey; 67 #endif 68 69 #define V_NAME 1 70 #define V_CRED_ID 2 71 #define V_CTX_ID 3 72 #define V_LCTX_ID 4 73 74 /* SUNW15resync 75 beware some of the uses below of type look dubious but seem 76 to have been working in Solaris for a long time */ 77 78 /* All these functions return 0 on failure, and non-zero on success */ 79 80 static int g_save(db, type, ptr) 81 g_set *db; 82 int type; 83 void *ptr; 84 { 85 int ret; 86 #ifdef HAVE_BSD_DB 87 DB **vdb; 88 vkey vk; 89 DBT key; 90 91 #ifndef _KERNEL 92 ret = gssint_initialize_library(); 93 if (ret) 94 return 0; 95 #endif 96 ret = k5_mutex_lock(&db->mutex); 97 if (ret) 98 return 0; 99 100 vdb = (DB **) &db->data; 101 102 if (!*vdb) 103 *vdb = dbopen(NULL, O_CREAT|O_RDWR, O_CREAT|O_RDWR, DB_HASH, NULL); 104 105 vk.type = type; 106 vk.ptr = ptr; 107 108 key.data = &vk; 109 key.size = sizeof(vk); 110 111 ret = ((*((*vdb)->put))(*vdb, &key, &dbtone, 0) == 0); 112 (void) k5_mutex_unlock(&db->mutex); 113 return ret; 114 #else 115 g_set_elt *gs; 116 117 #ifndef _KERNEL 118 ret = gssint_initialize_library(); 119 if (ret) 120 return 0; 121 #endif 122 ret = k5_mutex_lock(&db->mutex); 123 if (ret) 124 return 0; 125 126 gs = (g_set_elt *) &db->data; 127 128 if (!*gs) 129 if (g_set_init(gs)) { 130 (void) k5_mutex_unlock(&db->mutex); 131 return(0); 132 } 133 134 /* SUNW15resync */ 135 ret = (g_set_entry_add(gs, ptr, (void *)(intptr_t)type) == 0); 136 137 (void) k5_mutex_unlock(&db->mutex); 138 return ret; 139 #endif 140 } 141 142 static int g_validate(db, type, ptr) 143 g_set *db; 144 int type; 145 void *ptr; 146 { 147 int ret; 148 #ifdef HAVE_BSD_DB 149 DB **vdb; 150 vkey vk; 151 DBT key, value; 152 153 ret = k5_mutex_lock(&db->mutex); 154 if (ret) 155 return 0; 156 157 vdb = (DB **) &db->data; 158 if (!*vdb) { 159 (void) k5_mutex_unlock(&db->mutex); 160 return(0); 161 } 162 163 vk.type = type; 164 vk.ptr = ptr; 165 166 key.data = &vk; 167 key.size = sizeof(vk); 168 169 if ((*((*vdb)->get))(*vdb, &key, &value, 0)) { 170 (void) k5_mutex_unlock(&db->mutex); 171 return(0); 172 } 173 174 (void) k5_mutex_unlock(&db->mutex); 175 return((value.size == sizeof(one)) && 176 (*((int *) value.data) == one)); 177 #else 178 g_set_elt *gs; 179 void *value; 180 181 ret = k5_mutex_lock(&db->mutex); 182 if (ret) 183 return 0; 184 185 gs = (g_set_elt *) &db->data; 186 if (!*gs) { 187 (void) k5_mutex_unlock(&db->mutex); 188 return(0); 189 } 190 191 if (g_set_entry_get(gs, ptr, (void **) &value)) { 192 (void) k5_mutex_unlock(&db->mutex); 193 return(0); 194 } 195 (void) k5_mutex_unlock(&db->mutex); 196 return((intptr_t)value == (intptr_t)type); /* SUNW15resync */ 197 #endif 198 } 199 200 /*ARGSUSED*/ 201 static int g_delete(db, type, ptr) 202 g_set *db; 203 int type; 204 void *ptr; 205 { 206 int ret; 207 #ifdef HAVE_BSD_DB 208 DB **vdb; 209 vkey vk; 210 DBT key; 211 212 ret = k5_mutex_lock(&db->mutex); 213 if (ret) 214 return 0; 215 216 vdb = (DB **) &db->data; 217 if (!*vdb) { 218 (void) k5_mutex_unlock(&db->mutex); 219 return(0); 220 } 221 222 vk.type = type; 223 vk.ptr = ptr; 224 225 key.data = &vk; 226 key.size = sizeof(vk); 227 228 ret = ((*((*vdb)->del))(*vdb, &key, 0) == 0); 229 (void) k5_mutex_unlock(&db->mutex); 230 return ret; 231 #else 232 g_set_elt *gs; 233 234 ret = k5_mutex_lock(&db->mutex); 235 if (ret) 236 return 0; 237 238 gs = (g_set_elt *) &db->data; 239 if (!*gs) { 240 (void) k5_mutex_unlock(&db->mutex); 241 return(0); 242 } 243 244 if (g_set_entry_delete(gs, ptr)) { 245 (void) k5_mutex_unlock(&db->mutex); 246 return(0); 247 } 248 (void) k5_mutex_unlock(&db->mutex); 249 return(1); 250 #endif 251 } 252 253 /* functions for each type */ 254 255 /* save */ 256 257 int g_save_name(vdb, name) 258 g_set *vdb; 259 gss_name_t name; 260 { 261 return(g_save(vdb, V_NAME, (void *) name)); 262 } 263 int g_save_cred_id(vdb, cred) 264 g_set *vdb; 265 gss_cred_id_t cred; 266 { 267 return(g_save(vdb, V_CRED_ID, (void *) cred)); 268 } 269 int g_save_ctx_id(vdb, ctx) 270 g_set *vdb; 271 gss_ctx_id_t ctx; 272 { 273 return(g_save(vdb, V_CTX_ID, (void *) ctx)); 274 } 275 int g_save_lucidctx_id(vdb, lctx) 276 g_set *vdb; 277 void *lctx; 278 { 279 return(g_save(vdb, V_LCTX_ID, (void *) lctx)); 280 } 281 282 283 /* validate */ 284 285 int g_validate_name(vdb, name) 286 g_set *vdb; 287 gss_name_t name; 288 { 289 return(g_validate(vdb, V_NAME, (void *) name)); 290 } 291 int g_validate_cred_id(vdb, cred) 292 g_set *vdb; 293 gss_cred_id_t cred; 294 { 295 return(g_validate(vdb, V_CRED_ID, (void *) cred)); 296 } 297 int g_validate_ctx_id(vdb, ctx) 298 g_set *vdb; 299 gss_ctx_id_t ctx; 300 { 301 return(g_validate(vdb, V_CTX_ID, (void *) ctx)); 302 } 303 int g_validate_lucidctx_id(vdb, lctx) 304 g_set *vdb; 305 void *lctx; 306 { 307 return(g_validate(vdb, V_LCTX_ID, (void *) lctx)); 308 } 309 310 /* delete */ 311 312 int g_delete_name(vdb, name) 313 g_set *vdb; 314 gss_name_t name; 315 { 316 return(g_delete(vdb, V_NAME, (void *) name)); 317 } 318 int g_delete_cred_id(vdb, cred) 319 g_set *vdb; 320 gss_cred_id_t cred; 321 { 322 return(g_delete(vdb, V_CRED_ID, (void *) cred)); 323 } 324 int g_delete_ctx_id(vdb, ctx) 325 g_set *vdb; 326 gss_ctx_id_t ctx; 327 { 328 return(g_delete(vdb, V_CTX_ID, (void *) ctx)); 329 } 330 int g_delete_lucidctx_id(vdb, lctx) 331 g_set *vdb; 332 void *lctx; 333 { 334 return(g_delete(vdb, V_LCTX_ID, (void *) lctx)); 335 } 336 337