]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-bind.c
Use GetTickCount for timing on Windows
[openldap] / tests / progs / slapd-bind.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2005 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                                 port = atoi( optarg );
94                                 break;
95
96                         case 'D':
97                                 dn = strdup( optarg );
98                                 break;
99
100                         case 'w':
101                                 pass = strdup( optarg );
102                                 break;
103
104                         case 'l':               /* the number of loops */
105                                 loops = atoi( optarg );
106                                 break;
107
108                         case 'f':
109                                 filter = optarg;
110                                 break;
111
112                         case 'F':
113                                 force = 1;
114                                 break;
115
116                         default:
117                                 usage( argv[0] );
118                                 break;
119                 }
120         }
121
122         if ( port == -1 && uri == NULL )
123                 usage( argv[0] );
124
125         if ( base )
126                 do_base( uri, host, port, base, pass, ( 20 * loops ), force );
127         else
128                 do_bind( uri, host, port, dn, pass, ( 20 * loops ), force );
129         exit( EXIT_SUCCESS );
130 }
131
132
133 static int
134 do_bind( char *uri, char *host, int port, char *dn, char *pass, int maxloop,
135         int force )
136 {
137         LDAP    *ld = NULL;
138         int     i, rc = -1;
139         pid_t   pid = getpid();
140
141         if ( maxloop > 1 )
142                 fprintf( stderr, "PID=%ld - Bind(%d): dn=\"%s\".\n",
143                          (long) pid, maxloop, dn );
144
145         for ( i = 0; i < maxloop; i++ ) {
146                 if ( uri ) {
147                         ldap_initialize( &ld, uri );
148                 } else {
149                         ld = ldap_init( host, port );
150                 }
151                 if ( ld == NULL ) {
152                         perror( "ldap_init" );
153                         rc = -1;
154                         break;
155                 }
156
157                 {
158                         int version = LDAP_VERSION3;
159                         (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
160                                 &version ); 
161                 }
162
163                 rc = ldap_bind_s( ld, dn, pass, LDAP_AUTH_SIMPLE );
164                 if ( rc != LDAP_SUCCESS ) {
165                         ldap_perror( ld, "ldap_bind" );
166                 }
167                 ldap_unbind( ld );
168                 if ( rc != LDAP_SUCCESS && !force ) {
169                         break;
170                 }
171         }
172
173         if ( maxloop > 1 )
174                 fprintf( stderr, " PID=%ld - Bind done.\n", (long) pid );
175
176         return rc;
177 }
178
179
180 static int
181 do_base( char *uri, char *host, int port, char *base, char *pass, int maxloop,
182         int force )
183 {
184         LDAP    *ld = NULL;
185         int     i = 0;
186         pid_t   pid = getpid();
187         int     rc = LDAP_SUCCESS;
188         ber_int_t msgid;
189         LDAPMessage *res, *msg;
190         char **rdns = NULL;
191         char *attrs[] = { "dn", NULL };
192         int nrdns = 0;
193 #ifdef _WIN32
194         DWORD beg, end;
195 #else
196         struct timeval beg, end;
197 #endif
198
199         srand(pid);
200
201         if ( uri ) {
202                 ldap_initialize( &ld, uri );
203         } else {
204                 ld = ldap_init( host, port );
205         }
206         if ( ld == NULL ) {
207                 perror( "ldap_init" );
208                 exit( EXIT_FAILURE );
209         }
210
211         {
212                 int version = LDAP_VERSION3;
213                 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
214                         &version ); 
215         }
216         (void) ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
217
218         rc = ldap_bind_s( ld, NULL, NULL, LDAP_AUTH_SIMPLE );
219         if ( rc != LDAP_SUCCESS ) {
220                 ldap_perror( ld, "ldap_bind" );
221                 exit( EXIT_FAILURE );
222         }
223
224         rc = ldap_search_ext( ld, base, LDAP_SCOPE_ONE,
225                         filter, attrs, 0, NULL, NULL, 0, 0, &msgid );
226         if ( rc != LDAP_SUCCESS ) {
227                 ldap_perror( ld, "ldap_search_ex" );
228                 exit( EXIT_FAILURE );
229         }
230
231         while (( rc=ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res )) >0){
232                 BerElement *ber;
233                 struct berval bv;
234                 char *ptr;
235                 int done = 0;
236
237                 for (msg = ldap_first_message( ld, res ); msg;
238                         msg = ldap_next_message( ld, msg )) {
239                         switch ( ldap_msgtype( msg )) {
240                         case LDAP_RES_SEARCH_ENTRY:
241                                 rc = ldap_get_dn_ber( ld, msg, &ber, &bv );
242                                 ptr = strchr( bv.bv_val, ',');
243                                 i = ptr-bv.bv_val;
244                                 rdns = realloc( rdns, (nrdns+1)*sizeof(char *));
245                                 rdns[nrdns] = malloc( i+1 );
246                                 strncpy(rdns[nrdns], bv.bv_val, i );
247                                 rdns[nrdns][i] = '\0';
248                                 nrdns++;
249                                 ber_free( ber, 0 );
250                                 break;
251                         case LDAP_RES_SEARCH_RESULT:
252                                 done = 1;
253                                 break;
254                         }
255                         if ( done )
256                                 break;
257                 }
258                 ldap_msgfree( res );
259                 if ( done ) break;
260         }
261         ldap_unbind( ld );
262
263 #ifdef _WIN32
264         beg = GetTickCount();
265 #else
266         gettimeofday( &beg, NULL );
267 #endif
268
269         if ( nrdns == 0 ) {
270                 fprintf( stderr, "No RDNs.\n" );
271                 return 1;
272         }
273
274         /* Ok, got list of RDNs, now start binding to each */
275         for (i=0; i<maxloop; i++) {
276                 char dn[BUFSIZ], *ptr;
277                 int j = rand() % nrdns;
278                 ptr = lutil_strcopy(dn, rdns[j]);
279                 *ptr++ = ',';
280                 strcpy(ptr, base);
281                 if ( do_bind( uri, host, port, dn, pass, 1, force ) && !force )
282                         break;
283         }
284 #ifdef _WIN32
285         end = GetTickCount();
286         end -= beg;
287
288         fprintf( stderr, "Done %d Binds in %d.%03d seconds.\n", i,
289                 end / 1000, end % 1000 );
290 #else
291         gettimeofday( &end, NULL );
292         end.tv_usec -= beg.tv_usec;
293         if (end.tv_usec < 0 ) {
294                 end.tv_usec += 1000000;
295                 end.tv_sec -= 1;
296         }
297         end.tv_sec -= beg.tv_sec;
298
299         fprintf( stderr, "Done %d Binds in %d.%06d seconds.\n", i,
300                 end.tv_sec, end.tv_usec );
301 #endif
302         return 0;
303 }