]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-read.c
Update build tools based upon OPENLDAP_AC work
[openldap] / tests / progs / slapd-read.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_read( char *uri, char *host, int port, char *entry, int maxloop,
41                 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                 "-e <entry> "
50                 "[-l <loops>] "
51                 "[-r <maxretries>] "
52                 "[-t <delay>]\n",
53                         name );
54         exit( EXIT_FAILURE );
55 }
56
57 int
58 main( int argc, char **argv )
59 {
60         int             i;
61         char            *uri = NULL;
62         char            *host = "localhost";
63         int             port = -1;
64         char            *entry = NULL;
65         int             loops = LOOPS;
66         int             retries = RETRIES;
67         int             delay = 0;
68
69         while ( (i = getopt( argc, argv, "H:h:p:e:l:r:t:" )) != EOF ) {
70                 switch( i ) {
71                 case 'H':               /* the server uri */
72                         uri = strdup( optarg );
73                         break;
74
75                 case 'h':               /* the servers host */
76                         host = strdup( optarg );
77                         break;
78
79                 case 'p':               /* the servers port */
80                         port = atoi( optarg );
81                         break;
82
83                 case 'e':               /* DN to search for */
84                         entry = strdup( optarg );
85                         break;
86
87                 case 'l':               /* the number of loops */
88                         loops = atoi( optarg );
89                         break;
90
91                 case 'r':               /* the number of retries */
92                         retries = atoi( optarg );
93                         break;
94
95                 case 't':               /* delay in seconds */
96                         delay = atoi( optarg );
97                         break;
98
99                 default:
100                         usage( argv[0] );
101                         break;
102                 }
103         }
104
105         if (( entry == NULL ) || ( port == -1 && uri == NULL ))
106                 usage( argv[0] );
107
108         if ( *entry == '\0' ) {
109                 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
110                                 argv[0] );
111                 exit( EXIT_FAILURE );
112         }
113
114         do_read( uri, host, port, entry, ( 20 * loops ), retries, delay );
115         exit( EXIT_SUCCESS );
116 }
117
118
119 static void
120 do_read( char *uri, char *host, int port, char *entry, int maxloop,
121                 int maxretries, int delay )
122 {
123         LDAP    *ld = NULL;
124         int     i = 0, do_retry = maxretries;
125         char    *attrs[] = { "1.1", NULL };
126         pid_t   pid = getpid();
127         int     rc = LDAP_SUCCESS;
128         
129 retry:;
130         if ( uri ) {
131                 ldap_initialize( &ld, uri );
132         } else {
133                 ld = ldap_init( host, port );
134         }
135         if ( ld == NULL ) {
136                 perror( "ldap_init" );
137                 exit( EXIT_FAILURE );
138         }
139
140         {
141                 int version = LDAP_VERSION3;
142                 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
143                         &version ); 
144         }
145
146         if ( do_retry == maxretries ) {
147                 fprintf( stderr, "PID=%ld - Read(%d): entry=\"%s\".\n",
148                         (long) pid, maxloop, entry );
149         }
150
151         rc = ldap_bind_s( ld, NULL, NULL, LDAP_AUTH_SIMPLE );
152         if ( rc != LDAP_SUCCESS ) {
153                 ldap_perror( ld, "ldap_bind" );
154                 switch ( rc ) {
155                 case LDAP_BUSY:
156                 case LDAP_UNAVAILABLE:
157                         if ( do_retry > 0 ) {
158                                 do_retry--;
159                                 if ( delay > 0 ) {
160                                     sleep( delay );
161                                 }
162                                 goto retry;
163                         }
164                 /* fallthru */
165                 default:
166                         break;
167                 }
168                 exit( EXIT_FAILURE );
169         }
170
171         for ( ; i < maxloop; i++ ) {
172                 LDAPMessage *res;
173
174                 rc = ldap_search_s( ld, entry, LDAP_SCOPE_BASE,
175                                 NULL, attrs, 1, &res );
176                 if ( rc != LDAP_SUCCESS ) {
177                         ldap_perror( ld, "ldap_read" );
178                         if ( rc == LDAP_BUSY && do_retry > 0 ) {
179                                 do_retry--;
180                                 goto retry;
181                         }
182                         if ( rc != LDAP_NO_SUCH_OBJECT ) break;
183                         continue;
184
185                 }
186
187                 ldap_msgfree( res );
188         }
189
190         fprintf( stderr, " PID=%ld - Read done (%d).\n", (long) pid, rc );
191
192         ldap_unbind( ld );
193 }
194