]> git.sur5r.net Git - openldap/blob - tests/progs/slapd-search.c
Add LDAP_DEPRECATED macro
[openldap] / tests / progs / slapd-search.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2003 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_search( char *uri, char *host, int port, char *sbase, char *filter, int maxloop );
40
41 static void
42 usage( char *name )
43 {
44         fprintf( stderr, "usage: %s [-h <host>] -p port -b <searchbase> -f <searchfiter> [-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        *sbase = NULL;
57         char            *filter  = NULL;
58         int                     loops = LOOPS;
59
60         while ( (i = getopt( argc, argv, "H:h:p:b:f:l:" )) != EOF ) {
61                 switch( i ) {
62                         case 'H':               /* the server uri */
63                                 uri = strdup( optarg );
64                         break;
65                         case 'h':               /* the servers host */
66                                 host = strdup( optarg );
67                         break;
68
69                         case 'p':               /* the servers port */
70                                 port = atoi( optarg );
71                                 break;
72
73                         case 'b':               /* file with search base */
74                                 sbase = strdup( optarg );
75                         break;
76
77                         case 'f':               /* the search request */
78                                 filter = strdup( optarg );
79                                 break;
80
81                         case 'l':               /* number of loops */
82                                 loops = atoi( optarg );
83                                 break;
84
85                         default:
86                                 usage( argv[0] );
87                                 break;
88                 }
89         }
90
91         if (( sbase == NULL ) || ( filter == NULL ) || ( port == -1 && uri == NULL ))
92                 usage( argv[0] );
93
94         if ( *filter == '\0' ) {
95
96                 fprintf( stderr, "%s: invalid EMPTY search filter.\n",
97                                 argv[0] );
98                 exit( EXIT_FAILURE );
99
100         }
101
102         do_search( uri, host, port, sbase, filter, ( 10 * loops ));
103         exit( EXIT_SUCCESS );
104 }
105
106
107 static void
108 do_search( char *uri, char *host, int port, char *sbase, char *filter, int maxloop )
109 {
110         LDAP    *ld = NULL;
111         int     i;
112         char    *attrs[] = { "cn", "sn", NULL };
113         pid_t   pid = getpid();
114
115         if ( uri ) {
116                 ldap_initialize( &ld, uri );
117         } else {
118                 ld = ldap_init( host, port );
119         }
120         if ( ld == NULL ) {
121                 perror( "ldap_init" );
122                 exit( EXIT_FAILURE );
123         }
124
125         {
126                 int version = LDAP_VERSION3;
127                 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
128                         &version ); 
129         }
130
131         if ( ldap_bind_s( ld, NULL, NULL, LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
132                 ldap_perror( ld, "ldap_bind" );
133                  exit( EXIT_FAILURE );
134         }
135
136
137         fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\".\n",
138                                 (long) pid, maxloop, sbase, filter );
139
140         for ( i = 0; i < maxloop; i++ ) {
141                 LDAPMessage *res;
142                 int         rc;
143
144                 if (( rc = ldap_search_s( ld, sbase, LDAP_SCOPE_SUBTREE,
145                                 filter, attrs, 0, &res )) != LDAP_SUCCESS ) {
146
147                         ldap_perror( ld, "ldap_search" );
148                         if ( rc != LDAP_NO_SUCH_OBJECT ) break;
149                         continue;
150
151                 }
152
153                 ldap_msgfree( res );
154         }
155
156         fprintf( stderr, " PID=%ld - Search done.\n", (long) pid );
157
158         ldap_unbind( ld );
159 }
160
161