]> git.sur5r.net Git - openldap/blob - clients/tools/ldapsearch.c
#define LDAP_LDIF to default to LDIF output
[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 LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
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 LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
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 LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
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 #ifdef LDAP_LDIF
277         /* no alternative format */
278         if( ldif < 1 ) ldif = 1;
279 #endif
280
281         if( authmethod != LDAP_AUTH_SIMPLE ) {
282                 if( version == LDAP_VERSION3 ) {
283                         fprintf(stderr, "Kerberos requires LDAPv2\n");
284                         return EXIT_FAILURE;
285                 }
286                 version = LDAP_VERSION2;
287         }
288
289         if( manageDSAit ) {
290                 if( version == LDAP_VERSION2 ) {
291                         fprintf(stderr, "manage DSA control requires LDAPv3\n");
292                         return EXIT_FAILURE;
293                 }
294                 version = LDAP_VERSION3;
295         }
296
297         if ( argc - optind < 1 ) {
298                 usage( argv[ 0 ] );
299         }
300
301         filtpattern = strdup( argv[ optind ] );
302
303         if ( argv[ optind + 1 ] == NULL ) {
304                 attrs = NULL;
305         } else if ( sortattr == NULL || *sortattr == '\0' ) {
306                 attrs = &argv[ optind + 1 ];
307         } else {
308                 for ( i = optind + 1; i < argc; i++ ) {
309                         if ( strcasecmp( argv[ i ], sortattr ) == 0 ) {
310                                 break;
311                         }
312                 }
313                 if ( i == argc ) {
314                         skipsortattr = 1;
315                         argv[ optind ] = sortattr;
316                 } else {
317                         optind++;
318                 }
319                 attrs = &argv[ optind ];
320         }
321
322         if ( infile != NULL ) {
323                 if ( infile[0] == '-' && infile[1] == '\0' ) {
324                         fp = stdin;
325                 } else if (( fp = fopen( infile, "r" )) == NULL ) {
326                         perror( infile );
327                         return EXIT_FAILURE;
328                 }
329         }
330
331         if( tmpdir == NULL
332                 && (tmpdir = getenv("TMPDIR")) == NULL
333                 && (tmpdir = getenv("TMP")) == NULL
334                 && (tmpdir = getenv("TEMP")) == NULL )
335         {
336                 tmpdir = "/tmp";
337         }
338
339         if( urlpre == NULL ) {
340                 urlpre = malloc( sizeof("file:///") + strlen(tmpdir) );
341
342                 if( urlpre == NULL ) {
343                         perror( "malloc" );
344                         return EXIT_FAILURE;
345                 }
346
347                 sprintf( urlpre, "file:///%s/",
348                         tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
349
350                 /* urlpre should be URLized.... */
351         }
352
353         if ( debug ) {
354                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
355                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
356                 }
357                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
358                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
359                 }
360                 ldif_debug = debug;
361         }
362
363 #ifdef SIGPIPE
364         (void) SIGNAL( SIGPIPE, SIG_IGN );
365 #endif
366
367         if ( verbose ) {
368                 fprintf( stderr,
369                         (ldapport ? "ldap_init( %s, %d )\n" : "ldap_init( %s, <DEFAULT> )\n"),
370                         (ldaphost != NULL) ? ldaphost : "<DEFAULT>",
371                         ldapport );
372         }
373
374         if (( ld = ldap_init( ldaphost, ldapport )) == NULL ) {
375                 perror( "ldap_init" );
376                 return( EXIT_FAILURE );
377         }
378
379         if (deref != -1 &&
380                 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
381         {
382                 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
383         }
384         if (timelimit != -1 &&
385                 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
386         {
387                 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
388         }
389         if (sizelimit != -1 &&
390                 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
391         {
392                 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
393         }
394         if (referrals != -1 &&
395                 ldap_set_option( ld, LDAP_OPT_REFERRALS,
396                                  (referrals ? LDAP_OPT_ON : LDAP_OPT_OFF) ) != LDAP_OPT_SUCCESS )
397         {
398                 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
399                         referrals ? "on" : "off" );
400         }
401
402         if (version != -1 &&
403                 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) != LDAP_OPT_SUCCESS )
404         {
405                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", version );
406         }
407
408         if (want_bindpw) {
409                 passwd = getpass("Enter LDAP Password: ");
410         }
411
412         if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) {
413                 ldap_perror( ld, "ldap_bind" );
414                 return( EXIT_FAILURE );
415         }
416
417         if ( manageDSAit ) {
418                 int err;
419                 LDAPControl c;
420                 LDAPControl *ctrls[2];
421                 ctrls[0] = &c;
422                 ctrls[1] = NULL;
423
424                 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
425                 c.ldctl_value.bv_val = NULL;
426                 c.ldctl_value.bv_len = 0;
427                 c.ldctl_iscritical = manageDSAit > 1;
428
429                 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, &ctrls );
430
431                 if( err != LDAP_OPT_SUCCESS ) {
432                         fprintf( stderr, "Could not set Manage DSA IT Control\n" );
433                         if( c.ldctl_iscritical ) {
434                                 exit( EXIT_FAILURE );
435                         }
436                 }
437         }
438
439         if ( verbose ) {
440                 fprintf( stderr, "filter%s: %s\nreturning: ",
441                         infile != NULL ? " pattern" : "",
442                         filtpattern );
443
444                 if ( attrs == NULL ) {
445                         fprintf( stderr, "ALL" );
446                 } else {
447                         for ( i = 0; attrs[ i ] != NULL; ++i ) {
448                                 fprintf( stderr, "%s ", attrs[ i ] );
449                         }
450                 }
451                 fprintf( stderr, "\n" );
452         }
453
454         if ( ldif ) {
455                 if (ldif < 3 ) {
456                         printf( "version: 1\n\n");
457                 }
458
459                 if (ldif < 2 ) {
460                         printf( "#\n# filter%s: %s\n# returning: ",
461                                 infile != NULL ? " pattern" : "",
462                                 filtpattern );
463
464                         if ( attrs == NULL ) {
465                                 printf( "ALL" );
466                         } else {
467                                 for ( i = 0; attrs[ i ] != NULL; ++i ) {
468                                         printf( "%s ", attrs[ i ] );
469                                 }
470                         }
471                         printf( "\n#\n\n" );
472                 }
473         }
474
475         if ( infile == NULL ) {
476                 rc = dosearch( ld, base, scope, attrs, attrsonly, NULL, filtpattern );
477
478         } else {
479                 rc = 0;
480                 first = 1;
481                 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) {
482                         line[ strlen( line ) - 1 ] = '\0';
483                         if ( !first ) {
484                                 putchar( '\n' );
485                         } else {
486                                 first = 0;
487                         }
488                         rc = dosearch( ld, base, scope, attrs, attrsonly,
489                                 filtpattern, line );
490                 }
491                 if ( fp != stdin ) {
492                         fclose( fp );
493                 }
494         }
495
496         ldap_unbind( ld );
497         return( rc );
498 }
499
500
501 static int dosearch(
502         LDAP    *ld,
503         char    *base,
504         int             scope,
505         char    **attrs,
506         int             attrsonly,
507         char    *filtpatt,
508         char    *value)
509 {
510         char            filter[ BUFSIZ ];
511         int                     rc, first, matches;
512         LDAPMessage             *res, *e;
513
514         if( filtpatt != NULL ) {
515                 sprintf( filter, filtpatt, value );
516
517                 if ( verbose ) {
518                         fprintf( stderr, "filter is: (%s)\n", filter );
519                 }
520
521                 if( ldif == 1 ) {
522                         printf( "#\n# filter: %s\n#\n", filter );
523                 }
524
525         } else {
526                 sprintf( filter, "%s", value );
527         }
528
529         if ( not ) {
530                 return( LDAP_SUCCESS );
531         }
532
533         if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) {
534                 int ld_errno;
535                 ldap_perror( ld, "ldap_search" );
536
537                 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
538                 return( ld_errno );
539         }
540
541         matches = 0;
542         first = 1;
543         res = NULL;
544         while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res ))
545                 == LDAP_RES_SEARCH_ENTRY ) {
546         matches++;
547         e = ldap_first_entry( ld, res );
548         if ( !first ) {
549                 putchar( '\n' );
550         } else {
551                 first = 0;
552         }
553         print_entry( ld, e, attrsonly );
554         ldap_msgfree( res );
555         res = NULL;
556         }
557         if ( rc == -1 ) {
558         ldap_perror( ld, "ldap_result" );
559         return( rc );
560         }
561         if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) {
562                 ldap_perror( ld, "ldap_search" );
563         }
564         if ( sortattr != NULL ) {
565                 (void) ldap_sort_entries( ld, &res,
566                         ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
567                 matches = 0;
568                 first = 1;
569                 for ( e = ldap_first_entry( ld, res ); e != NULL;
570                         e = ldap_next_entry( ld, e ) ) {
571                 matches++;
572                 if ( !first ) {
573                         putchar( '\n' );
574                 } else {
575                         first = 0;
576                 }
577                 print_entry( ld, e, attrsonly );
578                 }
579         }
580
581         if ( verbose ) {
582                 printf( "%d matches\n", matches );
583         }
584
585         ldap_msgfree( res );
586         return( rc );
587 }
588
589
590 static void
591 print_entry(
592         LDAP    *ld,
593         LDAPMessage     *entry,
594         int             attrsonly)
595 {
596         char            *a, *dn, *ufn;
597         char    tmpfname[ 256 ];
598         char    url[ 256 ];
599         int                     i;
600         BerElement              *ber = NULL;
601         struct berval   **bvals;
602         FILE            *tmpfp;
603
604         dn = ldap_get_dn( ld, entry );
605         ufn = NULL;
606
607         if ( ldif == 1 ) {
608                 ufn = ldap_dn2ufn( dn );
609                 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, strlen( ufn ));
610         }
611         if ( ldif ) {
612                 write_ldif( LDIF_PUT_VALUE, "dn", dn, strlen( dn ));
613         } else {
614                 printf( "%s\n", dn );
615         }
616
617         if ( includeufn ) {
618                 if( ufn == NULL ) {
619                         ufn = ldap_dn2ufn( dn );
620                 }
621                 if ( ldif ) {
622                         write_ldif( LDIF_PUT_VALUE, "ufn", ufn, strlen( ufn ));
623                 } else {
624                         printf( "%s\n", ufn );
625                 }
626         }
627
628         if( ufn != NULL ) ldap_memfree( ufn );
629         ldap_memfree( dn );
630
631         for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
632                 a = ldap_next_attribute( ld, entry, ber ) )
633         {
634                 if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) {
635                         continue;
636                 }
637
638                 if ( attrsonly ) {
639                         if ( ldif ) {
640                                 write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
641                         } else {
642                                 printf( "%s\n", a );
643                         }
644
645                 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
646                         for ( i = 0; bvals[i] != NULL; i++ ) {
647                                 if ( vals2tmp > 1 || ( vals2tmp
648                                         && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
649                                 {
650                                         /* write value to file */
651                                         sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
652                                                 tmpdir, a );
653                                         tmpfp = NULL;
654
655                                         if ( mktemp( tmpfname ) == NULL ) {
656                                                 perror( tmpfname );
657                                                 continue;
658                                         }
659
660                                         if (( tmpfp = fopen( tmpfname, "w")) == NULL ) {
661                                                 perror( tmpfname );
662                                                 continue;
663                                         }
664
665                                         if ( fwrite( bvals[ i ]->bv_val,
666                                                 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
667                                         {
668                                                 perror( tmpfname );
669                                                 fclose( tmpfp );
670                                                 continue;
671                                         }
672
673                                         fclose( tmpfp );
674
675                                         sprintf( url, "%s%s", urlpre,
676                                                 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
677
678                                         if ( ldif ) {
679                                                 write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
680                                         } else {
681                                                 printf( "%s%s%s\n", a, sep, url );
682                                         }
683
684
685                                 } else {
686                                         if ( ldif ) {
687                                                 write_ldif( LDIF_PUT_VALUE, a,
688                                                         bvals[ i ]->bv_val, bvals[ i ]->bv_len );
689
690                                         } else {
691                                                 int notprint = !binary && !vals2tmp
692                                                         && ldif_is_not_printable( bvals[i]->bv_val,
693                                                                 bvals[i]->bv_len ); 
694                                                 printf( "%s%s", a, sep );
695                                                 puts( notprint ? "NOT PRINTABLE" : bvals[ i ]->bv_val );
696                                         }
697                                 }
698                         }
699                         ber_bvecfree( bvals );
700                 }
701         }
702
703         if( ber != NULL ) {
704                 ber_free( ber, 0 );
705         }
706 }
707
708
709 static int
710 write_ldif( int type, char *name, char *value, ber_len_t vallen )
711 {
712         char    *ldif;
713
714         if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
715                 return( -1 );
716         }
717
718         fputs( ldif, stdout );
719         ber_memfree( ldif );
720
721         return( 0 );
722 }