/* * Modified from ldap_search_async.c * (http://www.linuxdevcenter.com/pub/a/linux/2003/08/14/libldap.html) * * compile: gcc -lldap -llber -o ldapsrch ldapsrch.c * * usage: ldapsrch -h * -c : cause a crash * -b * -n * (nr of asysnc searches to perform) * -D * -W : ask password * -v : verbose output (all attributes, otherwise only DN) * * * first non option argument will be used as a filter * * Example: ldapsrch -h localhost -c -b 'ou=people,dc=example,dc=com' \ * -D 'cn=manager,dc=example,dc=com' -W '(ou=sales)' * */ #include #include #include #include #include #include #define RCSID "$Id: ldap_search_async.c,v 1.4 2003/07/14 15:21:56 rory Exp $" #define MAX_SRCH_COUNT 1024 int main( int argc, char *argv[] ) { LDAP *ld; int result; int auth_method = LDAP_AUTH_SIMPLE; int desired_version = LDAP_VERSION3; int srch_cnt = 2; char *ldap_host = "localhost"; char *root_dn = "cn=Manager, dc=example, dc=com"; char *root_pw = "secret"; BerElement* ber; LDAPMessage* msg; LDAPMessage* entry; char* base="ou=persoon,dc=example,dc=com"; char* filter="(objectClass=*)"; char* errstring; char* dn = NULL; char* attr; char** vals; int i; int msgid[MAX_SRCH_COUNT]; int verbose = 0; struct timeval tm; int c, count; int crash = 0; opterr = 0; while ((c = getopt (argc, argv, "n:b:D:Wch:vw:")) != -1) { switch (c) { case 'n': srch_cnt = atoi(optarg); if (srch_cnt > MAX_SRCH_COUNT) { printf("max srch count = %d\n", MAX_SRCH_COUNT); srch_cnt = MAX_SRCH_COUNT; } if ( srch_cnt < 1 ) { srch_cnt = 1; } break; case 'b': base = optarg; break; case 'D': root_dn = optarg; break; case 'W': root_pw = getpass("Password:"); break; case 'w': root_pw = optarg; break; case 'c': crash = 1; break; case 'h': ldap_host = optarg; break; case 'v': verbose = 1; break; default: abort (); } } if (optind < argc) { filter = argv[optind]; } printf("connecting\n"); if ((ld = ldap_init(ldap_host, LDAP_PORT)) == NULL ) { perror( "ldap_init failed" ); exit( EXIT_FAILURE ); } /* set the LDAP version to be 3 */ if (ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &desired_version) != LDAP_OPT_SUCCESS) { ldap_perror(ld, "ldap_set_option"); exit(EXIT_FAILURE); } if (ldap_bind_s(ld, root_dn, root_pw, auth_method) != LDAP_SUCCESS ) { ldap_perror( ld, "ldap_bind" ); exit( EXIT_FAILURE ); } printf("starting searches\n"); setbuf(stdout, NULL); /* ldap_search() returns -1 if there is an error, otherwise the msgid */ for (count=0; count