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