]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-search.c
Add OpenLDAP RCS id and copyright
[openldap] / tests / progs / slapd-search.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_search( char *host, int port, char *sbase, char *filter, int maxloop );
28
29 static void
30 usage( char *name )
31 {
32         fprintf( stderr, "usage: %s [-h <host>] -p port -b <searchbase> -f <searchfiter> [-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        *sbase = NULL;
44         char            *filter  = NULL;
45         int                     loops = LOOPS;
46
47         while ( (i = getopt( argc, argv, "h:p:b:f:l:" )) != EOF ) {
48                 switch( i ) {
49                         case 'h':               /* the servers host */
50                                 host = strdup( optarg );
51                         break;
52
53                         case 'p':               /* the servers port */
54                                 port = atoi( optarg );
55                                 break;
56
57                         case 'b':               /* file with search base */
58                                 sbase = strdup( optarg );
59                         break;
60
61                         case 'f':               /* the search request */
62                                 filter = strdup( optarg );
63                                 break;
64
65                         case 'l':               /* number of loops */
66                                 loops = atoi( optarg );
67                                 break;
68
69                         default:
70                                 usage( argv[0] );
71                                 break;
72                 }
73         }
74
75         if (( sbase == NULL ) || ( filter == NULL ) || ( port == -1 ))
76                 usage( argv[0] );
77
78         if ( *filter == '\0' ) {
79
80                 fprintf( stderr, "%s: invalid EMPTY search filter.\n",
81                                 argv[0] );
82                 exit( EXIT_FAILURE );
83
84         }
85
86         do_search( host, port, sbase, filter, ( 4 * loops ));
87
88         exit( EXIT_SUCCESS );
89 }
90
91
92 static void
93 do_search( char *host, int port, char *sbase, char *filter, int maxloop )
94 {
95         LDAP    *ld;
96         int     i;
97         char    *attrs[] = { "cn", "sn", NULL };
98         pid_t   pid = getpid();
99
100         if (( ld = ldap_init( host, port )) == NULL ) {
101                 perror( "ldap_init" );
102                 exit( EXIT_FAILURE );
103         }
104
105         if ( ldap_bind_s( ld, NULL, NULL, LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
106                 ldap_perror( ld, "ldap_bind" );
107                  exit( EXIT_FAILURE );
108         }
109
110
111         fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\".\n",
112                                 (long) pid, maxloop, sbase, filter );
113
114         for ( i = 0; i < maxloop; i++ ) {
115                 LDAPMessage *res;
116                 int         rc;
117
118                 if (( rc = ldap_search_s( ld, sbase, LDAP_SCOPE_SUBTREE,
119                                 filter, attrs, 0, &res )) != LDAP_SUCCESS ) {
120
121                         ldap_perror( ld, "ldap_search" );
122                         if ( rc != LDAP_NO_SUCH_OBJECT ) break;
123                         continue;
124
125                 }
126
127                 ldap_msgfree( res );
128         }
129
130         fprintf( stderr, " PID=%ld - Search done.\n", (long) pid );
131
132         ldap_unbind( ld );
133 }
134
135