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