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