CFMutableDictionaryRef get_record_at_index(KADirectoryNode *node, long unsigned index) { tRecordEntryPtr record_entry_ptr = NULL; tAttributeListRef attr_list_ref; CFMutableDictionaryRef attributes = NULL; assert(index > 0); // indexes start with 1 if (dsGetRecordEntry(node->_nodeRef, node->_dataBufferPtr, index, // start count at 1 &attr_list_ref, &record_entry_ptr) == eDSNoErr) { attributes = get_record_attributes(node, record_entry_ptr, attr_list_ref); dsCloseAttributeList(attr_list_ref); dsDeallocRecordEntry(node->_directoryRef, record_entry_ptr); } return attributes; }
tDirStatus userAuthInfo(tDirReference pDirRef, tDirNodeReference pNodeRef, const char *pszUsername, tDataListPtr *ppAuthNodeListOut) { tDirStatus dsErr = eDSNoErr; tDirStatus dsCleanErr = eDSNoErr; /* Create a buffer for the resulting authentication info */ tDataBufferPtr pTmpBuf = dsDataBufferAllocate(pDirRef, s_cBufferSize); if (pTmpBuf) { /* Create the necessary lists for kDSNAttrMetaNodeLocation and kDSNAttrRecordName. */ tDataListPtr pRecordType = dsBuildListFromStrings(pDirRef, kDSStdRecordTypeUsers, NULL); tDataListPtr pRecordName = dsBuildListFromStrings(pDirRef, pszUsername, NULL); tDataListPtr pRequestedAttributes = dsBuildListFromStrings(pDirRef, kDSNAttrMetaNodeLocation, kDSNAttrRecordName, NULL); if (!( pRecordType == NULL || pRecordName == NULL || pRequestedAttributes == NULL)) { /* Now search for the first matching record */ UInt32 cRecords = 1; tContextData pCtx = NULL; dsErr = dsGetRecordList(pNodeRef, pTmpBuf, pRecordName, eDSExact, pRecordType, pRequestedAttributes, false, &cRecords, &pCtx); if ( dsErr == eDSNoErr && cRecords >= 1) { /* Process the first found record. Look at any attribute one by one. */ tAttributeListRef pRecAttrListRef = NULL; tRecordEntryPtr pRecEntry = NULL; tDataListPtr pAuthNodeList = NULL; dsErr = dsGetRecordEntry(pNodeRef, pTmpBuf, 1, &pRecAttrListRef, &pRecEntry); if (dsErr == eDSNoErr) { for (size_t i = 1; i <= pRecEntry->fRecordAttributeCount; ++i) { tAttributeValueListRef pAttrValueListRef = NULL; tAttributeEntryPtr pAttrEntry = NULL; /* Get the information for this attribute. */ dsErr = dsGetAttributeEntry(pNodeRef, pTmpBuf, pRecAttrListRef, i, &pAttrValueListRef, &pAttrEntry); if (dsErr == eDSNoErr) { tAttributeValueEntryPtr pValueEntry = NULL; /* Has any value? */ if (pAttrEntry->fAttributeValueCount > 0) { dsErr = dsGetAttributeValue(pNodeRef, pTmpBuf, 1, pAttrValueListRef, &pValueEntry); if (dsErr == eDSNoErr) { /* Check for kDSNAttrMetaNodeLocation */ if (strcmp(pAttrEntry->fAttributeSignature.fBufferData, kDSNAttrMetaNodeLocation) == 0) { /* Convert the meta location attribute to a path node list */ pAuthNodeList = dsBuildFromPath(pDirRef, pValueEntry->fAttributeValueData.fBufferData, "/"); if (pAuthNodeList == NULL) dsErr = eDSAllocationFailed; } } } if (pValueEntry != NULL) dsDeallocAttributeValueEntry(pDirRef, pValueEntry); if (pAttrValueListRef) dsCloseAttributeValueList(pAttrValueListRef); if (pAttrEntry != NULL) dsDeallocAttributeEntry(pDirRef, pAttrEntry); if (dsErr != eDSNoErr) break; } } } /* Copy the results */ if (dsErr == eDSNoErr) { if (pAuthNodeList != NULL) { /* Copy out results. */ *ppAuthNodeListOut = pAuthNodeList; pAuthNodeList = NULL; } else dsErr = eDSAttributeNotFound; } if (pAuthNodeList != NULL) { dsCleanErr = dsDataListDeallocate(pDirRef, pAuthNodeList); if (dsCleanErr == eDSNoErr) free(pAuthNodeList); } if (pRecAttrListRef) dsCloseAttributeList(pRecAttrListRef); if (pRecEntry != NULL) dsDeallocRecordEntry(pDirRef, pRecEntry); } else dsErr = eDSRecordNotFound; if (pCtx) dsReleaseContinueData(pDirRef, pCtx); } else dsErr = eDSAllocationFailed; if (pRequestedAttributes != NULL) { dsCleanErr = dsDataListDeallocate(pDirRef, pRequestedAttributes); if (dsCleanErr == eDSNoErr) free(pRequestedAttributes); } if (pRecordName != NULL) { dsCleanErr = dsDataListDeallocate(pDirRef, pRecordName); if (dsCleanErr == eDSNoErr) free(pRecordName); } if (pRecordType != NULL) { dsCleanErr = dsDataListDeallocate(pDirRef, pRecordType); if (dsCleanErr == eDSNoErr) free(pRecordType); } dsDataBufferDeAllocate(pDirRef, pTmpBuf); } else dsErr = eDSAllocationFailed; return dsErr; }
tDirStatus FindUsersAuthInfo( tDirReference dirRef, tDirNodeReference nodeRef, const char * username, tDataListPtr * pathListToAuthNodePtr ) { tDirStatus err; tDirStatus junk; tDataBufferPtr buf; tDataListPtr recordType; tDataListPtr recordName; tDataListPtr requestedAttributes; unsigned long recordCount; tAttributeListRef foundRecAttrList; tContextData context; tRecordEntryPtr foundRecEntry; tDataListPtr pathListToAuthNode; if (!( (dirRef != NULL) || (nodeRef != NULL) || (username != NULL) || ( pathListToAuthNodePtr != NULL) || (*pathListToAuthNodePtr == NULL) ) ) return(eDSBadParam); recordType = NULL; recordName = NULL; requestedAttributes = NULL; foundRecAttrList = NULL; context = NULL; foundRecEntry = NULL; pathListToAuthNode = NULL; /* ** Allocate a buffer for the record results. We'll grow this ** buffer if it proves to be too small. */ err = eDSNoErr; buf = dsDataBufferAllocate(dirRef, kDefaultDSBufferSize); if (buf == NULL) err = eDSAllocationFailed; /* ** Create the information needed for the search. We're searching for ** a record of type kDSStdRecordTypeUsers whose name is "username". ** We want to get back the kDSNAttrMetaNodeLocation and kDSNAttrRecordName ** attributes. */ if (err == eDSNoErr) { recordType = dsBuildListFromStrings(dirRef, kDSStdRecordTypeUsers, NULL); recordName = dsBuildListFromStrings(dirRef, username, NULL); requestedAttributes = dsBuildListFromStrings(dirRef, kDSNAttrMetaNodeLocation, kDSNAttrRecordName, NULL); if ( (recordType == NULL) || (recordName == NULL) || (requestedAttributes == NULL) ) err = eDSAllocationFailed; } /* Search for a matching record. */ if (err == eDSNoErr) { recordCount = 1; /* we only want one match (the first) */ err = dsGetRecordListQ( dirRef, nodeRef, &buf, recordName, eDSExact, recordType, requestedAttributes, false, &recordCount, &context ); } if ( (err == eDSNoErr) && (recordCount < 1) ) err = eDSRecordNotFound; /* ** Get the first record from the search. Then enumerate the attributes for ** that record. For each attribute, extract the first value (remember that ** attributes can by multi-value). Then see if the attribute is one that ** we care about. If it is, remember the value for later processing. */ if (err == eDSNoErr) err = dsGetRecordEntry(nodeRef, buf, 1, &foundRecAttrList, &foundRecEntry); if (err == eDSNoErr) { unsigned long attrIndex; /* Iterate over the attributes. */ for (attrIndex = 1; attrIndex <= foundRecEntry->fRecordAttributeCount; attrIndex++) { tAttributeValueListRef thisValue; tAttributeEntryPtr thisAttrEntry; tAttributeValueEntryPtr thisValueEntry; const char * thisAttrName; thisValue = NULL; thisAttrEntry = NULL; thisValueEntry = NULL; /* Get the information for this attribute. */ err = dsGetAttributeEntry(nodeRef, buf, foundRecAttrList, attrIndex, &thisValue, &thisAttrEntry); if (err == eDSNoErr) { thisAttrName = thisAttrEntry->fAttributeSignature.fBufferData; /* We only care about attributes that have values. */ if (thisAttrEntry->fAttributeValueCount > 0) { /* ** Get the first value for this attribute. This is common ** code for the two potential attribute values listed below, ** so we do it first. */ err = dsGetAttributeValue(nodeRef, buf, 1, thisValue, &thisValueEntry); if (err == eDSNoErr) { const char * thisValueDataPtr; unsigned long thisValueDataLen; thisValueDataPtr = thisValueEntry->fAttributeValueData.fBufferData; thisValueDataLen = thisValueEntry->fAttributeValueData.fBufferLength; /* ** Handle each of the two attributes we care about; ** ignore any others. */ if ( STcompare(thisAttrName, kDSNAttrMetaNodeLocation) == 0 ) { /* ** This is the kDSNAttrMetaNodeLocation attribute, ** which contains a path to the node used for ** authenticating this record; convert its value ** into a path list. */ pathListToAuthNode = dsBuildFromPath( dirRef, thisValueDataPtr, "/" ); if (pathListToAuthNode == NULL) err = eDSAllocationFailed; } } else { fprintf(stderr, "FindUsersAuthInfo: Unexpected no-value attribute '%s'.", thisAttrName); } } /* Clean up. */ if (thisValueEntry != NULL) dsDeallocAttributeValueEntry(dirRef, thisValueEntry); if (thisValue != NULL) dsCloseAttributeValueList(thisValue); if (thisAttrEntry != NULL) dsDeallocAttributeEntry(dirRef, thisAttrEntry); if (err != eDSNoErr) break; } } /* Check attr loop */ } /* Copy results out to caller. */ if (err == eDSNoErr) { if (pathListToAuthNode != NULL) { /* Copy out results. */ *pathListToAuthNodePtr = pathListToAuthNode; /* NULL out locals so that we don't dispose them. */ pathListToAuthNode = NULL; } else err = eDSAttributeNotFound; } /* Clean up. */ if (pathListToAuthNode != NULL) dsDataListAndHeaderDeallocate(dirRef, pathListToAuthNode); if (foundRecAttrList != NULL) dsCloseAttributeList(foundRecAttrList); if (context != NULL) dsReleaseContinueData(dirRef, context); if (foundRecAttrList != NULL) dsDeallocRecordEntry(dirRef, foundRecEntry); if (requestedAttributes != NULL) dsDataListAndHeaderDeallocate(dirRef, requestedAttributes); if (recordName != NULL) dsDataListAndHeaderDeallocate(dirRef, recordName); if (recordType != NULL) dsDataListAndHeaderDeallocate(dirRef, recordType); if (buf != NULL) dsDataBufferDeAllocate(dirRef, buf); return err; }