]> git.sur5r.net Git - openldap/blob - clients/tools/ldapsearch.c
Streamlined Kerberos Code.
[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 #ifdef HAVE_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 void print_entry LDAP_P((
55     LDAP        *ld,
56     LDAPMessage *entry,
57     int         attrsonly));
58
59 static int write_ldif_value LDAP_P((
60         char *type,
61         char *value,
62         unsigned long vallen ));
63
64 static int dosearch LDAP_P((
65         LDAP    *ld,
66     char        *base,
67     int         scope,
68     char        **attrs,
69     int         attrsonly,
70     char        *filtpatt,
71     char        *value));
72
73 static char     *binddn = NULL;
74 static char     *passwd = NULL;
75 static char     *base = NULL;
76 static char     *ldaphost = NULL;
77 static int      ldapport = 0;
78 static char     *sep = DEFSEP;
79 static char     *sortattr = NULL;
80 static int      skipsortattr = 0;
81 static int      verbose, not, includeufn, allow_binary, vals2tmp, ldif;
82
83 int
84 main( int argc, char **argv )
85 {
86     char                *infile, *filtpattern, **attrs, line[ BUFSIZ ];
87     FILE                *fp;
88     int                 rc, i, first, scope, deref, attrsonly;
89     int                 referrals, timelimit, sizelimit, authmethod;
90     LDAP                *ld;
91
92     infile = NULL;
93     deref = verbose = allow_binary = not = vals2tmp =
94             attrsonly = ldif = 0;
95     referrals = (int) LDAP_OPT_ON;
96     sizelimit = timelimit = 0;
97     scope = LDAP_SCOPE_SUBTREE;
98     authmethod = LDAP_AUTH_SIMPLE;
99
100     while (( i = getopt( argc, argv, "KknuvtRABLD:s:f:h:b:d:p:F:a:w:l:z:S:")) != EOF ) {
101         switch( i ) {
102         case 'n':       /* do Not do any searches */
103             ++not;
104             break;
105         case 'v':       /* verbose mode */
106             ++verbose;
107             break;
108         case 'd':
109 #ifdef LDAP_DEBUG
110             ldap_debug = lber_debug = atoi( optarg );   /* */
111 #else /* LDAP_DEBUG */
112             fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" );
113 #endif /* LDAP_DEBUG */
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         default:
206             usage( argv[0] );
207         }
208     }
209
210     if ( argc - optind < 1 ) {
211         usage( argv[ 0 ] );
212     }
213     filtpattern = strdup( argv[ optind ] );
214     if ( argv[ optind + 1 ] == NULL ) {
215         attrs = NULL;
216     } else if ( sortattr == NULL || *sortattr == '\0' ) {
217         attrs = &argv[ optind + 1 ];
218     } else {
219         for ( i = optind + 1; i < argc; i++ ) {
220             if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
221                 break;
222             }
223         }
224         if ( i == argc ) {
225                 skipsortattr = 1;
226                 argv[ optind ] = sortattr;
227         } else {
228                 optind++;
229         }
230         attrs = &argv[ optind ];
231     }
232
233     if ( infile != NULL ) {
234         if ( infile[0] == '-' && infile[1] == '\0' ) {
235             fp = stdin;
236         } else if (( fp = fopen( infile, "r" )) == NULL ) {
237             perror( infile );
238             exit( 1 );
239         }
240     }
241
242     if ( verbose ) {
243         printf( "ldap_open( %s, %d )\n", ldaphost, ldapport );
244     }
245
246     if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) {
247         perror( ldaphost );
248         exit( 1 );
249     }
250
251         if (ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) == -1 ) {
252                 /* set option error */
253         }
254         if (ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) == -1 ) {
255                 /* set option error */
256         }
257         if (ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) == -1 ) {
258                 /* set option error */
259         }
260         if (ldap_set_option( ld, LDAP_OPT_REFERRALS, (void *) referrals ) == -1 ) {
261                 /* set option error */
262         }
263
264     if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
265         ldap_perror( ld, "ldap_bind" );
266         exit( 1 );
267     }
268
269     if ( verbose ) {
270         printf( "filter pattern: %s\nreturning: ", filtpattern );
271         if ( attrs == NULL ) {
272             printf( "ALL" );
273         } else {
274             for ( i = 0; attrs[ i ] != NULL; ++i ) {
275                 printf( "%s ", attrs[ i ] );
276             }
277         }
278         putchar( '\n' );
279     }
280
281     if ( infile == NULL ) {
282         rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern, "" );
283     } else {
284         rc = 0;
285         first = 1;
286         while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
287             line[ strlen( line ) - 1 ] = '\0';
288             if ( !first ) {
289                 putchar( '\n' );
290             } else {
291                 first = 0;
292             }
293             rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern,
294                     line );
295         }
296         if ( fp != stdin ) {
297             fclose( fp );
298         }
299     }
300
301     ldap_unbind( ld );
302     exit( rc );
303
304         /* UNREACHABLE */
305         return(0);
306 }
307
308
309 static int dosearch(
310         LDAP    *ld,
311     char        *base,
312     int         scope,
313     char        **attrs,
314     int         attrsonly,
315     char        *filtpatt,
316     char        *value)
317 {
318     char                filter[ BUFSIZ ];
319     int                 rc, first, matches;
320     LDAPMessage         *res, *e;
321
322     sprintf( filter, filtpatt, value );
323
324     if ( verbose ) {
325         printf( "filter is: (%s)\n", filter );
326     }
327
328     if ( not ) {
329         return( LDAP_SUCCESS );
330     }
331
332     if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
333                 int ld_errno;
334                 ldap_perror( ld, "ldap_search" );
335
336                 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
337                 return( ld_errno );
338     }
339
340     matches = 0;
341     first = 1;
342     while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
343             == LDAP_RES_SEARCH_ENTRY ) {
344         matches++;
345         e = ldap_first_entry( ld, res );
346         if ( !first ) {
347             putchar( '\n' );
348         } else {
349             first = 0;
350         }
351         print_entry( ld, e, attrsonly );
352         ldap_msgfree( res );
353     }
354     if ( rc == -1 ) {
355         ldap_perror( ld, "ldap_result" );
356         return( rc );
357     }
358     if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
359         ldap_perror( ld, "ldap_search" );
360     }
361     if ( sortattr != NULL ) {
362             (void) ldap_sort_entries( ld, &res,
363                     ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
364             matches = 0;
365             first = 1;
366             for ( e = ldap_first_entry( ld, res ); e != NULLMSG;
367                     e = ldap_next_entry( ld, e ) ) {
368                 matches++;
369                 if ( !first ) {
370                     putchar( '\n' );
371                 } else {
372                     first = 0;
373                 }
374                 print_entry( ld, e, attrsonly );
375             }
376     }
377
378     if ( verbose ) {
379         printf( "%d matches\n", matches );
380     }
381
382     ldap_msgfree( res );
383     return( rc );
384 }
385
386
387 void print_entry(
388     LDAP        *ld,
389     LDAPMessage *entry,
390     int         attrsonly)
391 {
392     char                *a, *dn, *ufn, tmpfname[ 64 ];
393     int                 i, j, notascii;
394     BerElement          *ber;
395     struct berval       **bvals;
396     FILE                *tmpfp;
397
398     dn = ldap_get_dn( ld, entry );
399     if ( ldif ) {
400         write_ldif_value( "dn", dn, strlen( dn ));
401     } else {
402         printf( "%s\n", dn );
403     }
404     if ( includeufn ) {
405         ufn = ldap_dn2ufn( dn );
406         if ( ldif ) {
407             write_ldif_value( "ufn", ufn, strlen( ufn ));
408         } else {
409             printf( "%s\n", ufn );
410         }
411         free( ufn );
412     }
413     free( dn );
414
415     for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
416             a = ldap_next_attribute( ld, entry, ber ) ) {
417         if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
418             continue;
419         }
420         if ( attrsonly ) {
421             if ( ldif ) {
422                 write_ldif_value( a, "", 0 );
423             } else {
424                 printf( "%s\n", a );
425             }
426         } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
427             for ( i = 0; bvals[i] != NULL; i++ ) {
428                 if ( vals2tmp ) {
429                     sprintf( tmpfname, "/tmp/ldapsearch-%s-XXXXXX", a );
430                     tmpfp = NULL;
431
432                     if ( mktemp( tmpfname ) == NULL ) {
433                         perror( tmpfname );
434                     } else if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
435                         perror( tmpfname );
436                     } else if ( fwrite( bvals[ i ]->bv_val,
437                             bvals[ i ]->bv_len, 1, tmpfp ) == 0 ) {
438                         perror( tmpfname );
439                     } else if ( ldif ) {
440                         write_ldif_value( a, tmpfname, strlen( tmpfname ));
441                     } else {
442                         printf( "%s%s%s\n", a, sep, tmpfname );
443                     }
444
445                     if ( tmpfp != NULL ) {
446                         fclose( tmpfp );
447                     }
448                 } else {
449                     notascii = 0;
450                     if ( !allow_binary ) {
451                         for ( j = 0; (unsigned long) j < bvals[ i ]->bv_len; ++j ) {
452                             if ( !isascii( bvals[ i ]->bv_val[ j ] )) {
453                                 notascii = 1;
454                                 break;
455                             }
456                         }
457                     }
458
459                     if ( ldif ) {
460                         write_ldif_value( a, bvals[ i ]->bv_val,
461                                 bvals[ i ]->bv_len );
462                     } else {
463                         printf( "%s%s%s\n", a, sep,
464                                 notascii ? "NOT ASCII" : bvals[ i ]->bv_val );
465                     }
466                 }
467             }
468             ber_bvecfree( bvals );
469         }
470     }
471 }
472
473
474 int
475 write_ldif_value( char *type, char *value, unsigned long vallen )
476 {
477     char        *ldif;
478
479     if (( ldif = ldif_type_and_value( type, value, (int)vallen )) == NULL ) {
480         return( -1 );
481     }
482
483     fputs( ldif, stdout );
484     free( ldif );
485
486     return( 0 );
487 }