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 );
200 for( p = optarg; *p == NULL; p++ ) {
205 case 'l': /* time limit */
206 timelimit = atoi( optarg );
208 case 'z': /* size limit */
209 sizelimit = atoi( optarg );
211 case 'S': /* sort attribute */
212 sortattr = strdup( optarg );
218 switch( atoi( optarg ) )
221 version = LDAP_VERSION2;
224 version = LDAP_VERSION3;
227 fprintf( stderr, "protocol version should be 2 or 3\n" );
236 if ( argc - optind < 1 ) {
239 filtpattern = strdup( argv[ optind ] );
240 if ( argv[ optind + 1 ] == NULL ) {
242 } else if ( sortattr == NULL || *sortattr == '\0' ) {
243 attrs = &argv[ optind + 1 ];
245 for ( i = optind + 1; i < argc; i++ ) {
246 if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
252 argv[ optind ] = sortattr;
256 attrs = &argv[ optind ];
259 if ( infile != NULL ) {
260 if ( infile[0] == '-' && infile[1] == '\0' ) {
262 } else if (( fp = fopen( infile, "r" )) == NULL ) {
264 return( EXIT_FAILURE );
269 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
270 fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
272 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
273 fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
279 (void) SIGNAL( SIGPIPE, SIG_IGN );
283 fprintf( stderr, "ldap_init( %s, %d )\n",
284 (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
288 if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
289 perror( "ldap_init" );
290 return( EXIT_FAILURE );
294 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
296 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
298 if (timelimit != -1 &&
299 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
301 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
303 if (sizelimit != -1 &&
304 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
306 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
308 if (referrals != -1 &&
309 ldap_set_option( ld, LDAP_OPT_REFERRALS,
310 (referrals ? LDAP_OPT_ON : LDAP_OPT_OFF) ) != LDAP_OPT_SUCCESS )
312 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
313 referrals ? "on" : "off" );
317 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS )
319 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
323 passwd = getpass("Enter LDAP Password: ");
325 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
326 ldap_perror( ld, "ldap_bind" );
327 return( EXIT_FAILURE );
331 fprintf( stderr, "filter pattern: %s\nreturning: ", filtpattern );
332 if ( attrs == NULL ) {
335 for ( i = 0; attrs[ i ] != NULL; ++i ) {
336 fprintf( stderr, "%s ", attrs[ i ] );
339 fprintf( stderr, "\n" );
342 if ( infile == NULL ) {
343 rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern, "" );
347 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
348 line[ strlen( line ) - 1 ] = '\0';
354 rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern,
378 char filter[ BUFSIZ ];
379 int rc, first, matches;
380 LDAPMessage *res, *e;
382 sprintf( filter, filtpatt, value );
385 fprintf( stderr, "filter is: (%s)\n", filter );
389 return( LDAP_SUCCESS );
392 if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
394 ldap_perror( ld, "ldap_search" );
396 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
403 while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
404 == LDAP_RES_SEARCH_ENTRY ) {
406 e = ldap_first_entry( ld, res );
412 print_entry( ld, e, attrsonly );
417 ldap_perror( ld, "ldap_result" );
420 if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
421 ldap_perror( ld, "ldap_search" );
423 if ( sortattr != NULL ) {
424 (void) ldap_sort_entries( ld, &res,
425 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
428 for ( e = ldap_first_entry( ld, res ); e != NULL;
429 e = ldap_next_entry( ld, e ) ) {
436 print_entry( ld, e, attrsonly );
441 printf( "%d matches\n", matches );
454 char *a, *dn, *ufn, tmpfname[ 64 ];
456 BerElement *ber = NULL;
457 struct berval **bvals;
460 dn = ldap_get_dn( ld, entry );
462 write_ldif_value( "dn", dn, strlen( dn ));
464 printf( "%s\n", dn );
467 ufn = ldap_dn2ufn( dn );
469 write_ldif_value( "ufn", ufn, strlen( ufn ));
471 printf( "%s\n", ufn );
477 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
478 a = ldap_next_attribute( ld, entry, ber ) ) {
479 if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
484 write_ldif_value( a, "", 0 );
488 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
489 for ( i = 0; bvals[i] != NULL; i++ ) {
491 sprintf( tmpfname, "/tmp/ldapsearch-%s-XXXXXX", a );
494 if ( mktemp( tmpfname ) == NULL ) {
496 } else if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
498 } else if ( fwrite( bvals[ i ]->bv_val,
499 bvals[ i ]->bv_len, 1, tmpfp ) == 0 ) {
502 write_ldif_value( a, tmpfname, strlen( tmpfname ));
504 printf( "%s%s%s\n", a, sep, tmpfname );
507 if ( tmpfp != NULL ) {
512 if ( !allow_binary ) {
513 for ( j = 0; (unsigned long) j < bvals[ i ]->bv_len; ++j ) {
514 if ( !isascii( bvals[ i ]->bv_val[ j ] )) {
522 write_ldif_value( a, bvals[ i ]->bv_val,
523 bvals[ i ]->bv_len );
525 printf( "%s%s%s\n", a, sep,
526 notascii ? "NOT ASCII" : bvals[ i ]->bv_val );
530 ber_bvecfree( bvals );
541 write_ldif_value( char *type, char *value, unsigned long vallen )
545 if (( ldif = ldif_type_and_value( type, value, (int)vallen )) == NULL ) {
549 fputs( ldif, stdout );