From 2ed1dfcf4e467a2816f3b768c8b37f2cdf3c7758 Mon Sep 17 00:00:00 2001 From: Jim McDonough Date: Fri, 29 Mar 2002 21:06:33 +0000 Subject: Added ads_process_results(), which takes a function that is called for each entry returned from a search, and applies it to the results. Re-structured ads_dump to use this, plus changed the ber_free in ads_dump from (b,1) to (b,0), in accordance with openldap manpages. Also allows proper free of result using ldap_msgfree afterwards, so you can do something with the results after an ads_dump. (This used to be commit f01f02fc569b4f5101a37d3b493f2fe2d2b2677a) --- source3/libads/ldap.c | 93 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 73 insertions(+), 20 deletions(-) diff --git a/source3/libads/ldap.c b/source3/libads/ldap.c index 431c32a35c..9670327dbe 100644 --- a/source3/libads/ldap.c +++ b/source3/libads/ldap.c @@ -700,14 +700,12 @@ static void dump_string(const char *field, struct berval **values) } /* - dump a record from LDAP on stdout + dump a field from LDAP on stdout used for debugging */ -void ads_dump(ADS_STRUCT *ads, void *res) + +static void ads_dump_field(char *field, void **values, void *data_area) { - char *field; - void *msg; - BerElement *b; struct { char *name; void (*handler)(const char *, struct berval **); @@ -717,34 +715,89 @@ void ads_dump(ADS_STRUCT *ads, void *res) {"objectSid", dump_sid}, {NULL, NULL} }; - - for (msg = ads_first_entry(ads, res); msg; msg = ads_next_entry(ads, msg)) { + int i; + + if (!field) { /* must be end of an entry */ + printf("\n"); + return; + } + + for (i=0; handlers[i].name; i++) { + if (StrCaseCmp(handlers[i].name, field) == 0) { + handlers[i].handler(field, (struct berval **) values); + break; + } + } + if (!handlers[i].name) { + dump_string(field, (struct berval **) values); + } +} + +/* + dump a result from LDAP on stdout + used for debugging +*/ + +void ads_dump(ADS_STRUCT *ads, void *res) +{ + ads_process_results(ads, res, ads_dump_field, NULL); +} + +/* + Walk through results, calling a function for each entry found. + The function receives a field name, a berval * array of values, + and a data area passed through from the start. The function is + called once with null for field and values at the end of each + entry. +*/ +void ads_process_results(ADS_STRUCT *ads, void *res, + void(*fn)(char *, void **, void *), + void *data_area) +{ + void *msg; + + for (msg = ads_first_entry(ads, res); msg; + msg = ads_next_entry(ads, msg)) { + char *field; + BerElement *b; + for (field = ldap_first_attribute(ads->ld, (LDAPMessage *)msg, &b); field; field = ldap_next_attribute(ads->ld, (LDAPMessage *)msg, b)) { struct berval **values; - int i; - + values = ldap_get_values_len(ads->ld, (LDAPMessage *)msg, field); + fn(field, (void **) values, data_area); - for (i=0; handlers[i].name; i++) { - if (StrCaseCmp(handlers[i].name, field) == 0) { - handlers[i].handler(field, values); - break; - } - } - if (!handlers[i].name) { - dump_string(field, values); - } ldap_value_free_len(values); ldap_memfree(field); } + ber_free(b, 0); + fn(NULL, NULL, data_area); /* completed an entry */ - ber_free(b, 1); - printf("\n"); } } +void ads_process_entry(ADS_STRUCT *ads, void *msg, + void(*fn)(ADS_STRUCT *, char *, void **, void *), + void *data_area) +{ + char *field; + BerElement *b; + + for (field = ldap_first_attribute(ads->ld, (LDAPMessage *)msg, &b); + field; + field = ldap_next_attribute(ads->ld, (LDAPMessage *)msg, b)) { + struct berval **values; + + values = ldap_get_values_len(ads->ld, (LDAPMessage *)msg, field); + fn(ads, field, (void **) values, data_area); + + ldap_value_free_len(values); + ldap_memfree(field); + } + ber_free(b, 0); +} /* count how many replies are in a LDAPMessage */ -- cgit