]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-bind.c
add support for extra ops after bind; allow to skip bind for slapd-search/read
[openldap] / tests / progs / slapd-bind.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 Howard Chu for inclusion
17  * in OpenLDAP Software.
18  */
19
20 #include "portable.h"
21
22 #include <stdio.h>
23
24 #include <ac/stdlib.h>
25 #include <ac/time.h>
26
27 #include <ac/ctype.h>
28 #include <ac/param.h>
29 #include <ac/socket.h>
30 #include <ac/string.h>
31 #include <ac/unistd.h>
32 #include <ac/wait.h>
33 #include <ac/time.h>
34
35 #include <ldap.h>
36 #include <lutil.h>
37 #include <lber_pvt.h>
38 #include <ldap_pvt.h>
39
40 #include "slapd-common.h"
41
42 #define LOOPS   100
43
44 static int
45 do_bind( char *uri, char *dn, struct berval *pass, int maxloop,
46         int force, int chaserefs, int noinit, LDAP **ldp,
47         int action_type, void *action );
48
49 static int
50 do_base( char *uri, char *dn, struct berval *pass, char *base, char *filter, char *pwattr,
51         int maxloop, int force, int chaserefs, int noinit, int delay,
52         int action_type, void *action );
53
54 /* This program can be invoked two ways: if -D is used to specify a Bind DN,
55  * that DN will be used repeatedly for all of the Binds. If instead -b is used
56  * to specify a base DN, a search will be done for all "person" objects under
57  * that base DN. Then DNs from this list will be randomly selected for each
58  * Bind request. All of the users must have identical passwords. Also it is
59  * assumed that the users are all onelevel children of the base.
60  */
61 static void
62 usage( char *name, char opt )
63 {
64         if ( opt ) {
65                 fprintf( stderr, "%s: unable to handle option \'%c\'\n\n",
66                         name, opt );
67         }
68
69         fprintf( stderr, "usage: %s "
70                 "[-H uri | -h <host> [-p port]] "
71                 "[-D <dn> [-w <passwd>]] "
72                 "[-b <baseDN> [-f <searchfilter>] [-a pwattr]] "
73                 "[-l <loops>] "
74                 "[-L <outerloops>] "
75                 "[-B <extra>[,...]] "
76                 "[-F] "
77                 "[-C] "
78                 "[-I] "
79                 "[-i <ignore>] "
80                 "[-t delay]\n",
81                 name );
82         exit( EXIT_FAILURE );
83 }
84
85 int
86 main( int argc, char **argv )
87 {
88         int             i;
89         char            *uri = NULL;
90         char            *host = "localhost";
91         char            *dn = NULL;
92         char            *base = NULL;
93         char            *filter = "(objectClass=person)";
94         struct berval   pass = { 0, NULL };
95         char            *pwattr = NULL;
96         int             port = -1;
97         int             loops = LOOPS;
98         int             outerloops = 1;
99         int             force = 0;
100         int             chaserefs = 0;
101         int             noinit = 0;
102         int             delay = 0;
103
104         /* extra action to do after bind... */
105         struct berval   type[] = {
106                 BER_BVC( "tester=" ),
107                 BER_BVC( "add=" ),
108                 BER_BVC( "bind=" ),
109                 BER_BVC( "modify=" ),
110                 BER_BVC( "modrdn=" ),
111                 BER_BVC( "read=" ),
112                 BER_BVC( "search=" ),
113                 BER_BVNULL
114         };
115
116         LDAPURLDesc     *extra_ludp = NULL;
117
118         tester_init( "slapd-bind", TESTER_BIND );
119
120         /* by default, tolerate invalid credentials */
121         tester_ignore_str2errlist( "INVALID_CREDENTIALS" );
122
123         while ( (i = getopt( argc, argv, "a:b:B:H:h:i:p:D:w:l:L:f:FIt:" )) != EOF ) {
124                 switch( i ) {
125                 case 'a':
126                         pwattr = optarg;
127                         break;
128
129                 case 'b':               /* base DN of a tree of user DNs */
130                         base = optarg;
131                         break;
132
133                 case 'B':
134                         {
135                         int     c;
136
137                         for ( c = 0; type[c].bv_val; c++ ) {
138                                 if ( strncasecmp( optarg, type[c].bv_val, type[c].bv_len ) == 0 )
139                                 {
140                                         break;
141                                 }
142                         }
143
144                         if ( type[c].bv_val == NULL ) {
145                                 usage( argv[0], 'B' );
146                         }
147
148                         switch ( c ) {
149                         case TESTER_TESTER:
150                         case TESTER_BIND:
151                                 /* invalid */
152                                 usage( argv[0], 'B' );
153
154                         case TESTER_SEARCH:
155                                 {
156                                 if ( ldap_url_parse( &optarg[type[c].bv_len], &extra_ludp ) != LDAP_URL_SUCCESS )
157                                 {
158                                         usage( argv[0], 'B' );
159                                 }
160                                 } break;
161
162                         case TESTER_ADDEL:
163                         case TESTER_MODIFY:
164                         case TESTER_MODRDN:
165                         case TESTER_READ:
166                                 /* nothing to do */
167                                 break;
168
169                         default:
170                                 assert( 0 );
171                         }
172
173                         } break;
174
175                 case 'C':
176                         chaserefs++;
177                         break;
178
179                 case 'H':               /* the server uri */
180                         uri = optarg;
181                         break;
182
183                 case 'h':               /* the servers host */
184                         host = optarg;
185                         break;
186
187                 case 'i':
188                         tester_ignore_str2errlist( optarg );
189                         break;
190
191                 case 'p':               /* the servers port */
192                         if ( lutil_atoi( &port, optarg ) != 0 ) {
193                                 usage( argv[0], 'p' );
194                         }
195                         break;
196
197                 case 'D':
198                         dn = optarg;
199                         break;
200
201                 case 'w':
202                         ber_str2bv( optarg, 0, 1, &pass );
203                         memset( optarg, '*', pass.bv_len );
204                         break;
205
206                 case 'l':               /* the number of loops */
207                         if ( lutil_atoi( &loops, optarg ) != 0 ) {
208                                 usage( argv[0], 'l' );
209                         }
210                         break;
211
212                 case 'L':               /* the number of outerloops */
213                         if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
214                                 usage( argv[0], 'L' );
215                         }
216                         break;
217
218                 case 'f':
219                         filter = optarg;
220                         break;
221
222                 case 'F':
223                         force++;
224                         break;
225
226                 case 'I':
227                         /* reuse connection */
228                         noinit++;
229                         break;
230
231                 case 't':
232                         /* sleep between binds */
233                         if ( lutil_atoi( &delay, optarg ) != 0 ) {
234                                 usage( argv[0], 't' );
235                         }
236                         break;
237
238                 default:
239                         usage( argv[0], i );
240                         break;
241                 }
242         }
243
244         if ( port == -1 && uri == NULL ) {
245                 usage( argv[0], '\0' );
246         }
247
248         uri = tester_uri( uri, host, port );
249
250         for ( i = 0; i < outerloops; i++ ) {
251                 if ( base != NULL ) {
252                         do_base( uri, dn, &pass, base, filter, pwattr, loops,
253                                 force, chaserefs, noinit, delay, -1, NULL );
254                 } else {
255                         do_bind( uri, dn, &pass, loops,
256                                 force, chaserefs, noinit, NULL, -1, NULL );
257                 }
258         }
259
260         exit( EXIT_SUCCESS );
261 }
262
263
264 static int
265 do_bind( char *uri, char *dn, struct berval *pass, int maxloop,
266         int force, int chaserefs, int noinit, LDAP **ldp,
267         int action_type, void *action )
268 {
269         LDAP    *ld = ldp ? *ldp : NULL;
270         int     i, rc = -1;
271
272         /* for internal search */
273         int     timelimit = 0;
274         int     sizelimit = 0;
275
276         switch ( action_type ) {
277         case -1:
278                 break;
279
280         case TESTER_SEARCH:
281                 {
282                 LDAPURLDesc     *ludp = (LDAPURLDesc *)action;
283
284                 assert( action != NULL );
285
286                 if ( ludp->lud_exts != NULL ) {
287                         for ( i = 0; ludp->lud_exts[ i ] != NULL; i++ ) {
288                                 char    *ext = ludp->lud_exts[ i ];
289                                 int     crit = 0;
290
291                                 if (ext[0] == '!') {
292                                         crit++;
293                                         ext++;
294                                 }
295
296                                 if ( strncasecmp( ext, "x-timelimit=", STRLENOF( "x-timelimit=" ) ) == 0 ) {
297                                         if ( lutil_atoi( &timelimit, &ext[ STRLENOF( "x-timelimit=" ) ] ) && crit ) {
298                                                 tester_error( "unable to parse critical extension x-timelimit" );
299                                         }
300
301                                 } else if ( strncasecmp( ext, "x-sizelimit=", STRLENOF( "x-sizelimit=" ) ) == 0 ) {
302                                         if ( lutil_atoi( &sizelimit, &ext[ STRLENOF( "x-sizelimit=" ) ] ) && crit ) {
303                                                 tester_error( "unable to parse critical extension x-sizelimit" );
304                                         }
305
306                                 } else if ( crit ) {
307                                         tester_error( "unknown critical extension" );
308                                 }
309                         }
310                 }
311                 } break;
312
313         default:
314                 /* nothing to do yet */
315                 break;
316         }
317                         
318         if ( maxloop > 1 ) {
319                 fprintf( stderr, "PID=%ld - Bind(%d): dn=\"%s\".\n",
320                          (long) pid, maxloop, dn );
321         }
322
323         for ( i = 0; i < maxloop; i++ ) {
324                 if ( !noinit || ld == NULL ) {
325                         int version = LDAP_VERSION3;
326                         ldap_initialize( &ld, uri );
327                         if ( ld == NULL ) {
328                                 tester_perror( "ldap_initialize", NULL );
329                                 rc = -1;
330                                 break;
331                         }
332
333                         (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
334                                 &version ); 
335                         (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
336                                 chaserefs ? LDAP_OPT_ON: LDAP_OPT_OFF );
337                 }
338
339                 rc = ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, pass, NULL, NULL, NULL );
340                 if ( rc ) {
341                         unsigned first = tester_ignore_err( rc );
342
343                         /* if ignore.. */
344                         if ( first ) {
345                                 /* only log if first occurrence */
346                                 if ( force < 2 || first == 1 ) {
347                                         tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
348                                 }
349                                 rc = LDAP_SUCCESS;
350
351                         } else {
352                                 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
353                         }
354                 }
355
356                 switch ( action_type ) {
357                 case -1:
358                         break;
359
360                 case TESTER_SEARCH:
361                         {
362                         LDAPURLDesc     *ludp = (LDAPURLDesc *)action;
363                         LDAPMessage     *res = NULL;
364                         struct timeval  tv = { 0 }, *tvp = NULL;
365
366                         if ( timelimit ) {
367                                 tv.tv_sec = timelimit;
368                                 tvp = &tv;
369                         }
370
371                         assert( action != NULL );
372
373                         rc = ldap_search_ext_s( ld,
374                                 ludp->lud_dn, ludp->lud_scope,
375                                 ludp->lud_filter, ludp->lud_attrs, 0,
376                                 NULL, NULL, tvp, sizelimit, &res );
377                         ldap_msgfree( res );
378                         } break;
379
380                 default:
381                         /* nothing to do yet */
382                         break;
383                 }
384                         
385                 if ( !noinit ) {
386                         ldap_unbind_ext( ld, NULL, NULL );
387                         ld = NULL;
388                 }
389
390                 if ( rc != LDAP_SUCCESS ) {
391                         break;
392                 }
393         }
394
395         if ( maxloop > 1 ) {
396                 fprintf( stderr, " PID=%ld - Bind done (%d).\n", (long) pid, rc );
397         }
398
399         if ( ldp && noinit ) {
400                 *ldp = ld;
401
402         } else if ( ld != NULL ) {
403                 ldap_unbind_ext( ld, NULL, NULL );
404         }
405
406         return rc;
407 }
408
409
410 static int
411 do_base( char *uri, char *dn, struct berval *pass, char *base, char *filter, char *pwattr,
412         int maxloop, int force, int chaserefs, int noinit, int delay,
413         int action_type, void *action )
414 {
415         LDAP    *ld = NULL;
416         int     i = 0;
417         int     rc = LDAP_SUCCESS;
418         ber_int_t msgid;
419         LDAPMessage *res, *msg;
420         char **dns = NULL;
421         struct berval *creds = NULL;
422         char *attrs[] = { LDAP_NO_ATTRS, NULL };
423         int ndns = 0;
424 #ifdef _WIN32
425         DWORD beg, end;
426 #else
427         struct timeval beg, end;
428 #endif
429         int version = LDAP_VERSION3;
430         char *nullstr = "";
431
432         ldap_initialize( &ld, uri );
433         if ( ld == NULL ) {
434                 tester_perror( "ldap_initialize", NULL );
435                 exit( EXIT_FAILURE );
436         }
437
438         (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
439         (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
440                 chaserefs ? LDAP_OPT_ON: LDAP_OPT_OFF );
441
442         rc = ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, pass, NULL, NULL, NULL );
443         if ( rc != LDAP_SUCCESS ) {
444                 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
445                 exit( EXIT_FAILURE );
446         }
447
448         fprintf( stderr, "PID=%ld - Bind(%d): base=\"%s\", filter=\"%s\" attr=\"%s\".\n",
449                         (long) pid, maxloop, base, filter, pwattr );
450
451         if ( pwattr != NULL ) {
452                 attrs[ 0 ] = pwattr;
453         }
454         rc = ldap_search_ext( ld, base, LDAP_SCOPE_SUBTREE,
455                         filter, attrs, 0, NULL, NULL, 0, 0, &msgid );
456         if ( rc != LDAP_SUCCESS ) {
457                 tester_ldap_error( ld, "ldap_search_ext", NULL );
458                 exit( EXIT_FAILURE );
459         }
460
461         while ( ( rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res ) ) > 0 )
462         {
463                 BerElement *ber;
464                 struct berval bv;
465                 int done = 0;
466
467                 for ( msg = ldap_first_message( ld, res ); msg;
468                         msg = ldap_next_message( ld, msg ) )
469                 {
470                         switch ( ldap_msgtype( msg ) ) {
471                         case LDAP_RES_SEARCH_ENTRY:
472                                 rc = ldap_get_dn_ber( ld, msg, &ber, &bv );
473                                 dns = realloc( dns, (ndns + 1)*sizeof(char *) );
474                                 dns[ndns] = ber_strdup( bv.bv_val );
475                                 if ( pwattr != NULL ) {
476                                         struct berval   **values = ldap_get_values_len( ld, msg, pwattr );
477
478                                         creds = realloc( creds, (ndns + 1)*sizeof(struct berval) );
479                                         if ( values == NULL ) {
480 novals:;
481                                                 creds[ndns].bv_len = 0;
482                                                 creds[ndns].bv_val = nullstr;
483
484                                         } else {
485                                                 static struct berval    cleartext = BER_BVC( "{CLEARTEXT} " );
486                                                 struct berval           value = *values[ 0 ];
487
488                                                 if ( value.bv_val[ 0 ] == '{' ) {
489                                                         char *end = ber_bvchr( &value, '}' );
490
491                                                         if ( end ) {
492                                                                 if ( ber_bvcmp( &value, &cleartext ) == 0 ) {
493                                                                         value.bv_val += cleartext.bv_len;
494                                                                         value.bv_len -= cleartext.bv_len;
495
496                                                                 } else {
497                                                                         ldap_value_free_len( values );
498                                                                         goto novals;
499                                                                 }
500                                                         }
501
502                                                 }
503
504                                                 ber_dupbv( &creds[ndns], &value );
505                                                 ldap_value_free_len( values );
506                                         }
507                                 }
508                                 ndns++;
509                                 ber_free( ber, 0 );
510                                 break;
511
512                         case LDAP_RES_SEARCH_RESULT:
513                                 done = 1;
514                                 break;
515                         }
516                         if ( done )
517                                 break;
518                 }
519                 ldap_msgfree( res );
520                 if ( done ) break;
521         }
522
523 #ifdef _WIN32
524         beg = GetTickCount();
525 #else
526         gettimeofday( &beg, NULL );
527 #endif
528
529         if ( ndns == 0 ) {
530                 tester_error( "No DNs" );
531                 return 1;
532         }
533
534         fprintf( stderr, "  PID=%ld - Bind base=\"%s\" filter=\"%s\" got %d values.\n",
535                 (long) pid, base, filter, ndns );
536
537         /* Ok, got list of DNs, now start binding to each */
538         for ( i = 0; i < maxloop; i++ ) {
539                 int             j;
540                 struct berval   cred = { 0, NULL };
541
542
543 #if 0   /* use high-order bits for better randomness (Numerical Recipes in "C") */
544                 j = rand() % ndns;
545 #endif
546                 j = ((double)ndns)*rand()/(RAND_MAX + 1.0);
547
548                 if ( creds && !BER_BVISEMPTY( &creds[j] ) ) {
549                         cred = creds[j];
550                 }
551
552                 if ( do_bind( uri, dns[j], &cred, 1, force, chaserefs, noinit, &ld,
553                         action_type, action ) && !force )
554                 {
555                         break;
556                 }
557
558                 if ( delay ) {
559                         sleep( delay );
560                 }
561         }
562
563         if ( ld != NULL ) {
564                 ldap_unbind_ext( ld, NULL, NULL );
565                 ld = NULL;
566         }
567
568 #ifdef _WIN32
569         end = GetTickCount();
570         end -= beg;
571
572         fprintf( stderr, " PID=%ld - Bind done %d in %d.%03d seconds.\n",
573                 (long) pid, i, end / 1000, end % 1000 );
574 #else
575         gettimeofday( &end, NULL );
576         end.tv_usec -= beg.tv_usec;
577         if (end.tv_usec < 0 ) {
578                 end.tv_usec += 1000000;
579                 end.tv_sec -= 1;
580         }
581         end.tv_sec -= beg.tv_sec;
582
583         fprintf( stderr, " PID=%ld - Bind done %d in %ld.%06ld seconds.\n",
584                 (long) pid, i, (long) end.tv_sec, (long) end.tv_usec );
585 #endif
586
587         if ( dns ) {
588                 for ( i = 0; i < ndns; i++ ) {
589                         free( dns[i] );
590                 }
591                 free( dns );
592         }
593
594         if ( creds ) {
595                 for ( i = 0; i < ndns; i++ ) {
596                         if ( creds[i].bv_val != nullstr ) {
597                                 free( creds[i].bv_val );
598                         }
599                 }
600                 free( creds );
601         }
602
603         return 0;
604 }