]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-read.c
Happy new year
[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 *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        *host = "localhost";
39         int                     port = -1;
40         char            *entry = NULL;
41         int                     loops = LOOPS;
42
43         while ( (i = getopt( argc, argv, "h:p:e:l:" )) != EOF ) {
44                 switch( i ) {
45                         case 'h':               /* the servers host */
46                                 host = strdup( optarg );
47                         break;
48
49                         case 'p':               /* the servers port */
50                                 port = atoi( optarg );
51                                 break;
52
53                         case 'e':               /* file with entry search request */
54                                 entry = strdup( optarg );
55                                 break;
56
57                         case 'l':               /* the number of loops */
58                                 loops = atoi( optarg );
59                                 break;
60
61                         default:
62                                 usage( argv[0] );
63                                 break;
64                 }
65         }
66
67         if (( entry == NULL ) || ( port == -1 ))
68                 usage( argv[0] );
69
70         if ( *entry == '\0' ) {
71
72                 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
73                                 argv[0] );
74                 exit( EXIT_FAILURE );
75
76         }
77
78         do_read( host, port, entry, ( 20 * loops ));
79         exit( EXIT_SUCCESS );
80 }
81
82
83 static void
84 do_read( char *host, int port, char *entry, int maxloop )
85 {
86         LDAP    *ld;
87         int     i;
88         char    *attrs[] = { "1.1", NULL };
89         pid_t   pid = getpid();
90
91         if (( ld = ldap_init( host, port )) == NULL ) {
92                 perror( "ldap_init" );
93                 exit( EXIT_FAILURE );
94         }
95
96         {
97                 int version = LDAP_VERSION3;
98                 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
99                         &version ); 
100         }
101
102         if ( ldap_bind_s( ld, NULL, NULL, LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
103                 ldap_perror( ld, "ldap_bind" );
104                  exit( EXIT_FAILURE );
105         }
106
107
108         fprintf( stderr, "PID=%ld - Read(%d): entry=\"%s\".\n",
109                  (long) pid, maxloop, entry );
110
111         for ( i = 0; i < maxloop; i++ ) {
112                 LDAPMessage *res;
113                 int         rc;
114
115                 if (( rc = ldap_search_s( ld, entry, LDAP_SCOPE_BASE,
116                                 NULL, attrs, 1, &res )) != LDAP_SUCCESS ) {
117
118                         ldap_perror( ld, "ldap_read" );
119                         if ( rc != LDAP_NO_SUCH_OBJECT ) break;
120                         continue;
121
122                 }
123
124                 ldap_msgfree( res );
125         }
126
127         fprintf( stderr, " PID=%ld - Read done.\n", (long) pid );
128
129         ldap_unbind( ld );
130 }
131
132