]> git.sur5r.net Git - openldap/blob - clients/tools/ldapsearch.c
s/NULL/'\0'/ where appropriate
[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                 {
198                         char* p;
199
200                         for( p = optarg; *p == '\0'; p++ ) {
201                                 *p = '*';
202                         }
203                 }
204             break;
205         case 'l':       /* time limit */
206             timelimit = atoi( optarg );
207             break;
208         case 'z':       /* size limit */
209             sizelimit = atoi( optarg );
210             break;
211         case 'S':       /* sort attribute */
212             sortattr = strdup( optarg );
213             break;
214         case 'W':
215                 want_bindpw++;
216                 break;
217         case 'P':
218                 switch( atoi( optarg ) )
219                 {
220                 case 2:
221                         version = LDAP_VERSION2;
222                         break;
223                 case 3:
224                         version = LDAP_VERSION3;
225                         break;
226                 default:
227                         fprintf( stderr, "protocol version should be 2 or 3\n" );
228                         usage( argv[0] );
229                 }
230                 break;
231         default:
232             usage( argv[0] );
233         }
234     }
235
236     if ( argc - optind < 1 ) {
237         usage( argv[ 0 ] );
238     }
239     filtpattern = strdup( argv[ optind ] );
240     if ( argv[ optind + 1 ] == NULL ) {
241         attrs = NULL;
242     } else if ( sortattr == NULL || *sortattr == '\0' ) {
243         attrs = &argv[ optind + 1 ];
244     } else {
245         for ( i = optind + 1; i < argc; i++ ) {
246             if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
247                 break;
248             }
249         }
250         if ( i == argc ) {
251                 skipsortattr = 1;
252                 argv[ optind ] = sortattr;
253         } else {
254                 optind++;
255         }
256         attrs = &argv[ optind ];
257     }
258
259     if ( infile != NULL ) {
260         if ( infile[0] == '-' && infile[1] == '\0' ) {
261             fp = stdin;
262         } else if (( fp = fopen( infile, "r" )) == NULL ) {
263             perror( infile );
264             return( EXIT_FAILURE );
265         }
266     }
267
268         if ( debug ) {
269                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
270                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
271                 }
272                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
273                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
274                 }
275                 ldif_debug = debug;
276         }
277
278 #ifdef SIGPIPE
279         (void) SIGNAL( SIGPIPE, SIG_IGN );
280 #endif
281
282     if ( verbose ) {
283         fprintf( stderr, "ldap_init( %s, %d )\n",
284                 (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
285                 ldapport );
286     }
287
288     if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
289         perror( "ldap_init" );
290         return( EXIT_FAILURE );
291     }
292
293         if (deref != -1 &&
294                 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
295         {
296                 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
297         }
298         if (timelimit != -1 &&
299                 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
300         {
301                 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
302         }
303         if (sizelimit != -1 &&
304                 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
305         {
306                 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
307         }
308         if (referrals != -1 &&
309                 ldap_set_option( ld, LDAP_OPT_REFERRALS,
310                                  (referrals ? LDAP_OPT_ON : LDAP_OPT_OFF) ) != LDAP_OPT_SUCCESS )
311         {
312                 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
313                         referrals ? "on" : "off" );
314         }
315
316         if (version != -1 &&
317                 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS )
318         {
319                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
320         }
321
322         if (want_bindpw)
323                 passwd = getpass("Enter LDAP Password: ");
324
325     if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
326         ldap_perror( ld, "ldap_bind" );
327         return( EXIT_FAILURE );
328     }
329
330     if ( verbose ) {
331         fprintf( stderr, "filter pattern: %s\nreturning: ", filtpattern );
332         if ( attrs == NULL ) {
333             printf( "ALL" );
334         } else {
335             for ( i = 0; attrs[ i ] != NULL; ++i ) {
336                 fprintf( stderr, "%s ", attrs[ i ] );
337             }
338         }
339         fprintf( stderr, "\n" );
340     }
341
342     if ( infile == NULL ) {
343         rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern, "" );
344     } else {
345         rc = 0;
346         first = 1;
347         while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
348             line[ strlen( line ) - 1 ] = '\0';
349             if ( !first ) {
350                 putchar( '\n' );
351             } else {
352                 first = 0;
353             }
354             rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern,
355                     line );
356         }
357         if ( fp != stdin ) {
358             fclose( fp );
359         }
360     }
361
362     ldap_unbind( ld );
363
364
365         return( rc );
366 }
367
368
369 static int dosearch(
370         LDAP    *ld,
371     char        *base,
372     int         scope,
373     char        **attrs,
374     int         attrsonly,
375     char        *filtpatt,
376     char        *value)
377 {
378     char                filter[ BUFSIZ ];
379     int                 rc, first, matches;
380     LDAPMessage         *res, *e;
381
382     sprintf( filter, filtpatt, value );
383
384     if ( verbose ) {
385         fprintf( stderr, "filter is: (%s)\n", filter );
386     }
387
388     if ( not ) {
389         return( LDAP_SUCCESS );
390     }
391
392     if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
393                 int ld_errno;
394                 ldap_perror( ld, "ldap_search" );
395
396                 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
397                 return( ld_errno );
398     }
399
400     matches = 0;
401     first = 1;
402     res = NULL;
403     while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
404             == LDAP_RES_SEARCH_ENTRY ) {
405         matches++;
406         e = ldap_first_entry( ld, res );
407         if ( !first ) {
408             putchar( '\n' );
409         } else {
410             first = 0;
411         }
412         print_entry( ld, e, attrsonly );
413         ldap_msgfree( res );
414         res = NULL;
415     }
416     if ( rc == -1 ) {
417         ldap_perror( ld, "ldap_result" );
418         return( rc );
419     }
420     if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
421         ldap_perror( ld, "ldap_search" );
422     }
423     if ( sortattr != NULL ) {
424             (void) ldap_sort_entries( ld, &res,
425                     ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
426             matches = 0;
427             first = 1;
428             for ( e = ldap_first_entry( ld, res ); e != NULL;
429                     e = ldap_next_entry( ld, e ) ) {
430                 matches++;
431                 if ( !first ) {
432                     putchar( '\n' );
433                 } else {
434                     first = 0;
435                 }
436                 print_entry( ld, e, attrsonly );
437             }
438     }
439
440     if ( verbose ) {
441         printf( "%d matches\n", matches );
442     }
443
444     ldap_msgfree( res );
445     return( rc );
446 }
447
448
449 void print_entry(
450     LDAP        *ld,
451     LDAPMessage *entry,
452     int         attrsonly)
453 {
454     char                *a, *dn, *ufn, tmpfname[ 64 ];
455     int                 i, j, notascii;
456     BerElement          *ber = NULL;
457     struct berval       **bvals;
458     FILE                *tmpfp;
459
460     dn = ldap_get_dn( ld, entry );
461     if ( ldif ) {
462         write_ldif_value( "dn", dn, strlen( dn ));
463     } else {
464         printf( "%s\n", dn );
465     }
466     if ( includeufn ) {
467         ufn = ldap_dn2ufn( dn );
468         if ( ldif ) {
469             write_ldif_value( "ufn", ufn, strlen( ufn ));
470         } else {
471             printf( "%s\n", ufn );
472         }
473         ldap_memfree( ufn );
474     }
475     ldap_memfree( dn );
476
477     for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
478             a = ldap_next_attribute( ld, entry, ber ) ) {
479         if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
480             continue;
481         }
482         if ( attrsonly ) {
483             if ( ldif ) {
484                 write_ldif_value( a, "", 0 );
485             } else {
486                 printf( "%s\n", a );
487             }
488         } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
489             for ( i = 0; bvals[i] != NULL; i++ ) {
490                 if ( vals2tmp ) {
491                     sprintf( tmpfname, "/tmp/ldapsearch-%s-XXXXXX", a );
492                     tmpfp = NULL;
493
494                     if ( mktemp( tmpfname ) == NULL ) {
495                         perror( tmpfname );
496                     } else if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
497                         perror( tmpfname );
498                     } else if ( fwrite( bvals[ i ]->bv_val,
499                             bvals[ i ]->bv_len, 1, tmpfp ) == 0 ) {
500                         perror( tmpfname );
501                     } else if ( ldif ) {
502                         write_ldif_value( a, tmpfname, strlen( tmpfname ));
503                     } else {
504                         printf( "%s%s%s\n", a, sep, tmpfname );
505                     }
506
507                     if ( tmpfp != NULL ) {
508                         fclose( tmpfp );
509                     }
510                 } else {
511                     notascii = 0;
512                     if ( !allow_binary ) {
513                         for ( j = 0; (unsigned long) j < bvals[ i ]->bv_len; ++j ) {
514                             if ( !isascii( bvals[ i ]->bv_val[ j ] )) {
515                                 notascii = 1;
516                                 break;
517                             }
518                         }
519                     }
520
521                     if ( ldif ) {
522                         write_ldif_value( a, bvals[ i ]->bv_val,
523                                 bvals[ i ]->bv_len );
524                     } else {
525                         printf( "%s%s%s\n", a, sep,
526                                 notascii ? "NOT ASCII" : bvals[ i ]->bv_val );
527                     }
528                 }
529             }
530             ber_bvecfree( bvals );
531         }
532     }
533
534         if( ber != NULL ) {
535                 ber_free( ber, 0 );
536         }
537 }
538
539
540 int
541 write_ldif_value( char *type, char *value, unsigned long vallen )
542 {
543     char        *ldif;
544
545     if (( ldif = ldif_type_and_value( type, value, (int)vallen )) == NULL ) {
546         return( -1 );
547     }
548
549     fputs( ldif, stdout );
550     ber_memfree( ldif );
551
552     return( 0 );
553 }