2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2005 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
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>.
16 * This work was initially developed by Kurt Spanier for inclusion
17 * in OpenLDAP Software.
24 #include <ac/stdlib.h>
28 #include <ac/socket.h>
29 #include <ac/string.h>
30 #include <ac/unistd.h>
33 #define LDAP_DEPRECATED 1
39 do_search( char *uri, char *host, int port, char *sbase, char *filter, int maxloop );
44 fprintf( stderr, "usage: %s [-h <host>] -p port -b <searchbase> -f <searchfiter> [-l <loops>]\n",
50 main( int argc, char **argv )
54 char *host = "localhost";
60 while ( (i = getopt( argc, argv, "H:h:p:b:f:l:" )) != EOF ) {
62 case 'H': /* the server uri */
63 uri = strdup( optarg );
65 case 'h': /* the servers host */
66 host = strdup( optarg );
69 case 'p': /* the servers port */
70 port = atoi( optarg );
73 case 'b': /* file with search base */
74 sbase = strdup( optarg );
77 case 'f': /* the search request */
78 filter = strdup( optarg );
81 case 'l': /* number of loops */
82 loops = atoi( optarg );
91 if (( sbase == NULL ) || ( filter == NULL ) || ( port == -1 && uri == NULL ))
94 if ( *filter == '\0' ) {
96 fprintf( stderr, "%s: invalid EMPTY search filter.\n",
102 do_search( uri, host, port, sbase, filter, ( 10 * loops ));
103 exit( EXIT_SUCCESS );
108 do_search( char *uri, char *host, int port, char *sbase, char *filter, int maxloop )
112 char *attrs[] = { "cn", "sn", NULL };
113 pid_t pid = getpid();
116 ldap_initialize( &ld, uri );
118 ld = ldap_init( host, port );
121 perror( "ldap_init" );
122 exit( EXIT_FAILURE );
126 int version = LDAP_VERSION3;
127 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
131 if ( ldap_bind_s( ld, NULL, NULL, LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
132 ldap_perror( ld, "ldap_bind" );
133 exit( EXIT_FAILURE );
137 fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\".\n",
138 (long) pid, maxloop, sbase, filter );
140 for ( i = 0; i < maxloop; i++ ) {
144 if (( rc = ldap_search_s( ld, sbase, LDAP_SCOPE_SUBTREE,
145 filter, attrs, 0, &res )) != LDAP_SUCCESS ) {
147 ldap_perror( ld, "ldap_search" );
148 if ( rc != LDAP_NO_SUCH_OBJECT ) break;
156 fprintf( stderr, " PID=%ld - Search done.\n", (long) pid );