]> git.sur5r.net Git - openldap/blob - clients/tools/ldapsearch.c
Add LDAP version to comment
[openldap] / clients / tools / ldapsearch.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10
11 #include <ac/stdlib.h>
12
13 #include <ac/ctype.h>
14 #include <ac/signal.h>
15 #include <ac/string.h>
16 #include <ac/unistd.h>
17 #include <ac/errno.h>
18 #include <sys/stat.h>
19
20 #ifdef HAVE_FCNTL_H
21 #include <fcntl.h>
22 #endif
23 #ifdef HAVE_SYS_TYPES_H
24 #include <sys/types.h>
25 #endif
26 #ifdef HAVE_IO_H
27 #include <io.h>
28 #endif
29
30 #include <ldap.h>
31
32 #include "ldif.h"
33 #include "lutil.h"
34 #include "lutil_ldap.h"
35 #include "ldap_defaults.h"
36
37 static void
38 usage( const char *s )
39 {
40         fprintf( stderr,
41 "usage: %s [options] [filter [attributes...]]\nwhere:\n"
42 "  filter\tRFC-2254 compliant LDAP search filter\n"
43 "  attributes\twhitespace-separated list of attribute descriptions\n"
44 "    which may include:\n"
45 "      1.1   no attributes\n"
46 "      *     all user attributes\n"
47 "      +     all operational attributes\n"
48
49 "Search options:\n"
50 "  -a deref   one of never (default), always, search, or find\n"
51 "  -A         retrieve attribute names only (no values)\n"
52 "  -b basedn  base dn for search\n"
53 "  -F prefix  URL prefix for files (default: \"" LDAP_FILE_URI_PREFIX ")\n"
54 "  -l limit   time limit (in seconds) for search\n"
55 "  -L         print responses in LDIFv1 format\n"
56 "  -LL        print responses in LDIF format without comments\n"
57 "  -LLL       print responses in LDIF format without comments\n"
58 "             and version\n"
59 "  -s scope   one of base, one, or sub (search scope)\n"
60 "  -S attr    sort the results by attribute `attr'\n"
61 "  -t         write binary values to files in temporary directory\n"
62 "  -tt        write all values to files in temporary directory\n"
63 "  -T path    write files to directory specified by path (default:\n"
64 "             " LDAP_TMPDIR ")\n"
65 "  -u         include User Friendly entry names in the output\n"
66 "  -z limit   size limit (in entries) for search\n"
67
68 "Common options:\n"
69 "  -d level   set LDAP debugging level to `level'\n"
70 "  -D binddn  bind DN\n"
71 "  -f file    read operations from `file'\n"
72 "  -h host    LDAP server\n"
73 "  -H URI     LDAP Uniform Resource Indentifier(s)\n"
74 "  -I         use SASL Interactive mode\n"
75 "  -k         use Kerberos authentication\n"
76 "  -K         like -k, but do only step 1 of the Kerberos bind\n"
77 "  -M         enable Manage DSA IT control (-MM to make critical)\n"
78 "  -n         show what would be done but don't actually search\n"
79 "  -O props   SASL security properties\n"
80 "  -p port    port on LDAP server\n"
81 "  -P version procotol version (default: 3)\n"
82 "  -Q         use SASL Quiet mode\n"
83 "  -R realm   SASL realm\n"
84 "  -U authcid SASL authentication identity\n"
85 "  -v         run in verbose mode (diagnostics to standard output)\n"
86 "  -w passwd  bind passwd (for simple authentication)\n"
87 "  -W         prompt for bind passwd\n"
88 "  -x         Simple authentication\n"
89 "  -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
90 "  -Y mech    SASL mechanism\n"
91 "  -Z         Start TLS request (-ZZ to require successful response)\n"
92 , s );
93
94         exit( EXIT_FAILURE );
95 }
96
97 static void print_entry LDAP_P((
98         LDAP    *ld,
99         LDAPMessage     *entry,
100         int             attrsonly));
101
102 static void print_reference(
103         LDAP *ld,
104         LDAPMessage *reference );
105
106 static void print_extended(
107         LDAP *ld,
108         LDAPMessage *extended );
109
110 static void print_partial(
111         LDAP *ld,
112         LDAPMessage *partial );
113
114 static int print_result(
115         LDAP *ld,
116         LDAPMessage *result,
117         int search );
118
119 static void print_ctrls(
120         LDAPControl **ctrls );
121
122 static int write_ldif LDAP_P((
123         int type,
124         char *name,
125         char *value,
126         ber_len_t vallen ));
127
128 static int dosearch LDAP_P((
129         LDAP    *ld,
130         char    *base,
131         int             scope,
132         char    *filtpatt,
133         char    *value,
134         char    **attrs,
135         int             attrsonly,
136         LDAPControl **sctrls,
137         LDAPControl **cctrls,
138         struct timeval *timeout,
139         int     sizelimit ));
140
141 static char *tmpdir = NULL;
142 static char *urlpre = NULL;
143
144 static char *prog = NULL;
145 static char     *binddn = NULL;
146 static struct berval passwd = { 0, NULL };
147 static char     *base = NULL;
148 static char     *ldaphost = NULL;
149 static char *ldapuri = NULL;
150 static int      ldapport = 0;
151 #ifdef HAVE_CYRUS_SASL
152 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
153 static char     *sasl_realm = NULL;
154 static char     *sasl_authc_id = NULL;
155 static char     *sasl_authz_id = NULL;
156 static char     *sasl_mech = NULL;
157 static char     *sasl_secprops = NULL;
158 #endif
159 static int      use_tls = 0;
160 static char     *sortattr = NULL;
161 static int      verbose, not, includeufn, vals2tmp, ldif;
162
163 int
164 main( int argc, char **argv )
165 {
166         char            *infile, *filtpattern, **attrs = NULL, line[BUFSIZ];
167         FILE            *fp = NULL;
168         FILE            *log = NULL;
169         int                     rc, i, first, scope, deref, attrsonly, manageDSAit;
170         int                     referrals, timelimit, sizelimit, debug;
171         int             authmethod, version, want_bindpw;
172         LDAP            *ld = NULL;
173
174         infile = NULL;
175         debug = verbose = not = vals2tmp = referrals =
176                 attrsonly = manageDSAit = ldif = want_bindpw = 0;
177
178         lutil_log_initialize(argc, argv);
179
180         deref = sizelimit = timelimit = version = -1;
181
182         scope = LDAP_SCOPE_SUBTREE;
183         authmethod = -1;
184
185     prog = (prog = strrchr(argv[0], *LDAP_DIRSEP)) == NULL ? argv[0] : prog + 1;
186
187         while (( i = getopt( argc, argv, "Aa:b:F:f:Ll:S:s:T:tuz:"
188                 "Cd:D:h:H:IkKMnO:p:P:QR:U:vw:WxX:Y:Z")) != EOF )
189         {
190         switch( i ) {
191         /* Search Options */
192         case 'a':       /* set alias deref option */
193                 if ( strcasecmp( optarg, "never" ) == 0 ) {
194                 deref = LDAP_DEREF_NEVER;
195                 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
196                 deref = LDAP_DEREF_SEARCHING;
197                 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
198                 deref = LDAP_DEREF_FINDING;
199                 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
200                 deref = LDAP_DEREF_ALWAYS;
201                 } else {
202                 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
203                 usage( argv[ 0 ] );
204                 }
205                 break;
206         case 'A':       /* retrieve attribute names only -- no values */
207                 ++attrsonly;
208                 break;
209         case 'b': /* search base */
210                 base = strdup( optarg );
211                 break;
212         case 'f':       /* input file */
213                 if( infile != NULL ) {
214                         fprintf( stderr, "%s: -f previously specified\n", prog );
215                         return EXIT_FAILURE;
216                 }
217                 infile = strdup( optarg );
218                 break;
219         case 'F':       /* uri prefix */
220                 if( urlpre ) free( urlpre );
221                 urlpre = strdup( optarg );
222                 break;
223         case 'l':       /* time limit */
224                 timelimit = atoi( optarg );
225                 if( timelimit < 0 ) {
226                         fprintf( stderr, "%s: invalid timelimit (%d) specified\n",
227                                 prog, timelimit );
228                         return EXIT_FAILURE;
229                 }
230                 break;
231         case 'L':       /* print entries in LDIF format */
232                 ++ldif;
233                 break;
234         case 's':       /* search scope */
235                 if ( strcasecmp( optarg, "base" ) == 0 ) {
236                 scope = LDAP_SCOPE_BASE;
237                 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
238                 scope = LDAP_SCOPE_ONELEVEL;
239                 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
240                 scope = LDAP_SCOPE_SUBTREE;
241                 } else {
242                 fprintf( stderr, "scope should be base, one, or sub\n" );
243                 usage( argv[ 0 ] );
244                 }
245                 break;
246         case 'S':       /* sort attribute */
247                 sortattr = strdup( optarg );
248                 break;
249         case 'u':       /* include UFN */
250                 ++includeufn;
251                 break;
252         case 't':       /* write attribute values to TMPDIR files */
253                 ++vals2tmp;
254                 break;
255         case 'T':       /* tmpdir */
256                 if( tmpdir ) free( tmpdir );
257                 tmpdir = strdup( optarg );
258                 break;
259         case 'z':       /* size limit */
260                 sizelimit = atoi( optarg );
261                 break;
262
263         /* Common Options */
264         case 'C':
265                 referrals++;
266                 break;
267         case 'd':
268             debug |= atoi( optarg );
269             break;
270         case 'D':       /* bind DN */
271                 if( binddn != NULL ) {
272                         fprintf( stderr, "%s: -D previously specified\n", prog );
273                         return EXIT_FAILURE;
274                 }
275             binddn = strdup( optarg );
276             break;
277         case 'h':       /* ldap host */
278                 if( ldapuri != NULL ) {
279                         fprintf( stderr, "%s: -h incompatible with -H\n", prog );
280                         return EXIT_FAILURE;
281                 }
282                 if( ldaphost != NULL ) {
283                         fprintf( stderr, "%s: -h previously specified\n", prog );
284                         return EXIT_FAILURE;
285                 }
286             ldaphost = strdup( optarg );
287             break;
288         case 'H':       /* ldap URI */
289                 if( ldaphost != NULL ) {
290                         fprintf( stderr, "%s: -H incompatible with -h\n", prog );
291                         return EXIT_FAILURE;
292                 }
293                 if( ldapport ) {
294                         fprintf( stderr, "%s: -H incompatible with -p\n", prog );
295                         return EXIT_FAILURE;
296                 }
297                 if( ldapuri != NULL ) {
298                         fprintf( stderr, "%s: -H previously specified\n", prog );
299                         return EXIT_FAILURE;
300                 }
301             ldapuri = strdup( optarg );
302             break;
303         case 'I':
304 #ifdef HAVE_CYRUS_SASL
305                 if( version == LDAP_VERSION2 ) {
306                         fprintf( stderr, "%s: -I incompatible with version %d\n",
307                                 prog, version );
308                         return EXIT_FAILURE;
309                 }
310                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
311                         fprintf( stderr, "%s: incompatible previous "
312                                 "authentication choice\n",
313                                 prog );
314                         return EXIT_FAILURE;
315                 }
316                 authmethod = LDAP_AUTH_SASL;
317                 version = LDAP_VERSION3;
318                 sasl_flags = LDAP_SASL_INTERACTIVE;
319                 break;
320 #else
321                 fprintf( stderr, "%s: was not compiled with SASL support\n",
322                         prog );
323                 return( EXIT_FAILURE );
324 #endif
325         case 'k':       /* kerberos bind */
326 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
327                 if( version > LDAP_VERSION2 ) {
328                         fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
329                                 prog, version );
330                         return EXIT_FAILURE;
331                 }
332
333                 if( authmethod != -1 ) {
334                         fprintf( stderr, "%s: -k incompatible with previous "
335                                 "authentication choice\n", prog );
336                         return EXIT_FAILURE;
337                 }
338                         
339                 authmethod = LDAP_AUTH_KRBV4;
340 #else
341                 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
342                 return EXIT_FAILURE;
343 #endif
344             break;
345         case 'K':       /* kerberos bind, part one only */
346 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
347                 if( version > LDAP_VERSION2 ) {
348                         fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
349                                 prog, version );
350                         return EXIT_FAILURE;
351                 }
352                 if( authmethod != -1 ) {
353                         fprintf( stderr, "%s: incompatible with previous "
354                                 "authentication choice\n", prog );
355                         return EXIT_FAILURE;
356                 }
357
358                 authmethod = LDAP_AUTH_KRBV41;
359 #else
360                 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
361                 return( EXIT_FAILURE );
362 #endif
363             break;
364         case 'M':
365                 /* enable Manage DSA IT */
366                 if( version == LDAP_VERSION2 ) {
367                         fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
368                                 prog, version );
369                         return EXIT_FAILURE;
370                 }
371                 manageDSAit++;
372                 version = LDAP_VERSION3;
373                 break;
374         case 'n':       /* print deletes, don't actually do them */
375             ++not;
376             break;
377         case 'O':
378 #ifdef HAVE_CYRUS_SASL
379                 if( sasl_secprops != NULL ) {
380                         fprintf( stderr, "%s: -O previously specified\n", prog );
381                         return EXIT_FAILURE;
382                 }
383                 if( version == LDAP_VERSION2 ) {
384                         fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
385                                 prog, version );
386                         return EXIT_FAILURE;
387                 }
388                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
389                         fprintf( stderr, "%s: incompatible previous "
390                                 "authentication choice\n", prog );
391                         return EXIT_FAILURE;
392                 }
393                 authmethod = LDAP_AUTH_SASL;
394                 version = LDAP_VERSION3;
395                 sasl_secprops = strdup( optarg );
396 #else
397                 fprintf( stderr, "%s: not compiled with SASL support\n",
398                         prog );
399                 return( EXIT_FAILURE );
400 #endif
401                 break;
402         case 'p':
403                 if( ldapport ) {
404                         fprintf( stderr, "%s: -p previously specified\n", prog );
405                         return EXIT_FAILURE;
406                 }
407             ldapport = atoi( optarg );
408             break;
409         case 'P':
410                 switch( atoi(optarg) ) {
411                 case 2:
412                         if( version == LDAP_VERSION3 ) {
413                                 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
414                                         prog, version );
415                                 return EXIT_FAILURE;
416                         }
417                         version = LDAP_VERSION2;
418                         break;
419                 case 3:
420                         if( version == LDAP_VERSION2 ) {
421                                 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
422                                         prog, version );
423                                 return EXIT_FAILURE;
424                         }
425                         version = LDAP_VERSION3;
426                         break;
427                 default:
428                         fprintf( stderr, "%s: protocol version should be 2 or 3\n",
429                                 prog );
430                         usage( prog );
431                         return( EXIT_FAILURE );
432                 } break;
433         case 'Q':
434 #ifdef HAVE_CYRUS_SASL
435                 if( version == LDAP_VERSION2 ) {
436                         fprintf( stderr, "%s: -Q incompatible with version %d\n",
437                                 prog, version );
438                         return EXIT_FAILURE;
439                 }
440                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
441                         fprintf( stderr, "%s: incompatible previous "
442                                 "authentication choice\n",
443                                 prog );
444                         return EXIT_FAILURE;
445                 }
446                 authmethod = LDAP_AUTH_SASL;
447                 version = LDAP_VERSION3;
448                 sasl_flags = LDAP_SASL_QUIET;
449                 break;
450 #else
451                 fprintf( stderr, "%s: not compiled with SASL support\n",
452                         prog );
453                 return( EXIT_FAILURE );
454 #endif
455         case 'R':
456 #ifdef HAVE_CYRUS_SASL
457                 if( sasl_realm != NULL ) {
458                         fprintf( stderr, "%s: -R previously specified\n", prog );
459                         return EXIT_FAILURE;
460                 }
461                 if( version == LDAP_VERSION2 ) {
462                         fprintf( stderr, "%s: -R incompatible with version %d\n",
463                                 prog, version );
464                         return EXIT_FAILURE;
465                 }
466                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
467                         fprintf( stderr, "%s: incompatible previous "
468                                 "authentication choice\n",
469                                 prog );
470                         return EXIT_FAILURE;
471                 }
472                 authmethod = LDAP_AUTH_SASL;
473                 version = LDAP_VERSION3;
474                 sasl_realm = strdup( optarg );
475 #else
476                 fprintf( stderr, "%s: not compiled with SASL support\n",
477                         prog );
478                 return( EXIT_FAILURE );
479 #endif
480                 break;
481         case 'U':
482 #ifdef HAVE_CYRUS_SASL
483                 if( sasl_authc_id != NULL ) {
484                         fprintf( stderr, "%s: -U previously specified\n", prog );
485                         return EXIT_FAILURE;
486                 }
487                 if( version == LDAP_VERSION2 ) {
488                         fprintf( stderr, "%s: -U incompatible with version %d\n",
489                                 prog, version );
490                         return EXIT_FAILURE;
491                 }
492                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
493                         fprintf( stderr, "%s: incompatible previous "
494                                 "authentication choice\n",
495                                 prog );
496                         return EXIT_FAILURE;
497                 }
498                 authmethod = LDAP_AUTH_SASL;
499                 version = LDAP_VERSION3;
500                 sasl_authc_id = strdup( optarg );
501 #else
502                 fprintf( stderr, "%s: not compiled with SASL support\n",
503                         prog );
504                 return( EXIT_FAILURE );
505 #endif
506                 break;
507         case 'v':       /* verbose mode */
508             verbose++;
509             break;
510         case 'w':       /* password */
511             passwd.bv_val = strdup( optarg );
512                 {
513                         char* p;
514
515                         for( p = optarg; *p != '\0'; p++ ) {
516                                 *p = '\0';
517                         }
518                 }
519                 passwd.bv_len = strlen( passwd.bv_val );
520             break;
521         case 'W':
522                 want_bindpw++;
523                 break;
524         case 'Y':
525 #ifdef HAVE_CYRUS_SASL
526                 if( sasl_mech != NULL ) {
527                         fprintf( stderr, "%s: -Y previously specified\n", prog );
528                         return EXIT_FAILURE;
529                 }
530                 if( version == LDAP_VERSION2 ) {
531                         fprintf( stderr, "%s: -Y incompatible with version %d\n",
532                                 prog, version );
533                         return EXIT_FAILURE;
534                 }
535                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
536                         fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
537                         return EXIT_FAILURE;
538                 }
539                 authmethod = LDAP_AUTH_SASL;
540                 version = LDAP_VERSION3;
541                 sasl_mech = strdup( optarg );
542 #else
543                 fprintf( stderr, "%s: not compiled with SASL support\n",
544                         prog );
545                 return( EXIT_FAILURE );
546 #endif
547                 break;
548         case 'x':
549                 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
550                         fprintf( stderr, "%s: incompatible with previous "
551                                 "authentication choice\n", prog );
552                         return EXIT_FAILURE;
553                 }
554                 authmethod = LDAP_AUTH_SIMPLE;
555                 break;
556         case 'X':
557 #ifdef HAVE_CYRUS_SASL
558                 if( sasl_authz_id != NULL ) {
559                         fprintf( stderr, "%s: -X previously specified\n", prog );
560                         return EXIT_FAILURE;
561                 }
562                 if( version == LDAP_VERSION2 ) {
563                         fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
564                                 prog, version );
565                         return EXIT_FAILURE;
566                 }
567                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
568                         fprintf( stderr, "%s: -X incompatible with "
569                                 "authentication choice\n", prog );
570                         return EXIT_FAILURE;
571                 }
572                 authmethod = LDAP_AUTH_SASL;
573                 version = LDAP_VERSION3;
574                 sasl_authz_id = strdup( optarg );
575 #else
576                 fprintf( stderr, "%s: not compiled with SASL support\n",
577                         prog );
578                 return( EXIT_FAILURE );
579 #endif
580                 break;
581         case 'Z':
582 #ifdef HAVE_TLS
583                 if( version == LDAP_VERSION2 ) {
584                         fprintf( stderr, "%s: -Z incompatible with version %d\n",
585                                 prog, version );
586                         return EXIT_FAILURE;
587                 }
588                 version = LDAP_VERSION3;
589                 use_tls++;
590 #else
591                 fprintf( stderr, "%s: not compiled with TLS support\n",
592                         prog );
593                 return( EXIT_FAILURE );
594 #endif
595                 break;
596         default:
597                 fprintf( stderr, "%s: unrecognized option -%c\n",
598                         prog, optopt );
599                 usage( argv[0] );
600         }
601         }
602
603         if (version == -1) {
604                 version = LDAP_VERSION3;
605         }
606         if (authmethod == -1 && version > LDAP_VERSION2) {
607 #ifdef HAVE_CYRUS_SASL
608                 authmethod = LDAP_AUTH_SASL;
609 #else
610                 authmethod = LDAP_AUTH_SIMPLE;
611 #endif
612         }
613
614         if (( argc - optind < 1 ) ||
615                 ( *argv[optind] != '(' /*')'*/ &&
616                 ( strchr( argv[optind], '=' ) == NULL ) ) )
617         {
618                 filtpattern = "(objectclass=*)";
619         } else {
620                 filtpattern = strdup( argv[optind++] );
621         }
622
623         if ( (argv[optind] != NULL) && (sortattr == NULL || *sortattr == '\0') ) {
624                 attrs = &argv[optind];
625         }
626
627         if ( infile != NULL ) {
628                 if ( infile[0] == '-' && infile[1] == '\0' ) {
629                         fp = stdin;
630                 } else if (( fp = fopen( infile, "r" )) == NULL ) {
631                         perror( infile );
632                         return EXIT_FAILURE;
633                 }
634         }
635
636         if( tmpdir == NULL
637                 && (tmpdir = getenv("TMPDIR")) == NULL
638                 && (tmpdir = getenv("TMP")) == NULL
639                 && (tmpdir = getenv("TEMP")) == NULL )
640         {
641                 tmpdir = LDAP_TMPDIR;
642         }
643
644         if( urlpre == NULL ) {
645                 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
646
647                 if( urlpre == NULL ) {
648                         perror( "malloc" );
649                         return EXIT_FAILURE;
650                 }
651
652                 sprintf( urlpre, "file:///%s/",
653                         tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
654
655                 /* urlpre should be URLized.... */
656         }
657
658         if ( debug ) {
659                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
660                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
661                 }
662                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
663                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
664                 }
665                 ldif_debug = debug;
666         }
667
668 #ifdef SIGPIPE
669         (void) SIGNAL( SIGPIPE, SIG_IGN );
670 #endif
671
672         if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
673                 if ( verbose ) {
674                         fprintf( stderr, "ldap_init( %s, %d )\n",
675                                 ldaphost != NULL ? ldaphost : "<DEFAULT>",
676                                 ldapport );
677                 }
678
679                 ld = ldap_init( ldaphost, ldapport );
680                 if( ld == NULL ) {
681                         perror("ldapsearch: ldap_init");
682                         return EXIT_FAILURE;
683                 }
684
685         } else {
686                 if ( verbose ) {
687                         fprintf( stderr, "ldap_initialize( %s )\n",
688                                 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
689                 }
690
691                 rc = ldap_initialize( &ld, ldapuri );
692                 if( rc != LDAP_SUCCESS ) {
693                         fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
694                                 rc, ldap_err2string(rc) );
695                         return EXIT_FAILURE;
696                 }
697         }
698
699         if (deref != -1 &&
700                 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
701         {
702                 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
703                 return EXIT_FAILURE;
704         }
705         if (timelimit != -1 &&
706                 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
707         {
708                 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
709                 return EXIT_FAILURE;
710         }
711         if (sizelimit != -1 &&
712                 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
713         {
714                 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
715                 return EXIT_FAILURE;
716         }
717
718         /* referrals */
719         if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
720                 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
721         {
722                 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
723                         referrals ? "on" : "off" );
724                 return EXIT_FAILURE;
725         }
726
727         if (version == -1 ) {
728                 version = LDAP_VERSION3;
729         }
730
731         if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
732                 != LDAP_OPT_SUCCESS )
733         {
734                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
735                         version );
736                 return EXIT_FAILURE;
737         }
738
739
740         if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
741                 ldap_perror( ld, "ldap_start_tls" );
742                 if ( use_tls > 1 ) {
743                         return EXIT_FAILURE;
744                 }
745         }
746
747         if (want_bindpw) {
748                 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
749                 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
750         }
751
752         if ( authmethod == LDAP_AUTH_SASL ) {
753 #ifdef HAVE_CYRUS_SASL
754                 void *defaults;
755
756                 if( sasl_secprops != NULL ) {
757                         rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
758                                 (void *) sasl_secprops );
759                         
760                         if( rc != LDAP_OPT_SUCCESS ) {
761                                 fprintf( stderr,
762                                         "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
763                                         sasl_secprops );
764                                 return( EXIT_FAILURE );
765                         }
766                 }
767                 
768                 defaults = lutil_sasl_defaults( ld,
769                         sasl_mech,
770                         sasl_realm,
771                         sasl_authc_id,
772                         passwd.bv_val,
773                         sasl_authz_id );
774
775                 rc = ldap_sasl_interactive_bind_s( ld, binddn,
776                         sasl_mech, NULL, NULL,
777                         sasl_flags, lutil_sasl_interact, defaults );
778
779                 if( rc != LDAP_SUCCESS ) {
780                         ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
781                         return( EXIT_FAILURE );
782                 }
783 #else
784                 fprintf( stderr, "%s: not compiled with SASL support\n",
785                         prog, argv[0] );
786                 return( EXIT_FAILURE );
787 #endif
788         } else {
789                 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
790                                 != LDAP_SUCCESS ) {
791                         ldap_perror( ld, "ldap_bind" );
792                         return( EXIT_FAILURE );
793                 }
794         }
795
796         if ( manageDSAit ) {
797                 int err;
798                 LDAPControl c;
799                 LDAPControl *ctrls[2];
800                 ctrls[0] = &c;
801                 ctrls[1] = NULL;
802
803                 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
804                 c.ldctl_value.bv_val = NULL;
805                 c.ldctl_value.bv_len = 0;
806                 c.ldctl_iscritical = manageDSAit > 1;
807
808                 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
809
810                 if( err != LDAP_OPT_SUCCESS ) {
811                         fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
812                                 c.ldctl_iscritical ? "critical " : "" );
813                         if( c.ldctl_iscritical ) {
814                                 exit( EXIT_FAILURE );
815                         }
816                 }
817         }
818
819         if ( verbose ) {
820                 fprintf( stderr, "filter%s: %s\nrequesting: ",
821                         infile != NULL ? " pattern" : "",
822                         filtpattern );
823
824                 if ( attrs == NULL ) {
825                         fprintf( stderr, "ALL" );
826                 } else {
827                         for ( i = 0; attrs[ i ] != NULL; ++i ) {
828                                 fprintf( stderr, "%s ", attrs[ i ] );
829                         }
830                 }
831                 fprintf( stderr, "\n" );
832         }
833
834         if (ldif < 3 ) {
835                 printf( "version: %d\n\n", ldif ? 1 : 2 );
836         }
837
838         if (ldif < 2 ) {
839                 printf( "#\n# LDAPv%d\n# filter%s: %s\n# requesting: ",
840                         version,
841                         infile != NULL ? " pattern" : "",
842                         filtpattern );
843
844                 if ( attrs == NULL ) {
845                         printf( "ALL" );
846                 } else {
847                         for ( i = 0; attrs[ i ] != NULL; ++i ) {
848                                 printf( "%s ", attrs[ i ] );
849                         }
850                 }
851
852                 if ( manageDSAit ) {
853                         printf("\n# with manageDSAit %scontrol",
854                                 manageDSAit > 1 ? "critical " : "" );
855                 }
856
857                 printf( "\n#\n\n" );
858         }
859
860         if ( infile == NULL ) {
861                 rc = dosearch( ld, base, scope, NULL, filtpattern,
862                         attrs, attrsonly, NULL, NULL, NULL, -1 );
863
864         } else {
865                 rc = 0;
866                 first = 1;
867                 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) { 
868                         line[ strlen( line ) - 1 ] = '\0';
869                         if ( !first ) {
870                                 putchar( '\n' );
871                         } else {
872                                 first = 0;
873                         }
874                         rc = dosearch( ld, base, scope, filtpattern, line,
875                                 attrs, attrsonly, NULL, NULL, NULL, -1 );
876                 }
877                 if ( fp != stdin ) {
878                         fclose( fp );
879                 }
880         }
881
882         ldap_unbind( ld );
883         return( rc );
884 }
885
886
887 static int dosearch(
888         LDAP    *ld,
889         char    *base,
890         int             scope,
891         char    *filtpatt,
892         char    *value,
893         char    **attrs,
894         int             attrsonly,
895         LDAPControl **sctrls,
896         LDAPControl **cctrls,
897         struct timeval *timeout,
898         int sizelimit )
899 {
900         char            filter[ BUFSIZ ];
901         int                     rc;
902         int                     nresponses;
903         int                     nentries;
904         int                     nreferences;
905         int                     nextended;
906         int                     npartial;
907         LDAPMessage             *res, *msg;
908         ber_int_t       msgid;
909
910         if( filtpatt != NULL ) {
911                 sprintf( filter, filtpatt, value );
912
913                 if ( verbose ) {
914                         fprintf( stderr, "filter is: (%s)\n", filter );
915                 }
916
917                 if( ldif < 2 ) {
918                         printf( "#\n# filter: %s\n#\n", filter );
919                 }
920
921         } else {
922                 sprintf( filter, "%s", value );
923         }
924
925         if ( not ) {
926                 return LDAP_SUCCESS;
927         }
928
929         rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
930                 sctrls, cctrls, timeout, sizelimit, &msgid );
931
932         if( rc != LDAP_SUCCESS ) {
933                 fprintf( stderr, "%s: ldap_search_ext: %s (%d)\n",
934                         prog, ldap_err2string( rc ), rc );
935                 return( rc );
936         }
937
938         nresponses = nentries = nreferences = nextended = npartial = 0;
939
940         res = NULL;
941
942         while ((rc = ldap_result( ld, LDAP_RES_ANY,
943                 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
944                 NULL, &res )) > 0 )
945         {
946                 if( sortattr ) {
947                         (void) ldap_sort_entries( ld, &res,
948                                 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
949                 }
950
951                 for ( msg = ldap_first_message( ld, res );
952                         msg != NULL;
953                         msg = ldap_next_message( ld, msg ) )
954                 {
955                         if( nresponses++ ) putchar('\n');
956
957                         switch( ldap_msgtype( msg ) ) {
958                         case LDAP_RES_SEARCH_ENTRY:
959                                 nentries++;
960                                 print_entry( ld, msg, attrsonly );
961                                 break;
962
963                         case LDAP_RES_SEARCH_REFERENCE:
964                                 nreferences++;
965                                 print_reference( ld, msg );
966                                 break;
967
968                         case LDAP_RES_EXTENDED:
969                                 nextended++;
970                                 print_extended( ld, msg );
971
972                                 if( ldap_msgid( msg ) == 0 ) {
973                                         /* unsolicited extended operation */
974                                         goto done;
975                                 }
976                                 break;
977
978                         case LDAP_RES_EXTENDED_PARTIAL:
979                                 npartial++;
980                                 print_partial( ld, msg );
981                                 break;
982
983                         case LDAP_RES_SEARCH_RESULT:
984                                 rc = print_result( ld, msg, 1 );
985                                 goto done;
986                         }
987                 }
988
989                 ldap_msgfree( res );
990         }
991
992         if ( rc == -1 ) {
993                 ldap_perror( ld, "ldap_result" );
994                 return( rc );
995         }
996
997 done:
998         if ( ldif < 2 ) {
999                 printf( "\n# numResponses: %d\n", nresponses );
1000                 if( nentries ) printf( "# numEntries: %d\n", nentries );
1001                 if( nextended ) printf( "# numExtended: %d\n", nextended );
1002                 if( npartial ) printf( "# numPartial: %d\n", npartial );
1003                 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
1004         }
1005
1006         return( rc );
1007 }
1008
1009 static void
1010 print_entry(
1011         LDAP    *ld,
1012         LDAPMessage     *entry,
1013         int             attrsonly)
1014 {
1015         char            *a, *dn, *ufn;
1016         char    tmpfname[ 256 ];
1017         char    url[ 256 ];
1018         int                     i, rc;
1019         BerElement              *ber = NULL;
1020         struct berval   **bvals;
1021         LDAPControl **ctrls = NULL;
1022         FILE            *tmpfp;
1023
1024         dn = ldap_get_dn( ld, entry );
1025         ufn = NULL;
1026
1027         if ( ldif < 2 ) {
1028                 ufn = ldap_dn2ufn( dn );
1029                 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1030         }
1031         write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
1032
1033         rc = ldap_get_entry_controls( ld, entry, &ctrls );
1034
1035         if( rc != LDAP_SUCCESS ) {
1036                 fprintf(stderr, "print_entry: %d\n", rc );
1037                 ldap_perror( ld, "ldap_get_entry_controls" );
1038                 exit( EXIT_FAILURE );
1039         }
1040
1041         if( ctrls ) {
1042                 print_ctrls( ctrls );
1043                 ldap_controls_free( ctrls );
1044         }
1045
1046         if ( includeufn ) {
1047                 if( ufn == NULL ) {
1048                         ufn = ldap_dn2ufn( dn );
1049                 }
1050                 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1051         }
1052
1053         if( ufn != NULL ) ldap_memfree( ufn );
1054         ldap_memfree( dn );
1055
1056         for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1057                 a = ldap_next_attribute( ld, entry, ber ) )
1058         {
1059                 if ( attrsonly ) {
1060                         write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
1061
1062                 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1063                         for ( i = 0; bvals[i] != NULL; i++ ) {
1064                                 if ( vals2tmp > 1 || ( vals2tmp
1065                                         && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1066                                 {
1067                                         int tmpfd;
1068                                         /* write value to file */
1069                                         sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1070                                                 tmpdir, a );
1071                                         tmpfp = NULL;
1072
1073                                         if ( mktemp( tmpfname ) == NULL ) {
1074                                                 perror( tmpfname );
1075                                                 continue;
1076                                         }
1077
1078                                         if (( tmpfd = open( tmpfname, O_WRONLY|O_CREAT|O_EXCL, 0600 )) == -1 ) {
1079                                                 perror( tmpfname );
1080                                                 continue;
1081                                         }
1082
1083                                         if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1084                                                 perror( tmpfname );
1085                                                 continue;
1086                                         }
1087
1088                                         if ( fwrite( bvals[ i ]->bv_val,
1089                                                 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1090                                         {
1091                                                 perror( tmpfname );
1092                                                 fclose( tmpfp );
1093                                                 continue;
1094                                         }
1095
1096                                         fclose( tmpfp );
1097
1098                                         sprintf( url, "%s%s", urlpre,
1099                                                 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1100
1101                                         write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1102
1103                                 } else {
1104                                         write_ldif( LDIF_PUT_VALUE, a,
1105                                                 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1106                                 }
1107                         }
1108                         ber_bvecfree( bvals );
1109                 }
1110         }
1111
1112         if( ber != NULL ) {
1113                 ber_free( ber, 0 );
1114         }
1115 }
1116
1117 static void print_reference(
1118         LDAP *ld,
1119         LDAPMessage *reference )
1120 {
1121         int rc;
1122         char **refs = NULL;
1123         LDAPControl **ctrls;
1124
1125         if( ldif < 2 ) {
1126                 printf("# search reference\n");
1127         }
1128
1129         rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1130
1131         if( rc != LDAP_SUCCESS ) {
1132                 ldap_perror(ld, "ldap_parse_reference");
1133                 exit( EXIT_FAILURE );
1134         }
1135
1136         if( refs ) {
1137                 int i;
1138                 for( i=0; refs[i] != NULL; i++ ) {
1139                         write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1140                                 "ref", refs[i], strlen(refs[i]) );
1141                 }
1142                 ber_memvfree( (void **) refs );
1143         }
1144
1145         if( ctrls ) {
1146                 print_ctrls( ctrls );
1147                 ldap_controls_free( ctrls );
1148         }
1149 }
1150
1151 static void print_extended(
1152         LDAP *ld,
1153         LDAPMessage *extended )
1154 {
1155         int rc;
1156         char *retoid = NULL;
1157         struct berval *retdata = NULL;
1158
1159         if( ldif < 2 ) {
1160                 printf("# extended result response\n");
1161         }
1162
1163         rc = ldap_parse_extended_result( ld, extended,
1164                 &retoid, &retdata, 0 );
1165
1166         if( rc != LDAP_SUCCESS ) {
1167                 ldap_perror(ld, "ldap_parse_extended_result");
1168                 exit( EXIT_FAILURE );
1169         }
1170
1171         write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1172                 "extended", retoid, retoid ? strlen(retoid) : 0 );
1173         ber_memfree( retoid );
1174
1175         if(retdata) {
1176                 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1177                         "data", retdata->bv_val, retdata->bv_len );
1178                 ber_bvfree( retdata );
1179         }
1180
1181         print_result( ld, extended, 0 );
1182 }
1183
1184 static void print_partial(
1185         LDAP *ld,
1186         LDAPMessage *partial )
1187 {
1188         int rc;
1189         char *retoid = NULL;
1190         struct berval *retdata = NULL;
1191         LDAPControl **ctrls = NULL;
1192
1193         if( ldif < 2 ) {
1194                 printf("# extended partial response\n");
1195         }
1196
1197         rc = ldap_parse_extended_partial( ld, partial,
1198                 &retoid, &retdata, &ctrls, 0 );
1199
1200         if( rc != LDAP_SUCCESS ) {
1201                 ldap_perror(ld, "ldap_parse_extended_partial");
1202                 exit( EXIT_FAILURE );
1203         }
1204
1205         write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1206                 "partial", retoid, retoid ? strlen(retoid) : 0 );
1207
1208         ber_memfree( retoid );
1209
1210         if( retdata ) {
1211                 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1212                         "data", 
1213                         retdata->bv_val, retdata->bv_len );
1214
1215                 ber_bvfree( retdata );
1216         }
1217
1218         if( ctrls ) {
1219                 print_ctrls( ctrls );
1220                 ldap_controls_free( ctrls );
1221         }
1222 }
1223
1224 static int print_result(
1225         LDAP *ld,
1226         LDAPMessage *result, int search )
1227 {
1228         int rc;
1229         int err;
1230         char *matcheddn = NULL;
1231         char *text = NULL;
1232         char **refs = NULL;
1233         LDAPControl **ctrls = NULL;
1234
1235         if( search ) {
1236                 if ( ldif < 2 ) {
1237                         printf("# search result\n");
1238                 }
1239                 if ( ldif < 1 ) {
1240                         printf("%s: %d\n", "search", ldap_msgid(result) );
1241                 }
1242         }
1243
1244         rc = ldap_parse_result( ld, result,
1245                 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1246
1247         if( rc != LDAP_SUCCESS ) {
1248                 ldap_perror(ld, "ldap_parse_result");
1249                 exit( EXIT_FAILURE );
1250         }
1251
1252
1253         if( !ldif ) {
1254                 printf( "result: %d %s\n", err, ldap_err2string(err) );
1255
1256         } else if ( err != LDAP_SUCCESS ) {
1257                 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1258         }
1259
1260         if( matcheddn && *matcheddn ) {
1261                 if( !ldif ) {
1262                         write_ldif( LDIF_PUT_VALUE,
1263                                 "matchedDN", matcheddn, strlen(matcheddn) );
1264                 } else {
1265                         fprintf( stderr, "Matched DN: %s\n", matcheddn );
1266                 }
1267
1268                 ber_memfree( matcheddn );
1269         }
1270
1271         if( text && *text ) {
1272                 if( !ldif ) {
1273                         write_ldif( LDIF_PUT_TEXT, "text",
1274                                 text, strlen(text) );
1275                 } else {
1276                         fprintf( stderr, "Additional information: %s\n", text );
1277                 }
1278
1279                 ber_memfree( text );
1280         }
1281
1282         if( refs ) {
1283                 int i;
1284                 for( i=0; refs[i] != NULL; i++ ) {
1285                         if( !ldif ) {
1286                                 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1287                         } else {
1288                                 fprintf( stderr, "Referral: %s\n", refs[i] );
1289                         }
1290                 }
1291
1292                 ber_memvfree( (void **) refs );
1293         }
1294
1295         if( ctrls ) {
1296                 print_ctrls( ctrls );
1297                 ldap_controls_free( ctrls );
1298         }
1299
1300         return err;
1301 }
1302
1303 static void print_ctrls(
1304         LDAPControl **ctrls )
1305 {
1306         int i;
1307         for(i=0; ctrls[i] != NULL; i++ ) {
1308                 /* control: OID criticality base64value */
1309                 struct berval *b64 = NULL;
1310                 ber_len_t len;
1311                 char *str;
1312                         
1313                 len = strlen( ctrls[i]->ldctl_oid );
1314
1315                 /* add enough for space after OID and the critical value itself */
1316                 len += ctrls[i]->ldctl_iscritical
1317                         ? sizeof("true") : sizeof("false");
1318
1319                 /* convert to base64 */
1320                 if( ctrls[i]->ldctl_value.bv_len ) {
1321                         b64 = ber_memalloc( sizeof(struct berval) );
1322                         
1323                         b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1324                                 ctrls[i]->ldctl_value.bv_len ) + 1;
1325                         b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1326
1327                         b64->bv_len = lutil_b64_ntop(
1328                                 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1329                                 b64->bv_val, b64->bv_len );
1330                 }
1331
1332                 if( b64 ) {
1333                         len += 1 + b64->bv_len;
1334                 }
1335
1336                 str = malloc( len + 1 );
1337                 strcpy( str, ctrls[i]->ldctl_oid );
1338                 strcat( str, ctrls[i]->ldctl_iscritical
1339                         ? " true" : " false" );
1340
1341                 if( b64 ) {
1342                         strcat(str, " ");
1343                         strcat(str, b64->bv_val );
1344                 }
1345
1346                 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1347                         "control", str, len );
1348
1349                 free( str );
1350                 ber_bvfree( b64 );
1351         }
1352 }
1353
1354 static int
1355 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1356 {
1357         char    *ldif;
1358
1359         if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1360                 return( -1 );
1361         }
1362
1363         fputs( ldif, stdout );
1364         ber_memfree( ldif );
1365
1366         return( 0 );
1367 }