1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
|
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
* Copyright 1999-2022 The OpenLDAP Foundation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
/* ACKNOWLEDGEMENTS:
* This work was initially developed by Howard Chu for inclusion
* in OpenLDAP Software.
*/
#include "portable.h"
#include <stdio.h>
#include "ac/stdlib.h"
#include "ac/unistd.h"
#include "ac/string.h"
#include "ac/errno.h"
#include "ldap.h"
#include "lutil.h"
#include "lutil_ldap.h"
#include "ldap_pvt.h"
#include "slapd-common.h"
/* global vars */
pid_t pid;
int debug;
/* static vars */
static char progname[ BUFSIZ ];
tester_t progtype;
/*
* ignore_count[] is indexed by result code:
* negative for OpenLDAP client-side errors, positive for protocol codes.
*/
#define TESTER_CLIENT_FIRST LDAP_REFERRAL_LIMIT_EXCEEDED /* negative */
#define TESTER_SERVER_LAST LDAP_OTHER
static int ignore_base [ -TESTER_CLIENT_FIRST + TESTER_SERVER_LAST + 1 ];
#define ignore_count (ignore_base - TESTER_CLIENT_FIRST)
static const struct {
const char *name;
int err;
} ignore_str2err[] = {
{ "OPERATIONS_ERROR", LDAP_OPERATIONS_ERROR },
{ "PROTOCOL_ERROR", LDAP_PROTOCOL_ERROR },
{ "TIMELIMIT_EXCEEDED", LDAP_TIMELIMIT_EXCEEDED },
{ "SIZELIMIT_EXCEEDED", LDAP_SIZELIMIT_EXCEEDED },
{ "COMPARE_FALSE", LDAP_COMPARE_FALSE },
{ "COMPARE_TRUE", LDAP_COMPARE_TRUE },
{ "AUTH_METHOD_NOT_SUPPORTED", LDAP_AUTH_METHOD_NOT_SUPPORTED },
{ "STRONG_AUTH_NOT_SUPPORTED", LDAP_STRONG_AUTH_NOT_SUPPORTED },
{ "STRONG_AUTH_REQUIRED", LDAP_STRONG_AUTH_REQUIRED },
{ "STRONGER_AUTH_REQUIRED", LDAP_STRONGER_AUTH_REQUIRED },
{ "PARTIAL_RESULTS", LDAP_PARTIAL_RESULTS },
{ "REFERRAL", LDAP_REFERRAL },
{ "ADMINLIMIT_EXCEEDED", LDAP_ADMINLIMIT_EXCEEDED },
{ "UNAVAILABLE_CRITICAL_EXTENSION", LDAP_UNAVAILABLE_CRITICAL_EXTENSION },
{ "CONFIDENTIALITY_REQUIRED", LDAP_CONFIDENTIALITY_REQUIRED },
{ "SASL_BIND_IN_PROGRESS", LDAP_SASL_BIND_IN_PROGRESS },
{ "NO_SUCH_ATTRIBUTE", LDAP_NO_SUCH_ATTRIBUTE },
{ "UNDEFINED_TYPE", LDAP_UNDEFINED_TYPE },
{ "INAPPROPRIATE_MATCHING", LDAP_INAPPROPRIATE_MATCHING },
{ "CONSTRAINT_VIOLATION", LDAP_CONSTRAINT_VIOLATION },
{ "TYPE_OR_VALUE_EXISTS", LDAP_TYPE_OR_VALUE_EXISTS },
{ "INVALID_SYNTAX", LDAP_INVALID_SYNTAX },
{ "NO_SUCH_OBJECT", LDAP_NO_SUCH_OBJECT },
{ "ALIAS_PROBLEM", LDAP_ALIAS_PROBLEM },
{ "INVALID_DN_SYNTAX", LDAP_INVALID_DN_SYNTAX },
{ "IS_LEAF", LDAP_IS_LEAF },
{ "ALIAS_DEREF_PROBLEM", LDAP_ALIAS_DEREF_PROBLEM },
/* obsolete */
{ "PROXY_AUTHZ_FAILURE", LDAP_X_PROXY_AUTHZ_FAILURE },
{ "INAPPROPRIATE_AUTH", LDAP_INAPPROPRIATE_AUTH },
{ "INVALID_CREDENTIALS", LDAP_INVALID_CREDENTIALS },
{ "INSUFFICIENT_ACCESS", LDAP_INSUFFICIENT_ACCESS },
{ "BUSY", LDAP_BUSY },
{ "UNAVAILABLE", LDAP_UNAVAILABLE },
{ "UNWILLING_TO_PERFORM", LDAP_UNWILLING_TO_PERFORM },
{ "LOOP_DETECT", LDAP_LOOP_DETECT },
{ "NAMING_VIOLATION", LDAP_NAMING_VIOLATION },
{ "OBJECT_CLASS_VIOLATION", LDAP_OBJECT_CLASS_VIOLATION },
{ "NOT_ALLOWED_ON_NONLEAF", LDAP_NOT_ALLOWED_ON_NONLEAF },
{ "NOT_ALLOWED_ON_RDN", LDAP_NOT_ALLOWED_ON_RDN },
{ "ALREADY_EXISTS", LDAP_ALREADY_EXISTS },
{ "NO_OBJECT_CLASS_MODS", LDAP_NO_OBJECT_CLASS_MODS },
{ "RESULTS_TOO_LARGE", LDAP_RESULTS_TOO_LARGE },
{ "AFFECTS_MULTIPLE_DSAS", LDAP_AFFECTS_MULTIPLE_DSAS },
{ "OTHER", LDAP_OTHER },
{ "SERVER_DOWN", LDAP_SERVER_DOWN },
{ "LOCAL_ERROR", LDAP_LOCAL_ERROR },
{ "ENCODING_ERROR", LDAP_ENCODING_ERROR },
{ "DECODING_ERROR", LDAP_DECODING_ERROR },
{ "TIMEOUT", LDAP_TIMEOUT },
{ "AUTH_UNKNOWN", LDAP_AUTH_UNKNOWN },
{ "FILTER_ERROR", LDAP_FILTER_ERROR },
{ "USER_CANCELLED", LDAP_USER_CANCELLED },
{ "PARAM_ERROR", LDAP_PARAM_ERROR },
{ "NO_MEMORY", LDAP_NO_MEMORY },
{ "CONNECT_ERROR", LDAP_CONNECT_ERROR },
{ "NOT_SUPPORTED", LDAP_NOT_SUPPORTED },
{ "CONTROL_NOT_FOUND", LDAP_CONTROL_NOT_FOUND },
{ "NO_RESULTS_RETURNED", LDAP_NO_RESULTS_RETURNED },
{ "MORE_RESULTS_TO_RETURN", LDAP_MORE_RESULTS_TO_RETURN },
{ "CLIENT_LOOP", LDAP_CLIENT_LOOP },
{ "REFERRAL_LIMIT_EXCEEDED", LDAP_REFERRAL_LIMIT_EXCEEDED },
{ NULL }
};
#define UNKNOWN_ERR (1234567890)
#define RETRIES 0
#define LOOPS 100
static int
tester_ignore_str2err( const char *err )
{
int i, ignore = 1;
if ( strcmp( err, "ALL" ) == 0 ) {
for ( i = 0; ignore_str2err[ i ].name != NULL; i++ ) {
ignore_count[ ignore_str2err[ i ].err ] = 1;
}
ignore_count[ LDAP_SUCCESS ] = 0;
return 0;
}
if ( err[ 0 ] == '!' ) {
ignore = 0;
err++;
} else if ( err[ 0 ] == '*' ) {
ignore = -1;
err++;
}
for ( i = 0; ignore_str2err[ i ].name != NULL; i++ ) {
if ( strcmp( err, ignore_str2err[ i ].name ) == 0 ) {
int err = ignore_str2err[ i ].err;
if ( err != LDAP_SUCCESS ) {
ignore_count[ err ] = ignore;
}
return err;
}
}
return UNKNOWN_ERR;
}
int
tester_ignore_str2errlist( const char *err )
{
int i;
char **errs = ldap_str2charray( err, "," );
for ( i = 0; errs[ i ] != NULL; i++ ) {
/* TODO: allow <err>:<prog> to ignore <err> only when <prog> */
(void)tester_ignore_str2err( errs[ i ] );
}
ldap_charray_free( errs );
return 0;
}
int
tester_ignore_err( int err )
{
int rc = 1;
if ( err && TESTER_CLIENT_FIRST <= err && err <= TESTER_SERVER_LAST ) {
rc = ignore_count[ err ];
if ( rc != 0 ) {
ignore_count[ err ] = rc + (rc > 0 ? 1 : -1);
}
}
/* SUCCESS is always "ignored" */
return rc;
}
struct tester_conn_args *
tester_init( const char *pname, tester_t ptype )
{
static struct tester_conn_args config = {
.authmethod = -1,
.retries = RETRIES,
.loops = LOOPS,
.outerloops = 1,
.uri = NULL,
};
pid = getpid();
srand( pid );
snprintf( progname, sizeof( progname ), "%s PID=%d", pname, pid );
progtype = ptype;
return &config;
}
void
tester_ldap_error( LDAP *ld, const char *fname, const char *msg )
{
int err;
char *text = NULL;
LDAPControl **ctrls = NULL;
ldap_get_option( ld, LDAP_OPT_RESULT_CODE, (void *)&err );
if ( err != LDAP_SUCCESS ) {
ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void *)&text );
}
fprintf( stderr, "%s: %s: %s (%d) %s %s\n",
progname, fname, ldap_err2string( err ), err,
text == NULL ? "" : text,
msg ? msg : "" );
if ( text ) {
ldap_memfree( text );
text = NULL;
}
ldap_get_option( ld, LDAP_OPT_MATCHED_DN, (void *)&text );
if ( text != NULL ) {
if ( text[ 0 ] != '\0' ) {
fprintf( stderr, "\tmatched: %s\n", text );
}
ldap_memfree( text );
text = NULL;
}
ldap_get_option( ld, LDAP_OPT_SERVER_CONTROLS, (void *)&ctrls );
if ( ctrls != NULL ) {
int i;
fprintf( stderr, "\tcontrols:\n" );
for ( i = 0; ctrls[ i ] != NULL; i++ ) {
fprintf( stderr, "\t\t%s\n", ctrls[ i ]->ldctl_oid );
}
ldap_controls_free( ctrls );
ctrls = NULL;
}
if ( err == LDAP_REFERRAL ) {
char **refs = NULL;
ldap_get_option( ld, LDAP_OPT_REFERRAL_URLS, (void *)&refs );
if ( refs ) {
int i;
fprintf( stderr, "\treferral:\n" );
for ( i = 0; refs[ i ] != NULL; i++ ) {
fprintf( stderr, "\t\t%s\n", refs[ i ] );
}
ber_memvfree( (void **)refs );
}
}
}
void
tester_perror( const char *fname, const char *msg )
{
int save_errno = errno;
char buf[ BUFSIZ ];
fprintf( stderr, "%s: %s: (%d) %s %s\n",
progname, fname, save_errno,
AC_STRERROR_R( save_errno, buf, sizeof( buf ) ),
msg ? msg : "" );
}
int
tester_config_opt( struct tester_conn_args *config, char opt, char *optarg )
{
switch ( opt ) {
case 'C':
config->chaserefs++;
break;
case 'D':
config->binddn = optarg;
break;
case 'd':
{
if ( lutil_atoi( &debug, optarg ) != 0 ) {
return -1;
}
if ( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug )
!= LBER_OPT_SUCCESS )
{
fprintf( stderr,
"Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
}
if ( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug )
!= LDAP_OPT_SUCCESS )
{
fprintf( stderr,
"Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
}
break;
}
case 'H':
config->uri = optarg;
break;
case 'i':
tester_ignore_str2errlist( optarg );
break;
case 'L':
if ( lutil_atoi( &config->outerloops, optarg ) != 0 ) {
return -1;
}
break;
case 'l':
if ( lutil_atoi( &config->loops, optarg ) != 0 ) {
return -1;
}
break;
#ifdef HAVE_CYRUS_SASL
case 'O':
if ( config->secprops != NULL ) {
return -1;
}
if ( config->authmethod != -1 && config->authmethod != LDAP_AUTH_SASL ) {
return -1;
}
config->authmethod = LDAP_AUTH_SASL;
config->secprops = optarg;
break;
case 'R':
if ( config->realm != NULL ) {
return -1;
}
if ( config->authmethod != -1 && config->authmethod != LDAP_AUTH_SASL ) {
return -1;
}
config->authmethod = LDAP_AUTH_SASL;
config->realm = optarg;
break;
case 'U':
if ( config->authc_id != NULL ) {
return -1;
}
if ( config->authmethod != -1 && config->authmethod != LDAP_AUTH_SASL ) {
return -1;
}
config->authmethod = LDAP_AUTH_SASL;
config->authc_id = optarg;
break;
case 'X':
if ( config->authz_id != NULL ) {
return -1;
}
if ( config->authmethod != -1 && config->authmethod != LDAP_AUTH_SASL ) {
return -1;
}
config->authmethod = LDAP_AUTH_SASL;
config->authz_id = optarg;
break;
case 'Y':
if ( config->mech != NULL ) {
return -1;
}
if ( config->authmethod != -1 && config->authmethod != LDAP_AUTH_SASL ) {
return -1;
}
config->authmethod = LDAP_AUTH_SASL;
config->mech = optarg;
break;
#endif
case 'r':
if ( lutil_atoi( &config->retries, optarg ) != 0 ) {
return -1;
}
break;
case 't':
if ( lutil_atoi( &config->delay, optarg ) != 0 ) {
return -1;
}
break;
case 'w':
config->pass.bv_val = strdup( optarg );
config->pass.bv_len = strlen( optarg );
memset( optarg, '*', config->pass.bv_len );
break;
case 'x':
if ( config->authmethod != -1 && config->authmethod != LDAP_AUTH_SIMPLE ) {
return -1;
}
config->authmethod = LDAP_AUTH_SIMPLE;
break;
default:
return -1;
}
return LDAP_SUCCESS;
}
void
tester_config_finish( struct tester_conn_args *config )
{
if ( config->authmethod == -1 ) {
#ifdef HAVE_CYRUS_SASL
if ( config->binddn != NULL ) {
config->authmethod = LDAP_AUTH_SIMPLE;
} else {
config->authmethod = LDAP_AUTH_SASL;
}
#else
config->authmethod = LDAP_AUTH_SIMPLE;
#endif
}
#ifdef HAVE_CYRUS_SASL
if ( config->authmethod == LDAP_AUTH_SASL ) {
config->defaults = lutil_sasl_defaults( NULL,
config->mech,
config->realm,
config->authc_id,
config->pass.bv_val,
config->authz_id );
if ( config->defaults == NULL ) {
tester_error( "unable to prepare SASL defaults" );
exit( EXIT_FAILURE );
}
}
#endif
}
void
tester_init_ld( LDAP **ldp, struct tester_conn_args *config, int flags )
{
LDAP *ld;
int rc, do_retry = config->retries;
int version = LDAP_VERSION3;
retry:;
ldap_initialize( &ld, config->uri );
if ( ld == NULL ) {
tester_perror( "ldap_initialize", NULL );
exit( EXIT_FAILURE );
}
(void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
(void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
config->chaserefs ? LDAP_OPT_ON: LDAP_OPT_OFF );
if ( !( flags & TESTER_INIT_ONLY ) ) {
if ( config->authmethod == LDAP_AUTH_SASL ) {
#ifdef HAVE_CYRUS_SASL
if ( config->secprops != NULL ) {
rc = ldap_set_option( ld,
LDAP_OPT_X_SASL_SECPROPS, config->secprops );
if ( rc != LDAP_OPT_SUCCESS ) {
tester_ldap_error( ld, "ldap_set_option(SECPROPS)", NULL );
ldap_unbind_ext( ld, NULL, NULL );
exit( EXIT_FAILURE );
}
}
rc = ldap_sasl_interactive_bind_s( ld,
config->binddn,
config->mech,
NULL, NULL,
LDAP_SASL_QUIET,
lutil_sasl_interact,
config->defaults );
#else /* HAVE_CYRUS_SASL */
/* caller shouldn't have allowed this */
assert(0);
#endif
} else if ( config->authmethod == LDAP_AUTH_SIMPLE ) {
rc = ldap_sasl_bind_s( ld,
config->binddn, LDAP_SASL_SIMPLE,
&config->pass, NULL, NULL, NULL );
}
if ( rc != LDAP_SUCCESS ) {
tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
switch ( rc ) {
case LDAP_BUSY:
case LDAP_UNAVAILABLE:
if ( do_retry > 0 ) {
do_retry--;
if ( config->delay > 0 ) {
sleep( config->delay );
}
goto retry;
}
}
ldap_unbind_ext( ld, NULL, NULL );
ld = NULL;
if ( !( flags & TESTER_INIT_NOEXIT ))
exit( EXIT_FAILURE );
}
}
*ldp = ld;
}
void
tester_error( const char *msg )
{
fprintf( stderr, "%s: %s\n", progname, msg );
}
|