2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2005 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
16 * This work was initially developed by Kurt Spanier for inclusion
17 * in OpenLDAP Software.
24 #include <ac/stdlib.h>
28 #include <ac/socket.h>
29 #include <ac/string.h>
30 #include <ac/unistd.h>
33 #define LDAP_DEPRECATED 1
40 do_search( char *uri, char *host, int port, char *manager, char *passwd,
41 char *sbase, char *filter, int maxloop, int maxretries );
46 fprintf( stderr, "usage: %s [-h <host>] -p port -b <searchbase> -f <searchfiter> [-l <loops>]\n",
52 main( int argc, char **argv )
56 char *host = "localhost";
63 int retries = RETRIES;
65 while ( (i = getopt( argc, argv, "b:D:f:H:h:l:p:w:r:" )) != EOF ) {
67 case 'H': /* the server uri */
68 uri = strdup( optarg );
71 case 'h': /* the servers host */
72 host = strdup( optarg );
75 case 'p': /* the servers port */
76 port = atoi( optarg );
79 case 'D': /* the servers manager */
80 manager = strdup( optarg );
83 case 'w': /* the server managers password */
84 passwd = strdup( optarg );
87 case 'b': /* file with search base */
88 sbase = strdup( optarg );
91 case 'f': /* the search request */
92 filter = strdup( optarg );
95 case 'l': /* number of loops */
96 loops = atoi( optarg );
99 case 'r': /* number of retries */
100 retries = atoi( optarg );
109 if (( sbase == NULL ) || ( filter == NULL ) || ( port == -1 && uri == NULL ))
112 if ( *filter == '\0' ) {
114 fprintf( stderr, "%s: invalid EMPTY search filter.\n",
116 exit( EXIT_FAILURE );
120 do_search( uri, host, port, manager, passwd, sbase, filter,
121 ( 10 * loops ), retries );
122 exit( EXIT_SUCCESS );
127 do_search( char *uri, char *host, int port, char *manager, char *passwd,
128 char *sbase, char *filter, int maxloop, int maxretries )
131 int i = 0, do_retry = maxretries;
132 char *attrs[] = { "cn", "sn", NULL };
133 pid_t pid = getpid();
134 int rc = LDAP_SUCCESS;
138 ldap_initialize( &ld, uri );
140 ld = ldap_init( host, port );
143 perror( "ldap_init" );
144 exit( EXIT_FAILURE );
148 int version = LDAP_VERSION3;
149 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
153 if ( do_retry == maxretries ) {
154 fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\".\n",
155 (long) pid, maxloop, sbase, filter );
158 rc = ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE );
159 if ( rc != LDAP_SUCCESS ) {
160 if ( rc == LDAP_BUSY && do_retry == 1 ) {
164 ldap_perror( ld, "ldap_bind" );
165 exit( EXIT_FAILURE );
168 for ( i = 0; i < maxloop; i++ ) {
171 rc = ldap_search_s( ld, sbase, LDAP_SCOPE_SUBTREE,
172 filter, attrs, 0, &res );
173 if ( rc != LDAP_SUCCESS ) {
174 ldap_perror( ld, "ldap_search" );
175 if ( rc == LDAP_BUSY && do_retry == 1 ) {
179 if ( rc != LDAP_NO_SUCH_OBJECT ) break;
187 fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );