]> git.sur5r.net Git - openldap/blob - clients/tools/ldapsearch.c
a22c208109cba2c9edc7f8e36016028543dc9516
[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     deref = verbose = allow_binary = not = vals2tmp =
97             attrsonly = ldif = want_bindpw = 0;
98     referrals = (int) LDAP_OPT_ON;
99     sizelimit = timelimit = debug = 0;
100     scope = LDAP_SCOPE_SUBTREE;
101     authmethod = LDAP_AUTH_SIMPLE;
102         version = LDAP_VERSION2;
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 (ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) == -1 ) {
272                 /* set option error */
273         }
274         if (ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) == -1 ) {
275                 /* set option error */
276         }
277         if (ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) == -1 ) {
278                 /* set option error */
279         }
280         if (ldap_set_option( ld, LDAP_OPT_REFERRALS, (void *) referrals ) == -1 ) {
281                 /* set option error */
282         }
283
284         if (ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) == -1) {
285                 /* set option error */
286         }
287
288         if (want_bindpw)
289                 passwd = getpass("Enter LDAP Password: ");
290
291     if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
292         ldap_perror( ld, "ldap_bind" );
293         exit( 1 );
294     }
295
296     if ( verbose ) {
297         printf( "filter pattern: %s\nreturning: ", filtpattern );
298         if ( attrs == NULL ) {
299             printf( "ALL" );
300         } else {
301             for ( i = 0; attrs[ i ] != NULL; ++i ) {
302                 printf( "%s ", attrs[ i ] );
303             }
304         }
305         putchar( '\n' );
306     }
307
308     if ( infile == NULL ) {
309         rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern, "" );
310     } else {
311         rc = 0;
312         first = 1;
313         while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
314             line[ strlen( line ) - 1 ] = '\0';
315             if ( !first ) {
316                 putchar( '\n' );
317             } else {
318                 first = 0;
319             }
320             rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern,
321                     line );
322         }
323         if ( fp != stdin ) {
324             fclose( fp );
325         }
326     }
327
328     ldap_unbind( ld );
329     exit( rc );
330
331         /* UNREACHABLE */
332         return(0);
333 }
334
335
336 static int dosearch(
337         LDAP    *ld,
338     char        *base,
339     int         scope,
340     char        **attrs,
341     int         attrsonly,
342     char        *filtpatt,
343     char        *value)
344 {
345     char                filter[ BUFSIZ ];
346     int                 rc, first, matches;
347     LDAPMessage         *res, *e;
348
349     sprintf( filter, filtpatt, value );
350
351     if ( verbose ) {
352         printf( "filter is: (%s)\n", filter );
353     }
354
355     if ( not ) {
356         return( LDAP_SUCCESS );
357     }
358
359     if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
360                 int ld_errno;
361                 ldap_perror( ld, "ldap_search" );
362
363                 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
364                 return( ld_errno );
365     }
366
367     matches = 0;
368     first = 1;
369     while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
370             == LDAP_RES_SEARCH_ENTRY ) {
371         matches++;
372         e = ldap_first_entry( ld, res );
373         if ( !first ) {
374             putchar( '\n' );
375         } else {
376             first = 0;
377         }
378         print_entry( ld, e, attrsonly );
379         ldap_msgfree( res );
380     }
381     if ( rc == -1 ) {
382         ldap_perror( ld, "ldap_result" );
383         return( rc );
384     }
385     if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
386         ldap_perror( ld, "ldap_search" );
387     }
388     if ( sortattr != NULL ) {
389             (void) ldap_sort_entries( ld, &res,
390                     ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
391             matches = 0;
392             first = 1;
393             for ( e = ldap_first_entry( ld, res ); e != NULLMSG;
394                     e = ldap_next_entry( ld, e ) ) {
395                 matches++;
396                 if ( !first ) {
397                     putchar( '\n' );
398                 } else {
399                     first = 0;
400                 }
401                 print_entry( ld, e, attrsonly );
402             }
403     }
404
405     if ( verbose ) {
406         printf( "%d matches\n", matches );
407     }
408
409     ldap_msgfree( res );
410     return( rc );
411 }
412
413
414 void print_entry(
415     LDAP        *ld,
416     LDAPMessage *entry,
417     int         attrsonly)
418 {
419     char                *a, *dn, *ufn, tmpfname[ 64 ];
420     int                 i, j, notascii;
421     BerElement          *ber;
422     struct berval       **bvals;
423     FILE                *tmpfp;
424
425     dn = ldap_get_dn( ld, entry );
426     if ( ldif ) {
427         write_ldif_value( "dn", dn, strlen( dn ));
428     } else {
429         printf( "%s\n", dn );
430     }
431     if ( includeufn ) {
432         ufn = ldap_dn2ufn( dn );
433         if ( ldif ) {
434             write_ldif_value( "ufn", ufn, strlen( ufn ));
435         } else {
436             printf( "%s\n", ufn );
437         }
438         free( ufn );
439     }
440     free( dn );
441
442     for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
443             a = ldap_next_attribute( ld, entry, ber ) ) {
444         if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
445             continue;
446         }
447         if ( attrsonly ) {
448             if ( ldif ) {
449                 write_ldif_value( a, "", 0 );
450             } else {
451                 printf( "%s\n", a );
452             }
453         } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
454             for ( i = 0; bvals[i] != NULL; i++ ) {
455                 if ( vals2tmp ) {
456                     sprintf( tmpfname, "/tmp/ldapsearch-%s-XXXXXX", a );
457                     tmpfp = NULL;
458
459                     if ( mktemp( tmpfname ) == NULL ) {
460                         perror( tmpfname );
461                     } else if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
462                         perror( tmpfname );
463                     } else if ( fwrite( bvals[ i ]->bv_val,
464                             bvals[ i ]->bv_len, 1, tmpfp ) == 0 ) {
465                         perror( tmpfname );
466                     } else if ( ldif ) {
467                         write_ldif_value( a, tmpfname, strlen( tmpfname ));
468                     } else {
469                         printf( "%s%s%s\n", a, sep, tmpfname );
470                     }
471
472                     if ( tmpfp != NULL ) {
473                         fclose( tmpfp );
474                     }
475                 } else {
476                     notascii = 0;
477                     if ( !allow_binary ) {
478                         for ( j = 0; (unsigned long) j < bvals[ i ]->bv_len; ++j ) {
479                             if ( !isascii( bvals[ i ]->bv_val[ j ] )) {
480                                 notascii = 1;
481                                 break;
482                             }
483                         }
484                     }
485
486                     if ( ldif ) {
487                         write_ldif_value( a, bvals[ i ]->bv_val,
488                                 bvals[ i ]->bv_len );
489                     } else {
490                         printf( "%s%s%s\n", a, sep,
491                                 notascii ? "NOT ASCII" : bvals[ i ]->bv_val );
492                     }
493                 }
494             }
495             ber_bvecfree( bvals );
496         }
497     }
498 }
499
500
501 int
502 write_ldif_value( char *type, char *value, unsigned long vallen )
503 {
504     char        *ldif;
505
506     if (( ldif = ldif_type_and_value( type, value, (int)vallen )) == NULL ) {
507         return( -1 );
508     }
509
510     fputs( ldif, stdout );
511     free( ldif );
512
513     return( 0 );
514 }