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, int delay );
48 "-H <uri> | ([-h <host>] -p <port>) "
61 main( int argc, char **argv )
65 char *host = "localhost";
72 int retries = RETRIES;
75 while ( (i = getopt( argc, argv, "b:D:f:H:h:l:p:w:r:t:" )) != EOF ) {
77 case 'H': /* the server uri */
78 uri = strdup( optarg );
81 case 'h': /* the servers host */
82 host = strdup( optarg );
85 case 'p': /* the servers port */
86 port = atoi( optarg );
89 case 'D': /* the servers manager */
90 manager = strdup( optarg );
93 case 'w': /* the server managers password */
94 passwd = strdup( optarg );
97 case 'b': /* file with search base */
98 sbase = strdup( optarg );
101 case 'f': /* the search request */
102 filter = strdup( optarg );
105 case 'l': /* number of loops */
106 loops = atoi( optarg );
109 case 'r': /* number of retries */
110 retries = atoi( optarg );
113 case 't': /* delay in seconds */
114 delay = atoi( optarg );
123 if (( sbase == NULL ) || ( filter == NULL ) || ( port == -1 && uri == NULL ))
126 if ( *filter == '\0' ) {
128 fprintf( stderr, "%s: invalid EMPTY search filter.\n",
130 exit( EXIT_FAILURE );
134 do_search( uri, host, port, manager, passwd, sbase, filter,
135 ( 10 * loops ), retries, delay );
136 exit( EXIT_SUCCESS );
141 do_search( char *uri, char *host, int port, char *manager, char *passwd,
142 char *sbase, char *filter, int maxloop, int maxretries, int delay )
145 int i = 0, do_retry = maxretries;
146 char *attrs[] = { "cn", "sn", NULL };
147 pid_t pid = getpid();
148 int rc = LDAP_SUCCESS;
152 ldap_initialize( &ld, uri );
154 ld = ldap_init( host, port );
157 perror( "ldap_init" );
158 exit( EXIT_FAILURE );
162 int version = LDAP_VERSION3;
163 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
167 if ( do_retry == maxretries ) {
168 fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\".\n",
169 (long) pid, maxloop, sbase, filter );
172 rc = ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE );
173 if ( rc != LDAP_SUCCESS ) {
174 ldap_perror( ld, "ldap_bind" );
177 case LDAP_UNAVAILABLE:
178 if ( do_retry > 0 ) {
189 exit( EXIT_FAILURE );
192 for ( ; i < maxloop; i++ ) {
195 rc = ldap_search_s( ld, sbase, LDAP_SCOPE_SUBTREE,
196 filter, attrs, 0, &res );
197 if ( rc != LDAP_SUCCESS ) {
198 ldap_perror( ld, "ldap_search" );
199 if ( rc == LDAP_BUSY && do_retry > 0 ) {
203 if ( rc != LDAP_NO_SUCH_OBJECT ) break;
211 fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );