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_read( char *uri, char *host, int port, char *entry, int maxloop,
41 int maxretries, int delay );
48 "-H <uri> | ([-h <host>] -p <port>) "
58 main( int argc, char **argv )
62 char *host = "localhost";
66 int retries = RETRIES;
69 while ( (i = getopt( argc, argv, "H:h:p:e:l:r:t:" )) != EOF ) {
71 case 'H': /* the server uri */
72 uri = strdup( optarg );
75 case 'h': /* the servers host */
76 host = strdup( optarg );
79 case 'p': /* the servers port */
80 port = atoi( optarg );
83 case 'e': /* DN to search for */
84 entry = strdup( optarg );
87 case 'l': /* the number of loops */
88 loops = atoi( optarg );
91 case 'r': /* the number of retries */
92 retries = atoi( optarg );
95 case 't': /* delay in seconds */
96 delay = atoi( optarg );
105 if (( entry == NULL ) || ( port == -1 && uri == NULL ))
108 if ( *entry == '\0' ) {
109 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
111 exit( EXIT_FAILURE );
114 do_read( uri, host, port, entry, ( 20 * loops ), retries, delay );
115 exit( EXIT_SUCCESS );
120 do_read( char *uri, char *host, int port, char *entry, int maxloop,
121 int maxretries, int delay )
124 int i = 0, do_retry = maxretries;
125 char *attrs[] = { "1.1", NULL };
126 pid_t pid = getpid();
127 int rc = LDAP_SUCCESS;
131 ldap_initialize( &ld, uri );
133 ld = ldap_init( host, port );
136 perror( "ldap_init" );
137 exit( EXIT_FAILURE );
141 int version = LDAP_VERSION3;
142 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
146 if ( do_retry == maxretries ) {
147 fprintf( stderr, "PID=%ld - Read(%d): entry=\"%s\".\n",
148 (long) pid, maxloop, entry );
151 rc = ldap_bind_s( ld, NULL, NULL, LDAP_AUTH_SIMPLE );
152 if ( rc != LDAP_SUCCESS ) {
153 ldap_perror( ld, "ldap_bind" );
156 case LDAP_UNAVAILABLE:
157 if ( do_retry > 0 ) {
168 exit( EXIT_FAILURE );
171 for ( ; i < maxloop; i++ ) {
174 rc = ldap_search_s( ld, entry, LDAP_SCOPE_BASE,
175 NULL, attrs, 1, &res );
176 if ( rc != LDAP_SUCCESS ) {
177 ldap_perror( ld, "ldap_read" );
178 if ( rc == LDAP_BUSY && do_retry > 0 ) {
182 if ( rc != LDAP_NO_SUCH_OBJECT ) break;
190 fprintf( stderr, " PID=%ld - Read done (%d).\n", (long) pid, rc );