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