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