15 #include "ldapconfig.h"
20 extern int ldap_debug, lber_debug;
21 #endif /* LDAP_DEBUG */
24 static void usage( s )
27 fprintf( stderr, "usage: %s [options] filter [attributes...]\nwhere:\n", s );
28 fprintf( stderr, " filter\tRFC-1558 compliant LDAP search filter\n" );
29 fprintf( stderr, " attributes\twhitespace-separated list of attributes to retrieve\n" );
30 fprintf( stderr, "\t\t(if no attribute list is given, all are retrieved)\n" );
31 fprintf( stderr, "options:\n" );
32 fprintf( stderr, " -n\t\tshow what would be done but don't actually search\n" );
33 fprintf( stderr, " -v\t\trun in verbose mode (diagnostics to standard output)\n" );
34 fprintf( stderr, " -t\t\twrite values to files in /tmp\n" );
35 fprintf( stderr, " -u\t\tinclude User Friendly entry names in the output\n" );
36 fprintf( stderr, " -A\t\tretrieve attribute names only (no values)\n" );
37 fprintf( stderr, " -B\t\tdo not suppress printing of non-ASCII values\n" );
38 fprintf( stderr, " -L\t\tprint entries in LDIF format (-B is implied)\n" );
40 fprintf( stderr, " -R\t\tdo not automatically follow referrals\n" );
41 #endif /* LDAP_REFERRALS */
42 fprintf( stderr, " -d level\tset LDAP debugging level to `level'\n" );
43 fprintf( stderr, " -F sep\tprint `sep' instead of `=' between attribute names and values\n" );
44 fprintf( stderr, " -S attr\tsort the results by attribute `attr'\n" );
45 fprintf( stderr, " -f file\tperform sequence of searches listed in `file'\n" );
46 fprintf( stderr, " -b basedn\tbase dn for search\n" );
47 fprintf( stderr, " -s scope\tone of base, one, or sub (search scope)\n" );
48 fprintf( stderr, " -a deref\tone of never, always, search, or find (alias dereferencing)\n" );
49 fprintf( stderr, " -l time lim\ttime limit (in seconds) for search\n" );
50 fprintf( stderr, " -z size lim\tsize limit (in entries) for search\n" );
51 fprintf( stderr, " -D binddn\tbind dn\n" );
52 fprintf( stderr, " -w passwd\tbind passwd (for simple authentication)\n" );
54 fprintf( stderr, " -k\t\tuse Kerberos instead of Simple Password authentication\n" );
56 fprintf( stderr, " -h host\tldap server\n" );
57 fprintf( stderr, " -p port\tport on ldap server\n" );
61 static void print_entry LDAP_P((
66 static int write_ldif_value LDAP_P((
69 unsigned long vallen ));
71 static int dosearch LDAP_P((
80 static char *binddn = LDAPSEARCH_BINDDN;
81 static char *passwd = LDAPSEARCH_BIND_CRED;
82 static char *base = LDAPSEARCH_BASE;
83 static char *ldaphost = LDAPHOST;
84 static int ldapport = LDAP_PORT;
85 static char *sep = DEFSEP;
86 static char *sortattr = NULL;
87 static int skipsortattr = 0;
88 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 ldap_options, timelimit, sizelimit, authmethod;
103 deref = verbose = allow_binary = not = kerberos = vals2tmp =
104 attrsonly = ldif = 0;
105 #ifdef LDAP_REFERRALS
106 ldap_options = LDAP_OPT_REFERRALS;
107 #else /* LDAP_REFERRALS */
109 #endif /* LDAP_REFERRALS */
110 sizelimit = timelimit = 0;
111 scope = LDAP_SCOPE_SUBTREE;
113 while (( i = getopt( argc, argv,
115 "KknuvtRABLD:s:f:h:b:d:p:F:a:w:l:z:S:"
117 "nuvtRABLD:s:f:h:b:d:p:F:a:w:l:z:S:"
121 case 'n': /* do Not do any searches */
124 case 'v': /* verbose mode */
129 ldap_debug = lber_debug = atoi( optarg ); /* */
130 #else /* LDAP_DEBUG */
131 fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
132 #endif /* LDAP_DEBUG */
135 case 'k': /* use kerberos bind */
138 case 'K': /* use kerberos bind, 1st part only */
142 case 'u': /* include UFN */
145 case 't': /* write attribute values to /tmp files */
148 case 'R': /* don't automatically chase referrals */
149 #ifdef LDAP_REFERRALS
150 ldap_options &= ~LDAP_OPT_REFERRALS;
151 #else /* LDAP_REFERRALS */
153 "compile with -DLDAP_REFERRALS for referral support\n" );
154 #endif /* LDAP_REFERRALS */
156 case 'A': /* retrieve attribute names only -- no values */
159 case 'L': /* print entries in LDIF format */
161 /* fall through -- always allow binary when outputting LDIF */
162 case 'B': /* allow binary values to be printed */
165 case 's': /* search scope */
166 if ( strncasecmp( optarg, "base", 4 ) == 0 ) {
167 scope = LDAP_SCOPE_BASE;
168 } else if ( strncasecmp( optarg, "one", 3 ) == 0 ) {
169 scope = LDAP_SCOPE_ONELEVEL;
170 } else if ( strncasecmp( optarg, "sub", 3 ) == 0 ) {
171 scope = LDAP_SCOPE_SUBTREE;
173 fprintf( stderr, "scope should be base, one, or sub\n" );
178 case 'a': /* set alias deref option */
179 if ( strncasecmp( optarg, "never", 5 ) == 0 ) {
180 deref = LDAP_DEREF_NEVER;
181 } else if ( strncasecmp( optarg, "search", 5 ) == 0 ) {
182 deref = LDAP_DEREF_SEARCHING;
183 } else if ( strncasecmp( optarg, "find", 4 ) == 0 ) {
184 deref = LDAP_DEREF_FINDING;
185 } else if ( strncasecmp( optarg, "always", 6 ) == 0 ) {
186 deref = LDAP_DEREF_ALWAYS;
188 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
193 case 'F': /* field separator */
194 sep = strdup( optarg );
196 case 'f': /* input file */
197 infile = strdup( optarg );
199 case 'h': /* ldap host */
200 ldaphost = strdup( optarg );
202 case 'b': /* searchbase */
203 base = strdup( optarg );
205 case 'D': /* bind DN */
206 binddn = strdup( optarg );
208 case 'p': /* ldap port */
209 ldapport = atoi( optarg );
211 case 'w': /* bind password */
212 passwd = strdup( optarg );
214 case 'l': /* time limit */
215 timelimit = atoi( optarg );
217 case 'z': /* size limit */
218 sizelimit = atoi( optarg );
220 case 'S': /* sort attribute */
221 sortattr = strdup( optarg );
228 if ( argc - optind < 1 ) {
231 filtpattern = strdup( argv[ optind ] );
232 if ( argv[ optind + 1 ] == NULL ) {
234 } else if ( sortattr == NULL || *sortattr == '\0' ) {
235 attrs = &argv[ optind + 1 ];
237 for ( i = optind + 1; i < argc; i++ ) {
238 if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
244 argv[ optind ] = sortattr;
248 attrs = &argv[ optind ];
251 if ( infile != NULL ) {
252 if ( infile[0] == '-' && infile[1] == '\0' ) {
254 } else if (( fp = fopen( infile, "r" )) == NULL ) {
261 printf( "ldap_open( %s, %d )\n", ldaphost, ldapport );
264 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
269 ld->ld_deref = deref;
270 ld->ld_timelimit = timelimit;
271 ld->ld_sizelimit = sizelimit;
272 ld->ld_options = ldap_options;
275 authmethod = LDAP_AUTH_SIMPLE;
276 } else if ( kerberos == 1 ) {
277 authmethod = LDAP_AUTH_KRBV41;
279 authmethod = LDAP_AUTH_KRBV4;
281 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
282 ldap_perror( ld, "ldap_bind" );
287 printf( "filter pattern: %s\nreturning: ", filtpattern );
288 if ( attrs == NULL ) {
291 for ( i = 0; attrs[ i ] != NULL; ++i ) {
292 printf( "%s ", attrs[ i ] );
298 if ( infile == NULL ) {
299 rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern, "" );
303 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
304 line[ strlen( line ) - 1 ] = '\0';
310 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 ) {
347 ldap_perror( ld, "ldap_search" );
348 return( ld->ld_errno );
353 while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
354 == LDAP_RES_SEARCH_ENTRY ) {
356 e = ldap_first_entry( ld, res );
362 print_entry( ld, e, attrsonly );
366 ldap_perror( ld, "ldap_result" );
369 if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
370 ldap_perror( ld, "ldap_search" );
372 if ( sortattr != NULL ) {
373 extern int strcasecmp();
375 (void) ldap_sort_entries( ld, &res,
376 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
379 for ( e = ldap_first_entry( ld, res ); e != NULLMSG;
380 e = ldap_next_entry( ld, e ) ) {
387 print_entry( ld, e, attrsonly );
392 printf( "%d matches\n", matches );
405 char *a, *dn, *ufn, tmpfname[ 64 ];
408 struct berval **bvals;
410 extern char *mktemp();
412 dn = ldap_get_dn( ld, entry );
414 write_ldif_value( "dn", dn, strlen( dn ));
416 printf( "%s\n", dn );
419 ufn = ldap_dn2ufn( dn );
421 write_ldif_value( "ufn", ufn, strlen( ufn ));
423 printf( "%s\n", ufn );
429 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
430 a = ldap_next_attribute( ld, entry, ber ) ) {
431 if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
436 write_ldif_value( a, "", 0 );
440 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
441 for ( i = 0; bvals[i] != NULL; i++ ) {
443 sprintf( tmpfname, "/tmp/ldapsearch-%s-XXXXXX", a );
446 if ( mktemp( tmpfname ) == NULL ) {
448 } else if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
450 } else if ( fwrite( bvals[ i ]->bv_val,
451 bvals[ i ]->bv_len, 1, tmpfp ) == 0 ) {
454 write_ldif_value( a, tmpfname, strlen( tmpfname ));
456 printf( "%s%s%s\n", a, sep, tmpfname );
459 if ( tmpfp != NULL ) {
464 if ( !allow_binary ) {
465 for ( j = 0; (unsigned long) j < bvals[ i ]->bv_len; ++j ) {
466 if ( !isascii( bvals[ i ]->bv_val[ j ] )) {
474 write_ldif_value( a, bvals[ i ]->bv_val,
475 bvals[ i ]->bv_len );
477 printf( "%s%s%s\n", a, sep,
478 notascii ? "NOT ASCII" : bvals[ i ]->bv_val );
482 ber_bvecfree( bvals );
489 write_ldif_value( char *type, char *value, unsigned long vallen )
493 if (( ldif = ldif_type_and_value( type, value, (int)vallen )) == NULL ) {
497 fputs( ldif, stdout );