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