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