17 extern int ldap_debug, lber_debug;
18 #endif /* LDAP_DEBUG */
21 static void usage( s )
24 fprintf( stderr, "usage: %s [options] filter [attributes...]\nwhere:\n", s );
25 fprintf( stderr, " filter\tRFC-1558 compliant LDAP search filter\n" );
26 fprintf( stderr, " attributes\twhitespace-separated list of attributes to retrieve\n" );
27 fprintf( stderr, "\t\t(if no attribute list is given, all are retrieved)\n" );
28 fprintf( stderr, "options:\n" );
29 fprintf( stderr, " -n\t\tshow what would be done but don't actually search\n" );
30 fprintf( stderr, " -v\t\trun in verbose mode (diagnostics to standard output)\n" );
31 fprintf( stderr, " -t\t\twrite values to files in /tmp\n" );
32 fprintf( stderr, " -u\t\tinclude User Friendly entry names in the output\n" );
33 fprintf( stderr, " -A\t\tretrieve attribute names only (no values)\n" );
34 fprintf( stderr, " -B\t\tdo not suppress printing of non-ASCII values\n" );
35 fprintf( stderr, " -L\t\tprint entries in LDIF format (-B is implied)\n" );
36 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
37 fprintf( stderr, " -R\t\tdo not automatically follow referrals\n" );
38 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
39 fprintf( stderr, " -d level\tset LDAP debugging level to `level'\n" );
40 fprintf( stderr, " -F sep\tprint `sep' instead of `=' between attribute names and values\n" );
41 fprintf( stderr, " -S attr\tsort the results by attribute `attr'\n" );
42 fprintf( stderr, " -f file\tperform sequence of searches listed in `file'\n" );
43 fprintf( stderr, " -b basedn\tbase dn for search\n" );
44 fprintf( stderr, " -s scope\tone of base, one, or sub (search scope)\n" );
45 fprintf( stderr, " -a deref\tone of never, always, search, or find (alias dereferencing)\n" );
46 fprintf( stderr, " -l time lim\ttime limit (in seconds) for search\n" );
47 fprintf( stderr, " -z size lim\tsize limit (in entries) for search\n" );
48 fprintf( stderr, " -D binddn\tbind dn\n" );
49 fprintf( stderr, " -w passwd\tbind passwd (for simple authentication)\n" );
51 fprintf( stderr, " -k\t\tuse Kerberos instead of Simple Password authentication\n" );
53 fprintf( stderr, " -h host\tldap server\n" );
54 fprintf( stderr, " -p port\tport on ldap server\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;
92 char *infile, *filtpattern, **attrs, line[ BUFSIZ ];
94 int rc, i, first, scope, kerberos, deref, attrsonly;
95 int referrals, timelimit, sizelimit, authmethod;
101 deref = verbose = allow_binary = not = kerberos = vals2tmp =
102 attrsonly = ldif = 0;
103 referrals = (int) LDAP_OPT_ON;
104 sizelimit = timelimit = 0;
105 scope = LDAP_SCOPE_SUBTREE;
107 while (( i = getopt( argc, argv,
109 "KknuvtRABLD:s:f:h:b:d:p:F:a:w:l:z:S:"
111 "nuvtRABLD:s:f:h:b:d:p:F:a:w:l:z:S:"
115 case 'n': /* do Not do any searches */
118 case 'v': /* verbose mode */
123 ldap_debug = lber_debug = atoi( optarg ); /* */
124 #else /* LDAP_DEBUG */
125 fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
126 #endif /* LDAP_DEBUG */
129 case 'k': /* use kerberos bind */
132 case 'K': /* use kerberos bind, 1st part only */
136 case 'u': /* include UFN */
139 case 't': /* write attribute values to /tmp files */
142 case 'R': /* don't automatically chase referrals */
143 referrals = (int) LDAP_OPT_OFF;
145 case 'A': /* retrieve attribute names only -- no values */
148 case 'L': /* print entries in LDIF format */
150 /* fall through -- always allow binary when outputting LDIF */
151 case 'B': /* allow binary values to be printed */
154 case 's': /* search scope */
155 if ( strncasecmp( optarg, "base", 4 ) == 0 ) {
156 scope = LDAP_SCOPE_BASE;
157 } else if ( strncasecmp( optarg, "one", 3 ) == 0 ) {
158 scope = LDAP_SCOPE_ONELEVEL;
159 } else if ( strncasecmp( optarg, "sub", 3 ) == 0 ) {
160 scope = LDAP_SCOPE_SUBTREE;
162 fprintf( stderr, "scope should be base, one, or sub\n" );
167 case 'a': /* set alias deref option */
168 if ( strncasecmp( optarg, "never", 5 ) == 0 ) {
169 deref = LDAP_DEREF_NEVER;
170 } else if ( strncasecmp( optarg, "search", 5 ) == 0 ) {
171 deref = LDAP_DEREF_SEARCHING;
172 } else if ( strncasecmp( optarg, "find", 4 ) == 0 ) {
173 deref = LDAP_DEREF_FINDING;
174 } else if ( strncasecmp( optarg, "always", 6 ) == 0 ) {
175 deref = LDAP_DEREF_ALWAYS;
177 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
182 case 'F': /* field separator */
183 sep = strdup( optarg );
185 case 'f': /* input file */
186 infile = strdup( optarg );
188 case 'h': /* ldap host */
189 ldaphost = strdup( optarg );
191 case 'b': /* searchbase */
192 base = strdup( optarg );
194 case 'D': /* bind DN */
195 binddn = strdup( optarg );
197 case 'p': /* ldap port */
198 ldapport = atoi( optarg );
200 case 'w': /* bind password */
201 passwd = strdup( optarg );
203 case 'l': /* time limit */
204 timelimit = atoi( optarg );
206 case 'z': /* size limit */
207 sizelimit = atoi( optarg );
209 case 'S': /* sort attribute */
210 sortattr = strdup( optarg );
217 if ( argc - optind < 1 ) {
220 filtpattern = strdup( argv[ optind ] );
221 if ( argv[ optind + 1 ] == NULL ) {
223 } else if ( sortattr == NULL || *sortattr == '\0' ) {
224 attrs = &argv[ optind + 1 ];
226 for ( i = optind + 1; i < argc; i++ ) {
227 if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
233 argv[ optind ] = sortattr;
237 attrs = &argv[ optind ];
240 if ( infile != NULL ) {
241 if ( infile[0] == '-' && infile[1] == '\0' ) {
243 } else if (( fp = fopen( infile, "r" )) == NULL ) {
250 printf( "ldap_open( %s, %d )\n", ldaphost, ldapport );
253 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
258 if (ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) == -1 ) {
259 /* set option error */
261 if (ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) == -1 ) {
262 /* set option error */
264 if (ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) == -1 ) {
265 /* set option error */
267 if (ldap_set_option( ld, LDAP_OPT_REFERRALS, (void *) referrals ) == -1 ) {
268 /* set option error */
272 authmethod = LDAP_AUTH_SIMPLE;
273 } else if ( kerberos == 1 ) {
274 authmethod = LDAP_AUTH_KRBV41;
276 authmethod = LDAP_AUTH_KRBV4;
278 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
279 ldap_perror( ld, "ldap_bind" );
284 printf( "filter pattern: %s\nreturning: ", filtpattern );
285 if ( attrs == NULL ) {
288 for ( i = 0; attrs[ i ] != NULL; ++i ) {
289 printf( "%s ", attrs[ i ] );
295 if ( infile == NULL ) {
296 rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern, "" );
300 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
301 line[ strlen( line ) - 1 ] = '\0';
307 rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern,
332 char filter[ BUFSIZ ];
333 int rc, first, matches;
334 LDAPMessage *res, *e;
336 sprintf( filter, filtpatt, value );
339 printf( "filter is: (%s)\n", filter );
343 return( LDAP_SUCCESS );
346 if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
348 ldap_perror( ld, "ldap_search" );
350 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
356 while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
357 == LDAP_RES_SEARCH_ENTRY ) {
359 e = ldap_first_entry( ld, res );
365 print_entry( ld, e, attrsonly );
369 ldap_perror( ld, "ldap_result" );
372 if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
373 ldap_perror( ld, "ldap_search" );
375 if ( sortattr != NULL ) {
376 extern int strcasecmp();
378 (void) ldap_sort_entries( ld, &res,
379 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
382 for ( e = ldap_first_entry( ld, res ); e != NULLMSG;
383 e = ldap_next_entry( ld, e ) ) {
390 print_entry( ld, e, attrsonly );
395 printf( "%d matches\n", matches );
408 char *a, *dn, *ufn, tmpfname[ 64 ];
411 struct berval **bvals;
413 extern char *mktemp();
415 dn = ldap_get_dn( ld, entry );
417 write_ldif_value( "dn", dn, strlen( dn ));
419 printf( "%s\n", dn );
422 ufn = ldap_dn2ufn( dn );
424 write_ldif_value( "ufn", ufn, strlen( ufn ));
426 printf( "%s\n", ufn );
432 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
433 a = ldap_next_attribute( ld, entry, ber ) ) {
434 if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
439 write_ldif_value( a, "", 0 );
443 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
444 for ( i = 0; bvals[i] != NULL; i++ ) {
446 sprintf( tmpfname, "/tmp/ldapsearch-%s-XXXXXX", a );
449 if ( mktemp( tmpfname ) == NULL ) {
451 } else if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
453 } else if ( fwrite( bvals[ i ]->bv_val,
454 bvals[ i ]->bv_len, 1, tmpfp ) == 0 ) {
457 write_ldif_value( a, tmpfname, strlen( tmpfname ));
459 printf( "%s%s%s\n", a, sep, tmpfname );
462 if ( tmpfp != NULL ) {
467 if ( !allow_binary ) {
468 for ( j = 0; (unsigned long) j < bvals[ i ]->bv_len; ++j ) {
469 if ( !isascii( bvals[ i ]->bv_val[ j ] )) {
477 write_ldif_value( a, bvals[ i ]->bv_val,
478 bvals[ i ]->bv_len );
480 printf( "%s%s%s\n", a, sep,
481 notascii ? "NOT ASCII" : bvals[ i ]->bv_val );
485 ber_bvecfree( bvals );
492 write_ldif_value( char *type, char *value, unsigned long vallen )
496 if (( ldif = ldif_type_and_value( type, value, (int)vallen )) == NULL ) {
500 fputs( ldif, stdout );