]> git.sur5r.net Git - openldap/blob - clients/tools/ldapsearch.c
a56e667fcf8b73c0c7b2c86869e3fd4157d4becd
[openldap] / clients / tools / ldapsearch.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 #include "portable.h"
7
8 #include <stdio.h>
9
10 #include <ac/stdlib.h>
11
12 #include <ac/ctype.h>
13 #include <ac/signal.h>
14 #include <ac/string.h>
15 #include <ac/unistd.h>
16
17 #ifdef HAVE_IO_H
18 #include <io.h>
19 #endif
20
21 #include <ldap.h>
22
23 #include "ldif.h"
24 #include "ldap_defaults.h"
25
26 #define DEFSEP          "="
27
28 static void
29 usage( const char *s )
30 {
31         fprintf( stderr,
32 "usage: %s [options] filter [attributes...]\nwhere:\n"
33 "       filter\tRFC-1558 compliant LDAP search filter\n"
34 "       attributes\twhitespace-separated list of attributes to retrieve\n"
35 "\t\t1.1                -- no attributes\n"
36 "\t\t*            -- all user attributes\n"
37 "\t\t+            -- all operational attributes\n"
38 "\t\tempty list -- all non-operational attributes\n"
39 "options:\n"
40 "       -n\t\tshow what would be done but don't actually search\n"
41 "       -v\t\trun in verbose mode (diagnostics to standard output)\n"
42 "       -t\t\twrite binary values to files in TMPDIR\n"
43 "       -tt\t\twrite all values to files in TMPDIR\n"
44 "       -T path\twrite files to directory specified by path (default: \"/tmp\")\n"
45 "       -V prefix\tURL prefix for files (default: \"file://tmp/\"\n"
46 "       -u\t\tinclude User Friendly entry names in the output\n"
47 "       -A\t\tretrieve attribute names only (no values)\n"
48 "       -B\t\tdo not suppress printing of binary values\n"
49 "       -F sep\tprint `sep' instead of `=' between attribute names and values\n"
50 "       -L\t\tprint entries in LDIF format (implies -B)\n"
51 "       -LL\t\tprint entries in LDIF format without comments\n"
52 "       -LLL\t\tprint entries in LDIF format without comments and version\n"
53 "       -M\t\tenable Manage DSA IT control (-MM to make critical)\n"
54 "       -R\t\tdo not automatically follow referrals\n"
55 "       -S attr\tsort the results by attribute `attr'\n"
56 "       -d level\tset LDAP debugging level to `level'\n"
57 "       -f file\tperform sequence of searches listed in `file'\n"
58 "       -b basedn\tbase dn for search\n"
59 "       -s scope\tone of base, one, or sub (search scope)\n"
60 "       -a deref\tone of never, always, search, or find (alias dereferencing)\n"
61 "       -l limit\ttime limit (in seconds) for search\n"
62 "       -z limit\tsize limit (in entries) for search\n"
63 "       -D binddn\tbind dn\n"
64 "       -w passwd\tbind passwd (for simple authentication)\n"
65 "       -W\t\tprompt for bind passwd\n"
66 #ifdef HAVE_KERBEROS
67 "       -k\t\tuse Kerberos instead of Simple Password authentication\n"
68 #endif
69 "       -h host\tldap server\n"
70 "       -p port\tport on ldap server\n"
71 "       -P version\tprocotol version (2 or 3)\n"
72 ,               s );
73
74         exit( EXIT_FAILURE );
75 }
76
77 static void print_entry LDAP_P((
78         LDAP    *ld,
79         LDAPMessage     *entry,
80         int             attrsonly));
81
82 static int write_ldif LDAP_P((
83         int type,
84         char *name,
85         char *value,
86         ber_len_t vallen ));
87
88 static int dosearch LDAP_P((
89         LDAP    *ld,
90         char    *base,
91         int             scope,
92         char    **attrs,
93         int             attrsonly,
94         char    *filtpatt,
95         char    *value));
96
97 #define TMPDIR "/tmp"
98 #define URLPRE "file:/tmp/"
99
100 static char *tmpdir = NULL;
101 static char *urlpre = NULL;
102
103 static char     *binddn = NULL;
104 static char     *passwd = NULL;
105 static char     *base = NULL;
106 static char     *ldaphost = NULL;
107 static int      ldapport = 0;
108 static char     *sep = DEFSEP;
109 static char     *sortattr = NULL;
110 static int      skipsortattr = 0;
111 static int      verbose, not, includeufn, binary, vals2tmp, ldif;
112
113 int
114 main( int argc, char **argv )
115 {
116         char            *infile, *filtpattern, **attrs, line[ BUFSIZ ];
117         FILE            *fp;
118         int                     rc, i, first, scope, deref, attrsonly, manageDSAit;
119         int                     referrals, timelimit, sizelimit, debug;
120         int             authmethod, version, want_bindpw;
121         LDAP            *ld;
122
123         infile = NULL;
124         debug = verbose = binary = not = vals2tmp =
125                 attrsonly = manageDSAit = ldif = want_bindpw = 0;
126
127         deref = referrals = sizelimit = timelimit = version = -1;
128
129         scope = LDAP_SCOPE_SUBTREE;
130         authmethod = LDAP_AUTH_SIMPLE;
131
132         while (( i = getopt( argc, argv,
133                 "WKknuvtMRABLD:s:f:h:b:d:P:p:F:a:w:l:z:S:T:V:")) != EOF )
134         {
135         switch( i ) {
136         case 'n':       /* do Not do any searches */
137                 ++not;
138                 break;
139         case 'v':       /* verbose mode */
140                 ++verbose;
141                 break;
142         case 'd':
143                 debug |= atoi( optarg );
144                 break;
145         case 'k':       /* use kerberos bind */
146 #ifdef HAVE_KERBEROS
147                 authmethod = LDAP_AUTH_KRBV4;
148 #else
149                 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
150 #endif
151                 break;
152         case 'K':       /* use kerberos bind, 1st part only */
153 #ifdef HAVE_KERBEROS
154                 authmethod = LDAP_AUTH_KRBV41;
155 #else
156                 fprintf (stderr, "%s was not compiled with Kerberos support\n", argv[0]);
157 #endif
158                 break;
159         case 'u':       /* include UFN */
160                 ++includeufn;
161                 break;
162         case 't':       /* write attribute values to /tmp files */
163                 ++vals2tmp;
164                 break;
165         case 'M':
166                 /* enable Manage DSA IT */
167                 manageDSAit++;
168                 break;
169         case 'R':       /* don't automatically chase referrals */
170                 referrals = (int) LDAP_OPT_OFF;
171                 break;
172         case 'A':       /* retrieve attribute names only -- no values */
173                 ++attrsonly;
174                 break;
175         case 'L':       /* print entries in LDIF format */
176                 ++ldif;
177                 /* fall through -- always allow binary when outputting LDIF */
178         case 'B':       /* allow binary values to be printed */
179                 ++binary;
180                 break;
181         case 's':       /* search scope */
182                 if ( strcasecmp( optarg, "base" ) == 0 ) {
183                 scope = LDAP_SCOPE_BASE;
184                 } else if ( strcasecmp( optarg, "one" ) == 0 ) {
185                 scope = LDAP_SCOPE_ONELEVEL;
186                 } else if ( strcasecmp( optarg, "sub" ) == 0 ) {
187                 scope = LDAP_SCOPE_SUBTREE;
188                 } else {
189                 fprintf( stderr, "scope should be base, one, or sub\n" );
190                 usage( argv[ 0 ] );
191                 }
192                 break;
193
194         case 'a':       /* set alias deref option */
195                 if ( strcasecmp( optarg, "never" ) == 0 ) {
196                 deref = LDAP_DEREF_NEVER;
197                 } else if ( strcasecmp( optarg, "search" ) == 0 ) {
198                 deref = LDAP_DEREF_SEARCHING;
199                 } else if ( strcasecmp( optarg, "find" ) == 0 ) {
200                 deref = LDAP_DEREF_FINDING;
201                 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
202                 deref = LDAP_DEREF_ALWAYS;
203                 } else {
204                 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
205                 usage( argv[ 0 ] );
206                 }
207                 break;
208                 
209         case 'T':       /* field separator */
210                 if( tmpdir ) free( tmpdir );
211                 tmpdir = strdup( optarg );
212                 break;
213         case 'V':       /* field separator */
214                 if( urlpre ) free( urlpre );
215                 urlpre = strdup( optarg );
216                 break;
217         case 'F':       /* field separator */
218                 sep = strdup( optarg );
219                 break;
220         case 'f':       /* input file */
221                 infile = strdup( optarg );
222                 break;
223         case 'h':       /* ldap host */
224                 ldaphost = strdup( optarg );
225                 break;
226         case 'b':       /* searchbase */
227                 base = strdup( optarg );
228                 break;
229         case 'D':       /* bind DN */
230                 binddn = strdup( optarg );
231                 break;
232         case 'p':       /* ldap port */
233                 ldapport = atoi( optarg );
234                 break;
235         case 'w':       /* bind password */
236                 passwd = strdup( optarg );
237                 {
238                         char* p;
239
240                         for( p = optarg; *p == '\0'; p++ ) {
241                                 *p = '*';
242                         }
243                 }
244                 break;
245         case 'l':       /* time limit */
246                 timelimit = atoi( optarg );
247                 break;
248         case 'z':       /* size limit */
249                 sizelimit = atoi( optarg );
250                 break;
251         case 'S':       /* sort attribute */
252                 sortattr = strdup( optarg );
253                 break;
254         case 'W':
255                 want_bindpw++;
256                 break;
257         case 'P':
258                 switch( atoi( optarg ) )
259                 {
260                 case 2:
261                         version = LDAP_VERSION2;
262                         break;
263                 case 3:
264                         version = LDAP_VERSION3;
265                         break;
266                 default:
267                         fprintf( stderr, "protocol version should be 2 or 3\n" );
268                         usage( argv[0] );
269                 }
270                 break;
271         default:
272                 usage( argv[0] );
273         }
274         }
275
276         if( authmethod != LDAP_AUTH_SIMPLE ) {
277                 if( version == LDAP_VERSION3 ) {
278                         fprintf(stderr, "Kerberos requires LDAPv2\n");
279                         return EXIT_FAILURE;
280                 }
281                 version = LDAP_VERSION2;
282         }
283
284         if( manageDSAit ) {
285                 if( version == LDAP_VERSION2 ) {
286                         fprintf(stderr, "manage DSA control requires LDAPv3\n");
287                         return EXIT_FAILURE;
288                 }
289                 version = LDAP_VERSION3;
290         }
291
292         if ( argc - optind < 1 ) {
293                 usage( argv[ 0 ] );
294         }
295
296         filtpattern = strdup( argv[ optind ] );
297
298         if ( argv[ optind + 1 ] == NULL ) {
299                 attrs = NULL;
300         } else if ( sortattr == NULL || *sortattr == '\0' ) {
301                 attrs = &argv[ optind + 1 ];
302         } else {
303                 for ( i = optind + 1; i < argc; i++ ) {
304                         if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
305                                 break;
306                         }
307                 }
308                 if ( i == argc ) {
309                         skipsortattr = 1;
310                         argv[ optind ] = sortattr;
311                 } else {
312                         optind++;
313                 }
314                 attrs = &argv[ optind ];
315         }
316
317         if ( infile != NULL ) {
318                 if ( infile[0] == '-' && infile[1] == '\0' ) {
319                         fp = stdin;
320                 } else if (( fp = fopen( infile, "r" )) == NULL ) {
321                         perror( infile );
322                         return EXIT_FAILURE;
323                 }
324         }
325
326         if( tmpdir == NULL
327                 && (tmpdir = getenv("TMPDIR")) == NULL
328                 && (tmpdir = getenv("TMP")) == NULL
329                 && (tmpdir = getenv("TEMP")) == NULL )
330         {
331                 tmpdir = "/tmp";
332         }
333
334         if( urlpre == NULL ) {
335                 urlpre = malloc( sizeof("file:///") + strlen(tmpdir) );
336
337                 if( urlpre == NULL ) {
338                         perror( "malloc" );
339                         return EXIT_FAILURE;
340                 }
341
342                 sprintf( urlpre, "file:///%s/",
343                         tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
344
345                 /* urlpre should be URLized.... */
346         }
347
348         if ( debug ) {
349                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
350                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
351                 }
352                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
353                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
354                 }
355                 ldif_debug = debug;
356         }
357
358 #ifdef SIGPIPE
359         (void) SIGNAL( SIGPIPE, SIG_IGN );
360 #endif
361
362         if ( verbose ) {
363                 fprintf( stderr,
364                         (ldapport ? "ldap_init( %s, %d )\n" : "ldap_init( %s, <DEFAULT> )\n"),
365                         (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
366                         ldapport );
367         }
368
369         if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
370                 perror( "ldap_init" );
371                 return( EXIT_FAILURE );
372         }
373
374         if (deref != -1 &&
375                 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
376         {
377                 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
378         }
379         if (timelimit != -1 &&
380                 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
381         {
382                 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
383         }
384         if (sizelimit != -1 &&
385                 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
386         {
387                 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
388         }
389         if (referrals != -1 &&
390                 ldap_set_option( ld, LDAP_OPT_REFERRALS,
391                                  (referrals ? LDAP_OPT_ON : LDAP_OPT_OFF) ) != LDAP_OPT_SUCCESS )
392         {
393                 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
394                         referrals ? "on" : "off" );
395         }
396
397         if (version != -1 &&
398                 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS )
399         {
400                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
401         }
402
403         if (want_bindpw) {
404                 passwd = getpass("Enter LDAP Password: ");
405         }
406
407         if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
408                 ldap_perror( ld, "ldap_bind" );
409                 return( EXIT_FAILURE );
410         }
411
412         if ( manageDSAit ) {
413                 int err;
414                 LDAPControl c;
415                 LDAPControl *ctrls[2];
416                 ctrls[0] = &c;
417                 ctrls[1] = NULL;
418
419                 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
420                 c.ldctl_value.bv_val = NULL;
421                 c.ldctl_value.bv_len = 0;
422                 c.ldctl_iscritical = manageDSAit > 1;
423
424                 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
425
426                 if( err != LDAP_OPT_SUCCESS ) {
427                         fprintf( stderr, "Could not set Manage DSA IT Control\n" );
428                         if( c.ldctl_iscritical ) {
429                                 exit( EXIT_FAILURE );
430                         }
431                 }
432         }
433
434         if ( verbose ) {
435                 fprintf( stderr, "filter%s: %s\nreturning: ",
436                         infile != NULL ? " pattern" : "",
437                         filtpattern );
438
439                 if ( attrs == NULL ) {
440                         fprintf( stderr, "ALL" );
441                 } else {
442                         for ( i = 0; attrs[ i ] != NULL; ++i ) {
443                                 fprintf( stderr, "%s ", attrs[ i ] );
444                         }
445                 }
446                 fprintf( stderr, "\n" );
447         }
448
449         if ( ldif ) {
450                 if (ldif < 3 ) {
451                         printf( "version: 1\n\n");
452                 }
453
454                 if (ldif < 2 ) {
455                         printf( "#\n# filter%s: %s\n# returning: ",
456                                 infile != NULL ? " pattern" : "",
457                                 filtpattern );
458
459                         if ( attrs == NULL ) {
460                                 printf( "ALL" );
461                         } else {
462                                 for ( i = 0; attrs[ i ] != NULL; ++i ) {
463                                         printf( "%s ", attrs[ i ] );
464                                 }
465                         }
466                         printf( "\n#\n\n" );
467                 }
468         }
469
470         if ( infile == NULL ) {
471                 rc = dosearch( ld, base, scope, attrs, attrsonly, NULL, filtpattern );
472
473         } else {
474                 rc = 0;
475                 first = 1;
476                 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
477                         line[ strlen( line ) - 1 ] = '\0';
478                         if ( !first ) {
479                                 putchar( '\n' );
480                         } else {
481                                 first = 0;
482                         }
483                         rc = dosearch( ld, base, scope, attrs, attrsonly,
484                                 filtpattern, line );
485                 }
486                 if ( fp != stdin ) {
487                         fclose( fp );
488                 }
489         }
490
491         ldap_unbind( ld );
492         return( rc );
493 }
494
495
496 static int dosearch(
497         LDAP    *ld,
498         char    *base,
499         int             scope,
500         char    **attrs,
501         int             attrsonly,
502         char    *filtpatt,
503         char    *value)
504 {
505         char            filter[ BUFSIZ ];
506         int                     rc, first, matches;
507         LDAPMessage             *res, *e;
508
509         if( filtpatt != NULL ) {
510                 sprintf( filter, filtpatt, value );
511
512                 if ( verbose ) {
513                         fprintf( stderr, "filter is: (%s)\n", filter );
514                 }
515
516                 if( ldif == 1 ) {
517                         printf( "#\n# filter: %s\n#\n", filter );
518                 }
519
520         } else {
521                 sprintf( filter, "%s", value );
522         }
523
524         if ( not ) {
525                 return( LDAP_SUCCESS );
526         }
527
528         if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
529                 int ld_errno;
530                 ldap_perror( ld, "ldap_search" );
531
532                 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
533                 return( ld_errno );
534         }
535
536         matches = 0;
537         first = 1;
538         res = NULL;
539         while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
540                 == LDAP_RES_SEARCH_ENTRY ) {
541         matches++;
542         e = ldap_first_entry( ld, res );
543         if ( !first ) {
544                 putchar( '\n' );
545         } else {
546                 first = 0;
547         }
548         print_entry( ld, e, attrsonly );
549         ldap_msgfree( res );
550         res = NULL;
551         }
552         if ( rc == -1 ) {
553         ldap_perror( ld, "ldap_result" );
554         return( rc );
555         }
556         if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
557                 ldap_perror( ld, "ldap_search" );
558         }
559         if ( sortattr != NULL ) {
560                 (void) ldap_sort_entries( ld, &res,
561                         ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
562                 matches = 0;
563                 first = 1;
564                 for ( e = ldap_first_entry( ld, res ); e != NULL;
565                         e = ldap_next_entry( ld, e ) ) {
566                 matches++;
567                 if ( !first ) {
568                         putchar( '\n' );
569                 } else {
570                         first = 0;
571                 }
572                 print_entry( ld, e, attrsonly );
573                 }
574         }
575
576         if ( verbose ) {
577                 printf( "%d matches\n", matches );
578         }
579
580         ldap_msgfree( res );
581         return( rc );
582 }
583
584
585 static void
586 print_entry(
587         LDAP    *ld,
588         LDAPMessage     *entry,
589         int             attrsonly)
590 {
591         char            *a, *dn, *ufn;
592         char    tmpfname[ 256 ];
593         char    url[ 256 ];
594         int                     i;
595         BerElement              *ber = NULL;
596         struct berval   **bvals;
597         FILE            *tmpfp;
598
599         dn = ldap_get_dn( ld, entry );
600         ufn = NULL;
601
602         if ( ldif == 1 ) {
603                 ufn = ldap_dn2ufn( dn );
604                 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, strlen( ufn ));
605         }
606         if ( ldif ) {
607                 write_ldif( LDIF_PUT_VALUE, "dn", dn, strlen( dn ));
608         } else {
609                 printf( "%s\n", dn );
610         }
611
612         if ( includeufn ) {
613                 if( ufn == NULL ) {
614                         ufn = ldap_dn2ufn( dn );
615                 }
616                 if ( ldif ) {
617                         write_ldif( LDIF_PUT_VALUE, "ufn", ufn, strlen( ufn ));
618                 } else {
619                         printf( "%s\n", ufn );
620                 }
621         }
622
623         if( ufn != NULL ) ldap_memfree( ufn );
624         ldap_memfree( dn );
625
626         for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
627                 a = ldap_next_attribute( ld, entry, ber ) )
628         {
629                 if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
630                         continue;
631                 }
632
633                 if ( attrsonly ) {
634                         if ( ldif ) {
635                                 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
636                         } else {
637                                 printf( "%s\n", a );
638                         }
639
640                 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
641                         for ( i = 0; bvals[i] != NULL; i++ ) {
642                                 if ( vals2tmp > 1 || ( vals2tmp
643                                         && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
644                                 {
645                                         /* write value to file */
646                                         sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
647                                                 tmpdir, a );
648                                         tmpfp = NULL;
649
650                                         if ( mktemp( tmpfname ) == NULL ) {
651                                                 perror( tmpfname );
652                                                 continue;
653                                         }
654
655                                         if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
656                                                 perror( tmpfname );
657                                                 continue;
658                                         }
659
660                                         if ( fwrite( bvals[ i ]->bv_val,
661                                                 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
662                                         {
663                                                 perror( tmpfname );
664                                                 fclose( tmpfp );
665                                                 continue;
666                                         }
667
668                                         fclose( tmpfp );
669
670                                         sprintf( url, "%s%s", urlpre,
671                                                 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
672
673                                         if ( ldif ) {
674                                                 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
675                                         } else {
676                                                 printf( "%s%s%s\n", a, sep, url );
677                                         }
678
679
680                                 } else {
681                                         if ( ldif ) {
682                                                 write_ldif( LDIF_PUT_VALUE, a,
683                                                         bvals[ i ]->bv_val, bvals[ i ]->bv_len );
684
685                                         } else {
686                                                 int notprint = !binary && !vals2tmp
687                                                         && ldif_is_not_printable( bvals[i]->bv_val,
688                                                                 bvals[i]->bv_len ); 
689                                                 printf( "%s%s", a, sep );
690                                                 puts( notprint ? "NOT PRINTABLE" : bvals[ i ]->bv_val );
691                                         }
692                                 }
693                         }
694                         ber_bvecfree( bvals );
695                 }
696         }
697
698         if( ber != NULL ) {
699                 ber_free( ber, 0 );
700         }
701 }
702
703
704 static int
705 write_ldif( int type, char *name, char *value, ber_len_t vallen )
706 {
707         char    *ldif;
708
709         if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
710                 return( -1 );
711         }
712
713         fputs( ldif, stdout );
714         ber_memfree( ldif );
715
716         return( 0 );
717 }