]> git.sur5r.net Git - openldap/blob - clients/tools/ldapsearch.c
Remove commented out (logging) code
[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 "  -l limit   time limit (in seconds) for search\n"
54 "  -L         print responses in LDIFv1 format\n"
55 "  -LL        print responses in LDIF format without comments\n"
56 "  -LLL       print responses in LDIF format without comments\n"
57 "             and version\n"
58 "  -s scope   one of base, one, or sub (search scope)\n"
59 "  -S attr    sort the results by attribute `attr'\n"
60 "  -t         write binary values to files in temporary directory\n"
61 "  -tt        write all values to files in temporary directory\n"
62 "  -T path    write files to directory specified by path (default:\n"
63 "             " LDAP_TMPDIR ")\n"
64 "  -u         include User Friendly entry names in the output\n"
65 "  -V prefix  URL prefix for files (default: \"" LDAP_FILE_URI_PREFIX ")\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 user    SASL authentication identity (username)\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 id      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:Ll:S:s:T:tuV:z:"
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 'l':       /* time limit */
220                 timelimit = atoi( optarg );
221                 break;
222         case 'L':       /* print entries in LDIF format */
223                 ++ldif;
224                 break;
225         case 's':       /* search scope */
226                 if ( strcasecmp( optarg, "base" ) == 0 ) {
227                 scope = LDAP_SCOPE_BASE;
228                 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
229                 scope = LDAP_SCOPE_ONELEVEL;
230                 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
231                 scope = LDAP_SCOPE_SUBTREE;
232                 } else {
233                 fprintf( stderr, "scope should be base, one, or sub\n" );
234                 usage( argv[ 0 ] );
235                 }
236                 break;
237         case 'S':       /* sort attribute */
238                 sortattr = strdup( optarg );
239                 break;
240         case 'u':       /* include UFN */
241                 ++includeufn;
242                 break;
243         case 't':       /* write attribute values to TMPDIR files */
244                 ++vals2tmp;
245                 break;
246         case 'T':       /* tmpdir */
247                 if( tmpdir ) free( tmpdir );
248                 tmpdir = strdup( optarg );
249                 break;
250         case 'V':       /* uri prefix */
251                 if( urlpre ) free( urlpre );
252                 urlpre = 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                 if ( use_tls > 1 ) {
737                         ldap_perror( ld, "ldap_start_tls" );
738                         return EXIT_FAILURE;
739                 }
740                 fprintf( stderr, "WARNING: could not start TLS\n" );
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 *timelimit,
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, timelimit, 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                                         if (( tmpfd = open( tmpfname, O_WRONLY|O_CREAT|O_EXCL, 0600 )) == -1 ) {
1074                                                 perror( tmpfname );
1075                                                 continue;
1076                                         }
1077
1078                                         if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1079                                                 perror( tmpfname );
1080                                                 continue;
1081                                         }
1082
1083                                         if ( fwrite( bvals[ i ]->bv_val,
1084                                                 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1085                                         {
1086                                                 perror( tmpfname );
1087                                                 fclose( tmpfp );
1088                                                 continue;
1089                                         }
1090
1091                                         fclose( tmpfp );
1092
1093                                         sprintf( url, "%s%s", urlpre,
1094                                                 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1095
1096                                         write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1097
1098                                 } else {
1099                                         write_ldif( LDIF_PUT_VALUE, a,
1100                                                 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1101                                 }
1102                         }
1103                         ber_bvecfree( bvals );
1104                 }
1105         }
1106
1107         if( ber != NULL ) {
1108                 ber_free( ber, 0 );
1109         }
1110 }
1111
1112 static void print_reference(
1113         LDAP *ld,
1114         LDAPMessage *reference )
1115 {
1116         int rc;
1117         char **refs = NULL;
1118         LDAPControl **ctrls;
1119
1120         if( ldif < 2 ) {
1121                 printf("# search reference\n");
1122         }
1123
1124         rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1125
1126         if( rc != LDAP_SUCCESS ) {
1127                 ldap_perror(ld, "ldap_parse_reference");
1128                 exit( EXIT_FAILURE );
1129         }
1130
1131         if( refs ) {
1132                 int i;
1133                 for( i=0; refs[i] != NULL; i++ ) {
1134                         write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1135                                 "ref", refs[i], strlen(refs[i]) );
1136                 }
1137                 ber_memvfree( (void **) refs );
1138         }
1139
1140         if( ctrls ) {
1141                 print_ctrls( ctrls );
1142                 ldap_controls_free( ctrls );
1143         }
1144 }
1145
1146 static void print_extended(
1147         LDAP *ld,
1148         LDAPMessage *extended )
1149 {
1150         int rc;
1151         char *retoid = NULL;
1152         struct berval *retdata = NULL;
1153
1154         if( ldif < 2 ) {
1155                 printf("# extended result response\n");
1156         }
1157
1158         rc = ldap_parse_extended_result( ld, extended,
1159                 &retoid, &retdata, 0 );
1160
1161         if( rc != LDAP_SUCCESS ) {
1162                 ldap_perror(ld, "ldap_parse_extended_result");
1163                 exit( EXIT_FAILURE );
1164         }
1165
1166         write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1167                 "extended", retoid, retoid ? strlen(retoid) : 0 );
1168         ber_memfree( retoid );
1169
1170         if(retdata) {
1171                 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1172                         "data", retdata->bv_val, retdata->bv_len );
1173                 ber_bvfree( retdata );
1174         }
1175
1176         print_result( ld, extended, 0 );
1177 }
1178
1179 static void print_partial(
1180         LDAP *ld,
1181         LDAPMessage *partial )
1182 {
1183         int rc;
1184         char *retoid = NULL;
1185         struct berval *retdata = NULL;
1186         LDAPControl **ctrls = NULL;
1187
1188         if( ldif < 2 ) {
1189                 printf("# extended partial response\n");
1190         }
1191
1192         rc = ldap_parse_extended_partial( ld, partial,
1193                 &retoid, &retdata, &ctrls, 0 );
1194
1195         if( rc != LDAP_SUCCESS ) {
1196                 ldap_perror(ld, "ldap_parse_extended_partial");
1197                 exit( EXIT_FAILURE );
1198         }
1199
1200         write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1201                 "partial", retoid, retoid ? strlen(retoid) : 0 );
1202
1203         ber_memfree( retoid );
1204
1205         if( retdata ) {
1206                 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1207                         "data", 
1208                         retdata->bv_val, retdata->bv_len );
1209
1210                 ber_bvfree( retdata );
1211         }
1212
1213         if( ctrls ) {
1214                 print_ctrls( ctrls );
1215                 ldap_controls_free( ctrls );
1216         }
1217 }
1218
1219 static int print_result(
1220         LDAP *ld,
1221         LDAPMessage *result, int search )
1222 {
1223         int rc;
1224         int err;
1225         char *matcheddn = NULL;
1226         char *text = NULL;
1227         char **refs = NULL;
1228         LDAPControl **ctrls = NULL;
1229
1230         if( search ) {
1231                 if ( ldif < 2 ) {
1232                         printf("# search result\n");
1233                 }
1234                 if ( ldif < 1 ) {
1235                         printf("%s: %d\n", "search", ldap_msgid(result) );
1236                 }
1237         }
1238
1239         rc = ldap_parse_result( ld, result,
1240                 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1241
1242         if( rc != LDAP_SUCCESS ) {
1243                 ldap_perror(ld, "ldap_parse_result");
1244                 exit( EXIT_FAILURE );
1245         }
1246
1247
1248         if( !ldif ) {
1249                 printf( "result: %d %s\n", err, ldap_err2string(err) );
1250
1251         } else if ( err != LDAP_SUCCESS ) {
1252                 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1253         }
1254
1255         if( matcheddn && *matcheddn ) {
1256                 if( !ldif ) {
1257                         write_ldif( LDIF_PUT_VALUE,
1258                                 "matchedDN", matcheddn, strlen(matcheddn) );
1259                 } else {
1260                         fprintf( stderr, "Matched DN: %s\n", matcheddn );
1261                 }
1262
1263                 ber_memfree( matcheddn );
1264         }
1265
1266         if( text && *text ) {
1267                 if( !ldif ) {
1268                         write_ldif( LDIF_PUT_TEXT, "text",
1269                                 text, strlen(text) );
1270                 } else {
1271                         fprintf( stderr, "Additional information: %s\n", text );
1272                 }
1273
1274                 ber_memfree( text );
1275         }
1276
1277         if( refs ) {
1278                 int i;
1279                 for( i=0; refs[i] != NULL; i++ ) {
1280                         if( !ldif ) {
1281                                 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1282                         } else {
1283                                 fprintf( stderr, "Referral: %s\n", refs[i] );
1284                         }
1285                 }
1286
1287                 ber_memvfree( (void **) refs );
1288         }
1289
1290         if( ctrls ) {
1291                 print_ctrls( ctrls );
1292                 ldap_controls_free( ctrls );
1293         }
1294
1295         return err;
1296 }
1297
1298 static void print_ctrls(
1299         LDAPControl **ctrls )
1300 {
1301         int i;
1302         for(i=0; ctrls[i] != NULL; i++ ) {
1303                 /* control: OID criticality base64value */
1304                 struct berval *b64 = NULL;
1305                 ber_len_t len;
1306                 char *str;
1307                         
1308                 len = strlen( ctrls[i]->ldctl_oid );
1309
1310                 /* add enough for space after OID and the critical value itself */
1311                 len += ctrls[i]->ldctl_iscritical
1312                         ? sizeof("true") : sizeof("false");
1313
1314                 /* convert to base64 */
1315                 if( ctrls[i]->ldctl_value.bv_len ) {
1316                         b64 = ber_memalloc( sizeof(struct berval) );
1317                         
1318                         b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1319                                 ctrls[i]->ldctl_value.bv_len ) + 1;
1320                         b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1321
1322                         b64->bv_len = lutil_b64_ntop(
1323                                 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1324                                 b64->bv_val, b64->bv_len );
1325                 }
1326
1327                 if( b64 ) {
1328                         len += 1 + b64->bv_len;
1329                 }
1330
1331                 str = malloc( len + 1 );
1332                 strcpy( str, ctrls[i]->ldctl_oid );
1333                 strcat( str, ctrls[i]->ldctl_iscritical
1334                         ? " true" : " false" );
1335
1336                 if( b64 ) {
1337                         strcat(str, " ");
1338                         strcat(str, b64->bv_val );
1339                 }
1340
1341                 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1342                         "control", str, len );
1343
1344                 free( str );
1345                 ber_bvfree( b64 );
1346         }
1347 }
1348
1349 static int
1350 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1351 {
1352         char    *ldif;
1353
1354         if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1355                 return( -1 );
1356         }
1357
1358         fputs( ldif, stdout );
1359         ber_memfree( ldif );
1360
1361         return( 0 );
1362 }