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