-: 0:Source:ccdefname.c -: 0:Graph:/var/tsitkova/Sources/v10/trunk/src/lib/krb5/os/ccdefname.so.gcno -: 0:Data:/var/tsitkova/Sources/v10/trunk/src/lib/krb5/os/ccdefname.so.gcda -: 0:Runs:1602 -: 0:Programs:1 -: 1:/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ -: 2:/* lib/krb5/os/ccdefname.c - Return default credential cache name */ -: 3:/* -: 4: * Copyright 1990, 2007 by the Massachusetts Institute of Technology. -: 5: * All Rights Reserved. -: 6: * -: 7: * Export of this software from the United States of America may -: 8: * require a specific license from the United States Government. -: 9: * It is the responsibility of any person or organization contemplating -: 10: * export to obtain such a license before exporting. -: 11: * -: 12: * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and -: 13: * distribute this software and its documentation for any purpose and -: 14: * without fee is hereby granted, provided that the above copyright -: 15: * notice appear in all copies and that both that copyright notice and -: 16: * this permission notice appear in supporting documentation, and that -: 17: * the name of M.I.T. not be used in advertising or publicity pertaining -: 18: * to distribution of the software without specific, written prior -: 19: * permission. Furthermore if you modify this software you must label -: 20: * your software as modified software and not distribute it in such a -: 21: * fashion that it might be confused with the original M.I.T. software. -: 22: * M.I.T. makes no representations about the suitability of -: 23: * this software for any purpose. It is provided "as is" without express -: 24: * or implied warranty. -: 25: */ -: 26: -: 27:#define NEED_WINDOWS -: 28:#include "k5-int.h" -: 29:#include -: 30: -: 31:#if defined(USE_CCAPI) -: 32:#include -: 33:#endif -: 34: -: 35:#if defined(_WIN32) -: 36:static int get_from_registry_indirect(char *name_buf, int name_size) -: 37:{ -: 38: /* If the RegKRB5CCNAME variable is set, it will point to -: 39: * the registry key that has the name of the cache to use. -: 40: * The Gradient PC-DCE sets the registry key -: 41: * [HKEY_CURRENT_USER\Software\Gradient\DCE\Default\KRB5CCNAME] -: 42: * to point at the cache file name (including the FILE: prefix). -: 43: * By indirecting with the RegKRB5CCNAME entry in kerberos.ini, -: 44: * we can accomodate other versions that might set a registry -: 45: * variable. -: 46: */ -: 47: char newkey[256]; -: 48: -: 49: LONG name_buf_size; -: 50: HKEY hkey; -: 51: int found = 0; -: 52: char *cp; -: 53: -: 54: newkey[0] = 0; -: 55: GetPrivateProfileString(INI_FILES, "RegKRB5CCNAME", "", -: 56: newkey, sizeof(newkey), KERBEROS_INI); -: 57: if (!newkey[0]) -: 58: return 0; -: 59: -: 60: newkey[sizeof(newkey)-1] = 0; -: 61: cp = strrchr(newkey,'\\'); -: 62: if (cp) { -: 63: *cp = '\0'; /* split the string */ -: 64: cp++; -: 65: } else -: 66: cp = ""; -: 67: -: 68: if (RegOpenKeyEx(HKEY_CURRENT_USER, newkey, 0, -: 69: KEY_QUERY_VALUE, &hkey) != ERROR_SUCCESS) -: 70: return 0; -: 71: -: 72: name_buf_size = name_size; -: 73: if (RegQueryValueEx(hkey, cp, 0, 0, -: 74: name_buf, &name_buf_size) != ERROR_SUCCESS) -: 75: { -: 76: RegCloseKey(hkey); -: 77: return 0; -: 78: } -: 79: -: 80: RegCloseKey(hkey); -: 81: return 1; -: 82:} -: 83: -: 84:/* -: 85: * get_from_registry -: 86: * -: 87: * This will find the ccname in the registry. Returns 0 on error, non-zero -: 88: * on success. -: 89: */ -: 90: -: 91:static int -: 92:get_from_registry( -: 93: HKEY hBaseKey, -: 94: char *name_buf, -: 95: int name_size -: 96:) -: 97:{ -: 98: HKEY hKey; -: 99: DWORD name_buf_size = (DWORD)name_size; -: 100: const char *key_path = "Software\\MIT\\Kerberos5"; -: 101: const char *value_name = "ccname"; -: 102: -: 103: if (RegOpenKeyEx(hBaseKey, key_path, 0, KEY_QUERY_VALUE, -: 104: &hKey) != ERROR_SUCCESS) -: 105: return 0; -: 106: if (RegQueryValueEx(hKey, value_name, 0, 0, -: 107: name_buf, &name_buf_size) != ERROR_SUCCESS) -: 108: { -: 109: RegCloseKey(hKey); -: 110: return 0; -: 111: } -: 112: RegCloseKey(hKey); -: 113: return 1; -: 114:} -: 115: -: 116:#define APPEND_KRB5CC "\\krb5cc" -: 117: -: 118:static int -: 119:try_dir( -: 120: char* dir, -: 121: char* buffer, -: 122: int buf_len -: 123:) -: 124:{ -: 125: struct _stat s; -: 126: if (!dir) -: 127: return 0; -: 128: if (_stat(dir, &s)) -: 129: return 0; -: 130: if (!(s.st_mode & _S_IFDIR)) -: 131: return 0; -: 132: if (buffer != dir) { -: 133: strncpy(buffer, dir, buf_len); -: 134: buffer[buf_len-1]='\0'; -: 135: } -: 136: strncat(buffer, APPEND_KRB5CC, buf_len-strlen(buffer)); -: 137: buffer[buf_len-1] = '\0'; -: 138: return 1; -: 139:} -: 140:#endif -: 141: -: 142:#if defined(_WIN32) -: 143:static krb5_error_code get_from_os(char *name_buf, unsigned int name_size) -: 144:{ -: 145: char *prefix = krb5_cc_dfl_ops->prefix; -: 146: int size; -: 147: char *p; -: 148: DWORD gle; -: 149: -: 150: SetLastError(0); -: 151: GetEnvironmentVariable(KRB5_ENV_CCNAME, name_buf, name_size); -: 152: gle = GetLastError(); -: 153: if (gle == 0) -: 154: return 0; -: 155: else if (gle != ERROR_ENVVAR_NOT_FOUND) -: 156: return ENOMEM; -: 157: -: 158: if (get_from_registry(HKEY_CURRENT_USER, -: 159: name_buf, name_size) != 0) -: 160: return 0; -: 161: -: 162: if (get_from_registry(HKEY_LOCAL_MACHINE, -: 163: name_buf, name_size) != 0) -: 164: return 0; -: 165: -: 166: if (get_from_registry_indirect(name_buf, name_size) != 0) -: 167: return 0; -: 168: -: 169: strncpy(name_buf, prefix, name_size - 1); -: 170: name_buf[name_size - 1] = 0; -: 171: size = name_size - strlen(prefix); -: 172: if (size > 0) -: 173: strcat(name_buf, ":"); -: 174: size--; -: 175: p = name_buf + name_size - size; -: 176: if (!strcmp(prefix, "API")) { -: 177: strncpy(p, "krb5cc", size); -: 178: } else if (!strcmp(prefix, "FILE") || !strcmp(prefix, "STDIO")) { -: 179: if (!try_dir(getenv("TEMP"), p, size) && -: 180: !try_dir(getenv("TMP"), p, size)) -: 181: { -: 182: int len = GetWindowsDirectory(p, size); -: 183: name_buf[name_size - 1] = 0; -: 184: if (len < size - sizeof(APPEND_KRB5CC)) -: 185: strcat(p, APPEND_KRB5CC); -: 186: } -: 187: } else { -: 188: strncpy(p, "default_cache_name", size); -: 189: } -: 190: name_buf[name_size - 1] = 0; -: 191: return 0; -: 192:} -: 193:#endif -: 194: -: 195:#if defined(USE_CCAPI) -: 196: -: 197:static krb5_error_code get_from_os(char *name_buf, unsigned int name_size) -: 198:{ -: 199: krb5_error_code result = 0; -: 200: cc_context_t cc_context = NULL; -: 201: cc_string_t default_name = NULL; -: 202: -: 203: cc_int32 ccerr = cc_initialize (&cc_context, ccapi_version_3, NULL, NULL); -: 204: if (ccerr == ccNoError) { -: 205: ccerr = cc_context_get_default_ccache_name (cc_context, &default_name); -: 206: } -: 207: -: 208: if (ccerr == ccNoError) { -: 209: if (strlen (default_name -> data) + 5 > name_size) { -: 210: result = ENOMEM; -: 211: goto cleanup; -: 212: } else { -: 213: snprintf (name_buf, name_size, "API:%s", -: 214: default_name -> data); -: 215: } -: 216: } -: 217: -: 218:cleanup: -: 219: if (cc_context != NULL) { -: 220: cc_context_release (cc_context); -: 221: } -: 222: -: 223: if (default_name != NULL) { -: 224: cc_string_release (default_name); -: 225: } -: 226: -: 227: return result; -: 228:} -: 229: -: 230:#else -: 231:#if !(defined(_WIN32)) #####: 232:static krb5_error_code get_from_os(char *name_buf, unsigned int name_size) -: 233:{ #####: 234: snprintf(name_buf, name_size, "FILE:/tmp/krb5cc_%ld", (long) getuid()); #####: 235: return 0; -: 236:} -: 237:#endif -: 238:#endif -: 239: -: 240:krb5_error_code KRB5_CALLCONV 1301: 241:krb5_cc_set_default_name(krb5_context context, const char *name) -: 242:{ 1301: 243: krb5_error_code err = 0; 1301: 244: char *new_ccname = NULL; -: 245: 1301: 246: if (!context || context->magic != KV5M_CONTEXT) { err = KV5M_CONTEXT; } -: 247: 1301: 248: if (name != NULL) { 937: 249: if (!err) { -: 250: /* If the name isn't NULL, make a copy of it */ 937: 251: new_ccname = strdup (name); 937: 252: if (new_ccname == NULL) { err = ENOMEM; } -: 253: } -: 254: } -: 255: 1301: 256: if (!err) { -: 257: /* free the old ccname and store the new one */ 1301: 258: krb5_os_context os_ctx = &context->os_context; 1301: 259: if (os_ctx->default_ccname) { free (os_ctx->default_ccname); } 1301: 260: os_ctx->default_ccname = new_ccname; 1301: 261: new_ccname = NULL; /* don't free */ -: 262: } -: 263: 1301: 264: return err; -: 265:} -: 266: -: 267: -: 268:const char * KRB5_CALLCONV 1176: 269:krb5_cc_default_name(krb5_context context) -: 270:{ 1176: 271: krb5_error_code err = 0; 1176: 272: krb5_os_context os_ctx = NULL; -: 273: 1176: 274: if (!context || context->magic != KV5M_CONTEXT) { err = KV5M_CONTEXT; } -: 275: 1176: 276: if (!err) { 1176: 277: os_ctx = &context->os_context; -: 278: 1176: 279: if (os_ctx->default_ccname == NULL) { -: 280: /* Default ccache name has not been set yet */ 863: 281: char *new_ccname = NULL; -: 282: char new_ccbuf[1024]; -: 283: -: 284: /* try the environment variable first */ 863: 285: new_ccname = getenv(KRB5_ENV_CCNAME); -: 286: 863: 287: if (new_ccname == NULL) { -: 288: /* fall back on the default ccache name for the OS */ #####: 289: new_ccname = new_ccbuf; #####: 290: err = get_from_os (new_ccbuf, sizeof (new_ccbuf)); -: 291: } -: 292: 863: 293: if (!err) { 863: 294: err = krb5_cc_set_default_name (context, new_ccname); -: 295: } -: 296: } -: 297: } -: 298: 1176: 299: return err ? NULL : os_ctx->default_ccname; -: 300:} -: 301: -: 302:/* -: 303: * caller must free name -: 304: */ -: 305:krb5_error_code #####: 306:krb5int_cc_os_default_name(krb5_context context, char **name) -: 307:{ #####: 308: krb5_error_code retval = 0; #####: 309: char *tmpname = NULL; -: 310: #####: 311: *name = NULL; #####: 312: tmpname = malloc(BUFSIZ); #####: 313: if (tmpname == NULL) #####: 314: return ENOMEM; -: 315: #####: 316: retval = get_from_os(tmpname, BUFSIZ); #####: 317: *name = tmpname; #####: 318: return retval; -: 319:}