21 fprintf( stderr, "usage: %s [options] filter [attributes...]\nwhere:\n", s );
22 fprintf( stderr, " filter\tRFC-1558 compliant LDAP search filter\n" );
23 fprintf( stderr, " attributes\twhitespace-separated list of attributes to retrieve\n" );
24 fprintf( stderr, "\t\t(if no attribute list is given, all are retrieved)\n" );
25 fprintf( stderr, "options:\n" );
26 fprintf( stderr, " -n\t\tshow what would be done but don't actually search\n" );
27 fprintf( stderr, " -v\t\trun in verbose mode (diagnostics to standard output)\n" );
28 fprintf( stderr, " -t\t\twrite values to files in /tmp\n" );
29 fprintf( stderr, " -u\t\tinclude User Friendly entry names in the output\n" );
30 fprintf( stderr, " -A\t\tretrieve attribute names only (no values)\n" );
31 fprintf( stderr, " -B\t\tdo not suppress printing of non-ASCII values\n" );
32 fprintf( stderr, " -L\t\tprint entries in LDIF format (-B is implied)\n" );
33 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
34 fprintf( stderr, " -R\t\tdo not automatically follow referrals\n" );
35 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
36 fprintf( stderr, " -d level\tset LDAP debugging level to `level'\n" );
37 fprintf( stderr, " -F sep\tprint `sep' instead of `=' between attribute names and values\n" );
38 fprintf( stderr, " -S attr\tsort the results by attribute `attr'\n" );
39 fprintf( stderr, " -f file\tperform sequence of searches listed in `file'\n" );
40 fprintf( stderr, " -b basedn\tbase dn for search\n" );
41 fprintf( stderr, " -s scope\tone of base, one, or sub (search scope)\n" );
42 fprintf( stderr, " -a deref\tone of never, always, search, or find (alias dereferencing)\n" );
43 fprintf( stderr, " -l time lim\ttime limit (in seconds) for search\n" );
44 fprintf( stderr, " -z size lim\tsize limit (in entries) for search\n" );
45 fprintf( stderr, " -D binddn\tbind dn\n" );
46 fprintf( stderr, " -w passwd\tbind passwd (for simple authentication)\n" );
47 fprintf( stderr, " -W\t\tprompt for bind passwd\n" );
49 fprintf( stderr, " -k\t\tuse Kerberos instead of Simple Password authentication\n" );
51 fprintf( stderr, " -h host\tldap server\n" );
52 fprintf( stderr, " -p port\tport on ldap server\n" );
53 fprintf( stderr, " -P version\tprocotol version (2 or 3)\n" );
57 static void print_entry LDAP_P((
62 static int write_ldif_value LDAP_P((
65 unsigned long vallen ));
67 static int dosearch LDAP_P((
76 static char *binddn = NULL;
77 static char *passwd = NULL;
78 static char *base = NULL;
79 static char *ldaphost = NULL;
80 static int ldapport = 0;
81 static char *sep = DEFSEP;
82 static char *sortattr = NULL;
83 static int skipsortattr = 0;
84 static int verbose, not, includeufn, allow_binary, vals2tmp, ldif;
87 main( int argc, char **argv )
89 char *infile, *filtpattern, **attrs, line[ BUFSIZ ];
91 int rc, i, first, scope, deref, attrsonly;
92 int referrals, timelimit, sizelimit, debug;
93 int authmethod, version, want_bindpw;
97 debug = verbose = allow_binary = not = vals2tmp =
98 attrsonly = ldif = want_bindpw = 0;
100 deref = referrals = sizelimit = timelimit = version = -1;
102 scope = LDAP_SCOPE_SUBTREE;
103 authmethod = LDAP_AUTH_SIMPLE;
105 while (( i = getopt( argc, argv, "WKknuvtRABLD:s:f:h:b:d:P:p:F:a:w:l:z:S:")) != EOF ) {
107 case 'n': /* do Not do any searches */
110 case 'v': /* verbose mode */
114 debug |= atoi( optarg );
116 case 'k': /* use kerberos bind */
118 authmethod = LDAP_AUTH_KRBV4;
120 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
123 case 'K': /* use kerberos bind, 1st part only */
125 authmethod = LDAP_AUTH_KRBV41;
127 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
130 case 'u': /* include UFN */
133 case 't': /* write attribute values to /tmp files */
136 case 'R': /* don't automatically chase referrals */
137 referrals = (int) LDAP_OPT_OFF;
139 case 'A': /* retrieve attribute names only -- no values */
142 case 'L': /* print entries in LDIF format */
144 /* fall through -- always allow binary when outputting LDIF */
145 case 'B': /* allow binary values to be printed */
148 case 's': /* search scope */
149 if ( strcasecmp( optarg, "base" ) == 0 ) {
150 scope = LDAP_SCOPE_BASE;
151 } else if ( strcasecmp( optarg, "one" ) == 0 ) {
152 scope = LDAP_SCOPE_ONELEVEL;
153 } else if ( strcasecmp( optarg, "sub" ) == 0 ) {
154 scope = LDAP_SCOPE_SUBTREE;
156 fprintf( stderr, "scope should be base, one, or sub\n" );
161 case 'a': /* set alias deref option */
162 if ( strcasecmp( optarg, "never" ) == 0 ) {
163 deref = LDAP_DEREF_NEVER;
164 } else if ( strcasecmp( optarg, "search" ) == 0 ) {
165 deref = LDAP_DEREF_SEARCHING;
166 } else if ( strcasecmp( optarg, "find" ) == 0 ) {
167 deref = LDAP_DEREF_FINDING;
168 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
169 deref = LDAP_DEREF_ALWAYS;
171 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
176 case 'F': /* field separator */
177 sep = strdup( optarg );
179 case 'f': /* input file */
180 infile = strdup( optarg );
182 case 'h': /* ldap host */
183 ldaphost = strdup( optarg );
185 case 'b': /* searchbase */
186 base = strdup( optarg );
188 case 'D': /* bind DN */
189 binddn = strdup( optarg );
191 case 'p': /* ldap port */
192 ldapport = atoi( optarg );
194 case 'w': /* bind password */
195 passwd = strdup( optarg );
197 case 'l': /* time limit */
198 timelimit = atoi( optarg );
200 case 'z': /* size limit */
201 sizelimit = atoi( optarg );
203 case 'S': /* sort attribute */
204 sortattr = strdup( optarg );
210 switch( atoi( optarg ) )
213 version = LDAP_VERSION2;
216 version = LDAP_VERSION3;
219 fprintf( stderr, "protocol version should be 2 or 3\n" );
228 if ( argc - optind < 1 ) {
231 filtpattern = strdup( argv[ optind ] );
232 if ( argv[ optind + 1 ] == NULL ) {
234 } else if ( sortattr == NULL || *sortattr == '\0' ) {
235 attrs = &argv[ optind + 1 ];
237 for ( i = optind + 1; i < argc; i++ ) {
238 if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
244 argv[ optind ] = sortattr;
248 attrs = &argv[ optind ];
251 if ( infile != NULL ) {
252 if ( infile[0] == '-' && infile[1] == '\0' ) {
254 } else if (( fp = fopen( infile, "r" )) == NULL ) {
256 return( EXIT_FAILURE );
261 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_ERROR ) {
262 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
264 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_ERROR ) {
265 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
271 (void) SIGNAL( SIGPIPE, SIG_IGN );
275 fprintf( stderr, "ldap_init( %s, %d )\n",
276 (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
280 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
281 perror( "ldap_init" );
282 return( EXIT_FAILURE );
286 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) == LDAP_OPT_ERROR )
288 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
290 if (timelimit != -1 &&
291 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) == LDAP_OPT_ERROR )
293 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
295 if (sizelimit != -1 &&
296 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) == LDAP_OPT_ERROR )
298 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
300 if (referrals != -1 &&
301 ldap_set_option( ld, LDAP_OPT_REFERRALS,
302 (referrals ? LDAP_OPT_ON : LDAP_OPT_OFF) ) == LDAP_OPT_ERROR )
304 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
305 referrals ? "on" : "off" );
309 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) == LDAP_OPT_ERROR)
311 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
315 passwd = getpass("Enter LDAP Password: ");
317 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
318 ldap_perror( ld, "ldap_bind" );
319 return( EXIT_FAILURE );
323 fprintf( stderr, "filter pattern: %s\nreturning: ", filtpattern );
324 if ( attrs == NULL ) {
327 for ( i = 0; attrs[ i ] != NULL; ++i ) {
328 fprintf( stderr, "%s ", attrs[ i ] );
331 fprintf( stderr, "\n" );
334 if ( infile == NULL ) {
335 rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern, "" );
339 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
340 line[ strlen( line ) - 1 ] = '\0';
346 rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern,
370 char filter[ BUFSIZ ];
371 int rc, first, matches;
372 LDAPMessage *res, *e;
374 sprintf( filter, filtpatt, value );
377 fprintf( stderr, "filter is: (%s)\n", filter );
381 return( LDAP_SUCCESS );
384 if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
386 ldap_perror( ld, "ldap_search" );
388 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
395 while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
396 == LDAP_RES_SEARCH_ENTRY ) {
398 e = ldap_first_entry( ld, res );
404 print_entry( ld, e, attrsonly );
409 ldap_perror( ld, "ldap_result" );
412 if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
413 ldap_perror( ld, "ldap_search" );
415 if ( sortattr != NULL ) {
416 (void) ldap_sort_entries( ld, &res,
417 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
420 for ( e = ldap_first_entry( ld, res ); e != NULL;
421 e = ldap_next_entry( ld, e ) ) {
428 print_entry( ld, e, attrsonly );
433 printf( "%d matches\n", matches );
446 char *a, *dn, *ufn, tmpfname[ 64 ];
448 BerElement *ber = NULL;
449 struct berval **bvals;
452 dn = ldap_get_dn( ld, entry );
454 write_ldif_value( "dn", dn, strlen( dn ));
456 printf( "%s\n", dn );
459 ufn = ldap_dn2ufn( dn );
461 write_ldif_value( "ufn", ufn, strlen( ufn ));
463 printf( "%s\n", ufn );
469 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
470 a = ldap_next_attribute( ld, entry, ber ) ) {
471 if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
476 write_ldif_value( a, "", 0 );
480 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
481 for ( i = 0; bvals[i] != NULL; i++ ) {
483 sprintf( tmpfname, "/tmp/ldapsearch-%s-XXXXXX", a );
486 if ( mktemp( tmpfname ) == NULL ) {
488 } else if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
490 } else if ( fwrite( bvals[ i ]->bv_val,
491 bvals[ i ]->bv_len, 1, tmpfp ) == 0 ) {
494 write_ldif_value( a, tmpfname, strlen( tmpfname ));
496 printf( "%s%s%s\n", a, sep, tmpfname );
499 if ( tmpfp != NULL ) {
504 if ( !allow_binary ) {
505 for ( j = 0; (unsigned long) j < bvals[ i ]->bv_len; ++j ) {
506 if ( !isascii( bvals[ i ]->bv_val[ j ] )) {
514 write_ldif_value( a, bvals[ i ]->bv_val,
515 bvals[ i ]->bv_len );
517 printf( "%s%s%s\n", a, sep,
518 notascii ? "NOT ASCII" : bvals[ i ]->bv_val );
522 ber_bvecfree( bvals );
533 write_ldif_value( char *type, char *value, unsigned long vallen )
537 if (( ldif = ldif_type_and_value( type, value, (int)vallen )) == NULL ) {
541 fputs( ldif, stdout );