1/* 2 * lookupDSLocalKDC.c 3 * KerberosHelper 4 */ 5/* 6 * Copyright (c) 2006-2007 Apple Inc. All rights reserved. 7 * 8 * @APPLE_LICENSE_HEADER_START@ 9 * 10 * This file contains Original Code and/or Modifications of Original Code 11 * as defined in and that are subject to the Apple Public Source License 12 * Version 2.0 (the 'License'). You may not use this file except in 13 * compliance with the License. Please obtain a copy of the License at 14 * http://www.opensource.apple.com/apsl/ and read it before using this 15 * file. 16 * 17 * The Original Code and all software distributed under the License are 18 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 19 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 20 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 21 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 22 * Please see the License for the specific language governing rights and 23 * limitations under the License. 24 * 25 * @APPLE_LICENSE_HEADER_END@ 26 */ 27 28 29#include "KerberosHelper.h" 30#include "KerberosHelperContext.h" 31 32#include "lookupDSLocalKDC.h" 33#include <CoreFoundation/CoreFoundation.h> 34 35#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5 36 37#include <Carbon/Carbon.h> 38#include <OpenDirectory/OpenDirectory.h> 39#include <DirectoryService/DirectoryService.h> 40 41OSStatus DSCopyLocalKDC (CFStringRef *realm) 42{ 43 OSStatus err = 0; 44 ODNodeRef cfNodeRef = NULL; 45 ODRecordRef cfRecord = NULL; 46 CFArrayRef cfLKDCName = NULL; 47 CFIndex limit; 48 CFTypeRef realmName; 49 50 if (NULL == realm) { err = paramErr; goto Error; } 51 52 *realm = NULL; 53 54 cfNodeRef = ODNodeCreateWithNodeType( kCFAllocatorDefault, kODSessionDefault, kODNodeTypeAuthentication, NULL ); 55 56 if ( NULL == cfNodeRef ) { err = paramErr; goto Error; } 57 58 cfRecord = ODNodeCopyRecord( cfNodeRef, CFSTR(kDSStdRecordTypeConfig), CFSTR(kKDCRecordName), NULL, NULL ); 59 60 if( NULL == cfRecord ) { err = paramErr; goto Error; } 61 62 cfLKDCName = ODRecordCopyValues ( cfRecord, CFSTR(kRealmNameKey), NULL); 63 64 if ( NULL == cfLKDCName ) { err = paramErr; goto Error; } 65 66 limit = CFArrayGetCount (cfLKDCName); 67 68 if (1 != limit) { err = paramErr; goto Error; } 69 70 realmName = CFArrayGetValueAtIndex (cfLKDCName, 0); 71 72 if (CFStringGetTypeID () != CFGetTypeID (realmName)) { err = paramErr; goto Error; } 73 74 *realm = CFRetain (realmName); 75 76Error: 77 if (cfLKDCName) { CFRelease( cfLKDCName ); } 78 if (cfRecord) { CFRelease( cfRecord ); } 79 if (cfNodeRef) { CFRelease( cfNodeRef ); } 80 81 return err; 82} 83 84#else 85 86#warning On Mac OS X 10.4 87 88#define kLocalKDCRealmFile "/var/db/realm.local" 89#include <sys/types.h> 90#include <sys/stat.h> 91#include <fcntl.h> 92#include <unistd.h> 93 94OSStatus DSCopyLocalKDC (CFStringRef *realm) 95{ 96 char *endRealm = NULL, *realmString = NULL; 97 struct stat realmFileSB; 98 int fd; 99 size_t wasRead, realmSize; 100 101 if (NULL == realm) { err = paramErr; goto Error; } 102 103 *realm = NULL; 104 105 if (0 != stat (kLocalKDCRealmFile, &realmFileSB)) { err = paramErr; goto Error; } 106 107 /* The LKDC: line should only be 51 characters in size */ 108 if (realmFileSB.st_size > 64) { err = paramErr; goto Error; } 109 110 realmSize = realmFileSB.st_size + 1; 111 112 realmString = malloc (realmSize); 113 114 fd = open (kLocalKDCRealmFile, O_RDONLY); 115 116 if (0 == fd || NULL == realmString) { err = paramErr; goto Error; } 117 118 wasRead = read (fd, realmString, realmFileSB.st_size); 119 120 close (fd); 121 122 if (wasRead != realmFileSB.st_size) { err = paramErr; goto Error; } 123 124 /* Make sure the buffer is null terminated */ 125 realmString [realmSize] = '\0'; 126 127 endRealm = strchr (realmString, '\n'); 128 129 /* Trim the trailing newline */ 130 if (NULL != endRealm) { *endRealm = '\0'; } 131 132 *realm = CFStringCreateWithCString (NULL, realmString, kCFStringEncodingASCII); 133 134Error: 135 if (realmString) { free (realmString); } 136 137 return err; 138} 139 140#endif 141