14 #include "ldapconfig.h"
19 extern int ldap_debug, lber_debug;
20 #endif /* LDAP_DEBUG */
23 static void usage( s )
26 fprintf( stderr, "usage: %s [options] filter [attributes...]\nwhere:\n", s );
27 fprintf( stderr, " filter\tRFC-1558 compliant LDAP search filter\n" );
28 fprintf( stderr, " attributes\twhitespace-separated list of attributes to retrieve\n" );
29 fprintf( stderr, "\t\t(if no attribute list is given, all are retrieved)\n" );
30 fprintf( stderr, "options:\n" );
31 fprintf( stderr, " -n\t\tshow what would be done but don't actually search\n" );
32 fprintf( stderr, " -v\t\trun in verbose mode (diagnostics to standard output)\n" );
33 fprintf( stderr, " -t\t\twrite values to files in /tmp\n" );
34 fprintf( stderr, " -u\t\tinclude User Friendly entry names in the output\n" );
35 fprintf( stderr, " -A\t\tretrieve attribute names only (no values)\n" );
36 fprintf( stderr, " -B\t\tdo not suppress printing of non-ASCII values\n" );
37 fprintf( stderr, " -L\t\tprint entries in LDIF format (-B is implied)\n" );
39 fprintf( stderr, " -R\t\tdo not automatically follow referrals\n" );
40 #endif /* LDAP_REFERRALS */
41 fprintf( stderr, " -d level\tset LDAP debugging level to `level'\n" );
42 fprintf( stderr, " -F sep\tprint `sep' instead of `=' between attribute names and values\n" );
43 fprintf( stderr, " -S attr\tsort the results by attribute `attr'\n" );
44 fprintf( stderr, " -f file\tperform sequence of searches listed in `file'\n" );
45 fprintf( stderr, " -b basedn\tbase dn for search\n" );
46 fprintf( stderr, " -s scope\tone of base, one, or sub (search scope)\n" );
47 fprintf( stderr, " -a deref\tone of never, always, search, or find (alias dereferencing)\n" );
48 fprintf( stderr, " -l time lim\ttime limit (in seconds) for search\n" );
49 fprintf( stderr, " -z size lim\tsize limit (in entries) for search\n" );
50 fprintf( stderr, " -D binddn\tbind dn\n" );
51 fprintf( stderr, " -w passwd\tbind passwd (for simple authentication)\n" );
53 fprintf( stderr, " -k\t\tuse Kerberos instead of Simple Password authentication\n" );
55 fprintf( stderr, " -h host\tldap server\n" );
56 fprintf( stderr, " -p port\tport on ldap server\n" );
60 static void print_entry LDAP_P((
65 static int write_ldif_value LDAP_P((
68 unsigned long vallen ));
70 static int dosearch LDAP_P((
79 static char *binddn = LDAPSEARCH_BINDDN;
80 static char *passwd = LDAPSEARCH_BIND_CRED;
81 static char *base = LDAPSEARCH_BASE;
82 static char *ldaphost = LDAPHOST;
83 static int ldapport = LDAP_PORT;
84 static char *sep = DEFSEP;
85 static char *sortattr = NULL;
86 static int skipsortattr = 0;
87 static int verbose, not, includeufn, allow_binary, vals2tmp, ldif;
93 char *infile, *filtpattern, **attrs, line[ BUFSIZ ];
95 int rc, i, first, scope, kerberos, deref, attrsonly;
96 int referrals, timelimit, sizelimit, authmethod;
102 deref = verbose = allow_binary = not = kerberos = vals2tmp =
103 attrsonly = ldif = 0;
104 referrals = (int) LDAP_OPT_ON;
105 sizelimit = timelimit = 0;
106 scope = LDAP_SCOPE_SUBTREE;
108 while (( i = getopt( argc, argv,
110 "KknuvtRABLD:s:f:h:b:d:p:F:a:w:l:z:S:"
112 "nuvtRABLD:s:f:h:b:d:p:F:a:w:l:z:S:"
116 case 'n': /* do Not do any searches */
119 case 'v': /* verbose mode */
124 ldap_debug = lber_debug = atoi( optarg ); /* */
125 #else /* LDAP_DEBUG */
126 fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
127 #endif /* LDAP_DEBUG */
130 case 'k': /* use kerberos bind */
133 case 'K': /* use kerberos bind, 1st part only */
137 case 'u': /* include UFN */
140 case 't': /* write attribute values to /tmp files */
143 case 'R': /* don't automatically chase referrals */
144 referrals = (int) LDAP_OPT_OFF;
146 case 'A': /* retrieve attribute names only -- no values */
149 case 'L': /* print entries in LDIF format */
151 /* fall through -- always allow binary when outputting LDIF */
152 case 'B': /* allow binary values to be printed */
155 case 's': /* search scope */
156 if ( strncasecmp( optarg, "base", 4 ) == 0 ) {
157 scope = LDAP_SCOPE_BASE;
158 } else if ( strncasecmp( optarg, "one", 3 ) == 0 ) {
159 scope = LDAP_SCOPE_ONELEVEL;
160 } else if ( strncasecmp( optarg, "sub", 3 ) == 0 ) {
161 scope = LDAP_SCOPE_SUBTREE;
163 fprintf( stderr, "scope should be base, one, or sub\n" );
168 case 'a': /* set alias deref option */
169 if ( strncasecmp( optarg, "never", 5 ) == 0 ) {
170 deref = LDAP_DEREF_NEVER;
171 } else if ( strncasecmp( optarg, "search", 5 ) == 0 ) {
172 deref = LDAP_DEREF_SEARCHING;
173 } else if ( strncasecmp( optarg, "find", 4 ) == 0 ) {
174 deref = LDAP_DEREF_FINDING;
175 } else if ( strncasecmp( optarg, "always", 6 ) == 0 ) {
176 deref = LDAP_DEREF_ALWAYS;
178 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
183 case 'F': /* field separator */
184 sep = strdup( optarg );
186 case 'f': /* input file */
187 infile = strdup( optarg );
189 case 'h': /* ldap host */
190 ldaphost = strdup( optarg );
192 case 'b': /* searchbase */
193 base = strdup( optarg );
195 case 'D': /* bind DN */
196 binddn = strdup( optarg );
198 case 'p': /* ldap port */
199 ldapport = atoi( optarg );
201 case 'w': /* bind password */
202 passwd = strdup( optarg );
204 case 'l': /* time limit */
205 timelimit = atoi( optarg );
207 case 'z': /* size limit */
208 sizelimit = atoi( optarg );
210 case 'S': /* sort attribute */
211 sortattr = strdup( optarg );
218 if ( argc - optind < 1 ) {
221 filtpattern = strdup( argv[ optind ] );
222 if ( argv[ optind + 1 ] == NULL ) {
224 } else if ( sortattr == NULL || *sortattr == '\0' ) {
225 attrs = &argv[ optind + 1 ];
227 for ( i = optind + 1; i < argc; i++ ) {
228 if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
234 argv[ optind ] = sortattr;
238 attrs = &argv[ optind ];
241 if ( infile != NULL ) {
242 if ( infile[0] == '-' && infile[1] == '\0' ) {
244 } else if (( fp = fopen( infile, "r" )) == NULL ) {
251 printf( "ldap_open( %s, %d )\n", ldaphost, ldapport );
254 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
259 if (ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) == -1 ) {
260 /* set option error */
262 if (ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) == -1 ) {
263 /* set option error */
265 if (ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) == -1 ) {
266 /* set option error */
268 if (ldap_set_option( ld, LDAP_OPT_REFERRALS, (void *) referrals ) == -1 ) {
269 /* set option error */
273 authmethod = LDAP_AUTH_SIMPLE;
274 } else if ( kerberos == 1 ) {
275 authmethod = LDAP_AUTH_KRBV41;
277 authmethod = LDAP_AUTH_KRBV4;
279 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
280 ldap_perror( ld, "ldap_bind" );
285 printf( "filter pattern: %s\nreturning: ", filtpattern );
286 if ( attrs == NULL ) {
289 for ( i = 0; attrs[ i ] != NULL; ++i ) {
290 printf( "%s ", attrs[ i ] );
296 if ( infile == NULL ) {
297 rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern, "" );
301 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
302 line[ strlen( line ) - 1 ] = '\0';
308 rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern,
330 char filter[ BUFSIZ ];
331 int rc, first, matches;
332 LDAPMessage *res, *e;
334 sprintf( filter, filtpatt, value );
337 printf( "filter is: (%s)\n", filter );
341 return( LDAP_SUCCESS );
344 if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
345 ldap_perror( ld, "ldap_search" );
347 return( ldap_lderrno(ld) );
355 while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
356 == LDAP_RES_SEARCH_ENTRY ) {
358 e = ldap_first_entry( ld, res );
364 print_entry( ld, e, attrsonly );
368 ldap_perror( ld, "ldap_result" );
371 if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
372 ldap_perror( ld, "ldap_search" );
374 if ( sortattr != NULL ) {
375 extern int strcasecmp();
377 (void) ldap_sort_entries( ld, &res,
378 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
381 for ( e = ldap_first_entry( ld, res ); e != NULLMSG;
382 e = ldap_next_entry( ld, e ) ) {
389 print_entry( ld, e, attrsonly );
394 printf( "%d matches\n", matches );
407 char *a, *dn, *ufn, tmpfname[ 64 ];
410 struct berval **bvals;
412 extern char *mktemp();
414 dn = ldap_get_dn( ld, entry );
416 write_ldif_value( "dn", dn, strlen( dn ));
418 printf( "%s\n", dn );
421 ufn = ldap_dn2ufn( dn );
423 write_ldif_value( "ufn", ufn, strlen( ufn ));
425 printf( "%s\n", ufn );
431 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
432 a = ldap_next_attribute( ld, entry, ber ) ) {
433 if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
438 write_ldif_value( a, "", 0 );
442 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
443 for ( i = 0; bvals[i] != NULL; i++ ) {
445 sprintf( tmpfname, "/tmp/ldapsearch-%s-XXXXXX", a );
448 if ( mktemp( tmpfname ) == NULL ) {
450 } else if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
452 } else if ( fwrite( bvals[ i ]->bv_val,
453 bvals[ i ]->bv_len, 1, tmpfp ) == 0 ) {
456 write_ldif_value( a, tmpfname, strlen( tmpfname ));
458 printf( "%s%s%s\n", a, sep, tmpfname );
461 if ( tmpfp != NULL ) {
466 if ( !allow_binary ) {
467 for ( j = 0; (unsigned long) j < bvals[ i ]->bv_len; ++j ) {
468 if ( !isascii( bvals[ i ]->bv_val[ j ] )) {
476 write_ldif_value( a, bvals[ i ]->bv_val,
477 bvals[ i ]->bv_len );
479 printf( "%s%s%s\n", a, sep,
480 notascii ? "NOT ASCII" : bvals[ i ]->bv_val );
484 ber_bvecfree( bvals );
491 write_ldif_value( char *type, char *value, unsigned long vallen )
495 if (( ldif = ldif_type_and_value( type, value, (int)vallen )) == NULL ) {
499 fputs( ldif, stdout );