10 #include "ldapconfig.h"
15 extern int ldap_debug, lber_debug;
16 #endif /* LDAP_DEBUG */
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" );
35 fprintf( stderr, " -R\t\tdo not automatically follow referrals\n" );
36 #endif /* LDAP_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" );
49 fprintf( stderr, " -k\t\tuse Kerberos instead of Simple Password authentication\n" );
51 fprintf( stderr, " -h host\tldap server\n" );
52 fprintf( stderr, " -p port\tport on ldap server\n" );
56 static char *binddn = LDAPSEARCH_BINDDN;
57 static char *passwd = LDAPSEARCH_BIND_CRED;
58 static char *base = LDAPSEARCH_BASE;
59 static char *ldaphost = LDAPHOST;
60 static int ldapport = LDAP_PORT;
61 static char *sep = DEFSEP;
62 static char *sortattr = NULL;
63 static int skipsortattr = 0;
64 static int verbose, not, includeufn, allow_binary, vals2tmp, ldif;
70 char *infile, *filtpattern, **attrs, line[ BUFSIZ ];
72 int rc, i, first, scope, kerberos, deref, attrsonly;
73 int ldap_options, timelimit, sizelimit, authmethod;
79 deref = verbose = allow_binary = not = kerberos = vals2tmp =
82 ldap_options = LDAP_OPT_REFERRALS;
83 #else /* LDAP_REFERRALS */
85 #endif /* LDAP_REFERRALS */
86 sizelimit = timelimit = 0;
87 scope = LDAP_SCOPE_SUBTREE;
89 while (( i = getopt( argc, argv,
91 "KknuvtRABLD:s:f:h:b:d:p:F:a:w:l:z:S:"
93 "nuvtRABLD:s:f:h:b:d:p:F:a:w:l:z:S:"
97 case 'n': /* do Not do any searches */
100 case 'v': /* verbose mode */
105 ldap_debug = lber_debug = atoi( optarg ); /* */
106 #else /* LDAP_DEBUG */
107 fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
108 #endif /* LDAP_DEBUG */
111 case 'k': /* use kerberos bind */
114 case 'K': /* use kerberos bind, 1st part only */
118 case 'u': /* include UFN */
121 case 't': /* write attribute values to /tmp files */
124 case 'R': /* don't automatically chase referrals */
125 #ifdef LDAP_REFERRALS
126 ldap_options &= ~LDAP_OPT_REFERRALS;
127 #else /* LDAP_REFERRALS */
129 "compile with -DLDAP_REFERRALS for referral support\n" );
130 #endif /* LDAP_REFERRALS */
132 case 'A': /* retrieve attribute names only -- no values */
135 case 'L': /* print entries in LDIF format */
137 /* fall through -- always allow binary when outputting LDIF */
138 case 'B': /* allow binary values to be printed */
141 case 's': /* search scope */
142 if ( strncasecmp( optarg, "base", 4 ) == 0 ) {
143 scope = LDAP_SCOPE_BASE;
144 } else if ( strncasecmp( optarg, "one", 3 ) == 0 ) {
145 scope = LDAP_SCOPE_ONELEVEL;
146 } else if ( strncasecmp( optarg, "sub", 3 ) == 0 ) {
147 scope = LDAP_SCOPE_SUBTREE;
149 fprintf( stderr, "scope should be base, one, or sub\n" );
154 case 'a': /* set alias deref option */
155 if ( strncasecmp( optarg, "never", 5 ) == 0 ) {
156 deref = LDAP_DEREF_NEVER;
157 } else if ( strncasecmp( optarg, "search", 5 ) == 0 ) {
158 deref = LDAP_DEREF_SEARCHING;
159 } else if ( strncasecmp( optarg, "find", 4 ) == 0 ) {
160 deref = LDAP_DEREF_FINDING;
161 } else if ( strncasecmp( optarg, "always", 6 ) == 0 ) {
162 deref = LDAP_DEREF_ALWAYS;
164 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
169 case 'F': /* field separator */
170 sep = strdup( optarg );
172 case 'f': /* input file */
173 infile = strdup( optarg );
175 case 'h': /* ldap host */
176 ldaphost = strdup( optarg );
178 case 'b': /* searchbase */
179 base = strdup( optarg );
181 case 'D': /* bind DN */
182 binddn = strdup( optarg );
184 case 'p': /* ldap port */
185 ldapport = atoi( optarg );
187 case 'w': /* bind password */
188 passwd = strdup( optarg );
190 case 'l': /* time limit */
191 timelimit = atoi( optarg );
193 case 'z': /* size limit */
194 sizelimit = atoi( optarg );
196 case 'S': /* sort attribute */
197 sortattr = strdup( optarg );
204 if ( argc - optind < 1 ) {
207 filtpattern = strdup( argv[ optind ] );
208 if ( argv[ optind + 1 ] == NULL ) {
210 } else if ( sortattr == NULL || *sortattr == '\0' ) {
211 attrs = &argv[ optind + 1 ];
213 for ( i = optind + 1; i < argc; i++ ) {
214 if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
220 argv[ optind ] = sortattr;
224 attrs = &argv[ optind ];
227 if ( infile != NULL ) {
228 if ( infile[0] == '-' && infile[1] == '\0' ) {
230 } else if (( fp = fopen( infile, "r" )) == NULL ) {
237 printf( "ldap_open( %s, %d )\n", ldaphost, ldapport );
240 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
245 ld->ld_deref = deref;
246 ld->ld_timelimit = timelimit;
247 ld->ld_sizelimit = sizelimit;
248 ld->ld_options = ldap_options;
251 authmethod = LDAP_AUTH_SIMPLE;
252 } else if ( kerberos == 1 ) {
253 authmethod = LDAP_AUTH_KRBV41;
255 authmethod = LDAP_AUTH_KRBV4;
257 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
258 ldap_perror( ld, "ldap_bind" );
263 printf( "filter pattern: %s\nreturning: ", filtpattern );
264 if ( attrs == NULL ) {
267 for ( i = 0; attrs[ i ] != NULL; ++i ) {
268 printf( "%s ", attrs[ i ] );
274 if ( infile == NULL ) {
275 rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern, "" );
279 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
280 line[ strlen( line ) - 1 ] = '\0';
286 rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern,
299 dosearch( ld, base, scope, attrs, attrsonly, filtpatt, value )
308 char filter[ BUFSIZ ], **val;
309 int rc, first, matches;
310 LDAPMessage *res, *e;
312 sprintf( filter, filtpatt, value );
315 printf( "filter is: (%s)\n", filter );
319 return( LDAP_SUCCESS );
322 if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
323 ldap_perror( ld, "ldap_search" );
324 return( ld->ld_errno );
329 while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
330 == LDAP_RES_SEARCH_ENTRY ) {
332 e = ldap_first_entry( ld, res );
338 print_entry( ld, e, attrsonly );
342 ldap_perror( ld, "ldap_result" );
345 if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
346 ldap_perror( ld, "ldap_search" );
348 if ( sortattr != NULL ) {
349 extern int strcasecmp();
351 (void) ldap_sort_entries( ld, &res,
352 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
355 for ( e = ldap_first_entry( ld, res ); e != NULLMSG;
356 e = ldap_next_entry( ld, e ) ) {
363 print_entry( ld, e, attrsonly );
368 printf( "%d matches\n", matches );
376 print_entry( ld, entry, attrsonly )
381 char *a, *dn, *ufn, tmpfname[ 64 ];
384 struct berval **bvals;
386 extern char *mktemp();
388 dn = ldap_get_dn( ld, entry );
390 write_ldif_value( "dn", dn, strlen( dn ));
392 printf( "%s\n", dn );
395 ufn = ldap_dn2ufn( dn );
397 write_ldif_value( "ufn", ufn, strlen( ufn ));
399 printf( "%s\n", ufn );
405 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
406 a = ldap_next_attribute( ld, entry, ber ) ) {
407 if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
412 write_ldif_value( a, "", 0 );
416 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
417 for ( i = 0; bvals[i] != NULL; i++ ) {
419 sprintf( tmpfname, "/tmp/ldapsearch-%s-XXXXXX", a );
422 if ( mktemp( tmpfname ) == NULL ) {
424 } else if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
426 } else if ( fwrite( bvals[ i ]->bv_val,
427 bvals[ i ]->bv_len, 1, tmpfp ) == 0 ) {
430 write_ldif_value( a, tmpfname, strlen( tmpfname ));
432 printf( "%s%s%s\n", a, sep, tmpfname );
435 if ( tmpfp != NULL ) {
440 if ( !allow_binary ) {
441 for ( j = 0; j < bvals[ i ]->bv_len; ++j ) {
442 if ( !isascii( bvals[ i ]->bv_val[ j ] )) {
450 write_ldif_value( a, bvals[ i ]->bv_val,
451 bvals[ i ]->bv_len );
453 printf( "%s%s%s\n", a, sep,
454 notascii ? "NOT ASCII" : bvals[ i ]->bv_val );
458 ber_bvecfree( bvals );
465 write_ldif_value( char *type, char *value, unsigned long vallen )
469 if (( ldif = ldif_type_and_value( type, value, (int)vallen )) == NULL ) {
473 fputs( ldif, stdout );