#include <syslog.h>
#include <sys/types.h>
#include <sys/time.h>
#include <string.h>
#include <CoreFoundation/CoreFoundation.h>
#include <DirectoryService/DirServices.h>
#include <DirectoryService/DirServicesUtils.h>
#include <DirectoryService/DirServicesConst.h>
#include "../../Helpers/pppd/pppd.h"
#include "DSUser.h"
#define BUF_LEN 1024
tDirStatus dsauth_get_search_node_ref(tDirReference dirRef, UInt32 index,
tDirNodeReference *searchNodeRef, UInt32 *count)
{
tDirStatus dsResult = -1;
tDataBufferPtr searchNodeDataBufferPtr = 0;
tDataListPtr searchNodeNameDataListPtr = 0;
UInt32 outNodeCount;
tContextData continueData = 0;
*searchNodeRef = 0;
*count = 0;
if ((searchNodeDataBufferPtr = dsDataBufferAllocate(dirRef, BUF_LEN)) == 0) {
error("DS plugin: Could not allocate tDataBuffer\n");
goto cleanup;
}
if ((searchNodeNameDataListPtr = dsDataListAllocate(dirRef)) == 0) {
error("DS plugin: Could not allocate tDataList\n");
goto cleanup;
}
if ((dsResult = dsFindDirNodes(dirRef, searchNodeDataBufferPtr, 0, eDSAuthenticationSearchNodeName,
&outNodeCount, &continueData)) == eDSNoErr) {
if (outNodeCount != 0) {
if ((dsResult = dsGetDirNodeName(dirRef, searchNodeDataBufferPtr, index,
&searchNodeNameDataListPtr)) == eDSNoErr) {
if ((dsResult = dsOpenDirNode(dirRef, searchNodeNameDataListPtr, searchNodeRef)) == eDSNoErr) {
*count = outNodeCount;
}
}
}
if (continueData)
dsReleaseContinueData(dirRef, continueData);
}
cleanup:
if (searchNodeDataBufferPtr)
dsDataBufferDeAllocate(dirRef, searchNodeDataBufferPtr);
if (searchNodeNameDataListPtr)
dsDataListDeallocate(dirRef, searchNodeNameDataListPtr);
return dsResult;
}
tDirStatus dsauth_get_user_attr(tDirReference dirRef, tDirNodeReference searchNodeRef, char *user_name,
char *attr, tAttributeValueEntryPtr *attr_value)
{
tDirStatus dsResult = -1;
tDataBufferPtr userRcdDataBufferPtr = 0;
tDataListPtr recordNameDataListPtr = 0;
tDataListPtr recordTypeDataListPtr = 0;
tDataListPtr attrTypeDataListPtr = 0;
tContextData continueData = 0;
UInt32 outRecordCount;
int userRcdFound = 0;
UInt32 userRecordIndex, attrIndex;
*attr_value = 0;
if ((userRcdDataBufferPtr = dsDataBufferAllocate(dirRef, BUF_LEN)) == 0) {
error("DS plugin: Could not allocate tDataBuffer\n");
goto cleanup;
}
if ((recordNameDataListPtr = dsBuildListFromStrings(dirRef, user_name, 0)) == 0) {
error("DS plugin: Could not allocate tDataList\n");
goto cleanup;
}
if ((recordTypeDataListPtr = dsBuildListFromStrings(dirRef, kDSStdRecordTypeUsers, 0)) == 0) {
error("DS plugin: Could not allocate tDataList\n");
goto cleanup;
}
if ((attrTypeDataListPtr = dsBuildListFromStrings(dirRef, kDSNAttrRecordName, kDS1AttrDistinguishedName, attr, 0)) == 0) {
error("DS plugin: Could not allocate tDataList\n");
goto cleanup;
}
do {
dsResult = dsGetRecordList(searchNodeRef, userRcdDataBufferPtr, recordNameDataListPtr, eDSExact,
recordTypeDataListPtr, attrTypeDataListPtr, 0, &outRecordCount, &continueData);
if (dsResult == eDSBufferTooSmall) {
u_int32_t size = userRcdDataBufferPtr->fBufferSize * 2;
dsDataBufferDeAllocate(dirRef, userRcdDataBufferPtr);
if ((userRcdDataBufferPtr = dsDataBufferAllocate(dirRef, size)) == 0) {
error("DS plugin: Could not allcoate tDataBuffer\n");
goto cleanup;
}
}
} while (dsResult == eDSBufferTooSmall);
if (dsResult == eDSNoErr) {
for (userRecordIndex = 1; (userRecordIndex <= outRecordCount) && (dsResult == eDSNoErr)
&& (userRcdFound == 0); userRecordIndex++) {
tAttributeListRef attrListRef;
tRecordEntryPtr userRcdEntryPtr;
if ((dsResult = dsGetRecordEntry(searchNodeRef, userRcdDataBufferPtr, userRecordIndex,
&attrListRef, &userRcdEntryPtr)) == eDSNoErr) {
for (attrIndex = 1; (attrIndex <= userRcdEntryPtr->fRecordAttributeCount)
&& (dsResult == eDSNoErr); attrIndex++) {
tAttributeValueListRef attrValueListRef;
tAttributeEntryPtr attrInfoPtr;
tAttributeValueEntryPtr attrValuePtr;
if ((dsResult = dsGetAttributeEntry(searchNodeRef, userRcdDataBufferPtr,
attrListRef, attrIndex, &attrValueListRef, &attrInfoPtr)) == eDSNoErr) {
if ((dsResult = dsGetAttributeValue(searchNodeRef, userRcdDataBufferPtr, 1,
attrValueListRef, &attrValuePtr)) == eDSNoErr) {
if (!strcmp(attrInfoPtr->fAttributeSignature.fBufferData, kDSNAttrRecordName)) {
if (!strcmp(attrValuePtr->fAttributeValueData.fBufferData, user_name))
userRcdFound = 1;
}
if (!strcmp(attrInfoPtr->fAttributeSignature.fBufferData, kDS1AttrDistinguishedName)) {
if (!strcmp(attrValuePtr->fAttributeValueData.fBufferData, user_name))
userRcdFound = 1;
}
if (!strcmp(attrInfoPtr->fAttributeSignature.fBufferData, attr)) {
*attr_value = attrValuePtr; attrValuePtr = 0; }
if (attrValuePtr)
dsDeallocAttributeValueEntry(dirRef, attrValuePtr);
}
dsCloseAttributeValueList(attrValueListRef);
dsDeallocAttributeEntry(dirRef, attrInfoPtr);
}
}
if(userRcdFound == 0 || *attr_value == 0) {
userRcdFound = 0;
if (*attr_value)
dsDeallocAttributeValueEntry(dirRef, *attr_value);
*attr_value = 0;
}
dsCloseAttributeList(attrListRef);
dsDeallocRecordEntry(dirRef, userRcdEntryPtr);
}
}
}
cleanup:
if (continueData)
dsReleaseContinueData(searchNodeRef, continueData);
if (userRcdDataBufferPtr)
dsDataBufferDeAllocate(dirRef, userRcdDataBufferPtr);
if (recordNameDataListPtr)
dsDataListDeallocate(dirRef, recordNameDataListPtr);
if (recordTypeDataListPtr)
dsDataListDeallocate(dirRef, recordTypeDataListPtr);
if (attrTypeDataListPtr)
dsDataListDeallocate(dirRef, attrTypeDataListPtr);
return dsResult;
}