]> git.sur5r.net Git - openldap/blob - clients/tools/ldapsearch.c
6ca5d274ddcb8178ccd86e7b2f1566dfeb14f472
[openldap] / clients / tools / ldapsearch.c
1 /* ldapsearch -- a tool for searching LDAP directories */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2007 The OpenLDAP Foundation.
6  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
7  * Portions Copyright 1998-2001 Net Boolean Incorporated.
8  * Portions Copyright 2001-2003 IBM Corporation.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted only as authorized by the OpenLDAP
13  * Public License.
14  *
15  * A copy of this license is available in the file LICENSE in the
16  * top-level directory of the distribution or, alternatively, at
17  * <http://www.OpenLDAP.org/license.html>.
18  */
19 /* Portions Copyright (c) 1992-1996 Regents of the University of Michigan.
20  * All rights reserved.
21  *
22  * Redistribution and use in source and binary forms are permitted
23  * provided that this notice is preserved and that due credit is given
24  * to the University of Michigan at Ann Arbor.  The name of the
25  * University may not be used to endorse or promote products derived
26  * from this software without specific prior written permission.  This
27  * software is provided ``as is'' without express or implied warranty.
28  */
29 /* ACKNOWLEDGEMENTS:
30  * This work was originally developed by the University of Michigan
31  * (as part of U-MICH LDAP).  Additional significant contributors
32  * include:
33  *   Jong Hyuk Choi
34  *   Lynn Moss
35  *   Mikhail Sahalaev
36  *   Kurt D. Zeilenga
37  */
38
39 #include "portable.h"
40
41 #include <stdio.h>
42
43 #include <ac/stdlib.h>
44
45 #include <ac/ctype.h>
46 #include <ac/string.h>
47 #include <ac/unistd.h>
48 #include <ac/errno.h>
49 #include <sys/stat.h>
50
51 #include <ac/signal.h>
52
53 #ifdef HAVE_FCNTL_H
54 #include <fcntl.h>
55 #endif
56 #ifdef HAVE_SYS_TYPES_H
57 #include <sys/types.h>
58 #endif
59 #ifdef HAVE_IO_H
60 #include <io.h>
61 #endif
62
63 #include <ldap.h>
64
65 #include "ldif.h"
66 #include "lutil.h"
67 #include "lutil_ldap.h"
68 #include "ldap_defaults.h"
69 #include "ldap_log.h"
70 #include "ldap_pvt.h"
71
72 #include "common.h"
73
74 #if !LDAP_DEPRECATED
75 /*
76  * NOTE: we use this deprecated function only because
77  * we want ldapsearch to provide some client-side sorting 
78  * capability.
79  */
80 /* from ldap.h */
81 typedef int (LDAP_SORT_AD_CMP_PROC) LDAP_P(( /* deprecated */
82         LDAP_CONST char *left,
83         LDAP_CONST char *right ));
84
85 LDAP_F( int )   /* deprecated */
86 ldap_sort_entries LDAP_P(( LDAP *ld,
87         LDAPMessage **chain,
88         LDAP_CONST char *attr,
89         LDAP_SORT_AD_CMP_PROC *cmp ));
90 #endif
91
92 static int scope = LDAP_SCOPE_SUBTREE;
93 static int deref = -1;
94 static int attrsonly;
95 static int timelimit = -1;
96 static int sizelimit = -1;
97
98 static char *control;
99
100 static char *def_tmpdir;
101 static char *def_urlpre;
102
103 #if defined(__CYGWIN__) || defined(__MINGW32__)
104 /* Turn off commandline globbing, otherwise you cannot search for
105  * attribute '*'
106  */
107 int _CRT_glob = 0;
108 #endif
109
110 void
111 usage( void )
112 {
113         fprintf( stderr, _("usage: %s [options] [filter [attributes...]]\nwhere:\n"), prog);
114         fprintf( stderr, _("  filter\tRFC 4515 compliant LDAP search filter\n"));
115         fprintf( stderr, _("  attributes\twhitespace-separated list of attribute descriptions\n"));
116         fprintf( stderr, _("    which may include:\n"));
117         fprintf( stderr, _("      1.1   no attributes\n"));
118         fprintf( stderr, _("      *     all user attributes\n"));
119         fprintf( stderr, _("      +     all operational attributes\n"));
120
121
122         fprintf( stderr, _("Search options:\n"));
123         fprintf( stderr, _("  -a deref   one of never (default), always, search, or find\n"));
124         fprintf( stderr, _("  -A         retrieve attribute names only (no values)\n"));
125         fprintf( stderr, _("  -b basedn  base dn for search\n"));
126         fprintf( stderr, _("  -E [!]<ext>[=<extparam>] search extensions (! indicates criticality)\n"));
127         fprintf( stderr, _("             [!]domainScope              (domain scope)\n"));
128         fprintf( stderr, _("             !dontUseCopy                (Don't Use Copy)\n"));
129         fprintf( stderr, _("             [!]mv=<filter>              (matched values filter)\n"));
130         fprintf( stderr, _("             [!]pr=<size>[/prompt|noprompt]   (paged results/prompt)\n"));
131         fprintf( stderr, _("             [!]subentries[=true|false]  (subentries)\n"));
132         fprintf( stderr, _("             [!]sync=ro[/<cookie>]            (LDAP Sync refreshOnly)\n"));
133         fprintf( stderr, _("                     rp[/<cookie>][/<slimit>] (LDAP Sync refreshAndPersist)\n"));
134         fprintf( stderr, _("             [!]<oid>=:<value>           (generic control; no response handling)\n"));
135         fprintf( stderr, _("  -F prefix  URL prefix for files (default: %s)\n"), def_urlpre);
136         fprintf( stderr, _("  -l limit   time limit (in seconds, or \"none\" or \"max\") for search\n"));
137         fprintf( stderr, _("  -L         print responses in LDIFv1 format\n"));
138         fprintf( stderr, _("  -LL        print responses in LDIF format without comments\n"));
139         fprintf( stderr, _("  -LLL       print responses in LDIF format without comments\n"));
140         fprintf( stderr, _("             and version\n"));
141         fprintf( stderr, _("  -s scope   one of base, one, sub or children (search scope)\n"));
142         fprintf( stderr, _("  -S attr    sort the results by attribute `attr'\n"));
143         fprintf( stderr, _("  -t         write binary values to files in temporary directory\n"));
144         fprintf( stderr, _("  -tt        write all values to files in temporary directory\n"));
145         fprintf( stderr, _("  -T path    write files to directory specified by path (default: %s)\n"), def_tmpdir);
146         fprintf( stderr, _("  -u         include User Friendly entry names in the output\n"));
147         fprintf( stderr, _("  -z limit   size limit (in entries, or \"none\" or \"max\") for search\n"));
148         tool_common_usage();
149         exit( EXIT_FAILURE );
150 }
151
152 static void print_entry LDAP_P((
153         LDAP    *ld,
154         LDAPMessage     *entry,
155         int             attrsonly));
156
157 static void print_reference(
158         LDAP *ld,
159         LDAPMessage *reference );
160
161 static void print_extended(
162         LDAP *ld,
163         LDAPMessage *extended );
164
165 static void print_partial(
166         LDAP *ld,
167         LDAPMessage *partial );
168
169 static int print_result(
170         LDAP *ld,
171         LDAPMessage *result,
172         int search );
173
174 static int dosearch LDAP_P((
175         LDAP    *ld,
176         char    *base,
177         int             scope,
178         char    *filtpatt,
179         char    *value,
180         char    **attrs,
181         int             attrsonly,
182         LDAPControl **sctrls,
183         LDAPControl **cctrls,
184         struct timeval *timeout,
185         int     sizelimit ));
186
187 static char *tmpdir = NULL;
188 static char *urlpre = NULL;
189 static char     *base = NULL;
190 static char     *sortattr = NULL;
191 static int  includeufn, vals2tmp = 0;
192
193 static int subentries = 0, valuesReturnFilter = 0;
194 static char     *vrFilter = NULL;
195
196 #ifdef LDAP_CONTROL_DONTUSECOPY
197 static int dontUseCopy = 0;
198 #endif
199
200 static int domainScope = 0;
201
202 static int ldapsync = 0;
203 static struct berval sync_cookie = { 0, NULL };
204 static int sync_slimit = -1;
205
206 /* cookie and morePagedResults moved to common.c */
207 static int pagedResults = 0;
208 static int pagePrompt = 1;
209 static ber_int_t pageSize = 0;
210 static ber_int_t entriesLeft = 0;
211 static int npagedresponses;
212 static int npagedentries;
213 static int npagedreferences;
214 static int npagedextended;
215 static int npagedpartial;
216
217 static LDAPControl *c = NULL;
218 static int nctrls = 0;
219 static int save_nctrls = 0;
220
221 static int
222 ctrl_add( void )
223 {
224         LDAPControl     *tmpc;
225
226         nctrls++;
227         tmpc = realloc( c, sizeof( LDAPControl ) * nctrls );
228         if ( tmpc == NULL ) {
229                 nctrls--;
230                 fprintf( stderr,
231                         _("unable to make room for control; out of memory?\n"));
232                 return -1;
233         }
234         c = tmpc;
235
236         return 0;
237 }
238
239 static void
240 urlize(char *url)
241 {
242         char *p;
243
244         if (*LDAP_DIRSEP != '/') {
245                 for (p = url; *p; p++) {
246                         if (*p == *LDAP_DIRSEP)
247                                 *p = '/';
248                 }
249         }
250 }
251
252
253 const char options[] = "a:Ab:cE:F:l:Ls:S:tT:uz:"
254         "Cd:D:e:f:h:H:IMnO:o:p:P:QR:U:vVw:WxX:y:Y:Z";
255
256 int
257 handle_private_option( int i )
258 {
259         int crit, ival;
260         char *cvalue, *next;
261         switch ( i ) {
262         case 'a':       /* set alias deref option */
263                 if ( strcasecmp( optarg, "never" ) == 0 ) {
264                         deref = LDAP_DEREF_NEVER;
265                 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
266                         deref = LDAP_DEREF_SEARCHING;
267                 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
268                         deref = LDAP_DEREF_FINDING;
269                 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
270                         deref = LDAP_DEREF_ALWAYS;
271                 } else {
272                         fprintf( stderr,
273                                 _("alias deref should be never, search, find, or always\n") );
274                         usage();
275                 }
276                 break;
277         case 'A':       /* retrieve attribute names only -- no values */
278                 ++attrsonly;
279                 break;
280         case 'b': /* search base */
281                 base = ber_strdup( optarg );
282                 break;
283         case 'E': /* search extensions */
284                 if( protocol == LDAP_VERSION2 ) {
285                         fprintf( stderr, _("%s: -E incompatible with LDAPv%d\n"),
286                                 prog, protocol );
287                         exit( EXIT_FAILURE );
288                 }
289
290                 /* should be extended to support comma separated list of
291                  *      [!]key[=value] parameters, e.g.  -E !foo,bar=567
292                  */
293
294                 crit = 0;
295                 cvalue = NULL;
296                 if( optarg[0] == '!' ) {
297                         crit = 1;
298                         optarg++;
299                 }
300
301                 control = ber_strdup( optarg );
302                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
303                         *cvalue++ = '\0';
304                 }
305
306                 if ( strcasecmp( control, "mv" ) == 0 ) {
307                         /* ValuesReturnFilter control */
308                         if( valuesReturnFilter ) {
309                                 fprintf( stderr,
310                                         _("ValuesReturnFilter previously specified\n"));
311                                 exit( EXIT_FAILURE );
312                         }
313                         valuesReturnFilter= 1 + crit;
314
315                         if ( cvalue == NULL ) {
316                                 fprintf( stderr,
317                                         _("missing filter in ValuesReturnFilter control\n"));
318                                 exit( EXIT_FAILURE );
319                         }
320
321                         vrFilter = cvalue;
322                         protocol = LDAP_VERSION3;
323
324                 } else if ( strcasecmp( control, "pr" ) == 0 ) {
325                         int num, tmp;
326                         /* PagedResults control */
327                         if ( pagedResults != 0 ) {
328                                 fprintf( stderr,
329                                         _("PagedResultsControl previously specified\n") );
330                                 exit( EXIT_FAILURE );
331                         }
332
333                         if( cvalue != NULL ) {
334                                 char *promptp;
335
336                                 promptp = strchr( cvalue, '/' );
337                                 if ( promptp != NULL ) {
338                                         *promptp++ = '\0';
339                                         if ( strcasecmp( promptp, "prompt" ) == 0 ) {
340                                                 pagePrompt = 1;
341                                         } else if ( strcasecmp( promptp, "noprompt" ) == 0) {
342                                                 pagePrompt = 0;
343                                         } else {
344                                                 fprintf( stderr,
345                                                         _("Invalid value for PagedResultsControl,"
346                                                         " %s/%s.\n"), cvalue, promptp );
347                                                 exit( EXIT_FAILURE );
348                                         }
349                                 }
350                                 num = sscanf( cvalue, "%d", &tmp );
351                                 if ( num != 1 ) {
352                                         fprintf( stderr,
353                                                 _("Invalid value for PagedResultsControl, %s.\n"),
354                                                 cvalue );
355                                         exit( EXIT_FAILURE );
356                                 }
357                         } else {
358                                 fprintf(stderr, _("Invalid value for PagedResultsControl.\n"));
359                                 exit( EXIT_FAILURE );
360                         }
361                         pageSize = (ber_int_t) tmp;
362                         pagedResults = 1 + crit;
363
364 #ifdef LDAP_CONTROL_DONTUSECOPY
365                 } else if ( strcasecmp( control, "dontUseCopy" ) == 0 ) {
366                         if( dontUseCopy ) {
367                                 fprintf( stderr,
368                                         _("dontUseCopy control previously specified\n"));
369                                 exit( EXIT_FAILURE );
370                         }
371                         if( cvalue != NULL ) {
372                                 fprintf( stderr,
373                                  _("dontUseCopy: no control value expected\n") );
374                                 usage();
375                         }
376                         if( !crit ) {
377                                 fprintf( stderr,
378                                  _("dontUseCopy: critical flag required\n") );
379                                 usage();
380                         }
381
382                         dontUseCopy = 1 + crit;
383 #endif
384                 } else if ( strcasecmp( control, "domainScope" ) == 0 ) {
385                         if( domainScope ) {
386                                 fprintf( stderr,
387                                         _("domainScope control previously specified\n"));
388                                 exit( EXIT_FAILURE );
389                         }
390                         if( cvalue != NULL ) {
391                                 fprintf( stderr,
392                                  _("domainScope: no control value expected\n") );
393                                 usage();
394                         }
395
396                         domainScope = 1 + crit;
397
398                 } else if ( strcasecmp( control, "subentries" ) == 0 ) {
399                         if( subentries ) {
400                                 fprintf( stderr,
401                                         _("subentries control previously specified\n"));
402                                 exit( EXIT_FAILURE );
403                         }
404                         if( cvalue == NULL || strcasecmp( cvalue, "true") == 0 ) {
405                                 subentries = 2;
406                         } else if ( strcasecmp( cvalue, "false") == 0 ) {
407                                 subentries = 1;
408                         } else {
409                                 fprintf( stderr,
410                                         _("subentries control value \"%s\" invalid\n"),
411                                         cvalue );
412                                 exit( EXIT_FAILURE );
413                         }
414                         if( crit ) subentries *= -1;
415
416                 } else if ( strcasecmp( control, "sync" ) == 0 ) {
417                         char *cookiep;
418                         char *slimitp;
419                         if ( ldapsync ) {
420                                 fprintf( stderr, _("sync control previously specified\n") );
421                                 exit( EXIT_FAILURE );
422                         }
423                         if ( cvalue == NULL ) {
424                                 fprintf( stderr, _("missing specification of sync control\n"));
425                                 exit( EXIT_FAILURE );
426                         }
427                         if ( strncasecmp( cvalue, "ro", 2 ) == 0 ) {
428                                 ldapsync = LDAP_SYNC_REFRESH_ONLY;
429                                 cookiep = strchr( cvalue, '/' );
430                                 if ( cookiep != NULL ) {
431                                         cookiep++;
432                                         if ( *cookiep != '\0' ) {
433                                                 ber_str2bv( cookiep, 0, 0, &sync_cookie );
434                                         }
435                                 }
436                         } else if ( strncasecmp( cvalue, "rp", 2 ) == 0 ) {
437                                 ldapsync = LDAP_SYNC_REFRESH_AND_PERSIST;
438                                 cookiep = strchr( cvalue, '/' );
439                                 if ( cookiep != NULL ) {
440                                         *cookiep++ = '\0';      
441                                         cvalue = cookiep;
442                                 }
443                                 slimitp = strchr( cvalue, '/' );
444                                 if ( slimitp != NULL ) {
445                                         *slimitp++ = '\0';
446                                 }
447                                 if ( cookiep != NULL && *cookiep != '\0' )
448                                         ber_str2bv( cookiep, 0, 0, &sync_cookie );
449                                 if ( slimitp != NULL && *slimitp != '\0' ) {
450                                         ival = strtol( slimitp, &next, 10 );
451                                         if ( next == NULL || next[0] != '\0' ) {
452                                                 fprintf( stderr, _("Unable to parse sync control value \"%s\"\n"), slimitp );
453                                                 exit( EXIT_FAILURE );
454                                         }
455                                         sync_slimit = ival;
456                                 }
457                         } else {
458                                 fprintf( stderr, _("sync control value \"%s\" invalid\n"),
459                                         cvalue );
460                                 exit( EXIT_FAILURE );
461                         }
462                         if ( crit ) ldapsync *= -1;
463
464                 } else if ( tool_is_oid( control ) ) {
465                         if ( ctrl_add() ) {
466                                 exit( EXIT_FAILURE );
467                         }
468
469                         /* OID */
470                         c[ nctrls - 1 ].ldctl_oid = control;
471
472                         /* value */
473                         if ( cvalue == NULL ) {
474                                 c[ nctrls - 1 ].ldctl_value.bv_val = NULL;
475                                 c[ nctrls - 1 ].ldctl_value.bv_len = 0;
476
477                         } else if ( cvalue[ 0 ] == ':' ) {
478                                 struct berval   type;
479                                 struct berval   value;
480                                 int             freeval;
481
482                                 cvalue++;
483
484                                 /* dummy type "x"
485                                  * to use ldif_parse_line2() */
486                                 cvalue[ -2 ] = 'x';
487                                 ldif_parse_line2( &cvalue[ -2 ], &type,
488                                         &value, &freeval );
489                                 cvalue[ -2 ] = '\0';
490
491                                 if ( freeval ) {
492                                         c[ nctrls - 1 ].ldctl_value = value;
493
494                                 } else {
495                                         ber_dupbv( &c[ nctrls - 1 ].ldctl_value, &value );
496                                 }
497                         }
498
499                         /* criticality */
500                         c[ nctrls - 1 ].ldctl_iscritical = crit;
501
502                 } else {
503                         fprintf( stderr, _("Invalid search extension name: %s\n"),
504                                 control );
505                         usage();
506                 }
507                 break;
508         case 'F':       /* uri prefix */
509                 if( urlpre ) free( urlpre );
510                 urlpre = strdup( optarg );
511                 break;
512         case 'l':       /* time limit */
513                 if ( strcasecmp( optarg, "none" ) == 0 ) {
514                         timelimit = 0;
515
516                 } else if ( strcasecmp( optarg, "max" ) == 0 ) {
517                         timelimit = LDAP_MAXINT;
518
519                 } else {
520                         ival = strtol( optarg, &next, 10 );
521                         if ( next == NULL || next[0] != '\0' ) {
522                                 fprintf( stderr,
523                                         _("Unable to parse time limit \"%s\"\n"), optarg );
524                                 exit( EXIT_FAILURE );
525                         }
526                         timelimit = ival;
527                 }
528                 if( timelimit < 0 || timelimit > LDAP_MAXINT ) {
529                         fprintf( stderr, _("%s: invalid timelimit (%d) specified\n"),
530                                 prog, timelimit );
531                         exit( EXIT_FAILURE );
532                 }
533                 break;
534         case 'L':       /* print entries in LDIF format */
535                 ++ldif;
536                 break;
537         case 's':       /* search scope */
538                 if ( strncasecmp( optarg, "base", sizeof("base")-1 ) == 0 ) {
539                         scope = LDAP_SCOPE_BASE;
540                 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
541                         scope = LDAP_SCOPE_ONELEVEL;
542                 } else if (( strcasecmp( optarg, "subordinate" ) == 0 )
543                         || ( strcasecmp( optarg, "children" ) == 0 ))
544                 {
545                         scope = LDAP_SCOPE_SUBORDINATE;
546                 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
547                         scope = LDAP_SCOPE_SUBTREE;
548                 } else {
549                         fprintf( stderr, _("scope should be base, one, or sub\n") );
550                         usage();
551                 }
552                 break;
553         case 'S':       /* sort attribute */
554                 sortattr = strdup( optarg );
555                 break;
556         case 't':       /* write attribute values to TMPDIR files */
557                 ++vals2tmp;
558                 break;
559         case 'T':       /* tmpdir */
560                 if( tmpdir ) free( tmpdir );
561                 tmpdir = strdup( optarg );
562                 break;
563         case 'u':       /* include UFN */
564                 ++includeufn;
565                 break;
566         case 'z':       /* size limit */
567                 if ( strcasecmp( optarg, "none" ) == 0 ) {
568                         sizelimit = 0;
569
570                 } else if ( strcasecmp( optarg, "max" ) == 0 ) {
571                         sizelimit = LDAP_MAXINT;
572
573                 } else {
574                         ival = strtol( optarg, &next, 10 );
575                         if ( next == NULL || next[0] != '\0' ) {
576                                 fprintf( stderr,
577                                         _("Unable to parse size limit \"%s\"\n"), optarg );
578                                 exit( EXIT_FAILURE );
579                         }
580                         sizelimit = ival;
581                 }
582                 if( sizelimit < 0 || sizelimit > LDAP_MAXINT ) {
583                         fprintf( stderr, _("%s: invalid sizelimit (%d) specified\n"),
584                                 prog, sizelimit );
585                         exit( EXIT_FAILURE );
586                 }
587                 break;
588         default:
589                 return 0;
590         }
591         return 1;
592 }
593
594
595 static void
596 private_conn_setup( LDAP *ld )
597 {
598         if (deref != -1 &&
599                 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref )
600                         != LDAP_OPT_SUCCESS )
601         {
602                 fprintf( stderr, _("Could not set LDAP_OPT_DEREF %d\n"), deref );
603                 exit( EXIT_FAILURE );
604         }
605         if (timelimit > 0 &&
606                 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit )
607                         != LDAP_OPT_SUCCESS )
608         {
609                 fprintf( stderr,
610                         _("Could not set LDAP_OPT_TIMELIMIT %d\n"), timelimit );
611                 exit( EXIT_FAILURE );
612         }
613         if (sizelimit > 0 &&
614                 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit )
615                         != LDAP_OPT_SUCCESS )
616         {
617                 fprintf( stderr,
618                         _("Could not set LDAP_OPT_SIZELIMIT %d\n"), sizelimit );
619                 exit( EXIT_FAILURE );
620         }
621 }
622
623 int
624 main( int argc, char **argv )
625 {
626         char            *filtpattern, **attrs = NULL, line[BUFSIZ];
627         FILE            *fp = NULL;
628         int             rc, i, first;
629         LDAP            *ld = NULL;
630         BerElement      *seber = NULL, *vrber = NULL;
631
632         BerElement      *syncber = NULL;
633         struct berval   *syncbvalp = NULL;
634         int             err;
635
636         tool_init( TOOL_SEARCH );
637
638         npagedresponses = npagedentries = npagedreferences =
639                 npagedextended = npagedpartial = 0;
640
641         prog = lutil_progname( "ldapsearch", argc, argv );
642
643         if((def_tmpdir = getenv("TMPDIR")) == NULL &&
644            (def_tmpdir = getenv("TMP")) == NULL &&
645            (def_tmpdir = getenv("TEMP")) == NULL )
646         {
647                 def_tmpdir = LDAP_TMPDIR;
648         }
649
650         if ( !*def_tmpdir )
651                 def_tmpdir = LDAP_TMPDIR;
652
653         def_urlpre = malloc( sizeof("file:////") + strlen(def_tmpdir) );
654
655         if( def_urlpre == NULL ) {
656                 perror( "malloc" );
657                 return EXIT_FAILURE;
658         }
659
660         sprintf( def_urlpre, "file:///%s/",
661                 def_tmpdir[0] == *LDAP_DIRSEP ? &def_tmpdir[1] : def_tmpdir );
662
663         urlize( def_urlpre );
664
665         tool_args( argc, argv );
666
667         if (( argc - optind < 1 ) ||
668                 ( *argv[optind] != '(' /*')'*/ &&
669                 ( strchr( argv[optind], '=' ) == NULL ) ) )
670         {
671                 filtpattern = "(objectclass=*)";
672         } else {
673                 filtpattern = argv[optind++];
674         }
675
676         if ( argv[optind] != NULL ) {
677                 attrs = &argv[optind];
678         }
679
680         if ( infile != NULL ) {
681                 int percent = 0;
682         
683                 if ( infile[0] == '-' && infile[1] == '\0' ) {
684                         fp = stdin;
685                 } else if (( fp = fopen( infile, "r" )) == NULL ) {
686                         perror( infile );
687                         return EXIT_FAILURE;
688                 }
689
690                 for( i=0 ; filtpattern[i] ; i++ ) {
691                         if( filtpattern[i] == '%' ) {
692                                 if( percent ) {
693                                         fprintf( stderr, _("Bad filter pattern \"%s\"\n"),
694                                                 filtpattern );
695                                         return EXIT_FAILURE;
696                                 }
697
698                                 percent++;
699
700                                 if( filtpattern[i+1] != 's' ) {
701                                         fprintf( stderr, _("Bad filter pattern \"%s\"\n"),
702                                                 filtpattern );
703                                         return EXIT_FAILURE;
704                                 }
705                         }
706                 }
707         }
708
709         if ( tmpdir == NULL ) {
710                 tmpdir = def_tmpdir;
711
712                 if ( urlpre == NULL )
713                         urlpre = def_urlpre;
714         }
715
716         if( urlpre == NULL ) {
717                 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
718
719                 if( urlpre == NULL ) {
720                         perror( "malloc" );
721                         return EXIT_FAILURE;
722                 }
723
724                 sprintf( urlpre, "file:///%s/",
725                         tmpdir[0] == *LDAP_DIRSEP ? &tmpdir[1] : tmpdir );
726
727                 urlize( urlpre );
728         }
729
730         if ( debug )
731                 ldif_debug = debug;
732
733         ld = tool_conn_setup( 0, &private_conn_setup );
734
735         if ( pw_file || want_bindpw ) {
736                 if ( pw_file ) {
737                         rc = lutil_get_filed_password( pw_file, &passwd );
738                         if( rc ) return EXIT_FAILURE;
739                 } else {
740                         passwd.bv_val = getpassphrase( _("Enter LDAP Password: ") );
741                         passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
742                 }
743         }
744
745         tool_bind( ld );
746
747 getNextPage:
748         save_nctrls = nctrls;
749         i = nctrls;
750         if ( nctrls > 0
751 #ifdef LDAP_CONTROL_DONTUSECOPY
752                 || dontUseCopy
753 #endif
754                 || domainScope
755                 || pagedResults
756                 || ldapsync
757                 || subentries
758                 || valuesReturnFilter )
759         {
760
761 #ifdef LDAP_CONTROL_DONTUSECOPY
762                 if ( dontUseCopy ) {
763                         if ( ctrl_add() ) {
764                                 return EXIT_FAILURE;
765                         }
766
767                         c[i].ldctl_oid = LDAP_CONTROL_DONTUSECOPY;
768                         c[i].ldctl_value.bv_val = NULL;
769                         c[i].ldctl_value.bv_len = 0;
770                         c[i].ldctl_iscritical = dontUseCopy > 1;
771                         i++;
772                 }
773 #endif
774
775                 if ( domainScope ) {
776                         if ( ctrl_add() ) {
777                                 return EXIT_FAILURE;
778                         }
779
780                         c[i].ldctl_oid = LDAP_CONTROL_X_DOMAIN_SCOPE;
781                         c[i].ldctl_value.bv_val = NULL;
782                         c[i].ldctl_value.bv_len = 0;
783                         c[i].ldctl_iscritical = domainScope > 1;
784                         i++;
785                 }
786
787                 if ( subentries ) {
788                         if ( ctrl_add() ) {
789                                 return EXIT_FAILURE;
790                         }
791
792                         if (( seber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
793                                 return EXIT_FAILURE;
794                         }
795
796                         err = ber_printf( seber, "b", abs(subentries) == 1 ? 0 : 1 );
797                         if ( err == -1 ) {
798                                 ber_free( seber, 1 );
799                                 fprintf( stderr, _("Subentries control encoding error!\n") );
800                                 return EXIT_FAILURE;
801                         }
802
803                         if ( ber_flatten2( seber, &c[i].ldctl_value, 0 ) == -1 ) {
804                                 return EXIT_FAILURE;
805                         }
806
807                         c[i].ldctl_oid = LDAP_CONTROL_SUBENTRIES;
808                         c[i].ldctl_iscritical = subentries < 1;
809                         i++;
810                 }
811
812                 if ( ldapsync ) {
813                         if ( ctrl_add() ) {
814                                 return EXIT_FAILURE;
815                         }
816
817                         if (( syncber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
818                                 return EXIT_FAILURE;
819                         }
820
821                         if ( sync_cookie.bv_len == 0 ) {
822                                 err = ber_printf( syncber, "{e}", abs(ldapsync) );
823                         } else {
824                                 err = ber_printf( syncber, "{eO}", abs(ldapsync),
825                                                         &sync_cookie );
826                         }
827
828                         if ( err == LBER_ERROR ) {
829                                 ber_free( syncber, 1 );
830                                 fprintf( stderr, _("ldap sync control encoding error!\n") );
831                                 return EXIT_FAILURE;
832                         }
833
834                         if ( ber_flatten( syncber, &syncbvalp ) == LBER_ERROR ) {
835                                 return EXIT_FAILURE;
836                         }
837
838                         c[i].ldctl_oid = LDAP_CONTROL_SYNC;
839                         c[i].ldctl_value = (*syncbvalp);
840                         c[i].ldctl_iscritical = ldapsync < 0;
841                         i++;
842                 }
843
844                 if ( valuesReturnFilter ) {
845                         if ( ctrl_add() ) {
846                                 return EXIT_FAILURE;
847                         }
848
849                         if (( vrber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
850                                 return EXIT_FAILURE;
851                         }
852
853                         if ( ( err = ldap_put_vrFilter( vrber, vrFilter ) ) == -1 ) {
854                                 ber_free( vrber, 1 );
855                                 fprintf( stderr, _("Bad ValuesReturnFilter: %s\n"), vrFilter );
856                                 return EXIT_FAILURE;
857                         }
858
859                         if ( ber_flatten2( vrber, &c[i].ldctl_value, 0 ) == -1 ) {
860                                 return EXIT_FAILURE;
861                         }
862
863                         c[i].ldctl_oid = LDAP_CONTROL_VALUESRETURNFILTER;
864                         c[i].ldctl_iscritical = valuesReturnFilter > 1;
865                         i++;
866                 }
867
868                 if ( pagedResults ) {
869                         if ( ctrl_add() ) {
870                                 return EXIT_FAILURE;
871                         }
872
873                         if ( ldap_create_page_control_value( ld,
874                                 pageSize, &pr_cookie, &c[i].ldctl_value ) )
875                         {
876                                 return EXIT_FAILURE;
877                         }
878
879                         if ( pr_cookie.bv_val != NULL ) {
880                                 ber_memfree( pr_cookie.bv_val );
881                                 pr_cookie.bv_val = NULL;
882                                 pr_cookie.bv_len = 0;
883                         }
884                         
885                         c[i].ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
886                         c[i].ldctl_iscritical = pagedResults > 1;
887                         i++;
888                 }
889         }
890
891         tool_server_controls( ld, c, i );
892
893         ber_free( seber, 1 );
894         ber_free( vrber, 1 );
895
896         /* step back to the original number of controls, so that 
897          * those set while parsing args are preserved */
898         nctrls = save_nctrls;
899
900         if ( verbose ) {
901                 fprintf( stderr, _("filter%s: %s\nrequesting: "),
902                         infile != NULL ? _(" pattern") : "",
903                         filtpattern );
904
905                 if ( attrs == NULL ) {
906                         fprintf( stderr, _("All userApplication attributes") );
907                 } else {
908                         for ( i = 0; attrs[ i ] != NULL; ++i ) {
909                                 fprintf( stderr, "%s ", attrs[ i ] );
910                         }
911                 }
912                 fprintf( stderr, "\n" );
913         }
914
915         if ( ldif == 0 ) {
916                 printf( _("# extended LDIF\n") );
917         } else if ( ldif < 3 ) {
918                 printf( _("version: %d\n\n"), 1 );
919         }
920
921         if (ldif < 2 ) {
922                 char    *realbase = base;
923
924                 if ( realbase == NULL ) {
925                         ldap_get_option( ld, LDAP_OPT_DEFBASE, (void **)(char *)&realbase );
926                 }
927                 
928                 printf( "#\n" );
929                 printf(_("# LDAPv%d\n"), protocol);
930                 printf(_("# base <%s>%s with scope %s\n"),
931                         realbase ? realbase : "",
932                         ( realbase == NULL || realbase != base ) ? " (default)" : "",
933                         ((scope == LDAP_SCOPE_BASE) ? "baseObject"
934                                 : ((scope == LDAP_SCOPE_ONELEVEL) ? "oneLevel"
935                                 : ((scope == LDAP_SCOPE_SUBORDINATE) ? "children"
936                                 : "subtree" ))));
937                 printf(_("# filter%s: %s\n"), infile != NULL ? _(" pattern") : "",
938                        filtpattern);
939                 printf(_("# requesting: "));
940
941                 if ( attrs == NULL ) {
942                         printf( _("ALL") );
943                 } else {
944                         for ( i = 0; attrs[ i ] != NULL; ++i ) {
945                                 printf( "%s ", attrs[ i ] );
946                         }
947                 }
948
949                 if ( manageDSAit ) {
950                         printf(_("\n# with manageDSAit %scontrol"),
951                                 manageDSAit > 1 ? _("critical ") : "" );
952                 }
953                 if ( noop ) {
954                         printf(_("\n# with noop %scontrol"),
955                                 noop > 1 ? _("critical ") : "" );
956                 }
957                 if ( subentries ) {
958                         printf(_("\n# with subentries %scontrol: %s"),
959                                 subentries < 0 ? _("critical ") : "",
960                                 abs(subentries) == 1 ? "false" : "true" );
961                 }
962                 if ( valuesReturnFilter ) {
963                         printf(_("\n# with valuesReturnFilter %scontrol: %s"),
964                                 valuesReturnFilter > 1 ? _("critical ") : "", vrFilter );
965                 }
966                 if ( pagedResults ) {
967                         printf(_("\n# with pagedResults %scontrol: size=%d"),
968                                 (pagedResults > 1) ? _("critical ") : "", 
969                                 pageSize );
970                 }
971
972                 printf( _("\n#\n\n") );
973
974                 if ( realbase && realbase != base ) {
975                         ldap_memfree( realbase );
976                 }
977         }
978
979         if ( infile == NULL ) {
980                 rc = dosearch( ld, base, scope, NULL, filtpattern,
981                         attrs, attrsonly, NULL, NULL, NULL, -1 );
982
983         } else {
984                 first = 1;
985                 while ( fgets( line, sizeof( line ), fp ) != NULL ) { 
986                         line[ strlen( line ) - 1 ] = '\0';
987                         if ( !first ) {
988                                 putchar( '\n' );
989                         } else {
990                                 first = 0;
991                         }
992                         rc = dosearch( ld, base, scope, filtpattern, line,
993                                 attrs, attrsonly, NULL, NULL, NULL, -1 );
994
995                         if ( rc != 0 && !contoper ) {
996                                 break;
997                         }
998                 }
999                 if ( fp != stdin ) {
1000                         fclose( fp );
1001                 }
1002         }
1003
1004         if (( rc == LDAP_SUCCESS ) && pageSize && pr_morePagedResults ) {
1005                 char    buf[6];
1006                 int     i, moreEntries, tmpSize;
1007
1008                 /* Loop to get the next pages when 
1009                  * enter is pressed on the terminal.
1010                  */
1011                 if ( pagePrompt != 0 ) {
1012                         if ( entriesLeft > 0 ) {
1013                                 printf( _("Estimate entries: %d\n"), entriesLeft );
1014                         }
1015                         printf( _("Press [size] Enter for the next {%d|size} entries.\n"),
1016                                 (int)pageSize );
1017                         i = 0;
1018                         moreEntries = getchar();
1019                         while ( moreEntries != EOF && moreEntries != '\n' ) { 
1020                                 if ( i < (int)sizeof(buf) - 1 ) {
1021                                         buf[i] = moreEntries;
1022                                         i++;
1023                                 }
1024                                 moreEntries = getchar();
1025                         }
1026                         buf[i] = '\0';
1027
1028                         if ( i > 0 && isdigit( (unsigned char)buf[0] ) ) {
1029                                 int num = sscanf( buf, "%d", &tmpSize );
1030                                 if ( num != 1 ) {
1031                                         fprintf( stderr,
1032                                                 _("Invalid value for PagedResultsControl, %s.\n"), buf);
1033                                         return EXIT_FAILURE;
1034         
1035                                 }
1036                                 pageSize = (ber_int_t)tmpSize;
1037                         }
1038                 }
1039
1040                 goto getNextPage;
1041         }
1042
1043         tool_unbind( ld );
1044         tool_destroy();
1045         if ( base != NULL ) {
1046                 ber_memfree( base );
1047         }
1048         if ( control != NULL ) {
1049                 ber_memfree( control );
1050         }
1051
1052         if ( c ) {
1053                 for ( ; save_nctrls-- > 0; ) {
1054                         ber_memfree( c[ save_nctrls ].ldctl_value.bv_val );
1055                 }
1056                 free( c );
1057                 c = NULL;
1058         }
1059
1060         return( rc );
1061 }
1062
1063
1064 static int dosearch(
1065         LDAP    *ld,
1066         char    *base,
1067         int             scope,
1068         char    *filtpatt,
1069         char    *value,
1070         char    **attrs,
1071         int             attrsonly,
1072         LDAPControl **sctrls,
1073         LDAPControl **cctrls,
1074         struct timeval *timeout,
1075         int sizelimit )
1076 {
1077         char                    *filter;
1078         int                     rc;
1079         int                     nresponses;
1080         int                     nentries;
1081         int                     nreferences;
1082         int                     nextended;
1083         int                     npartial;
1084         LDAPMessage             *res, *msg;
1085         ber_int_t               msgid;
1086         char                    *retoid = NULL;
1087         struct berval           *retdata = NULL;
1088         int                     nresponses_psearch = -1;
1089         int                     cancel_msgid = -1;
1090
1091         if( filtpatt != NULL ) {
1092                 size_t max_fsize = strlen( filtpatt ) + strlen( value ) + 1;
1093                 filter = malloc( max_fsize );
1094                 if( filter == NULL ) {
1095                         perror( "malloc" );
1096                         return EXIT_FAILURE;
1097                 }
1098
1099                 if( snprintf( filter, max_fsize, filtpatt, value ) >= max_fsize ) {
1100                         fprintf( stderr, "Bad filter pattern: \"%s\"\n", filtpatt );
1101                         free( filter );
1102                         return EXIT_FAILURE;
1103                 }
1104
1105                 if ( verbose ) {
1106                         fprintf( stderr, _("filter: %s\n"), filter );
1107                 }
1108
1109                 if( ldif < 2 ) {
1110                         printf( _("#\n# filter: %s\n#\n"), filter );
1111                 }
1112
1113         } else {
1114                 filter = value;
1115         }
1116
1117         if ( dont ) {
1118                 if ( filtpatt != NULL ) {
1119                         free( filter );
1120                 }
1121                 return LDAP_SUCCESS;
1122         }
1123
1124         rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
1125                 sctrls, cctrls, timeout, sizelimit, &msgid );
1126
1127         if ( filtpatt != NULL ) {
1128                 free( filter );
1129         }
1130
1131         if( rc != LDAP_SUCCESS ) {
1132                 fprintf( stderr, _("%s: ldap_search_ext: %s (%d)\n"),
1133                         prog, ldap_err2string( rc ), rc );
1134                 return( rc );
1135         }
1136
1137         nresponses = nentries = nreferences = nextended = npartial = 0;
1138
1139         res = NULL;
1140
1141         while ((rc = ldap_result( ld, LDAP_RES_ANY,
1142                 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
1143                 NULL, &res )) > 0 )
1144         {
1145                 rc = tool_check_abandon( ld, msgid );
1146                 if ( rc ) {
1147                         return rc;
1148                 }
1149
1150                 if( sortattr ) {
1151                         (void) ldap_sort_entries( ld, &res,
1152                                 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
1153                 }
1154
1155                 for ( msg = ldap_first_message( ld, res );
1156                         msg != NULL;
1157                         msg = ldap_next_message( ld, msg ) )
1158                 {
1159                         if ( nresponses++ ) putchar('\n');
1160                         if ( nresponses_psearch >= 0 ) 
1161                                 nresponses_psearch++;
1162
1163                         switch( ldap_msgtype( msg ) ) {
1164                         case LDAP_RES_SEARCH_ENTRY:
1165                                 nentries++;
1166                                 print_entry( ld, msg, attrsonly );
1167                                 break;
1168
1169                         case LDAP_RES_SEARCH_REFERENCE:
1170                                 nreferences++;
1171                                 print_reference( ld, msg );
1172                                 break;
1173
1174                         case LDAP_RES_EXTENDED:
1175                                 nextended++;
1176                                 print_extended( ld, msg );
1177
1178                                 if ( ldap_msgid( msg ) == 0 ) {
1179                                         /* unsolicited extended operation */
1180                                         goto done;
1181                                 }
1182
1183                                 if ( cancel_msgid != -1 &&
1184                                                 cancel_msgid == ldap_msgid( msg ) ) {
1185                                         printf(_("Cancelled \n"));
1186                                         printf(_("cancel_msgid = %d\n"), cancel_msgid);
1187                                         goto done;
1188                                 }
1189                                 break;
1190
1191                         case LDAP_RES_SEARCH_RESULT:
1192                                 /* pagedResults stuff is dealt with
1193                                  * in tool_print_ctrls(), called by
1194                                  * print_results(). */
1195                                 rc = print_result( ld, msg, 1 );
1196                                 if ( ldapsync == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1197                                         break;
1198                                 }
1199
1200                                 goto done;
1201
1202                         case LDAP_RES_INTERMEDIATE:
1203                                 npartial++;
1204                                 ldap_parse_intermediate( ld, msg,
1205                                         &retoid, &retdata, NULL, 0 );
1206
1207                                 nresponses_psearch = 0;
1208
1209                                 if ( strcmp( retoid, LDAP_SYNC_INFO ) == 0 ) {
1210                                         printf(_("SyncInfo Received\n"));
1211                                         ldap_memfree( retoid );
1212                                         ber_bvfree( retdata );
1213                                         break;
1214                                 }
1215
1216                                 print_partial( ld, msg );
1217                                 ldap_memfree( retoid );
1218                                 ber_bvfree( retdata );
1219                                 goto done;
1220                         }
1221
1222                         if ( ldapsync && sync_slimit != -1 &&
1223                                         nresponses_psearch >= sync_slimit ) {
1224                                 BerElement *msgidber = NULL;
1225                                 struct berval *msgidvalp = NULL;
1226                                 msgidber = ber_alloc_t(LBER_USE_DER);
1227                                 ber_printf(msgidber, "{i}", msgid);
1228                                 ber_flatten(msgidber, &msgidvalp);
1229                                 ldap_extended_operation(ld, LDAP_EXOP_CANCEL,
1230                                         msgidvalp, NULL, NULL, &cancel_msgid);
1231                                 nresponses_psearch = -1;
1232                         }
1233                 }
1234
1235                 ldap_msgfree( res );
1236         }
1237
1238 done:
1239         if ( rc == -1 ) {
1240                 tool_perror( "ldap_result", rc, NULL, NULL, NULL, NULL );
1241                 return( rc );
1242         }
1243
1244         ldap_msgfree( res );
1245
1246         if ( pagedResults ) { 
1247                 npagedresponses += nresponses;
1248                 npagedentries += nentries;
1249                 npagedextended += nextended;
1250                 npagedpartial += npartial;
1251                 npagedreferences += nreferences;
1252                 if ( ( pr_morePagedResults == 0 ) && ( ldif < 2 ) ) {
1253                         printf( _("\n# numResponses: %d\n"), npagedresponses );
1254                         if( npagedentries ) {
1255                                 printf( _("# numEntries: %d\n"), npagedentries );
1256                         }
1257                         if( npagedextended ) {
1258                                 printf( _("# numExtended: %d\n"), npagedextended );
1259                         }
1260                         if( npagedpartial ) {
1261                                 printf( _("# numPartial: %d\n"), npagedpartial );
1262                         }
1263                         if( npagedreferences ) {
1264                                 printf( _("# numReferences: %d\n"), npagedreferences );
1265                         }
1266                 }
1267         } else if ( ldif < 2 ) {
1268                 printf( _("\n# numResponses: %d\n"), nresponses );
1269                 if( nentries ) printf( _("# numEntries: %d\n"), nentries );
1270                 if( nextended ) printf( _("# numExtended: %d\n"), nextended );
1271                 if( npartial ) printf( _("# numPartial: %d\n"), npartial );
1272                 if( nreferences ) printf( _("# numReferences: %d\n"), nreferences );
1273         }
1274
1275         return( rc );
1276 }
1277
1278 /* This is the proposed new way of doing things.
1279  * It is more efficient, but the API is non-standard.
1280  */
1281 static void
1282 print_entry(
1283         LDAP    *ld,
1284         LDAPMessage     *entry,
1285         int             attrsonly)
1286 {
1287         char            *ufn = NULL;
1288         char    tmpfname[ 256 ];
1289         char    url[ 256 ];
1290         int                     i, rc;
1291         BerElement              *ber = NULL;
1292         struct berval           bv, *bvals, **bvp = &bvals;
1293         LDAPControl **ctrls = NULL;
1294         FILE            *tmpfp;
1295
1296         rc = ldap_get_dn_ber( ld, entry, &ber, &bv );
1297
1298         if ( ldif < 2 ) {
1299                 ufn = ldap_dn2ufn( bv.bv_val );
1300                 tool_write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1301         }
1302         tool_write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1303
1304         rc = ldap_get_entry_controls( ld, entry, &ctrls );
1305         if( rc != LDAP_SUCCESS ) {
1306                 fprintf(stderr, _("print_entry: %d\n"), rc );
1307                 tool_perror( "ldap_get_entry_controls", rc, NULL, NULL, NULL, NULL );
1308                 exit( EXIT_FAILURE );
1309         }
1310
1311         if( ctrls ) {
1312                 tool_print_ctrls( ld, ctrls );
1313                 ldap_controls_free( ctrls );
1314         }
1315
1316         if ( includeufn ) {
1317                 if( ufn == NULL ) {
1318                         ufn = ldap_dn2ufn( bv.bv_val );
1319                 }
1320                 tool_write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1321         }
1322
1323         if( ufn != NULL ) ldap_memfree( ufn );
1324
1325         if ( attrsonly ) bvp = NULL;
1326
1327         for ( rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp );
1328                 rc == LDAP_SUCCESS;
1329                 rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp ) )
1330         {
1331                 if (bv.bv_val == NULL) break;
1332
1333                 if ( attrsonly ) {
1334                         tool_write_ldif( LDIF_PUT_NOVALUE, bv.bv_val, NULL, 0 );
1335
1336                 } else if ( bvals ) {
1337                         for ( i = 0; bvals[i].bv_val != NULL; i++ ) {
1338                                 if ( vals2tmp > 1 || ( vals2tmp &&
1339                                         ldif_is_not_printable( bvals[i].bv_val, bvals[i].bv_len )))
1340                                 {
1341                                         int tmpfd;
1342                                         /* write value to file */
1343                                         snprintf( tmpfname, sizeof tmpfname,
1344                                                 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1345                                                 tmpdir, bv.bv_val );
1346                                         tmpfp = NULL;
1347
1348                                         tmpfd = mkstemp( tmpfname );
1349
1350                                         if ( tmpfd < 0  ) {
1351                                                 perror( tmpfname );
1352                                                 continue;
1353                                         }
1354
1355                                         if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1356                                                 perror( tmpfname );
1357                                                 continue;
1358                                         }
1359
1360                                         if ( fwrite( bvals[ i ].bv_val,
1361                                                 bvals[ i ].bv_len, 1, tmpfp ) == 0 )
1362                                         {
1363                                                 perror( tmpfname );
1364                                                 fclose( tmpfp );
1365                                                 continue;
1366                                         }
1367
1368                                         fclose( tmpfp );
1369
1370                                         snprintf( url, sizeof url, "%s%s", urlpre,
1371                                                 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1372
1373                                         urlize( url );
1374                                         tool_write_ldif( LDIF_PUT_URL, bv.bv_val, url, strlen( url ));
1375
1376                                 } else {
1377                                         tool_write_ldif( LDIF_PUT_VALUE, bv.bv_val,
1378                                                 bvals[ i ].bv_val, bvals[ i ].bv_len );
1379                                 }
1380                         }
1381                         ber_memfree( bvals );
1382                 }
1383         }
1384
1385         if( ber != NULL ) {
1386                 ber_free( ber, 0 );
1387         }
1388 }
1389
1390 static void print_reference(
1391         LDAP *ld,
1392         LDAPMessage *reference )
1393 {
1394         int rc;
1395         char **refs = NULL;
1396         LDAPControl **ctrls;
1397
1398         if( ldif < 2 ) {
1399                 printf(_("# search reference\n"));
1400         }
1401
1402         rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1403
1404         if( rc != LDAP_SUCCESS ) {
1405                 tool_perror( "ldap_parse_reference", rc, NULL, NULL, NULL, NULL );
1406                 exit( EXIT_FAILURE );
1407         }
1408
1409         if( refs ) {
1410                 int i;
1411                 for( i=0; refs[i] != NULL; i++ ) {
1412                         tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1413                                 "ref", refs[i], strlen(refs[i]) );
1414                 }
1415                 ber_memvfree( (void **) refs );
1416         }
1417
1418         if( ctrls ) {
1419                 tool_print_ctrls( ld, ctrls );
1420                 ldap_controls_free( ctrls );
1421         }
1422 }
1423
1424 static void print_extended(
1425         LDAP *ld,
1426         LDAPMessage *extended )
1427 {
1428         int rc;
1429         char *retoid = NULL;
1430         struct berval *retdata = NULL;
1431
1432         if( ldif < 2 ) {
1433                 printf(_("# extended result response\n"));
1434         }
1435
1436         rc = ldap_parse_extended_result( ld, extended,
1437                 &retoid, &retdata, 0 );
1438
1439         if( rc != LDAP_SUCCESS ) {
1440                 tool_perror( "ldap_parse_extended_result", rc, NULL, NULL, NULL, NULL );
1441                 exit( EXIT_FAILURE );
1442         }
1443
1444         if ( ldif < 2 ) {
1445                 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1446                         "extended", retoid, retoid ? strlen(retoid) : 0 );
1447         }
1448         ber_memfree( retoid );
1449
1450         if(retdata) {
1451                 if ( ldif < 2 ) {
1452                         tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1453                                 "data", retdata->bv_val, retdata->bv_len );
1454                 }
1455                 ber_bvfree( retdata );
1456         }
1457
1458         print_result( ld, extended, 0 );
1459 }
1460
1461 static void print_partial(
1462         LDAP *ld,
1463         LDAPMessage *partial )
1464 {
1465         int rc;
1466         char *retoid = NULL;
1467         struct berval *retdata = NULL;
1468         LDAPControl **ctrls = NULL;
1469
1470         if( ldif < 2 ) {
1471                 printf(_("# extended partial response\n"));
1472         }
1473
1474         rc = ldap_parse_intermediate( ld, partial,
1475                 &retoid, &retdata, &ctrls, 0 );
1476
1477         if( rc != LDAP_SUCCESS ) {
1478                 tool_perror( "ldap_parse_intermediate", rc, NULL, NULL, NULL, NULL );
1479                 exit( EXIT_FAILURE );
1480         }
1481
1482         if ( ldif < 2 ) {
1483                 tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1484                         "partial", retoid, retoid ? strlen(retoid) : 0 );
1485         }
1486
1487         ber_memfree( retoid );
1488
1489         if( retdata ) {
1490                 if ( ldif < 2 ) {
1491                         tool_write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1492                                 "data", retdata->bv_val, retdata->bv_len );
1493                 }
1494
1495                 ber_bvfree( retdata );
1496         }
1497
1498         if( ctrls ) {
1499                 tool_print_ctrls( ld, ctrls );
1500                 ldap_controls_free( ctrls );
1501         }
1502 }
1503
1504 static int print_result(
1505         LDAP *ld,
1506         LDAPMessage *result, int search )
1507 {
1508         int rc;
1509         int err;
1510         char *matcheddn = NULL;
1511         char *text = NULL;
1512         char **refs = NULL;
1513         LDAPControl **ctrls = NULL;
1514
1515         if( search ) {
1516                 if ( ldif < 2 ) {
1517                         printf(_("# search result\n"));
1518                 }
1519                 if ( ldif < 1 ) {
1520                         printf("%s: %d\n", _("search"), ldap_msgid(result) );
1521                 }
1522         }
1523
1524         rc = ldap_parse_result( ld, result,
1525                 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1526
1527         if( rc != LDAP_SUCCESS ) {
1528                 tool_perror( "ldap_parse_result", rc, NULL, NULL, NULL, NULL );
1529                 exit( EXIT_FAILURE );
1530         }
1531
1532
1533         if( !ldif ) {
1534                 printf( _("result: %d %s\n"), err, ldap_err2string(err) );
1535
1536         } else if ( err != LDAP_SUCCESS ) {
1537                 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1538         }
1539
1540         if( matcheddn ) {
1541                 if( *matcheddn ) {
1542                 if( !ldif ) {
1543                         tool_write_ldif( LDIF_PUT_VALUE,
1544                                 "matchedDN", matcheddn, strlen(matcheddn) );
1545                 } else {
1546                         fprintf( stderr, _("Matched DN: %s\n"), matcheddn );
1547                 }
1548                 }
1549
1550                 ber_memfree( matcheddn );
1551         }
1552
1553         if( text ) {
1554                 if( *text ) {
1555                         if( !ldif ) {
1556                                 if ( err == LDAP_PARTIAL_RESULTS ) {
1557                                         char    *line;
1558
1559                                         for ( line = text; line != NULL; ) {
1560                                                 char    *next = strchr( line, '\n' );
1561
1562                                                 tool_write_ldif( LDIF_PUT_TEXT,
1563                                                         "text", line,
1564                                                         next ? next - line : strlen( line ) );
1565
1566                                                 line = next ? next + 1 : NULL;
1567                                         }
1568
1569                                 } else {
1570                                         tool_write_ldif( LDIF_PUT_TEXT, "text",
1571                                                 text, strlen(text) );
1572                                 }
1573                         } else {
1574                                 fprintf( stderr, _("Additional information: %s\n"), text );
1575                         }
1576                 }
1577
1578                 ber_memfree( text );
1579         }
1580
1581         if( refs ) {
1582                 int i;
1583                 for( i=0; refs[i] != NULL; i++ ) {
1584                         if( !ldif ) {
1585                                 tool_write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1586                         } else {
1587                                 fprintf( stderr, _("Referral: %s\n"), refs[i] );
1588                         }
1589                 }
1590
1591                 ber_memvfree( (void **) refs );
1592         }
1593
1594         pr_morePagedResults = 0;
1595
1596         if( ctrls ) {
1597                 tool_print_ctrls( ld, ctrls );
1598                 ldap_controls_free( ctrls );
1599         }
1600
1601         return err;
1602 }
1603