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