]> git.sur5r.net Git - openldap/blob - clients/tools/ldapsearch.c
ldapsearch -E subentries=... lacked a break; for case 'E':
[openldap] / clients / tools / ldapsearch.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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 #include "ldap_log.h"
37
38 static char *def_tmpdir;
39 static char *def_urlpre;
40
41 static void
42 usage( const char *s )
43 {
44         fprintf( stderr,
45 "usage: %s [options] [filter [attributes...]]\nwhere:\n"
46 "  filter\tRFC-2254 compliant LDAP search filter\n"
47 "  attributes\twhitespace-separated list of attribute descriptions\n"
48 "    which may include:\n"
49 "      1.1   no attributes\n"
50 "      *     all user attributes\n"
51 "      +     all operational attributes\n"
52
53 "Search options:\n"
54 "  -a deref   one of never (default), always, search, or find\n"
55 "  -A         retrieve attribute names only (no values)\n"
56 "  -b basedn  base dn for search\n"
57 "  -E [!]<ctrl>[=<ctrlparam>] search controls (! indicates criticality)\n"
58 "             [!]mv=<filter>              (matched values filter)\n"
59 "             [!]pr=<size>                (paged results)\n"
60 #ifdef LDAP_CONTROL_SUBENTRIES
61 "             [!]subentries[=true|false]  (subentries)\n"
62 #endif
63 "  -F prefix  URL prefix for files (default: %s)\n"
64 "  -l limit   time limit (in seconds) for search\n"
65 "  -L         print responses in LDIFv1 format\n"
66 "  -LL        print responses in LDIF format without comments\n"
67 "  -LLL       print responses in LDIF format without comments\n"
68 "             and version\n"
69 "  -s scope   one of base, one, or sub (search scope)\n"
70 "  -S attr    sort the results by attribute `attr'\n"
71 "  -t         write binary values to files in temporary directory\n"
72 "  -tt        write all values to files in temporary directory\n"
73 "  -T path    write files to directory specified by path (default: %s)\n"
74 "  -u         include User Friendly entry names in the output\n"
75 "  -z limit   size limit (in entries) for search\n"
76
77 "Common options:\n"
78 "  -d level   set LDAP debugging level to `level'\n"
79 "  -D binddn  bind DN\n"
80 "  -e [!]<ctrl>[=<ctrlparam>] general controls (! indicates criticality)\n"
81 "             [!]authzid=<authzid> (\"dn:<dn>\" or \"u:<user>\")\n"
82 "             [!]manageDSAit       (alternate form, see -M)\n"
83 "             [!]noop\n"
84 "  -f file    read operations from `file'\n"
85 "  -h host    LDAP server\n"
86 "  -H URI     LDAP Uniform Resource Indentifier(s)\n"
87 "  -I         use SASL Interactive mode\n"
88 "  -k         use Kerberos authentication\n"
89 "  -K         like -k, but do only step 1 of the Kerberos bind\n"
90 "  -M         enable Manage DSA IT control (-MM to make critical)\n"
91 "  -n         show what would be done but don't actually search\n"
92 "  -O props   SASL security properties\n"
93 "  -p port    port on LDAP server\n"
94 "  -P version procotol version (default: 3)\n"
95 "  -Q         use SASL Quiet mode\n"
96 "  -R realm   SASL realm\n"
97 "  -U authcid SASL authentication identity\n"
98 "  -v         run in verbose mode (diagnostics to standard output)\n"
99 "  -w passwd  bind passwd (for simple authentication)\n"
100 "  -W         prompt for bind passwd\n"
101 "  -x         Simple authentication\n"
102 "  -X authzid SASL authorization identity (\"dn:<dn>\" or \"u:<user>\")\n"
103 "  -y file    Read passwd from file\n"
104 "  -Y mech    SASL mechanism\n"
105 "  -Z         Start TLS request (-ZZ to require successful response)\n"
106 , s, def_urlpre, def_tmpdir );
107
108         exit( EXIT_FAILURE );
109 }
110
111 static void print_entry LDAP_P((
112         LDAP    *ld,
113         LDAPMessage     *entry,
114         int             attrsonly));
115
116 static void print_reference(
117         LDAP *ld,
118         LDAPMessage *reference );
119
120 static void print_extended(
121         LDAP *ld,
122         LDAPMessage *extended );
123
124 static void print_partial(
125         LDAP *ld,
126         LDAPMessage *partial );
127
128 static int print_result(
129         LDAP *ld,
130         LDAPMessage *result,
131         int search );
132
133 static void print_ctrls(
134         LDAPControl **ctrls );
135
136 static int write_ldif LDAP_P((
137         int type,
138         char *name,
139         char *value,
140         ber_len_t vallen ));
141
142 static int dosearch LDAP_P((
143         LDAP    *ld,
144         char    *base,
145         int             scope,
146         char    *filtpatt,
147         char    *value,
148         char    **attrs,
149         int             attrsonly,
150         LDAPControl **sctrls,
151         LDAPControl **cctrls,
152         struct timeval *timeout,
153         int     sizelimit ));
154
155 static char *tmpdir = NULL;
156 static char *urlpre = NULL;
157 static char *prog = NULL;
158 static char     *binddn = NULL;
159 static struct berval passwd = { 0, NULL };
160 static char     *base = NULL;
161 static char     *ldaphost = NULL;
162 static char *ldapuri = NULL;
163 static int      ldapport = 0;
164 #ifdef HAVE_CYRUS_SASL
165 static unsigned sasl_flags = LDAP_SASL_AUTOMATIC;
166 static char     *sasl_realm = NULL;
167 static char     *sasl_authc_id = NULL;
168 static char     *sasl_authz_id = NULL;
169 static char     *sasl_mech = NULL;
170 static char     *sasl_secprops = NULL;
171 #endif
172 static int      use_tls = 0;
173 static char     *sortattr = NULL;
174 static int      verbose, not, includeufn, vals2tmp, ldif;
175
176 static int pagedResults = 0;
177 static ber_int_t pageSize = 0;
178 static ber_int_t entriesLeft = 0;
179 static ber_int_t morePagedResults = 1;
180 static struct berval cookie = { 0, NULL };
181 static int npagedresponses;
182 static int npagedentries;
183 static int npagedreferences;
184 static int npagedextended;
185 static int npagedpartial;
186
187 static int parse_page_control(
188         LDAP *ld,
189         LDAPMessage *result,
190         struct berval *cookie );
191
192 static void
193 urlize(char *url)
194 {
195         char *p;
196
197         if (*LDAP_DIRSEP != '/') {
198                 for (p = url; *p; p++) {
199                         if (*p == *LDAP_DIRSEP)
200                                 *p = '/';
201                 }
202         }
203 }
204
205 int
206 main( int argc, char **argv )
207 {
208         char            *infile, *filtpattern, **attrs = NULL, line[BUFSIZ];
209         FILE            *fp = NULL;
210         int                     rc, i, first, scope, deref, attrsonly, manageDSAit, noop, crit;
211         int                     referrals, timelimit, sizelimit, debug;
212         int             authmethod, version, want_bindpw;
213         LDAP            *ld = NULL;
214         BerElement      *ber = NULL;
215         char    *control = NULL, *cvalue;
216         int             subentries, valuesReturnFilter;
217         struct berval   *sebvalp = NULL, *vrbvalp = NULL;
218         char    *vrFilter  = NULL;
219         char    *pw_file = NULL;
220         char    *authzid = NULL;
221         struct berval   *prbvalp = NULL;
222
223         infile = NULL;
224         debug = verbose = not = vals2tmp = referrals =
225                 subentries = valuesReturnFilter =
226                 attrsonly = manageDSAit = noop = ldif = want_bindpw = 0;
227
228         npagedresponses = npagedentries = npagedreferences =
229                 npagedextended = npagedpartial = 0;
230
231         prog = lutil_progname( "ldapsearch", argc, argv );
232
233         lutil_log_initialize(argc, argv);
234
235         deref = sizelimit = timelimit = version = -1;
236
237         scope = LDAP_SCOPE_SUBTREE;
238         authmethod = -1;
239
240         if((def_tmpdir = getenv("TMPDIR")) == NULL &&
241            (def_tmpdir = getenv("TMP")) == NULL &&
242            (def_tmpdir = getenv("TEMP")) == NULL )
243         {
244                 def_tmpdir = LDAP_TMPDIR;
245         }
246
247         if ( !*def_tmpdir )
248                 def_tmpdir = LDAP_TMPDIR;
249
250         def_urlpre = malloc( sizeof("file:////") + strlen(def_tmpdir) );
251
252         if( def_urlpre == NULL ) {
253                 perror( "malloc" );
254                 return EXIT_FAILURE;
255         }
256
257         sprintf( def_urlpre, "file:///%s/",
258                 def_tmpdir[0] == *LDAP_DIRSEP ? &def_tmpdir[1] : def_tmpdir );
259
260         urlize( def_urlpre );
261
262         while (( i = getopt( argc, argv, "Aa:b:E:F:f:Ll:S:s:T:tuz:"
263                 "Cd:e:D:h:H:IkKMnO:p:P:QR:U:vw:WxX:y:Y:Z")) != EOF )
264         {
265         switch( i ) {
266         /* Search Options */
267         case 'a':       /* set alias deref option */
268                 if ( strcasecmp( optarg, "never" ) == 0 ) {
269                 deref = LDAP_DEREF_NEVER;
270                 } else if ( strncasecmp( optarg, "search", sizeof("search")-1 ) == 0 ) {
271                 deref = LDAP_DEREF_SEARCHING;
272                 } else if ( strncasecmp( optarg, "find", sizeof("find")-1 ) == 0 ) {
273                 deref = LDAP_DEREF_FINDING;
274                 } else if ( strcasecmp( optarg, "always" ) == 0 ) {
275                 deref = LDAP_DEREF_ALWAYS;
276                 } else {
277                 fprintf( stderr, "alias deref should be never, search, find, or always\n" );
278                 usage(prog);
279                 }
280                 break;
281         case 'A':       /* retrieve attribute names only -- no values */
282                 ++attrsonly;
283                 break;
284         case 'b': /* search base */
285                 base = strdup( optarg );
286                 break;
287         case 'E': /* search controls */
288                 if( version == LDAP_VERSION2 ) {
289                         fprintf( stderr, "%s: -E incompatible with LDAPv%d\n",
290                                 prog, version );
291                         return EXIT_FAILURE;
292                 }
293
294                 /* should be extended to support comma separated list of
295                  *      [!]key[=value] parameters, e.g.  -E !foo,bar=567
296                  */
297
298                 crit = 0;
299                 cvalue = NULL;
300                 if( optarg[0] == '!' ) {
301                         crit = 1;
302                         optarg++;
303                 }
304
305                 control = strdup( optarg );
306                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
307                         *cvalue++ = '\0';
308                 }
309
310                 if ( strcasecmp( control, "mv" ) == 0 ) {
311                         /* ValuesReturnFilter control */
312                         if( valuesReturnFilter ) {
313                                 fprintf( stderr, "ValuesReturnFilter previously specified\n");
314                                 return EXIT_FAILURE;
315                         }
316                         valuesReturnFilter= 1 + crit;
317
318                         if ( cvalue == NULL ) {
319                                 fprintf( stderr,
320                                         "missing filter in ValuesReturnFilter control\n");
321                                 return EXIT_FAILURE;
322                         }
323
324                         vrFilter = cvalue;
325                         version = LDAP_VERSION3;
326                         break;
327
328                 } else if ( strcasecmp( control, "pr" ) == 0 ) {
329                         int num, tmp;
330                         /* PagedResults control */
331                         if ( pagedResults != 0 ) {
332                                 fprintf( stderr, "PagedResultsControl previously specified\n" );
333                                 return EXIT_FAILURE;
334                         }
335                         
336                         num = sscanf( cvalue, "%d", &tmp );
337                         if ( num != 1 ) {
338                                 fprintf( stderr, "Invalid value for PagedResultsControl, %s.\n", cvalue);
339                                 return EXIT_FAILURE;
340
341                         }
342                         pageSize = (ber_int_t) tmp;
343                         pagedResults = 1 + crit;
344                         break;
345
346 #ifdef LDAP_CONTROL_SUBENTRIES
347                 } else if ( strcasecmp( control, "subentries" ) == 0 ) {
348                         if( subentries ) {
349                                 fprintf( stderr, "subentries control previously specified\n");
350                                 return EXIT_FAILURE;
351                         }
352                         if( cvalue == NULL || strcasecmp( cvalue, "true") == 0 ) {
353                                 subentries = 2;
354                         } else if ( strcasecmp( cvalue, "false") == 0 ) {
355                                 subentries = 1;
356                         } else {
357                                 fprintf( stderr,
358                                         "subentries control value \"%s\" invalid\n");
359                                 return EXIT_FAILURE;
360                         }
361                         if( crit ) subentries *= -1;
362                         break;
363 #endif
364
365                 } else {
366                         fprintf( stderr, "Invalid control name: %s\n", control );
367                         usage(prog);
368                         return EXIT_FAILURE;
369                 }
370         case 'f':       /* input file */
371                 if( infile != NULL ) {
372                         fprintf( stderr, "%s: -f previously specified\n", prog );
373                         return EXIT_FAILURE;
374                 }
375                 infile = strdup( optarg );
376                 break;
377         case 'F':       /* uri prefix */
378                 if( urlpre ) free( urlpre );
379                 urlpre = strdup( optarg );
380                 break;
381         case 'l':       /* time limit */
382                 timelimit = atoi( optarg );
383                 if( timelimit < 0 ) {
384                         fprintf( stderr, "%s: invalid timelimit (%d) specified\n",
385                                 prog, timelimit );
386                         return EXIT_FAILURE;
387                 }
388                 break;
389         case 'L':       /* print entries in LDIF format */
390                 ++ldif;
391                 break;
392         case 's':       /* search scope */
393                 if ( strcasecmp( optarg, "base" ) == 0 ) {
394                 scope = LDAP_SCOPE_BASE;
395                 } else if ( strncasecmp( optarg, "one", sizeof("one")-1 ) == 0 ) {
396                 scope = LDAP_SCOPE_ONELEVEL;
397                 } else if ( strncasecmp( optarg, "sub", sizeof("sub")-1 ) == 0 ) {
398                 scope = LDAP_SCOPE_SUBTREE;
399                 } else {
400                 fprintf( stderr, "scope should be base, one, or sub\n" );
401                 usage(prog);
402                 }
403                 break;
404         case 'S':       /* sort attribute */
405                 sortattr = strdup( optarg );
406                 break;
407         case 'u':       /* include UFN */
408                 ++includeufn;
409                 break;
410         case 't':       /* write attribute values to TMPDIR files */
411                 ++vals2tmp;
412                 break;
413         case 'T':       /* tmpdir */
414                 if( tmpdir ) free( tmpdir );
415                 tmpdir = strdup( optarg );
416                 break;
417         case 'z':       /* size limit */
418                 sizelimit = atoi( optarg );
419                 break;
420
421         /* Common Options */
422         case 'C':
423                 referrals++;
424                 break;
425         case 'd':
426             debug |= atoi( optarg );
427             break;
428         case 'D':       /* bind DN */
429                 if( binddn != NULL ) {
430                         fprintf( stderr, "%s: -D previously specified\n", prog );
431                         return EXIT_FAILURE;
432                 }
433             binddn = strdup( optarg );
434             break;
435         case 'e': /* general controls */
436                 if( version == LDAP_VERSION2 ) {
437                         fprintf( stderr, "%s: -e incompatible with LDAPv%d\n",
438                                 prog, version );
439                         return EXIT_FAILURE;
440                 }
441
442                 /* should be extended to support comma separated list of
443                  *      [!]key[=value] parameters, e.g.  -e !foo,bar=567
444                  */
445
446                 crit = 0;
447                 cvalue = NULL;
448                 if( optarg[0] == '!' ) {
449                         crit = 1;
450                         optarg++;
451                 }
452
453                 control = strdup( optarg );
454                 if ( (cvalue = strchr( control, '=' )) != NULL ) {
455                         *cvalue++ = '\0';
456                 }
457
458                 if ( strcasecmp( control, "authzid" ) == 0 ) {
459                         if( authzid != NULL ) {
460                                 fprintf( stderr, "authzid control previously specified");
461                                 return EXIT_FAILURE;
462                         }
463                         if( cvalue == NULL ) {
464                                 fprintf( stderr, "authzid: control value expected" );
465                                 usage(prog);
466                                 return EXIT_FAILURE;
467                         }
468                         if( !crit ) {
469                                 fprintf( stderr, "authzid: must be marked critical" );
470                                 usage(prog);
471                                 return EXIT_FAILURE;
472                         }
473
474                         assert( authzid == NULL );
475                         authzid = control;
476                         break;
477                         
478                 } else if ( strcasecmp( control, "manageDSAit" ) == 0 ) {
479                         if( manageDSAit ) {
480                                 fprintf( stderr, "manageDSAit control previously specified");
481                                 return EXIT_FAILURE;
482                         }
483                         if( cvalue != NULL ) {
484                                 fprintf( stderr, "manageDSAit: no control value expected" );
485                                 usage(prog);
486                                 return EXIT_FAILURE;
487                         }
488
489                         manageDSAit = 1 + crit;
490                         break;
491                         
492                 } else if ( strcasecmp( control, "noop" ) == 0 ) {
493                         if( noop ) {
494                                 fprintf( stderr, "noop control previously specified");
495                                 return EXIT_FAILURE;
496                         }
497                         if( cvalue != NULL ) {
498                                 fprintf( stderr, "noop: no control value expected" );
499                                 usage(prog);
500                                 return EXIT_FAILURE;
501                         }
502
503                         noop = 1 + crit;
504                         break;
505
506                 } else {
507                         fprintf( stderr, "Invalid general control name: %s\n", control );
508                         usage(prog);
509                         return EXIT_FAILURE;
510                 }
511         case 'h':       /* ldap host */
512                 if( ldapuri != NULL ) {
513                         fprintf( stderr, "%s: -h incompatible with -H\n", prog );
514                         return EXIT_FAILURE;
515                 }
516                 if( ldaphost != NULL ) {
517                         fprintf( stderr, "%s: -h previously specified\n", prog );
518                         return EXIT_FAILURE;
519                 }
520             ldaphost = strdup( optarg );
521             break;
522         case 'H':       /* ldap URI */
523                 if( ldaphost != NULL ) {
524                         fprintf( stderr, "%s: -H incompatible with -h\n", prog );
525                         return EXIT_FAILURE;
526                 }
527                 if( ldapport ) {
528                         fprintf( stderr, "%s: -H incompatible with -p\n", prog );
529                         return EXIT_FAILURE;
530                 }
531                 if( ldapuri != NULL ) {
532                         fprintf( stderr, "%s: -H previously specified\n", prog );
533                         return EXIT_FAILURE;
534                 }
535             ldapuri = strdup( optarg );
536             break;
537         case 'I':
538 #ifdef HAVE_CYRUS_SASL
539                 if( version == LDAP_VERSION2 ) {
540                         fprintf( stderr, "%s: -I incompatible with version %d\n",
541                                 prog, version );
542                         return EXIT_FAILURE;
543                 }
544                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
545                         fprintf( stderr, "%s: incompatible previous "
546                                 "authentication choice\n",
547                                 prog );
548                         return EXIT_FAILURE;
549                 }
550                 authmethod = LDAP_AUTH_SASL;
551                 version = LDAP_VERSION3;
552                 sasl_flags = LDAP_SASL_INTERACTIVE;
553                 break;
554 #else
555                 fprintf( stderr, "%s: was not compiled with SASL support\n",
556                         prog );
557                 return( EXIT_FAILURE );
558 #endif
559         case 'k':       /* kerberos bind */
560 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
561                 if( version > LDAP_VERSION2 ) {
562                         fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
563                                 prog, version );
564                         return EXIT_FAILURE;
565                 }
566
567                 if( authmethod != -1 ) {
568                         fprintf( stderr, "%s: -k incompatible with previous "
569                                 "authentication choice\n", prog );
570                         return EXIT_FAILURE;
571                 }
572                         
573                 authmethod = LDAP_AUTH_KRBV4;
574 #else
575                 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
576                 return EXIT_FAILURE;
577 #endif
578             break;
579         case 'K':       /* kerberos bind, part one only */
580 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
581                 if( version > LDAP_VERSION2 ) {
582                         fprintf( stderr, "%s: -k incompatible with LDAPv%d\n",
583                                 prog, version );
584                         return EXIT_FAILURE;
585                 }
586                 if( authmethod != -1 ) {
587                         fprintf( stderr, "%s: incompatible with previous "
588                                 "authentication choice\n", prog );
589                         return EXIT_FAILURE;
590                 }
591
592                 authmethod = LDAP_AUTH_KRBV41;
593 #else
594                 fprintf( stderr, "%s: not compiled with Kerberos support\n", prog );
595                 return( EXIT_FAILURE );
596 #endif
597             break;
598         case 'M':
599                 /* enable Manage DSA IT */
600                 if( version == LDAP_VERSION2 ) {
601                         fprintf( stderr, "%s: -M incompatible with LDAPv%d\n",
602                                 prog, version );
603                         return EXIT_FAILURE;
604                 }
605                 manageDSAit++;
606                 version = LDAP_VERSION3;
607                 break;
608         case 'n':       /* print deletes, don't actually do them */
609             ++not;
610             break;
611         case 'O':
612 #ifdef HAVE_CYRUS_SASL
613                 if( sasl_secprops != NULL ) {
614                         fprintf( stderr, "%s: -O previously specified\n", prog );
615                         return EXIT_FAILURE;
616                 }
617                 if( version == LDAP_VERSION2 ) {
618                         fprintf( stderr, "%s: -O incompatible with LDAPv%d\n",
619                                 prog, version );
620                         return EXIT_FAILURE;
621                 }
622                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
623                         fprintf( stderr, "%s: incompatible previous "
624                                 "authentication choice\n", prog );
625                         return EXIT_FAILURE;
626                 }
627                 authmethod = LDAP_AUTH_SASL;
628                 version = LDAP_VERSION3;
629                 sasl_secprops = strdup( optarg );
630 #else
631                 fprintf( stderr, "%s: not compiled with SASL support\n",
632                         prog );
633                 return( EXIT_FAILURE );
634 #endif
635                 break;
636         case 'p':
637                 if( ldapport ) {
638                         fprintf( stderr, "%s: -p previously specified\n", prog );
639                         return EXIT_FAILURE;
640                 }
641             ldapport = atoi( optarg );
642             break;
643         case 'P':
644                 switch( atoi(optarg) ) {
645                 case 2:
646                         if( version == LDAP_VERSION3 ) {
647                                 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
648                                         prog, version );
649                                 return EXIT_FAILURE;
650                         }
651                         version = LDAP_VERSION2;
652                         break;
653                 case 3:
654                         if( version == LDAP_VERSION2 ) {
655                                 fprintf( stderr, "%s: -P 2 incompatible with version %d\n",
656                                         prog, version );
657                                 return EXIT_FAILURE;
658                         }
659                         version = LDAP_VERSION3;
660                         break;
661                 default:
662                         fprintf( stderr, "%s: protocol version should be 2 or 3\n",
663                                 prog );
664                         usage( prog );
665                         return( EXIT_FAILURE );
666                 } break;
667         case 'Q':
668 #ifdef HAVE_CYRUS_SASL
669                 if( version == LDAP_VERSION2 ) {
670                         fprintf( stderr, "%s: -Q incompatible with version %d\n",
671                                 prog, version );
672                         return EXIT_FAILURE;
673                 }
674                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
675                         fprintf( stderr, "%s: incompatible previous "
676                                 "authentication choice\n",
677                                 prog );
678                         return EXIT_FAILURE;
679                 }
680                 authmethod = LDAP_AUTH_SASL;
681                 version = LDAP_VERSION3;
682                 sasl_flags = LDAP_SASL_QUIET;
683                 break;
684 #else
685                 fprintf( stderr, "%s: not compiled with SASL support\n",
686                         prog );
687                 return( EXIT_FAILURE );
688 #endif
689         case 'R':
690 #ifdef HAVE_CYRUS_SASL
691                 if( sasl_realm != NULL ) {
692                         fprintf( stderr, "%s: -R previously specified\n", prog );
693                         return EXIT_FAILURE;
694                 }
695                 if( version == LDAP_VERSION2 ) {
696                         fprintf( stderr, "%s: -R incompatible with version %d\n",
697                                 prog, version );
698                         return EXIT_FAILURE;
699                 }
700                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
701                         fprintf( stderr, "%s: incompatible previous "
702                                 "authentication choice\n",
703                                 prog );
704                         return EXIT_FAILURE;
705                 }
706                 authmethod = LDAP_AUTH_SASL;
707                 version = LDAP_VERSION3;
708                 sasl_realm = strdup( optarg );
709 #else
710                 fprintf( stderr, "%s: not compiled with SASL support\n",
711                         prog );
712                 return( EXIT_FAILURE );
713 #endif
714                 break;
715         case 'U':
716 #ifdef HAVE_CYRUS_SASL
717                 if( sasl_authc_id != NULL ) {
718                         fprintf( stderr, "%s: -U previously specified\n", prog );
719                         return EXIT_FAILURE;
720                 }
721                 if( version == LDAP_VERSION2 ) {
722                         fprintf( stderr, "%s: -U incompatible with version %d\n",
723                                 prog, version );
724                         return EXIT_FAILURE;
725                 }
726                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
727                         fprintf( stderr, "%s: incompatible previous "
728                                 "authentication choice\n",
729                                 prog );
730                         return EXIT_FAILURE;
731                 }
732                 authmethod = LDAP_AUTH_SASL;
733                 version = LDAP_VERSION3;
734                 sasl_authc_id = strdup( optarg );
735 #else
736                 fprintf( stderr, "%s: not compiled with SASL support\n",
737                         prog );
738                 return( EXIT_FAILURE );
739 #endif
740                 break;
741         case 'v':       /* verbose mode */
742             verbose++;
743             break;
744         case 'w':       /* password */
745             passwd.bv_val = strdup( optarg );
746                 {
747                         char* p;
748
749                         for( p = optarg; *p != '\0'; p++ ) {
750                                 *p = '\0';
751                         }
752                 }
753                 passwd.bv_len = strlen( passwd.bv_val );
754             break;
755         case 'W':
756                 want_bindpw++;
757                 break;
758         case 'y':
759                 pw_file = optarg;
760                 break;
761         case 'Y':
762 #ifdef HAVE_CYRUS_SASL
763                 if( sasl_mech != NULL ) {
764                         fprintf( stderr, "%s: -Y previously specified\n", prog );
765                         return EXIT_FAILURE;
766                 }
767                 if( version == LDAP_VERSION2 ) {
768                         fprintf( stderr, "%s: -Y incompatible with version %d\n",
769                                 prog, version );
770                         return EXIT_FAILURE;
771                 }
772                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
773                         fprintf( stderr, "%s: incompatible with authentication choice\n", prog );
774                         return EXIT_FAILURE;
775                 }
776                 authmethod = LDAP_AUTH_SASL;
777                 version = LDAP_VERSION3;
778                 sasl_mech = strdup( optarg );
779 #else
780                 fprintf( stderr, "%s: not compiled with SASL support\n",
781                         prog );
782                 return( EXIT_FAILURE );
783 #endif
784                 break;
785         case 'x':
786                 if( authmethod != -1 && authmethod != LDAP_AUTH_SIMPLE ) {
787                         fprintf( stderr, "%s: incompatible with previous "
788                                 "authentication choice\n", prog );
789                         return EXIT_FAILURE;
790                 }
791                 authmethod = LDAP_AUTH_SIMPLE;
792                 break;
793         case 'X':
794 #ifdef HAVE_CYRUS_SASL
795                 if( sasl_authz_id != NULL ) {
796                         fprintf( stderr, "%s: -X previously specified\n", prog );
797                         return EXIT_FAILURE;
798                 }
799                 if( version == LDAP_VERSION2 ) {
800                         fprintf( stderr, "%s: -X incompatible with LDAPv%d\n",
801                                 prog, version );
802                         return EXIT_FAILURE;
803                 }
804                 if( authmethod != -1 && authmethod != LDAP_AUTH_SASL ) {
805                         fprintf( stderr, "%s: -X incompatible with "
806                                 "authentication choice\n", prog );
807                         return EXIT_FAILURE;
808                 }
809                 authmethod = LDAP_AUTH_SASL;
810                 version = LDAP_VERSION3;
811                 sasl_authz_id = strdup( optarg );
812 #else
813                 fprintf( stderr, "%s: not compiled with SASL support\n",
814                         prog );
815                 return( EXIT_FAILURE );
816 #endif
817                 break;
818         case 'Z':
819 #ifdef HAVE_TLS
820                 if( version == LDAP_VERSION2 ) {
821                         fprintf( stderr, "%s: -Z incompatible with version %d\n",
822                                 prog, version );
823                         return EXIT_FAILURE;
824                 }
825                 version = LDAP_VERSION3;
826                 use_tls++;
827 #else
828                 fprintf( stderr, "%s: not compiled with TLS support\n",
829                         prog );
830                 return( EXIT_FAILURE );
831 #endif
832                 break;
833         default:
834                 fprintf( stderr, "%s: unrecognized option -%c\n",
835                         prog, optopt );
836                 usage(prog);
837         }
838         }
839
840         if (version == -1) {
841                 version = LDAP_VERSION3;
842         }
843         if (authmethod == -1 && version > LDAP_VERSION2) {
844 #ifdef HAVE_CYRUS_SASL
845                 authmethod = LDAP_AUTH_SASL;
846 #else
847                 authmethod = LDAP_AUTH_SIMPLE;
848 #endif
849         }
850
851         if (( argc - optind < 1 ) ||
852                 ( *argv[optind] != '(' /*')'*/ &&
853                 ( strchr( argv[optind], '=' ) == NULL ) ) )
854         {
855                 filtpattern = "(objectclass=*)";
856         } else {
857                 filtpattern = strdup( argv[optind++] );
858         }
859
860         if ( argv[optind] != NULL ) {
861                 attrs = &argv[optind];
862         }
863
864         if ( infile != NULL ) {
865                 if ( infile[0] == '-' && infile[1] == '\0' ) {
866                         fp = stdin;
867                 } else if (( fp = fopen( infile, "r" )) == NULL ) {
868                         perror( infile );
869                         return EXIT_FAILURE;
870                 }
871         }
872
873         if ( tmpdir == NULL ) {
874                 tmpdir = def_tmpdir;
875
876                 if ( urlpre == NULL )
877                         urlpre = def_urlpre;
878         }
879
880         if( urlpre == NULL ) {
881                 urlpre = malloc( sizeof("file:////") + strlen(tmpdir) );
882
883                 if( urlpre == NULL ) {
884                         perror( "malloc" );
885                         return EXIT_FAILURE;
886                 }
887
888                 sprintf( urlpre, "file:///%s/",
889                         tmpdir[0] == *LDAP_DIRSEP ? &tmpdir[1] : tmpdir );
890
891                 urlize( urlpre );
892         }
893
894         if ( debug ) {
895                 if( ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &debug ) != LBER_OPT_SUCCESS ) {
896                         fprintf( stderr, "Could not set LBER_OPT_DEBUG_LEVEL %d\n", debug );
897                 }
898                 if( ldap_set_option( NULL, LDAP_OPT_DEBUG_LEVEL, &debug ) != LDAP_OPT_SUCCESS ) {
899                         fprintf( stderr, "Could not set LDAP_OPT_DEBUG_LEVEL %d\n", debug );
900                 }
901                 ldif_debug = debug;
902         }
903
904 #ifdef SIGPIPE
905         (void) SIGNAL( SIGPIPE, SIG_IGN );
906 #endif
907
908         if( ( ldaphost != NULL || ldapport ) && ( ldapuri == NULL ) ) {
909                 if ( verbose ) {
910                         fprintf( stderr, "ldap_init( %s, %d )\n",
911                                 ldaphost != NULL ? ldaphost : "<DEFAULT>",
912                                 ldapport );
913                 }
914
915                 ld = ldap_init( ldaphost, ldapport );
916                 if( ld == NULL ) {
917                         perror("ldapsearch: ldap_init");
918                         return EXIT_FAILURE;
919                 }
920
921         } else {
922                 if ( verbose ) {
923                         fprintf( stderr, "ldap_initialize( %s )\n",
924                                 ldapuri != NULL ? ldapuri : "<DEFAULT>" );
925                 }
926
927                 rc = ldap_initialize( &ld, ldapuri );
928                 if( rc != LDAP_SUCCESS ) {
929                         fprintf( stderr, "Could not create LDAP session handle (%d): %s\n",
930                                 rc, ldap_err2string(rc) );
931                         return EXIT_FAILURE;
932                 }
933         }
934
935         if (deref != -1 &&
936                 ldap_set_option( ld, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS )
937         {
938                 fprintf( stderr, "Could not set LDAP_OPT_DEREF %d\n", deref );
939                 return EXIT_FAILURE;
940         }
941         if (timelimit != -1 &&
942                 ldap_set_option( ld, LDAP_OPT_TIMELIMIT, (void *) &timelimit ) != LDAP_OPT_SUCCESS )
943         {
944                 fprintf( stderr, "Could not set LDAP_OPT_TIMELIMIT %d\n", timelimit );
945                 return EXIT_FAILURE;
946         }
947         if (sizelimit != -1 &&
948                 ldap_set_option( ld, LDAP_OPT_SIZELIMIT, (void *) &sizelimit ) != LDAP_OPT_SUCCESS )
949         {
950                 fprintf( stderr, "Could not set LDAP_OPT_SIZELIMIT %d\n", sizelimit );
951                 return EXIT_FAILURE;
952         }
953
954         /* referrals */
955         if (ldap_set_option( ld, LDAP_OPT_REFERRALS,
956                 referrals ? LDAP_OPT_ON : LDAP_OPT_OFF ) != LDAP_OPT_SUCCESS )
957         {
958                 fprintf( stderr, "Could not set LDAP_OPT_REFERRALS %s\n",
959                         referrals ? "on" : "off" );
960                 return EXIT_FAILURE;
961         }
962
963         if (version == -1 ) {
964                 version = LDAP_VERSION3;
965         }
966
967         if( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version )
968                 != LDAP_OPT_SUCCESS )
969         {
970                 fprintf( stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n",
971                         version );
972                 return EXIT_FAILURE;
973         }
974
975         if ( use_tls && ( ldap_start_tls_s( ld, NULL, NULL ) != LDAP_SUCCESS )) {
976                 ldap_perror( ld, "ldap_start_tls" );
977                 if ( use_tls > 1 ) {
978                         return EXIT_FAILURE;
979                 }
980         }
981
982         if ( pw_file || want_bindpw ) {
983                 if ( pw_file ) {
984                         rc = lutil_get_filed_password( pw_file, &passwd );
985                         if( rc ) return EXIT_FAILURE;
986                 } else {
987                         passwd.bv_val = getpassphrase( "Enter LDAP Password: " );
988                         passwd.bv_len = passwd.bv_val ? strlen( passwd.bv_val ) : 0;
989                 }
990         }
991
992         if ( authmethod == LDAP_AUTH_SASL ) {
993 #ifdef HAVE_CYRUS_SASL
994                 void *defaults;
995
996                 if( sasl_secprops != NULL ) {
997                         rc = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
998                                 (void *) sasl_secprops );
999                         
1000                         if( rc != LDAP_OPT_SUCCESS ) {
1001                                 fprintf( stderr,
1002                                         "Could not set LDAP_OPT_X_SASL_SECPROPS: %s\n",
1003                                         sasl_secprops );
1004                                 return( EXIT_FAILURE );
1005                         }
1006                 }
1007                 
1008                 defaults = lutil_sasl_defaults( ld,
1009                         sasl_mech,
1010                         sasl_realm,
1011                         sasl_authc_id,
1012                         passwd.bv_val,
1013                         sasl_authz_id );
1014
1015                 rc = ldap_sasl_interactive_bind_s( ld, binddn,
1016                         sasl_mech, NULL, NULL,
1017                         sasl_flags, lutil_sasl_interact, defaults );
1018
1019                 if( rc != LDAP_SUCCESS ) {
1020                         ldap_perror( ld, "ldap_sasl_interactive_bind_s" );
1021                         return( EXIT_FAILURE );
1022                 }
1023 #else
1024                 fprintf( stderr, "%s: not compiled with SASL support\n",
1025                         prog);
1026                 return( EXIT_FAILURE );
1027 #endif
1028         } else {
1029                 if ( ldap_bind_s( ld, binddn, passwd.bv_val, authmethod )
1030                                 != LDAP_SUCCESS ) {
1031                         ldap_perror( ld, "ldap_bind" );
1032                         return( EXIT_FAILURE );
1033                 }
1034         }
1035
1036 getNextPage:
1037         if ( manageDSAit || noop || subentries
1038                 || valuesReturnFilter || pageSize )
1039         {
1040                 int err;
1041                 int i=0;
1042                 int crit = 0;
1043                 LDAPControl c[6];
1044                 LDAPControl *ctrls[7];
1045                 
1046                 if ( authzid ) {
1047                         c[i].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1048                         c[i].ldctl_value.bv_val = authzid;
1049                         c[i].ldctl_value.bv_len = strlen( authzid );
1050                         c[i].ldctl_iscritical = 1;
1051
1052                         if( c[i].ldctl_iscritical ) crit++;
1053                         ctrls[i] = &c[i];
1054                         ctrls[++i] = NULL;
1055                 }
1056
1057                 if ( manageDSAit ) {
1058                         c[i].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
1059                         c[i].ldctl_value.bv_val = NULL;
1060                         c[i].ldctl_value.bv_len = 0;
1061                         c[i].ldctl_iscritical = manageDSAit > 1;
1062
1063                         if( c[i].ldctl_iscritical ) crit++;
1064                         ctrls[i] = &c[i];
1065                         ctrls[++i] = NULL;
1066                 }
1067
1068                 if ( noop ) {
1069                         c[i].ldctl_oid = LDAP_CONTROL_NOOP;
1070                         c[i].ldctl_value.bv_val = NULL;
1071                         c[i].ldctl_value.bv_len = 0;
1072                         c[i].ldctl_iscritical = noop > 1;
1073
1074                         if( c[i].ldctl_iscritical ) crit++;
1075                         ctrls[i] = &c[i];
1076                         ctrls[++i] = NULL;
1077                 }
1078
1079 #ifdef LDAP_CONTROL_SUBENTRIES
1080                 if ( subentries ) {
1081                 if (( ber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
1082                                 return EXIT_FAILURE;
1083                         }
1084
1085                         err = ber_printf( ber, "{b}", abs(subentries) == 1 ? 0 : 1 );
1086                 if ( err == LBER_ERROR ) {
1087                                 ber_free( ber, 1 );
1088                                 fprintf( stderr, "Subentries control encoding error!\n" );
1089                                 return EXIT_FAILURE;
1090                         }
1091
1092                         if ( ber_flatten( ber, &sebvalp ) == LBER_ERROR ) {
1093                                 return EXIT_FAILURE;
1094                         }
1095
1096                         c[i].ldctl_oid = LDAP_CONTROL_SUBENTRIES;
1097                         c[i].ldctl_value=(*sebvalp);
1098                         c[i].ldctl_iscritical = subentries < 1;
1099
1100                         if( c[i].ldctl_iscritical ) crit++;
1101                         ctrls[i] = &c[i];
1102                         ctrls[++i] = NULL;
1103                 }
1104 #endif
1105
1106                 if ( valuesReturnFilter ) {
1107                 if (( ber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
1108                                 return EXIT_FAILURE;
1109                         }
1110
1111                 if ( ( err = ldap_put_vrFilter( ber, vrFilter ) ) == -1 ) {
1112                                 ber_free( ber, 1 );
1113                                 fprintf( stderr, "Bad ValuesReturnFilter: %s\n", vrFilter );
1114                                 return EXIT_FAILURE;
1115                         }
1116
1117                         if ( ber_flatten( ber, &vrbvalp ) == LBER_ERROR ) {
1118                                 return EXIT_FAILURE;
1119                         }
1120
1121                         ber_free( ber, 1 );
1122
1123                         c[i].ldctl_oid = LDAP_CONTROL_VALUESRETURNFILTER;
1124                         c[i].ldctl_value=(*vrbvalp);
1125                         c[i].ldctl_iscritical = valuesReturnFilter > 1;
1126
1127                         if( c[i].ldctl_iscritical ) crit++;
1128                         ctrls[i] = &c[i];
1129                         ctrls[++i] = NULL;
1130                 }
1131
1132                 if ( pagedResults ) {
1133                         if (( ber = ber_alloc_t(LBER_USE_DER)) == NULL ) {
1134                                 return EXIT_FAILURE;
1135                         }
1136
1137                         ber_printf( ber, "{iO}", pageSize, &cookie );
1138                         if ( ber_flatten( ber, &prbvalp ) == LBER_ERROR ) {
1139                                 return EXIT_FAILURE;
1140                         }
1141                         
1142                         ber_free( ber, 1 );
1143
1144                         c[i].ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1145                         c[i].ldctl_value=(*prbvalp);
1146                         c[i].ldctl_iscritical = pagedResults > 1;
1147
1148                         if( c[i].ldctl_iscritical ) crit++;
1149                         ctrls[i] = &c[i];
1150                         ctrls[++i] = NULL;
1151                 }
1152
1153                 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
1154
1155                 if( err != LDAP_OPT_SUCCESS ) {
1156                         fprintf( stderr, "Could not set %scontrols\n",
1157                                 crit ? "critical " : "" );
1158                         if( crit ) {
1159                                 return EXIT_FAILURE;
1160                         }
1161                 }
1162
1163                 ber_bvfree( sebvalp );
1164                 ber_bvfree( vrbvalp );
1165                 ber_bvfree( prbvalp );
1166         }
1167         
1168         if ( verbose ) {
1169                 fprintf( stderr, "filter%s: %s\nrequesting: ",
1170                         infile != NULL ? " pattern" : "",
1171                         filtpattern );
1172
1173                 if ( attrs == NULL ) {
1174                         fprintf( stderr, "ALL" );
1175                 } else {
1176                         for ( i = 0; attrs[ i ] != NULL; ++i ) {
1177                                 fprintf( stderr, "%s ", attrs[ i ] );
1178                         }
1179                 }
1180                 fprintf( stderr, "\n" );
1181         }
1182
1183         if ( ldif == 0 ) {
1184                 printf( "# extended LDIF\n" );
1185         } else if ( ldif < 3 ) {
1186                 printf( "version: %d\n\n", 1 );
1187         }
1188
1189         if (ldif < 2 ) {
1190                 printf( "#\n"
1191                         "# LDAPv%d\n"
1192                         "# base <%s> with scope %s\n"
1193                         "# filter%s: %s\n"
1194                         "# requesting: ",
1195                         version,
1196                         base, (scope == LDAP_SCOPE_BASE) ? "base"
1197                                 : ((scope == LDAP_SCOPE_ONELEVEL) ? "one" : "sub"),
1198                         infile != NULL ? " pattern" : "",
1199                         filtpattern );
1200
1201                 if ( attrs == NULL ) {
1202                         printf( "ALL" );
1203                 } else {
1204                         for ( i = 0; attrs[ i ] != NULL; ++i ) {
1205                                 printf( "%s ", attrs[ i ] );
1206                         }
1207                 }
1208
1209                 if ( manageDSAit ) {
1210                         printf("\n# with manageDSAit %scontrol",
1211                                 manageDSAit > 1 ? "critical " : "" );
1212                 }
1213                 if ( noop ) {
1214                         printf("\n# with noop %scontrol",
1215                                 noop > 1 ? "critical " : "" );
1216                 }
1217                 if ( subentries ) {
1218                         printf("\n# with subentries %scontrol: %s",
1219                                 subentries < 0 ? "critical " : "",
1220                                 abs(subentries) == 1 ? "false" : "true" );
1221                 }
1222                 if ( valuesReturnFilter ) {
1223                         printf("\n# with valuesReturnFilter %scontrol: %s",
1224                                 valuesReturnFilter > 1 ? "critical " : "", vrFilter );
1225                 }
1226                 if ( pageSize ) {
1227                         printf("\n# with pagedResults %scontrol: size=%d",
1228                                 (pagedResults > 1) ? "critical " : "", 
1229                                 pageSize );
1230                 }
1231
1232                 printf( "\n#\n\n" );
1233         }
1234
1235         if ( infile == NULL ) {
1236                 rc = dosearch( ld, base, scope, NULL, filtpattern,
1237                         attrs, attrsonly, NULL, NULL, NULL, -1 );
1238
1239         } else {
1240                 rc = 0;
1241                 first = 1;
1242                 while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) { 
1243                         line[ strlen( line ) - 1 ] = '\0';
1244                         if ( !first ) {
1245                                 putchar( '\n' );
1246                         } else {
1247                                 first = 0;
1248                         }
1249                         rc = dosearch( ld, base, scope, filtpattern, line,
1250                                 attrs, attrsonly, NULL, NULL, NULL, -1 );
1251                 }
1252                 if ( fp != stdin ) {
1253                         fclose( fp );
1254                 }
1255         }
1256
1257         if ( ( pageSize != 0 ) && ( morePagedResults != 0 ) ) { 
1258                 char    buf[6];
1259                 int     i, moreEntries, tmpSize;
1260
1261                 /* Loop to get the next pages when 
1262                  * enter is pressed on the terminal.
1263                  */
1264                 if ( entriesLeft > 0 ) {
1265                         printf( "Estimate entries: %d\n", entriesLeft );
1266                 }
1267                 printf( "Press [size] Enter for the next {%d|size} entries.\n",
1268                         (int)pageSize ); 
1269                 i = 0;
1270                 moreEntries = getchar();
1271                 while ( moreEntries != EOF && moreEntries != '\n' ) { 
1272                         if ( i < sizeof(buf) - 1 ) {
1273                                 buf[i] = moreEntries;
1274                                 i++;
1275                         }
1276                         moreEntries = getchar();
1277                 }
1278                 buf[i] = '\0';
1279
1280                 if ( i > 0 && isdigit( buf[0] ) ) {
1281                         int num = sscanf( buf, "%d", &tmpSize );
1282                         if ( num != 1 ) {
1283                                 fprintf( stderr, "Invalid value for PagedResultsControl, %s.\n", buf);
1284                                 return EXIT_FAILURE;
1285
1286                         }
1287                         pageSize = (ber_int_t)tmpSize;
1288                 }
1289
1290                 goto getNextPage;       
1291         }
1292
1293         ldap_unbind( ld );
1294         return( rc );
1295 }
1296
1297
1298 static int dosearch(
1299         LDAP    *ld,
1300         char    *base,
1301         int             scope,
1302         char    *filtpatt,
1303         char    *value,
1304         char    **attrs,
1305         int             attrsonly,
1306         LDAPControl **sctrls,
1307         LDAPControl **cctrls,
1308         struct timeval *timeout,
1309         int sizelimit )
1310 {
1311         char                    *filter;
1312         int                     rc;
1313         int                     nresponses;
1314         int                     nentries;
1315         int                     nreferences;
1316         int                     nextended;
1317         int                     npartial;
1318         LDAPMessage             *res, *msg;
1319         ber_int_t       msgid;
1320
1321         if( filtpatt != NULL ) {
1322                 filter = malloc( strlen( filtpatt ) + strlen( value ) );
1323                 if( filter == NULL ) {
1324                         perror( "malloc" );
1325                         return EXIT_FAILURE;
1326                 }
1327
1328                 sprintf( filter, filtpatt, value );
1329
1330                 if ( verbose ) {
1331                         fprintf( stderr, "filter: %s\n", filter );
1332                 }
1333
1334                 if( ldif < 2 ) {
1335                         printf( "#\n# filter: %s\n#\n", filter );
1336                 }
1337
1338         } else {
1339                 filter = value;
1340         }
1341
1342         if ( not ) {
1343                 return LDAP_SUCCESS;
1344         }
1345
1346         rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
1347                 sctrls, cctrls, timeout, sizelimit, &msgid );
1348
1349         if ( filtpatt != NULL ) {
1350                 free( filter );
1351         }
1352
1353         if( rc != LDAP_SUCCESS ) {
1354                 fprintf( stderr, "%s: ldap_search_ext: %s (%d)\n",
1355                         prog, ldap_err2string( rc ), rc );
1356                 return( rc );
1357         }
1358
1359         nresponses = nentries = nreferences = nextended = npartial = 0;
1360
1361         res = NULL;
1362
1363         while ((rc = ldap_result( ld, LDAP_RES_ANY,
1364                 sortattr ? LDAP_MSG_ALL : LDAP_MSG_ONE,
1365                 NULL, &res )) > 0 )
1366         {
1367                 if( sortattr ) {
1368                         (void) ldap_sort_entries( ld, &res,
1369                                 ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp );
1370                 }
1371
1372                 for ( msg = ldap_first_message( ld, res );
1373                         msg != NULL;
1374                         msg = ldap_next_message( ld, msg ) )
1375                 {
1376                         if( nresponses++ ) putchar('\n');
1377
1378                         switch( ldap_msgtype( msg ) ) {
1379                         case LDAP_RES_SEARCH_ENTRY:
1380                                 nentries++;
1381                                 print_entry( ld, msg, attrsonly );
1382                                 break;
1383
1384                         case LDAP_RES_SEARCH_REFERENCE:
1385                                 nreferences++;
1386                                 print_reference( ld, msg );
1387                                 break;
1388
1389                         case LDAP_RES_EXTENDED:
1390                                 nextended++;
1391                                 print_extended( ld, msg );
1392
1393                                 if( ldap_msgid( msg ) == 0 ) {
1394                                         /* unsolicited extended operation */
1395                                         goto done;
1396                                 }
1397                                 break;
1398
1399                         case LDAP_RES_EXTENDED_PARTIAL:
1400                                 npartial++;
1401                                 print_partial( ld, msg );
1402                                 break;
1403
1404                         case LDAP_RES_SEARCH_RESULT:
1405                                 rc = print_result( ld, msg, 1 );
1406                                 if ( pageSize != 0 ) { 
1407                                         rc = parse_page_control( ld, msg, &cookie );
1408                                 }
1409                                 goto done;
1410                         }
1411                 }
1412
1413                 ldap_msgfree( res );
1414         }
1415
1416         if ( rc == -1 ) {
1417                 ldap_perror( ld, "ldap_result" );
1418                 return( rc );
1419         }
1420
1421 done:
1422         if ( pageSize != 0 ) { 
1423                 npagedresponses = npagedresponses + nresponses;
1424                 npagedentries = npagedentries + nentries;
1425                 npagedreferences = npagedreferences + nreferences;
1426                 npagedextended = npagedextended + nextended;
1427                 npagedpartial = npagedpartial + npartial;
1428                 if ( ( morePagedResults == 0 ) && ( ldif < 2 ) ) {
1429                         printf( "\n# numResponses: %d\n", npagedresponses );
1430                         if( nentries ) printf( "# numEntries: %d\n", npagedentries );
1431                         if( nextended ) printf( "# numExtended: %d\n", npagedextended );
1432                         if( npartial ) printf( "# numPartial: %d\n", npagedpartial );
1433                         if( nreferences ) printf( "# numReferences: %d\n", npagedreferences );
1434                 }
1435         } else if ( ldif < 2 ) {
1436                 printf( "\n# numResponses: %d\n", nresponses );
1437                 if( nentries ) printf( "# numEntries: %d\n", nentries );
1438                 if( nextended ) printf( "# numExtended: %d\n", nextended );
1439                 if( npartial ) printf( "# numPartial: %d\n", npartial );
1440                 if( nreferences ) printf( "# numReferences: %d\n", nreferences );
1441         }
1442
1443         return( rc );
1444 }
1445
1446 #if 1
1447 /* This is the original version, the old way of doing things. */
1448 static void
1449 print_entry(
1450         LDAP    *ld,
1451         LDAPMessage     *entry,
1452         int             attrsonly)
1453 {
1454         char            *a, *dn, *ufn;
1455         char    tmpfname[ 256 ];
1456         char    url[ 256 ];
1457         int                     i, rc;
1458         BerElement              *ber = NULL;
1459         struct berval   **bvals;
1460         LDAPControl **ctrls = NULL;
1461         FILE            *tmpfp;
1462
1463         dn = ldap_get_dn( ld, entry );
1464         ufn = NULL;
1465
1466         if ( ldif < 2 ) {
1467                 ufn = ldap_dn2ufn( dn );
1468                 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1469         }
1470         write_ldif( LDIF_PUT_VALUE, "dn", dn, dn ? strlen( dn ) : 0);
1471
1472         rc = ldap_get_entry_controls( ld, entry, &ctrls );
1473
1474         if( rc != LDAP_SUCCESS ) {
1475                 fprintf(stderr, "print_entry: %d\n", rc );
1476                 ldap_perror( ld, "ldap_get_entry_controls" );
1477                 exit( EXIT_FAILURE );
1478         }
1479
1480         if( ctrls ) {
1481                 print_ctrls( ctrls );
1482                 ldap_controls_free( ctrls );
1483         }
1484
1485         if ( includeufn ) {
1486                 if( ufn == NULL ) {
1487                         ufn = ldap_dn2ufn( dn );
1488                 }
1489                 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1490         }
1491
1492         if( ufn != NULL ) ldap_memfree( ufn );
1493         ldap_memfree( dn );
1494
1495         for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL;
1496                 a = ldap_next_attribute( ld, entry, ber ) )
1497         {
1498                 if ( attrsonly ) {
1499                         write_ldif( LDIF_PUT_NOVALUE, a, NULL, 0 );
1500
1501                 } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) {
1502                         for ( i = 0; bvals[i] != NULL; i++ ) {
1503                                 if ( vals2tmp > 1 || ( vals2tmp
1504                                         && ldif_is_not_printable( bvals[i]->bv_val, bvals[i]->bv_len ) ))
1505                                 {
1506                                         int tmpfd;
1507                                         /* write value to file */
1508                                         snprintf( tmpfname, sizeof tmpfname,
1509                                                 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1510                                                 tmpdir, a );
1511                                         tmpfp = NULL;
1512
1513                                         tmpfd = mkstemp( tmpfname );
1514
1515                                         if ( tmpfd < 0  ) {
1516                                                 perror( tmpfname );
1517                                                 continue;
1518                                         }
1519
1520                                         if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1521                                                 perror( tmpfname );
1522                                                 continue;
1523                                         }
1524
1525                                         if ( fwrite( bvals[ i ]->bv_val,
1526                                                 bvals[ i ]->bv_len, 1, tmpfp ) == 0 )
1527                                         {
1528                                                 perror( tmpfname );
1529                                                 fclose( tmpfp );
1530                                                 continue;
1531                                         }
1532
1533                                         fclose( tmpfp );
1534
1535                                         snprintf( url, sizeof url, "%s%s", urlpre,
1536                                                 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1537
1538                                         urlize( url );
1539                                         write_ldif( LDIF_PUT_URL, a, url, strlen( url ));
1540
1541                                 } else {
1542                                         write_ldif( LDIF_PUT_VALUE, a,
1543                                                 bvals[ i ]->bv_val, bvals[ i ]->bv_len );
1544                                 }
1545                         }
1546                         ber_bvecfree( bvals );
1547                 }
1548         }
1549
1550         if( ber != NULL ) {
1551                 ber_free( ber, 0 );
1552         }
1553 }
1554 #else
1555 /* This is the proposed new way of doing things.
1556  * It is more efficient, but the API is non-standard.
1557  */
1558 static void
1559 print_entry(
1560         LDAP    *ld,
1561         LDAPMessage     *entry,
1562         int             attrsonly)
1563 {
1564         char            *ufn = NULL;
1565         char    tmpfname[ 256 ];
1566         char    url[ 256 ];
1567         int                     i, rc;
1568         BerElement              *ber = NULL;
1569         struct berval           bv, *bvals, **bvp = &bvals;
1570         LDAPControl **ctrls = NULL;
1571         FILE            *tmpfp;
1572
1573         rc = ldap_get_dn_ber( ld, entry, &ber, &bv );
1574
1575         if ( ldif < 2 ) {
1576                 ufn = ldap_dn2ufn( bv.bv_val );
1577                 write_ldif( LDIF_PUT_COMMENT, NULL, ufn, ufn ? strlen( ufn ) : 0 );
1578         }
1579         write_ldif( LDIF_PUT_VALUE, "dn", bv.bv_val, bv.bv_len );
1580
1581         rc = ldap_int_get_controls( ber, &ctrls );
1582
1583         if( rc != LDAP_SUCCESS ) {
1584                 fprintf(stderr, "print_entry: %d\n", rc );
1585                 ldap_perror( ld, "ldap_get_entry_controls" );
1586                 exit( EXIT_FAILURE );
1587         }
1588
1589         if( ctrls ) {
1590                 print_ctrls( ctrls );
1591                 ldap_controls_free( ctrls );
1592         }
1593
1594         if ( includeufn ) {
1595                 if( ufn == NULL ) {
1596                         ufn = ldap_dn2ufn( bv.bv_val );
1597                 }
1598                 write_ldif( LDIF_PUT_VALUE, "ufn", ufn, ufn ? strlen( ufn ) : 0 );
1599         }
1600
1601         if( ufn != NULL ) ldap_memfree( ufn );
1602
1603         if ( attrsonly ) bvp = NULL;
1604
1605         for ( rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp );
1606                 rc == LDAP_SUCCESS;
1607                 rc = ldap_get_attribute_ber( ld, entry, ber, &bv, bvp ) )
1608         {
1609                 if (bv.bv_val == NULL) break;
1610
1611                 if ( attrsonly ) {
1612                         write_ldif( LDIF_PUT_NOVALUE, bv.bv_val, NULL, 0 );
1613
1614                 } else {
1615                         for ( i = 0; bvals[i].bv_val != NULL; i++ ) {
1616                                 if ( vals2tmp > 1 || ( vals2tmp
1617                                         && ldif_is_not_printable( bvals[i].bv_val, bvals[i].bv_len ) ))
1618                                 {
1619                                         int tmpfd;
1620                                         /* write value to file */
1621                                         snprintf( tmpfname, sizeof tmpfname,
1622                                                 "%s" LDAP_DIRSEP "ldapsearch-%s-XXXXXX",
1623                                                 tmpdir, bv.bv_val );
1624                                         tmpfp = NULL;
1625
1626                                         tmpfd = mkstemp( tmpfname );
1627
1628                                         if ( tmpfd < 0  ) {
1629                                                 perror( tmpfname );
1630                                                 continue;
1631                                         }
1632
1633                                         if (( tmpfp = fdopen( tmpfd, "w")) == NULL ) {
1634                                                 perror( tmpfname );
1635                                                 continue;
1636                                         }
1637
1638                                         if ( fwrite( bvals[ i ].bv_val,
1639                                                 bvals[ i ].bv_len, 1, tmpfp ) == 0 )
1640                                         {
1641                                                 perror( tmpfname );
1642                                                 fclose( tmpfp );
1643                                                 continue;
1644                                         }
1645
1646                                         fclose( tmpfp );
1647
1648                                         snprintf( url, sizeof url, "%s%s", urlpre,
1649                                                 &tmpfname[strlen(tmpdir) + sizeof(LDAP_DIRSEP) - 1] );
1650
1651                                         urlize( url );
1652                                         write_ldif( LDIF_PUT_URL, bv.bv_val, url, strlen( url ));
1653
1654                                 } else {
1655                                         write_ldif( LDIF_PUT_VALUE, bv.bv_val,
1656                                                 bvals[ i ].bv_val, bvals[ i ].bv_len );
1657                                 }
1658                         }
1659                         ber_memfree( bvals );
1660                 }
1661         }
1662
1663         if( ber != NULL ) {
1664                 ber_free( ber, 0 );
1665         }
1666 }
1667 #endif
1668
1669 static void print_reference(
1670         LDAP *ld,
1671         LDAPMessage *reference )
1672 {
1673         int rc;
1674         char **refs = NULL;
1675         LDAPControl **ctrls;
1676
1677         if( ldif < 2 ) {
1678                 printf("# search reference\n");
1679         }
1680
1681         rc = ldap_parse_reference( ld, reference, &refs, &ctrls, 0 );
1682
1683         if( rc != LDAP_SUCCESS ) {
1684                 ldap_perror(ld, "ldap_parse_reference");
1685                 exit( EXIT_FAILURE );
1686         }
1687
1688         if( refs ) {
1689                 int i;
1690                 for( i=0; refs[i] != NULL; i++ ) {
1691                         write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1692                                 "ref", refs[i], strlen(refs[i]) );
1693                 }
1694                 ber_memvfree( (void **) refs );
1695         }
1696
1697         if( ctrls ) {
1698                 print_ctrls( ctrls );
1699                 ldap_controls_free( ctrls );
1700         }
1701 }
1702
1703 static void print_extended(
1704         LDAP *ld,
1705         LDAPMessage *extended )
1706 {
1707         int rc;
1708         char *retoid = NULL;
1709         struct berval *retdata = NULL;
1710
1711         if( ldif < 2 ) {
1712                 printf("# extended result response\n");
1713         }
1714
1715         rc = ldap_parse_extended_result( ld, extended,
1716                 &retoid, &retdata, 0 );
1717
1718         if( rc != LDAP_SUCCESS ) {
1719                 ldap_perror(ld, "ldap_parse_extended_result");
1720                 exit( EXIT_FAILURE );
1721         }
1722
1723         if ( ldif < 2 ) {
1724                 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1725                         "extended", retoid, retoid ? strlen(retoid) : 0 );
1726         }
1727         ber_memfree( retoid );
1728
1729         if(retdata) {
1730                 if ( ldif < 2 ) {
1731                         write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1732                                 "data", retdata->bv_val, retdata->bv_len );
1733                 }
1734                 ber_bvfree( retdata );
1735         }
1736
1737         print_result( ld, extended, 0 );
1738 }
1739
1740 static void print_partial(
1741         LDAP *ld,
1742         LDAPMessage *partial )
1743 {
1744         int rc;
1745         char *retoid = NULL;
1746         struct berval *retdata = NULL;
1747         LDAPControl **ctrls = NULL;
1748
1749         if( ldif < 2 ) {
1750                 printf("# extended partial response\n");
1751         }
1752
1753         rc = ldap_parse_extended_partial( ld, partial,
1754                 &retoid, &retdata, &ctrls, 0 );
1755
1756         if( rc != LDAP_SUCCESS ) {
1757                 ldap_perror(ld, "ldap_parse_extended_partial");
1758                 exit( EXIT_FAILURE );
1759         }
1760
1761         if ( ldif < 2 ) {
1762                 write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1763                         "partial", retoid, retoid ? strlen(retoid) : 0 );
1764         }
1765
1766         ber_memfree( retoid );
1767
1768         if( retdata ) {
1769                 if ( ldif < 2 ) {
1770                         write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_BINARY,
1771                                 "data", retdata->bv_val, retdata->bv_len );
1772                 }
1773
1774                 ber_bvfree( retdata );
1775         }
1776
1777         if( ctrls ) {
1778                 print_ctrls( ctrls );
1779                 ldap_controls_free( ctrls );
1780         }
1781 }
1782
1783 static int print_result(
1784         LDAP *ld,
1785         LDAPMessage *result, int search )
1786 {
1787         int rc;
1788         int err;
1789         char *matcheddn = NULL;
1790         char *text = NULL;
1791         char **refs = NULL;
1792         LDAPControl **ctrls = NULL;
1793
1794         if( search ) {
1795                 if ( ldif < 2 ) {
1796                         printf("# search result\n");
1797                 }
1798                 if ( ldif < 1 ) {
1799                         printf("%s: %d\n", "search", ldap_msgid(result) );
1800                 }
1801         }
1802
1803         rc = ldap_parse_result( ld, result,
1804                 &err, &matcheddn, &text, &refs, &ctrls, 0 );
1805
1806         if( rc != LDAP_SUCCESS ) {
1807                 ldap_perror(ld, "ldap_parse_result");
1808                 exit( EXIT_FAILURE );
1809         }
1810
1811
1812         if( !ldif ) {
1813                 printf( "result: %d %s\n", err, ldap_err2string(err) );
1814
1815         } else if ( err != LDAP_SUCCESS ) {
1816                 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1817         }
1818
1819         if( matcheddn && *matcheddn ) {
1820                 if( !ldif ) {
1821                         write_ldif( LDIF_PUT_VALUE,
1822                                 "matchedDN", matcheddn, strlen(matcheddn) );
1823                 } else {
1824                         fprintf( stderr, "Matched DN: %s\n", matcheddn );
1825                 }
1826
1827                 ber_memfree( matcheddn );
1828         }
1829
1830         if( text && *text ) {
1831                 if( !ldif ) {
1832                         write_ldif( LDIF_PUT_TEXT, "text",
1833                                 text, strlen(text) );
1834                 } else {
1835                         fprintf( stderr, "Additional information: %s\n", text );
1836                 }
1837
1838                 ber_memfree( text );
1839         }
1840
1841         if( refs ) {
1842                 int i;
1843                 for( i=0; refs[i] != NULL; i++ ) {
1844                         if( !ldif ) {
1845                                 write_ldif( LDIF_PUT_VALUE, "ref", refs[i], strlen(refs[i]) );
1846                         } else {
1847                                 fprintf( stderr, "Referral: %s\n", refs[i] );
1848                         }
1849                 }
1850
1851                 ber_memvfree( (void **) refs );
1852         }
1853
1854         if( ctrls ) {
1855                 print_ctrls( ctrls );
1856                 ldap_controls_free( ctrls );
1857         }
1858
1859         return err;
1860 }
1861
1862 static void print_ctrls(
1863         LDAPControl **ctrls )
1864 {
1865         int i;
1866         for(i=0; ctrls[i] != NULL; i++ ) {
1867                 /* control: OID criticality base64value */
1868                 struct berval *b64 = NULL;
1869                 ber_len_t len;
1870                 char *str;
1871
1872                 len = ldif ? 2 : 0;
1873                 len += strlen( ctrls[i]->ldctl_oid );
1874
1875                 /* add enough for space after OID and the critical value itself */
1876                 len += ctrls[i]->ldctl_iscritical
1877                         ? sizeof("true") : sizeof("false");
1878
1879                 /* convert to base64 */
1880                 if( ctrls[i]->ldctl_value.bv_len ) {
1881                         b64 = ber_memalloc( sizeof(struct berval) );
1882                         
1883                         b64->bv_len = LUTIL_BASE64_ENCODE_LEN(
1884                                 ctrls[i]->ldctl_value.bv_len ) + 1;
1885                         b64->bv_val = ber_memalloc( b64->bv_len + 1 );
1886
1887                         b64->bv_len = lutil_b64_ntop(
1888                                 ctrls[i]->ldctl_value.bv_val, ctrls[i]->ldctl_value.bv_len,
1889                                 b64->bv_val, b64->bv_len );
1890                 }
1891
1892                 if( b64 ) {
1893                         len += 1 + b64->bv_len;
1894                 }
1895
1896                 str = malloc( len + 1 );
1897                 if ( ldif ) {
1898                         strcpy( str, ": " );
1899                 } else {
1900                         str[0] = '\0';
1901                 }
1902                 strcat( str, ctrls[i]->ldctl_oid );
1903                 strcat( str, ctrls[i]->ldctl_iscritical
1904                         ? " true" : " false" );
1905
1906                 if( b64 ) {
1907                         strcat(str, " ");
1908                         strcat(str, b64->bv_val );
1909                 }
1910
1911                 if ( ldif < 2 ) {
1912                         write_ldif( ldif ? LDIF_PUT_COMMENT : LDIF_PUT_VALUE,
1913                                 "control", str, len );
1914                 }
1915
1916                 free( str );
1917                 ber_bvfree( b64 );
1918         }
1919 }
1920
1921 static int
1922 write_ldif( int type, char *name, char *value, ber_len_t vallen )
1923 {
1924         char    *ldif;
1925
1926         if (( ldif = ldif_put( type, name, value, vallen )) == NULL ) {
1927                 return( -1 );
1928         }
1929
1930         fputs( ldif, stdout );
1931         ber_memfree( ldif );
1932
1933         return( 0 );
1934 }
1935
1936
1937 static int 
1938 parse_page_control(
1939         LDAP *ld,
1940         LDAPMessage *result,
1941         struct berval *cookie )
1942 {
1943         int rc;
1944         int err;
1945         LDAPControl **ctrl = NULL;
1946         LDAPControl *ctrlp = NULL;
1947         BerElement *ber;
1948         ber_tag_t tag;
1949         struct berval servercookie = { 0, NULL };
1950
1951
1952         rc = ldap_parse_result( ld, result,
1953                 &err, NULL, NULL, NULL, &ctrl, 0 );
1954
1955         if( rc != LDAP_SUCCESS ) {
1956                 ldap_perror(ld, "ldap_parse_result");
1957                 exit( EXIT_FAILURE );
1958         }
1959
1960         if ( err != LDAP_SUCCESS ) {
1961                 fprintf( stderr, "%s (%d)\n", ldap_err2string(err), err );
1962         }
1963
1964         if( ctrl ) {
1965                 /* Parse the control value
1966                  * searchResult ::= SEQUENCE {
1967                  *              size    INTEGER (0..maxInt),
1968                  *                              -- result set size estimate from server - unused
1969                  *              cookie  OCTET STRING
1970                  */
1971                 ctrlp = *ctrl;
1972                 ber = ber_init( &ctrlp->ldctl_value );
1973                 if ( ber == NULL ) {
1974                         fprintf( stderr, "Internal error.\n" );
1975                         return EXIT_FAILURE;
1976                 }
1977
1978                 tag = ber_scanf( ber, "{im}", &entriesLeft, &servercookie );
1979                 ber_dupbv( cookie, &servercookie );
1980                 (void) ber_free( ber, 1 );
1981
1982                 if( tag == LBER_ERROR ) {
1983                         fprintf( stderr, "Paged results response control could not be decoded.\n" );
1984                         return EXIT_FAILURE;
1985                 }
1986
1987                 if( entriesLeft < 0 ) {
1988                         fprintf( stderr, "Invalid entries estimate in paged results response.\n" );
1989                         return EXIT_FAILURE;
1990                 }
1991
1992
1993                 ldap_controls_free( ctrl );
1994         } else {
1995                 morePagedResults = 0;
1996         }
1997
1998         return err;
1999 }