3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/stdlib.h>
14 #include <ac/socket.h>
15 #include <ac/string.h>
16 #include <ac/unistd.h>
25 do_search( char *host, int port, char *sbase, char *filter, int maxloop );
30 fprintf( stderr, "usage: %s [-h <host>] -p port -b <searchbase> -f <searchfiter> [-l <loops>]\n",
36 main( int argc, char **argv )
39 char *host = "localhost";
45 while ( (i = getopt( argc, argv, "h:p:b:f:l:" )) != EOF ) {
47 case 'h': /* the servers host */
48 host = strdup( optarg );
51 case 'p': /* the servers port */
52 port = atoi( optarg );
55 case 'b': /* file with search base */
56 sbase = strdup( optarg );
59 case 'f': /* the search request */
60 filter = strdup( optarg );
63 case 'l': /* number of loops */
64 loops = atoi( optarg );
73 if (( sbase == NULL ) || ( filter == NULL ) || ( port == -1 ))
76 if ( *filter == '\0' ) {
78 fprintf( stderr, "%s: invalid EMPTY search filter.\n",
84 do_search( host, port, sbase, filter, ( 10 * loops ));
90 do_search( char *host, int port, char *sbase, char *filter, int maxloop )
94 char *attrs[] = { "cn", "sn", NULL };
97 if (( ld = ldap_init( host, port )) == NULL ) {
98 perror( "ldap_init" );
103 int version = LDAP_VERSION3;
104 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
108 if ( ldap_bind_s( ld, NULL, NULL, LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
109 ldap_perror( ld, "ldap_bind" );
110 exit( EXIT_FAILURE );
114 fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\".\n",
115 (long) pid, maxloop, sbase, filter );
117 for ( i = 0; i < maxloop; i++ ) {
121 if (( rc = ldap_search_s( ld, sbase, LDAP_SCOPE_SUBTREE,
122 filter, attrs, 0, &res )) != LDAP_SUCCESS ) {
124 ldap_perror( ld, "ldap_search" );
125 if ( rc != LDAP_NO_SUCH_OBJECT ) break;
133 fprintf( stderr, " PID=%ld - Search done.\n", (long) pid );