]> git.sur5r.net Git - openldap/blob - clients/tools/ldapsearch.c
Add missing 'static'... (ITS#823,825,828)
[openldap] / clients / tools / ldapsearch.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10
11 #include <ac/stdlib.h>
12
13 #include <ac/ctype.h>
14 #include <ac/signal.h>
15 #include <ac/string.h>
16 #include <ac/unistd.h>
17 #include <ac/errno.h>
18 #include <sys/stat.h>
19
20 #ifdef HAVE_FCNTL_H
21 #include <fcntl.h>
22 #endif
23 #ifdef HAVE_SYS_TYPES_H
24 #include <sys/types.h>
25 #endif
26 #ifdef HAVE_IO_H
27 #include <io.h>
28 #endif
29
30 #include <ldap.h>
31
32 #include "ldif.h"
33 #include "lutil.h"
34 #include "lutil_ldap.h"
35 #include "ldap_defaults.h"
36
37 static void
38 usage( const char *s )
39 {
40         fprintf( stderr,
41 "usage: %s [options] [filter [attributes...]]\nwhere:\n"
42 "  filter\tRFC-2254 compliant LDAP search filter\n"
43 "  attributes\twhitespace-separated list of attribute descriptions\n"
44 "    which may include:\n"
45 "      1.1   no attributes\n"
46 "      *     all user attributes\n"
47 "      +     all operational attributes\n"
48
49 "Search options:\n"
50 "  -a deref   one of never (default), always, search, or find\n"
51 "  -A         retrieve attribute names only (no values)\n"
52 "  -b basedn  base dn for search\n"
53 "  -l limit   time limit (in seconds) for search\n"
54 "  -L         print responses in LDIFv1 format\n"
55 "  -LL        print responses in LDIF format without comments\n"
56 "  -LLL       print responses in LDIF format without comments\n"
57 "             and version\n"
58 "  -s scope   one of base, one, or sub (search scope)\n"
59 "  -S attr    sort the results by attribute `attr'\n"
60 "  -t         write binary values to files in temporary directory\n"
61 "  -tt        write all values to files in temporary directory\n"
62 "  -T path    write files to directory specified by path (default:\n"
63 "             " LDAP_TMPDIR ")\n"
64 "  -u         include User Friendly entry names in the output\n"
65 "  -V prefix  URL prefix for files (default: \"" LDAP_FILE_URI_PREFIX ")\n"
66 "  -z limit   size limit (in entries) for search\n"
67
68 "Common options:\n"
69 "  -d level   set LDAP debugging level to `level'\n"
70 "  -D binddn  bind DN\n"
71 "  -f file    read operations from `file'\n"
72 "  -h host    LDAP server\n"
73 "  -H URI     LDAP Uniform Resource Indentifier(s)\n"
74 "  -I         use SASL Interactive mode\n"
75 "  -k         use Kerberos authentication\n"
76 "  -K         like -k, but do only step 1 of the Kerberos bind\n"
77 "  -M         enable Manage DSA IT control (-MM to make critical)\n"
78 "  -n         show what would be done but don't actually search\n"
79 "  -O props   SASL security properties\n"
80 "  -p port    port on LDAP server\n"
81 "  -P version procotol version (default: 3)\n"
82 "  -Q         use SASL Quiet mode\n"
83 "  -R realm   SASL realm\n"
84 "  -U user    SASL authentication identity (username)\n"
85 "  -v         run in verbose mode (diagnostics to standard output)\n"
86 "  -w passwd  bind passwd (for simple authentication)\n"
87 "  -W         prompt for bind passwd\n"
88 "  -x         Simple authentication\n"
89 "  -X id      SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
90 "  -Y mech    SASL mechanism\n"
91 "  -Z         Start TLS request (-ZZ to require successful response)\n"
92 , s );
93
94         exit( EXIT_FAILURE );
95 }
96
97 static void print_entry LDAP_P((
98         LDAP    *ld,
99         LDAPMessage     *entry,
100         int             attrsonly));
101
102 static void print_reference(
103         LDAP *ld,
104         LDAPMessage *reference );
105
106 static void print_extended(
107         LDAP *ld,
108         LDAPMessage *extended );
109
110 static void print_partial(
111         LDAP *ld,
112         LDAPMessage *partial );
113
114 static int print_result(
115         LDAP *ld,
116         LDAPMessage *result,
117         int search );
118
119 static void print_ctrls(
120         LDAPControl **ctrls );
121
122 static int write_ldif LDAP_P((
123         int type,
124         char *name,
125         char *value,
126         ber_len_t vallen ));
127
128 static int dosearch LDAP_P((
129         LDAP    *ld,
130         char    *base,
131         int             scope,
132         char    *filtpatt,
133         char    *value,
134         char    **attrs,
135         int             attrsonly,
136         LDAPControl **sctrls,
137         LDAPControl **cctrls,
138         struct timeval *timelimit,
139         int     sizelimit ));
140
141 static char *tmpdir = NULL;
142 static char *urlpre = NULL;
143
144 static char *prog = NULL;
145 static char     *binddn = NULL;
146 static struct berval passwd = { 0, NULL };
147 static char     *base = NULL;
148 static char     *ldaphost = NULL;
149 static char *ldapuri = NULL;
150 static int      ldapport = 0;
151 #ifdef HAVE_CYRUS_SASL
152 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
153 static char     *sasl_realm = NULL;
154 static char     *sasl_authc_id = NULL;
155 static char     *sasl_authz_id = NULL;
156 static char     *sasl_mech = NULL;
157 static char     *sasl_secprops = NULL;
158 #endif
159 static int      use_tls = 0;
160 static char     *sortattr = NULL;
161 static int      verbose, not, includeufn, vals2tmp, ldif;
162
163 int
164 main( int argc, char **argv )
165 {
166         char            *infile, *filtpattern, **attrs = NULL, line[BUFSIZ];
167         FILE            *fp = NULL;
168         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", prog );
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", prog );
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", prog );
272                         return EXIT_FAILURE;
273                 }
274                 if( ldaphost != NULL ) {
275                         fprintf( stderr, "%s: -h previously specified\n", prog );
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", prog );
283                         return EXIT_FAILURE;
284                 }
285                 if( ldapport ) {
286                         fprintf( stderr, "%s: -H incompatible with -p\n", prog );
287                         return EXIT_FAILURE;
288                 }
289                 if( ldapuri != NULL ) {
290                         fprintf( stderr, "%s: -H previously specified\n", prog );
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", prog );
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", prog );
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", prog );
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", prog );
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", prog );
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", prog );
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: unrecognized 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) && (sortattr == NULL || *sortattr == '\0') ) {
616                 attrs = &argv[optind];
617         }
618
619         if ( infile != NULL ) {
620                 if ( infile[0] == '-' && infile[1] == '\0' ) {
621                         fp = stdin;
622                 } else if (( fp = fopen( infile, "r" )) == NULL ) {
623                         perror( infile );
624                         return EXIT_FAILURE;
625                 }
626         }
627
628         if( tmpdir == NULL
629                 && (tmpdir = getenv("TMPDIR")) == NULL
630                 && (tmpdir = getenv("TMP")) == NULL
631                 && (tmpdir = getenv("TEMP")) == NULL )
632         {
633                 tmpdir = LDAP_TMPDIR;
634         }
635
636         if( urlpre == NULL ) {
637                 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
638
639                 if( urlpre == NULL ) {
640                         perror( "malloc" );
641                         return EXIT_FAILURE;
642                 }
643
644                 sprintf( urlpre, "file:///%s/",
645                         tmpdir[0] == '/' ? &tmpdir[1] : tmpdir );
646
647                 /* urlpre should be URLized.... */
648         }
649
650         if ( debug ) {
651                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
652                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
653                 }
654                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
655                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
656                 }
657                 ldif_debug = debug;
658         }
659
660 #ifdef SIGPIPE
661         (void) SIGNAL( SIGPIPE, SIG_IGN );
662 #endif
663
664         if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
665                 if ( verbose ) {
666                         fprintf( stderr, "ldap_init( %s, %d )\n",
667                                 ldaphost != NULL ? ldaphost : "<DEFAULT>",
668                                 ldapport );
669                 }
670
671                 ld = ldap_init( ldaphost, ldapport );
672                 if( ld == NULL ) {
673                         perror("ldapsearch: ldap_init");
674                         return EXIT_FAILURE;
675                 }
676
677         } else {
678                 if ( verbose ) {
679                         fprintf( stderr, "ldap_initialize( %s )\n",
680                                 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
681                 }
682
683                 rc = ldap_initialize( &ld, ldapuri );
684                 if( rc != LDAP_SUCCESS ) {
685                         fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
686                                 rc, ldap_err2string(rc) );
687                         return EXIT_FAILURE;
688                 }
689         }
690
691         if (deref != -1 &&
692                 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
693         {
694                 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
695                 return EXIT_FAILURE;
696         }
697         if (timelimit != -1 &&
698                 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
699         {
700                 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
701                 return EXIT_FAILURE;
702         }
703         if (sizelimit != -1 &&
704                 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
705         {
706                 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
707                 return EXIT_FAILURE;
708         }
709
710         /* referrals */
711         if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
712                 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
713         {
714                 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
715                         referrals ? "on" : "off" );
716                 return EXIT_FAILURE;
717         }
718
719         if (version == -1 ) {
720                 version = 3;
721         }
722
723         if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
724                 != LDAP_OPT_SUCCESS )
725         {
726                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
727                         version );
728                 return EXIT_FAILURE;
729         }
730
731         if ( use_tls && ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
732                 if ( use_tls > 1 ) {
733                         ldap_perror( ld, "ldap_start_tls" );
734                         return EXIT_FAILURE;
735                 }
736                 fprintf( stderr, "WARNING: could not start TLS\n" );
737         }
738
739         if (want_bindpw) {
740                 passwd.bv_val = getpassphrase("Enter LDAP Password: ");
741                 passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
742         }
743
744         if ( authmethod == LDAP_AUTH_SASL ) {
745 #ifdef HAVE_CYRUS_SASL
746                 void *defaults;
747
748                 if( sasl_secprops != NULL ) {
749                         rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
750                                 (void *) sasl_secprops );
751                         
752                         if( rc != LDAP_OPT_SUCCESS ) {
753                                 fprintf( stderr,
754                                         "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
755                                         sasl_secprops );
756                                 return( EXIT_FAILURE );
757                         }
758                 }
759                 
760                 defaults = lutil_sasl_defaults( ld,
761                         sasl_mech,
762                         sasl_realm,
763                         sasl_authc_id,
764                         passwd.bv_val,
765                         sasl_authz_id );
766
767                 rc = ldap_sasl_interactive_bind_s( ld, binddn,
768                         sasl_mech, NULL, NULL,
769                         sasl_flags, lutil_sasl_interact, defaults );
770
771                 if( rc != LDAP_SUCCESS ) {
772                         ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
773                         return( EXIT_FAILURE );
774                 }
775 #else
776                 fprintf( stderr, "%s: not compiled with SASL support\n",
777                         prog, argv[0] );
778                 return( EXIT_FAILURE );
779 #endif
780         } else {
781                 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
782                                 != LDAP_SUCCESS ) {
783                         ldap_perror( ld, "ldap_bind" );
784                         return( EXIT_FAILURE );
785                 }
786         }
787
788         if ( manageDSAit ) {
789                 int err;
790                 LDAPControl c;
791                 LDAPControl *ctrls[2];
792                 ctrls[0] = &c;
793                 ctrls[1] = NULL;
794
795                 c.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
796                 c.ldctl_value.bv_val = NULL;
797                 c.ldctl_value.bv_len = 0;
798                 c.ldctl_iscritical = manageDSAit > 1;
799
800                 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
801
802                 if( err != LDAP_OPT_SUCCESS ) {
803                         fprintf( stderr, "Could not set ManageDSAit %scontrol\n",
804                                 c.ldctl_iscritical ? "critical " : "" );
805                         if( c.ldctl_iscritical ) {
806                                 exit( EXIT_FAILURE );
807                         }
808                 }
809         }
810
811         if ( verbose ) {
812                 fprintf( stderr, "filter%s: %s\nrequesting: ",
813                         infile != NULL ? " pattern" : "",
814                         filtpattern );
815
816                 if ( attrs == NULL ) {
817                         fprintf( stderr, "ALL" );
818                 } else {
819                         for ( i = 0; attrs[ i ] != NULL; ++i ) {
820                                 fprintf( stderr, "%s ", attrs[ i ] );
821                         }
822                 }
823                 fprintf( stderr, "\n" );
824         }
825
826         if (ldif < 3 ) {
827                 printf( "version: %d\n\n", ldif ? 1 : 2 );
828         }
829
830         if (ldif < 2 ) {
831                 printf( "#\n# filter%s: %s\n# requesting: ",
832                         infile != NULL ? " pattern" : "",
833                         filtpattern );
834
835                 if ( attrs == NULL ) {
836                         printf( "ALL" );
837                 } else {
838                         for ( i = 0; attrs[ i ] != NULL; ++i ) {
839                                 printf( "%s ", attrs[ i ] );
840                         }
841                 }
842
843                 if ( manageDSAit ) {
844                         printf("\n# with manageDSAit %scontrol",
845                                 manageDSAit > 1 ? "critical " : "" );
846                 }
847
848                 printf( "\n#\n\n" );
849         }
850
851         if ( infile == NULL ) {
852                 rc = dosearch( ld, base, scope, NULL, filtpattern,
853                         attrs, attrsonly, NULL, NULL, NULL, -1 );
854
855         } else {
856                 rc = 0;
857                 first = 1;
858                 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) { 
859                         line[ strlen( line ) - 1 ] = '\0';
860                         if ( !first ) {
861                                 putchar( '\n' );
862                         } else {
863                                 first = 0;
864                         }
865                         rc = dosearch( ld, base, scope, filtpattern, line,
866                                 attrs, attrsonly, NULL, NULL, NULL, -1 );
867                 }
868                 if ( fp != stdin ) {
869                         fclose( fp );
870                 }
871         }
872
873         ldap_unbind( ld );
874         return( rc );
875 }
876
877
878 static int dosearch(
879         LDAP    *ld,
880         char    *base,
881         int             scope,
882         char    *filtpatt,
883         char    *value,
884         char    **attrs,
885         int             attrsonly,
886         LDAPControl **sctrls,
887         LDAPControl **cctrls,
888         struct timeval *timelimit,
889         int sizelimit )
890 {
891         char            filter[ BUFSIZ ];
892         int                     rc;
893         int                     nresponses;
894         int                     nentries;
895         int                     nreferences;
896         int                     nextended;
897         int                     npartial;
898         LDAPMessage             *res, *msg;
899         ber_int_t       msgid;
900
901         if( filtpatt != NULL ) {
902                 sprintf( filter, filtpatt, value );
903
904                 if ( verbose ) {
905                         fprintf( stderr, "filter is: (%s)\n", filter );
906                 }
907
908                 if( ldif < 2 ) {
909                         printf( "#\n# filter: %s\n#\n", filter );
910                 }
911
912         } else {
913                 sprintf( filter, "%s", value );
914         }
915
916         if ( not ) {
917                 return LDAP_SUCCESS;
918         }
919
920         rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
921                 sctrls, cctrls, timelimit, sizelimit, &msgid );
922
923         if( rc != LDAP_SUCCESS ) {
924                 fprintf( stderr, "%s: ldap_search_ext: %s (%d)\n",
925                         prog, ldap_err2string( rc ), rc );
926                 return( rc );
927         }
928
929         nresponses = nentries = nreferences = nextended = npartial = 0;
930
931         res = NULL;
932
933         while ((rc = ldap_result( ld, LDAP_RES_ANY,
934                 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
935                 NULL, &res )) > 0 )
936         {
937                 if( sortattr ) {
938                         (void) ldap_sort_entries( ld, &res,
939                                 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
940                 }
941
942                 for ( msg = ldap_first_message( ld, res );
943                         msg != NULL;
944                         msg = ldap_next_message( ld, msg ) )
945                 {
946                         if( nresponses++ ) putchar('\n');
947
948                         switch( ldap_msgtype( msg ) ) {
949                         case LDAP_RES_SEARCH_ENTRY:
950                                 nentries++;
951                                 print_entry( ld, msg, attrsonly );
952                                 break;
953
954                         case LDAP_RES_SEARCH_REFERENCE:
955                                 nreferences++;
956                                 print_reference( ld, msg );
957                                 break;
958
959                         case LDAP_RES_EXTENDED:
960                                 nextended++;
961                                 print_extended( ld, msg );
962
963                                 if( ldap_msgid( msg ) == 0 ) {
964                                         /* unsolicited extended operation */
965                                         goto done;
966                                 }
967                                 break;
968
969                         case LDAP_RES_EXTENDED_PARTIAL:
970                                 npartial++;
971                                 print_partial( ld, msg );
972                                 break;
973
974                         case LDAP_RES_SEARCH_RESULT:
975                                 rc = print_result( ld, msg, 1 );
976                                 goto done;
977                         }
978                 }
979
980                 ldap_msgfree( res );
981         }
982
983         if ( rc == -1 ) {
984                 ldap_perror( ld, "ldap_result" );
985                 return( rc );
986         }
987
988 done:
989         if ( ldif < 2 ) {
990                 printf( "\n# numResponses: %d\n", nresponses );
991                 if( nentries ) printf( "# numEntries: %d\n", nentries );
992                 if( nextended ) printf( "# numExtended: %d\n", nextended );
993                 if( npartial ) printf( "# numPartial: %d\n", npartial );
994                 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
995         }
996
997         return( rc );
998 }
999
1000 static void
1001 print_entry(
1002         LDAP    *ld,
1003         LDAPMessage     *entry,
1004         int             attrsonly)
1005 {
1006         char            *a, *dn, *ufn;
1007         char    tmpfname[ 256 ];
1008         char    url[ 256 ];
1009         int                     i, rc;
1010         BerElement              *ber = NULL;
1011         struct berval   **bvals;
1012         LDAPControl **ctrls = NULL;
1013         FILE            *tmpfp;
1014
1015         dn = ldap_get_dn( ld, entry );
1016         ufn = NULL;
1017
1018         if ( ldif < 2 ) {
1019                 ufn = ldap_dn2ufn( dn );
1020                 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1021         }
1022         write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
1023
1024         rc = ldap_get_entry_controls( ld, entry, &ctrls );
1025
1026         if( rc != LDAP_SUCCESS ) {
1027                 fprintf(stderr, "print_entry: %d\n", rc );
1028                 ldap_perror( ld, "ldap_get_entry_controls" );
1029                 exit( EXIT_FAILURE );
1030         }
1031
1032         if( ctrls ) {
1033                 print_ctrls( ctrls );
1034                 ldap_controls_free( ctrls );
1035         }
1036
1037         if ( includeufn ) {
1038                 if( ufn == NULL ) {
1039                         ufn = ldap_dn2ufn( dn );
1040                 }
1041                 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1042         }
1043
1044         if( ufn != NULL ) ldap_memfree( ufn );
1045         ldap_memfree( dn );
1046
1047         for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1048                 a = ldap_next_attribute( ld, entry, ber ) )
1049         {
1050                 if ( attrsonly ) {
1051                         write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
1052
1053                 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1054                         for ( i = 0; bvals[i] != NULL; i++ ) {
1055                                 if ( vals2tmp > 1 || ( vals2tmp
1056                                         && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1057                                 {
1058                                         int tmpfd;
1059                                         /* write value to file */
1060                                         sprintf( tmpfname, "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1061                                                 tmpdir, a );
1062                                         tmpfp = NULL;
1063
1064                                         if ( mktemp( tmpfname ) == NULL ) {
1065                                                 perror( tmpfname );
1066                                                 continue;
1067                                         }
1068
1069                                         if (( tmpfd = open( tmpfname, O_WRONLY|O_CREAT|O_EXCL, 0600 )) == -1 ) {
1070                                                 perror( tmpfname );
1071                                                 continue;
1072                                         }
1073
1074                                         if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1075                                                 perror( tmpfname );
1076                                                 continue;
1077                                         }
1078
1079                                         if ( fwrite( bvals[ i ]->bv_val,
1080                                                 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1081                                         {
1082                                                 perror( tmpfname );
1083                                                 fclose( tmpfp );
1084                                                 continue;
1085                                         }
1086
1087                                         fclose( tmpfp );
1088
1089                                         sprintf( url, "%s%s", urlpre,
1090                                                 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1091
1092                                         write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1093
1094                                 } else {
1095                                         write_ldif( LDIF_PUT_VALUE, a,
1096                                                 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1097                                 }
1098                         }
1099                         ber_bvecfree( bvals );
1100                 }
1101         }
1102
1103         if( ber != NULL ) {
1104                 ber_free( ber, 0 );
1105         }
1106 }
1107
1108 static void print_reference(
1109         LDAP *ld,
1110         LDAPMessage *reference )
1111 {
1112         int rc;
1113         char **refs = NULL;
1114         LDAPControl **ctrls;
1115
1116         if( ldif < 2 ) {
1117                 printf("# search reference\n");
1118         }
1119
1120         rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1121
1122         if( rc != LDAP_SUCCESS ) {
1123                 ldap_perror(ld, "ldap_parse_reference");
1124                 exit( EXIT_FAILURE );
1125         }
1126
1127         if( refs ) {
1128                 int i;
1129                 for( i=0; refs[i] != NULL; i++ ) {
1130                         write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1131                                 "ref", refs[i], strlen(refs[i]) );
1132                 }
1133                 ber_memvfree( (void **) refs );
1134         }
1135
1136         if( ctrls ) {
1137                 print_ctrls( ctrls );
1138                 ldap_controls_free( ctrls );
1139         }
1140 }
1141
1142 static void print_extended(
1143         LDAP *ld,
1144         LDAPMessage *extended )
1145 {
1146         int rc;
1147         char *retoid = NULL;
1148         struct berval *retdata = NULL;
1149
1150         if( ldif < 2 ) {
1151                 printf("# extended result response\n");
1152         }
1153
1154         rc = ldap_parse_extended_result( ld, extended,
1155                 &retoid, &retdata, 0 );
1156
1157         if( rc != LDAP_SUCCESS ) {
1158                 ldap_perror(ld, "ldap_parse_extended_result");
1159                 exit( EXIT_FAILURE );
1160         }
1161
1162         write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1163                 "extended", retoid, retoid ? strlen(retoid) : 0 );
1164         ber_memfree( retoid );
1165
1166         if(retdata) {
1167                 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1168                         "data", retdata->bv_val, retdata->bv_len );
1169                 ber_bvfree( retdata );
1170         }
1171
1172         print_result( ld, extended, 0 );
1173 }
1174
1175 static void print_partial(
1176         LDAP *ld,
1177         LDAPMessage *partial )
1178 {
1179         int rc;
1180         char *retoid = NULL;
1181         struct berval *retdata = NULL;
1182         LDAPControl **ctrls = NULL;
1183
1184         if( ldif < 2 ) {
1185                 printf("# extended partial response\n");
1186         }
1187
1188         rc = ldap_parse_extended_partial( ld, partial,
1189                 &retoid, &retdata, &ctrls, 0 );
1190
1191         if( rc != LDAP_SUCCESS ) {
1192                 ldap_perror(ld, "ldap_parse_extended_partial");
1193                 exit( EXIT_FAILURE );
1194         }
1195
1196         write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1197                 "partial", retoid, retoid ? strlen(retoid) : 0 );
1198
1199         ber_memfree( retoid );
1200
1201         if( retdata ) {
1202                 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1203                         "data", 
1204                         retdata->bv_val, retdata->bv_len );
1205
1206                 ber_bvfree( retdata );
1207         }
1208
1209         if( ctrls ) {
1210                 print_ctrls( ctrls );
1211                 ldap_controls_free( ctrls );
1212         }
1213 }
1214
1215 static int print_result(
1216         LDAP *ld,
1217         LDAPMessage *result, int search )
1218 {
1219         int rc;
1220         int err;
1221         char *matcheddn = NULL;
1222         char *text = NULL;
1223         char **refs = NULL;
1224         LDAPControl **ctrls = NULL;
1225
1226         if( search ) {
1227                 if ( ldif < 2 ) {
1228                         printf("# search result\n");
1229                 }
1230                 if ( ldif < 1 ) {
1231                         printf("%s: %d\n", "search", ldap_msgid(result) );
1232                 }
1233         }
1234
1235         rc = ldap_parse_result( ld, result,
1236                 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1237
1238         if( rc != LDAP_SUCCESS ) {
1239                 ldap_perror(ld, "ldap_parse_result");
1240                 exit( EXIT_FAILURE );
1241         }
1242
1243
1244         if( !ldif ) {
1245                 printf( "result: %d %s\n", err, ldap_err2string(err) );
1246
1247         } else if ( err != LDAP_SUCCESS ) {
1248                 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1249         }
1250
1251         if( matcheddn && *matcheddn ) {
1252                 if( !ldif ) {
1253                         write_ldif( LDIF_PUT_VALUE,
1254                                 "matchedDN", matcheddn, strlen(matcheddn) );
1255                 } else {
1256                         fprintf( stderr, "Matched DN: %s\n", matcheddn );
1257                 }
1258
1259                 ber_memfree( matcheddn );
1260         }
1261
1262         if( text && *text ) {
1263                 if( !ldif ) {
1264                         write_ldif( LDIF_PUT_TEXT, "text",
1265                                 text, strlen(text) );
1266                 } else {
1267                         fprintf( stderr, "Additional information: %s\n", text );
1268                 }
1269
1270                 ber_memfree( text );
1271         }
1272
1273         if( refs ) {
1274                 int i;
1275                 for( i=0; refs[i] != NULL; i++ ) {
1276                         if( !ldif ) {
1277                                 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1278                         } else {
1279                                 fprintf( stderr, "Referral: %s\n", refs[i] );
1280                         }
1281                 }
1282
1283                 ber_memvfree( (void **) refs );
1284         }
1285
1286         if( ctrls ) {
1287                 print_ctrls( ctrls );
1288                 ldap_controls_free( ctrls );
1289         }
1290
1291         return err;
1292 }
1293
1294 static void print_ctrls(
1295         LDAPControl **ctrls )
1296 {
1297         int i;
1298         for(i=0; ctrls[i] != NULL; i++ ) {
1299                 /* control: OID criticality base64value */
1300                 struct berval *b64 = NULL;
1301                 ber_len_t len;
1302                 char *str;
1303                         
1304                 len = strlen( ctrls[i]->ldctl_oid );
1305
1306                 /* add enough for space after OID and the critical value itself */
1307                 len += ctrls[i]->ldctl_iscritical
1308                         ? sizeof("true") : sizeof("false");
1309
1310                 /* convert to base64 */
1311                 if( ctrls[i]->ldctl_value.bv_len ) {
1312                         b64 = ber_memalloc( sizeof(struct berval) );
1313                         
1314                         b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1315                                 ctrls[i]->ldctl_value.bv_len ) + 1;
1316                         b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1317
1318                         b64->bv_len = lutil_b64_ntop(
1319                                 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1320                                 b64->bv_val, b64->bv_len );
1321                 }
1322
1323                 if( b64 ) {
1324                         len += 1 + b64->bv_len;
1325                 }
1326
1327                 str = malloc( len + 1 );
1328                 strcpy( str, ctrls[i]->ldctl_oid );
1329                 strcat( str, ctrls[i]->ldctl_iscritical
1330                         ? " true" : " false" );
1331
1332                 if( b64 ) {
1333                         strcat(str, " ");
1334                         strcat(str, b64->bv_val );
1335                 }
1336
1337                 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1338                         "control", str, len );
1339
1340                 free( str );
1341                 ber_bvfree( b64 );
1342         }
1343 }
1344
1345 static int
1346 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1347 {
1348         char    *ldif;
1349
1350         if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1351                 return( -1 );
1352         }
1353
1354         fputs( ldif, stdout );
1355         ber_memfree( ldif );
1356
1357         return( 0 );
1358 }