KerberosServiceSetup.cpp [plain text]
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/syslog.h>
#include <sys/uio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <CoreFoundation/CoreFoundation.h>
#include "KerberosServiceSetup.h"
#include "FTPAccessFile.h"
#include <smb_server_prefs.h>
#include <SystemConfiguration/SystemConfiguration.h>
#define kAFPConfigPath "/Library/Preferences/com.apple.AppleFileServer.plist"
#define kAFPPrincipalKey "kerberosPrincipal"
#define kAFPPidFilePath "/var/run/AppleFileServer.pid"
#define kFTPConfigPath "/Library/FTPServer/Configuration/ftpaccess"
#define kFTPPrincipalKey "ktb5_principal"
#define kFTPPidFilePath "/var/run/"
#define kMailConfigPath "/etc/MailServicesOther.plist"
#define kSMTPConfigKey "postfix"
#define kSMTPPrincipalKey "smtp_principal"
#define kIMAPConfigKey "cyrus"
#define kIMAPPrincipalKey "imap_principal"
#define kPOPConfigKey "cyrus"
#define kPOPPrincipalKey "pop_principal"
#define kSMBConfigTool "/usr/share/servermgrd/cgi-bin/servermgr_smb"
#define kVPNConfigPath "/Library/Preferences/SystemConfiguration/com.apple.RemoteAccessServers.plist"
#define kVPNPrincipalKey "KerberosServicePrincipalName"
#define kVPNServerKey "Servers"
#define kVPNServiceKey "com.apple.ppp.l2tp"
#define kVPNEAPKey "EAP"
#define kXGridConfigPath "/etc/xgrid/controller/service-principal"
#define kApacheConfigTool "/usr/sbin/apache-kerberos"
static CFDictionaryRef CreateMyPropertyListFromFile( CFURLRef fileURL, Boolean isMutable);
static CFMutableDictionaryRef CreateVPNDefaults(void);
static CFErrorRef OpenXGridPrincipalFile(int openFlags, int *configFileRef);
static CFMutableDictionaryRef CreateDictionaryFromFD(int inFd, Boolean inMakeMutable);
static CFErrorRef MyCFErrorCreate(CFStringRef domain, CFIndex code, CFStringRef description);
#pragma mark -
#pragma mark File Services
#pragma mark -
CFErrorRef SetAFPPrincipal(CFStringRef inPrincipal)
{
CFErrorRef theError = NULL;
CFMutableDictionaryRef theConfig = NULL;
CFURLRef thePathURL = NULL;
CFDataRef theData = NULL;
CFStringRef logString = NULL;
const void *keys[1];
const void *values[1];
CFIndex dataLength;
int fd;
pid_t serverPid = 0;
size_t len;
UInt8 theBuffer[16];
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
thePathURL = CFURLCreateFromFileSystemRepresentation(NULL, (UInt8 *)kAFPConfigPath, strlen(kAFPConfigPath), false);
theConfig = (CFMutableDictionaryRef) CreateMyPropertyListFromFile(thePathURL, true);
if (theConfig != NULL)
{
CFDictionarySetValue(theConfig, CFSTR(kAFPPrincipalKey), inPrincipal);
} else {
keys[0] = CFSTR(kAFPPrincipalKey);
values[0] = inPrincipal;
theConfig = (CFMutableDictionaryRef)CFDictionaryCreate(NULL, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
}
theData = CFPropertyListCreateXMLData(NULL, theConfig);
if (theData != NULL)
{
dataLength = CFDataGetLength(theData);
if ((fd = open(kAFPConfigPath, O_RDWR | O_CREAT | O_TRUNC, mode)) == -1)
{
logString = CFStringCreateWithFormat(NULL, NULL, CFSTR("Error writing AFP config file at %s error = %d\n"),
kAFPConfigPath, errno);
theError = MyCFErrorCreate(kCFErrorDomainPOSIX, (CFIndex)errno, logString);
if ( logString != NULL )
CFRelease( logString );
}
else
{
write(fd, CFDataGetBytePtr(theData), dataLength);
close(fd);
}
CFRelease(theData);
}
CFRelease(theConfig);
if (theError == NULL)
{
bzero(theBuffer, sizeof(theBuffer));
fd = open(kAFPPidFilePath, O_RDONLY, 0); if (fd != -1) {
len = read(fd, theBuffer, sizeof(theBuffer));
if (len != 0)
serverPid = atoi((char *)theBuffer);
close(fd);
if (serverPid > 0)
kill(serverPid,SIGHUP);
}
}
return theError;
}
CFErrorRef SetFTPPrincipal(CFStringRef inPrincipal)
{
CFErrorRef theError = NULL;
char buffer[1024];
if ( CFStringGetCString(inPrincipal, buffer, sizeof(buffer), kCFStringEncodingASCII) == false )
return MyCFErrorCreate(kCFErrorDomainPOSIX, (CFIndex)-2, NULL);
try
{
FTPAccessFile *configFile = (FTPAccessFile *) new FTPAccessFile();
configFile->SetKerberosPrincipal(buffer);
delete configFile;
}
catch(...)
{
theError = MyCFErrorCreate(kCFErrorDomainPOSIX, (CFIndex)-1, NULL);
}
return theError;
}
CFErrorRef SetNFSPrincipal(CFStringRef inPrincipal)
{
return NULL;
}
CFErrorRef SetSMBPrincipal(CFStringRef inPrincipal, CFStringRef inAdminName, const char *inPassword)
{
CFErrorRef theError = NULL;
CFArrayRef tmpArray = NULL;
SInt32 tmpInt;
SCPreferencesRef prefs = NULL;
prefs = SCPreferencesCreate(NULL, CFSTR("Password Server Plugin"), CFSTR(kSMBPreferencesAppID));
if (NULL == prefs) {
theError = MyCFErrorCreate(kCFErrorDomainPOSIX, (CFIndex)-1, CFSTR("SetSMBPrincipal: Cannot create SMB preferences\n"));
} else {
tmpArray = CFStringCreateArrayBySeparatingStrings(NULL, inPrincipal, CFSTR("@"));
tmpInt = CFArrayGetCount(tmpArray);
if (tmpInt != 2)
{
CFRelease(tmpArray);
tmpArray = NULL;
theError = MyCFErrorCreate(kCFErrorDomainPOSIX, (CFIndex)-1, CFSTR("SetSMBPrincipal: Cannot find the realm name\n"));
}
else
{
CFStringRef newValueStrRef = (CFStringRef)CFArrayGetValueAtIndex(tmpArray, tmpInt-1);
CFPropertyListRef cfpRealmRef = NULL;
cfpRealmRef = newValueStrRef;
if (SCPreferencesSetValue(prefs, CFSTR(kSMBPrefKerberosRealm), cfpRealmRef)) {
if (!SCPreferencesCommitChanges(prefs)) {
syslog(LOG_ERR,"Error in adding the SMB principal");
theError = MyCFErrorCreate(kCFErrorDomainPOSIX, (CFIndex)-1, CFSTR("SetSMBPrincipal: Cannot set the SMB Principal name\n"));
}
else
SCPreferencesApplyChanges(prefs);
}
}
CFRelease(prefs);
CFRelease(tmpArray);
}
return theError;
}
#pragma mark -
#pragma mark Mail Services
#pragma mark -
CFErrorRef SetIMAPPrincipal(CFStringRef inPrincipal)
{
CFErrorRef theError = NULL;
CFMutableDictionaryRef theConfig = NULL;
CFMutableDictionaryRef theService = NULL;
CFURLRef thePathURL = NULL;
CFDataRef theData = NULL;
CFStringRef logString = NULL;
const void *keys[1];
const void *values[1];
CFIndex dataLength;
int fd;
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
thePathURL = CFURLCreateFromFileSystemRepresentation(NULL, (UInt8 *)kMailConfigPath, strlen(kMailConfigPath), false);
theConfig = (CFMutableDictionaryRef)CreateMyPropertyListFromFile(thePathURL, true);
CFRelease( thePathURL );
thePathURL = NULL;
if (theConfig != NULL)
{
theService = (CFMutableDictionaryRef)CFDictionaryGetValue(theConfig, CFSTR(kIMAPConfigKey)); if (theService == NULL)
{
keys[0] = CFSTR(kIMAPPrincipalKey);
values[0] = inPrincipal;
theService = (CFMutableDictionaryRef)CFDictionaryCreate(NULL, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(theConfig, CFSTR(kIMAPConfigKey), theService);
}
else
{
CFDictionarySetValue(theService, CFSTR(kIMAPPrincipalKey), inPrincipal);
}
}
else
{
keys[0] = CFSTR(kIMAPPrincipalKey);
values[0] = inPrincipal;
theService = (CFMutableDictionaryRef)CFDictionaryCreate(NULL, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
keys[0] = CFSTR(kIMAPConfigKey);
values[0] = theService;
theConfig = (CFMutableDictionaryRef)CFDictionaryCreate(NULL, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
}
theData = CFPropertyListCreateXMLData(NULL, theConfig);
if (theData != NULL)
{
dataLength = CFDataGetLength(theData);
if ((fd = open(kMailConfigPath, O_RDWR | O_CREAT | O_TRUNC, mode)) == -1)
{
logString = CFStringCreateWithFormat(NULL, NULL, CFSTR("Error writing Mail config file at %s error = %d\n"),
kMailConfigPath, errno);
theError = MyCFErrorCreate(kCFErrorDomainPOSIX, (CFIndex)errno, logString);
if ( logString != NULL )
CFRelease( logString );
}
else
{
write(fd, CFDataGetBytePtr(theData), dataLength);
close(fd);
}
CFRelease(theData);
}
CFRelease(theConfig);
return theError;
}
CFErrorRef SetPOPPrincipal(CFStringRef inPrincipal)
{
CFErrorRef theError = NULL;
CFMutableDictionaryRef theConfig = NULL;
CFMutableDictionaryRef theService = NULL;
CFURLRef thePathURL = NULL;
CFDataRef theData = NULL;
CFStringRef logString = NULL;
const void *keys[1];
const void *values[1];
CFIndex dataLength;
int fd;
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
thePathURL = CFURLCreateFromFileSystemRepresentation(NULL, (UInt8 *)kMailConfigPath, strlen(kMailConfigPath), false);
theConfig = (CFMutableDictionaryRef)CreateMyPropertyListFromFile(thePathURL, true);
CFRelease( thePathURL );
thePathURL = NULL;
if (theConfig != NULL)
{
theService = (CFMutableDictionaryRef)CFDictionaryGetValue(theConfig, CFSTR(kPOPConfigKey)); if (theService == NULL)
{
keys[0] = CFSTR(kPOPPrincipalKey);
values[0] = inPrincipal;
theService = (CFMutableDictionaryRef)CFDictionaryCreate(NULL, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(theConfig, CFSTR(kPOPConfigKey), theService);
} else {
CFDictionarySetValue(theService, CFSTR(kPOPPrincipalKey), inPrincipal);
}
} else {
keys[0] = CFSTR(kPOPPrincipalKey);
values[0] = inPrincipal;
theService = (CFMutableDictionaryRef)CFDictionaryCreate(NULL, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
keys[0] = CFSTR(kPOPConfigKey);
values[0] = theService;
theConfig = (CFMutableDictionaryRef)CFDictionaryCreate(NULL, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
}
theData = CFPropertyListCreateXMLData(NULL, theConfig);
if (theData != NULL)
{
dataLength = CFDataGetLength(theData);
if ((fd = open(kMailConfigPath, O_RDWR | O_CREAT | O_TRUNC, mode)) == -1)
{
logString = CFStringCreateWithFormat(NULL, NULL, CFSTR("Error writing Mail config file at %s error = %d\n"),
kMailConfigPath, errno);
theError = MyCFErrorCreate(kCFErrorDomainPOSIX, (CFIndex)errno, logString);
if ( logString != NULL )
CFRelease( logString );
}
else
{
write(fd, CFDataGetBytePtr(theData), dataLength);
close(fd);
}
CFRelease(theData);
}
CFRelease(theConfig);
return theError;
}
CFErrorRef SetSMTPPrincipal(CFStringRef inPrincipal)
{
CFErrorRef theError = NULL;
CFMutableDictionaryRef theConfig = NULL;
CFMutableDictionaryRef theService = NULL;
CFURLRef thePathURL = NULL;
CFDataRef theData = NULL;
CFStringRef logString = NULL;
const void *keys[1];
const void *values[1];
CFIndex dataLength;
int fd;
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
thePathURL = CFURLCreateFromFileSystemRepresentation(NULL, (UInt8 *)kMailConfigPath, strlen(kMailConfigPath), false);
theConfig = (CFMutableDictionaryRef)CreateMyPropertyListFromFile(thePathURL, true);
CFRelease( thePathURL );
thePathURL = NULL;
if (theConfig != NULL)
{
theService = (CFMutableDictionaryRef)CFDictionaryGetValue(theConfig, CFSTR(kSMTPConfigKey)); if (theService == NULL)
{
keys[0] = CFSTR(kIMAPPrincipalKey);
values[0] = inPrincipal;
theService = (CFMutableDictionaryRef)CFDictionaryCreate(NULL, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(theConfig, CFSTR(kSMTPConfigKey), theService);
} else {
CFDictionarySetValue(theService, CFSTR(kSMTPPrincipalKey), inPrincipal);
}
} else {
keys[0] = CFSTR(kSMTPPrincipalKey);
values[0] = inPrincipal;
theService = (CFMutableDictionaryRef)CFDictionaryCreate(NULL, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
keys[0] = CFSTR(kSMTPConfigKey);
values[0] = theService;
theConfig = (CFMutableDictionaryRef)CFDictionaryCreate(NULL, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
}
theData = CFPropertyListCreateXMLData(NULL, theConfig);
if (theData != NULL)
{
dataLength = CFDataGetLength(theData);
if ((fd = open(kMailConfigPath, O_RDWR | O_CREAT | O_TRUNC, mode)) == -1)
{
logString = CFStringCreateWithFormat(NULL, NULL, CFSTR("Error writing Mail config file at %s error = %d\n"),
kMailConfigPath, errno);
theError = MyCFErrorCreate(kCFErrorDomainPOSIX, (CFIndex)errno, logString);
if ( logString != NULL )
CFRelease( logString );
}
else
{
write(fd, CFDataGetBytePtr(theData), dataLength);
close(fd);
}
CFRelease(theData);
}
CFRelease(theConfig);
return theError;
}
#pragma mark -
#pragma mark More Services
#pragma mark -
CFErrorRef SetSSHPrincipal(CFStringRef inPrincipal)
{
return NULL;
}
CFErrorRef SetLDAPPrincipal(CFStringRef inPrincipal)
{
return NULL;
}
CFErrorRef SetHTTPPrincipal(CFStringRef inPrincipal)
{
return NULL;
}
CFErrorRef SetIPPPrincipal(CFStringRef inPrincipal)
{
return NULL;
}
CFErrorRef SetJABBERPrincipal(CFStringRef inPrincipal)
{
return NULL;
}
CFErrorRef SetVNCPrincipal(CFStringRef inPrincipal)
{
return NULL;
}
CFErrorRef SetVPNPrincipal(CFStringRef inPrincipal)
{
CFMutableDictionaryRef workingDict = NULL;
CFMutableDictionaryRef tmpDict1 = NULL;
CFMutableDictionaryRef tmpDict2 = NULL;
CFMutableDictionaryRef tmpDict3 = NULL;
CFDataRef theData = NULL;
CFErrorRef theError = NULL;
CFStringRef logString = NULL;
int theConfigFile;
CFIndex dataLength = 0;
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; struct stat fileInfo;
bool releaseTmpDict3 = false;
theConfigFile = open(kVPNConfigPath, O_CREAT | O_EXLOCK | O_RDWR, mode);
if (theConfigFile == -1)
{
logString = CFStringCreateWithFormat(NULL, NULL, CFSTR("SetVPNPrincipal: could not open/create %s errno = %d\n"),
kVPNConfigPath, errno);
theError = MyCFErrorCreate(kCFErrorDomainPOSIX, (CFIndex)errno, logString);
if ( logString != NULL )
CFRelease( logString );
return theError;
}
if (fstat(theConfigFile, &fileInfo) == -1)
{
logString = CFStringCreateWithFormat(NULL, NULL, CFSTR("SetVPNPrincipal: could not stat %s errno = %d\n"),
kVPNConfigPath, errno);
theError = MyCFErrorCreate(kCFErrorDomainPOSIX, (CFIndex)errno, logString);
if ( logString != NULL )
CFRelease( logString );
close(theConfigFile); return theError;
}
if (fileInfo.st_size == 0) {
workingDict = CreateVPNDefaults();
}
else
{
workingDict = CreateDictionaryFromFD(theConfigFile, true);
}
if (workingDict == NULL)
{
logString = CFStringCreateWithFormat(NULL, NULL, CFSTR("SetVPNPrincipal: File is not a recognizable config file %s\n"),
kVPNConfigPath, EINVAL);
theError = MyCFErrorCreate(kCFErrorDomainPOSIX, (CFIndex)EINVAL, logString);
if ( logString != NULL )
CFRelease( logString );
close(theConfigFile);
return theError;
}
tmpDict1 = (CFMutableDictionaryRef)CFDictionaryGetValue(workingDict, CFSTR(kVPNServerKey));
if (tmpDict1 == NULL)
{
logString = CFStringCreateWithFormat(NULL, NULL, CFSTR("SetVPNPrincipal: File is not a recognizable config file %s\n"),
kVPNConfigPath, EINVAL);
theError = MyCFErrorCreate(kCFErrorDomainPOSIX, (CFIndex)EINVAL, logString);
if ( logString != NULL )
CFRelease( logString );
close(theConfigFile);
CFRelease(workingDict);
return theError;
}
tmpDict2 = (CFMutableDictionaryRef)CFDictionaryGetValue(tmpDict1, CFSTR(kVPNServiceKey));
if (tmpDict2 == NULL)
{
logString = CFStringCreateWithFormat(NULL, NULL, CFSTR("SetVPNPrincipal: File is not a recognizable config file %s\n"),
kVPNConfigPath, EINVAL);
theError = MyCFErrorCreate(kCFErrorDomainPOSIX, (CFIndex)EINVAL, logString);
if ( logString != NULL )
CFRelease( logString );
close(theConfigFile);
CFRelease(workingDict);
return theError;
}
tmpDict3 = (CFMutableDictionaryRef)CFDictionaryGetValue(tmpDict2, CFSTR(kVPNEAPKey));
if (tmpDict3 == NULL) {
tmpDict3 = CFDictionaryCreateMutable(NULL, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionaryAddValue(tmpDict2, CFSTR(kVPNEAPKey), tmpDict3);
releaseTmpDict3 = true;
}
CFDictionarySetValue(tmpDict3, CFSTR(kVPNPrincipalKey), inPrincipal);
if (releaseTmpDict3)
{
CFRelease(tmpDict3); }
theData = CFPropertyListCreateXMLData(NULL, workingDict);
if (theData != NULL)
{
dataLength = CFDataGetLength(theData);
lseek(theConfigFile, 0, SEEK_SET); if (write(theConfigFile, CFDataGetBytePtr(theData), dataLength) != dataLength) {
logString = CFStringCreateWithFormat(NULL, NULL, CFSTR("SetVPNPrincipal: failed to write %s, errno = %d\n"),
kVPNConfigPath, theError);
theError = MyCFErrorCreate(kCFErrorDomainPOSIX, (CFIndex)errno, logString);
if ( logString != NULL )
CFRelease( logString );
close(theConfigFile); CFRelease(theData);
CFRelease(workingDict);
return theError;
}
CFRelease(theData);
}
else
{
close(theConfigFile);
CFRelease(workingDict);
return MyCFErrorCreate(kCFErrorDomainPOSIX, (CFIndex)EIO, NULL);
}
close(theConfigFile);
CFRelease(workingDict);
return NULL;
}
CFErrorRef SetXGridPrincipal(CFStringRef inPrincipal)
{
CFErrorRef theError = NULL;
CFStringRef logString = NULL;
UInt8 buffer[1024];
size_t length;
int theConfigFile = -1;
theError = OpenXGridPrincipalFile(O_CREAT | O_EXLOCK | O_RDWR | O_TRUNC, &theConfigFile);
if ( theError == NULL )
{
bzero(buffer, sizeof(buffer));
if (CFStringGetCString(inPrincipal, (char *)buffer, sizeof(buffer), kCFStringEncodingUTF8) == false)
{
theError = MyCFErrorCreate(kCFErrorDomainPOSIX, (CFIndex)EINVAL,
CFSTR("SetXGridPrincipal: could not convert the principal name\n"));
}
else
{
length = strlen((char *)buffer);
if (write(theConfigFile, buffer, length) != (ssize_t)length)
{
logString = CFStringCreateWithFormat(NULL, NULL, CFSTR("SetXGridPrincipal: failed to write %s, errno = %d\n"),
kXGridConfigPath, errno);
theError = MyCFErrorCreate(kCFErrorDomainPOSIX, (CFIndex)errno, logString);
if ( logString != NULL )
CFRelease( logString );
}
}
close(theConfigFile);
}
return theError;
}
CFErrorRef AddXGridPrincipal(CFStringRef inPrincipal)
{
CFErrorRef theError = NULL;
CFStringRef logString = NULL;
UInt8 buffer[1024];
size_t length;
int theConfigFile = -1;
char linefeed = '\n';
struct stat sb;
int err;
err = stat(kXGridConfigPath, &sb);
if (err != 0)
{
theError = SetXGridPrincipal(inPrincipal);
}
else
{
theError = OpenXGridPrincipalFile(O_CREAT | O_EXLOCK | O_RDWR | O_APPEND, &theConfigFile);
if (theError == NULL)
{
bzero(buffer, sizeof(buffer));
if (CFStringGetCString(inPrincipal, (char *)buffer, sizeof(buffer), kCFStringEncodingUTF8) == false)
{
theError = MyCFErrorCreate(kCFErrorDomainPOSIX, (CFIndex)EINVAL,
CFSTR("SetXGridPrincipal: could not convert the principal name\n"));
}
else
{
if (sb.st_size > 0)
write(theConfigFile, &linefeed, 1);
length = strlen((char *)buffer);
if (write(theConfigFile, buffer, length) != (ssize_t)length)
{
logString = CFStringCreateWithFormat(NULL, NULL, CFSTR("SetXGridPrincipal: failed to write %s, errno = %d\n"),
kXGridConfigPath, errno);
theError = MyCFErrorCreate(kCFErrorDomainPOSIX, (CFIndex)errno, logString);
if ( logString != NULL )
CFRelease( logString );
}
}
close(theConfigFile);
}
}
return theError;
}
#pragma mark -
#pragma mark Support Functions
#pragma mark -
CFMutableDictionaryRef CreateVPNDefaults(void)
{
CFMutableDictionaryRef tmpDict1 = CFDictionaryCreateMutable(NULL, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFMutableDictionaryRef tmpDict2 = CFDictionaryCreateMutable(NULL, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionaryAddValue(tmpDict1, CFSTR(kVPNServiceKey), tmpDict2);
CFRelease(tmpDict2);
tmpDict2 = CFDictionaryCreateMutable(NULL, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionaryAddValue(tmpDict2, CFSTR(kVPNServerKey), tmpDict1);
CFRelease(tmpDict1);
CFDictionaryAddValue(tmpDict2, CFSTR("AutoGenerated"), CFSTR("krbservicesetup"));
return tmpDict2;
}
static CFErrorRef OpenXGridPrincipalFile(int openFlags, int *configFileRef)
{
CFErrorRef theError = NULL;
CFStringRef logString = NULL;
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP;
*configFileRef = open( kXGridConfigPath, openFlags, mode );
if ( *configFileRef == -1 )
{
logString = CFStringCreateWithFormat( NULL, NULL, CFSTR("SetXGridPrincipal: could not open/create %s errno = %d\n"),
kXGridConfigPath, errno );
theError = MyCFErrorCreate( kCFErrorDomainPOSIX, (CFIndex)errno, logString );
if ( logString != NULL )
CFRelease( logString );
}
return theError;
}
static CFMutableDictionaryRef CreateDictionaryFromFD(int inFd, Boolean inMakeMutable)
{
struct stat fileInfo;
UInt8 *buffer = NULL;
CFIndex size;
CFMutableDictionaryRef theDictionary = NULL;
CFDataRef theData = NULL;
off_t savedPos;
if (fstat(inFd, &fileInfo) == -1)
return NULL;
size = fileInfo.st_size;
buffer = (UInt8 *)calloc(1,size);
savedPos = lseek(inFd, 0, SEEK_SET);
if (read(inFd, buffer, size) != size)
{
free((void *)buffer);
lseek(inFd, savedPos, SEEK_SET);
return NULL;
}
lseek(inFd, savedPos, SEEK_SET);
theData = CFDataCreate (NULL, buffer, size);
free((void *)buffer);
if (theData == NULL)
return NULL;
if (inMakeMutable == true)
{
theDictionary = (CFMutableDictionaryRef)CFPropertyListCreateFromXMLData (NULL, theData, kCFPropertyListMutableContainersAndLeaves, NULL);
}
else
{
theDictionary = (CFMutableDictionaryRef)CFPropertyListCreateFromXMLData (NULL, theData, kCFPropertyListImmutable, NULL);
}
CFRelease(theData);
return theDictionary;
}
static CFDictionaryRef CreateMyPropertyListFromFile( CFURLRef fileURL, Boolean isMutable )
{
CFPropertyListRef propertyList = NULL;
CFStringRef errorString = NULL;
CFDataRef resourceData = NULL;
Boolean status;
SInt32 errorCode;
status = CFURLCreateDataAndPropertiesFromResource(
kCFAllocatorDefault,
fileURL,
&resourceData, NULL,
NULL,
&errorCode);
if(resourceData != NULL)
{
if (isMutable == true)
{
propertyList = CFPropertyListCreateFromXMLData( kCFAllocatorDefault,
resourceData,
kCFPropertyListMutableContainersAndLeaves,
&errorString);
}
else {
propertyList = CFPropertyListCreateFromXMLData( kCFAllocatorDefault,
resourceData,
kCFPropertyListImmutable,
&errorString);
}
CFRelease( resourceData );
}
return (CFDictionaryRef)propertyList;
}
static CFErrorRef MyCFErrorCreate(CFStringRef domain, CFIndex code, CFStringRef description)
{
CFErrorRef theError = NULL;
CFDictionaryRef logDict = NULL;
if ( description != NULL )
{
logDict = CFDictionaryCreate(NULL, (CFTypeRef *)&kCFErrorLocalizedDescriptionKey, (CFTypeRef *)&description, 1,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
}
theError = CFErrorCreate(NULL, domain, code, logDict);
if ( logDict != NULL )
CFRelease( logDict );
return theError;
}