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