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