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