slapd-search.c revision 1.1.1.1.2.2 1 1.1.1.1.2.2 yamt /* $OpenLDAP: pkg/ldap/tests/progs/slapd-search.c,v 1.41.2.7 2008/02/11 23:26:50 kurt Exp $ */
2 1.1.1.1.2.2 yamt /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 1.1.1.1.2.2 yamt *
4 1.1.1.1.2.2 yamt * Copyright 1999-2008 The OpenLDAP Foundation.
5 1.1.1.1.2.2 yamt * All rights reserved.
6 1.1.1.1.2.2 yamt *
7 1.1.1.1.2.2 yamt * Redistribution and use in source and binary forms, with or without
8 1.1.1.1.2.2 yamt * modification, are permitted only as authorized by the OpenLDAP
9 1.1.1.1.2.2 yamt * Public License.
10 1.1.1.1.2.2 yamt *
11 1.1.1.1.2.2 yamt * A copy of this license is available in file LICENSE in the
12 1.1.1.1.2.2 yamt * top-level directory of the distribution or, alternatively, at
13 1.1.1.1.2.2 yamt * <http://www.OpenLDAP.org/license.html>.
14 1.1.1.1.2.2 yamt */
15 1.1.1.1.2.2 yamt /* ACKNOWLEDGEMENTS:
16 1.1.1.1.2.2 yamt * This work was initially developed by Kurt Spanier for inclusion
17 1.1.1.1.2.2 yamt * in OpenLDAP Software.
18 1.1.1.1.2.2 yamt */
19 1.1.1.1.2.2 yamt
20 1.1.1.1.2.2 yamt #include "portable.h"
21 1.1.1.1.2.2 yamt
22 1.1.1.1.2.2 yamt #include <stdio.h>
23 1.1.1.1.2.2 yamt
24 1.1.1.1.2.2 yamt #include "ac/stdlib.h"
25 1.1.1.1.2.2 yamt
26 1.1.1.1.2.2 yamt #include "ac/ctype.h"
27 1.1.1.1.2.2 yamt #include "ac/param.h"
28 1.1.1.1.2.2 yamt #include "ac/socket.h"
29 1.1.1.1.2.2 yamt #include "ac/string.h"
30 1.1.1.1.2.2 yamt #include "ac/unistd.h"
31 1.1.1.1.2.2 yamt #include "ac/wait.h"
32 1.1.1.1.2.2 yamt
33 1.1.1.1.2.2 yamt #include "ldap.h"
34 1.1.1.1.2.2 yamt #include "lutil.h"
35 1.1.1.1.2.2 yamt #include "ldap_pvt.h"
36 1.1.1.1.2.2 yamt
37 1.1.1.1.2.2 yamt #include "slapd-common.h"
38 1.1.1.1.2.2 yamt
39 1.1.1.1.2.2 yamt #define LOOPS 100
40 1.1.1.1.2.2 yamt #define RETRIES 0
41 1.1.1.1.2.2 yamt
42 1.1.1.1.2.2 yamt static void
43 1.1.1.1.2.2 yamt do_search( char *uri, char *manager, struct berval *passwd,
44 1.1.1.1.2.2 yamt char *sbase, int scope, char *filter, LDAP **ldp,
45 1.1.1.1.2.2 yamt char **attrs, int noattrs, int nobind,
46 1.1.1.1.2.2 yamt int innerloop, int maxretries, int delay, int force, int chaserefs );
47 1.1.1.1.2.2 yamt
48 1.1.1.1.2.2 yamt static void
49 1.1.1.1.2.2 yamt do_random( char *uri, char *manager, struct berval *passwd,
50 1.1.1.1.2.2 yamt char *sbase, int scope, char *filter, char *attr,
51 1.1.1.1.2.2 yamt char **attrs, int noattrs, int nobind,
52 1.1.1.1.2.2 yamt int innerloop, int maxretries, int delay, int force, int chaserefs );
53 1.1.1.1.2.2 yamt
54 1.1.1.1.2.2 yamt static void
55 1.1.1.1.2.2 yamt usage( char *name, char o )
56 1.1.1.1.2.2 yamt {
57 1.1.1.1.2.2 yamt if ( o != '\0' ) {
58 1.1.1.1.2.2 yamt fprintf( stderr, "unknown/incorrect option \"%c\"\n", o );
59 1.1.1.1.2.2 yamt }
60 1.1.1.1.2.2 yamt
61 1.1.1.1.2.2 yamt fprintf( stderr,
62 1.1.1.1.2.2 yamt "usage: %s "
63 1.1.1.1.2.2 yamt "-H <uri> | ([-h <host>] -p <port>) "
64 1.1.1.1.2.2 yamt "-D <manager> "
65 1.1.1.1.2.2 yamt "-w <passwd> "
66 1.1.1.1.2.2 yamt "-b <searchbase> "
67 1.1.1.1.2.2 yamt "-s <scope> "
68 1.1.1.1.2.2 yamt "-f <searchfilter> "
69 1.1.1.1.2.2 yamt "[-a <attr>] "
70 1.1.1.1.2.2 yamt "[-A] "
71 1.1.1.1.2.2 yamt "[-C] "
72 1.1.1.1.2.2 yamt "[-F] "
73 1.1.1.1.2.2 yamt "[-N] "
74 1.1.1.1.2.2 yamt "[-i <ignore>] "
75 1.1.1.1.2.2 yamt "[-l <loops>] "
76 1.1.1.1.2.2 yamt "[-L <outerloops>] "
77 1.1.1.1.2.2 yamt "[-r <maxretries>] "
78 1.1.1.1.2.2 yamt "[-t <delay>] "
79 1.1.1.1.2.2 yamt "[<attrs>] "
80 1.1.1.1.2.2 yamt "\n",
81 1.1.1.1.2.2 yamt name );
82 1.1.1.1.2.2 yamt exit( EXIT_FAILURE );
83 1.1.1.1.2.2 yamt }
84 1.1.1.1.2.2 yamt
85 1.1.1.1.2.2 yamt int
86 1.1.1.1.2.2 yamt main( int argc, char **argv )
87 1.1.1.1.2.2 yamt {
88 1.1.1.1.2.2 yamt int i;
89 1.1.1.1.2.2 yamt char *uri = NULL;
90 1.1.1.1.2.2 yamt char *host = "localhost";
91 1.1.1.1.2.2 yamt int port = -1;
92 1.1.1.1.2.2 yamt char *manager = NULL;
93 1.1.1.1.2.2 yamt struct berval passwd = { 0, NULL };
94 1.1.1.1.2.2 yamt char *sbase = NULL;
95 1.1.1.1.2.2 yamt int scope = LDAP_SCOPE_SUBTREE;
96 1.1.1.1.2.2 yamt char *filter = NULL;
97 1.1.1.1.2.2 yamt char *attr = NULL;
98 1.1.1.1.2.2 yamt char *srchattrs[] = { "cn", "sn", NULL };
99 1.1.1.1.2.2 yamt char **attrs = srchattrs;
100 1.1.1.1.2.2 yamt int loops = LOOPS;
101 1.1.1.1.2.2 yamt int outerloops = 1;
102 1.1.1.1.2.2 yamt int retries = RETRIES;
103 1.1.1.1.2.2 yamt int delay = 0;
104 1.1.1.1.2.2 yamt int force = 0;
105 1.1.1.1.2.2 yamt int chaserefs = 0;
106 1.1.1.1.2.2 yamt int noattrs = 0;
107 1.1.1.1.2.2 yamt int nobind = 0;
108 1.1.1.1.2.2 yamt
109 1.1.1.1.2.2 yamt tester_init( "slapd-search", TESTER_SEARCH );
110 1.1.1.1.2.2 yamt
111 1.1.1.1.2.2 yamt /* by default, tolerate referrals and no such object */
112 1.1.1.1.2.2 yamt tester_ignore_str2errlist( "REFERRAL,NO_SUCH_OBJECT" );
113 1.1.1.1.2.2 yamt
114 1.1.1.1.2.2 yamt while ( ( i = getopt( argc, argv, "Aa:b:CD:f:FH:h:i:l:L:Np:r:s:t:T:w:" ) ) != EOF )
115 1.1.1.1.2.2 yamt {
116 1.1.1.1.2.2 yamt switch ( i ) {
117 1.1.1.1.2.2 yamt case 'A':
118 1.1.1.1.2.2 yamt noattrs++;
119 1.1.1.1.2.2 yamt break;
120 1.1.1.1.2.2 yamt
121 1.1.1.1.2.2 yamt case 'C':
122 1.1.1.1.2.2 yamt chaserefs++;
123 1.1.1.1.2.2 yamt break;
124 1.1.1.1.2.2 yamt
125 1.1.1.1.2.2 yamt case 'H': /* the server uri */
126 1.1.1.1.2.2 yamt uri = strdup( optarg );
127 1.1.1.1.2.2 yamt break;
128 1.1.1.1.2.2 yamt
129 1.1.1.1.2.2 yamt case 'h': /* the servers host */
130 1.1.1.1.2.2 yamt host = strdup( optarg );
131 1.1.1.1.2.2 yamt break;
132 1.1.1.1.2.2 yamt
133 1.1.1.1.2.2 yamt case 'i':
134 1.1.1.1.2.2 yamt tester_ignore_str2errlist( optarg );
135 1.1.1.1.2.2 yamt break;
136 1.1.1.1.2.2 yamt
137 1.1.1.1.2.2 yamt case 'N':
138 1.1.1.1.2.2 yamt nobind++;
139 1.1.1.1.2.2 yamt break;
140 1.1.1.1.2.2 yamt
141 1.1.1.1.2.2 yamt case 'p': /* the servers port */
142 1.1.1.1.2.2 yamt if ( lutil_atoi( &port, optarg ) != 0 ) {
143 1.1.1.1.2.2 yamt usage( argv[0], i );
144 1.1.1.1.2.2 yamt }
145 1.1.1.1.2.2 yamt break;
146 1.1.1.1.2.2 yamt
147 1.1.1.1.2.2 yamt case 'D': /* the servers manager */
148 1.1.1.1.2.2 yamt manager = strdup( optarg );
149 1.1.1.1.2.2 yamt break;
150 1.1.1.1.2.2 yamt
151 1.1.1.1.2.2 yamt case 'w': /* the server managers password */
152 1.1.1.1.2.2 yamt passwd.bv_val = strdup( optarg );
153 1.1.1.1.2.2 yamt passwd.bv_len = strlen( optarg );
154 1.1.1.1.2.2 yamt memset( optarg, '*', passwd.bv_len );
155 1.1.1.1.2.2 yamt break;
156 1.1.1.1.2.2 yamt
157 1.1.1.1.2.2 yamt case 'a':
158 1.1.1.1.2.2 yamt attr = strdup( optarg );
159 1.1.1.1.2.2 yamt break;
160 1.1.1.1.2.2 yamt
161 1.1.1.1.2.2 yamt case 'b': /* file with search base */
162 1.1.1.1.2.2 yamt sbase = strdup( optarg );
163 1.1.1.1.2.2 yamt break;
164 1.1.1.1.2.2 yamt
165 1.1.1.1.2.2 yamt case 'f': /* the search request */
166 1.1.1.1.2.2 yamt filter = strdup( optarg );
167 1.1.1.1.2.2 yamt break;
168 1.1.1.1.2.2 yamt
169 1.1.1.1.2.2 yamt case 'F':
170 1.1.1.1.2.2 yamt force++;
171 1.1.1.1.2.2 yamt break;
172 1.1.1.1.2.2 yamt
173 1.1.1.1.2.2 yamt case 'l': /* number of loops */
174 1.1.1.1.2.2 yamt if ( lutil_atoi( &loops, optarg ) != 0 ) {
175 1.1.1.1.2.2 yamt usage( argv[0], i );
176 1.1.1.1.2.2 yamt }
177 1.1.1.1.2.2 yamt break;
178 1.1.1.1.2.2 yamt
179 1.1.1.1.2.2 yamt case 'L': /* number of loops */
180 1.1.1.1.2.2 yamt if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
181 1.1.1.1.2.2 yamt usage( argv[0], i );
182 1.1.1.1.2.2 yamt }
183 1.1.1.1.2.2 yamt break;
184 1.1.1.1.2.2 yamt
185 1.1.1.1.2.2 yamt case 'r': /* number of retries */
186 1.1.1.1.2.2 yamt if ( lutil_atoi( &retries, optarg ) != 0 ) {
187 1.1.1.1.2.2 yamt usage( argv[0], i );
188 1.1.1.1.2.2 yamt }
189 1.1.1.1.2.2 yamt break;
190 1.1.1.1.2.2 yamt
191 1.1.1.1.2.2 yamt case 't': /* delay in seconds */
192 1.1.1.1.2.2 yamt if ( lutil_atoi( &delay, optarg ) != 0 ) {
193 1.1.1.1.2.2 yamt usage( argv[0], i );
194 1.1.1.1.2.2 yamt }
195 1.1.1.1.2.2 yamt break;
196 1.1.1.1.2.2 yamt
197 1.1.1.1.2.2 yamt case 'T':
198 1.1.1.1.2.2 yamt attrs = ldap_str2charray( optarg, "," );
199 1.1.1.1.2.2 yamt if ( attrs == NULL ) {
200 1.1.1.1.2.2 yamt usage( argv[0], i );
201 1.1.1.1.2.2 yamt }
202 1.1.1.1.2.2 yamt break;
203 1.1.1.1.2.2 yamt
204 1.1.1.1.2.2 yamt case 's':
205 1.1.1.1.2.2 yamt scope = ldap_pvt_str2scope( optarg );
206 1.1.1.1.2.2 yamt if ( scope == -1 ) {
207 1.1.1.1.2.2 yamt usage( argv[0], i );
208 1.1.1.1.2.2 yamt }
209 1.1.1.1.2.2 yamt break;
210 1.1.1.1.2.2 yamt
211 1.1.1.1.2.2 yamt default:
212 1.1.1.1.2.2 yamt usage( argv[0], i );
213 1.1.1.1.2.2 yamt break;
214 1.1.1.1.2.2 yamt }
215 1.1.1.1.2.2 yamt }
216 1.1.1.1.2.2 yamt
217 1.1.1.1.2.2 yamt if (( sbase == NULL ) || ( filter == NULL ) || ( port == -1 && uri == NULL ))
218 1.1.1.1.2.2 yamt usage( argv[0], '\0' );
219 1.1.1.1.2.2 yamt
220 1.1.1.1.2.2 yamt if ( *filter == '\0' ) {
221 1.1.1.1.2.2 yamt
222 1.1.1.1.2.2 yamt fprintf( stderr, "%s: invalid EMPTY search filter.\n",
223 1.1.1.1.2.2 yamt argv[0] );
224 1.1.1.1.2.2 yamt exit( EXIT_FAILURE );
225 1.1.1.1.2.2 yamt
226 1.1.1.1.2.2 yamt }
227 1.1.1.1.2.2 yamt
228 1.1.1.1.2.2 yamt if ( argv[optind] != NULL ) {
229 1.1.1.1.2.2 yamt attrs = &argv[optind];
230 1.1.1.1.2.2 yamt }
231 1.1.1.1.2.2 yamt
232 1.1.1.1.2.2 yamt uri = tester_uri( uri, host, port );
233 1.1.1.1.2.2 yamt
234 1.1.1.1.2.2 yamt for ( i = 0; i < outerloops; i++ ) {
235 1.1.1.1.2.2 yamt if ( attr != NULL ) {
236 1.1.1.1.2.2 yamt do_random( uri, manager, &passwd,
237 1.1.1.1.2.2 yamt sbase, scope, filter, attr,
238 1.1.1.1.2.2 yamt attrs, noattrs, nobind,
239 1.1.1.1.2.2 yamt loops, retries, delay, force, chaserefs );
240 1.1.1.1.2.2 yamt
241 1.1.1.1.2.2 yamt } else {
242 1.1.1.1.2.2 yamt do_search( uri, manager, &passwd,
243 1.1.1.1.2.2 yamt sbase, scope, filter, NULL,
244 1.1.1.1.2.2 yamt attrs, noattrs, nobind,
245 1.1.1.1.2.2 yamt loops, retries, delay, force, chaserefs );
246 1.1.1.1.2.2 yamt }
247 1.1.1.1.2.2 yamt }
248 1.1.1.1.2.2 yamt
249 1.1.1.1.2.2 yamt exit( EXIT_SUCCESS );
250 1.1.1.1.2.2 yamt }
251 1.1.1.1.2.2 yamt
252 1.1.1.1.2.2 yamt
253 1.1.1.1.2.2 yamt static void
254 1.1.1.1.2.2 yamt do_random( char *uri, char *manager, struct berval *passwd,
255 1.1.1.1.2.2 yamt char *sbase, int scope, char *filter, char *attr,
256 1.1.1.1.2.2 yamt char **srchattrs, int noattrs, int nobind,
257 1.1.1.1.2.2 yamt int innerloop, int maxretries, int delay, int force, int chaserefs )
258 1.1.1.1.2.2 yamt {
259 1.1.1.1.2.2 yamt LDAP *ld = NULL;
260 1.1.1.1.2.2 yamt int i = 0, do_retry = maxretries;
261 1.1.1.1.2.2 yamt char *attrs[ 2 ];
262 1.1.1.1.2.2 yamt int rc = LDAP_SUCCESS;
263 1.1.1.1.2.2 yamt int version = LDAP_VERSION3;
264 1.1.1.1.2.2 yamt int nvalues = 0;
265 1.1.1.1.2.2 yamt char **values = NULL;
266 1.1.1.1.2.2 yamt LDAPMessage *res = NULL, *e = NULL;
267 1.1.1.1.2.2 yamt
268 1.1.1.1.2.2 yamt attrs[ 0 ] = attr;
269 1.1.1.1.2.2 yamt attrs[ 1 ] = NULL;
270 1.1.1.1.2.2 yamt
271 1.1.1.1.2.2 yamt ldap_initialize( &ld, uri );
272 1.1.1.1.2.2 yamt if ( ld == NULL ) {
273 1.1.1.1.2.2 yamt tester_perror( "ldap_initialize", NULL );
274 1.1.1.1.2.2 yamt exit( EXIT_FAILURE );
275 1.1.1.1.2.2 yamt }
276 1.1.1.1.2.2 yamt
277 1.1.1.1.2.2 yamt (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
278 1.1.1.1.2.2 yamt (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
279 1.1.1.1.2.2 yamt chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
280 1.1.1.1.2.2 yamt
281 1.1.1.1.2.2 yamt if ( do_retry == maxretries ) {
282 1.1.1.1.2.2 yamt fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\" attr=\"%s\".\n",
283 1.1.1.1.2.2 yamt (long) pid, innerloop, sbase, filter, attr );
284 1.1.1.1.2.2 yamt }
285 1.1.1.1.2.2 yamt
286 1.1.1.1.2.2 yamt if ( nobind == 0 ) {
287 1.1.1.1.2.2 yamt rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
288 1.1.1.1.2.2 yamt if ( rc != LDAP_SUCCESS ) {
289 1.1.1.1.2.2 yamt tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
290 1.1.1.1.2.2 yamt switch ( rc ) {
291 1.1.1.1.2.2 yamt case LDAP_BUSY:
292 1.1.1.1.2.2 yamt case LDAP_UNAVAILABLE:
293 1.1.1.1.2.2 yamt /* fallthru */
294 1.1.1.1.2.2 yamt default:
295 1.1.1.1.2.2 yamt break;
296 1.1.1.1.2.2 yamt }
297 1.1.1.1.2.2 yamt exit( EXIT_FAILURE );
298 1.1.1.1.2.2 yamt }
299 1.1.1.1.2.2 yamt }
300 1.1.1.1.2.2 yamt
301 1.1.1.1.2.2 yamt rc = ldap_search_ext_s( ld, sbase, LDAP_SCOPE_SUBTREE,
302 1.1.1.1.2.2 yamt filter, attrs, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
303 1.1.1.1.2.2 yamt switch ( rc ) {
304 1.1.1.1.2.2 yamt case LDAP_SIZELIMIT_EXCEEDED:
305 1.1.1.1.2.2 yamt case LDAP_TIMELIMIT_EXCEEDED:
306 1.1.1.1.2.2 yamt case LDAP_SUCCESS:
307 1.1.1.1.2.2 yamt if ( ldap_count_entries( ld, res ) == 0 ) {
308 1.1.1.1.2.2 yamt if ( rc ) {
309 1.1.1.1.2.2 yamt tester_ldap_error( ld, "ldap_search_ext_s", NULL );
310 1.1.1.1.2.2 yamt }
311 1.1.1.1.2.2 yamt break;
312 1.1.1.1.2.2 yamt }
313 1.1.1.1.2.2 yamt
314 1.1.1.1.2.2 yamt for ( e = ldap_first_entry( ld, res ); e != NULL; e = ldap_next_entry( ld, e ) )
315 1.1.1.1.2.2 yamt {
316 1.1.1.1.2.2 yamt struct berval **v = ldap_get_values_len( ld, e, attr );
317 1.1.1.1.2.2 yamt
318 1.1.1.1.2.2 yamt if ( v != NULL ) {
319 1.1.1.1.2.2 yamt int n = ldap_count_values_len( v );
320 1.1.1.1.2.2 yamt int j;
321 1.1.1.1.2.2 yamt
322 1.1.1.1.2.2 yamt values = realloc( values, ( nvalues + n + 1 )*sizeof( char * ) );
323 1.1.1.1.2.2 yamt for ( j = 0; j < n; j++ ) {
324 1.1.1.1.2.2 yamt values[ nvalues + j ] = strdup( v[ j ]->bv_val );
325 1.1.1.1.2.2 yamt }
326 1.1.1.1.2.2 yamt values[ nvalues + j ] = NULL;
327 1.1.1.1.2.2 yamt nvalues += n;
328 1.1.1.1.2.2 yamt ldap_value_free_len( v );
329 1.1.1.1.2.2 yamt }
330 1.1.1.1.2.2 yamt }
331 1.1.1.1.2.2 yamt
332 1.1.1.1.2.2 yamt ldap_msgfree( res );
333 1.1.1.1.2.2 yamt
334 1.1.1.1.2.2 yamt if ( !values ) {
335 1.1.1.1.2.2 yamt fprintf( stderr, " PID=%ld - Search base=\"%s\" filter=\"%s\" got %d values.\n",
336 1.1.1.1.2.2 yamt (long) pid, sbase, filter, nvalues );
337 1.1.1.1.2.2 yamt exit(EXIT_FAILURE);
338 1.1.1.1.2.2 yamt }
339 1.1.1.1.2.2 yamt
340 1.1.1.1.2.2 yamt if ( do_retry == maxretries ) {
341 1.1.1.1.2.2 yamt fprintf( stderr, " PID=%ld - Search base=\"%s\" filter=\"%s\" got %d values.\n",
342 1.1.1.1.2.2 yamt (long) pid, sbase, filter, nvalues );
343 1.1.1.1.2.2 yamt }
344 1.1.1.1.2.2 yamt
345 1.1.1.1.2.2 yamt for ( i = 0; i < innerloop; i++ ) {
346 1.1.1.1.2.2 yamt char buf[ BUFSIZ ];
347 1.1.1.1.2.2 yamt #if 0 /* use high-order bits for better randomness (Numerical Recipes in "C") */
348 1.1.1.1.2.2 yamt int r = rand() % nvalues;
349 1.1.1.1.2.2 yamt #endif
350 1.1.1.1.2.2 yamt int r = ((double)nvalues)*rand()/(RAND_MAX + 1.0);
351 1.1.1.1.2.2 yamt
352 1.1.1.1.2.2 yamt snprintf( buf, sizeof( buf ), "(%s=%s)", attr, values[ r ] );
353 1.1.1.1.2.2 yamt
354 1.1.1.1.2.2 yamt do_search( uri, manager, passwd,
355 1.1.1.1.2.2 yamt sbase, scope, buf, &ld,
356 1.1.1.1.2.2 yamt srchattrs, noattrs, nobind,
357 1.1.1.1.2.2 yamt 1, maxretries, delay, force, chaserefs );
358 1.1.1.1.2.2 yamt }
359 1.1.1.1.2.2 yamt break;
360 1.1.1.1.2.2 yamt
361 1.1.1.1.2.2 yamt default:
362 1.1.1.1.2.2 yamt tester_ldap_error( ld, "ldap_search_ext_s", NULL );
363 1.1.1.1.2.2 yamt break;
364 1.1.1.1.2.2 yamt }
365 1.1.1.1.2.2 yamt
366 1.1.1.1.2.2 yamt fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
367 1.1.1.1.2.2 yamt
368 1.1.1.1.2.2 yamt if ( ld != NULL ) {
369 1.1.1.1.2.2 yamt ldap_unbind_ext( ld, NULL, NULL );
370 1.1.1.1.2.2 yamt }
371 1.1.1.1.2.2 yamt }
372 1.1.1.1.2.2 yamt
373 1.1.1.1.2.2 yamt static void
374 1.1.1.1.2.2 yamt do_search( char *uri, char *manager, struct berval *passwd,
375 1.1.1.1.2.2 yamt char *sbase, int scope, char *filter, LDAP **ldp,
376 1.1.1.1.2.2 yamt char **attrs, int noattrs, int nobind,
377 1.1.1.1.2.2 yamt int innerloop, int maxretries, int delay, int force, int chaserefs )
378 1.1.1.1.2.2 yamt {
379 1.1.1.1.2.2 yamt LDAP *ld = ldp ? *ldp : NULL;
380 1.1.1.1.2.2 yamt int i = 0, do_retry = maxretries;
381 1.1.1.1.2.2 yamt int rc = LDAP_SUCCESS;
382 1.1.1.1.2.2 yamt int version = LDAP_VERSION3;
383 1.1.1.1.2.2 yamt char buf[ BUFSIZ ];
384 1.1.1.1.2.2 yamt
385 1.1.1.1.2.2 yamt
386 1.1.1.1.2.2 yamt retry:;
387 1.1.1.1.2.2 yamt if ( ld == NULL ) {
388 1.1.1.1.2.2 yamt ldap_initialize( &ld, uri );
389 1.1.1.1.2.2 yamt if ( ld == NULL ) {
390 1.1.1.1.2.2 yamt tester_perror( "ldap_initialize", NULL );
391 1.1.1.1.2.2 yamt exit( EXIT_FAILURE );
392 1.1.1.1.2.2 yamt }
393 1.1.1.1.2.2 yamt
394 1.1.1.1.2.2 yamt (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
395 1.1.1.1.2.2 yamt (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
396 1.1.1.1.2.2 yamt chaserefs ? LDAP_OPT_ON : LDAP_OPT_OFF );
397 1.1.1.1.2.2 yamt
398 1.1.1.1.2.2 yamt if ( do_retry == maxretries ) {
399 1.1.1.1.2.2 yamt fprintf( stderr,
400 1.1.1.1.2.2 yamt "PID=%ld - Search(%d): "
401 1.1.1.1.2.2 yamt "base=\"%s\" scope=%s filter=\"%s\" "
402 1.1.1.1.2.2 yamt "attrs=%s%s.\n",
403 1.1.1.1.2.2 yamt (long) pid, innerloop,
404 1.1.1.1.2.2 yamt sbase, ldap_pvt_scope2str( scope ), filter,
405 1.1.1.1.2.2 yamt attrs[0], attrs[1] ? " (more...)" : "" );
406 1.1.1.1.2.2 yamt }
407 1.1.1.1.2.2 yamt
408 1.1.1.1.2.2 yamt if ( nobind == 0 ) {
409 1.1.1.1.2.2 yamt rc = ldap_sasl_bind_s( ld, manager, LDAP_SASL_SIMPLE, passwd, NULL, NULL, NULL );
410 1.1.1.1.2.2 yamt if ( rc != LDAP_SUCCESS ) {
411 1.1.1.1.2.2 yamt snprintf( buf, sizeof( buf ),
412 1.1.1.1.2.2 yamt "bindDN=\"%s\"", manager );
413 1.1.1.1.2.2 yamt tester_ldap_error( ld, "ldap_sasl_bind_s", buf );
414 1.1.1.1.2.2 yamt switch ( rc ) {
415 1.1.1.1.2.2 yamt case LDAP_BUSY:
416 1.1.1.1.2.2 yamt case LDAP_UNAVAILABLE:
417 1.1.1.1.2.2 yamt if ( do_retry > 0 ) {
418 1.1.1.1.2.2 yamt ldap_unbind_ext( ld, NULL, NULL );
419 1.1.1.1.2.2 yamt ld = NULL;
420 1.1.1.1.2.2 yamt do_retry--;
421 1.1.1.1.2.2 yamt if ( delay != 0 ) {
422 1.1.1.1.2.2 yamt sleep( delay );
423 1.1.1.1.2.2 yamt }
424 1.1.1.1.2.2 yamt goto retry;
425 1.1.1.1.2.2 yamt }
426 1.1.1.1.2.2 yamt /* fallthru */
427 1.1.1.1.2.2 yamt default:
428 1.1.1.1.2.2 yamt break;
429 1.1.1.1.2.2 yamt }
430 1.1.1.1.2.2 yamt exit( EXIT_FAILURE );
431 1.1.1.1.2.2 yamt }
432 1.1.1.1.2.2 yamt }
433 1.1.1.1.2.2 yamt }
434 1.1.1.1.2.2 yamt
435 1.1.1.1.2.2 yamt for ( ; i < innerloop; i++ ) {
436 1.1.1.1.2.2 yamt LDAPMessage *res = NULL;
437 1.1.1.1.2.2 yamt
438 1.1.1.1.2.2 yamt rc = ldap_search_ext_s( ld, sbase, scope,
439 1.1.1.1.2.2 yamt filter, attrs, noattrs, NULL, NULL,
440 1.1.1.1.2.2 yamt NULL, LDAP_NO_LIMIT, &res );
441 1.1.1.1.2.2 yamt if ( res != NULL ) {
442 1.1.1.1.2.2 yamt ldap_msgfree( res );
443 1.1.1.1.2.2 yamt }
444 1.1.1.1.2.2 yamt
445 1.1.1.1.2.2 yamt if ( rc ) {
446 1.1.1.1.2.2 yamt unsigned first = tester_ignore_err( rc );
447 1.1.1.1.2.2 yamt /* if ignore.. */
448 1.1.1.1.2.2 yamt if ( first ) {
449 1.1.1.1.2.2 yamt /* only log if first occurrence */
450 1.1.1.1.2.2 yamt if ( force < 2 || first == 1 ) {
451 1.1.1.1.2.2 yamt tester_ldap_error( ld, "ldap_search_ext_s", NULL );
452 1.1.1.1.2.2 yamt }
453 1.1.1.1.2.2 yamt continue;
454 1.1.1.1.2.2 yamt }
455 1.1.1.1.2.2 yamt
456 1.1.1.1.2.2 yamt /* busy needs special handling */
457 1.1.1.1.2.2 yamt snprintf( buf, sizeof( buf ),
458 1.1.1.1.2.2 yamt "base=\"%s\" filter=\"%s\"\n",
459 1.1.1.1.2.2 yamt sbase, filter );
460 1.1.1.1.2.2 yamt tester_ldap_error( ld, "ldap_search_ext_s", buf );
461 1.1.1.1.2.2 yamt if ( rc == LDAP_BUSY && do_retry > 0 ) {
462 1.1.1.1.2.2 yamt ldap_unbind_ext( ld, NULL, NULL );
463 1.1.1.1.2.2 yamt ld = NULL;
464 1.1.1.1.2.2 yamt do_retry--;
465 1.1.1.1.2.2 yamt goto retry;
466 1.1.1.1.2.2 yamt }
467 1.1.1.1.2.2 yamt break;
468 1.1.1.1.2.2 yamt }
469 1.1.1.1.2.2 yamt }
470 1.1.1.1.2.2 yamt
471 1.1.1.1.2.2 yamt if ( ldp != NULL ) {
472 1.1.1.1.2.2 yamt *ldp = ld;
473 1.1.1.1.2.2 yamt
474 1.1.1.1.2.2 yamt } else {
475 1.1.1.1.2.2 yamt fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
476 1.1.1.1.2.2 yamt
477 1.1.1.1.2.2 yamt if ( ld != NULL ) {
478 1.1.1.1.2.2 yamt ldap_unbind_ext( ld, NULL, NULL );
479 1.1.1.1.2.2 yamt }
480 1.1.1.1.2.2 yamt }
481 1.1.1.1.2.2 yamt }
482