]> git.sur5r.net Git - openldap/blob - clients/tools/ldapsearch.c
Made dnNormalize() do Unicode normalization and case folding.
[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# filter%s: %s\n# requesting: ",
840                         infile != NULL ? " pattern" : "",
841                         filtpattern );
842
843                 if ( attrs == NULL ) {
844                         printf( "ALL" );
845                 } else {
846                         for ( i = 0; attrs[ i ] != NULL; ++i ) {
847                                 printf( "%s ", attrs[ i ] );
848                         }
849                 }
850
851                 if ( manageDSAit ) {
852                         printf("\n# with manageDSAit %scontrol",
853                                 manageDSAit > 1 ? "critical " : "" );
854                 }
855
856                 printf( "\n#\n\n" );
857         }
858
859         if ( infile == NULL ) {
860                 rc = dosearch( ld, base, scope, NULL, filtpattern,
861                         attrs, attrsonly, NULL, NULL, NULL, -1 );
862
863         } else {
864                 rc = 0;
865                 first = 1;
866                 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) { 
867                         line[ strlen( line ) - 1 ] = '\0';
868                         if ( !first ) {
869                                 putchar( '\n' );
870                         } else {
871                                 first = 0;
872                         }
873                         rc = dosearch( ld, base, scope, filtpattern, line,
874                                 attrs, attrsonly, NULL, NULL, NULL, -1 );
875                 }
876                 if ( fp != stdin ) {
877                         fclose( fp );
878                 }
879         }
880
881         ldap_unbind( ld );
882         return( rc );
883 }
884
885
886 static int dosearch(
887         LDAP    *ld,
888         char    *base,
889         int             scope,
890         char    *filtpatt,
891         char    *value,
892         char    **attrs,
893         int             attrsonly,
894         LDAPControl **sctrls,
895         LDAPControl **cctrls,
896         struct timeval *timeout,
897         int sizelimit )
898 {
899         char            filter[ BUFSIZ ];
900         int                     rc;
901         int                     nresponses;
902         int                     nentries;
903         int                     nreferences;
904         int                     nextended;
905         int                     npartial;
906         LDAPMessage             *res, *msg;
907         ber_int_t       msgid;
908
909         if( filtpatt != NULL ) {
910                 sprintf( filter, filtpatt, value );
911
912                 if ( verbose ) {
913                         fprintf( stderr, "filter is: (%s)\n", filter );
914                 }
915
916                 if( ldif < 2 ) {
917                         printf( "#\n# filter: %s\n#\n", filter );
918                 }
919
920         } else {
921                 sprintf( filter, "%s", value );
922         }
923
924         if ( not ) {
925                 return LDAP_SUCCESS;
926         }
927
928         rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
929                 sctrls, cctrls, timeout, sizelimit, &msgid );
930
931         if( rc != LDAP_SUCCESS ) {
932                 fprintf( stderr, "%s: ldap_search_ext: %s (%d)\n",
933                         prog, ldap_err2string( rc ), rc );
934                 return( rc );
935         }
936
937         nresponses = nentries = nreferences = nextended = npartial = 0;
938
939         res = NULL;
940
941         while ((rc = ldap_result( ld, LDAP_RES_ANY,
942                 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
943                 NULL, &res )) > 0 )
944         {
945                 if( sortattr ) {
946                         (void) ldap_sort_entries( ld, &res,
947                                 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
948                 }
949
950                 for ( msg = ldap_first_message( ld, res );
951                         msg != NULL;
952                         msg = ldap_next_message( ld, msg ) )
953                 {
954                         if( nresponses++ ) putchar('\n');
955
956                         switch( ldap_msgtype( msg ) ) {
957                         case LDAP_RES_SEARCH_ENTRY:
958                                 nentries++;
959                                 print_entry( ld, msg, attrsonly );
960                                 break;
961
962                         case LDAP_RES_SEARCH_REFERENCE:
963                                 nreferences++;
964                                 print_reference( ld, msg );
965                                 break;
966
967                         case LDAP_RES_EXTENDED:
968                                 nextended++;
969                                 print_extended( ld, msg );
970
971                                 if( ldap_msgid( msg ) == 0 ) {
972                                         /* unsolicited extended operation */
973                                         goto done;
974                                 }
975                                 break;
976
977                         case LDAP_RES_EXTENDED_PARTIAL:
978                                 npartial++;
979                                 print_partial( ld, msg );
980                                 break;
981
982                         case LDAP_RES_SEARCH_RESULT:
983                                 rc = print_result( ld, msg, 1 );
984                                 goto done;
985                         }
986                 }
987
988                 ldap_msgfree( res );
989         }
990
991         if ( rc == -1 ) {
992                 ldap_perror( ld, "ldap_result" );
993                 return( rc );
994         }
995
996 done:
997         if ( ldif < 2 ) {
998                 printf( "\n# numResponses: %d\n", nresponses );
999                 if( nentries ) printf( "# numEntries: %d\n", nentries );
1000                 if( nextended ) printf( "# numExtended: %d\n", nextended );
1001                 if( npartial ) printf( "# numPartial: %d\n", npartial );
1002                 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
1003         }
1004
1005         return( rc );
1006 }
1007
1008 static void
1009 print_entry(
1010         LDAP    *ld,
1011         LDAPMessage     *entry,
1012         int             attrsonly)
1013 {
1014         char            *a, *dn, *ufn;
1015         char    tmpfname[ 256 ];
1016         char    url[ 256 ];
1017         int                     i, rc;
1018         BerElement              *ber = NULL;
1019         struct berval   **bvals;
1020         LDAPControl **ctrls = NULL;
1021         FILE            *tmpfp;
1022
1023         dn = ldap_get_dn( ld, entry );
1024         ufn = NULL;
1025
1026         if ( ldif < 2 ) {
1027                 ufn = ldap_dn2ufn( dn );
1028                 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1029         }
1030         write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
1031
1032         rc = ldap_get_entry_controls( ld, entry, &ctrls );
1033
1034         if( rc != LDAP_SUCCESS ) {
1035                 fprintf(stderr, "print_entry: %d\n", rc );
1036                 ldap_perror( ld, "ldap_get_entry_controls" );
1037                 exit( EXIT_FAILURE );
1038         }
1039
1040         if( ctrls ) {
1041                 print_ctrls( ctrls );
1042                 ldap_controls_free( ctrls );
1043         }
1044
1045         if ( includeufn ) {
1046                 if( ufn == NULL ) {
1047                         ufn = ldap_dn2ufn( dn );
1048                 }
1049                 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1050         }
1051
1052         if( ufn != NULL ) ldap_memfree( ufn );
1053         ldap_memfree( dn );
1054
1055         for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1056                 a = ldap_next_attribute( ld, entry, ber ) )
1057         {
1058                 if ( attrsonly ) {
1059                         write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
1060
1061                 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1062                         for ( i = 0; bvals[i] != NULL; i++ ) {
1063                                 if ( vals2tmp > 1 || ( vals2tmp
1064                                         && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1065                                 {
1066                                         int tmpfd;
1067                                         /* write value to file */
1068                                         sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1069                                                 tmpdir, a );
1070                                         tmpfp = NULL;
1071
1072                                         if ( mktemp( tmpfname ) == NULL ) {
1073                                                 perror( tmpfname );
1074                                                 continue;
1075                                         }
1076
1077                                         if (( tmpfd = open( tmpfname, O_WRONLY|O_CREAT|O_EXCL, 0600 )) == -1 ) {
1078                                                 perror( tmpfname );
1079                                                 continue;
1080                                         }
1081
1082                                         if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1083                                                 perror( tmpfname );
1084                                                 continue;
1085                                         }
1086
1087                                         if ( fwrite( bvals[ i ]->bv_val,
1088                                                 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1089                                         {
1090                                                 perror( tmpfname );
1091                                                 fclose( tmpfp );
1092                                                 continue;
1093                                         }
1094
1095                                         fclose( tmpfp );
1096
1097                                         sprintf( url, "%s%s", urlpre,
1098                                                 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1099
1100                                         write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1101
1102                                 } else {
1103                                         write_ldif( LDIF_PUT_VALUE, a,
1104                                                 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1105                                 }
1106                         }
1107                         ber_bvecfree( bvals );
1108                 }
1109         }
1110
1111         if( ber != NULL ) {
1112                 ber_free( ber, 0 );
1113         }
1114 }
1115
1116 static void print_reference(
1117         LDAP *ld,
1118         LDAPMessage *reference )
1119 {
1120         int rc;
1121         char **refs = NULL;
1122         LDAPControl **ctrls;
1123
1124         if( ldif < 2 ) {
1125                 printf("# search reference\n");
1126         }
1127
1128         rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1129
1130         if( rc != LDAP_SUCCESS ) {
1131                 ldap_perror(ld, "ldap_parse_reference");
1132                 exit( EXIT_FAILURE );
1133         }
1134
1135         if( refs ) {
1136                 int i;
1137                 for( i=0; refs[i] != NULL; i++ ) {
1138                         write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1139                                 "ref", refs[i], strlen(refs[i]) );
1140                 }
1141                 ber_memvfree( (void **) refs );
1142         }
1143
1144         if( ctrls ) {
1145                 print_ctrls( ctrls );
1146                 ldap_controls_free( ctrls );
1147         }
1148 }
1149
1150 static void print_extended(
1151         LDAP *ld,
1152         LDAPMessage *extended )
1153 {
1154         int rc;
1155         char *retoid = NULL;
1156         struct berval *retdata = NULL;
1157
1158         if( ldif < 2 ) {
1159                 printf("# extended result response\n");
1160         }
1161
1162         rc = ldap_parse_extended_result( ld, extended,
1163                 &retoid, &retdata, 0 );
1164
1165         if( rc != LDAP_SUCCESS ) {
1166                 ldap_perror(ld, "ldap_parse_extended_result");
1167                 exit( EXIT_FAILURE );
1168         }
1169
1170         write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1171                 "extended", retoid, retoid ? strlen(retoid) : 0 );
1172         ber_memfree( retoid );
1173
1174         if(retdata) {
1175                 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1176                         "data", retdata->bv_val, retdata->bv_len );
1177                 ber_bvfree( retdata );
1178         }
1179
1180         print_result( ld, extended, 0 );
1181 }
1182
1183 static void print_partial(
1184         LDAP *ld,
1185         LDAPMessage *partial )
1186 {
1187         int rc;
1188         char *retoid = NULL;
1189         struct berval *retdata = NULL;
1190         LDAPControl **ctrls = NULL;
1191
1192         if( ldif < 2 ) {
1193                 printf("# extended partial response\n");
1194         }
1195
1196         rc = ldap_parse_extended_partial( ld, partial,
1197                 &retoid, &retdata, &ctrls, 0 );
1198
1199         if( rc != LDAP_SUCCESS ) {
1200                 ldap_perror(ld, "ldap_parse_extended_partial");
1201                 exit( EXIT_FAILURE );
1202         }
1203
1204         write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1205                 "partial", retoid, retoid ? strlen(retoid) : 0 );
1206
1207         ber_memfree( retoid );
1208
1209         if( retdata ) {
1210                 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1211                         "data", 
1212                         retdata->bv_val, retdata->bv_len );
1213
1214                 ber_bvfree( retdata );
1215         }
1216
1217         if( ctrls ) {
1218                 print_ctrls( ctrls );
1219                 ldap_controls_free( ctrls );
1220         }
1221 }
1222
1223 static int print_result(
1224         LDAP *ld,
1225         LDAPMessage *result, int search )
1226 {
1227         int rc;
1228         int err;
1229         char *matcheddn = NULL;
1230         char *text = NULL;
1231         char **refs = NULL;
1232         LDAPControl **ctrls = NULL;
1233
1234         if( search ) {
1235                 if ( ldif < 2 ) {
1236                         printf("# search result\n");
1237                 }
1238                 if ( ldif < 1 ) {
1239                         printf("%s: %d\n", "search", ldap_msgid(result) );
1240                 }
1241         }
1242
1243         rc = ldap_parse_result( ld, result,
1244                 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1245
1246         if( rc != LDAP_SUCCESS ) {
1247                 ldap_perror(ld, "ldap_parse_result");
1248                 exit( EXIT_FAILURE );
1249         }
1250
1251
1252         if( !ldif ) {
1253                 printf( "result: %d %s\n", err, ldap_err2string(err) );
1254
1255         } else if ( err != LDAP_SUCCESS ) {
1256                 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1257         }
1258
1259         if( matcheddn && *matcheddn ) {
1260                 if( !ldif ) {
1261                         write_ldif( LDIF_PUT_VALUE,
1262                                 "matchedDN", matcheddn, strlen(matcheddn) );
1263                 } else {
1264                         fprintf( stderr, "Matched DN: %s\n", matcheddn );
1265                 }
1266
1267                 ber_memfree( matcheddn );
1268         }
1269
1270         if( text && *text ) {
1271                 if( !ldif ) {
1272                         write_ldif( LDIF_PUT_TEXT, "text",
1273                                 text, strlen(text) );
1274                 } else {
1275                         fprintf( stderr, "Additional information: %s\n", text );
1276                 }
1277
1278                 ber_memfree( text );
1279         }
1280
1281         if( refs ) {
1282                 int i;
1283                 for( i=0; refs[i] != NULL; i++ ) {
1284                         if( !ldif ) {
1285                                 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1286                         } else {
1287                                 fprintf( stderr, "Referral: %s\n", refs[i] );
1288                         }
1289                 }
1290
1291                 ber_memvfree( (void **) refs );
1292         }
1293
1294         if( ctrls ) {
1295                 print_ctrls( ctrls );
1296                 ldap_controls_free( ctrls );
1297         }
1298
1299         return err;
1300 }
1301
1302 static void print_ctrls(
1303         LDAPControl **ctrls )
1304 {
1305         int i;
1306         for(i=0; ctrls[i] != NULL; i++ ) {
1307                 /* control: OID criticality base64value */
1308                 struct berval *b64 = NULL;
1309                 ber_len_t len;
1310                 char *str;
1311                         
1312                 len = strlen( ctrls[i]->ldctl_oid );
1313
1314                 /* add enough for space after OID and the critical value itself */
1315                 len += ctrls[i]->ldctl_iscritical
1316                         ? sizeof("true") : sizeof("false");
1317
1318                 /* convert to base64 */
1319                 if( ctrls[i]->ldctl_value.bv_len ) {
1320                         b64 = ber_memalloc( sizeof(struct berval) );
1321                         
1322                         b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1323                                 ctrls[i]->ldctl_value.bv_len ) + 1;
1324                         b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1325
1326                         b64->bv_len = lutil_b64_ntop(
1327                                 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1328                                 b64->bv_val, b64->bv_len );
1329                 }
1330
1331                 if( b64 ) {
1332                         len += 1 + b64->bv_len;
1333                 }
1334
1335                 str = malloc( len + 1 );
1336                 strcpy( str, ctrls[i]->ldctl_oid );
1337                 strcat( str, ctrls[i]->ldctl_iscritical
1338                         ? " true" : " false" );
1339
1340                 if( b64 ) {
1341                         strcat(str, " ");
1342                         strcat(str, b64->bv_val );
1343                 }
1344
1345                 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1346                         "control", str, len );
1347
1348                 free( str );
1349                 ber_bvfree( b64 );
1350         }
1351 }
1352
1353 static int
1354 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1355 {
1356         char    *ldif;
1357
1358         if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1359                 return( -1 );
1360         }
1361
1362         fputs( ldif, stdout );
1363         ber_memfree( ldif );
1364
1365         return( 0 );
1366 }