]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-read.c
Merge in all -devel changes made since branch was created.
[openldap] / tests / progs / slapd-read.c
1 #include "portable.h"
2
3 #include <stdio.h>
4
5 #include <ac/stdlib.h>
6
7 #include <ac/ctype.h>
8 #include <ac/socket.h>
9 #include <ac/string.h>
10 #include <ac/unistd.h>
11 #include <ac/wait.h>
12
13 #ifdef HAVE_SYS_PARAM_H
14 #include <sys/param.h>
15 #endif
16
17 #include <ldap.h>
18
19 #define LOOPS   100
20
21 static void
22 do_read( char *host, int port, char *entry, int maxloop );
23
24 static void
25 usage( char *name )
26 {
27         fprintf( stderr, "usage: %s [-h <host>] -p port -e <entry> [-l <loops>]\n",
28                         name );
29         exit( EXIT_FAILURE );
30 }
31
32 int
33 main( int argc, char **argv )
34 {
35         int             i;
36         char        *host = "localhost";
37         int                     port = -1;
38         char            *entry = NULL;
39         int                     loops = LOOPS;
40
41         while ( (i = getopt( argc, argv, "h:p:e:l:" )) != EOF ) {
42                 switch( i ) {
43                         case 'h':               /* the servers host */
44                                 host = strdup( optarg );
45                         break;
46
47                         case 'p':               /* the servers port */
48                                 port = atoi( optarg );
49                                 break;
50
51                         case 'e':               /* file with entry search request */
52                                 entry = strdup( optarg );
53                                 break;
54
55                         case 'l':               /* the number of loops */
56                                 loops = atoi( optarg );
57                                 break;
58
59                         default:
60                                 usage( argv[0] );
61                                 break;
62                 }
63         }
64
65         if (( entry == NULL ) || ( port == -1 ))
66                 usage( argv[0] );
67
68         if ( *entry == '\0' ) {
69
70                 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
71                                 argv[0] );
72                 exit( EXIT_FAILURE );
73
74         }
75
76         do_read( host, port, entry, ( 4 * loops ));
77
78         exit( EXIT_SUCCESS );
79 }
80
81
82 static void
83 do_read( char *host, int port, char *entry, int maxloop )
84 {
85         LDAP    *ld;
86         int     i;
87         char    *attrs[] = { "cn", "sn", NULL };
88         char    *filter = "(objectclass=*)";
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         if ( ldap_bind_s( ld, NULL, NULL, LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
97                 ldap_perror( ld, "ldap_bind" );
98                  exit( EXIT_FAILURE );
99         }
100
101
102         fprintf( stderr, "PID=%ld - Read(%d): entry=\"%s\".\n",
103                  (long) pid, maxloop, entry );
104
105         for ( i = 0; i < maxloop; i++ ) {
106                 LDAPMessage *res;
107                 int         rc;
108
109                 if (( rc = ldap_search_s( ld, entry, LDAP_SCOPE_BASE,
110                                 filter, attrs, 0, &res )) != LDAP_SUCCESS ) {
111
112                         ldap_perror( ld, "ldap_read" );
113                         if ( rc != LDAP_NO_SUCH_OBJECT ) break;
114                         continue;
115
116                 }
117
118                 ldap_msgfree( res );
119         }
120
121         fprintf( stderr, " PID=%ld - Read done.\n", (long) pid );
122
123         ldap_unbind( ld );
124 }
125
126