10 #include <ac/unistd.h>
22 fprintf( stderr, "usage: %s [options] filter [attributes...]\nwhere:\n", s );
23 fprintf( stderr, " filter\tRFC-1558 compliant LDAP search filter\n" );
24 fprintf( stderr, " attributes\twhitespace-separated list of attributes to retrieve\n" );
25 fprintf( stderr, "\t\t(if no attribute list is given, all are retrieved)\n" );
26 fprintf( stderr, "options:\n" );
27 fprintf( stderr, " -n\t\tshow what would be done but don't actually search\n" );
28 fprintf( stderr, " -v\t\trun in verbose mode (diagnostics to standard output)\n" );
29 fprintf( stderr, " -t\t\twrite values to files in /tmp\n" );
30 fprintf( stderr, " -u\t\tinclude User Friendly entry names in the output\n" );
31 fprintf( stderr, " -A\t\tretrieve attribute names only (no values)\n" );
32 fprintf( stderr, " -B\t\tdo not suppress printing of non-ASCII values\n" );
33 fprintf( stderr, " -L\t\tprint entries in LDIF format (-B is implied)\n" );
34 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
35 fprintf( stderr, " -R\t\tdo not automatically follow referrals\n" );
36 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
37 fprintf( stderr, " -d level\tset LDAP debugging level to `level'\n" );
38 fprintf( stderr, " -F sep\tprint `sep' instead of `=' between attribute names and values\n" );
39 fprintf( stderr, " -S attr\tsort the results by attribute `attr'\n" );
40 fprintf( stderr, " -f file\tperform sequence of searches listed in `file'\n" );
41 fprintf( stderr, " -b basedn\tbase dn for search\n" );
42 fprintf( stderr, " -s scope\tone of base, one, or sub (search scope)\n" );
43 fprintf( stderr, " -a deref\tone of never, always, search, or find (alias dereferencing)\n" );
44 fprintf( stderr, " -l time lim\ttime limit (in seconds) for search\n" );
45 fprintf( stderr, " -z size lim\tsize limit (in entries) for search\n" );
46 fprintf( stderr, " -D binddn\tbind dn\n" );
47 fprintf( stderr, " -w passwd\tbind passwd (for simple authentication)\n" );
48 fprintf( stderr, " -W\t\tprompt for bind passwd\n" );
50 fprintf( stderr, " -k\t\tuse Kerberos instead of Simple Password authentication\n" );
52 fprintf( stderr, " -h host\tldap server\n" );
53 fprintf( stderr, " -p port\tport on ldap server\n" );
54 fprintf( stderr, " -P version\tprocotol version (2 or 3)\n" );
58 static void print_entry LDAP_P((
63 static int write_ldif_value LDAP_P((
66 unsigned long vallen ));
68 static int dosearch LDAP_P((
77 static char *binddn = NULL;
78 static char *passwd = NULL;
79 static char *base = NULL;
80 static char *ldaphost = NULL;
81 static int ldapport = 0;
82 static char *sep = DEFSEP;
83 static char *sortattr = NULL;
84 static int skipsortattr = 0;
85 static int verbose, not, includeufn, allow_binary, vals2tmp, ldif;
88 main( int argc, char **argv )
90 char *infile, *filtpattern, **attrs, line[ BUFSIZ ];
92 int rc, i, first, scope, deref, attrsonly;
93 int referrals, timelimit, sizelimit, debug;
94 int authmethod, version, want_bindpw;
98 debug = verbose = allow_binary = not = vals2tmp =
99 attrsonly = ldif = want_bindpw = 0;
101 deref = referrals = sizelimit = timelimit = version = -1;
103 scope = LDAP_SCOPE_SUBTREE;
104 authmethod = LDAP_AUTH_SIMPLE;
106 while (( i = getopt( argc, argv, "WKknuvtRABLD:s:f:h:b:d:P:p:F:a:w:l:z:S:")) != EOF ) {
108 case 'n': /* do Not do any searches */
111 case 'v': /* verbose mode */
115 debug |= atoi( optarg );
117 case 'k': /* use kerberos bind */
119 authmethod = LDAP_AUTH_KRBV4;
121 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
124 case 'K': /* use kerberos bind, 1st part only */
126 authmethod = LDAP_AUTH_KRBV41;
128 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
131 case 'u': /* include UFN */
134 case 't': /* write attribute values to /tmp files */
137 case 'R': /* don't automatically chase referrals */
138 referrals = (int) LDAP_OPT_OFF;
140 case 'A': /* retrieve attribute names only -- no values */
143 case 'L': /* print entries in LDIF format */
145 /* fall through -- always allow binary when outputting LDIF */
146 case 'B': /* allow binary values to be printed */
149 case 's': /* search scope */
150 if ( strcasecmp( optarg, "base" ) == 0 ) {
151 scope = LDAP_SCOPE_BASE;
152 } else if ( strcasecmp( optarg, "one" ) == 0 ) {
153 scope = LDAP_SCOPE_ONELEVEL;
154 } else if ( strcasecmp( optarg, "sub" ) == 0 ) {
155 scope = LDAP_SCOPE_SUBTREE;
157 fprintf( stderr, "scope should be base, one, or sub\n" );
162 case 'a': /* set alias deref option */
163 if ( strcasecmp( optarg, "never" ) == 0 ) {
164 deref = LDAP_DEREF_NEVER;
165 } else if ( strcasecmp( optarg, "search" ) == 0 ) {
166 deref = LDAP_DEREF_SEARCHING;
167 } else if ( strcasecmp( optarg, "find" ) == 0 ) {
168 deref = LDAP_DEREF_FINDING;
169 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
170 deref = LDAP_DEREF_ALWAYS;
172 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
177 case 'F': /* field separator */
178 sep = strdup( optarg );
180 case 'f': /* input file */
181 infile = strdup( optarg );
183 case 'h': /* ldap host */
184 ldaphost = strdup( optarg );
186 case 'b': /* searchbase */
187 base = strdup( optarg );
189 case 'D': /* bind DN */
190 binddn = strdup( optarg );
192 case 'p': /* ldap port */
193 ldapport = atoi( optarg );
195 case 'w': /* bind password */
196 passwd = strdup( optarg );
198 case 'l': /* time limit */
199 timelimit = atoi( optarg );
201 case 'z': /* size limit */
202 sizelimit = atoi( optarg );
204 case 'S': /* sort attribute */
205 sortattr = strdup( optarg );
211 switch( atoi( optarg ) )
214 version = LDAP_VERSION2;
217 version = LDAP_VERSION3;
220 fprintf( stderr, "protocol version should be 2 or 3\n" );
229 if ( argc - optind < 1 ) {
232 filtpattern = strdup( argv[ optind ] );
233 if ( argv[ optind + 1 ] == NULL ) {
235 } else if ( sortattr == NULL || *sortattr == '\0' ) {
236 attrs = &argv[ optind + 1 ];
238 for ( i = optind + 1; i < argc; i++ ) {
239 if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
245 argv[ optind ] = sortattr;
249 attrs = &argv[ optind ];
252 if ( infile != NULL ) {
253 if ( infile[0] == '-' && infile[1] == '\0' ) {
255 } else if (( fp = fopen( infile, "r" )) == NULL ) {
257 return( EXIT_FAILURE );
262 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_ERROR ) {
263 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
265 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_ERROR ) {
266 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
272 (void) SIGNAL( SIGPIPE, SIG_IGN );
276 fprintf( stderr, "ldap_init( %s, %d )\n",
277 (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
281 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
282 perror( "ldap_init" );
283 return( EXIT_FAILURE );
287 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) == LDAP_OPT_ERROR )
289 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
291 if (timelimit != -1 &&
292 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) == LDAP_OPT_ERROR )
294 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
296 if (sizelimit != -1 &&
297 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) == LDAP_OPT_ERROR )
299 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
301 if (referrals != -1 &&
302 ldap_set_option( ld, LDAP_OPT_REFERRALS,
303 (referrals ? LDAP_OPT_ON : LDAP_OPT_OFF) ) == LDAP_OPT_ERROR )
305 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
306 referrals ? "on" : "off" );
310 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) == LDAP_OPT_ERROR)
312 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
316 passwd = getpass("Enter LDAP Password: ");
318 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
319 ldap_perror( ld, "ldap_bind" );
320 return( EXIT_FAILURE );
324 fprintf( stderr, "filter pattern: %s\nreturning: ", filtpattern );
325 if ( attrs == NULL ) {
328 for ( i = 0; attrs[ i ] != NULL; ++i ) {
329 fprintf( stderr, "%s ", attrs[ i ] );
332 fprintf( stderr, "\n" );
335 if ( infile == NULL ) {
336 rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern, "" );
340 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
341 line[ strlen( line ) - 1 ] = '\0';
347 rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern,
371 char filter[ BUFSIZ ];
372 int rc, first, matches;
373 LDAPMessage *res, *e;
375 sprintf( filter, filtpatt, value );
378 fprintf( stderr, "filter is: (%s)\n", filter );
382 return( LDAP_SUCCESS );
385 if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
387 ldap_perror( ld, "ldap_search" );
389 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
396 while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
397 == LDAP_RES_SEARCH_ENTRY ) {
399 e = ldap_first_entry( ld, res );
405 print_entry( ld, e, attrsonly );
410 ldap_perror( ld, "ldap_result" );
413 if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
414 ldap_perror( ld, "ldap_search" );
416 if ( sortattr != NULL ) {
417 (void) ldap_sort_entries( ld, &res,
418 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
421 for ( e = ldap_first_entry( ld, res ); e != NULL;
422 e = ldap_next_entry( ld, e ) ) {
429 print_entry( ld, e, attrsonly );
434 printf( "%d matches\n", matches );
447 char *a, *dn, *ufn, tmpfname[ 64 ];
449 BerElement *ber = NULL;
450 struct berval **bvals;
453 dn = ldap_get_dn( ld, entry );
455 write_ldif_value( "dn", dn, strlen( dn ));
457 printf( "%s\n", dn );
460 ufn = ldap_dn2ufn( dn );
462 write_ldif_value( "ufn", ufn, strlen( ufn ));
464 printf( "%s\n", ufn );
470 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
471 a = ldap_next_attribute( ld, entry, ber ) ) {
472 if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
477 write_ldif_value( a, "", 0 );
481 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
482 for ( i = 0; bvals[i] != NULL; i++ ) {
484 sprintf( tmpfname, "/tmp/ldapsearch-%s-XXXXXX", a );
487 if ( mktemp( tmpfname ) == NULL ) {
489 } else if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
491 } else if ( fwrite( bvals[ i ]->bv_val,
492 bvals[ i ]->bv_len, 1, tmpfp ) == 0 ) {
495 write_ldif_value( a, tmpfname, strlen( tmpfname ));
497 printf( "%s%s%s\n", a, sep, tmpfname );
500 if ( tmpfp != NULL ) {
505 if ( !allow_binary ) {
506 for ( j = 0; (unsigned long) j < bvals[ i ]->bv_len; ++j ) {
507 if ( !isascii( bvals[ i ]->bv_val[ j ] )) {
515 write_ldif_value( a, bvals[ i ]->bv_val,
516 bvals[ i ]->bv_len );
518 printf( "%s%s%s\n", a, sep,
519 notascii ? "NOT ASCII" : bvals[ i ]->bv_val );
523 ber_bvecfree( bvals );
534 write_ldif_value( char *type, char *value, unsigned long vallen )
538 if (( ldif = ldif_type_and_value( type, value, (int)vallen )) == NULL ) {
542 fputs( ldif, stdout );