8 #include "ldapconfig.h"
13 extern int ldap_debug, lber_debug;
14 #endif /* LDAP_DEBUG */
20 fprintf( stderr, "usage: %s [options] filter [attributes...]\nwhere:\n", s );
21 fprintf( stderr, " filter\tRFC-1558 compliant LDAP search filter\n" );
22 fprintf( stderr, " attributes\twhitespace-separated list of attributes to retrieve\n" );
23 fprintf( stderr, "\t\t(if no attribute list is given, all are retrieved)\n" );
24 fprintf( stderr, "options:\n" );
25 fprintf( stderr, " -n\t\tshow what would be done but don't actually search\n" );
26 fprintf( stderr, " -v\t\trun in verbose mode (diagnostics to standard output)\n" );
27 fprintf( stderr, " -t\t\twrite values to files in /tmp\n" );
28 fprintf( stderr, " -u\t\tinclude User Friendly entry names in the output\n" );
29 fprintf( stderr, " -A\t\tretrieve attribute names only (no values)\n" );
30 fprintf( stderr, " -B\t\tdo not suppress printing of non-ASCII values\n" );
31 fprintf( stderr, " -L\t\tprint entries in LDIF format (-B is implied)\n" );
33 fprintf( stderr, " -R\t\tdo not automatically follow referrals\n" );
34 #endif /* LDAP_REFERRALS */
35 fprintf( stderr, " -d level\tset LDAP debugging level to `level'\n" );
36 fprintf( stderr, " -F sep\tprint `sep' instead of `=' between attribute names and values\n" );
37 fprintf( stderr, " -S attr\tsort the results by attribute `attr'\n" );
38 fprintf( stderr, " -f file\tperform sequence of searches listed in `file'\n" );
39 fprintf( stderr, " -b basedn\tbase dn for search\n" );
40 fprintf( stderr, " -s scope\tone of base, one, or sub (search scope)\n" );
41 fprintf( stderr, " -a deref\tone of never, always, search, or find (alias dereferencing)\n" );
42 fprintf( stderr, " -l time lim\ttime limit (in seconds) for search\n" );
43 fprintf( stderr, " -z size lim\tsize limit (in entries) for search\n" );
44 fprintf( stderr, " -D binddn\tbind dn\n" );
45 fprintf( stderr, " -w passwd\tbind passwd (for simple authentication)\n" );
47 fprintf( stderr, " -k\t\tuse Kerberos instead of Simple Password authentication\n" );
49 fprintf( stderr, " -h host\tldap server\n" );
50 fprintf( stderr, " -p port\tport on ldap server\n" );
54 static char *binddn = LDAPSEARCH_BINDDN;
55 static char *passwd = NULL;
56 static char *base = LDAPSEARCH_BASE;
57 static char *ldaphost = LDAPHOST;
58 static int ldapport = LDAP_PORT;
59 static char *sep = DEFSEP;
60 static char *sortattr = NULL;
61 static int skipsortattr = 0;
62 static int verbose, not, includeufn, allow_binary, vals2tmp, ldif;
68 char *infile, *filtpattern, **attrs, line[ BUFSIZ ];
70 int rc, i, first, scope, kerberos, deref, attrsonly;
71 int ldap_options, timelimit, sizelimit, authmethod;
77 deref = verbose = allow_binary = not = kerberos = vals2tmp =
80 ldap_options = LDAP_OPT_REFERRALS;
81 #else /* LDAP_REFERRALS */
83 #endif /* LDAP_REFERRALS */
84 sizelimit = timelimit = 0;
85 scope = LDAP_SCOPE_SUBTREE;
87 while (( i = getopt( argc, argv,
89 "KknuvtRABLD:s:f:h:b:d:p:F:a:w:l:z:S:"
91 "nuvtRABLD:s:f:h:b:d:p:F:a:w:l:z:S:"
95 case 'n': /* do Not do any searches */
98 case 'v': /* verbose mode */
103 ldap_debug = lber_debug = atoi( optarg ); /* */
104 #else /* LDAP_DEBUG */
105 fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
106 #endif /* LDAP_DEBUG */
109 case 'k': /* use kerberos bind */
112 case 'K': /* use kerberos bind, 1st part only */
116 case 'u': /* include UFN */
119 case 't': /* write attribute values to /tmp files */
122 case 'R': /* don't automatically chase referrals */
123 #ifdef LDAP_REFERRALS
124 ldap_options &= ~LDAP_OPT_REFERRALS;
125 #else /* LDAP_REFERRALS */
127 "compile with -DLDAP_REFERRALS for referral support\n" );
128 #endif /* LDAP_REFERRALS */
130 case 'A': /* retrieve attribute names only -- no values */
133 case 'L': /* print entries in LDIF format */
135 /* fall through -- always allow binary when outputting LDIF */
136 case 'B': /* allow binary values to be printed */
139 case 's': /* search scope */
140 if ( strncasecmp( optarg, "base", 4 ) == 0 ) {
141 scope = LDAP_SCOPE_BASE;
142 } else if ( strncasecmp( optarg, "one", 3 ) == 0 ) {
143 scope = LDAP_SCOPE_ONELEVEL;
144 } else if ( strncasecmp( optarg, "sub", 3 ) == 0 ) {
145 scope = LDAP_SCOPE_SUBTREE;
147 fprintf( stderr, "scope should be base, one, or sub\n" );
152 case 'a': /* set alias deref option */
153 if ( strncasecmp( optarg, "never", 5 ) == 0 ) {
154 deref = LDAP_DEREF_NEVER;
155 } else if ( strncasecmp( optarg, "search", 5 ) == 0 ) {
156 deref = LDAP_DEREF_SEARCHING;
157 } else if ( strncasecmp( optarg, "find", 4 ) == 0 ) {
158 deref = LDAP_DEREF_FINDING;
159 } else if ( strncasecmp( optarg, "always", 6 ) == 0 ) {
160 deref = LDAP_DEREF_ALWAYS;
162 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
167 case 'F': /* field separator */
168 sep = strdup( optarg );
170 case 'f': /* input file */
171 infile = strdup( optarg );
173 case 'h': /* ldap host */
174 ldaphost = strdup( optarg );
176 case 'b': /* searchbase */
177 base = strdup( optarg );
179 case 'D': /* bind DN */
180 binddn = strdup( optarg );
182 case 'p': /* ldap port */
183 ldapport = atoi( optarg );
185 case 'w': /* bind password */
186 passwd = strdup( optarg );
188 case 'l': /* time limit */
189 timelimit = atoi( optarg );
191 case 'z': /* size limit */
192 sizelimit = atoi( optarg );
194 case 'S': /* sort attribute */
195 sortattr = strdup( optarg );
202 if ( argc - optind < 1 ) {
205 filtpattern = strdup( argv[ optind ] );
206 if ( argv[ optind + 1 ] == NULL ) {
208 } else if ( sortattr == NULL || *sortattr == '\0' ) {
209 attrs = &argv[ optind + 1 ];
211 for ( i = optind + 1; i < argc; i++ ) {
212 if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
218 argv[ optind ] = sortattr;
222 attrs = &argv[ optind ];
225 if ( infile != NULL ) {
226 if ( infile[0] == '-' && infile[1] == '\0' ) {
228 } else if (( fp = fopen( infile, "r" )) == NULL ) {
235 printf( "ldap_open( %s, %d )\n", ldaphost, ldapport );
238 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
243 ld->ld_deref = deref;
244 ld->ld_timelimit = timelimit;
245 ld->ld_sizelimit = sizelimit;
246 ld->ld_options = ldap_options;
249 authmethod = LDAP_AUTH_SIMPLE;
250 } else if ( kerberos == 1 ) {
251 authmethod = LDAP_AUTH_KRBV41;
253 authmethod = LDAP_AUTH_KRBV4;
255 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
256 ldap_perror( ld, "ldap_bind" );
261 printf( "filter pattern: %s\nreturning: ", filtpattern );
262 if ( attrs == NULL ) {
265 for ( i = 0; attrs[ i ] != NULL; ++i ) {
266 printf( "%s ", attrs[ i ] );
272 if ( infile == NULL ) {
273 rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern, "" );
277 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
278 line[ strlen( line ) - 1 ] = '\0';
284 rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern,
297 dosearch( ld, base, scope, attrs, attrsonly, filtpatt, value )
306 char filter[ BUFSIZ ], **val;
307 int rc, first, matches;
308 LDAPMessage *res, *e;
310 sprintf( filter, filtpatt, value );
313 printf( "filter is: (%s)\n", filter );
317 return( LDAP_SUCCESS );
320 if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
321 ldap_perror( ld, "ldap_search" );
322 return( ld->ld_errno );
327 while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
328 == LDAP_RES_SEARCH_ENTRY ) {
330 e = ldap_first_entry( ld, res );
336 print_entry( ld, e, attrsonly );
340 ldap_perror( ld, "ldap_result" );
343 if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
344 ldap_perror( ld, "ldap_search" );
346 if ( sortattr != NULL ) {
347 extern int strcasecmp();
349 (void) ldap_sort_entries( ld, &res,
350 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
353 for ( e = ldap_first_entry( ld, res ); e != NULLMSG;
354 e = ldap_next_entry( ld, e ) ) {
361 print_entry( ld, e, attrsonly );
366 printf( "%d matches\n", matches );
374 print_entry( ld, entry, attrsonly )
379 char *a, *dn, *ufn, tmpfname[ 64 ];
382 struct berval **bvals;
384 extern char *mktemp();
386 dn = ldap_get_dn( ld, entry );
388 write_ldif_value( "dn", dn, strlen( dn ));
390 printf( "%s\n", dn );
393 ufn = ldap_dn2ufn( dn );
395 write_ldif_value( "ufn", ufn, strlen( ufn ));
397 printf( "%s\n", ufn );
403 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
404 a = ldap_next_attribute( ld, entry, ber ) ) {
405 if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
410 write_ldif_value( a, "", 0 );
414 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
415 for ( i = 0; bvals[i] != NULL; i++ ) {
417 sprintf( tmpfname, "/tmp/ldapsearch-%s-XXXXXX", a );
420 if ( mktemp( tmpfname ) == NULL ) {
422 } else if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
424 } else if ( fwrite( bvals[ i ]->bv_val,
425 bvals[ i ]->bv_len, 1, tmpfp ) == 0 ) {
428 write_ldif_value( a, tmpfname, strlen( tmpfname ));
430 printf( "%s%s%s\n", a, sep, tmpfname );
433 if ( tmpfp != NULL ) {
438 if ( !allow_binary ) {
439 for ( j = 0; j < bvals[ i ]->bv_len; ++j ) {
440 if ( !isascii( bvals[ i ]->bv_val[ j ] )) {
448 write_ldif_value( a, bvals[ i ]->bv_val,
449 bvals[ i ]->bv_len );
451 printf( "%s%s%s\n", a, sep,
452 notascii ? "NOT ASCII" : bvals[ i ]->bv_val );
456 ber_bvecfree( bvals );
463 write_ldif_value( char *type, char *value, unsigned long vallen )
467 if (( ldif = ldif_type_and_value( type, value, (int)vallen )) == NULL ) {
471 fputs( ldif, stdout );