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