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