CFTypeRef WBODCopyUserAttribute(CFStringRef username, ODAttributeType attribute) { assert(username && attribute); if (!username || !attribute) return NULL; CFErrorRef error; ODQueryRef query = ODQueryCreateWithNodeType(kCFAllocatorDefault, kODNodeTypeLocalNodes, kODRecordTypeUsers, kODAttributeTypeRecordName, kODMatchEqualTo, username, attribute, 0, &error); CFArrayRef records = ODQueryCopyResults(query, false, &error); CFRelease(query); if (!records) { if (error) CFRelease(error); return NULL; } CFTypeRef result = NULL; if (CFArrayGetCount(records) >= 1) { ODRecordRef user = (ODRecordRef)CFArrayGetValueAtIndex(records, 0); result = WBODRecordCopyFirstValue(user, attribute); } CFRelease(records); return result; }
Boolean AFPUserList_init(AFPUserListRef users) { CFErrorRef error; int i; int n; CFArrayRef results; ODQueryRef query; bzero(users, sizeof(*users)); users->node = ODNodeCreateWithNodeType(NULL, kODSessionDefault, kODNodeTypeLocalNodes, &error); if (users->node == NULL) { my_log(LOG_NOTICE, "AFPUserList_init: ODNodeCreateWithNodeType() failed"); goto failed; } query = ODQueryCreateWithNode(NULL, users->node, // inNode CFSTR(kDSStdRecordTypeUsers), // inRecordTypeOrList CFSTR(NIPROP__CREATOR), // inAttribute kODMatchEqualTo, // inMatchType CFSTR(BSDPD_CREATOR), // inQueryValueOrList CFSTR(kDSAttributesAll), // inReturnAttributeOrList 0, // inMaxResults &error); if (query == NULL) { my_log(LOG_NOTICE, "AFPUserList_init: ODQueryCreateWithNode() failed"); my_CFRelease(&error); goto failed; } results = ODQueryCopyResults(query, FALSE, &error); CFRelease(query); if (results == NULL) { my_log(LOG_NOTICE, "AFPUserList_init: ODQueryCopyResults() failed"); my_CFRelease(&error); goto failed; } users->list = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); n = CFArrayGetCount(results); for (i = 0; i < n; i++) { ODRecordRef record; AFPUserRef user; record = (ODRecordRef)CFArrayGetValueAtIndex(results, i); user = AFPUser_create(record); CFArrayAppendValue(users->list, user); CFRelease(user); } CFRelease(results); return (TRUE); failed: AFPUserList_free(users); return (FALSE); }
CFArrayRef WBODCopyVisibleUsersAttributes(ODAttributeType attribute, ...) { assert(attribute); CFArrayRef required = CFArrayCreate(kCFAllocatorDefault, (const void **)(ODAttributeType[]) { kODAttributeTypePassword, kODAttributeTypeUserShell }, 2, &kCFTypeArrayCallBacks); CFErrorRef error; ODQueryRef query = ODQueryCreateWithNodeType(kCFAllocatorDefault, kODNodeTypeLocalNodes, kODRecordTypeUsers, kODAttributeTypeAllAttributes, kODMatchAny, NULL, required, 0, &error); CFRelease(required); if (!query) { CFRelease(error); return NULL; } CFArrayRef records = ODQueryCopyResults(query, false, &error); CFRelease(query); if (!records) { if (error) CFRelease(error); return NULL; } va_list args; va_start(args, attribute); CFMutableArrayRef requested = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); CFStringRef attr = attribute; do { CFArrayAppendValue(requested, attr); attr = va_arg(args, CFStringRef); } while (attr); va_end(args); CFMutableArrayRef users = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); for (CFIndex idx = 0, count = CFArrayGetCount(records); idx < count; ++idx) { ODRecordRef record = (ODRecordRef)CFArrayGetValueAtIndex(records, idx); CFStringRef shell = WBODRecordCopyFirstValue(record, kODAttributeTypeUserShell); if (shell && !CFEqual(shell, CFSTR("/usr/bin/false"))) { CFStringRef passwd = WBODRecordCopyFirstValue(record, kODAttributeTypePassword); if (passwd && !CFEqual(passwd, CFSTR("*"))) { CFDictionaryRef user = WBODRecordCopyAttributes(record, requested); if (user) { CFArrayAppendValue(users, user); CFRelease(user); } } if (passwd) CFRelease(passwd); } if (shell) CFRelease(shell); } CFRelease(requested); CFRelease(records); return users; }
CFDictionaryRef WBODCopyUserAttributes(CFStringRef username, ODAttributeType attribute, ...) { assert(username && attribute); if (!username || !attribute) return NULL; CFMutableArrayRef attributes = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); if (!attributes) return NULL; va_list args; va_start(args, attribute); do { CFArrayAppendValue(attributes, attribute); attribute = va_arg(args, ODAttributeType); } while (attribute); va_end(args); CFErrorRef error; ODQueryRef query = ODQueryCreateWithNodeType(kCFAllocatorDefault, kODNodeTypeLocalNodes, kODRecordTypeUsers, kODAttributeTypeRecordName, kODMatchEqualTo, username, attributes, 0, &error); CFArrayRef records = ODQueryCopyResults(query, false, &error); CFRelease(query); if (!records) { CFRelease(attributes); if (error) CFRelease(error); return NULL; } CFMutableDictionaryRef result = NULL; if (CFArrayGetCount(records) >= 1) { result = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); ODRecordRef user = (ODRecordRef)CFArrayGetValueAtIndex(records, 0); for (CFIndex idx = 0, count = CFArrayGetCount(attributes); idx < count; ++idx) { CFStringRef attr = CFArrayGetValueAtIndex(attributes, idx); CFTypeRef value = WBODRecordCopyFirstValue(user, attr); if (value) { CFDictionarySetValue(result, attr, value); CFRelease(value); } } } CFRelease(records); CFRelease(attributes); return result; }
static __inline__ Boolean S_uid_taken(AFPUserListRef users, CFStringRef uid) { CFErrorRef error; Boolean taken = FALSE; ODQueryRef query; CFArrayRef results; query = ODQueryCreateWithNode(NULL, users->node, // inNode CFSTR(kDSStdRecordTypeUsers), // inRecordTypeOrList CFSTR(kDS1AttrUniqueID), // inAttribute kODMatchEqualTo, // inMatchType uid, // inQueryValueOrList NULL, // inReturnAttributeOrList 0, // inMaxResults &error); if (query == NULL) { my_log(LOG_NOTICE, "S_uid_taken: ODQueryCreateWithNode() failed"); my_CFRelease(&error); goto failed; } results = ODQueryCopyResults(query, FALSE, &error); CFRelease(query); if (results == NULL) { my_log(LOG_NOTICE, "S_uid_taken: ODQueryCopyResults() failed"); my_CFRelease(&error); goto failed; } if (CFArrayGetCount(results) > 0) { taken = TRUE; } CFRelease(results); failed: return (taken); }
/* ------------------------------------------------------------------ * get_user_attributes () */ static int get_user_attributes ( ODNodeRef in_node_ref, const char *in_user_name, struct od_user_opts *in_out_opts ) { CFStringRef cf_str_ref = CFStringCreateWithCString( NULL, in_user_name, kCFStringEncodingUTF8 ); if ( !cf_str_ref ) { msg_error( "aod: unable to create user name CFStringRef"); return( -1 ); } /* look up user record */ ODRecordRef od_rec_ref = NULL; CFErrorRef cf_err_ref = NULL; CFTypeRef cf_type_ref[] = { CFSTR(kDSAttributesStandardAll) }; CFArrayRef cf_arry_ref = CFArrayCreate( NULL, cf_type_ref, 1, &kCFTypeArrayCallBacks ); ODQueryRef cf_query_ref = ODQueryCreateWithNode( NULL, in_node_ref, CFSTR(kDSStdRecordTypeUsers), CFSTR(kDSNAttrRecordName), kODMatchInsensitiveEqualTo, cf_str_ref, cf_arry_ref, 100, &cf_err_ref ); if ( cf_query_ref ) { CFArrayRef cf_arry_result = ODQueryCopyResults( cf_query_ref, false, &cf_err_ref ); if ( cf_arry_result ) { if ( CFArrayGetCount( cf_arry_result ) == 1 ) { od_rec_ref = (ODRecordRef)CFArrayGetValueAtIndex( cf_arry_result, 0 ); CFRetain(od_rec_ref); } else { if ( CFArrayGetCount( cf_arry_result ) == 0 ) msg_error( "aod: no user record found for: %s", in_user_name ); else msg_error( "aod: multiple user records (%ld) found for: %s", CFArrayGetCount( cf_arry_result ), in_user_name ); } CFRelease(cf_arry_result); } else print_cf_error( cf_err_ref, in_user_name, "aod: ODQueryCopyResults() failed" ); CFRelease( cf_query_ref ); } else print_cf_error( cf_err_ref, in_user_name, "aod: ODQueryCreateWithNode() failed" ); CFRelease( cf_str_ref ); CFRelease( cf_arry_ref ); if ( !od_rec_ref ) { /* print the error and bail */ print_cf_error( cf_err_ref, in_user_name, "aod: unable to lookup user record" ); return( -1 ); } /* get guid */ char user_guid[ 64 ]; size_t str_size = 0; memset(user_guid, 0, sizeof user_guid); CFStringRef cf_str_value = get_attr_from_record( od_rec_ref, CFSTR(kDS1AttrGeneratedUID) ); if ( cf_str_value ) { str_size = CFStringGetMaximumSizeForEncoding(CFStringGetLength( cf_str_value ), kCFStringEncodingUTF8) + 1; CFStringGetCString( cf_str_value, user_guid, str_size, kCFStringEncodingUTF8 ); CFRelease( cf_str_value ); } /* get record name */ cf_str_value = ODRecordGetRecordName( od_rec_ref ); if ( cf_str_value ) str_size = CFStringGetMaximumSizeForEncoding(CFStringGetLength( cf_str_value ), kCFStringEncodingUTF8) + 1; if (str_size ) CFStringGetCString( cf_str_value, in_out_opts->fRecName, sizeof(in_out_opts->fRecName), kCFStringEncodingUTF8 ); /* get mail attribute */ if ( !get_attributes_local(in_out_opts, user_guid) ) { cf_str_value = get_attr_from_record( od_rec_ref, CFSTR(kDS1AttrMailAttribute) ); if ( cf_str_value != NULL ) { str_size = CFStringGetMaximumSizeForEncoding(CFStringGetLength( cf_str_value ), kCFStringEncodingUTF8) + 1; char *c_str = malloc( str_size ); if ( c_str ) { if( CFStringGetCString( cf_str_value, c_str, str_size, kCFStringEncodingUTF8 ) ) { CFMutableDictionaryRef user_dict = get_mail_attribute_values( c_str, in_out_opts ); set_attributes_local(user_dict, user_guid); CFRelease(user_dict); } free( c_str ); } CFRelease( cf_str_value ); } } CFRelease(od_rec_ref); return( 0 ); } /* get_user_attributes */
int benchmark(void *tsd, result_t *res) { tsd_t *ts = (tsd_t *)tsd; int i; ODNodeRef node; CFErrorRef error; CFArrayRef results; ODQueryRef query; res->re_errors = 0; node = ts->node; debug("in to benchmark - optB = %i, node = 0x%lx \n", lm_optB, node); for (i = 0; i < lm_optB; i++) { debug("loop %d: querying\n", i); query = ODQueryCreateWithNode(NULL, node, // inNode rectype_dict[optType], // inRecordTypeOrList CFSTR(kDSNAttrRecordName), // inAttribute kODMatchInsensitiveEqualTo, // inMatchType key[i % optRecords], // inQueryValueOrList NULL, // inReturnAttributeOrList 1, // inMaxResults &error); if (query) { // we do not want to factually fetch the result in benchmark run // debug("loop %d: calling ODQueryCopyResults\n", i); results = ODQueryCopyResults(query, FALSE, &error); CFRelease(query); if (results) { #if DEBUG int c; c = CFArrayGetCount(results); if (c > 0) { debug("Successful run: %d results, ", c); } else { debug("no result for "); } CFShow (key[i % optRecords]); debug("\n"); #endif CFRelease(results); } else { debug("loop %d: ODQueryCopyResults returned empty result for ", i); res->re_errors++; CFShow (key[i % optRecords]); debug("\n"); } // if (results) } // if (query) else { res->re_errors++; } } res->re_count = i; return (0); }
int odkerb_copy_user_record_with_alt_security_identity(CFStringRef principalID, ODRecordRef *out) { int retval = -1; CFStringRef cfAltSecurityIdentity = NULL; CFErrorRef cfError = NULL; ODQueryRef cfQueryRef = NULL; CFArrayRef cfUserRecords = NULL; int mbrErr = 0; uuid_t user_uuid = { 0 }; uuid_string_t uuidStr; char principal[1024]; ODKERB_PARAM_ASSERT(principalID != NULL); ODKERB_PARAM_ASSERT(out != 0); *out = NULL; if (odkerb_configure_search_node() != 0) goto failure; CFStringGetCString(principalID, principal, sizeof(principal), kCFStringEncodingUTF8); mbrErr = mbr_identifier_to_uuid(ID_TYPE_KERBEROS, principal, strlen(principal), user_uuid); if (mbrErr != 0) { ODKERB_LOG_CFSTRING(LOG_DEBUG, "No UUID found for principal by mbr_identifier_to_uuid ID_TYPE_KERBEROS", principalID); goto failure; } uuid_unparse_upper(user_uuid, uuidStr); CFStringRef cfUserUUID = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%s"), uuidStr); cfQueryRef = ODQueryCreateWithNode(kCFAllocatorDefault, gSearchNode, kODRecordTypeUsers, kODAttributeTypeGUID, kODMatchEqualTo, cfUserUUID, kODAttributeTypeRecordName, 1, &cfError); if (cfQueryRef == NULL || cfError != NULL) { ODKERB_LOG_CFERROR(LOG_ERR, "Unable to query the search node", cfError); goto failure; } cfUserRecords = ODQueryCopyResults(cfQueryRef, false, &cfError); if (cfUserRecords == NULL || cfError != NULL) { ODKERB_LOG_CFERROR(LOG_ERR, "Unable to find user record", cfError); goto failure; } else if (CFArrayGetCount(cfUserRecords) == 0) { ODKERB_LOG_CFSTRING(LOG_INFO, "Unable to find user record", cfUserUUID); goto failure; } ODRecordRef cfUserRecord = (ODRecordRef)CFArrayGetValueAtIndex(cfUserRecords, 0); *out = (ODRecordRef)CFRetain(cfUserRecord); retval = 0; failure: if (cfError != NULL) odkerb_possibly_reset_search_node(cfError); CF_SAFE_RELEASE(cfError); CF_SAFE_RELEASE(cfAltSecurityIdentity); CF_SAFE_RELEASE(cfQueryRef); CF_SAFE_RELEASE(cfUserRecords); return retval; }