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