]> git.sur5r.net Git - openldap/blob - servers/ldapd/search.c
merged with autoconf branch
[openldap] / servers / ldapd / search.c
1 /*
2  * Copyright (c) 1990 Regents of the University of Michigan.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of Michigan at Ann Arbor. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16
17 #include <ac/socket.h>
18 #include <ac/string.h>
19
20 #include <quipu/commonarg.h>
21 #include <quipu/attrvalue.h>
22 #include <quipu/ds_error.h>
23 #include <quipu/ds_search.h>
24 #include <quipu/dap2.h>
25 #include <quipu/dua.h>
26
27 #include "lber.h"
28 #include "ldap.h"
29 #include "common.h"
30
31 static get_filter();
32 static get_filter_list();
33 static get_substring_filter();
34
35 #ifdef LDAP_COMPAT
36 extern int      version;
37 extern int      ldap_compat;
38 #define SEARCHRESTAG    (ldap_compat == 20 ? OLD_LDAP_RES_SEARCH_RESULT : LDAP_RES_SEARCH_RESULT)
39 #else
40 #define SEARCHRESTAG    LDAP_RES_SEARCH_RESULT
41 #endif
42
43 int
44 do_search(
45     Sockbuf     *clientsb,
46     struct msg  *m,
47     BerElement  *ber
48 )
49 {
50         int                     rc, err;
51         int                     deref, attrsonly;
52         int                     sizelimit, timelimit;
53         char                    *base;
54         char                    **attrs;
55         struct ds_search_arg    sa;
56         static CommonArgs       common = default_common_args;
57         extern DN               ldap_str2dn();
58
59         Debug( LDAP_DEBUG_TRACE, "do_search\n", 0, 0, 0 );
60
61         /*
62          * Parse the search request.  It looks like this:
63          *      SearchRequest := [APPLICATION 3] SEQUENCE {
64          *              baseObject      DistinguishedName,
65          *              scope           ENUMERATED {
66          *                      baseObject      (0),
67          *                      singleLevel     (1),
68          *                      wholeSubtree    (2)
69          *              },
70          *              derefAliases    ENUMERATED {
71          *                      neverDerefaliases       (0),
72          *                      derefInSearching        (1),
73          *                      derefFindingBaseObj     (2),
74          *                      alwaysDerefAliases      (3)
75          *              },
76          *              sizelimit       INTEGER (0 .. 65535),
77          *              timelimit       INTEGER (0 .. 65535),
78          *              attrsOnly       BOOLEAN,
79          *              filter          Filter,
80          *              attributes      SEQUENCE OF AttributeType
81          *      }
82          */
83
84 #if ISODEPACKAGE == IC
85 #if ICRELEASE > 2
86         DAS_SearchArgument_INIT( &sa );
87 #endif
88 #endif
89
90         if ( ber_scanf( ber, "{aiiiib", &base, &sa.sra_subset, &deref,
91             &sizelimit, &timelimit, &attrsonly ) == LBER_ERROR ) {
92                 send_ldap_msgresult( clientsb, SEARCHRESTAG, m,
93                     LDAP_PROTOCOL_ERROR, NULL, "" );
94                 return( 0 );
95         }
96
97         sa.sra_baseobject = ldap_str2dn( base );
98         if ( sa.sra_baseobject == NULLDN && *base != '\0' ) {
99                 free( base );
100                 send_ldap_msgresult( clientsb, SEARCHRESTAG, m,
101                     LDAP_INVALID_DN_SYNTAX, NULL, "" );
102                 return( 0 );
103         }
104         free( base );
105
106         sa.sra_common = common; /* struct copy */
107         sa.sra_searchaliases = (deref == LDAP_DEREF_SEARCHING ||
108             deref == LDAP_DEREF_ALWAYS);
109         if ( deref == LDAP_DEREF_NEVER || deref == LDAP_DEREF_SEARCHING )
110                 sa.sra_common.ca_servicecontrol.svc_options |=
111                     SVC_OPT_DONTDEREFERENCEALIAS;
112
113         sa.sra_common.ca_servicecontrol.svc_sizelimit = (sizelimit == 0 ?
114             SVC_NOSIZELIMIT : sizelimit);
115
116         sa.sra_common.ca_servicecontrol.svc_timelimit = (timelimit == 0 ?
117             SVC_NOTIMELIMIT : timelimit);
118
119         sa.sra_eis.eis_infotypes = (attrsonly ? EIS_ATTRIBUTETYPESONLY :
120             EIS_ATTRIBUTESANDVALUES);
121
122         /* search filter */
123         if ( (err = get_filter( ber, &sa.sra_filter )) != 0 ) {
124                 send_ldap_msgresult( clientsb, SEARCHRESTAG, m,
125                     err, NULL, "Bad search filter" );
126                 return( 0 );
127         }
128
129 #ifdef LDAP_DEBUG
130         if ( ldap_debug & LDAP_DEBUG_ARGS ) {
131                 PS      ps;
132
133                 ps = ps_alloc( std_open );
134                 std_setup( ps, stderr );
135                 ps_print( ps, "Filter: " );
136                 fi_print( ps, sa.sra_filter, EDBOUT );
137                 ps_print( ps, "\n" );
138                 ps_free( ps );
139         }
140 #endif
141
142         /* attrs to return */
143         attrs = NULL;
144         if ( ber_scanf( ber, "{v}}", &attrs ) == LBER_ERROR ) {
145                 send_ldap_msgresult( clientsb, SEARCHRESTAG, m,
146                     LDAP_PROTOCOL_ERROR, NULL, "" );
147                 return( 0 );
148         }
149         sa.sra_eis.eis_select = NULLATTR;
150         if ( attrs == NULL ) {
151                 sa.sra_eis.eis_allattributes = 1;
152         } else {
153                 Attr_Sequence   as;
154                 int             i;
155
156                 sa.sra_eis.eis_allattributes = 0;
157                 for ( i = 0; attrs[i] != NULL; i++ ) {
158                         AttributeType   type;
159
160                         if ( (type = AttrT_new( attrs[i] )) == NULLAttrT ) {
161                                 Debug( LDAP_DEBUG_TRACE, "unknown attr (%s)\n",
162                                     attrs[i], 0, 0 );
163                                 continue;
164                         }
165
166                         as = as_comp_alloc();
167                         as->attr_type = type;
168                         as->attr_acl = NULLACL_INFO;
169                         as->attr_link = NULLATTR;
170                         as->attr_value = NULLAV;
171
172                         sa.sra_eis.eis_select = as_merge( as,
173                             sa.sra_eis.eis_select );
174                 }
175
176                 /* complain only if we know about none of the attrs */
177                 if ( sa.sra_eis.eis_select == NULLATTR ) {
178                         send_ldap_msgresult( clientsb, SEARCHRESTAG,
179                             m, LDAP_UNDEFINED_TYPE, NULL, attrs[0] );
180                         charlist_free( attrs );
181                         return( 0 );
182                 }
183
184                 charlist_free( attrs );
185         }
186
187         rc = initiate_dap_operation( OP_SEARCH, m, &sa );
188
189 #ifdef LDAP_CONNECTIONLESS
190         if (  m->m_cldap )
191                 m->m_searchbase = sa.sra_baseobject;
192         else
193 #endif /* LDAP_CONNECTIONLESS */
194                 dn_free( sa.sra_baseobject );
195
196         filter_free( sa.sra_filter );
197         as_free( sa.sra_eis.eis_select );
198
199         if ( rc != 0 ) {
200                 send_ldap_msgresult( clientsb, SEARCHRESTAG, m,
201                     rc, NULL, "" );
202                 return( 0 );
203         }
204
205         return( 1 );
206 }
207
208 static get_filter( BerElement *ber, Filter *filt )
209 {
210         unsigned long   tag, len;
211         int             err;
212         char            typestr[64];
213         Filter          f;
214
215         Debug( LDAP_DEBUG_TRACE, "get_filter\n", 0, 0, 0 );
216
217         /*
218          * A filter looks like this coming in:
219          *      Filter ::= CHOICE {
220          *              and             [0]     SET OF Filter,
221          *              or              [1]     SET OF Filter,
222          *              not             [2]     Filter,
223          *              equalityMatch   [3]     AttributeValueAssertion,
224          *              substrings      [4]     SubstringFilter,
225          *              greaterOrEqual  [5]     AttributeValueAssertion,
226          *              lessOrEqual     [6]     AttributeValueAssertion,
227          *              present         [7]     AttributeType,,
228          *              approxMatch     [8]     AttributeValueAssertion
229          *      }
230          *
231          *      SubstringFilter ::= SEQUENCE {
232          *              type               AttributeType,
233          *              SEQUENCE OF CHOICE {
234          *                      initial          [0] IA5String,
235          *                      any              [1] IA5String,
236          *                      final            [2] IA5String
237          *              }
238          *      }
239          */
240
241         f = filter_alloc();
242         *filt = f;
243         f->flt_next = NULLFILTER;
244
245         err = 0;
246         switch (tag = ber_peek_tag( ber, &len )) {
247 #ifdef LDAP_COMPAT20
248         case OLD_LDAP_FILTER_EQUALITY:
249 #endif
250         case LDAP_FILTER_EQUALITY:
251                 Debug( LDAP_DEBUG_ARGS, "EQUALITY\n", 0, 0, 0 );
252                 f->flt_type = FILTER_ITEM;
253                 f->FUITEM.fi_type = FILTERITEM_EQUALITY;
254 #ifdef LDAP_COMPAT30
255                 if ( ldap_compat == 30 )
256                         (void) ber_skip_tag( ber, &len );
257 #endif
258
259                 if ( (err = get_ava( ber, &f->FUITEM.UNAVA )) != 0 ) {
260                         free( f );
261                         return( err );
262                 }
263                 break;
264
265 #ifdef LDAP_COMPAT20
266         case OLD_LDAP_FILTER_SUBSTRINGS:
267 #endif
268         case LDAP_FILTER_SUBSTRINGS:
269                 Debug( LDAP_DEBUG_ARGS, "SUBSTRINGS\n", 0, 0, 0 );
270                 err = get_substring_filter( ber, f );
271                 break;
272
273 #ifdef LDAP_COMPAT20
274         case OLD_LDAP_FILTER_GE:
275 #endif
276         case LDAP_FILTER_GE:
277                 Debug( LDAP_DEBUG_ARGS, "GE\n", 0, 0, 0 );
278                 f->flt_type = FILTER_ITEM;
279                 f->FUITEM.fi_type = FILTERITEM_GREATEROREQUAL;
280 #ifdef LDAP_COMPAT30
281                 if ( ldap_compat == 30 )
282                         (void) ber_skip_tag( ber, &len );
283 #endif
284                 if ( (err = get_ava( ber, &f->FUITEM.UNAVA )) != 0 ) {
285                         free( f );
286                         return( err );
287                 }
288                 break;
289
290 #ifdef LDAP_COMPAT20
291         case OLD_LDAP_FILTER_LE:
292 #endif
293         case LDAP_FILTER_LE:
294                 Debug( LDAP_DEBUG_ARGS, "LE\n", 0, 0, 0 );
295                 f->flt_type = FILTER_ITEM;
296                 f->FUITEM.fi_type = FILTERITEM_LESSOREQUAL;
297 #ifdef LDAP_COMPAT30
298                 if ( ldap_compat == 30 )
299                         (void) ber_skip_tag( ber, &len );
300 #endif
301
302                 if ( (err = get_ava( ber, &f->FUITEM.UNAVA )) != 0 ) {
303                         free( f );
304                         return( err );
305                 }
306                 break;
307
308 #ifdef LDAP_COMPAT20
309         case OLD_LDAP_FILTER_PRESENT:
310 #endif
311 #ifdef LDAP_COMPAT30
312         case LDAP_FILTER_PRESENT_30:
313 #endif
314         case LDAP_FILTER_PRESENT:
315                 Debug( LDAP_DEBUG_ARGS, "PRESENT\n", 0, 0, 0 );
316                 f->flt_type = FILTER_ITEM;
317                 f->FUITEM.fi_type = FILTERITEM_PRESENT;
318                 len = sizeof(typestr);
319 #ifdef LDAP_COMPAT30
320                 if ( ldap_compat == 30 )
321                         (void) ber_skip_tag( ber, &len );
322 #endif
323
324                 if ( ber_scanf( ber, "s", typestr, &len ) == LBER_ERROR )
325                         return( LDAP_PROTOCOL_ERROR );
326                 if ( (f->FUITEM.UNTYPE = str2AttrT( typestr )) == NULLAttrT )
327                         return( LDAP_UNDEFINED_TYPE );
328                 break;
329
330 #ifdef LDAP_COMPAT20
331         case OLD_LDAP_FILTER_APPROX:
332 #endif
333         case LDAP_FILTER_APPROX:
334                 Debug( LDAP_DEBUG_ARGS, "APPROX\n", 0, 0, 0 );
335                 f->flt_type = FILTER_ITEM;
336                 f->FUITEM.fi_type = FILTERITEM_APPROX;
337 #ifdef LDAP_COMPAT30
338                 if ( ldap_compat == 30 )
339                         (void) ber_skip_tag( ber, &len );
340 #endif
341
342                 if ( (err = get_ava( ber, &f->FUITEM.UNAVA )) != 0 ) {
343                         free( f );
344                         return( err );
345                 }
346                 break;
347
348 #ifdef LDAP_COMPAT20
349         case OLD_LDAP_FILTER_AND:
350 #endif
351         case LDAP_FILTER_AND:
352                 Debug( LDAP_DEBUG_ARGS, "AND\n", 0, 0, 0 );
353                 f->flt_type = FILTER_AND;
354                 err = get_filter_list( ber, f );
355                 break;
356
357 #ifdef LDAP_COMPAT20
358         case OLD_LDAP_FILTER_OR:
359 #endif
360         case LDAP_FILTER_OR:
361                 Debug( LDAP_DEBUG_ARGS, "OR\n", 0, 0, 0 );
362                 f->flt_type = FILTER_OR;
363                 err = get_filter_list( ber, f );
364                 break;
365
366 #ifdef LDAP_COMPAT20
367         case OLD_LDAP_FILTER_NOT:
368 #endif
369         case LDAP_FILTER_NOT:
370                 Debug( LDAP_DEBUG_ARGS, "NOT\n", 0, 0, 0 );
371                 f->flt_type = FILTER_NOT;
372                 (void) ber_skip_tag( ber, &len );
373                 err = get_filter( ber, &f->FUFILT );
374                 break;
375
376         default:
377                 Debug( LDAP_DEBUG_ANY, "unknown filter type %d\n", tag, 0, 0 );
378                 free( f );
379                 return( LDAP_PROTOCOL_ERROR );
380                 break;
381         }
382
383         Debug( LDAP_DEBUG_TRACE, "end get_filter\n", 0, 0, 0 );
384         return( err );
385 }
386
387 static get_filter_list( BerElement *ber, Filter f )
388 {
389         Filter          new, tail;
390         int             err;
391         unsigned long   tag, len;
392         char            *last;
393
394         Debug( LDAP_DEBUG_TRACE, "get_filter_list\n", 0, 0, 0 );
395
396 #ifdef LDAP_COMPAT30
397         if ( ldap_compat == 30 )
398                 (void) ber_skip_tag( ber, &len );
399 #endif
400         f->FUFILT = tail = NULLFILTER;
401         for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
402             tag = ber_next_element( ber, &len, last ) ) {
403                 if ( (err = get_filter( ber, &new )) != 0 )
404                         return( err );
405
406                 if ( f->FUFILT == NULLFILTER ) {
407                         f->FUFILT = new;
408                 } else {
409                         tail->flt_next = new;
410                 }
411                 tail = new;
412         }
413
414         Debug( LDAP_DEBUG_TRACE, "end get_filter_list\n", 0, 0, 0 );
415         return( 0 );
416 }
417
418 static get_substring_filter( BerElement *ber, Filter f )
419 {
420         unsigned long   tag, len;
421         char            typestr[64];
422         AttributeType   type;
423         char            *valstr, *last;
424         AttributeValue  value;
425         extern short    ldap_dn_syntax;
426
427         Debug( LDAP_DEBUG_TRACE, "get_substring_filter\n", 0, 0, 0 );
428
429 #ifdef LDAP_COMPAT30
430         if ( ldap_compat == 30 )
431                 (void) ber_skip_tag( ber, &len );
432 #endif
433
434         f->flt_type = FILTER_ITEM;
435         f->FUITEM.fi_type = FILTERITEM_SUBSTRINGS;
436         len = sizeof(typestr);
437         if ( ber_scanf( ber, "{s", typestr, &len ) == LBER_ERROR ) {
438                 return( LDAP_PROTOCOL_ERROR );
439         }
440         if ( (type = str2AttrT( typestr )) == NULLAttrT ) {
441                 return( LDAP_UNDEFINED_TYPE );
442         }
443         f->FUITEM.UNSUB.fi_sub_type = type;
444         f->FUITEM.UNSUB.fi_sub_initial = NULLAV;
445         f->FUITEM.UNSUB.fi_sub_any = NULLAV;
446         f->FUITEM.UNSUB.fi_sub_final = NULLAV;
447         for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
448             tag = ber_next_element( ber, &len, last ) ) {
449                 AV_Sequence     avs, any_end;
450
451 #ifdef LDAP_COMPAT30
452                 if ( ldap_compat == 30 ) {
453                         if ( ber_scanf( ber, "{a}", &valstr ) == LBER_ERROR ) {
454                                 return( LDAP_PROTOCOL_ERROR );
455                         }
456                 } else
457 #endif
458                         if ( ber_scanf( ber, "a", &valstr ) == LBER_ERROR ) {
459                                 return( LDAP_PROTOCOL_ERROR );
460                         }
461
462                 value = ldap_str2AttrV( valstr, type->oa_syntax );
463                 free( valstr );
464
465                 if ( value == NULLAttrV ) {
466                         return( LDAP_INVALID_SYNTAX );
467                 }
468
469                 if ( (avs = avs_comp_new( value )) == NULLAV )
470                         return( LDAP_OPERATIONS_ERROR );
471
472                 switch ( tag ) {
473 #ifdef LDAP_COMPAT20
474                 case OLD_LDAP_SUBSTRING_INITIAL:
475 #endif
476 #ifdef LDAP_COMPAT30
477                 case LDAP_SUBSTRING_INITIAL_30:
478 #endif
479                 case LDAP_SUBSTRING_INITIAL:
480                         Debug( LDAP_DEBUG_ARGS, "  INITIAL\n", 0, 0, 0 );
481                         if ( f->FUITEM.UNSUB.fi_sub_initial != NULLAV
482                             && f->FUITEM.UNSUB.fi_sub_initial->avseq_next
483                             != NULLAV ) {
484                                 return( LDAP_PROTOCOL_ERROR );
485                         }
486                         f->FUITEM.UNSUB.fi_sub_initial = avs;
487                         break;
488
489 #ifdef LDAP_COMPAT20
490                 case OLD_LDAP_SUBSTRING_ANY:
491 #endif
492 #ifdef LDAP_COMPAT30
493                 case LDAP_SUBSTRING_ANY_30:
494 #endif
495                 case LDAP_SUBSTRING_ANY:
496                         Debug( LDAP_DEBUG_ARGS, "  ANY\n", 0, 0, 0 );
497         
498                         if (f->FUITEM.UNSUB.fi_sub_any != NULLAV) {
499                                 any_end->avseq_next = avs;
500                         } else {
501                                 f->FUITEM.UNSUB.fi_sub_any = avs;
502                         }
503
504                         any_end = avs;
505                         break;
506
507 #ifdef LDAP_COMPAT20
508                 case OLD_LDAP_SUBSTRING_FINAL:
509 #endif
510 #ifdef LDAP_COMPAT30
511                 case LDAP_SUBSTRING_FINAL_30:
512 #endif
513                 case LDAP_SUBSTRING_FINAL:
514                         Debug( LDAP_DEBUG_ARGS, "  FINAL\n", 0, 0, 0 );
515                         if ( f->FUITEM.UNSUB.fi_sub_final != NULLAV
516                             && f->FUITEM.UNSUB.fi_sub_final->avseq_next
517                             != NULLAV ) {
518                                 return( LDAP_PROTOCOL_ERROR );
519                         }
520                         f->FUITEM.UNSUB.fi_sub_final = avs;
521                         break;
522
523                 default:
524                         Debug( LDAP_DEBUG_ARGS, "  unknown type\n", tag, 0, 0 );
525                         return( LDAP_PROTOCOL_ERROR );
526                 }
527         }
528
529         Debug( LDAP_DEBUG_TRACE, "end get_substring_filter\n", 0, 0, 0 );
530         return( 0 );
531 }
532
533 void
534 search_result(
535     Sockbuf                     *sb,
536     struct msg                  *m,
537     struct ds_search_result     *sr
538 )
539 {
540         EntryInfo       *e;
541         BerElement      *ber;
542         int             rc;
543
544         Debug( LDAP_DEBUG_TRACE, "search_result\n", 0, 0, 0 );
545
546         ber = NULL;
547
548         if ( ! sr->srr_correlated ) {
549                 Debug( LDAP_DEBUG_ARGS, "correlating results\n", 0, 0, 0 );
550                 correlate_search_results( sr );
551         }
552
553 #ifdef LDAP_CONNECTIONLESS
554         if ( m->m_cldap ) {
555                 if ((ber = der_alloc()) == NULLBER ) {
556                         send_ldap_msgresult( sb, SEARCHRESTAG, m,
557                             LDAP_OPERATIONS_ERROR, NULL, "der_alloc" );
558                         return;
559                 }
560                 if ( ber_printf( ber, "t{is{", LBER_SEQUENCE, m->m_msgid,
561                     "" ) == -1 ) {
562                         send_ldap_msgresult( sb, SEARCHRESTAG, m,
563                             LDAP_OPERATIONS_ERROR, NULL, "ber_printf" );
564                         return;
565                 }
566         }
567 #endif
568
569         for ( e = sr->CSR_entries; e != NULLENTRYINFO; e = e->ent_next ) {
570                 Debug( LDAP_DEBUG_ARGS, "\tentry:\n", 0, 0, 0 );
571
572 #ifdef LDAP_CONNECTIONLESS
573                 if ( !m->m_cldap )
574 #endif /* LDAP_CONNECTIONLESS */
575
576                         if ( (ber = der_alloc()) == NULLBER ) {
577                                 send_ldap_msgresult( sb, SEARCHRESTAG, m,
578                                     LDAP_OPERATIONS_ERROR, NULL, "der_alloc" );
579                                 return;
580                         }
581
582 #ifdef LDAP_COMPAT20
583                 if ( version == 1 ) {
584                         if ( ber_printf( ber, "t{it{", OLD_LBER_SEQUENCE,
585                             m->m_msgid, OLD_LDAP_RES_SEARCH_ENTRY ) == -1 ) {
586                                 send_ldap_msgresult( sb, SEARCHRESTAG, m,
587                                     LDAP_OPERATIONS_ERROR, NULL, "ber_printf" );
588                                 return;
589                         }
590                 } else
591 #endif
592 #ifdef LDAP_COMPAT30
593                 if ( ldap_compat == 30 ) {
594                         if ( ber_printf( ber, "{it{{", m->m_msgid,
595                             LDAP_RES_SEARCH_ENTRY ) == -1 ) {
596                                 send_ldap_msgresult( sb, SEARCHRESTAG, m,
597                                     LDAP_OPERATIONS_ERROR, NULL, "ber_printf" );
598                                 return;
599                         }
600                 } else
601 #endif
602 #ifdef LDAP_CONNECTIONLESS
603                 if ( m->m_cldap )
604                         rc = ber_printf( ber, "t{", LDAP_RES_SEARCH_ENTRY );
605                 else
606 #endif /* LDAP_CONNECTIONLESS */
607                         rc = ber_printf( ber, "{it{", m->m_msgid,
608                             LDAP_RES_SEARCH_ENTRY );
609
610                 if ( rc == -1 ) {
611                         send_ldap_msgresult( sb, SEARCHRESTAG, m,
612                             LDAP_OPERATIONS_ERROR, NULL, "ber_printf" );
613                         return;
614                 }
615
616 #ifdef LDAP_CONNECTIONLESS
617                 if (  m->m_cldap )
618                         rc = encode_dn( ber, e->ent_dn, m->m_searchbase );
619 #endif /* LDAP_CONNECTIONLESS */
620                 else
621                         rc = encode_dn( ber, e->ent_dn, NULLDN );
622
623                 if ( rc == -1 ) {
624                         send_ldap_msgresult( sb, SEARCHRESTAG, m,
625                             LDAP_OPERATIONS_ERROR, NULL, "encode_dn" );
626                         return;
627                 }
628
629                 if ( encode_attrs( ber, e->ent_attr ) == -1 ) {
630                         send_ldap_msgresult( sb, SEARCHRESTAG, m,
631                             LDAP_OPERATIONS_ERROR, NULL, "encode_attrs" );
632                         return;
633                 }
634
635 #ifdef LDAP_COMPAT20
636                 if ( version == 1 ) {
637                         if ( ber_printf( ber, "}}" ) == -1 ) {
638                                 send_ldap_msgresult( sb, SEARCHRESTAG, m,
639                                     LDAP_OPERATIONS_ERROR, NULL,
640                                     "ber_printf 2" );
641                                 return;
642                         }
643                 } else
644 #endif
645 #ifdef LDAP_COMPAT30
646                 if ( ldap_compat == 30 ) {
647                         if ( ber_printf( ber, "}}}" ) == -1 ) {
648                                 send_ldap_msgresult( sb, SEARCHRESTAG, m,
649                                     LDAP_OPERATIONS_ERROR, NULL,
650                                     "ber_printf 2" );
651                                 return;
652                         }
653                 } else
654 #endif
655 #ifdef LDAP_CONNECTIONLESS
656                 if ( m->m_cldap )
657                         rc = ber_printf( ber, "}" );
658                 else
659 #endif /* LDAP_CONNECTIONLESS */
660                         rc = ber_printf( ber, "}}" );
661
662                 if ( rc == -1 ) {
663                         send_ldap_msgresult( sb, SEARCHRESTAG, m,
664                             LDAP_OPERATIONS_ERROR, NULL, "ber_printf 2" );
665                         return;
666                 }
667
668 #ifdef LDAP_DEBUG
669                 if ( ldap_debug & LDAP_DEBUG_BER )
670                         trace_ber( 0, ber->ber_ptr - ber->ber_buf,
671                             ber->ber_buf, stderr, 0, 0 );
672 #endif
673
674 #ifdef LDAP_CONNECTIONLESS
675                 if ( !m->m_cldap )
676 #endif
677                         (void) ber_flush( sb, ber, 1 );
678         }
679
680         switch ( sr->CSR_limitproblem ) {
681         case LSR_NOLIMITPROBLEM:
682                 rc = LDAP_SUCCESS;
683                 break;
684         case LSR_TIMELIMITEXCEEDED:
685                 rc = LDAP_TIMELIMIT_EXCEEDED;
686                 break;
687         case LSR_SIZELIMITEXCEEDED:
688         case LSR_ADMINSIZEEXCEEDED:
689                 rc = LDAP_SIZELIMIT_EXCEEDED;
690                 break;
691         }
692
693         Debug( LDAP_DEBUG_ARGS, "\tresult:\n", 0, 0, 0 );
694
695 #ifdef LDAP_CONNECTIONLESS
696         if ( m->m_cldap ) {
697                 if ( ber_printf( ber, "t{ess}}}", SEARCHRESTAG, rc, "", "" )
698                     == -1 ) {
699                         send_ldap_msgresult( sb, SEARCHRESTAG, m,
700                             LDAP_OPERATIONS_ERROR, NULL, "ber_printf" );
701                         return;
702                 }
703                 SAFEMEMCPY( (char *)sb->sb_useaddr, &m->m_clientaddr,
704                     sizeof( struct sockaddr ));
705                 if ( ber_flush( sb, ber, 1 ) != 0 ) {
706                     send_ldap_msgresult( sb, SEARCHRESTAG, m, 
707                         LDAP_RESULTS_TOO_LARGE, NULL, "ber_flush" );
708                 }
709         } else
710 #endif
711         send_ldap_msgresult( sb, SEARCHRESTAG, m, rc, NULL, "" );
712
713         return;
714 }