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