14 #include "ldapconfig.h"
19 extern int ldap_debug, lber_debug;
20 #endif /* LDAP_DEBUG */
23 static void usage( s )
26 fprintf( stderr, "usage: %s [options] filter [attributes...]\nwhere:\n", s );
27 fprintf( stderr, " filter\tRFC-1558 compliant LDAP search filter\n" );
28 fprintf( stderr, " attributes\twhitespace-separated list of attributes to retrieve\n" );
29 fprintf( stderr, "\t\t(if no attribute list is given, all are retrieved)\n" );
30 fprintf( stderr, "options:\n" );
31 fprintf( stderr, " -n\t\tshow what would be done but don't actually search\n" );
32 fprintf( stderr, " -v\t\trun in verbose mode (diagnostics to standard output)\n" );
33 fprintf( stderr, " -t\t\twrite values to files in /tmp\n" );
34 fprintf( stderr, " -u\t\tinclude User Friendly entry names in the output\n" );
35 fprintf( stderr, " -A\t\tretrieve attribute names only (no values)\n" );
36 fprintf( stderr, " -B\t\tdo not suppress printing of non-ASCII values\n" );
37 fprintf( stderr, " -L\t\tprint entries in LDIF format (-B is implied)\n" );
38 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
39 fprintf( stderr, " -R\t\tdo not automatically follow referrals\n" );
40 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
41 fprintf( stderr, " -d level\tset LDAP debugging level to `level'\n" );
42 fprintf( stderr, " -F sep\tprint `sep' instead of `=' between attribute names and values\n" );
43 fprintf( stderr, " -S attr\tsort the results by attribute `attr'\n" );
44 fprintf( stderr, " -f file\tperform sequence of searches listed in `file'\n" );
45 fprintf( stderr, " -b basedn\tbase dn for search\n" );
46 fprintf( stderr, " -s scope\tone of base, one, or sub (search scope)\n" );
47 fprintf( stderr, " -a deref\tone of never, always, search, or find (alias dereferencing)\n" );
48 fprintf( stderr, " -l time lim\ttime limit (in seconds) for search\n" );
49 fprintf( stderr, " -z size lim\tsize limit (in entries) for search\n" );
50 fprintf( stderr, " -D binddn\tbind dn\n" );
51 fprintf( stderr, " -w passwd\tbind passwd (for simple authentication)\n" );
53 fprintf( stderr, " -k\t\tuse Kerberos instead of Simple Password authentication\n" );
55 fprintf( stderr, " -h host\tldap server\n" );
56 fprintf( stderr, " -p port\tport on ldap server\n" );
60 static void print_entry LDAP_P((
65 static int write_ldif_value LDAP_P((
68 unsigned long vallen ));
70 static int dosearch LDAP_P((
79 static char *binddn = LDAPSEARCH_BINDDN;
80 static char *passwd = LDAPSEARCH_BIND_CRED;
81 static char *base = LDAPSEARCH_BASE;
82 static char *ldaphost = LDAPHOST;
83 static int ldapport = LDAP_PORT;
84 static char *sep = DEFSEP;
85 static char *sortattr = NULL;
86 static int skipsortattr = 0;
87 static int verbose, not, includeufn, allow_binary, vals2tmp, ldif;
94 char *infile, *filtpattern, **attrs, line[ BUFSIZ ];
96 int rc, i, first, scope, kerberos, deref, attrsonly;
97 int referrals, timelimit, sizelimit, authmethod;
103 deref = verbose = allow_binary = not = kerberos = vals2tmp =
104 attrsonly = ldif = 0;
105 referrals = (int) LDAP_OPT_ON;
106 sizelimit = timelimit = 0;
107 scope = LDAP_SCOPE_SUBTREE;
109 while (( i = getopt( argc, argv,
111 "KknuvtRABLD:s:f:h:b:d:p:F:a:w:l:z:S:"
113 "nuvtRABLD:s:f:h:b:d:p:F:a:w:l:z:S:"
117 case 'n': /* do Not do any searches */
120 case 'v': /* verbose mode */
125 ldap_debug = lber_debug = atoi( optarg ); /* */
126 #else /* LDAP_DEBUG */
127 fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
128 #endif /* LDAP_DEBUG */
131 case 'k': /* use kerberos bind */
134 case 'K': /* use kerberos bind, 1st part only */
138 case 'u': /* include UFN */
141 case 't': /* write attribute values to /tmp files */
144 case 'R': /* don't automatically chase referrals */
145 referrals = (int) LDAP_OPT_OFF;
147 case 'A': /* retrieve attribute names only -- no values */
150 case 'L': /* print entries in LDIF format */
152 /* fall through -- always allow binary when outputting LDIF */
153 case 'B': /* allow binary values to be printed */
156 case 's': /* search scope */
157 if ( strncasecmp( optarg, "base", 4 ) == 0 ) {
158 scope = LDAP_SCOPE_BASE;
159 } else if ( strncasecmp( optarg, "one", 3 ) == 0 ) {
160 scope = LDAP_SCOPE_ONELEVEL;
161 } else if ( strncasecmp( optarg, "sub", 3 ) == 0 ) {
162 scope = LDAP_SCOPE_SUBTREE;
164 fprintf( stderr, "scope should be base, one, or sub\n" );
169 case 'a': /* set alias deref option */
170 if ( strncasecmp( optarg, "never", 5 ) == 0 ) {
171 deref = LDAP_DEREF_NEVER;
172 } else if ( strncasecmp( optarg, "search", 5 ) == 0 ) {
173 deref = LDAP_DEREF_SEARCHING;
174 } else if ( strncasecmp( optarg, "find", 4 ) == 0 ) {
175 deref = LDAP_DEREF_FINDING;
176 } else if ( strncasecmp( optarg, "always", 6 ) == 0 ) {
177 deref = LDAP_DEREF_ALWAYS;
179 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
184 case 'F': /* field separator */
185 sep = strdup( optarg );
187 case 'f': /* input file */
188 infile = strdup( optarg );
190 case 'h': /* ldap host */
191 ldaphost = strdup( optarg );
193 case 'b': /* searchbase */
194 base = strdup( optarg );
196 case 'D': /* bind DN */
197 binddn = strdup( optarg );
199 case 'p': /* ldap port */
200 ldapport = atoi( optarg );
202 case 'w': /* bind password */
203 passwd = strdup( optarg );
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 );
219 if ( argc - optind < 1 ) {
222 filtpattern = strdup( argv[ optind ] );
223 if ( argv[ optind + 1 ] == NULL ) {
225 } else if ( sortattr == NULL || *sortattr == '\0' ) {
226 attrs = &argv[ optind + 1 ];
228 for ( i = optind + 1; i < argc; i++ ) {
229 if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
235 argv[ optind ] = sortattr;
239 attrs = &argv[ optind ];
242 if ( infile != NULL ) {
243 if ( infile[0] == '-' && infile[1] == '\0' ) {
245 } else if (( fp = fopen( infile, "r" )) == NULL ) {
252 printf( "ldap_open( %s, %d )\n", ldaphost, ldapport );
255 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
260 if (ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) == -1 ) {
261 /* set option error */
263 if (ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) == -1 ) {
264 /* set option error */
266 if (ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) == -1 ) {
267 /* set option error */
269 if (ldap_set_option( ld, LDAP_OPT_REFERRALS, (void *) referrals ) == -1 ) {
270 /* set option error */
274 authmethod = LDAP_AUTH_SIMPLE;
275 } else if ( kerberos == 1 ) {
276 authmethod = LDAP_AUTH_KRBV41;
278 authmethod = LDAP_AUTH_KRBV4;
280 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
281 ldap_perror( ld, "ldap_bind" );
286 printf( "filter pattern: %s\nreturning: ", filtpattern );
287 if ( attrs == NULL ) {
290 for ( i = 0; attrs[ i ] != NULL; ++i ) {
291 printf( "%s ", attrs[ i ] );
297 if ( infile == NULL ) {
298 rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern, "" );
302 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
303 line[ strlen( line ) - 1 ] = '\0';
309 rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern,
334 char filter[ BUFSIZ ];
335 int rc, first, matches;
336 LDAPMessage *res, *e;
338 sprintf( filter, filtpatt, value );
341 printf( "filter is: (%s)\n", filter );
345 return( LDAP_SUCCESS );
348 if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
350 ldap_perror( ld, "ldap_search" );
352 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
358 while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
359 == LDAP_RES_SEARCH_ENTRY ) {
361 e = ldap_first_entry( ld, res );
367 print_entry( ld, e, attrsonly );
371 ldap_perror( ld, "ldap_result" );
374 if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
375 ldap_perror( ld, "ldap_search" );
377 if ( sortattr != NULL ) {
378 extern int strcasecmp();
380 (void) ldap_sort_entries( ld, &res,
381 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
384 for ( e = ldap_first_entry( ld, res ); e != NULLMSG;
385 e = ldap_next_entry( ld, e ) ) {
392 print_entry( ld, e, attrsonly );
397 printf( "%d matches\n", matches );
410 char *a, *dn, *ufn, tmpfname[ 64 ];
413 struct berval **bvals;
415 extern char *mktemp();
417 dn = ldap_get_dn( ld, entry );
419 write_ldif_value( "dn", dn, strlen( dn ));
421 printf( "%s\n", dn );
424 ufn = ldap_dn2ufn( dn );
426 write_ldif_value( "ufn", ufn, strlen( ufn ));
428 printf( "%s\n", ufn );
434 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
435 a = ldap_next_attribute( ld, entry, ber ) ) {
436 if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
441 write_ldif_value( a, "", 0 );
445 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
446 for ( i = 0; bvals[i] != NULL; i++ ) {
448 sprintf( tmpfname, "/tmp/ldapsearch-%s-XXXXXX", a );
451 if ( mktemp( tmpfname ) == NULL ) {
453 } else if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
455 } else if ( fwrite( bvals[ i ]->bv_val,
456 bvals[ i ]->bv_len, 1, tmpfp ) == 0 ) {
459 write_ldif_value( a, tmpfname, strlen( tmpfname ));
461 printf( "%s%s%s\n", a, sep, tmpfname );
464 if ( tmpfp != NULL ) {
469 if ( !allow_binary ) {
470 for ( j = 0; (unsigned long) j < bvals[ i ]->bv_len; ++j ) {
471 if ( !isascii( bvals[ i ]->bv_val[ j ] )) {
479 write_ldif_value( a, bvals[ i ]->bv_val,
480 bvals[ i ]->bv_len );
482 printf( "%s%s%s\n", a, sep,
483 notascii ? "NOT ASCII" : bvals[ i ]->bv_val );
487 ber_bvecfree( bvals );
494 write_ldif_value( char *type, char *value, unsigned long vallen )
498 if (( ldif = ldif_type_and_value( type, value, (int)vallen )) == NULL ) {
502 fputs( ldif, stdout );