]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-bind.c
ITS#5177 from HEAD
[openldap] / tests / progs / slapd-bind.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2007 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 #define LDAP_DEPRECATED 1
36 #include <ldap.h>
37 #include <lutil.h>
38
39 #define LOOPS   100
40
41 static int
42 do_bind( char *uri, char *host, int port, char *dn, char *pass, int maxloop,
43         int force );
44
45 static int
46 do_base( char *uri, char *host, int port, char *base, char *pass, int maxloop,
47         int force );
48
49 /* This program can be invoked two ways: if -D is used to specify a Bind DN,
50  * that DN will be used repeatedly for all of the Binds. If instead -b is used
51  * to specify a base DN, a search will be done for all "person" objects under
52  * that base DN. Then DNs from this list will be randomly selected for each
53  * Bind request. All of the users must have identical passwords. Also it is
54  * assumed that the users are all onelevel children of the base.
55  */
56 static void
57 usage( char *name )
58 {
59         fprintf( stderr, "usage: %s [-h <host>] -p port (-D <dn>|-b <baseDN> [-f <searchfilter>]) -w <passwd> [-l <loops>] [-F]\n",
60                         name );
61         exit( EXIT_FAILURE );
62 }
63
64 static char *filter = "(objectClass=person)";
65
66 int
67 main( int argc, char **argv )
68 {
69         int             i;
70         char            *uri = NULL;
71         char            *host = "localhost";
72         char            *dn = NULL;
73         char            *base = NULL;
74         char            *pass = NULL;
75         int             port = -1;
76         int             loops = LOOPS;
77         int             force = 0;
78
79         while ( (i = getopt( argc, argv, "b:H:h:p:D:w:l:f:F" )) != EOF ) {
80                 switch( i ) {
81                         case 'b':               /* base DN of a tree of user DNs */
82                                 base = strdup( optarg );
83                                 break;
84
85                         case 'H':               /* the server uri */
86                                 uri = strdup( optarg );
87                         break;
88                         case 'h':               /* the servers host */
89                                 host = strdup( optarg );
90                         break;
91
92                         case 'p':               /* the servers port */
93                                 if ( lutil_atoi( &port, optarg ) != 0 ) {
94                                         usage( argv[0] );
95                                 }
96                                 break;
97
98                         case 'D':
99                                 dn = strdup( optarg );
100                                 break;
101
102                         case 'w':
103                                 pass = strdup( optarg );
104                                 break;
105
106                         case 'l':               /* the number of loops */
107                                 if ( lutil_atoi( &loops, optarg ) != 0 ) {
108                                         usage( argv[0] );
109                                 }
110                                 break;
111
112                         case 'f':
113                                 filter = optarg;
114                                 break;
115
116                         case 'F':
117                                 force = 1;
118                                 break;
119
120                         default:
121                                 usage( argv[0] );
122                                 break;
123                 }
124         }
125
126         if ( port == -1 && uri == NULL )
127                 usage( argv[0] );
128
129         if ( base )
130                 do_base( uri, host, port, base, pass, ( 20 * loops ), force );
131         else
132                 do_bind( uri, host, port, dn, pass, ( 20 * loops ), force );
133         exit( EXIT_SUCCESS );
134 }
135
136
137 static int
138 do_bind( char *uri, char *host, int port, char *dn, char *pass, int maxloop,
139         int force )
140 {
141         LDAP    *ld = NULL;
142         int     i, rc = -1;
143         pid_t   pid = getpid();
144
145         if ( maxloop > 1 )
146                 fprintf( stderr, "PID=%ld - Bind(%d): dn=\"%s\".\n",
147                          (long) pid, maxloop, dn );
148
149         for ( i = 0; i < maxloop; i++ ) {
150                 if ( uri ) {
151                         ldap_initialize( &ld, uri );
152                 } else {
153                         ld = ldap_init( host, port );
154                 }
155                 if ( ld == NULL ) {
156                         perror( "ldap_init" );
157                         rc = -1;
158                         break;
159                 }
160
161                 {
162                         int version = LDAP_VERSION3;
163                         (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
164                                 &version ); 
165                 }
166
167                 rc = ldap_bind_s( ld, dn, pass, LDAP_AUTH_SIMPLE );
168                 if ( rc != LDAP_SUCCESS ) {
169                         ldap_perror( ld, "ldap_bind" );
170                 }
171                 ldap_unbind( ld );
172                 if ( rc != LDAP_SUCCESS && !force ) {
173                         break;
174                 }
175         }
176
177         if ( maxloop > 1 )
178                 fprintf( stderr, " PID=%ld - Bind done.\n", (long) pid );
179
180         return rc;
181 }
182
183
184 static int
185 do_base( char *uri, char *host, int port, char *base, char *pass, int maxloop,
186         int force )
187 {
188         LDAP    *ld = NULL;
189         int     i = 0;
190         pid_t   pid = getpid();
191         int     rc = LDAP_SUCCESS;
192         ber_int_t msgid;
193         LDAPMessage *res, *msg;
194         char **rdns = NULL;
195         char *attrs[] = { "dn", NULL };
196         int nrdns = 0;
197 #ifdef _WIN32
198         DWORD beg, end;
199 #else
200         struct timeval beg, end;
201 #endif
202
203         srand(pid);
204
205         if ( uri ) {
206                 ldap_initialize( &ld, uri );
207         } else {
208                 ld = ldap_init( host, port );
209         }
210         if ( ld == NULL ) {
211                 perror( "ldap_init" );
212                 exit( EXIT_FAILURE );
213         }
214
215         {
216                 int version = LDAP_VERSION3;
217                 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
218                         &version ); 
219         }
220         (void) ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
221
222         rc = ldap_bind_s( ld, NULL, NULL, LDAP_AUTH_SIMPLE );
223         if ( rc != LDAP_SUCCESS ) {
224                 ldap_perror( ld, "ldap_bind" );
225                 exit( EXIT_FAILURE );
226         }
227
228         rc = ldap_search_ext( ld, base, LDAP_SCOPE_ONE,
229                         filter, attrs, 0, NULL, NULL, 0, 0, &msgid );
230         if ( rc != LDAP_SUCCESS ) {
231                 ldap_perror( ld, "ldap_search_ex" );
232                 exit( EXIT_FAILURE );
233         }
234
235         while (( rc=ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res )) >0){
236                 BerElement *ber;
237                 struct berval bv;
238                 char *ptr;
239                 int done = 0;
240
241                 for (msg = ldap_first_message( ld, res ); msg;
242                         msg = ldap_next_message( ld, msg )) {
243                         switch ( ldap_msgtype( msg )) {
244                         case LDAP_RES_SEARCH_ENTRY:
245                                 rc = ldap_get_dn_ber( ld, msg, &ber, &bv );
246                                 ptr = strchr( bv.bv_val, ',');
247                                 i = ptr-bv.bv_val;
248                                 rdns = realloc( rdns, (nrdns+1)*sizeof(char *));
249                                 rdns[nrdns] = malloc( i+1 );
250                                 strncpy(rdns[nrdns], bv.bv_val, i );
251                                 rdns[nrdns][i] = '\0';
252                                 nrdns++;
253                                 ber_free( ber, 0 );
254                                 break;
255                         case LDAP_RES_SEARCH_RESULT:
256                                 done = 1;
257                                 break;
258                         }
259                         if ( done )
260                                 break;
261                 }
262                 ldap_msgfree( res );
263                 if ( done ) break;
264         }
265         ldap_unbind( ld );
266
267 #ifdef _WIN32
268         beg = GetTickCount();
269 #else
270         gettimeofday( &beg, NULL );
271 #endif
272
273         if ( nrdns == 0 ) {
274                 fprintf( stderr, "No RDNs.\n" );
275                 return 1;
276         }
277
278         /* Ok, got list of RDNs, now start binding to each */
279         for (i=0; i<maxloop; i++) {
280                 char dn[BUFSIZ], *ptr;
281                 int j = rand() % nrdns;
282                 ptr = lutil_strcopy(dn, rdns[j]);
283                 *ptr++ = ',';
284                 strcpy(ptr, base);
285                 if ( do_bind( uri, host, port, dn, pass, 1, force ) && !force )
286                         break;
287         }
288 #ifdef _WIN32
289         end = GetTickCount();
290         end -= beg;
291
292         fprintf( stderr, "Done %d Binds in %d.%03d seconds.\n", i,
293                 end / 1000, end % 1000 );
294 #else
295         gettimeofday( &end, NULL );
296         end.tv_usec -= beg.tv_usec;
297         if (end.tv_usec < 0 ) {
298                 end.tv_usec += 1000000;
299                 end.tv_sec -= 1;
300         }
301         end.tv_sec -= beg.tv_sec;
302
303         fprintf( stderr, "Done %d Binds in %ld.%06ld seconds.\n", i,
304                 (long) end.tv_sec, (long) end.tv_usec );
305 #endif
306         return 0;
307 }