]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-bind.c
Perform binds in a loop, using either a single DN or many DNs
[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 Kurt Spanier 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 void
40 do_bind( char *uri, char *host, int port, char *dn, char *pass, int maxloop );
41
42 static void
43 do_base( char *uri, char *host, int port, char *base, char *pass, int maxloop );
44
45 static void
46 usage( char *name )
47 {
48         fprintf( stderr, "usage: %s [-h <host>] -p port (-D <dn>|-b <baseDN>) -w <passwd> [-l <loops>]\n",
49                         name );
50         exit( EXIT_FAILURE );
51 }
52
53 static char *filter = "(objectClass=person)";
54
55 int
56 main( int argc, char **argv )
57 {
58         int             i;
59         char            *uri = NULL;
60         char        *host = "localhost";
61         char            *dn = NULL;
62         char            *base = NULL;
63         char            *pass = NULL;
64         int                     port = -1;
65         int                     loops = LOOPS;
66
67         while ( (i = getopt( argc, argv, "b:H:h:p:D:w:l:" )) != EOF ) {
68                 switch( i ) {
69                         case 'b':               /* base DN of a tree of user DNs */
70                                 base = strdup( optarg );
71                                 break;
72
73                         case 'H':               /* the server uri */
74                                 uri = strdup( optarg );
75                         break;
76                         case 'h':               /* the servers host */
77                                 host = strdup( optarg );
78                         break;
79
80                         case 'p':               /* the servers port */
81                                 port = atoi( optarg );
82                                 break;
83
84                         case 'D':
85                                 dn = strdup( optarg );
86                                 break;
87
88                         case 'w':
89                                 pass = strdup( optarg );
90                                 break;
91
92                         case 'l':               /* the number of loops */
93                                 loops = atoi( optarg );
94                                 break;
95
96                         default:
97                                 usage( argv[0] );
98                                 break;
99                 }
100         }
101
102         if ( port == -1 && uri == NULL )
103                 usage( argv[0] );
104
105         if ( base )
106                 do_base( uri, host, port, base, pass, ( 20 * loops ));
107         else
108                 do_bind( uri, host, port, dn, pass, ( 20 * loops ));
109         exit( EXIT_SUCCESS );
110 }
111
112
113 static void
114 do_bind( char *uri, char *host, int port, char *dn, char *pass, int maxloop )
115 {
116         LDAP    *ld = NULL;
117         int     i;
118         char    *attrs[] = { "1.1", NULL };
119         pid_t   pid = getpid();
120
121         if ( maxloop > 1 )
122                 fprintf( stderr, "PID=%ld - Bind(%d): dn=\"%s\".\n",
123                          (long) pid, maxloop, dn );
124
125         for ( i = 0; i < maxloop; i++ ) {
126                 LDAPMessage *res;
127                 int         rc;
128
129                 if ( uri ) {
130                         ldap_initialize( &ld, uri );
131                 } else {
132                         ld = ldap_init( host, port );
133                 }
134                 if ( ld == NULL ) {
135                         perror( "ldap_init" );
136                         break;
137                 }
138
139                 {
140                         int version = LDAP_VERSION3;
141                         (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
142                                 &version ); 
143                 }
144
145                 rc = ldap_bind_s( ld, dn, pass, LDAP_AUTH_SIMPLE );
146                 if ( rc != LDAP_SUCCESS ) {
147                         ldap_perror( ld, "ldap_bind" );
148                 }
149                 ldap_unbind( ld );
150                 if ( rc != LDAP_SUCCESS ) {
151                         break;
152                 }
153         }
154
155         if ( maxloop > 1 )
156                 fprintf( stderr, " PID=%ld - Bind done.\n", (long) pid );
157
158 }
159
160
161 static void
162 do_base( char *uri, char *host, int port, char *base, char *pass, int maxloop )
163 {
164         LDAP    *ld = NULL;
165         int     i = 0;
166         pid_t   pid = getpid();
167         int     rc = LDAP_SUCCESS;
168         ber_int_t msgid;
169         LDAPMessage *res, *msg;
170         char **rdns = NULL;
171         char *attrs[] = { "dn", NULL };
172         int nrdns = 0;
173
174         srand(pid);
175
176         if ( uri ) {
177                 ldap_initialize( &ld, uri );
178         } else {
179                 ld = ldap_init( host, port );
180         }
181         if ( ld == NULL ) {
182                 perror( "ldap_init" );
183                 exit( EXIT_FAILURE );
184         }
185
186         {
187                 int version = LDAP_VERSION3;
188                 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
189                         &version ); 
190         }
191         (void) ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
192
193         rc = ldap_bind_s( ld, NULL, NULL, LDAP_AUTH_SIMPLE );
194         if ( rc != LDAP_SUCCESS ) {
195                 ldap_perror( ld, "ldap_bind" );
196                 exit( EXIT_FAILURE );
197         }
198
199         rc = ldap_search_ext( ld, base, LDAP_SCOPE_SUBTREE,
200                         filter, attrs, 0, NULL, NULL, 0, 0, &msgid );
201         if ( rc != LDAP_SUCCESS ) {
202                 ldap_perror( ld, "ldap_search_ex" );
203                 exit( EXIT_FAILURE );
204         }
205
206         while (( rc=ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res )) >0){
207                 BerElement *ber;
208                 struct berval bv;
209                 char *ptr;
210                 int done = 0;
211
212                 for (msg = ldap_first_message( ld, res ); msg;
213                         msg = ldap_next_message( ld, msg )) {
214                         switch ( ldap_msgtype( msg )) {
215                         case LDAP_RES_SEARCH_ENTRY:
216                                 rc = ldap_get_dn_ber( ld, msg, &ber, &bv );
217                                 ptr = strchr( bv.bv_val, ',');
218                                 i = ptr-bv.bv_val;
219                                 rdns = realloc( rdns, (nrdns+1)*sizeof(char *));
220                                 rdns[nrdns] = malloc( i+1 );
221                                 strncpy(rdns[nrdns], bv.bv_val, i );
222                                 rdns[nrdns][i] = '\0';
223                                 nrdns++;
224                                 ber_free( ber, 0 );
225                                 break;
226                         case LDAP_RES_SEARCH_RESULT:
227                                 done = 1;
228                                 break;
229                         }
230                         if ( done )
231                                 break;
232                 }
233                 ldap_msgfree( res );
234                 if ( done ) break;
235         }
236         ldap_unbind( ld );
237
238         /* Ok, got list of RDNs, now start binding to each */
239         for (i=0; i<maxloop; i++) {
240                 char dn[BUFSIZ], *ptr;
241                 int j = rand() % nrdns;
242                 ptr = lutil_strcopy(dn, rdns[j]);
243                 *ptr++ = ',';
244                 strcpy(ptr, base);
245                 do_bind( uri, host, port, dn, pass, 1 );
246         }
247 }