]> git.sur5r.net Git - openldap/blob - clients/tools/ldapsearch.c
7ae964cd763c3c8b2f93fd39a4081af876a605b3
[openldap] / clients / tools / ldapsearch.c
1 #include "portable.h"
2
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 #include <ac/ctype.h>
7 #include <ac/string.h>
8 #include <ac/unistd.h>
9
10 #include <lber.h>
11 #include <ldap.h>
12 #include <ldif.h>
13
14 #define DEFSEP          "="
15
16
17 static void
18 usage( char *s )
19 {
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" );
46 #ifdef HAVE_KERBEROS
47     fprintf( stderr, "    -k\t\tuse Kerberos instead of Simple Password authentication\n" );
48 #endif
49     fprintf( stderr, "    -h host\tldap server\n" );
50     fprintf( stderr, "    -p port\tport on ldap server\n" );
51     exit( 1 );
52 }
53
54 static void print_entry LDAP_P((
55     LDAP        *ld,
56     LDAPMessage *entry,
57     int         attrsonly));
58
59 static int write_ldif_value LDAP_P((
60         char *type,
61         char *value,
62         unsigned long vallen ));
63
64 static int dosearch LDAP_P((
65         LDAP    *ld,
66     char        *base,
67     int         scope,
68     char        **attrs,
69     int         attrsonly,
70     char        *filtpatt,
71     char        *value));
72
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;
82
83 int
84 main( int argc, char **argv )
85 {
86     char                *infile, *filtpattern, **attrs, line[ BUFSIZ ];
87     FILE                *fp;
88     int                 rc, i, first, scope, kerberos, deref, attrsonly;
89     int                 referrals, timelimit, sizelimit, authmethod;
90     LDAP                *ld;
91
92     infile = NULL;
93     deref = verbose = allow_binary = not = kerberos = vals2tmp =
94             attrsonly = ldif = 0;
95     referrals = (int) LDAP_OPT_ON;
96     sizelimit = timelimit = 0;
97     scope = LDAP_SCOPE_SUBTREE;
98
99     while (( i = getopt( argc, argv,
100 #ifdef HAVE_KERBEROS
101             "KknuvtRABLD:s:f:h:b:d:p:F:a:w:l:z:S:"
102 #else
103             "nuvtRABLD:s:f:h:b:d:p:F:a:w:l:z:S:"
104 #endif
105             )) != EOF ) {
106         switch( i ) {
107         case 'n':       /* do Not do any searches */
108             ++not;
109             break;
110         case 'v':       /* verbose mode */
111             ++verbose;
112             break;
113         case 'd':
114 #ifdef LDAP_DEBUG
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 */
119             break;
120 #ifdef HAVE_KERBEROS
121         case 'k':       /* use kerberos bind */
122             kerberos = 2;
123             break;
124         case 'K':       /* use kerberos bind, 1st part only */
125             kerberos = 1;
126             break;
127 #endif
128         case 'u':       /* include UFN */
129             ++includeufn;
130             break;
131         case 't':       /* write attribute values to /tmp files */
132             ++vals2tmp;
133             break;
134         case 'R':       /* don't automatically chase referrals */
135                 referrals = (int) LDAP_OPT_OFF;
136             break;
137         case 'A':       /* retrieve attribute names only -- no values */
138             ++attrsonly;
139             break;
140         case 'L':       /* print entries in LDIF format */
141             ++ldif;
142             /* fall through -- always allow binary when outputting LDIF */
143         case 'B':       /* allow binary values to be printed */
144             ++allow_binary;
145             break;
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;
153             } else {
154                 fprintf( stderr, "scope should be base, one, or sub\n" );
155                 usage( argv[ 0 ] );
156             }
157             break;
158
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;
168             } else {
169                 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
170                 usage( argv[ 0 ] );
171             }
172             break;
173             
174         case 'F':       /* field separator */
175             sep = strdup( optarg );
176             break;
177         case 'f':       /* input file */
178             infile = strdup( optarg );
179             break;
180         case 'h':       /* ldap host */
181             ldaphost = strdup( optarg );
182             break;
183         case 'b':       /* searchbase */
184             base = strdup( optarg );
185             break;
186         case 'D':       /* bind DN */
187             binddn = strdup( optarg );
188             break;
189         case 'p':       /* ldap port */
190             ldapport = atoi( optarg );
191             break;
192         case 'w':       /* bind password */
193             passwd = strdup( optarg );
194             break;
195         case 'l':       /* time limit */
196             timelimit = atoi( optarg );
197             break;
198         case 'z':       /* size limit */
199             sizelimit = atoi( optarg );
200             break;
201         case 'S':       /* sort attribute */
202             sortattr = strdup( optarg );
203             break;
204         default:
205             usage( argv[0] );
206         }
207     }
208
209     if ( argc - optind < 1 ) {
210         usage( argv[ 0 ] );
211     }
212     filtpattern = strdup( argv[ optind ] );
213     if ( argv[ optind + 1 ] == NULL ) {
214         attrs = NULL;
215     } else if ( sortattr == NULL || *sortattr == '\0' ) {
216         attrs = &argv[ optind + 1 ];
217     } else {
218         for ( i = optind + 1; i < argc; i++ ) {
219             if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
220                 break;
221             }
222         }
223         if ( i == argc ) {
224                 skipsortattr = 1;
225                 argv[ optind ] = sortattr;
226         } else {
227                 optind++;
228         }
229         attrs = &argv[ optind ];
230     }
231
232     if ( infile != NULL ) {
233         if ( infile[0] == '-' && infile[1] == '\0' ) {
234             fp = stdin;
235         } else if (( fp = fopen( infile, "r" )) == NULL ) {
236             perror( infile );
237             exit( 1 );
238         }
239     }
240
241     if ( verbose ) {
242         printf( "ldap_open( %s, %d )\n", ldaphost, ldapport );
243     }
244
245     if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
246         perror( ldaphost );
247         exit( 1 );
248     }
249
250         if (ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) == -1 ) {
251                 /* set option error */
252         }
253         if (ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) == -1 ) {
254                 /* set option error */
255         }
256         if (ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) == -1 ) {
257                 /* set option error */
258         }
259         if (ldap_set_option( ld, LDAP_OPT_REFERRALS, (void *) referrals ) == -1 ) {
260                 /* set option error */
261         }
262
263     if ( !kerberos ) {
264         authmethod = LDAP_AUTH_SIMPLE;
265     } else if ( kerberos == 1 ) {
266         authmethod = LDAP_AUTH_KRBV41;
267     } else {
268         authmethod =  LDAP_AUTH_KRBV4;
269     }
270     if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
271         ldap_perror( ld, "ldap_bind" );
272         exit( 1 );
273     }
274
275     if ( verbose ) {
276         printf( "filter pattern: %s\nreturning: ", filtpattern );
277         if ( attrs == NULL ) {
278             printf( "ALL" );
279         } else {
280             for ( i = 0; attrs[ i ] != NULL; ++i ) {
281                 printf( "%s ", attrs[ i ] );
282             }
283         }
284         putchar( '\n' );
285     }
286
287     if ( infile == NULL ) {
288         rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern, "" );
289     } else {
290         rc = 0;
291         first = 1;
292         while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
293             line[ strlen( line ) - 1 ] = '\0';
294             if ( !first ) {
295                 putchar( '\n' );
296             } else {
297                 first = 0;
298             }
299             rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern,
300                     line );
301         }
302         if ( fp != stdin ) {
303             fclose( fp );
304         }
305     }
306
307     ldap_unbind( ld );
308     exit( rc );
309
310         /* UNREACHABLE */
311         return(0);
312 }
313
314
315 static int dosearch(
316         LDAP    *ld,
317     char        *base,
318     int         scope,
319     char        **attrs,
320     int         attrsonly,
321     char        *filtpatt,
322     char        *value)
323 {
324     char                filter[ BUFSIZ ];
325     int                 rc, first, matches;
326     LDAPMessage         *res, *e;
327
328     sprintf( filter, filtpatt, value );
329
330     if ( verbose ) {
331         printf( "filter is: (%s)\n", filter );
332     }
333
334     if ( not ) {
335         return( LDAP_SUCCESS );
336     }
337
338     if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
339                 int ld_errno;
340                 ldap_perror( ld, "ldap_search" );
341
342                 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
343                 return( ld_errno );
344     }
345
346     matches = 0;
347     first = 1;
348     while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
349             == LDAP_RES_SEARCH_ENTRY ) {
350         matches++;
351         e = ldap_first_entry( ld, res );
352         if ( !first ) {
353             putchar( '\n' );
354         } else {
355             first = 0;
356         }
357         print_entry( ld, e, attrsonly );
358         ldap_msgfree( res );
359     }
360     if ( rc == -1 ) {
361         ldap_perror( ld, "ldap_result" );
362         return( rc );
363     }
364     if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
365         ldap_perror( ld, "ldap_search" );
366     }
367     if ( sortattr != NULL ) {
368             (void) ldap_sort_entries( ld, &res,
369                     ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
370             matches = 0;
371             first = 1;
372             for ( e = ldap_first_entry( ld, res ); e != NULLMSG;
373                     e = ldap_next_entry( ld, e ) ) {
374                 matches++;
375                 if ( !first ) {
376                     putchar( '\n' );
377                 } else {
378                     first = 0;
379                 }
380                 print_entry( ld, e, attrsonly );
381             }
382     }
383
384     if ( verbose ) {
385         printf( "%d matches\n", matches );
386     }
387
388     ldap_msgfree( res );
389     return( rc );
390 }
391
392
393 void print_entry(
394     LDAP        *ld,
395     LDAPMessage *entry,
396     int         attrsonly)
397 {
398     char                *a, *dn, *ufn, tmpfname[ 64 ];
399     int                 i, j, notascii;
400     BerElement          *ber;
401     struct berval       **bvals;
402     FILE                *tmpfp;
403
404     dn = ldap_get_dn( ld, entry );
405     if ( ldif ) {
406         write_ldif_value( "dn", dn, strlen( dn ));
407     } else {
408         printf( "%s\n", dn );
409     }
410     if ( includeufn ) {
411         ufn = ldap_dn2ufn( dn );
412         if ( ldif ) {
413             write_ldif_value( "ufn", ufn, strlen( ufn ));
414         } else {
415             printf( "%s\n", ufn );
416         }
417         free( ufn );
418     }
419     free( dn );
420
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 ) {
424             continue;
425         }
426         if ( attrsonly ) {
427             if ( ldif ) {
428                 write_ldif_value( a, "", 0 );
429             } else {
430                 printf( "%s\n", a );
431             }
432         } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
433             for ( i = 0; bvals[i] != NULL; i++ ) {
434                 if ( vals2tmp ) {
435                     sprintf( tmpfname, "/tmp/ldapsearch-%s-XXXXXX", a );
436                     tmpfp = NULL;
437
438                     if ( mktemp( tmpfname ) == NULL ) {
439                         perror( tmpfname );
440                     } else if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
441                         perror( tmpfname );
442                     } else if ( fwrite( bvals[ i ]->bv_val,
443                             bvals[ i ]->bv_len, 1, tmpfp ) == 0 ) {
444                         perror( tmpfname );
445                     } else if ( ldif ) {
446                         write_ldif_value( a, tmpfname, strlen( tmpfname ));
447                     } else {
448                         printf( "%s%s%s\n", a, sep, tmpfname );
449                     }
450
451                     if ( tmpfp != NULL ) {
452                         fclose( tmpfp );
453                     }
454                 } else {
455                     notascii = 0;
456                     if ( !allow_binary ) {
457                         for ( j = 0; (unsigned long) j < bvals[ i ]->bv_len; ++j ) {
458                             if ( !isascii( bvals[ i ]->bv_val[ j ] )) {
459                                 notascii = 1;
460                                 break;
461                             }
462                         }
463                     }
464
465                     if ( ldif ) {
466                         write_ldif_value( a, bvals[ i ]->bv_val,
467                                 bvals[ i ]->bv_len );
468                     } else {
469                         printf( "%s%s%s\n", a, sep,
470                                 notascii ? "NOT ASCII" : bvals[ i ]->bv_val );
471                     }
472                 }
473             }
474             ber_bvecfree( bvals );
475         }
476     }
477 }
478
479
480 int
481 write_ldif_value( char *type, char *value, unsigned long vallen )
482 {
483     char        *ldif;
484
485     if (( ldif = ldif_type_and_value( type, value, (int)vallen )) == NULL ) {
486         return( -1 );
487     }
488
489     fputs( ldif, stdout );
490     free( ldif );
491
492     return( 0 );
493 }