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