]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-read.c
Notices and Acknowledgements
[openldap] / tests / progs / slapd-read.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2003 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 #include <ldap.h>
34
35 #define LOOPS   100
36
37 static void
38 do_read( char *uri, char *host, int port, char *entry, int maxloop );
39
40 static void
41 usage( char *name )
42 {
43         fprintf( stderr, "usage: %s [-h <host>] -p port -e <entry> [-l <loops>]\n",
44                         name );
45         exit( EXIT_FAILURE );
46 }
47
48 int
49 main( int argc, char **argv )
50 {
51         int             i;
52         char            *uri = NULL;
53         char        *host = "localhost";
54         int                     port = -1;
55         char            *entry = NULL;
56         int                     loops = LOOPS;
57
58         while ( (i = getopt( argc, argv, "H:h:p:e:l:" )) != EOF ) {
59                 switch( i ) {
60                         case 'H':               /* the server uri */
61                                 uri = strdup( optarg );
62                         break;
63                         case 'h':               /* the servers host */
64                                 host = strdup( optarg );
65                         break;
66
67                         case 'p':               /* the servers port */
68                                 port = atoi( optarg );
69                                 break;
70
71                         case 'e':               /* file with entry search request */
72                                 entry = strdup( optarg );
73                                 break;
74
75                         case 'l':               /* the number of loops */
76                                 loops = atoi( optarg );
77                                 break;
78
79                         default:
80                                 usage( argv[0] );
81                                 break;
82                 }
83         }
84
85         if (( entry == NULL ) || ( port == -1 && uri == NULL ))
86                 usage( argv[0] );
87
88         if ( *entry == '\0' ) {
89
90                 fprintf( stderr, "%s: invalid EMPTY entry DN.\n",
91                                 argv[0] );
92                 exit( EXIT_FAILURE );
93
94         }
95
96         do_read( uri, host, port, entry, ( 20 * loops ));
97         exit( EXIT_SUCCESS );
98 }
99
100
101 static void
102 do_read( char *uri, char *host, int port, char *entry, int maxloop )
103 {
104         LDAP    *ld = NULL;
105         int     i;
106         char    *attrs[] = { "1.1", NULL };
107         pid_t   pid = getpid();
108
109         if ( uri ) {
110                 ldap_initialize( &ld, uri );
111         } else {
112                 ld = ldap_init( host, port );
113         }
114         if ( ld == NULL ) {
115                 perror( "ldap_init" );
116                 exit( EXIT_FAILURE );
117         }
118
119         {
120                 int version = LDAP_VERSION3;
121                 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
122                         &version ); 
123         }
124
125         if ( ldap_bind_s( ld, NULL, NULL, LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
126                 ldap_perror( ld, "ldap_bind" );
127                  exit( EXIT_FAILURE );
128         }
129
130
131         fprintf( stderr, "PID=%ld - Read(%d): entry=\"%s\".\n",
132                  (long) pid, maxloop, entry );
133
134         for ( i = 0; i < maxloop; i++ ) {
135                 LDAPMessage *res;
136                 int         rc;
137
138                 if (( rc = ldap_search_s( ld, entry, LDAP_SCOPE_BASE,
139                                 NULL, attrs, 1, &res )) != LDAP_SUCCESS ) {
140
141                         ldap_perror( ld, "ldap_read" );
142                         if ( rc != LDAP_NO_SUCH_OBJECT ) break;
143                         continue;
144
145                 }
146
147                 ldap_msgfree( res );
148         }
149
150         fprintf( stderr, " PID=%ld - Read done.\n", (long) pid );
151
152         ldap_unbind( ld );
153 }
154
155