-: 0:Source:util_errmap.c -: 0:Graph:/var/tsitkova/Sources/v10/trunk/src/lib/gssapi/generic/util_errmap.so.gcno -: 0:Data:/var/tsitkova/Sources/v10/trunk/src/lib/gssapi/generic/util_errmap.so.gcda -: 0:Runs:1069 -: 0:Programs:1 -: 1:/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ -: 2:/* -: 3: * Copyright 2007, 2008 by the Massachusetts Institute of Technology. -: 4: * All Rights Reserved. -: 5: * -: 6: * Export of this software from the United States of America may -: 7: * require a specific license from the United States Government. -: 8: * It is the responsibility of any person or organization contemplating -: 9: * export to obtain such a license before exporting. -: 10: * -: 11: * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and -: 12: * distribute this software and its documentation for any purpose and -: 13: * without fee is hereby granted, provided that the above copyright -: 14: * notice appear in all copies and that both that copyright notice and -: 15: * this permission notice appear in supporting documentation, and that -: 16: * the name of M.I.T. not be used in advertising or publicity pertaining -: 17: * to distribution of the software without specific, written prior -: 18: * permission. Furthermore if you modify this software you must label -: 19: * your software as modified software and not distribute it in such a -: 20: * fashion that it might be confused with the original M.I.T. software. -: 21: * M.I.T. makes no representations about the suitability of -: 22: * this software for any purpose. It is provided "as is" without express -: 23: * or implied warranty. -: 24: * -: 25: */ -: 26: -: 27:#include "gssapiP_generic.h" -: 28:#include -: 29:#ifndef _WIN32 -: 30:#include -: 31:#endif -: 32: -: 33:/* The mapping table is 0-based, but let's export codes that are -: 34: 1-based, keeping 0 for errors or unknown errors. -: 35: -: 36: The elements in the mapping table currently have separate copies of -: 37: each OID stored. This is a bit wasteful, but we are assuming the -: 38: table isn't likely to grow very large. */ -: 39: -: 40:struct mecherror { -: 41: gss_OID_desc mech; -: 42: OM_uint32 code; -: 43:}; -: 44: -: 45:static inline int 8545: 46:cmp_OM_uint32(OM_uint32 m1, OM_uint32 m2) -: 47:{ 8545: 48: if (m1 < m2) 816: 49: return -1; 7729: 50: else if (m1 > m2) 7178: 51: return 1; -: 52: else 551: 53: return 0; -: 54:} -: 55: -: 56:static inline int 12326: 57:mecherror_cmp(struct mecherror m1, struct mecherror m2) -: 58:{ 12326: 59: if (m1.code < m2.code) #####: 60: return -1; 12326: 61: if (m1.code > m2.code) 94: 62: return 1; 12232: 63: if (m1.mech.length < m2.mech.length) 2772: 64: return -1; 9460: 65: if (m1.mech.length > m2.mech.length) 6472: 66: return 1; 2988: 67: if (m1.mech.length == 0) #####: 68: return 0; 2988: 69: return memcmp(m1.mech.elements, m2.mech.elements, m1.mech.length); -: 70:} -: 71: -: 72:static void #####: 73:print_OM_uint32 (OM_uint32 value, FILE *f) -: 74:{ #####: 75: fprintf(f, "%lu", (unsigned long) value); #####: 76:} -: 77: -: 78:static inline int 551: 79:mecherror_copy(struct mecherror *dest, struct mecherror src) -: 80:{ 551: 81: *dest = src; 551: 82: dest->mech.elements = malloc(src.mech.length); 551: 83: if (dest->mech.elements == NULL) { #####: 84: if (src.mech.length) #####: 85: return ENOMEM; -: 86: else #####: 87: return 0; -: 88: } 551: 89: memcpy(dest->mech.elements, src.mech.elements, src.mech.length); 551: 90: return 0; -: 91:} -: 92: -: 93:static void #####: 94:mecherror_print(struct mecherror value, FILE *f) -: 95:{ -: 96: OM_uint32 minor; -: 97: gss_buffer_desc str; -: 98: static const struct { -: 99: const char *oidstr, *name; -: 100: } mechnames[] = { -: 101: { "{ 1 2 840 113554 1 2 2 }", "krb5-new" }, -: 102: { "{ 1 3 5 1 5 2 }", "krb5-old" }, -: 103: { "{ 1 2 840 48018 1 2 2 }", "krb5-microsoft" }, -: 104: { "{ 1 3 6 1 5 5 2 }", "spnego" }, -: 105: }; -: 106: unsigned int i; -: 107: #####: 108: fprintf(f, "%lu@", (unsigned long) value.code); -: 109: #####: 110: if (value.mech.length == 0) { #####: 111: fprintf(f, "(com_err)"); #####: 112: return; -: 113: } #####: 114: fprintf(f, "%p=", value.mech.elements); #####: 115: if (generic_gss_oid_to_str(&minor, &value.mech, &str)) { #####: 116: fprintf(f, "(error in conversion)"); #####: 117: return; -: 118: } -: 119: /* Note: generic_gss_oid_to_str returns a null-terminated string. */ #####: 120: for (i = 0; i < sizeof(mechnames)/sizeof(mechnames[0]); i++) { #####: 121: if (!strcmp(str.value, mechnames[i].oidstr) && mechnames[i].name != 0) { #####: 122: fprintf(f, "%s", mechnames[i].name); #####: 123: break; -: 124: } -: 125: } #####: 126: if (i == sizeof(mechnames)/sizeof(mechnames[0])) #####: 127: fprintf(f, "%s", (char *) str.value); #####: 128: generic_gss_release_buffer(&minor, &str); -: 129:} -: 130: -: 131:#include "errmap.h" -: 132:#include "krb5.h" /* for KRB5KRB_AP_WRONG_PRINC */ -: 133: -: 134:static mecherrmap m; -: 135:static k5_mutex_t mutex = K5_MUTEX_PARTIAL_INITIALIZER; -: 136:static OM_uint32 next_fake = 100000; -: 137: 790: 138:int gssint_mecherrmap_init(void) -: 139:{ -: 140: int err; -: 141: 790: 142: err = mecherrmap_init(&m); 790: 143: if (err) #####: 144: return err; 790: 145: err = k5_mutex_finish_init(&mutex); 790: 146: if (err) { #####: 147: mecherrmap_destroy(&m); #####: 148: return err; -: 149: } -: 150: 790: 151: return 0; -: 152:} -: 153: -: 154:/* Currently the enumeration template doesn't handle freeing -: 155: element storage when destroying the collection. */ #####: 156:static int free_one(OM_uint32 i, struct mecherror value, void *p) -: 157:{ #####: 158: if (value.mech.length && value.mech.elements) #####: 159: free(value.mech.elements); #####: 160: return 0; -: 161:} -: 162: #####: 163:void gssint_mecherrmap_destroy(void) -: 164:{ #####: 165: mecherrmap_foreach(&m, free_one, NULL); #####: 166: mecherrmap_destroy(&m); #####: 167: k5_mutex_destroy(&mutex); #####: 168:} -: 169: 2615: 170:OM_uint32 gssint_mecherrmap_map(OM_uint32 minor, const gss_OID_desc * oid) -: 171:{ -: 172: const struct mecherror *mep; -: 173: struct mecherror me, me_copy; -: 174: const OM_uint32 *p; -: 175: int err; -: 176: OM_uint32 new_status; -: 177: -: 178:#ifdef DEBUG -: 179: FILE *f; -: 180: f = fopen("/dev/pts/9", "w+"); -: 181: if (f == NULL) -: 182: f = stderr; -: 183:#endif -: 184: 2615: 185: me.code = minor; 2615: 186: me.mech = *oid; 2615: 187: err = k5_mutex_lock(&mutex); 2615: 188: if (err) { -: 189:#ifdef DEBUG -: 190: if (f != stderr) fclose(f); -: 191:#endif #####: 192: return 0; -: 193: } -: 194: -: 195: /* Is this status+oid already mapped? */ 2615: 196: p = mecherrmap_findright(&m, me); 2615: 197: if (p != NULL) { 2064: 198: k5_mutex_unlock(&mutex); -: 199:#ifdef DEBUG -: 200: fprintf(f, "%s: found ", __FUNCTION__); -: 201: mecherror_print(me, f); -: 202: fprintf(f, " in map as %lu\n", (unsigned long) *p); -: 203: if (f != stderr) fclose(f); -: 204:#endif 2064: 205: return *p; -: 206: } -: 207: /* Is this status code already mapped to something else -: 208: mech-specific? */ 551: 209: mep = mecherrmap_findleft(&m, minor); 551: 210: if (mep == NULL) { -: 211: /* Map it to itself plus this mech-oid. */ 7: 212: new_status = minor; -: 213: } else { -: 214: /* Already assigned. Pick a fake new value and map it. */ -: 215: /* There's a theoretical infinite loop risk here, if we fill -: 216: in 2**32 values. Also, returning 0 has a special -: 217: meaning. */ -: 218: do { 544: 219: next_fake++; 544: 220: new_status = next_fake; -: 221: if (new_status == 0) -: 222: /* ??? */; 544: 223: } while (mecherrmap_findleft(&m, new_status) != NULL); -: 224: } 551: 225: err = mecherror_copy(&me_copy, me); 551: 226: if (err) { #####: 227: k5_mutex_unlock(&mutex); #####: 228: return err; -: 229: } 551: 230: err = mecherrmap_add(&m, new_status, me_copy); 551: 231: k5_mutex_unlock(&mutex); 551: 232: if (err) { #####: 233: if (me_copy.mech.length) #####: 234: free(me_copy.mech.elements); -: 235: } -: 236:#ifdef DEBUG -: 237: fprintf(f, "%s: mapping ", __FUNCTION__); -: 238: mecherror_print(me, f); -: 239: fprintf(f, " to %lu: err=%d\nnew map: ", (unsigned long) new_status, err); -: 240: mecherrmap_printmap(&m, f); -: 241: fprintf(f, "\n"); -: 242: if (f != stderr) fclose(f); -: 243:#endif 551: 244: if (err) #####: 245: return 0; -: 246: else 551: 247: return new_status; -: 248:} -: 249: -: 250:static gss_OID_desc no_oid = { 0, 0 }; #####: 251:OM_uint32 gssint_mecherrmap_map_errcode(OM_uint32 errcode) -: 252:{ #####: 253: return gssint_mecherrmap_map(errcode, &no_oid); -: 254:} -: 255: 7: 256:int gssint_mecherrmap_get(OM_uint32 minor, gss_OID mech_oid, -: 257: OM_uint32 *mech_minor) -: 258:{ -: 259: const struct mecherror *p; -: 260: int err; -: 261: 7: 262: if (minor == 0) { #####: 263: return EINVAL; -: 264: } 7: 265: err = k5_mutex_lock(&mutex); 7: 266: if (err) #####: 267: return err; 7: 268: p = mecherrmap_findleft(&m, minor); 7: 269: k5_mutex_unlock(&mutex); 7: 270: if (!p) { #####: 271: return EINVAL; -: 272: } 7: 273: *mech_oid = p->mech; 7: 274: *mech_minor = p->code; 7: 275: return 0; -: 276:}