14 #if LDAP_VERSION == LDAP_VERSION2
15 /* until we implement ldap_set_options() */
16 #include "../libraries/libldap/ldap-int.h"
19 #include "ldapconfig.h"
24 extern int ldap_debug, lber_debug;
25 #endif /* LDAP_DEBUG */
28 static void usage( s )
31 fprintf( stderr, "usage: %s [options] filter [attributes...]\nwhere:\n", s );
32 fprintf( stderr, " filter\tRFC-1558 compliant LDAP search filter\n" );
33 fprintf( stderr, " attributes\twhitespace-separated list of attributes to retrieve\n" );
34 fprintf( stderr, "\t\t(if no attribute list is given, all are retrieved)\n" );
35 fprintf( stderr, "options:\n" );
36 fprintf( stderr, " -n\t\tshow what would be done but don't actually search\n" );
37 fprintf( stderr, " -v\t\trun in verbose mode (diagnostics to standard output)\n" );
38 fprintf( stderr, " -t\t\twrite values to files in /tmp\n" );
39 fprintf( stderr, " -u\t\tinclude User Friendly entry names in the output\n" );
40 fprintf( stderr, " -A\t\tretrieve attribute names only (no values)\n" );
41 fprintf( stderr, " -B\t\tdo not suppress printing of non-ASCII values\n" );
42 fprintf( stderr, " -L\t\tprint entries in LDIF format (-B is implied)\n" );
44 fprintf( stderr, " -R\t\tdo not automatically follow referrals\n" );
45 #endif /* LDAP_REFERRALS */
46 fprintf( stderr, " -d level\tset LDAP debugging level to `level'\n" );
47 fprintf( stderr, " -F sep\tprint `sep' instead of `=' between attribute names and values\n" );
48 fprintf( stderr, " -S attr\tsort the results by attribute `attr'\n" );
49 fprintf( stderr, " -f file\tperform sequence of searches listed in `file'\n" );
50 fprintf( stderr, " -b basedn\tbase dn for search\n" );
51 fprintf( stderr, " -s scope\tone of base, one, or sub (search scope)\n" );
52 fprintf( stderr, " -a deref\tone of never, always, search, or find (alias dereferencing)\n" );
53 fprintf( stderr, " -l time lim\ttime limit (in seconds) for search\n" );
54 fprintf( stderr, " -z size lim\tsize limit (in entries) for search\n" );
55 fprintf( stderr, " -D binddn\tbind dn\n" );
56 fprintf( stderr, " -w passwd\tbind passwd (for simple authentication)\n" );
58 fprintf( stderr, " -k\t\tuse Kerberos instead of Simple Password authentication\n" );
60 fprintf( stderr, " -h host\tldap server\n" );
61 fprintf( stderr, " -p port\tport on ldap server\n" );
65 static void print_entry LDAP_P((
70 static int write_ldif_value LDAP_P((
73 unsigned long vallen ));
75 static int dosearch LDAP_P((
84 static char *binddn = LDAPSEARCH_BINDDN;
85 static char *passwd = LDAPSEARCH_BIND_CRED;
86 static char *base = LDAPSEARCH_BASE;
87 static char *ldaphost = LDAPHOST;
88 static int ldapport = LDAP_PORT;
89 static char *sep = DEFSEP;
90 static char *sortattr = NULL;
91 static int skipsortattr = 0;
92 static int verbose, not, includeufn, allow_binary, vals2tmp, ldif;
98 char *infile, *filtpattern, **attrs, line[ BUFSIZ ];
100 int rc, i, first, scope, kerberos, deref, attrsonly;
101 int ldap_options, timelimit, sizelimit, authmethod;
107 deref = verbose = allow_binary = not = kerberos = vals2tmp =
108 attrsonly = ldif = 0;
109 #ifdef LDAP_REFERRALS
110 ldap_options = LDAP_OPT_REFERRALS;
111 #else /* LDAP_REFERRALS */
113 #endif /* LDAP_REFERRALS */
114 sizelimit = timelimit = 0;
115 scope = LDAP_SCOPE_SUBTREE;
117 while (( i = getopt( argc, argv,
119 "KknuvtRABLD:s:f:h:b:d:p:F:a:w:l:z:S:"
121 "nuvtRABLD:s:f:h:b:d:p:F:a:w:l:z:S:"
125 case 'n': /* do Not do any searches */
128 case 'v': /* verbose mode */
133 ldap_debug = lber_debug = atoi( optarg ); /* */
134 #else /* LDAP_DEBUG */
135 fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
136 #endif /* LDAP_DEBUG */
139 case 'k': /* use kerberos bind */
142 case 'K': /* use kerberos bind, 1st part only */
146 case 'u': /* include UFN */
149 case 't': /* write attribute values to /tmp files */
152 case 'R': /* don't automatically chase referrals */
153 #ifdef LDAP_REFERRALS
154 ldap_options &= ~LDAP_OPT_REFERRALS;
155 #else /* LDAP_REFERRALS */
157 "compile with -DLDAP_REFERRALS for referral support\n" );
158 #endif /* LDAP_REFERRALS */
160 case 'A': /* retrieve attribute names only -- no values */
163 case 'L': /* print entries in LDIF format */
165 /* fall through -- always allow binary when outputting LDIF */
166 case 'B': /* allow binary values to be printed */
169 case 's': /* search scope */
170 if ( strncasecmp( optarg, "base", 4 ) == 0 ) {
171 scope = LDAP_SCOPE_BASE;
172 } else if ( strncasecmp( optarg, "one", 3 ) == 0 ) {
173 scope = LDAP_SCOPE_ONELEVEL;
174 } else if ( strncasecmp( optarg, "sub", 3 ) == 0 ) {
175 scope = LDAP_SCOPE_SUBTREE;
177 fprintf( stderr, "scope should be base, one, or sub\n" );
182 case 'a': /* set alias deref option */
183 if ( strncasecmp( optarg, "never", 5 ) == 0 ) {
184 deref = LDAP_DEREF_NEVER;
185 } else if ( strncasecmp( optarg, "search", 5 ) == 0 ) {
186 deref = LDAP_DEREF_SEARCHING;
187 } else if ( strncasecmp( optarg, "find", 4 ) == 0 ) {
188 deref = LDAP_DEREF_FINDING;
189 } else if ( strncasecmp( optarg, "always", 6 ) == 0 ) {
190 deref = LDAP_DEREF_ALWAYS;
192 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
197 case 'F': /* field separator */
198 sep = strdup( optarg );
200 case 'f': /* input file */
201 infile = strdup( optarg );
203 case 'h': /* ldap host */
204 ldaphost = strdup( optarg );
206 case 'b': /* searchbase */
207 base = strdup( optarg );
209 case 'D': /* bind DN */
210 binddn = strdup( optarg );
212 case 'p': /* ldap port */
213 ldapport = atoi( optarg );
215 case 'w': /* bind password */
216 passwd = strdup( optarg );
218 case 'l': /* time limit */
219 timelimit = atoi( optarg );
221 case 'z': /* size limit */
222 sizelimit = atoi( optarg );
224 case 'S': /* sort attribute */
225 sortattr = strdup( optarg );
232 if ( argc - optind < 1 ) {
235 filtpattern = strdup( argv[ optind ] );
236 if ( argv[ optind + 1 ] == NULL ) {
238 } else if ( sortattr == NULL || *sortattr == '\0' ) {
239 attrs = &argv[ optind + 1 ];
241 for ( i = optind + 1; i < argc; i++ ) {
242 if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
248 argv[ optind ] = sortattr;
252 attrs = &argv[ optind ];
255 if ( infile != NULL ) {
256 if ( infile[0] == '-' && infile[1] == '\0' ) {
258 } else if (( fp = fopen( infile, "r" )) == NULL ) {
265 printf( "ldap_open( %s, %d )\n", ldaphost, ldapport );
268 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
273 ld->ld_deref = deref;
274 ld->ld_timelimit = timelimit;
275 ld->ld_sizelimit = sizelimit;
276 ld->ld_options = ldap_options;
279 authmethod = LDAP_AUTH_SIMPLE;
280 } else if ( kerberos == 1 ) {
281 authmethod = LDAP_AUTH_KRBV41;
283 authmethod = LDAP_AUTH_KRBV4;
285 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
286 ldap_perror( ld, "ldap_bind" );
291 printf( "filter pattern: %s\nreturning: ", filtpattern );
292 if ( attrs == NULL ) {
295 for ( i = 0; attrs[ i ] != NULL; ++i ) {
296 printf( "%s ", attrs[ i ] );
302 if ( infile == NULL ) {
303 rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern, "" );
307 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
308 line[ strlen( line ) - 1 ] = '\0';
314 rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern,
336 char filter[ BUFSIZ ];
337 int rc, first, matches;
338 LDAPMessage *res, *e;
340 sprintf( filter, filtpatt, value );
343 printf( "filter is: (%s)\n", filter );
347 return( LDAP_SUCCESS );
350 if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
351 ldap_perror( ld, "ldap_search" );
352 return( ld->ld_errno );
357 while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
358 == LDAP_RES_SEARCH_ENTRY ) {
360 e = ldap_first_entry( ld, res );
366 print_entry( ld, e, attrsonly );
370 ldap_perror( ld, "ldap_result" );
373 if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
374 ldap_perror( ld, "ldap_search" );
376 if ( sortattr != NULL ) {
377 extern int strcasecmp();
379 (void) ldap_sort_entries( ld, &res,
380 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
383 for ( e = ldap_first_entry( ld, res ); e != NULLMSG;
384 e = ldap_next_entry( ld, e ) ) {
391 print_entry( ld, e, attrsonly );
396 printf( "%d matches\n", matches );
409 char *a, *dn, *ufn, tmpfname[ 64 ];
412 struct berval **bvals;
414 extern char *mktemp();
416 dn = ldap_get_dn( ld, entry );
418 write_ldif_value( "dn", dn, strlen( dn ));
420 printf( "%s\n", dn );
423 ufn = ldap_dn2ufn( dn );
425 write_ldif_value( "ufn", ufn, strlen( ufn ));
427 printf( "%s\n", ufn );
433 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
434 a = ldap_next_attribute( ld, entry, ber ) ) {
435 if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
440 write_ldif_value( a, "", 0 );
444 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
445 for ( i = 0; bvals[i] != NULL; i++ ) {
447 sprintf( tmpfname, "/tmp/ldapsearch-%s-XXXXXX", a );
450 if ( mktemp( tmpfname ) == NULL ) {
452 } else if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
454 } else if ( fwrite( bvals[ i ]->bv_val,
455 bvals[ i ]->bv_len, 1, tmpfp ) == 0 ) {
458 write_ldif_value( a, tmpfname, strlen( tmpfname ));
460 printf( "%s%s%s\n", a, sep, tmpfname );
463 if ( tmpfp != NULL ) {
468 if ( !allow_binary ) {
469 for ( j = 0; (unsigned long) j < bvals[ i ]->bv_len; ++j ) {
470 if ( !isascii( bvals[ i ]->bv_val[ j ] )) {
478 write_ldif_value( a, bvals[ i ]->bv_val,
479 bvals[ i ]->bv_len );
481 printf( "%s%s%s\n", a, sep,
482 notascii ? "NOT ASCII" : bvals[ i ]->bv_val );
486 ber_bvecfree( bvals );
493 write_ldif_value( char *type, char *value, unsigned long vallen )
497 if (( ldif = ldif_type_and_value( type, value, (int)vallen )) == NULL ) {
501 fputs( ldif, stdout );