#include "CFOpenDirectory.h"
#include "internal.h"
#include "extauth.h"
#include "record_internal.h"
enum {
eAuthTypeChangePasswd = 1,
eAuthTypeSetPasswordAsCurrent,
eAuthTypeSetPassword,
eAuthTypeSetPolicy,
eAuthTypeGetEffectivePolicy,
eAuthTypeSimpleVerify,
};
uint32_t
extauth_map_type(ODAuthenticationType authType)
{
int32_t type = 0;
if (CFEqual(authType, kODAuthenticationTypeChangePasswd)) {
type = eAuthTypeChangePasswd;
} else if (CFEqual(authType, kODAuthenticationTypeSetPasswordAsCurrent)) {
type = eAuthTypeSetPasswordAsCurrent;
} else if (CFEqual(authType, kODAuthenticationTypeSetPassword)) {
type = eAuthTypeSetPassword;
} else if (CFEqual(authType, kODAuthenticationTypeSetPolicy)) {
type = eAuthTypeSetPolicy;
} else if (CFEqual(authType, kODAuthenticationTypeGetEffectivePolicy)) {
type = eAuthTypeGetEffectivePolicy;
} else if (CFEqual(authType, kODAuthenticationTypeClearText) || CFEqual(authType, kODAuthenticationTypeNodeNativeClearTextOK) || CFEqual(authType, kODAuthenticationTypeNodeNativeNoClearText)) {
type = eAuthTypeSimpleVerify;
}
return type;
}
bool
extauth_record_verify(ODRecordRef record, uint32_t mapped_type, CFArrayRef authItems, CFArrayRef *authItemsOut, ODContextRef *context, CFErrorRef *error)
{
bool success = false;
ODRecordRef tmprecord;
CFMutableArrayRef tmpitems;
switch (mapped_type) {
case eAuthTypeChangePasswd:
if (CFArrayGetCount(authItems) == 3) {
success = ODRecordChangePassword(record, CFArrayGetValueAtIndex(authItems, 1), CFArrayGetValueAtIndex(authItems, 2), error);
} else {
_ODErrorSet(error, kODErrorCredentialsParameterError, NULL);
}
break;
case eAuthTypeSetPasswordAsCurrent:
if (CFArrayGetCount(authItems) == 2) {
success = ODRecordChangePassword(record, NULL, CFArrayGetValueAtIndex(authItems, 1), error);
} else {
_ODErrorSet(error, kODErrorCredentialsParameterError, NULL);
}
break;
case eAuthTypeSetPassword:
if (CFArrayGetCount(authItems) == 4) {
tmprecord = ODNodeCopyRecord(_ODRecordGetNode(record), record->_type, record->_name, NULL, error);
if (tmprecord != NULL) {
if (ODRecordSetNodeCredentials(tmprecord, CFArrayGetValueAtIndex(authItems, 2), CFArrayGetValueAtIndex(authItems, 3), error) == true) {
success = ODRecordChangePassword(tmprecord, NULL, CFArrayGetValueAtIndex(authItems, 1), error);
}
CFRelease(tmprecord);
}
} else {
_ODErrorSet(error, kODErrorCredentialsParameterError, NULL);
}
break;
case eAuthTypeSetPolicy:
if (CFArrayGetCount(authItems) == 4) {
tmprecord = ODNodeCopyRecord(_ODRecordGetNode(record), record->_type, record->_name, NULL, error);
if (tmprecord != NULL) {
if (ODRecordSetNodeCredentials(tmprecord, CFArrayGetValueAtIndex(authItems, 0), CFArrayGetValueAtIndex(authItems, 1), error) == true) {
tmpitems = CFArrayCreateMutable(NULL, 2, &kCFTypeArrayCallBacks);
CFArrayAppendArray(tmpitems, authItems, CFRangeMake(2, 2));
success = ODRecordVerifyPasswordExtended(record, kODAuthenticationTypeSetPolicyAsCurrent, tmpitems, authItemsOut, context, error);
CFRelease(tmpitems);
}
CFRelease(tmprecord);
}
} else {
_ODErrorSet(error, kODErrorCredentialsParameterError, NULL);
}
break;
case eAuthTypeGetEffectivePolicy:
if (CFArrayGetCount(authItems) == 1) {
tmpitems = CFArrayCreateMutableCopy(NULL, 3, authItems);
CFArrayInsertValueAtIndex(tmpitems, 0, CFSTR(""));
CFArrayInsertValueAtIndex(tmpitems, 0, CFSTR(""));
success = ODRecordVerifyPasswordExtended(record, kODAuthenticationTypeGetPolicy, tmpitems, authItemsOut, context, error);
CFRelease(tmpitems);
} else {
_ODErrorSet(error, kODErrorCredentialsParameterError, NULL);
}
break;
case eAuthTypeSimpleVerify:
if (CFArrayGetCount(authItems) == 2) {
success = ODRecordVerifyPassword(record, CFArrayGetValueAtIndex(authItems, 1), error);
} else {
_ODErrorSet(error, kODErrorCredentialsParameterError, NULL);
}
break;
}
return success;
}
bool
extauth_node_verify(ODNodeRef node, ODRecordType recordType, uint32_t mapped_type, CFArrayRef authItems, CFArrayRef *authItemsOut, ODContextRef *context, CFErrorRef *error)
{
bool success = false;
CFIndex recname_idx;
ODRecordRef record;
recname_idx = (mapped_type == eAuthTypeSetPolicy) ? 2 : 0;
if (CFArrayGetCount(authItems) > recname_idx) {
record = ODNodeCopyRecord(node, recordType, CFArrayGetValueAtIndex(authItems, recname_idx), NULL, error);
if (record != NULL) {
success = extauth_record_verify(record, mapped_type, authItems, authItemsOut, context, error);
CFRelease(record);
}
} else {
_ODErrorSet(error, kODErrorCredentialsParameterError, NULL);
}
return success;
}