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_read( char *uri, char *host, int port, char *entry, int maxloop,
42 int maxretries, int delay );
49 "-H <uri> | ([-h <host>] -p <port>) "
59 main( int argc, char **argv )
63 char *host = "localhost";
67 int retries = RETRIES;
70 while ( (i = getopt( argc, argv, "H:h:p:e:l:r:t:" )) != EOF ) {
72 case 'H': /* the server uri */
73 uri = strdup( optarg );
76 case 'h': /* the servers host */
77 host = strdup( optarg );
80 case 'p': /* the servers port */
81 if ( lutil_atoi( &port, optarg ) != 0 ) {
86 case 'e': /* DN to search for */
87 entry = strdup( optarg );
90 case 'l': /* the number of loops */
91 if ( lutil_atoi( &loops, optarg ) != 0 ) {
96 case 'r': /* the number of retries */
97 if ( lutil_atoi( &retries, optarg ) != 0 ) {
102 case 't': /* delay in seconds */
103 if ( lutil_atoi( &delay, optarg ) != 0 ) {
114 if (( entry == NULL ) || ( port == -1 && uri == NULL ))
117 if ( *entry == '\0' ) {
118 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
120 exit( EXIT_FAILURE );
123 do_read( uri, host, port, entry, ( 20 * loops ), retries, delay );
124 exit( EXIT_SUCCESS );
129 do_read( char *uri, char *host, int port, char *entry, int maxloop,
130 int maxretries, int delay )
133 int i = 0, do_retry = maxretries;
134 char *attrs[] = { "1.1", NULL };
135 pid_t pid = getpid();
136 int rc = LDAP_SUCCESS;
140 ldap_initialize( &ld, uri );
142 ld = ldap_init( host, port );
145 perror( "ldap_init" );
146 exit( EXIT_FAILURE );
150 int version = LDAP_VERSION3;
151 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
155 if ( do_retry == maxretries ) {
156 fprintf( stderr, "PID=%ld - Read(%d): entry=\"%s\".\n",
157 (long) pid, maxloop, entry );
160 rc = ldap_bind_s( ld, NULL, NULL, LDAP_AUTH_SIMPLE );
161 if ( rc != LDAP_SUCCESS ) {
162 ldap_perror( ld, "ldap_bind" );
165 case LDAP_UNAVAILABLE:
166 if ( do_retry > 0 ) {
177 exit( EXIT_FAILURE );
180 for ( ; i < maxloop; i++ ) {
183 rc = ldap_search_s( ld, entry, LDAP_SCOPE_BASE,
184 NULL, attrs, 1, &res );
185 if ( rc != LDAP_SUCCESS ) {
186 ldap_perror( ld, "ldap_read" );
187 if ( rc == LDAP_BUSY && do_retry > 0 ) {
191 if ( rc != LDAP_NO_SUCH_OBJECT ) break;
199 fprintf( stderr, " PID=%ld - Read done (%d).\n", (long) pid, rc );