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