9 extern char *strdup (const char *);
10 extern int strcasecmp(const char *, const char *);
11 extern char *mktemp(char *);
23 fprintf( stderr, "usage: %s [options] filter [attributes...]\nwhere:\n", s );
24 fprintf( stderr, " filter\tRFC-1558 compliant LDAP search filter\n" );
25 fprintf( stderr, " attributes\twhitespace-separated list of attributes to retrieve\n" );
26 fprintf( stderr, "\t\t(if no attribute list is given, all are retrieved)\n" );
27 fprintf( stderr, "options:\n" );
28 fprintf( stderr, " -n\t\tshow what would be done but don't actually search\n" );
29 fprintf( stderr, " -v\t\trun in verbose mode (diagnostics to standard output)\n" );
30 fprintf( stderr, " -t\t\twrite values to files in /tmp\n" );
31 fprintf( stderr, " -u\t\tinclude User Friendly entry names in the output\n" );
32 fprintf( stderr, " -A\t\tretrieve attribute names only (no values)\n" );
33 fprintf( stderr, " -B\t\tdo not suppress printing of non-ASCII values\n" );
34 fprintf( stderr, " -L\t\tprint entries in LDIF format (-B is implied)\n" );
35 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
36 fprintf( stderr, " -R\t\tdo not automatically follow referrals\n" );
37 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
38 fprintf( stderr, " -d level\tset LDAP debugging level to `level'\n" );
39 fprintf( stderr, " -F sep\tprint `sep' instead of `=' between attribute names and values\n" );
40 fprintf( stderr, " -S attr\tsort the results by attribute `attr'\n" );
41 fprintf( stderr, " -f file\tperform sequence of searches listed in `file'\n" );
42 fprintf( stderr, " -b basedn\tbase dn for search\n" );
43 fprintf( stderr, " -s scope\tone of base, one, or sub (search scope)\n" );
44 fprintf( stderr, " -a deref\tone of never, always, search, or find (alias dereferencing)\n" );
45 fprintf( stderr, " -l time lim\ttime limit (in seconds) for search\n" );
46 fprintf( stderr, " -z size lim\tsize limit (in entries) for search\n" );
47 fprintf( stderr, " -D binddn\tbind dn\n" );
48 fprintf( stderr, " -w passwd\tbind passwd (for simple authentication)\n" );
50 fprintf( stderr, " -k\t\tuse Kerberos instead of Simple Password authentication\n" );
52 fprintf( stderr, " -h host\tldap server\n" );
53 fprintf( stderr, " -p port\tport on ldap server\n" );
57 static void print_entry LDAP_P((
62 static int write_ldif_value LDAP_P((
65 unsigned long vallen ));
67 static int dosearch LDAP_P((
76 static char *binddn = NULL;
77 static char *passwd = NULL;
78 static char *base = NULL;
79 static char *ldaphost = NULL;
80 static int ldapport = 0;
81 static char *sep = DEFSEP;
82 static char *sortattr = NULL;
83 static int skipsortattr = 0;
84 static int verbose, not, includeufn, allow_binary, vals2tmp, ldif;
87 main( int argc, char **argv )
89 char *infile, *filtpattern, **attrs, line[ BUFSIZ ];
91 int rc, i, first, scope, kerberos, deref, attrsonly;
92 int referrals, timelimit, sizelimit, authmethod;
98 deref = verbose = allow_binary = not = kerberos = vals2tmp =
100 referrals = (int) LDAP_OPT_ON;
101 sizelimit = timelimit = 0;
102 scope = LDAP_SCOPE_SUBTREE;
104 while (( i = getopt( argc, argv,
106 "KknuvtRABLD:s:f:h:b:d:p:F:a:w:l:z:S:"
108 "nuvtRABLD:s:f:h:b:d:p:F:a:w:l:z:S:"
112 case 'n': /* do Not do any searches */
115 case 'v': /* verbose mode */
120 ldap_debug = lber_debug = atoi( optarg ); /* */
121 #else /* LDAP_DEBUG */
122 fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
123 #endif /* LDAP_DEBUG */
126 case 'k': /* use kerberos bind */
129 case 'K': /* use kerberos bind, 1st part only */
133 case 'u': /* include UFN */
136 case 't': /* write attribute values to /tmp files */
139 case 'R': /* don't automatically chase referrals */
140 referrals = (int) LDAP_OPT_OFF;
142 case 'A': /* retrieve attribute names only -- no values */
145 case 'L': /* print entries in LDIF format */
147 /* fall through -- always allow binary when outputting LDIF */
148 case 'B': /* allow binary values to be printed */
151 case 's': /* search scope */
152 if ( strncasecmp( optarg, "base", 4 ) == 0 ) {
153 scope = LDAP_SCOPE_BASE;
154 } else if ( strncasecmp( optarg, "one", 3 ) == 0 ) {
155 scope = LDAP_SCOPE_ONELEVEL;
156 } else if ( strncasecmp( optarg, "sub", 3 ) == 0 ) {
157 scope = LDAP_SCOPE_SUBTREE;
159 fprintf( stderr, "scope should be base, one, or sub\n" );
164 case 'a': /* set alias deref option */
165 if ( strncasecmp( optarg, "never", 5 ) == 0 ) {
166 deref = LDAP_DEREF_NEVER;
167 } else if ( strncasecmp( optarg, "search", 5 ) == 0 ) {
168 deref = LDAP_DEREF_SEARCHING;
169 } else if ( strncasecmp( optarg, "find", 4 ) == 0 ) {
170 deref = LDAP_DEREF_FINDING;
171 } else if ( strncasecmp( optarg, "always", 6 ) == 0 ) {
172 deref = LDAP_DEREF_ALWAYS;
174 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
179 case 'F': /* field separator */
180 sep = strdup( optarg );
182 case 'f': /* input file */
183 infile = strdup( optarg );
185 case 'h': /* ldap host */
186 ldaphost = strdup( optarg );
188 case 'b': /* searchbase */
189 base = strdup( optarg );
191 case 'D': /* bind DN */
192 binddn = strdup( optarg );
194 case 'p': /* ldap port */
195 ldapport = atoi( optarg );
197 case 'w': /* bind password */
198 passwd = strdup( optarg );
200 case 'l': /* time limit */
201 timelimit = atoi( optarg );
203 case 'z': /* size limit */
204 sizelimit = atoi( optarg );
206 case 'S': /* sort attribute */
207 sortattr = strdup( optarg );
214 if ( argc - optind < 1 ) {
217 filtpattern = strdup( argv[ optind ] );
218 if ( argv[ optind + 1 ] == NULL ) {
220 } else if ( sortattr == NULL || *sortattr == '\0' ) {
221 attrs = &argv[ optind + 1 ];
223 for ( i = optind + 1; i < argc; i++ ) {
224 if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
230 argv[ optind ] = sortattr;
234 attrs = &argv[ optind ];
237 if ( infile != NULL ) {
238 if ( infile[0] == '-' && infile[1] == '\0' ) {
240 } else if (( fp = fopen( infile, "r" )) == NULL ) {
247 printf( "ldap_open( %s, %d )\n", ldaphost, ldapport );
250 if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
255 if (ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) == -1 ) {
256 /* set option error */
258 if (ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) == -1 ) {
259 /* set option error */
261 if (ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) == -1 ) {
262 /* set option error */
264 if (ldap_set_option( ld, LDAP_OPT_REFERRALS, (void *) referrals ) == -1 ) {
265 /* set option error */
269 authmethod = LDAP_AUTH_SIMPLE;
270 } else if ( kerberos == 1 ) {
271 authmethod = LDAP_AUTH_KRBV41;
273 authmethod = LDAP_AUTH_KRBV4;
275 if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
276 ldap_perror( ld, "ldap_bind" );
281 printf( "filter pattern: %s\nreturning: ", filtpattern );
282 if ( attrs == NULL ) {
285 for ( i = 0; attrs[ i ] != NULL; ++i ) {
286 printf( "%s ", attrs[ i ] );
292 if ( infile == NULL ) {
293 rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern, "" );
297 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
298 line[ strlen( line ) - 1 ] = '\0';
304 rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern,
329 char filter[ BUFSIZ ];
330 int rc, first, matches;
331 LDAPMessage *res, *e;
333 sprintf( filter, filtpatt, value );
336 printf( "filter is: (%s)\n", filter );
340 return( LDAP_SUCCESS );
343 if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
345 ldap_perror( ld, "ldap_search" );
347 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &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 (void) ldap_sort_entries( ld, &res,
374 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
377 for ( e = ldap_first_entry( ld, res ); e != NULLMSG;
378 e = ldap_next_entry( ld, e ) ) {
385 print_entry( ld, e, attrsonly );
390 printf( "%d matches\n", matches );
403 char *a, *dn, *ufn, tmpfname[ 64 ];
406 struct berval **bvals;
409 dn = ldap_get_dn( ld, entry );
411 write_ldif_value( "dn", dn, strlen( dn ));
413 printf( "%s\n", dn );
416 ufn = ldap_dn2ufn( dn );
418 write_ldif_value( "ufn", ufn, strlen( ufn ));
420 printf( "%s\n", ufn );
426 for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
427 a = ldap_next_attribute( ld, entry, ber ) ) {
428 if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
433 write_ldif_value( a, "", 0 );
437 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
438 for ( i = 0; bvals[i] != NULL; i++ ) {
440 sprintf( tmpfname, "/tmp/ldapsearch-%s-XXXXXX", a );
443 if ( mktemp( tmpfname ) == NULL ) {
445 } else if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
447 } else if ( fwrite( bvals[ i ]->bv_val,
448 bvals[ i ]->bv_len, 1, tmpfp ) == 0 ) {
451 write_ldif_value( a, tmpfname, strlen( tmpfname ));
453 printf( "%s%s%s\n", a, sep, tmpfname );
456 if ( tmpfp != NULL ) {
461 if ( !allow_binary ) {
462 for ( j = 0; (unsigned long) j < bvals[ i ]->bv_len; ++j ) {
463 if ( !isascii( bvals[ i ]->bv_val[ j ] )) {
471 write_ldif_value( a, bvals[ i ]->bv_val,
472 bvals[ i ]->bv_len );
474 printf( "%s%s%s\n", a, sep,
475 notascii ? "NOT ASCII" : bvals[ i ]->bv_val );
479 ber_bvecfree( bvals );
486 write_ldif_value( char *type, char *value, unsigned long vallen )
490 if (( ldif = ldif_type_and_value( type, value, (int)vallen )) == NULL ) {
494 fputs( ldif, stdout );