diff options
Diffstat (limited to 'src/lib/krb5/os/init_os_ctx.c')
-rw-r--r-- | src/lib/krb5/os/init_os_ctx.c | 137 |
1 files changed, 87 insertions, 50 deletions
diff --git a/src/lib/krb5/os/init_os_ctx.c b/src/lib/krb5/os/init_os_ctx.c index 5222e6a..f599697 100644 --- a/src/lib/krb5/os/init_os_ctx.c +++ b/src/lib/krb5/os/init_os_ctx.c @@ -30,22 +30,7 @@ #include "k5-int.h" #ifdef macintosh -OSErr -GetMacProfileFileSpec (FSSpec* outFileSpec, StringPtr inName, UInt32 whichFolder) -{ - OSErr err; - - - - err = FindFolder (kOnSystemDisk, whichFolder, kCreateFolder, - &(outFileSpec -> vRefNum) , &(outFileSpec -> parID)); - - if (err == noErr) { - BlockMoveData (inName, &(outFileSpec -> name), strlen (inName) + 1); - } - - return err; -} +#include <PreferencesLib.h> #endif /* macintosh */ #if defined(_MSDOS) || defined(_WIN32) @@ -204,42 +189,94 @@ os_get_default_config_files(pfiles, secure) { profile_filespec_t* files; #ifdef macintosh - files = malloc(7 * sizeof(FSSpec)); - - if (files != 0) { - OSErr err = GetMacProfileFileSpec(&(files [3]), "\pKerberos Preferences", kApplicationSupportFolderType); - if (err == noErr) { - err = GetMacProfileFileSpec( &(files [4]), "\pkrb5.ini", kApplicationSupportFolderType); - } - if (err == noErr) { - err = GetMacProfileFileSpec( &(files [5]), "\pKerberos5 Configuration", kApplicationSupportFolderType); - } - - if (err == noErr) { - files[6].vRefNum = 0; - files[6].parID = 0; - files[6].name[0] = '\0'; - } else { - files[3].vRefNum = 0; - files[3].parID = 0; - files[3].name[0] = '\0'; + FSSpec* preferencesFiles = nil; + UInt32 numPreferencesFiles; + FSSpec* preferencesFilesToInit = nil; + UInt32 numPreferencesFilesToInit; + UInt32 i; + Boolean foundPreferences = false; + Boolean writtenPreferences = false; + SInt16 refNum = -1; + SInt32 length = 0; + + OSErr err = KPGetListOfPreferencesFiles ( + secure ? kpSystemPreferences : kpUserPreferences | kpSystemPreferences, + &preferencesFiles, + &numPreferencesFiles); + + if (err == noErr) { + /* After we get the list of files, check whether any of them contain any useful information */ + for (i = 0; i < numPreferencesFiles; i++) { + if (KPPreferencesFileIsReadable (&preferencesFiles [i]) == noErr) { + /* It's readable, check if it has anything in the data fork */ + err = FSpOpenDF (&preferencesFiles [i], fsRdPerm, &refNum); + if (err == noErr) { + err = GetEOF (refNum, &length); + } + + if (refNum != -1) { + FSClose (refNum); + } + + if (length != 0) { + foundPreferences = true; + break; + } + } } - err = GetMacProfileFileSpec(&(files [0]), "\pKerberos Preferences", kPreferencesFolderType); - if (err == noErr) { - err = GetMacProfileFileSpec( &(files [1]), "\pkrb5.ini", kPreferencesFolderType); - } - if (err == noErr) { - err = GetMacProfileFileSpec( &(files [2]), "\pKerberos5 Configuration", kPreferencesFolderType); + if (!foundPreferences) { + /* We found no profile data in any of those files; try to initialize one */ + /* If we are running "secure" do not try to initialize preferences */ + if (!secure) { + err = KPGetListOfPreferencesFiles (kpUserPreferences, &preferencesFilesToInit, &numPreferencesFilesToInit); + if (err == noErr) { + for (i = 0; i < numPreferencesFilesToInit; i++) { + if (KPPreferencesFileIsWritable (&preferencesFilesToInit [i]) == noErr) { + err = noErr; + /* If not readable, create it */ + if (KPPreferencesFileIsReadable (&preferencesFilesToInit [i]) != noErr) { + err = KPCreatePreferencesFile (&preferencesFilesToInit [i]); + } + /* Initialize it */ + if (err == noErr) { + err = KPInitializeWithDefaultKerberosLibraryPreferences (&preferencesFilesToInit [i]); + } + break; + } + } + } + } } + } + + if (err == noErr) { + files = malloc ((numPreferencesFiles + 1) * sizeof (FSSpec)); + if (files == NULL) + err = memFullErr; + } + + if (err == noErr) { + for (i = 0; i < numPreferencesFiles; i++) { + files [i] = preferencesFiles [i]; + } + + files [numPreferencesFiles].vRefNum = 0; + files [numPreferencesFiles].parID = 0; + files [numPreferencesFiles].name[0] = '\0'; + } + + if (preferencesFiles != nil) + KPFreeListOfPreferencesFiles (preferencesFiles); + + if (preferencesFilesToInit != nil) + KPFreeListOfPreferencesFiles (preferencesFilesToInit); - if (err != noErr) { - free (files); - return ENFILE; - } - } else { + if (err == memFullErr) return ENOMEM; - } + else if (err != noErr) + return ENOENT; + #else /* !macintosh */ #if defined(_MSDOS) || defined(_WIN32) krb5_error_code retval = 0; @@ -405,7 +442,7 @@ krb5_os_init_context(ctx) os_ctx->os_flags = 0; os_ctx->default_ccname = 0; os_ctx->default_ccprincipal = 0; - + krb5_cc_set_default_name(ctx, NULL); retval = os_init_paths(ctx); @@ -522,8 +559,8 @@ krb5_os_free_context(ctx) if (os_ctx->default_ccname) { free(os_ctx->default_ccname); - os_ctx->default_ccname = 0; - } + os_ctx->default_ccname = 0; + } if (os_ctx->default_ccprincipal) { krb5_free_principal (ctx, os_ctx->default_ccprincipal); |