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,
46 fprintf( stderr, "usage: %s [-h <host>] -p port -e <entry> [-l <loops>]\n",
52 main( int argc, char **argv )
56 char *host = "localhost";
60 int retries = RETRIES;
62 while ( (i = getopt( argc, argv, "H:h:p:e:l:r:" )) != EOF ) {
64 case 'H': /* the server uri */
65 uri = strdup( optarg );
68 case 'h': /* the servers host */
69 host = strdup( optarg );
72 case 'p': /* the servers port */
73 port = atoi( optarg );
76 case 'e': /* DN to search for */
77 entry = strdup( optarg );
80 case 'l': /* the number of loops */
81 loops = atoi( optarg );
84 case 'r': /* the number of retries */
85 retries = atoi( optarg );
94 if (( entry == NULL ) || ( port == -1 && uri == NULL ))
97 if ( *entry == '\0' ) {
98 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
100 exit( EXIT_FAILURE );
103 do_read( uri, host, port, entry, ( 20 * loops ), retries );
104 exit( EXIT_SUCCESS );
109 do_read( char *uri, char *host, int port, char *entry, int maxloop,
113 int i = 0, do_retry = maxretries;
114 char *attrs[] = { "1.1", NULL };
115 pid_t pid = getpid();
116 int rc = LDAP_SUCCESS;
120 ldap_initialize( &ld, uri );
122 ld = ldap_init( host, port );
125 perror( "ldap_init" );
126 exit( EXIT_FAILURE );
130 int version = LDAP_VERSION3;
131 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
135 if ( do_retry == maxretries ) {
136 fprintf( stderr, "PID=%ld - Read(%d): entry=\"%s\".\n",
137 (long) pid, maxloop, entry );
140 rc = ldap_bind_s( ld, NULL, NULL, LDAP_AUTH_SIMPLE );
141 if ( rc != LDAP_SUCCESS ) {
142 ldap_perror( ld, "ldap_bind" );
143 if ( rc == LDAP_BUSY && do_retry > 0 ) {
147 exit( EXIT_FAILURE );
150 for ( ; i < maxloop; i++ ) {
153 rc = ldap_search_s( ld, entry, LDAP_SCOPE_BASE,
154 NULL, attrs, 1, &res );
155 if ( rc != LDAP_SUCCESS ) {
156 ldap_perror( ld, "ldap_read" );
157 if ( rc == LDAP_BUSY && do_retry > 0 ) {
161 if ( rc != LDAP_NO_SUCH_OBJECT ) break;
169 fprintf( stderr, " PID=%ld - Read done (%d).\n", (long) pid, rc );