]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-read.c
don't chase referrals (essentially, because it may cause an endless loop in libldap...
[openldap] / tests / progs / slapd-read.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2006 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
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>.
14  */
15 /* ACKNOWLEDGEMENTS:
16  * This work was initially developed by Kurt Spanier for inclusion
17  * in OpenLDAP Software.
18  */
19
20 #include "portable.h"
21
22 #include <stdio.h>
23
24 #include <ac/stdlib.h>
25
26 #include <ac/ctype.h>
27 #include <ac/param.h>
28 #include <ac/socket.h>
29 #include <ac/string.h>
30 #include <ac/unistd.h>
31 #include <ac/wait.h>
32
33 #include <ldap.h>
34 #include <lutil.h>
35
36 #include "slapd-common.h"
37
38 #define LOOPS   100
39 #define RETRIES 0
40
41 static void
42 do_read( char *uri, char *entry, int maxloop,
43                 int maxretries, int delay, int force );
44
45 static void
46 usage( char *name )
47 {
48         fprintf( stderr,
49                 "usage: %s "
50                 "-H <uri> | ([-h <host>] -p <port>) "
51                 "-e <entry> "
52                 "[-F] "
53                 "[-l <loops>] "
54                 "[-L <outerloops>] "
55                 "[-r <maxretries>] "
56                 "[-t <delay>]\n",
57                 name );
58         exit( EXIT_FAILURE );
59 }
60
61 int
62 main( int argc, char **argv )
63 {
64         int             i;
65         char            *uri = NULL;
66         char            *host = "localhost";
67         int             port = -1;
68         char            *entry = NULL;
69         int             loops = LOOPS;
70         int             outerloops = 1;
71         int             retries = RETRIES;
72         int             delay = 0;
73         int             force = 0;
74
75         tester_init( "slapd-read" );
76
77         while ( (i = getopt( argc, argv, "H:h:p:e:Fl:L:r:t:" )) != EOF ) {
78                 switch( i ) {
79                 case 'H':               /* the server uri */
80                         uri = strdup( optarg );
81                         break;
82
83                 case 'h':               /* the servers host */
84                         host = strdup( optarg );
85                         break;
86
87                 case 'p':               /* the servers port */
88                         if ( lutil_atoi( &port, optarg ) != 0 ) {
89                                 usage( argv[0] );
90                         }
91                         break;
92
93                 case 'e':               /* DN to search for */
94                         entry = strdup( optarg );
95                         break;
96
97                 case 'F':
98                         force++;
99                         break;
100
101                 case 'l':               /* the number of loops */
102                         if ( lutil_atoi( &loops, optarg ) != 0 ) {
103                                 usage( argv[0] );
104                         }
105                         break;
106
107                 case 'L':               /* the number of outerloops */
108                         if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
109                                 usage( argv[0] );
110                         }
111                         break;
112
113                 case 'r':               /* the number of retries */
114                         if ( lutil_atoi( &retries, optarg ) != 0 ) {
115                                 usage( argv[0] );
116                         }
117                         break;
118
119                 case 't':               /* delay in seconds */
120                         if ( lutil_atoi( &delay, optarg ) != 0 ) {
121                                 usage( argv[0] );
122                         }
123                         break;
124
125                 default:
126                         usage( argv[0] );
127                         break;
128                 }
129         }
130
131         if (( entry == NULL ) || ( port == -1 && uri == NULL ))
132                 usage( argv[0] );
133
134         if ( *entry == '\0' ) {
135                 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
136                                 argv[0] );
137                 exit( EXIT_FAILURE );
138         }
139
140         uri = tester_uri( uri, host, port );
141
142         for ( i = 0; i < outerloops; i++ ) {
143                 do_read( uri, entry, loops, retries, delay, force );
144         }
145
146         exit( EXIT_SUCCESS );
147 }
148
149
150 static void
151 do_read( char *uri, char *entry, int maxloop,
152                 int maxretries, int delay, int force )
153 {
154         LDAP    *ld = NULL;
155         int     i = 0, do_retry = maxretries;
156         char    *attrs[] = { "1.1", NULL };
157         pid_t   pid = getpid();
158         int     rc = LDAP_SUCCESS;
159         int     version = LDAP_VERSION3;
160         struct berval   passwd = { 0, "" };
161         int     first = 1;
162         
163 retry:;
164         ldap_initialize( &ld, uri );
165         if ( ld == NULL ) {
166                 tester_perror( "ldap_initialize" );
167                 exit( EXIT_FAILURE );
168         }
169
170         (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ); 
171         (void) ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
172
173         if ( do_retry == maxretries ) {
174                 fprintf( stderr, "PID=%ld - Read(%d): entry=\"%s\".\n",
175                         (long) pid, maxloop, entry );
176         }
177
178         rc = ldap_sasl_bind_s( ld, NULL, LDAP_SASL_SIMPLE, &passwd, NULL, NULL, NULL );
179         if ( rc != LDAP_SUCCESS ) {
180                 tester_ldap_error( ld, "ldap_sasl_bind_s" );
181                 switch ( rc ) {
182                 case LDAP_BUSY:
183                 case LDAP_UNAVAILABLE:
184                         if ( do_retry > 0 ) {
185                                 do_retry--;
186                                 if ( delay > 0 ) {
187                                     sleep( delay );
188                                 }
189                                 goto retry;
190                         }
191                 /* fallthru */
192                 default:
193                         break;
194                 }
195                 exit( EXIT_FAILURE );
196         }
197
198         for ( ; i < maxloop; i++ ) {
199                 LDAPMessage *res = NULL;
200
201                 rc = ldap_search_ext_s( ld, entry, LDAP_SCOPE_BASE,
202                                 NULL, attrs, 1, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
203                 switch ( rc ) {
204                 case LDAP_REFERRAL:
205                         /* don't log: it's intended */
206                         if ( force >= 2 ) {
207                                 if ( !first ) {
208                                         break;
209                                 }
210                                 first = 0;
211                         }
212                         tester_ldap_error( ld, "ldap_search_ext_s" );
213                         /* fallthru */
214
215                 case LDAP_SUCCESS:
216                         break;
217
218                 default:
219                         tester_ldap_error( ld, "ldap_search_ext_s" );
220                         if ( rc == LDAP_BUSY && do_retry > 0 ) {
221                                 do_retry--;
222                                 goto retry;
223                         }
224                         if ( rc != LDAP_NO_SUCH_OBJECT ) {
225                                 goto done;
226                         }
227                         break;
228                 }
229
230                 if ( res != NULL ) {
231                         ldap_msgfree( res );
232                 }
233         }
234
235 done:;
236         fprintf( stderr, " PID=%ld - Read done (%d).\n", (long) pid, rc );
237
238         ldap_unbind_ext( ld, NULL, NULL );
239 }
240