]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-search.c
Better fix for req_pwdexop_s
[openldap] / tests / progs / slapd-search.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
36 #define LOOPS   100
37 #define RETRIES 0
38
39 static void
40 do_search( char *uri, char *host, int port, char *manager, char *passwd,
41                 char *sbase, char *filter, int maxloop, int maxretries, int delay );
42
43 static void
44 usage( char *name )
45 {
46         fprintf( stderr,
47                 "usage: %s "
48                 "-H <uri> | ([-h <host>] -p <port>) "
49                 "-D <manager> "
50                 "-w <passwd> "
51                 "-b <searchbase> "
52                 "-f <searchfilter> "
53                 "[-l <loops>] "
54                 "[-r <maxretries>] "
55                 "[-t <delay>]\n",
56                         name );
57         exit( EXIT_FAILURE );
58 }
59
60 int
61 main( int argc, char **argv )
62 {
63         int             i;
64         char            *uri = NULL;
65         char            *host = "localhost";
66         int             port = -1;
67         char            *manager = NULL;
68         char            *passwd = NULL;
69         char            *sbase = NULL;
70         char            *filter  = NULL;
71         int             loops = LOOPS;
72         int             retries = RETRIES;
73         int             delay = 0;
74
75         while ( (i = getopt( argc, argv, "b:D:f:H:h:l:p:w:r:t:" )) != EOF ) {
76                 switch( i ) {
77                 case 'H':               /* the server uri */
78                         uri = strdup( optarg );
79                         break;
80
81                 case 'h':               /* the servers host */
82                         host = strdup( optarg );
83                         break;
84
85                 case 'p':               /* the servers port */
86                         port = atoi( optarg );
87                         break;
88
89                 case 'D':               /* the servers manager */
90                         manager = strdup( optarg );
91                         break;
92
93                 case 'w':               /* the server managers password */
94                         passwd = strdup( optarg );
95                         break;
96
97                 case 'b':               /* file with search base */
98                         sbase = strdup( optarg );
99                         break;
100
101                 case 'f':               /* the search request */
102                         filter = strdup( optarg );
103                         break;
104
105                 case 'l':               /* number of loops */
106                         loops = atoi( optarg );
107                         break;
108
109                 case 'r':               /* number of retries */
110                         retries = atoi( optarg );
111                         break;
112
113                 case 't':               /* delay in seconds */
114                         delay = atoi( optarg );
115                         break;
116
117                 default:
118                         usage( argv[0] );
119                         break;
120                 }
121         }
122
123         if (( sbase == NULL ) || ( filter == NULL ) || ( port == -1 && uri == NULL ))
124                 usage( argv[0] );
125
126         if ( *filter == '\0' ) {
127
128                 fprintf( stderr, "%s: invalid EMPTY search filter.\n",
129                                 argv[0] );
130                 exit( EXIT_FAILURE );
131
132         }
133
134         do_search( uri, host, port, manager, passwd, sbase, filter,
135                         ( 10 * loops ), retries, delay );
136         exit( EXIT_SUCCESS );
137 }
138
139
140 static void
141 do_search( char *uri, char *host, int port, char *manager, char *passwd,
142                 char *sbase, char *filter, int maxloop, int maxretries, int delay )
143 {
144         LDAP    *ld = NULL;
145         int     i = 0, do_retry = maxretries;
146         char    *attrs[] = { "cn", "sn", NULL };
147         pid_t   pid = getpid();
148         int     rc = LDAP_SUCCESS;
149
150 retry:;
151         if ( uri ) {
152                 ldap_initialize( &ld, uri );
153         } else {
154                 ld = ldap_init( host, port );
155         }
156         if ( ld == NULL ) {
157                 perror( "ldap_init" );
158                 exit( EXIT_FAILURE );
159         }
160
161         {
162                 int version = LDAP_VERSION3;
163                 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
164                         &version ); 
165         }
166
167         if ( do_retry == maxretries ) {
168                 fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\".\n",
169                                 (long) pid, maxloop, sbase, filter );
170         }
171
172         rc = ldap_bind_s( ld, manager, passwd, LDAP_AUTH_SIMPLE );
173         if ( rc != LDAP_SUCCESS ) {
174                 ldap_perror( ld, "ldap_bind" );
175                 switch ( rc ) {
176                 case LDAP_BUSY:
177                 case LDAP_UNAVAILABLE:
178                         if ( do_retry > 0 ) {
179                                 do_retry--;
180                                 if ( delay != 0 ) {
181                                     sleep( delay );
182                                 }
183                                 goto retry;
184                         }
185                 /* fallthru */
186                 default:
187                         break;
188                 }
189                 exit( EXIT_FAILURE );
190         }
191
192         for ( ; i < maxloop; i++ ) {
193                 LDAPMessage *res;
194
195                 rc = ldap_search_s( ld, sbase, LDAP_SCOPE_SUBTREE,
196                                 filter, attrs, 0, &res );
197                 if ( rc != LDAP_SUCCESS ) {
198                         ldap_perror( ld, "ldap_search" );
199                         if ( rc == LDAP_BUSY && do_retry > 0 ) {
200                                 do_retry--;
201                                 goto retry;
202                         }
203                         if ( rc != LDAP_NO_SUCH_OBJECT ) break;
204                         continue;
205
206                 }
207
208                 ldap_msgfree( res );
209         }
210
211         fprintf( stderr, " PID=%ld - Search done (%d).\n", (long) pid, rc );
212
213         ldap_unbind( ld );
214 }