CPolicyGlobalXML.cpp [plain text]
#include <syslog.h>
#include "CPolicyGlobalXML.h"
#pragma mark -
#pragma mark C API
#pragma mark -
int ConvertGlobalXMLPolicyToSpaceDelimited( const char *inXMLDataStr, char **outPolicyStr )
{
if ( inXMLDataStr == NULL || outPolicyStr == NULL )
return -1;
CPolicyGlobalXML policyObj( inXMLDataStr );
*outPolicyStr = policyObj.GetPolicyAsSpaceDelimitedData();
if ( *outPolicyStr == NULL )
return -1;
return 0;
}
int ConvertGlobalSpaceDelimitedPolicyToXML( const char *inPolicyStr, char **outXMLDataStr )
{
PWGlobalAccessFeatures policies = {0};
PWGlobalMoreAccessFeatures morePolicies = {0};
if ( inPolicyStr == NULL || outXMLDataStr == NULL )
return -1;
if ( ! StringToPWGlobalAccessFeaturesExtra( inPolicyStr, &policies, &morePolicies ) )
return -1;
CPolicyGlobalXML policyObj;
policyObj.SetPolicyExtra( &policies, &morePolicies );
*outXMLDataStr = policyObj.GetPolicyAsXMLData();
if ( *outXMLDataStr == NULL )
return -1;
return 0;
}
#pragma mark -
#pragma mark Public Methods
#pragma mark -
CPolicyGlobalXML::CPolicyGlobalXML() : CPolicyBase()
{
CPolicyCommonInit();
}
CPolicyGlobalXML::CPolicyGlobalXML( CFDictionaryRef inPolicyDict ) : CPolicyBase()
{
CPolicyCommonInit();
}
CPolicyGlobalXML::CPolicyGlobalXML( const char *xmlDataStr ) : CPolicyBase()
{
CFDataRef xmlData;
CFStringRef errorString;
CFMutableDictionaryRef policyDict;
CPolicyCommonInit();
if ( xmlDataStr != NULL )
{
xmlData = CFDataCreate( kCFAllocatorDefault, (const unsigned char *)xmlDataStr, strlen(xmlDataStr) );
if ( xmlData != NULL )
{
policyDict = (CFMutableDictionaryRef) CFPropertyListCreateFromXMLData( kCFAllocatorDefault, xmlData, kCFPropertyListMutableContainersAndLeaves, &errorString );
if ( policyDict != NULL ) {
this->ConvertPropertyListPolicyToStruct( policyDict );
CFRelease( policyDict );
}
CFRelease( xmlData );
}
}
}
CPolicyGlobalXML::~CPolicyGlobalXML()
{
}
void
CPolicyGlobalXML::CPolicyCommonInit( void )
{
bzero( &mGlobalPolicy, sizeof(mGlobalPolicy) );
mGlobalPolicy.usingHistory = false;
mGlobalPolicy.usingExpirationDate = false;
mGlobalPolicy.usingHardExpirationDate = false;
mGlobalPolicy.requiresAlpha = false;
mGlobalPolicy.requiresNumeric = false;
mGlobalPolicy.passwordCannotBeName = false;
SetGlobalHistoryCount(mGlobalPolicy, 0);
mGlobalPolicy.maxMinutesUntilChangePassword = 0;
mGlobalPolicy.maxMinutesUntilDisabled = 0;
mGlobalPolicy.maxMinutesOfNonUse = 0;
mGlobalPolicy.maxFailedLoginAttempts = 0;
mGlobalPolicy.minChars = 0;
mGlobalPolicy.maxChars = 0;
bzero( &mExtraGlobalPolicy, sizeof(mExtraGlobalPolicy) );
}
void
CPolicyGlobalXML::GetPolicy( PWGlobalAccessFeatures *outPolicy )
{
if ( outPolicy != NULL )
memcpy( outPolicy, &mGlobalPolicy, sizeof(PWGlobalAccessFeatures) );
}
char *
CPolicyGlobalXML::GetPolicyAsSpaceDelimitedData( void )
{
char *returnString = NULL;
char featureString[2048];
PWGlobalAccessFeaturesToStringExtra( &mGlobalPolicy, &mExtraGlobalPolicy, sizeof(featureString), featureString );
returnString = strdup( featureString );
return returnString;
}
void
CPolicyGlobalXML::SetPolicy( PWGlobalAccessFeatures *inPolicy )
{
if ( inPolicy != NULL )
{
memcpy( &mGlobalPolicy, inPolicy, sizeof(PWGlobalAccessFeatures) );
this->ConvertStructToPropertyListPolicy();
}
}
void
CPolicyGlobalXML::SetPolicyExtra( PWGlobalAccessFeatures *inPolicy, PWGlobalMoreAccessFeatures *inMorePolicy )
{
if ( inPolicy != NULL && inMorePolicy != NULL )
{
memcpy( &mGlobalPolicy, inPolicy, sizeof(PWGlobalAccessFeatures) );
memcpy( &mExtraGlobalPolicy, inMorePolicy, sizeof(PWGlobalMoreAccessFeatures) );
this->ConvertStructToPropertyListPolicy();
}
}
#pragma mark -
#pragma mark Protected Methods
#pragma mark -
int
CPolicyGlobalXML::ConvertPropertyListPolicyToStruct( CFMutableDictionaryRef inPolicyDict )
{
int result = 0;
short aShortValue;
long aLongValue;
bool aBoolValue;
CFTypeRef valueRef;
if ( inPolicyDict == NULL )
return -1;
CFRetain( inPolicyDict );
if ( mPolicyDict != NULL )
CFRelease( mPolicyDict );
mPolicyDict = inPolicyDict;
if ( this->GetBooleanForKey( CFSTR(kPWPolicyStr_usingExpirationDate), &aBoolValue ) )
mGlobalPolicy.usingExpirationDate = aBoolValue;
if ( this->GetBooleanForKey( CFSTR(kPWPolicyStr_usingHardExpirationDate), &aBoolValue ) )
mGlobalPolicy.usingHardExpirationDate = aBoolValue;
if ( this->GetBooleanForKey( CFSTR(kPWPolicyStr_requiresAlpha), &aBoolValue ) )
mGlobalPolicy.requiresAlpha = aBoolValue;
if ( this->GetBooleanForKey( CFSTR(kPWPolicyStr_requiresNumeric), &aBoolValue ) )
mGlobalPolicy.requiresNumeric = aBoolValue;
if ( this->GetBooleanForKey( CFSTR(kPWPolicyStr_requiresMixedCase), &aBoolValue ) )
mGlobalPolicy.requiresMixedCase = aBoolValue;
if ( CFDictionaryGetValueIfPresent( mPolicyDict, CFSTR(kPWPolicyStr_notGuessablePattern), (const void **)&valueRef ) &&
CFGetTypeID(valueRef) == CFNumberGetTypeID() &&
CFNumberGetValue( (CFNumberRef)valueRef, kCFNumberLongType, &aLongValue) )
{
mExtraGlobalPolicy.notGuessablePattern = aLongValue;
}
if ( CFDictionaryGetValueIfPresent( mPolicyDict, CFSTR(kPWPolicyStr_expirationDateGMT), (const void **)&valueRef ) &&
CFGetTypeID(valueRef) == CFDateGetTypeID() )
{
this->ConvertCFDateToBSDTime( (CFDateRef)valueRef, (struct tm *)&mGlobalPolicy.expirationDateGMT );
}
if ( CFDictionaryGetValueIfPresent( mPolicyDict, CFSTR(kPWPolicyStr_hardExpireDateGMT), (const void **)&valueRef ) &&
CFGetTypeID(valueRef) == CFDateGetTypeID() )
{
this->ConvertCFDateToBSDTime( (CFDateRef)valueRef, (struct tm *)&mGlobalPolicy.hardExpireDateGMT );
}
if ( CFDictionaryGetValueIfPresent( mPolicyDict, CFSTR(kPWPolicyStr_maxMinutesUntilChangePW), (const void **)&valueRef ) &&
CFGetTypeID(valueRef) == CFNumberGetTypeID() &&
CFNumberGetValue( (CFNumberRef)valueRef, kCFNumberLongType, &aLongValue) )
{
mGlobalPolicy.maxMinutesUntilChangePassword = aLongValue;
}
if ( CFDictionaryGetValueIfPresent( mPolicyDict, CFSTR(kPWPolicyStr_maxMinutesUntilDisabled), (const void **)&valueRef ) &&
CFGetTypeID(valueRef) == CFNumberGetTypeID() &&
CFNumberGetValue( (CFNumberRef)valueRef, kCFNumberLongType, &aLongValue) )
{
mGlobalPolicy.maxMinutesUntilDisabled = aLongValue;
}
if ( CFDictionaryGetValueIfPresent( mPolicyDict, CFSTR(kPWPolicyStr_maxMinutesOfNonUse), (const void **)&valueRef ) &&
CFGetTypeID(valueRef) == CFNumberGetTypeID() &&
CFNumberGetValue( (CFNumberRef)valueRef, kCFNumberLongType, &aLongValue) )
{
mGlobalPolicy.maxMinutesOfNonUse = aLongValue;
}
if ( CFDictionaryGetValueIfPresent( mPolicyDict, CFSTR(kPWPolicyStr_maxFailedLoginAttempts), (const void **)&valueRef ) &&
CFGetTypeID(valueRef) == CFNumberGetTypeID() &&
CFNumberGetValue( (CFNumberRef)valueRef, kCFNumberShortType, &aShortValue) )
{
mGlobalPolicy.maxFailedLoginAttempts = aShortValue;
}
if ( CFDictionaryGetValueIfPresent( mPolicyDict, CFSTR(kPWPolicyStr_minChars), (const void **)&valueRef ) &&
CFGetTypeID(valueRef) == CFNumberGetTypeID() &&
CFNumberGetValue( (CFNumberRef)valueRef, kCFNumberShortType, &aShortValue) )
{
mGlobalPolicy.minChars = aShortValue;
}
if ( CFDictionaryGetValueIfPresent( mPolicyDict, CFSTR(kPWPolicyStr_maxChars), (const void **)&valueRef ) &&
CFGetTypeID(valueRef) == CFNumberGetTypeID() &&
CFNumberGetValue( (CFNumberRef)valueRef, kCFNumberShortType, &aShortValue) )
{
mGlobalPolicy.maxChars = aShortValue;
}
if ( CFDictionaryGetValueIfPresent( mPolicyDict, CFSTR(kPWPolicyStr_usingHistory), (const void **)&valueRef ) &&
CFGetTypeID(valueRef) == CFNumberGetTypeID() &&
CFNumberGetValue( (CFNumberRef)valueRef, kCFNumberShortType, &aShortValue) )
{
if ( aShortValue > kPWFileMaxHistoryCount )
aShortValue = kPWFileMaxHistoryCount;
if ( aShortValue > 0 )
{
mGlobalPolicy.usingHistory = true;
SetGlobalHistoryCount(mGlobalPolicy, aShortValue - 1);
}
else
{
mGlobalPolicy.usingHistory = false;
SetGlobalHistoryCount(mGlobalPolicy, 0);
}
}
if ( this->GetBooleanForKey( CFSTR(kPWPolicyStr_passwordCannotBeName), &aBoolValue ) )
mGlobalPolicy.passwordCannotBeName = aBoolValue;
if ( this->GetBooleanForKey( CFSTR(kPWPolicyStr_canModifyPasswordforSelf), &aBoolValue ) )
mGlobalPolicy.noModifyPasswordforSelf = (aBoolValue==0);
return result;
}
int
CPolicyGlobalXML::ConvertStructToPropertyListPolicy( void )
{
CFMutableDictionaryRef policyDict;
int historyNumber;
CFDateRef expirationDateGMTRef;
CFDateRef hardExpireDateGMTRef;
unsigned int aBoolVal;
policyDict = CFDictionaryCreateMutable( kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks );
if ( policyDict == NULL )
return -1;
historyNumber = (mGlobalPolicy.usingHistory != 0);
if ( historyNumber > 0 )
historyNumber += GlobalHistoryCount(mGlobalPolicy);
CFNumberRef usingHistoryRef = CFNumberCreate( kCFAllocatorDefault, kCFNumberIntType, &historyNumber );
aBoolVal = (mGlobalPolicy.usingExpirationDate != 0);
CFNumberRef usingExpirationDateRef = CFNumberCreate( kCFAllocatorDefault, kCFNumberIntType, &aBoolVal );
aBoolVal = (mGlobalPolicy.usingHardExpirationDate != 0);
CFNumberRef usingHardExpirationDateRef = CFNumberCreate( kCFAllocatorDefault, kCFNumberIntType, &aBoolVal );
aBoolVal = (mGlobalPolicy.requiresAlpha != 0);
CFNumberRef requiresAlphaRef = CFNumberCreate( kCFAllocatorDefault, kCFNumberIntType, &aBoolVal );
aBoolVal = (mGlobalPolicy.requiresNumeric != 0);
CFNumberRef requiresNumericRef = CFNumberCreate( kCFAllocatorDefault, kCFNumberIntType, &aBoolVal );
aBoolVal = (mGlobalPolicy.passwordCannotBeName != 0);
CFNumberRef passwordCannotBeNameRef = CFNumberCreate( kCFAllocatorDefault, kCFNumberIntType, &aBoolVal );
aBoolVal = (mGlobalPolicy.requiresMixedCase != 0);
CFNumberRef requiresMixedCaseRef = CFNumberCreate( kCFAllocatorDefault, kCFNumberIntType, &aBoolVal );
CFNumberRef notGuessablePatternRef = CFNumberCreate( kCFAllocatorDefault, kCFNumberLongType, &(mExtraGlobalPolicy.notGuessablePattern) );
CFNumberRef maxMinutesUntilChangePasswordRef = CFNumberCreate( kCFAllocatorDefault, kCFNumberLongType, &mGlobalPolicy.maxMinutesUntilChangePassword );
CFNumberRef maxMinutesUntilDisabledRef = CFNumberCreate( kCFAllocatorDefault, kCFNumberLongType, &mGlobalPolicy.maxMinutesUntilDisabled );
CFNumberRef maxMinutesOfNonUseRef = CFNumberCreate( kCFAllocatorDefault, kCFNumberLongType, &mGlobalPolicy.maxMinutesOfNonUse );
CFNumberRef maxFailedLoginAttemptsRef = CFNumberCreate( kCFAllocatorDefault, kCFNumberShortType, &mGlobalPolicy.maxFailedLoginAttempts );
CFNumberRef minCharsRef = CFNumberCreate( kCFAllocatorDefault, kCFNumberShortType, &mGlobalPolicy.minChars );
CFNumberRef maxCharsRef = CFNumberCreate( kCFAllocatorDefault, kCFNumberShortType, &mGlobalPolicy.maxChars );
CFBooleanRef canModifyPasswordforSelfRef = mGlobalPolicy.noModifyPasswordforSelf ? kCFBooleanFalse : kCFBooleanTrue;
this->ConvertBSDTimeToCFDate( (struct tm *)&(mGlobalPolicy.expirationDateGMT), &expirationDateGMTRef );
this->ConvertBSDTimeToCFDate( (struct tm *)&(mGlobalPolicy.hardExpireDateGMT), &hardExpireDateGMTRef );
if ( usingHistoryRef != NULL )
{
CFDictionaryAddValue( policyDict, CFSTR(kPWPolicyStr_usingHistory), usingHistoryRef );
CFRelease( usingHistoryRef );
}
if ( canModifyPasswordforSelfRef != NULL )
CFDictionaryAddValue( policyDict, CFSTR(kPWPolicyStr_canModifyPasswordforSelf), canModifyPasswordforSelfRef );
if ( usingExpirationDateRef != NULL )
{
CFDictionaryAddValue( policyDict, CFSTR(kPWPolicyStr_usingExpirationDate), usingExpirationDateRef );
CFRelease( usingExpirationDateRef );
}
if ( usingHardExpirationDateRef != NULL )
{
CFDictionaryAddValue( policyDict, CFSTR(kPWPolicyStr_usingHardExpirationDate), usingHardExpirationDateRef );
CFRelease( usingHardExpirationDateRef );
}
if ( requiresAlphaRef != NULL )
{
CFDictionaryAddValue( policyDict, CFSTR(kPWPolicyStr_requiresAlpha), requiresAlphaRef );
CFRelease( requiresAlphaRef );
}
if ( requiresNumericRef != NULL )
{
CFDictionaryAddValue( policyDict, CFSTR(kPWPolicyStr_requiresNumeric), requiresNumericRef );
CFRelease( requiresNumericRef );
}
if ( requiresMixedCaseRef != NULL )
{
CFDictionaryAddValue( policyDict, CFSTR(kPWPolicyStr_requiresMixedCase), requiresMixedCaseRef );
CFRelease( requiresMixedCaseRef );
}
if ( notGuessablePatternRef != NULL )
{
CFDictionaryAddValue( policyDict, CFSTR(kPWPolicyStr_notGuessablePattern), notGuessablePatternRef );
CFRelease( notGuessablePatternRef );
}
if ( expirationDateGMTRef != NULL )
{
CFDictionaryAddValue( policyDict, CFSTR(kPWPolicyStr_expirationDateGMT), expirationDateGMTRef );
CFRelease( expirationDateGMTRef );
}
if ( hardExpireDateGMTRef != NULL )
{
CFDictionaryAddValue( policyDict, CFSTR(kPWPolicyStr_hardExpireDateGMT), hardExpireDateGMTRef );
CFRelease( hardExpireDateGMTRef );
}
if ( maxMinutesUntilChangePasswordRef != NULL )
{
CFDictionaryAddValue( policyDict, CFSTR(kPWPolicyStr_maxMinutesUntilChangePW), maxMinutesUntilChangePasswordRef );
CFRelease( maxMinutesUntilChangePasswordRef );
}
if ( maxMinutesUntilDisabledRef != NULL )
{
CFDictionaryAddValue( policyDict, CFSTR(kPWPolicyStr_maxMinutesUntilDisabled), maxMinutesUntilDisabledRef );
CFRelease( maxMinutesUntilDisabledRef );
}
if ( maxMinutesOfNonUseRef != NULL )
{
CFDictionaryAddValue( policyDict, CFSTR(kPWPolicyStr_maxMinutesOfNonUse), maxMinutesOfNonUseRef );
CFRelease( maxMinutesOfNonUseRef );
}
if ( maxFailedLoginAttemptsRef != NULL )
{
CFDictionaryAddValue( policyDict, CFSTR(kPWPolicyStr_maxFailedLoginAttempts), maxFailedLoginAttemptsRef );
CFRelease( maxFailedLoginAttemptsRef );
}
if ( minCharsRef != NULL )
{
CFDictionaryAddValue( policyDict, CFSTR(kPWPolicyStr_minChars), minCharsRef );
CFRelease( minCharsRef );
}
if ( maxCharsRef != NULL )
{
CFDictionaryAddValue( policyDict, CFSTR(kPWPolicyStr_maxChars), maxCharsRef );
CFRelease( maxCharsRef );
}
if ( passwordCannotBeNameRef != NULL )
{
CFDictionaryAddValue( policyDict, CFSTR(kPWPolicyStr_passwordCannotBeName), passwordCannotBeNameRef );
CFRelease( passwordCannotBeNameRef );
}
if ( mPolicyDict != NULL )
CFRelease( mPolicyDict );
mPolicyDict = policyDict;
return 0;
}