]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-bind.c
remove password from command line
[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
39 #include "slapd-common.h"
40
41 #define LOOPS   100
42
43 static int
44 do_bind( char *uri, char *dn, struct berval *pass, int maxloop,
45         int force, int chaserefs, int noinit, LDAP **ldp );
46
47 static int
48 do_base( char *uri, char *dn, struct berval *pass, char *base, char *filter, char *pwattr,
49         int maxloop, int force, int chaserefs, int noinit, int delay );
50
51 /* This program can be invoked two ways: if -D is used to specify a Bind DN,
52  * that DN will be used repeatedly for all of the Binds. If instead -b is used
53  * to specify a base DN, a search will be done for all "person" objects under
54  * that base DN. Then DNs from this list will be randomly selected for each
55  * Bind request. All of the users must have identical passwords. Also it is
56  * assumed that the users are all onelevel children of the base.
57  */
58 static void
59 usage( char *name )
60 {
61         fprintf( stderr, "usage: %s "
62                 "[-H uri | -h <host> [-p port]] "
63                 "[-D <dn> [-w <passwd>]] "
64                 "[-b <baseDN> [-f <searchfilter>] [-a pwattr]] "
65                 "[-l <loops>] "
66                 "[-L <outerloops>] "
67                 "[-F] "
68                 "[-C] "
69                 "[-I] "
70                 "[-i <ignore>] "
71                 "[-t delay]\n",
72                 name );
73         exit( EXIT_FAILURE );
74 }
75
76 int
77 main( int argc, char **argv )
78 {
79         int             i;
80         char            *uri = NULL;
81         char            *host = "localhost";
82         char            *dn = NULL;
83         char            *base = NULL;
84         char            *filter = "(objectClass=person)";
85         struct berval   pass = { 0, NULL };
86         char            *pwattr = NULL;
87         int             port = -1;
88         int             loops = LOOPS;
89         int             outerloops = 1;
90         int             force = 0;
91         int             chaserefs = 0;
92         int             noinit = 0;
93         int             delay = 0;
94
95         tester_init( "slapd-bind", TESTER_BIND );
96
97         /* by default, tolerate invalid credentials */
98         tester_ignore_str2errlist( "INVALID_CREDENTIALS" );
99
100         while ( (i = getopt( argc, argv, "a:b:H:h:i:p:D:w:l:L:f:FIt:" )) != EOF ) {
101                 switch( i ) {
102                 case 'a':
103                         pwattr = optarg;
104                         break;
105
106                 case 'b':               /* base DN of a tree of user DNs */
107                         base = optarg;
108                         break;
109
110                 case 'C':
111                         chaserefs++;
112                         break;
113
114                 case 'H':               /* the server uri */
115                         uri = optarg;
116                         break;
117
118                 case 'h':               /* the servers host */
119                         host = optarg;
120                         break;
121
122                 case 'i':
123                         tester_ignore_str2errlist( optarg );
124                         break;
125
126                 case 'p':               /* the servers port */
127                         if ( lutil_atoi( &port, optarg ) != 0 ) {
128                                 usage( argv[0] );
129                         }
130                         break;
131
132                 case 'D':
133                         dn = optarg;
134                         break;
135
136                 case 'w':
137                         ber_str2bv( optarg, 0, 0, &pass );
138                         memset( optarg, '*', pass.bv_len );
139                         break;
140
141                 case 'l':               /* the number of loops */
142                         if ( lutil_atoi( &loops, optarg ) != 0 ) {
143                                 usage( argv[0] );
144                         }
145                         break;
146
147                 case 'L':               /* the number of outerloops */
148                         if ( lutil_atoi( &outerloops, optarg ) != 0 ) {
149                                 usage( argv[0] );
150                         }
151                         break;
152
153                 case 'f':
154                         filter = optarg;
155                         break;
156
157                 case 'F':
158                         force++;
159                         break;
160
161                 case 'I':
162                         /* reuse connection */
163                         noinit++;
164                         break;
165
166                 case 't':
167                         /* sleep between binds */
168                         if ( lutil_atoi( &delay, optarg ) != 0 ) {
169                                 usage( argv[0] );
170                         }
171                         break;
172
173                 default:
174                         usage( argv[0] );
175                         break;
176                 }
177         }
178
179         if ( port == -1 && uri == NULL ) {
180                 usage( argv[0] );
181         }
182
183         uri = tester_uri( uri, host, port );
184
185         for ( i = 0; i < outerloops; i++ ) {
186                 if ( base != NULL ) {
187                         do_base( uri, dn, &pass, base, filter, pwattr, loops,
188                                 force, chaserefs, noinit, delay );
189                 } else {
190                         do_bind( uri, dn, &pass, loops,
191                                 force, chaserefs, noinit, NULL );
192                 }
193         }
194
195         exit( EXIT_SUCCESS );
196 }
197
198
199 static int
200 do_bind( char *uri, char *dn, struct berval *pass, int maxloop,
201         int force, int chaserefs, int noinit, LDAP **ldp )
202 {
203         LDAP    *ld = ldp ? *ldp : NULL;
204         int     i, rc = -1;
205         pid_t   pid = getpid();
206
207         if ( maxloop > 1 )
208                 fprintf( stderr, "PID=%ld - Bind(%d): dn=\"%s\".\n",
209                          (long) pid, maxloop, dn );
210
211         for ( i = 0; i < maxloop; i++ ) {
212                 if ( !noinit || ld == NULL ) {
213                         int version = LDAP_VERSION3;
214                         ldap_initialize( &ld, uri );
215                         if ( ld == NULL ) {
216                                 tester_perror( "ldap_initialize", NULL );
217                                 rc = -1;
218                                 break;
219                         }
220
221                         (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
222                                 &version ); 
223                         (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
224                                 chaserefs ? LDAP_OPT_ON: LDAP_OPT_OFF );
225                 }
226
227                 rc = ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, pass, NULL, NULL, NULL );
228                 if ( rc ) {
229                         unsigned first = tester_ignore_err( rc );
230
231                         /* if ignore.. */
232                         if ( first ) {
233                                 /* only log if first occurrence */
234                                 if ( force < 2 || first == 1 ) {
235                                         tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
236                                 }
237                                 rc = LDAP_SUCCESS;
238
239                         } else {
240                                 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
241                         }
242                 }
243                         
244                 if ( !noinit ) {
245                         ldap_unbind_ext( ld, NULL, NULL );
246                         ld = NULL;
247                 }
248
249                 if ( rc != LDAP_SUCCESS ) {
250                         break;
251                 }
252         }
253
254         if ( maxloop > 1 ) {
255                 fprintf( stderr, " PID=%ld - Bind done (%d).\n", (long) pid, rc );
256         }
257
258         if ( ldp && noinit ) {
259                 *ldp = ld;
260
261         } else if ( ld != NULL ) {
262                 ldap_unbind_ext( ld, NULL, NULL );
263         }
264
265         return rc;
266 }
267
268
269 static int
270 do_base( char *uri, char *dn, struct berval *pass, char *base, char *filter, char *pwattr,
271         int maxloop, int force, int chaserefs, int noinit, int delay )
272 {
273         LDAP    *ld = NULL;
274         int     i = 0;
275         pid_t   pid = getpid();
276         int     rc = LDAP_SUCCESS;
277         ber_int_t msgid;
278         LDAPMessage *res, *msg;
279         char **dns = NULL;
280         struct berval *creds = NULL;
281         char *attrs[] = { LDAP_NO_ATTRS, NULL };
282         int ndns = 0;
283 #ifdef _WIN32
284         DWORD beg, end;
285 #else
286         struct timeval beg, end;
287 #endif
288         int version = LDAP_VERSION3;
289         char *nullstr = "";
290
291         srand(pid);
292
293         ldap_initialize( &ld, uri );
294         if ( ld == NULL ) {
295                 tester_perror( "ldap_initialize", NULL );
296                 exit( EXIT_FAILURE );
297         }
298
299         (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version );
300         (void) ldap_set_option( ld, LDAP_OPT_REFERRALS,
301                 chaserefs ? LDAP_OPT_ON: LDAP_OPT_OFF );
302
303         rc = ldap_sasl_bind_s( ld, dn, LDAP_SASL_SIMPLE, pass, NULL, NULL, NULL );
304         if ( rc != LDAP_SUCCESS ) {
305                 tester_ldap_error( ld, "ldap_sasl_bind_s", NULL );
306                 exit( EXIT_FAILURE );
307         }
308
309         fprintf( stderr, "PID=%ld - Bind(%d): base=\"%s\", filter=\"%s\" attr=\"%s\".\n",
310                         (long) pid, maxloop, base, filter, pwattr );
311
312         if ( pwattr != NULL ) {
313                 attrs[ 0 ] = pwattr;
314         }
315         rc = ldap_search_ext( ld, base, LDAP_SCOPE_SUBTREE,
316                         filter, attrs, 0, NULL, NULL, 0, 0, &msgid );
317         if ( rc != LDAP_SUCCESS ) {
318                 tester_ldap_error( ld, "ldap_search_ext", NULL );
319                 exit( EXIT_FAILURE );
320         }
321
322         while ( ( rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res ) ) > 0 )
323         {
324                 BerElement *ber;
325                 struct berval bv;
326                 int done = 0;
327
328                 for ( msg = ldap_first_message( ld, res ); msg;
329                         msg = ldap_next_message( ld, msg ) )
330                 {
331                         switch ( ldap_msgtype( msg ) ) {
332                         case LDAP_RES_SEARCH_ENTRY:
333                                 rc = ldap_get_dn_ber( ld, msg, &ber, &bv );
334                                 dns = realloc( dns, (ndns + 1)*sizeof(char *) );
335                                 dns[ndns] = ber_strdup( bv.bv_val );
336                                 if ( pwattr != NULL ) {
337                                         struct berval   **values = ldap_get_values_len( ld, msg, pwattr );
338
339                                         creds = realloc( creds, (ndns + 1)*sizeof(struct berval) );
340                                         if ( values == NULL ) {
341 novals:;
342                                                 creds[ndns].bv_len = 0;
343                                                 creds[ndns].bv_val = nullstr;
344
345                                         } else {
346                                                 static struct berval    cleartext = BER_BVC( "{CLEARTEXT} " );
347                                                 struct berval           value = *values[ 0 ];
348
349                                                 if ( value.bv_val[ 0 ] == '{' ) {
350                                                         char *end = ber_bvchr( &value, '}' );
351
352                                                         if ( end ) {
353                                                                 if ( ber_bvcmp( &value, &cleartext ) == 0 ) {
354                                                                         value.bv_val += cleartext.bv_len;
355                                                                         value.bv_len -= cleartext.bv_len;
356
357                                                                 } else {
358                                                                         ldap_value_free_len( values );
359                                                                         goto novals;
360                                                                 }
361                                                         }
362
363                                                 }
364
365                                                 ber_dupbv( &creds[ndns], &value );
366                                                 ldap_value_free_len( values );
367                                         }
368                                 }
369                                 ndns++;
370                                 ber_free( ber, 0 );
371                                 break;
372
373                         case LDAP_RES_SEARCH_RESULT:
374                                 done = 1;
375                                 break;
376                         }
377                         if ( done )
378                                 break;
379                 }
380                 ldap_msgfree( res );
381                 if ( done ) break;
382         }
383
384 #ifdef _WIN32
385         beg = GetTickCount();
386 #else
387         gettimeofday( &beg, NULL );
388 #endif
389
390         if ( ndns == 0 ) {
391                 tester_error( "No DNs" );
392                 return 1;
393         }
394
395         fprintf( stderr, "  PID=%ld - Bind base=\"%s\" filter=\"%s\" got %d values.\n",
396                 (long) pid, base, filter, ndns );
397
398         /* Ok, got list of DNs, now start binding to each */
399         for ( i = 0; i < maxloop; i++ ) {
400                 int j, k;
401                 struct berval cred = { 0, NULL };
402
403                 for ( j = 0, k = 0; k < ndns; k++) {
404                         j = rand() % ndns;
405                 }
406
407                 if ( creds && !BER_BVISEMPTY( &creds[j] ) ) {
408                         cred = creds[j];
409                 }
410                 if ( do_bind( uri, dns[j], &cred, 1, force, chaserefs, noinit, &ld )
411                         && !force )
412                 {
413                         break;
414                 }
415
416                 if ( delay ) {
417                         sleep( delay );
418                 }
419         }
420
421         if ( ld != NULL ) {
422                 ldap_unbind_ext( ld, NULL, NULL );
423                 ld = NULL;
424         }
425
426 #ifdef _WIN32
427         end = GetTickCount();
428         end -= beg;
429
430         fprintf( stderr, " PID=%ld - Bind done %d in %d.%03d seconds.\n",
431                 (long) pid, i, end / 1000, end % 1000 );
432 #else
433         gettimeofday( &end, NULL );
434         end.tv_usec -= beg.tv_usec;
435         if (end.tv_usec < 0 ) {
436                 end.tv_usec += 1000000;
437                 end.tv_sec -= 1;
438         }
439         end.tv_sec -= beg.tv_sec;
440
441         fprintf( stderr, " PID=%ld - Bind done %d in %ld.%06ld seconds.\n",
442                 (long) pid, i, (long) end.tv_sec, (long) end.tv_usec );
443 #endif
444
445         if ( dns ) {
446                 for ( i = 0; i < ndns; i++ ) {
447                         free( dns[i] );
448                 }
449                 free( dns );
450         }
451
452         if ( creds ) {
453                 for ( i = 0; i < ndns; i++ ) {
454                         if ( creds[i].bv_val != nullstr ) {
455                                 free( creds[i].bv_val );
456                         }
457                 }
458                 free( creds );
459         }
460
461         return 0;
462 }