2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2007 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
41 do_search( char *uri, char *host, int port, char *manager, char *passwd,
42 char *sbase, char *filter, int maxloop, int maxretries, int delay );
49 "-H <uri> | ([-h <host>] -p <port>) "
62 main( int argc, char **argv )
66 char *host = "localhost";
73 int retries = RETRIES;
76 while ( (i = getopt( argc, argv, "b:D:f:H:h:l:p:w:r:t:" )) != EOF ) {
78 case 'H': /* the server uri */
79 uri = strdup( optarg );
82 case 'h': /* the servers host */
83 host = strdup( optarg );
86 case 'p': /* the servers port */
87 if ( lutil_atoi( &port, optarg ) != 0 ) {
92 case 'D': /* the servers manager */
93 manager = strdup( optarg );
96 case 'w': /* the server managers password */
97 passwd = strdup( optarg );
100 case 'b': /* file with search base */
101 sbase = strdup( optarg );
104 case 'f': /* the search request */
105 filter = strdup( optarg );
108 case 'l': /* number of loops */
109 if ( lutil_atoi( &loops, optarg ) != 0 ) {
114 case 'r': /* number of retries */
115 if ( lutil_atoi( &retries, optarg ) != 0 ) {
120 case 't': /* delay in seconds */
121 if ( lutil_atoi( &delay, optarg ) != 0 ) {
132 if (( sbase == NULL ) || ( filter == NULL ) || ( port == -1 && uri == NULL ))
135 if ( *filter == '\0' ) {
137 fprintf( stderr, "%s: invalid EMPTY search filter.\n",
139 exit( EXIT_FAILURE );
143 do_search( uri, host, port, manager, passwd, sbase, filter,
144 ( 10 * loops ), retries, delay );
145 exit( EXIT_SUCCESS );
150 do_search( char *uri, char *host, int port, char *manager, char *passwd,
151 char *sbase, char *filter, int maxloop, int maxretries, int delay )
154 int i = 0, do_retry = maxretries;
155 char *attrs[] = { "cn", "sn", NULL };
156 pid_t pid = getpid();
157 int rc = LDAP_SUCCESS;
161 ldap_initialize( &ld, uri );
163 ld = ldap_init( host, port );
166 perror( "ldap_init" );
167 exit( EXIT_FAILURE );
171 int version = LDAP_VERSION3;
172 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
176 if ( do_retry == maxretries ) {
177 fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\".\n",
178 (long) pid, maxloop, sbase, filter );
181 rc = ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE );
182 if ( rc != LDAP_SUCCESS ) {
183 ldap_perror( ld, "ldap_bind" );
186 case LDAP_UNAVAILABLE:
187 if ( do_retry > 0 ) {
198 exit( EXIT_FAILURE );
201 for ( ; i < maxloop; i++ ) {
204 rc = ldap_search_s( ld, sbase, LDAP_SCOPE_SUBTREE,
205 filter, attrs, 0, &res );
206 if ( rc != LDAP_SUCCESS ) {
207 ldap_perror( ld, "ldap_search" );
208 if ( rc == LDAP_BUSY && do_retry > 0 ) {
212 if ( rc != LDAP_NO_SUCH_OBJECT ) break;
220 fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );