]> git.sur5r.net Git - openldap/blob - clients/tools/ldapsearch.c
add define of service name (shared in main and nt_svc, that's why it's here)
[openldap] / clients / tools / ldapsearch.c
1 #include "portable.h"
2
3 #include <stdio.h>
4
5 #include <ac/stdlib.h>
6
7 #include <ac/ctype.h>
8 #include <ac/signal.h>
9 #include <ac/string.h>
10 #include <ac/unistd.h>
11
12 #include <lber.h>
13 #include <ldap.h>
14 #include <ldif.h>
15
16 #define DEFSEP          "="
17
18
19 static void
20 usage( 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_API_FEATURE_X_OPENLDAP_V2_REFERRALS
35     fprintf( stderr, "    -R\t\tdo not automatically follow referrals\n" );
36 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_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     fprintf( stderr, "    -W\t\tprompt for bind passwd\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     fprintf( stderr, "    -P version\tprocotol version (2 or 3)\n" );
55     exit( EXIT_FAILURE );
56 }
57
58 static void print_entry LDAP_P((
59     LDAP        *ld,
60     LDAPMessage *entry,
61     int         attrsonly));
62
63 static int write_ldif_value LDAP_P((
64         char *type,
65         char *value,
66         unsigned long vallen ));
67
68 static int dosearch LDAP_P((
69         LDAP    *ld,
70     char        *base,
71     int         scope,
72     char        **attrs,
73     int         attrsonly,
74     char        *filtpatt,
75     char        *value));
76
77 static char     *binddn = NULL;
78 static char     *passwd = NULL;
79 static char     *base = NULL;
80 static char     *ldaphost = NULL;
81 static int      ldapport = 0;
82 static char     *sep = DEFSEP;
83 static char     *sortattr = NULL;
84 static int      skipsortattr = 0;
85 static int      verbose, not, includeufn, allow_binary, vals2tmp, ldif;
86
87 int
88 main( int argc, char **argv )
89 {
90     char                *infile, *filtpattern, **attrs, line[ BUFSIZ ];
91     FILE                *fp;
92     int                 rc, i, first, scope, deref, attrsonly;
93     int                 referrals, timelimit, sizelimit, debug;
94         int             authmethod, version, want_bindpw;
95     LDAP                *ld;
96
97     infile = NULL;
98     debug = verbose = allow_binary = not = vals2tmp =
99             attrsonly = ldif = want_bindpw = 0;
100
101         deref = referrals = sizelimit = timelimit = version = -1;
102
103         scope = LDAP_SCOPE_SUBTREE;
104     authmethod = LDAP_AUTH_SIMPLE;
105
106     while (( i = getopt( argc, argv, "WKknuvtRABLD:s:f:h:b:d:P:p:F:a:w:l:z:S:")) != EOF ) {
107         switch( i ) {
108         case 'n':       /* do Not do any searches */
109             ++not;
110             break;
111         case 'v':       /* verbose mode */
112             ++verbose;
113             break;
114         case 'd':
115             debug |= atoi( optarg );
116             break;
117         case 'k':       /* use kerberos bind */
118 #ifdef HAVE_KERBEROS
119                 authmethod = LDAP_AUTH_KRBV4;
120 #else
121                 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
122 #endif
123             break;
124         case 'K':       /* use kerberos bind, 1st part only */
125 #ifdef HAVE_KERBEROS
126                 authmethod = LDAP_AUTH_KRBV41;
127 #else
128                 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
129 #endif
130             break;
131         case 'u':       /* include UFN */
132             ++includeufn;
133             break;
134         case 't':       /* write attribute values to /tmp files */
135             ++vals2tmp;
136             break;
137         case 'R':       /* don't automatically chase referrals */
138                 referrals = (int) LDAP_OPT_OFF;
139             break;
140         case 'A':       /* retrieve attribute names only -- no values */
141             ++attrsonly;
142             break;
143         case 'L':       /* print entries in LDIF format */
144             ++ldif;
145             /* fall through -- always allow binary when outputting LDIF */
146         case 'B':       /* allow binary values to be printed */
147             ++allow_binary;
148             break;
149         case 's':       /* search scope */
150             if ( strcasecmp( optarg, "base" ) == 0 ) {
151                 scope = LDAP_SCOPE_BASE;
152             } else if ( strcasecmp( optarg, "one" ) == 0 ) {
153                 scope = LDAP_SCOPE_ONELEVEL;
154             } else if ( strcasecmp( optarg, "sub" ) == 0 ) {
155                 scope = LDAP_SCOPE_SUBTREE;
156             } else {
157                 fprintf( stderr, "scope should be base, one, or sub\n" );
158                 usage( argv[ 0 ] );
159             }
160             break;
161
162         case 'a':       /* set alias deref option */
163             if ( strcasecmp( optarg, "never" ) == 0 ) {
164                 deref = LDAP_DEREF_NEVER;
165             } else if ( strcasecmp( optarg, "search" ) == 0 ) {
166                 deref = LDAP_DEREF_SEARCHING;
167             } else if ( strcasecmp( optarg, "find" ) == 0 ) {
168                 deref = LDAP_DEREF_FINDING;
169             } else if ( strcasecmp( optarg, "always" ) == 0 ) {
170                 deref = LDAP_DEREF_ALWAYS;
171             } else {
172                 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
173                 usage( argv[ 0 ] );
174             }
175             break;
176             
177         case 'F':       /* field separator */
178             sep = strdup( optarg );
179             break;
180         case 'f':       /* input file */
181             infile = strdup( optarg );
182             break;
183         case 'h':       /* ldap host */
184             ldaphost = strdup( optarg );
185             break;
186         case 'b':       /* searchbase */
187             base = strdup( optarg );
188             break;
189         case 'D':       /* bind DN */
190             binddn = strdup( optarg );
191             break;
192         case 'p':       /* ldap port */
193             ldapport = atoi( optarg );
194             break;
195         case 'w':       /* bind password */
196             passwd = strdup( optarg );
197             break;
198         case 'l':       /* time limit */
199             timelimit = atoi( optarg );
200             break;
201         case 'z':       /* size limit */
202             sizelimit = atoi( optarg );
203             break;
204         case 'S':       /* sort attribute */
205             sortattr = strdup( optarg );
206             break;
207         case 'W':
208                 want_bindpw++;
209                 break;
210         case 'P':
211                 switch( atoi( optarg ) )
212                 {
213                 case 2:
214                         version = LDAP_VERSION2;
215                         break;
216                 case 3:
217                         version = LDAP_VERSION3;
218                         break;
219                 default:
220                         fprintf( stderr, "protocol version should be 2 or 3\n" );
221                         usage( argv[0] );
222                 }
223                 break;
224         default:
225             usage( argv[0] );
226         }
227     }
228
229     if ( argc - optind < 1 ) {
230         usage( argv[ 0 ] );
231     }
232     filtpattern = strdup( argv[ optind ] );
233     if ( argv[ optind + 1 ] == NULL ) {
234         attrs = NULL;
235     } else if ( sortattr == NULL || *sortattr == '\0' ) {
236         attrs = &argv[ optind + 1 ];
237     } else {
238         for ( i = optind + 1; i < argc; i++ ) {
239             if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
240                 break;
241             }
242         }
243         if ( i == argc ) {
244                 skipsortattr = 1;
245                 argv[ optind ] = sortattr;
246         } else {
247                 optind++;
248         }
249         attrs = &argv[ optind ];
250     }
251
252     if ( infile != NULL ) {
253         if ( infile[0] == '-' && infile[1] == '\0' ) {
254             fp = stdin;
255         } else if (( fp = fopen( infile, "r" )) == NULL ) {
256             perror( infile );
257             return( EXIT_FAILURE );
258         }
259     }
260
261         if ( debug ) {
262                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_ERROR ) {
263                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
264                 }
265                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_ERROR ) {
266                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
267                 }
268                 ldif_debug = debug;
269         }
270
271 #ifdef SIGPIPE
272         (void) SIGNAL( SIGPIPE, SIG_IGN );
273 #endif
274
275     if ( verbose ) {
276         fprintf( stderr, "ldap_init( %s, %d )\n",
277                 (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
278                 ldapport );
279     }
280
281     if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
282         perror( "ldap_init" );
283         return( EXIT_FAILURE );
284     }
285
286         if (deref != -1 &&
287                 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) == LDAP_OPT_ERROR )
288         {
289                 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
290         }
291         if (timelimit != -1 &&
292                 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) == LDAP_OPT_ERROR )
293         {
294                 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
295         }
296         if (sizelimit != -1 &&
297                 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) == LDAP_OPT_ERROR )
298         {
299                 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
300         }
301         if (referrals != -1 &&
302                 ldap_set_option( ld, LDAP_OPT_REFERRALS,
303                                  (referrals ? LDAP_OPT_ON : LDAP_OPT_OFF) ) == LDAP_OPT_ERROR )
304         {
305                 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
306                         referrals ? "on" : "off" );
307         }
308
309         if (version != -1 &&
310                 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) == LDAP_OPT_ERROR)
311         {
312                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
313         }
314
315         if (want_bindpw)
316                 passwd = getpass("Enter LDAP Password: ");
317
318     if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
319         ldap_perror( ld, "ldap_bind" );
320         return( EXIT_FAILURE );
321     }
322
323     if ( verbose ) {
324         fprintf( stderr, "filter pattern: %s\nreturning: ", filtpattern );
325         if ( attrs == NULL ) {
326             printf( "ALL" );
327         } else {
328             for ( i = 0; attrs[ i ] != NULL; ++i ) {
329                 fprintf( stderr, "%s ", attrs[ i ] );
330             }
331         }
332         fprintf( stderr, "\n" );
333     }
334
335     if ( infile == NULL ) {
336         rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern, "" );
337     } else {
338         rc = 0;
339         first = 1;
340         while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
341             line[ strlen( line ) - 1 ] = '\0';
342             if ( !first ) {
343                 putchar( '\n' );
344             } else {
345                 first = 0;
346             }
347             rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern,
348                     line );
349         }
350         if ( fp != stdin ) {
351             fclose( fp );
352         }
353     }
354
355     ldap_unbind( ld );
356
357
358         return( rc );
359 }
360
361
362 static int dosearch(
363         LDAP    *ld,
364     char        *base,
365     int         scope,
366     char        **attrs,
367     int         attrsonly,
368     char        *filtpatt,
369     char        *value)
370 {
371     char                filter[ BUFSIZ ];
372     int                 rc, first, matches;
373     LDAPMessage         *res, *e;
374
375     sprintf( filter, filtpatt, value );
376
377     if ( verbose ) {
378         fprintf( stderr, "filter is: (%s)\n", filter );
379     }
380
381     if ( not ) {
382         return( LDAP_SUCCESS );
383     }
384
385     if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
386                 int ld_errno;
387                 ldap_perror( ld, "ldap_search" );
388
389                 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
390                 return( ld_errno );
391     }
392
393     matches = 0;
394     first = 1;
395     res = NULL;
396     while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
397             == LDAP_RES_SEARCH_ENTRY ) {
398         matches++;
399         e = ldap_first_entry( ld, res );
400         if ( !first ) {
401             putchar( '\n' );
402         } else {
403             first = 0;
404         }
405         print_entry( ld, e, attrsonly );
406         ldap_msgfree( res );
407         res = NULL;
408     }
409     if ( rc == -1 ) {
410         ldap_perror( ld, "ldap_result" );
411         return( rc );
412     }
413     if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
414         ldap_perror( ld, "ldap_search" );
415     }
416     if ( sortattr != NULL ) {
417             (void) ldap_sort_entries( ld, &res,
418                     ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
419             matches = 0;
420             first = 1;
421             for ( e = ldap_first_entry( ld, res ); e != NULL;
422                     e = ldap_next_entry( ld, e ) ) {
423                 matches++;
424                 if ( !first ) {
425                     putchar( '\n' );
426                 } else {
427                     first = 0;
428                 }
429                 print_entry( ld, e, attrsonly );
430             }
431     }
432
433     if ( verbose ) {
434         printf( "%d matches\n", matches );
435     }
436
437     ldap_msgfree( res );
438     return( rc );
439 }
440
441
442 void print_entry(
443     LDAP        *ld,
444     LDAPMessage *entry,
445     int         attrsonly)
446 {
447     char                *a, *dn, *ufn, tmpfname[ 64 ];
448     int                 i, j, notascii;
449     BerElement          *ber = NULL;
450     struct berval       **bvals;
451     FILE                *tmpfp;
452
453     dn = ldap_get_dn( ld, entry );
454     if ( ldif ) {
455         write_ldif_value( "dn", dn, strlen( dn ));
456     } else {
457         printf( "%s\n", dn );
458     }
459     if ( includeufn ) {
460         ufn = ldap_dn2ufn( dn );
461         if ( ldif ) {
462             write_ldif_value( "ufn", ufn, strlen( ufn ));
463         } else {
464             printf( "%s\n", ufn );
465         }
466         ldap_memfree( ufn );
467     }
468     ldap_memfree( dn );
469
470     for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
471             a = ldap_next_attribute( ld, entry, ber ) ) {
472         if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
473             continue;
474         }
475         if ( attrsonly ) {
476             if ( ldif ) {
477                 write_ldif_value( a, "", 0 );
478             } else {
479                 printf( "%s\n", a );
480             }
481         } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
482             for ( i = 0; bvals[i] != NULL; i++ ) {
483                 if ( vals2tmp ) {
484                     sprintf( tmpfname, "/tmp/ldapsearch-%s-XXXXXX", a );
485                     tmpfp = NULL;
486
487                     if ( mktemp( tmpfname ) == NULL ) {
488                         perror( tmpfname );
489                     } else if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
490                         perror( tmpfname );
491                     } else if ( fwrite( bvals[ i ]->bv_val,
492                             bvals[ i ]->bv_len, 1, tmpfp ) == 0 ) {
493                         perror( tmpfname );
494                     } else if ( ldif ) {
495                         write_ldif_value( a, tmpfname, strlen( tmpfname ));
496                     } else {
497                         printf( "%s%s%s\n", a, sep, tmpfname );
498                     }
499
500                     if ( tmpfp != NULL ) {
501                         fclose( tmpfp );
502                     }
503                 } else {
504                     notascii = 0;
505                     if ( !allow_binary ) {
506                         for ( j = 0; (unsigned long) j < bvals[ i ]->bv_len; ++j ) {
507                             if ( !isascii( bvals[ i ]->bv_val[ j ] )) {
508                                 notascii = 1;
509                                 break;
510                             }
511                         }
512                     }
513
514                     if ( ldif ) {
515                         write_ldif_value( a, bvals[ i ]->bv_val,
516                                 bvals[ i ]->bv_len );
517                     } else {
518                         printf( "%s%s%s\n", a, sep,
519                                 notascii ? "NOT ASCII" : bvals[ i ]->bv_val );
520                     }
521                 }
522             }
523             ber_bvecfree( bvals );
524         }
525     }
526
527         if( ber != NULL ) {
528                 ber_free( ber, 0 );
529         }
530 }
531
532
533 int
534 write_ldif_value( char *type, char *value, unsigned long vallen )
535 {
536     char        *ldif;
537
538     if (( ldif = ldif_type_and_value( type, value, (int)vallen )) == NULL ) {
539         return( -1 );
540     }
541
542     fputs( ldif, stdout );
543     ber_memfree( ldif );
544
545     return( 0 );
546 }