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