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