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