/* Netprog 2000 */ /* Sample LDAP client based on the code in RFC 1823 - LDAP API This example uses synchronous calls, see async_client for an example that uses asynchronous LDAP requests */ #include #include /* the encoding library */ #include /* the LDAP library */ #include /* The name of the server is hard-coded here! In general we would want this to be specified on the command line, or in a config file somewhere! */ char *serverhost = "monte.cs.rpi.edu"; int main(void) { LDAP *ld; /* ldap connection handle */ LDAPMessage *res, *e; /* point to LDAP messages */ int i; char *a, *dn; void *ptr; char **vals; /* open a connection to the LDAP server running on the default LDAP port on the machine serverhost */ if ( (ld = ldap_open( serverhost, LDAP_PORT )) == NULL ) { printf("Problem opening LDAP connection to %s\n",serverhost); exit( 1 ); } /* authenticate as nobody */ if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) { /* ldap_perror is like perror, but uses the ldap_errno */ ldap_perror( ld, "ldap_simple_bind_s" ); exit( 1 ); } /* search for all records that have the substring "ho" as part of the cn attribute (cn is "common name" - used to hold a person's name) parameters are: ld - the ldap connection handle the base is hard-coded to "course=Network Programming, school=RPI" this works only with the demo server running for netpworg 2000! the scope is LDAP_SCOPE_SUBTREE - means the server should search everything below the base of the search the search filter is "(cn=*ho*)" attrs is NULL - this means we are not specifying what attributes we want to get - so we get them all. We could include a null terminated array of char * pointing to attribute names, and only get those attributes returned by the server. attrsonly flag is set to 0, this means we get both the attribute names and values from the server (if set to 1 we only get the attribute names). res is the address of a pointer to an LDAP message. If the search succeeds *res will be changed to point to an LDAP message that holds the result. */ if ( ldap_search_s( ld, "course=Network Programming, school=RPI", LDAP_SCOPE_SUBTREE, "(cn=*ho*)", NULL, 0, &res ) != LDAP_SUCCESS ) { ldap_perror( ld, "ldap_search_s" ); exit( 1 ); } /* The search has worked - now we go through the result and print out the stuff returned in res. */ /* step through each entry returned */ for ( e = ldap_first_entry( ld, res ); e != NULL; e = ldap_next_entry( ld, e ) ) { /* e is a pointer to an ldap message that holds one record that matched our search filter */ /* print the distinguished name associated with the record */ dn = ldap_get_dn( ld, e ); printf( "dn: %s\n", dn ); /* ldap_get_dn creates a copy for us - we need to free it */ free( dn ); /* Print the name each attribute in this record */ /* Some implementations want the third parameter to be of type **BerElement instead of **void -so the compiler may complain here. You can ignore the warning... */ for ( a = ldap_first_attribute( ld, e, &ptr ); a != NULL; a = ldap_next_attribute( ld, e, ptr ) ) { printf( " attribute: %s: ", a ); /* and print all the values (there can be multiple values for each attribute ) */ vals = ldap_get_values( ld, e, a ); for ( i = 0; vals[i] != NULL; i++ ) { printf( "%s ", vals[i] ); } printf("\n"); /* vals was a copy of the values - must free it! */ ldap_value_free( vals ); } printf("\n\n"); } /* free the search results */ ldap_msgfree( res ); /* close and free connection resources */ ldap_unbind( ld ); return(0); }