]> git.sur5r.net Git - openldap/blob - clients/tools/ldapsearch.c
Initial ldapcompare(1) implementation. Adapted from patch
[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[ BUFSIZ ];
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                 sprintf( filter, filtpatt, value );
950
951                 if ( verbose ) {
952                         fprintf( stderr, "filter: %s\n", filter );
953                 }
954
955                 if( ldif < 2 ) {
956                         printf( "#\n# filter: %s\n#\n", filter );
957                 }
958
959         } else {
960                 sprintf( filter, "%s", value );
961         }
962
963         if ( not ) {
964                 return LDAP_SUCCESS;
965         }
966
967         rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
968                 sctrls, cctrls, timeout, sizelimit, &msgid );
969
970         if( rc != LDAP_SUCCESS ) {
971                 fprintf( stderr, "%s: ldap_search_ext: %s (%d)\n",
972                         prog, ldap_err2string( rc ), rc );
973                 return( rc );
974         }
975
976         nresponses = nentries = nreferences = nextended = npartial = 0;
977
978         res = NULL;
979
980         while ((rc = ldap_result( ld, LDAP_RES_ANY,
981                 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
982                 NULL, &res )) > 0 )
983         {
984                 if( sortattr ) {
985                         (void) ldap_sort_entries( ld, &res,
986                                 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
987                 }
988
989                 for ( msg = ldap_first_message( ld, res );
990                         msg != NULL;
991                         msg = ldap_next_message( ld, msg ) )
992                 {
993                         if( nresponses++ ) putchar('\n');
994
995                         switch( ldap_msgtype( msg ) ) {
996                         case LDAP_RES_SEARCH_ENTRY:
997                                 nentries++;
998                                 print_entry( ld, msg, attrsonly );
999                                 break;
1000
1001                         case LDAP_RES_SEARCH_REFERENCE:
1002                                 nreferences++;
1003                                 print_reference( ld, msg );
1004                                 break;
1005
1006                         case LDAP_RES_EXTENDED:
1007                                 nextended++;
1008                                 print_extended( ld, msg );
1009
1010                                 if( ldap_msgid( msg ) == 0 ) {
1011                                         /* unsolicited extended operation */
1012                                         goto done;
1013                                 }
1014                                 break;
1015
1016                         case LDAP_RES_EXTENDED_PARTIAL:
1017                                 npartial++;
1018                                 print_partial( ld, msg );
1019                                 break;
1020
1021                         case LDAP_RES_SEARCH_RESULT:
1022                                 rc = print_result( ld, msg, 1 );
1023                                 goto done;
1024                         }
1025                 }
1026
1027                 ldap_msgfree( res );
1028         }
1029
1030         if ( rc == -1 ) {
1031                 ldap_perror( ld, "ldap_result" );
1032                 return( rc );
1033         }
1034
1035 done:
1036         if ( ldif < 2 ) {
1037                 printf( "\n# numResponses: %d\n", nresponses );
1038                 if( nentries ) printf( "# numEntries: %d\n", nentries );
1039                 if( nextended ) printf( "# numExtended: %d\n", nextended );
1040                 if( npartial ) printf( "# numPartial: %d\n", npartial );
1041                 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
1042         }
1043
1044         return( rc );
1045 }
1046
1047 static void
1048 print_entry(
1049         LDAP    *ld,
1050         LDAPMessage     *entry,
1051         int             attrsonly)
1052 {
1053         char            *a, *dn, *ufn;
1054         char    tmpfname[ 256 ];
1055         char    url[ 256 ];
1056         int                     i, rc;
1057         BerElement              *ber = NULL;
1058         struct berval   **bvals;
1059         LDAPControl **ctrls = NULL;
1060         FILE            *tmpfp;
1061
1062         dn = ldap_get_dn( ld, entry );
1063         ufn = NULL;
1064
1065         if ( ldif < 2 ) {
1066                 ufn = ldap_dn2ufn( dn );
1067                 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1068         }
1069         write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
1070
1071         rc = ldap_get_entry_controls( ld, entry, &ctrls );
1072
1073         if( rc != LDAP_SUCCESS ) {
1074                 fprintf(stderr, "print_entry: %d\n", rc );
1075                 ldap_perror( ld, "ldap_get_entry_controls" );
1076                 exit( EXIT_FAILURE );
1077         }
1078
1079         if( ctrls ) {
1080                 print_ctrls( ctrls );
1081                 ldap_controls_free( ctrls );
1082         }
1083
1084         if ( includeufn ) {
1085                 if( ufn == NULL ) {
1086                         ufn = ldap_dn2ufn( dn );
1087                 }
1088                 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1089         }
1090
1091         if( ufn != NULL ) ldap_memfree( ufn );
1092         ldap_memfree( dn );
1093
1094         for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1095                 a = ldap_next_attribute( ld, entry, ber ) )
1096         {
1097                 if ( attrsonly ) {
1098                         write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
1099
1100                 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1101                         for ( i = 0; bvals[i] != NULL; i++ ) {
1102                                 if ( vals2tmp > 1 || ( vals2tmp
1103                                         && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1104                                 {
1105                                         int tmpfd;
1106                                         /* write value to file */
1107                                         sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1108                                                 tmpdir, a );
1109                                         tmpfp = NULL;
1110
1111                                         tmpfd = mkstemp( tmpfname );
1112
1113                                         if ( tmpfd < 0  ) {
1114                                                 perror( tmpfname );
1115                                                 continue;
1116                                         }
1117
1118                                         if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1119                                                 perror( tmpfname );
1120                                                 continue;
1121                                         }
1122
1123                                         if ( fwrite( bvals[ i ]->bv_val,
1124                                                 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1125                                         {
1126                                                 perror( tmpfname );
1127                                                 fclose( tmpfp );
1128                                                 continue;
1129                                         }
1130
1131                                         fclose( tmpfp );
1132
1133                                         sprintf( url, "%s%s", urlpre,
1134                                                 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1135
1136                                         urlize( url );
1137                                         write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1138
1139                                 } else {
1140                                         write_ldif( LDIF_PUT_VALUE, a,
1141                                                 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1142                                 }
1143                         }
1144                         ber_bvecfree( bvals );
1145                 }
1146         }
1147
1148         if( ber != NULL ) {
1149                 ber_free( ber, 0 );
1150         }
1151 }
1152
1153 static void print_reference(
1154         LDAP *ld,
1155         LDAPMessage *reference )
1156 {
1157         int rc;
1158         char **refs = NULL;
1159         LDAPControl **ctrls;
1160
1161         if( ldif < 2 ) {
1162                 printf("# search reference\n");
1163         }
1164
1165         rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1166
1167         if( rc != LDAP_SUCCESS ) {
1168                 ldap_perror(ld, "ldap_parse_reference");
1169                 exit( EXIT_FAILURE );
1170         }
1171
1172         if( refs ) {
1173                 int i;
1174                 for( i=0; refs[i] != NULL; i++ ) {
1175                         write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1176                                 "ref", refs[i], strlen(refs[i]) );
1177                 }
1178                 ber_memvfree( (void **) refs );
1179         }
1180
1181         if( ctrls ) {
1182                 print_ctrls( ctrls );
1183                 ldap_controls_free( ctrls );
1184         }
1185 }
1186
1187 static void print_extended(
1188         LDAP *ld,
1189         LDAPMessage *extended )
1190 {
1191         int rc;
1192         char *retoid = NULL;
1193         struct berval *retdata = NULL;
1194
1195         if( ldif < 2 ) {
1196                 printf("# extended result response\n");
1197         }
1198
1199         rc = ldap_parse_extended_result( ld, extended,
1200                 &retoid, &retdata, 0 );
1201
1202         if( rc != LDAP_SUCCESS ) {
1203                 ldap_perror(ld, "ldap_parse_extended_result");
1204                 exit( EXIT_FAILURE );
1205         }
1206
1207         write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1208                 "extended", retoid, retoid ? strlen(retoid) : 0 );
1209         ber_memfree( retoid );
1210
1211         if(retdata) {
1212                 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1213                         "data", retdata->bv_val, retdata->bv_len );
1214                 ber_bvfree( retdata );
1215         }
1216
1217         print_result( ld, extended, 0 );
1218 }
1219
1220 static void print_partial(
1221         LDAP *ld,
1222         LDAPMessage *partial )
1223 {
1224         int rc;
1225         char *retoid = NULL;
1226         struct berval *retdata = NULL;
1227         LDAPControl **ctrls = NULL;
1228
1229         if( ldif < 2 ) {
1230                 printf("# extended partial response\n");
1231         }
1232
1233         rc = ldap_parse_extended_partial( ld, partial,
1234                 &retoid, &retdata, &ctrls, 0 );
1235
1236         if( rc != LDAP_SUCCESS ) {
1237                 ldap_perror(ld, "ldap_parse_extended_partial");
1238                 exit( EXIT_FAILURE );
1239         }
1240
1241         write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1242                 "partial", retoid, retoid ? strlen(retoid) : 0 );
1243
1244         ber_memfree( retoid );
1245
1246         if( retdata ) {
1247                 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1248                         "data", 
1249                         retdata->bv_val, retdata->bv_len );
1250
1251                 ber_bvfree( retdata );
1252         }
1253
1254         if( ctrls ) {
1255                 print_ctrls( ctrls );
1256                 ldap_controls_free( ctrls );
1257         }
1258 }
1259
1260 static int print_result(
1261         LDAP *ld,
1262         LDAPMessage *result, int search )
1263 {
1264         int rc;
1265         int err;
1266         char *matcheddn = NULL;
1267         char *text = NULL;
1268         char **refs = NULL;
1269         LDAPControl **ctrls = NULL;
1270
1271         if( search ) {
1272                 if ( ldif < 2 ) {
1273                         printf("# search result\n");
1274                 }
1275                 if ( ldif < 1 ) {
1276                         printf("%s: %d\n", "search", ldap_msgid(result) );
1277                 }
1278         }
1279
1280         rc = ldap_parse_result( ld, result,
1281                 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1282
1283         if( rc != LDAP_SUCCESS ) {
1284                 ldap_perror(ld, "ldap_parse_result");
1285                 exit( EXIT_FAILURE );
1286         }
1287
1288
1289         if( !ldif ) {
1290                 printf( "result: %d %s\n", err, ldap_err2string(err) );
1291
1292         } else if ( err != LDAP_SUCCESS ) {
1293                 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1294         }
1295
1296         if( matcheddn && *matcheddn ) {
1297                 if( !ldif ) {
1298                         write_ldif( LDIF_PUT_VALUE,
1299                                 "matchedDN", matcheddn, strlen(matcheddn) );
1300                 } else {
1301                         fprintf( stderr, "Matched DN: %s\n", matcheddn );
1302                 }
1303
1304                 ber_memfree( matcheddn );
1305         }
1306
1307         if( text && *text ) {
1308                 if( !ldif ) {
1309                         write_ldif( LDIF_PUT_TEXT, "text",
1310                                 text, strlen(text) );
1311                 } else {
1312                         fprintf( stderr, "Additional information: %s\n", text );
1313                 }
1314
1315                 ber_memfree( text );
1316         }
1317
1318         if( refs ) {
1319                 int i;
1320                 for( i=0; refs[i] != NULL; i++ ) {
1321                         if( !ldif ) {
1322                                 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1323                         } else {
1324                                 fprintf( stderr, "Referral: %s\n", refs[i] );
1325                         }
1326                 }
1327
1328                 ber_memvfree( (void **) refs );
1329         }
1330
1331         if( ctrls ) {
1332                 print_ctrls( ctrls );
1333                 ldap_controls_free( ctrls );
1334         }
1335
1336         return err;
1337 }
1338
1339 static void print_ctrls(
1340         LDAPControl **ctrls )
1341 {
1342         int i;
1343         for(i=0; ctrls[i] != NULL; i++ ) {
1344                 /* control: OID criticality base64value */
1345                 struct berval *b64 = NULL;
1346                 ber_len_t len;
1347                 char *str;
1348                         
1349                 len = strlen( ctrls[i]->ldctl_oid );
1350
1351                 /* add enough for space after OID and the critical value itself */
1352                 len += ctrls[i]->ldctl_iscritical
1353                         ? sizeof("true") : sizeof("false");
1354
1355                 /* convert to base64 */
1356                 if( ctrls[i]->ldctl_value.bv_len ) {
1357                         b64 = ber_memalloc( sizeof(struct berval) );
1358                         
1359                         b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1360                                 ctrls[i]->ldctl_value.bv_len ) + 1;
1361                         b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1362
1363                         b64->bv_len = lutil_b64_ntop(
1364                                 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1365                                 b64->bv_val, b64->bv_len );
1366                 }
1367
1368                 if( b64 ) {
1369                         len += 1 + b64->bv_len;
1370                 }
1371
1372                 str = malloc( len + 1 );
1373                 strcpy( str, ctrls[i]->ldctl_oid );
1374                 strcat( str, ctrls[i]->ldctl_iscritical
1375                         ? " true" : " false" );
1376
1377                 if( b64 ) {
1378                         strcat(str, " ");
1379                         strcat(str, b64->bv_val );
1380                 }
1381
1382                 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1383                         "control", str, len );
1384
1385                 free( str );
1386                 ber_bvfree( b64 );
1387         }
1388 }
1389
1390 static int
1391 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1392 {
1393         char    *ldif;
1394
1395         if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1396                 return( -1 );
1397         }
1398
1399         fputs( ldif, stdout );
1400         ber_memfree( ldif );
1401
1402         return( 0 );
1403 }