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