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