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" );
32 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
33 fprintf( stderr, " -R\t\tdo not automatically follow referrals\n" );
34 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_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 void print_entry LDAP_P((
59 static int write_ldif_value LDAP_P((
62 unsigned long vallen ));
64 static int dosearch LDAP_P((
73 static char *binddn = NULL;
74 static char *passwd = NULL;
75 static char *base = NULL;
76 static char *ldaphost = NULL;
77 static int ldapport = 0;
78 static char *sep = DEFSEP;
79 static char *sortattr = NULL;
80 static int skipsortattr = 0;
81 static int verbose, not, includeufn, allow_binary, vals2tmp, ldif;
84 main( int argc, char **argv )
86 char *infile, *filtpattern, **attrs, line[ BUFSIZ ];
88 int rc, i, first, scope, kerberos, deref, attrsonly;
89 int referrals, timelimit, sizelimit, authmethod;
93 deref = verbose = allow_binary = not = kerberos = vals2tmp =
95 referrals = (int) LDAP_OPT_ON;
96 sizelimit = timelimit = 0;
97 scope = LDAP_SCOPE_SUBTREE;
99 while (( i = getopt( argc, argv,
101 "KknuvtRABLD:s:f:h:b:d:p:F:a:w:l:z:S:"
103 "nuvtRABLD:s:f:h:b:d:p:F:a:w:l:z:S:"
107 case 'n': /* do Not do any searches */
110 case 'v': /* verbose mode */
115 ldap_debug = lber_debug = atoi( optarg ); /* */
116 #else /* LDAP_DEBUG */
117 fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
118 #endif /* LDAP_DEBUG */
121 case 'k': /* use kerberos bind */
124 case 'K': /* use kerberos bind, 1st part only */
128 case 'u': /* include UFN */
131 case 't': /* write attribute values to /tmp files */
134 case 'R': /* don't automatically chase referrals */
135 referrals = (int) LDAP_OPT_OFF;
137 case 'A': /* retrieve attribute names only -- no values */
140 case 'L': /* print entries in LDIF format */
142 /* fall through -- always allow binary when outputting LDIF */
143 case 'B': /* allow binary values to be printed */
146 case 's': /* search scope */
147 if ( strncasecmp( optarg, "base", 4 ) == 0 ) {
148 scope = LDAP_SCOPE_BASE;
149 } else if ( strncasecmp( optarg, "one", 3 ) == 0 ) {
150 scope = LDAP_SCOPE_ONELEVEL;
151 } else if ( strncasecmp( optarg, "sub", 3 ) == 0 ) {
152 scope = LDAP_SCOPE_SUBTREE;
154 fprintf( stderr, "scope should be base, one, or sub\n" );
159 case 'a': /* set alias deref option */
160 if ( strncasecmp( optarg, "never", 5 ) == 0 ) {
161 deref = LDAP_DEREF_NEVER;
162 } else if ( strncasecmp( optarg, "search", 5 ) == 0 ) {
163 deref = LDAP_DEREF_SEARCHING;
164 } else if ( strncasecmp( optarg, "find", 4 ) == 0 ) {
165 deref = LDAP_DEREF_FINDING;
166 } else if ( strncasecmp( optarg, "always", 6 ) == 0 ) {
167 deref = LDAP_DEREF_ALWAYS;
169 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
174 case 'F': /* field separator */
175 sep = strdup( optarg );
177 case 'f': /* input file */
178 infile = strdup( optarg );
180 case 'h': /* ldap host */
181 ldaphost = strdup( optarg );
183 case 'b': /* searchbase */
184 base = strdup( optarg );
186 case 'D': /* bind DN */
187 binddn = strdup( optarg );
189 case 'p': /* ldap port */
190 ldapport = atoi( optarg );
192 case 'w': /* bind password */
193 passwd = strdup( optarg );
195 case 'l': /* time limit */
196 timelimit = atoi( optarg );
198 case 'z': /* size limit */
199 sizelimit = atoi( optarg );
201 case 'S': /* sort attribute */
202 sortattr = strdup( optarg );
209 if ( argc - optind < 1 ) {
212 filtpattern = strdup( argv[ optind ] );
213 if ( argv[ optind + 1 ] == NULL ) {
215 } else if ( sortattr == NULL || *sortattr == '\0' ) {
216 attrs = &argv[ optind + 1 ];
218 for ( i = optind + 1; i < argc; i++ ) {
219 if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
225 argv[ optind ] = sortattr;
229 attrs = &argv[ optind ];
232 if ( infile != NULL ) {
233 if ( infile[0] == '-' && infile[1] == '\0' ) {
235 } else if (( fp = fopen( infile, "r" )) == NULL ) {
242 printf( "ldap_open( %s, %d )\n", ldaphost, ldapport );
245 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
250 if (ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) == -1 ) {
251 /* set option error */
253 if (ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) == -1 ) {
254 /* set option error */
256 if (ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) == -1 ) {
257 /* set option error */
259 if (ldap_set_option( ld, LDAP_OPT_REFERRALS, (void *) referrals ) == -1 ) {
260 /* set option error */
264 authmethod = LDAP_AUTH_SIMPLE;
265 } else if ( kerberos == 1 ) {
266 authmethod = LDAP_AUTH_KRBV41;
268 authmethod = LDAP_AUTH_KRBV4;
270 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
271 ldap_perror( ld, "ldap_bind" );
276 printf( "filter pattern: %s\nreturning: ", filtpattern );
277 if ( attrs == NULL ) {
280 for ( i = 0; attrs[ i ] != NULL; ++i ) {
281 printf( "%s ", attrs[ i ] );
287 if ( infile == NULL ) {
288 rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern, "" );
292 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
293 line[ strlen( line ) - 1 ] = '\0';
299 rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern,
324 char filter[ BUFSIZ ];
325 int rc, first, matches;
326 LDAPMessage *res, *e;
328 sprintf( filter, filtpatt, value );
331 printf( "filter is: (%s)\n", filter );
335 return( LDAP_SUCCESS );
338 if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
340 ldap_perror( ld, "ldap_search" );
342 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
348 while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
349 == LDAP_RES_SEARCH_ENTRY ) {
351 e = ldap_first_entry( ld, res );
357 print_entry( ld, e, attrsonly );
361 ldap_perror( ld, "ldap_result" );
364 if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
365 ldap_perror( ld, "ldap_search" );
367 if ( sortattr != NULL ) {
368 (void) ldap_sort_entries( ld, &res,
369 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
372 for ( e = ldap_first_entry( ld, res ); e != NULLMSG;
373 e = ldap_next_entry( ld, e ) ) {
380 print_entry( ld, e, attrsonly );
385 printf( "%d matches\n", matches );
398 char *a, *dn, *ufn, tmpfname[ 64 ];
401 struct berval **bvals;
404 dn = ldap_get_dn( ld, entry );
406 write_ldif_value( "dn", dn, strlen( dn ));
408 printf( "%s\n", dn );
411 ufn = ldap_dn2ufn( dn );
413 write_ldif_value( "ufn", ufn, strlen( ufn ));
415 printf( "%s\n", ufn );
421 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
422 a = ldap_next_attribute( ld, entry, ber ) ) {
423 if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
428 write_ldif_value( a, "", 0 );
432 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
433 for ( i = 0; bvals[i] != NULL; i++ ) {
435 sprintf( tmpfname, "/tmp/ldapsearch-%s-XXXXXX", a );
438 if ( mktemp( tmpfname ) == NULL ) {
440 } else if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
442 } else if ( fwrite( bvals[ i ]->bv_val,
443 bvals[ i ]->bv_len, 1, tmpfp ) == 0 ) {
446 write_ldif_value( a, tmpfname, strlen( tmpfname ));
448 printf( "%s%s%s\n", a, sep, tmpfname );
451 if ( tmpfp != NULL ) {
456 if ( !allow_binary ) {
457 for ( j = 0; (unsigned long) j < bvals[ i ]->bv_len; ++j ) {
458 if ( !isascii( bvals[ i ]->bv_val[ j ] )) {
466 write_ldif_value( a, bvals[ i ]->bv_val,
467 bvals[ i ]->bv_len );
469 printf( "%s%s%s\n", a, sep,
470 notascii ? "NOT ASCII" : bvals[ i ]->bv_val );
474 ber_bvecfree( bvals );
481 write_ldif_value( char *type, char *value, unsigned long vallen )
485 if (( ldif = ldif_type_and_value( type, value, (int)vallen )) == NULL ) {
489 fputs( ldif, stdout );