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