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