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