]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-read.c
Fix lib specs
[openldap] / tests / progs / slapd-read.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 #include "portable.h"
7
8 #include <stdio.h>
9
10 #include <ac/stdlib.h>
11
12 #include <ac/ctype.h>
13 #include <ac/param.h>
14 #include <ac/socket.h>
15 #include <ac/string.h>
16 #include <ac/unistd.h>
17 #include <ac/wait.h>
18
19 #include <ldap.h>
20
21 #define LOOPS   100
22
23 static void
24 do_read( char *uri, char *host, int port, char *entry, int maxloop );
25
26 static void
27 usage( char *name )
28 {
29         fprintf( stderr, "usage: %s [-h <host>] -p port -e <entry> [-l <loops>]\n",
30                         name );
31         exit( EXIT_FAILURE );
32 }
33
34 int
35 main( int argc, char **argv )
36 {
37         int             i;
38         char            *uri = NULL;
39         char        *host = "localhost";
40         int                     port = -1;
41         char            *entry = NULL;
42         int                     loops = LOOPS;
43
44         while ( (i = getopt( argc, argv, "H:h:p:e:l:" )) != EOF ) {
45                 switch( i ) {
46                         case 'H':               /* the server uri */
47                                 uri = strdup( optarg );
48                         break;
49                         case 'h':               /* the servers host */
50                                 host = strdup( optarg );
51                         break;
52
53                         case 'p':               /* the servers port */
54                                 port = atoi( optarg );
55                                 break;
56
57                         case 'e':               /* file with entry search request */
58                                 entry = strdup( optarg );
59                                 break;
60
61                         case 'l':               /* the number of loops */
62                                 loops = atoi( optarg );
63                                 break;
64
65                         default:
66                                 usage( argv[0] );
67                                 break;
68                 }
69         }
70
71         if (( entry == NULL ) || ( port == -1 && uri == NULL ))
72                 usage( argv[0] );
73
74         if ( *entry == '\0' ) {
75
76                 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
77                                 argv[0] );
78                 exit( EXIT_FAILURE );
79
80         }
81
82         do_read( uri, host, port, entry, ( 20 * loops ));
83         exit( EXIT_SUCCESS );
84 }
85
86
87 static void
88 do_read( char *uri, char *host, int port, char *entry, int maxloop )
89 {
90         LDAP    *ld = NULL;
91         int     i;
92         char    *attrs[] = { "1.1", NULL };
93         pid_t   pid = getpid();
94
95         if ( uri ) {
96                 ldap_initialize( &ld, uri );
97         } else {
98                 ld = ldap_init( host, port );
99         }
100         if ( ld == NULL ) {
101                 perror( "ldap_init" );
102                 exit( EXIT_FAILURE );
103         }
104
105         {
106                 int version = LDAP_VERSION3;
107                 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
108                         &version ); 
109         }
110
111         if ( ldap_bind_s( ld, NULL, NULL, LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
112                 ldap_perror( ld, "ldap_bind" );
113                  exit( EXIT_FAILURE );
114         }
115
116
117         fprintf( stderr, "PID=%ld - Read(%d): entry=\"%s\".\n",
118                  (long) pid, maxloop, entry );
119
120         for ( i = 0; i < maxloop; i++ ) {
121                 LDAPMessage *res;
122                 int         rc;
123
124                 if (( rc = ldap_search_s( ld, entry, LDAP_SCOPE_BASE,
125                                 NULL, attrs, 1, &res )) != LDAP_SUCCESS ) {
126
127                         ldap_perror( ld, "ldap_read" );
128                         if ( rc != LDAP_NO_SUCH_OBJECT ) break;
129                         continue;
130
131                 }
132
133                 ldap_msgfree( res );
134         }
135
136         fprintf( stderr, " PID=%ld - Read done.\n", (long) pid );
137
138         ldap_unbind( ld );
139 }
140
141