20 fprintf( stderr, "usage: %s [options] filter [attributes...]\nwhere:\n", s );
21 fprintf( stderr, " filter\tRFC-1558 compliant LDAP search filter\n" );
22 fprintf( stderr, " attributes\twhitespace-separated list of attributes to retrieve\n" );
23 fprintf( stderr, "\t\t(if no attribute list is given, all are retrieved)\n" );
24 fprintf( stderr, "options:\n" );
25 fprintf( stderr, " -n\t\tshow what would be done but don't actually search\n" );
26 fprintf( stderr, " -v\t\trun in verbose mode (diagnostics to standard output)\n" );
27 fprintf( stderr, " -t\t\twrite values to files in /tmp\n" );
28 fprintf( stderr, " -u\t\tinclude User Friendly entry names in the output\n" );
29 fprintf( stderr, " -A\t\tretrieve attribute names only (no values)\n" );
30 fprintf( stderr, " -B\t\tdo not suppress printing of non-ASCII values\n" );
31 fprintf( stderr, " -L\t\tprint entries in LDIF format (-B is implied)\n" );
32 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
33 fprintf( stderr, " -R\t\tdo not automatically follow referrals\n" );
34 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
35 fprintf( stderr, " -d level\tset LDAP debugging level to `level'\n" );
36 fprintf( stderr, " -F sep\tprint `sep' instead of `=' between attribute names and values\n" );
37 fprintf( stderr, " -S attr\tsort the results by attribute `attr'\n" );
38 fprintf( stderr, " -f file\tperform sequence of searches listed in `file'\n" );
39 fprintf( stderr, " -b basedn\tbase dn for search\n" );
40 fprintf( stderr, " -s scope\tone of base, one, or sub (search scope)\n" );
41 fprintf( stderr, " -a deref\tone of never, always, search, or find (alias dereferencing)\n" );
42 fprintf( stderr, " -l time lim\ttime limit (in seconds) for search\n" );
43 fprintf( stderr, " -z size lim\tsize limit (in entries) for search\n" );
44 fprintf( stderr, " -D binddn\tbind dn\n" );
45 fprintf( stderr, " -w passwd\tbind passwd (for simple authentication)\n" );
46 fprintf( stderr, " -W\t\tprompt for bind passwd\n" );
48 fprintf( stderr, " -k\t\tuse Kerberos instead of Simple Password authentication\n" );
50 fprintf( stderr, " -h host\tldap server\n" );
51 fprintf( stderr, " -p port\tport on ldap server\n" );
52 fprintf( stderr, " -P version\tprocotol version (2 or 3)\n" );
56 static void print_entry LDAP_P((
61 static int write_ldif_value LDAP_P((
64 unsigned long vallen ));
66 static int dosearch LDAP_P((
75 static char *binddn = NULL;
76 static char *passwd = NULL;
77 static char *base = NULL;
78 static char *ldaphost = NULL;
79 static int ldapport = 0;
80 static char *sep = DEFSEP;
81 static char *sortattr = NULL;
82 static int skipsortattr = 0;
83 static int verbose, not, includeufn, allow_binary, vals2tmp, ldif;
86 main( int argc, char **argv )
88 char *infile, *filtpattern, **attrs, line[ BUFSIZ ];
90 int rc, i, first, scope, deref, attrsonly;
91 int referrals, timelimit, sizelimit, debug;
92 int authmethod, version, want_bindpw;
96 debug = verbose = allow_binary = not = vals2tmp =
97 attrsonly = ldif = want_bindpw = 0;
99 deref = referrals = sizelimit = timelimit =
100 scope = version = -1;
102 authmethod = LDAP_AUTH_SIMPLE;
104 while (( i = getopt( argc, argv, "WKknuvtRABLD:s:f:h:b:d:P:p:F:a:w:l:z:S:")) != EOF ) {
106 case 'n': /* do Not do any searches */
109 case 'v': /* verbose mode */
113 debug |= atoi( optarg );
115 case 'k': /* use kerberos bind */
117 authmethod = LDAP_AUTH_KRBV4;
119 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
122 case 'K': /* use kerberos bind, 1st part only */
124 authmethod = LDAP_AUTH_KRBV41;
126 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
129 case 'u': /* include UFN */
132 case 't': /* write attribute values to /tmp files */
135 case 'R': /* don't automatically chase referrals */
136 referrals = (int) LDAP_OPT_OFF;
138 case 'A': /* retrieve attribute names only -- no values */
141 case 'L': /* print entries in LDIF format */
143 /* fall through -- always allow binary when outputting LDIF */
144 case 'B': /* allow binary values to be printed */
147 case 's': /* search scope */
148 if ( strncasecmp( optarg, "base", 4 ) == 0 ) {
149 scope = LDAP_SCOPE_BASE;
150 } else if ( strncasecmp( optarg, "one", 3 ) == 0 ) {
151 scope = LDAP_SCOPE_ONELEVEL;
152 } else if ( strncasecmp( optarg, "sub", 3 ) == 0 ) {
153 scope = LDAP_SCOPE_SUBTREE;
155 fprintf( stderr, "scope should be base, one, or sub\n" );
160 case 'a': /* set alias deref option */
161 if ( strncasecmp( optarg, "never", 5 ) == 0 ) {
162 deref = LDAP_DEREF_NEVER;
163 } else if ( strncasecmp( optarg, "search", 5 ) == 0 ) {
164 deref = LDAP_DEREF_SEARCHING;
165 } else if ( strncasecmp( optarg, "find", 4 ) == 0 ) {
166 deref = LDAP_DEREF_FINDING;
167 } else if ( strncasecmp( optarg, "always", 6 ) == 0 ) {
168 deref = LDAP_DEREF_ALWAYS;
170 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
175 case 'F': /* field separator */
176 sep = strdup( optarg );
178 case 'f': /* input file */
179 infile = strdup( optarg );
181 case 'h': /* ldap host */
182 ldaphost = strdup( optarg );
184 case 'b': /* searchbase */
185 base = strdup( optarg );
187 case 'D': /* bind DN */
188 binddn = strdup( optarg );
190 case 'p': /* ldap port */
191 ldapport = atoi( optarg );
193 case 'w': /* bind password */
194 passwd = strdup( optarg );
196 case 'l': /* time limit */
197 timelimit = atoi( optarg );
199 case 'z': /* size limit */
200 sizelimit = atoi( optarg );
202 case 'S': /* sort attribute */
203 sortattr = strdup( optarg );
212 version = LDAP_VERSION2;
215 version = LDAP_VERSION3;
224 if ( argc - optind < 1 ) {
227 filtpattern = strdup( argv[ optind ] );
228 if ( argv[ optind + 1 ] == NULL ) {
230 } else if ( sortattr == NULL || *sortattr == '\0' ) {
231 attrs = &argv[ optind + 1 ];
233 for ( i = optind + 1; i < argc; i++ ) {
234 if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
240 argv[ optind ] = sortattr;
244 attrs = &argv[ optind ];
247 if ( infile != NULL ) {
248 if ( infile[0] == '-' && infile[1] == '\0' ) {
250 } else if (( fp = fopen( infile, "r" )) == NULL ) {
257 printf( "ldap_open( %s, %d )\n", ldaphost, ldapport );
261 lber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug );
262 ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug );
266 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
272 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) == -1 )
274 /* set option error */
276 if (timelimit != -1 &&
277 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) == -1 )
279 /* set option error */
281 if (sizelimit != -1 &&
282 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) == -1 )
284 /* set option error */
286 if (referrals != -1 &&
287 ldap_set_option( ld, LDAP_OPT_REFERRALS, (void *) referrals ) == -1 )
289 /* set option error */
293 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) == -1)
295 /* set option error */
299 passwd = getpass("Enter LDAP Password: ");
301 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
302 ldap_perror( ld, "ldap_bind" );
307 printf( "filter pattern: %s\nreturning: ", filtpattern );
308 if ( attrs == NULL ) {
311 for ( i = 0; attrs[ i ] != NULL; ++i ) {
312 printf( "%s ", attrs[ i ] );
318 if ( infile == NULL ) {
319 rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern, "" );
323 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
324 line[ strlen( line ) - 1 ] = '\0';
330 rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern,
355 char filter[ BUFSIZ ];
356 int rc, first, matches;
357 LDAPMessage *res, *e;
359 sprintf( filter, filtpatt, value );
362 printf( "filter is: (%s)\n", filter );
366 return( LDAP_SUCCESS );
369 if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
371 ldap_perror( ld, "ldap_search" );
373 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
379 while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
380 == LDAP_RES_SEARCH_ENTRY ) {
382 e = ldap_first_entry( ld, res );
388 print_entry( ld, e, attrsonly );
392 ldap_perror( ld, "ldap_result" );
395 if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
396 ldap_perror( ld, "ldap_search" );
398 if ( sortattr != NULL ) {
399 (void) ldap_sort_entries( ld, &res,
400 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
403 for ( e = ldap_first_entry( ld, res ); e != NULLMSG;
404 e = ldap_next_entry( ld, e ) ) {
411 print_entry( ld, e, attrsonly );
416 printf( "%d matches\n", matches );
429 char *a, *dn, *ufn, tmpfname[ 64 ];
432 struct berval **bvals;
435 dn = ldap_get_dn( ld, entry );
437 write_ldif_value( "dn", dn, strlen( dn ));
439 printf( "%s\n", dn );
442 ufn = ldap_dn2ufn( dn );
444 write_ldif_value( "ufn", ufn, strlen( ufn ));
446 printf( "%s\n", ufn );
452 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
453 a = ldap_next_attribute( ld, entry, ber ) ) {
454 if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
459 write_ldif_value( a, "", 0 );
463 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
464 for ( i = 0; bvals[i] != NULL; i++ ) {
466 sprintf( tmpfname, "/tmp/ldapsearch-%s-XXXXXX", a );
469 if ( mktemp( tmpfname ) == NULL ) {
471 } else if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
473 } else if ( fwrite( bvals[ i ]->bv_val,
474 bvals[ i ]->bv_len, 1, tmpfp ) == 0 ) {
477 write_ldif_value( a, tmpfname, strlen( tmpfname ));
479 printf( "%s%s%s\n", a, sep, tmpfname );
482 if ( tmpfp != NULL ) {
487 if ( !allow_binary ) {
488 for ( j = 0; (unsigned long) j < bvals[ i ]->bv_len; ++j ) {
489 if ( !isascii( bvals[ i ]->bv_val[ j ] )) {
497 write_ldif_value( a, bvals[ i ]->bv_val,
498 bvals[ i ]->bv_len );
500 printf( "%s%s%s\n", a, sep,
501 notascii ? "NOT ASCII" : bvals[ i ]->bv_val );
505 ber_bvecfree( bvals );
512 write_ldif_value( char *type, char *value, unsigned long vallen )
516 if (( ldif = ldif_type_and_value( type, value, (int)vallen )) == NULL ) {
520 fputs( ldif, stdout );