]> git.sur5r.net Git - openldap/blob - servers/slapd/result.c
081eae7b0ec6d8929f5c0536ae7a797e76275dd0
[openldap] / servers / slapd / result.c
1 /* result.c - routines to send ldap results, errors, and referrals */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/socket.h>
32 #include <ac/errno.h>
33 #include <ac/string.h>
34 #include <ac/ctype.h>
35 #include <ac/time.h>
36 #include <ac/unistd.h>
37
38 #include "slap.h"
39
40 const struct berval slap_dummy_bv = BER_BVNULL;
41
42 int slap_null_cb( Operation *op, SlapReply *rs )
43 {
44         return 0;
45 }
46
47 int slap_freeself_cb( Operation *op, SlapReply *rs )
48 {
49         assert( op->o_callback != NULL );
50
51         op->o_tmpfree( op->o_callback, op->o_tmpmemctx );
52         op->o_callback = NULL;
53
54         return SLAP_CB_CONTINUE;
55 }
56
57 int slap_replog_cb( Operation *op, SlapReply *rs )
58 {
59         if ( rs->sr_err == LDAP_SUCCESS ) {
60                 replog( op );
61         }
62         return SLAP_CB_CONTINUE;
63 }
64
65 static char *v2ref( BerVarray ref, const char *text )
66 {
67         size_t len = 0, i = 0;
68         char *v2;
69
70         if(ref == NULL) {
71                 if (text) {
72                         return ch_strdup(text);
73                 } else {
74                         return NULL;
75                 }
76         }
77         
78         if ( text != NULL ) {
79                 len = strlen( text );
80                 if (text[len-1] != '\n') {
81                     i = 1;
82                 }
83         }
84
85         v2 = SLAP_MALLOC( len+i+sizeof("Referral:") );
86         if( v2 == NULL ) {
87                 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
88                 return NULL;
89         }
90
91         if( text != NULL ) {
92                 strcpy(v2, text);
93                 if( i ) {
94                         v2[len++] = '\n';
95                 }
96         }
97         strcpy( v2+len, "Referral:" );
98         len += sizeof("Referral:");
99
100         for( i=0; ref[i].bv_val != NULL; i++ ) {
101                 v2 = SLAP_REALLOC( v2, len + ref[i].bv_len + 1 );
102                 if( v2 == NULL ) {
103                         Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
104                         return NULL;
105                 }
106                 v2[len-1] = '\n';
107                 AC_MEMCPY(&v2[len], ref[i].bv_val, ref[i].bv_len );
108                 len += ref[i].bv_len;
109                 if (ref[i].bv_val[ref[i].bv_len-1] != '/') {
110                         ++len;
111                 }
112         }
113
114         v2[len-1] = '\0';
115         return v2;
116 }
117
118 static ber_tag_t req2res( ber_tag_t tag )
119 {
120         switch( tag ) {
121         case LDAP_REQ_ADD:
122         case LDAP_REQ_BIND:
123         case LDAP_REQ_COMPARE:
124         case LDAP_REQ_EXTENDED:
125         case LDAP_REQ_MODIFY:
126         case LDAP_REQ_MODRDN:
127                 tag++;
128                 break;
129
130         case LDAP_REQ_DELETE:
131                 tag = LDAP_RES_DELETE;
132                 break;
133
134         case LDAP_REQ_ABANDON:
135         case LDAP_REQ_UNBIND:
136                 tag = LBER_SEQUENCE;
137                 break;
138
139         case LDAP_REQ_SEARCH:
140                 tag = LDAP_RES_SEARCH_RESULT;
141                 break;
142
143         default:
144                 tag = LBER_SEQUENCE;
145         }
146
147         return tag;
148 }
149
150 static long send_ldap_ber(
151         Connection *conn,
152         BerElement *ber )
153 {
154         ber_len_t bytes;
155
156         ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
157
158         /* write only one pdu at a time - wait til it's our turn */
159         ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
160
161         /* lock the connection */ 
162         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
163
164         /* write the pdu */
165         while( 1 ) {
166                 int err;
167                 ber_socket_t    sd;
168
169                 if ( connection_state_closing( conn ) ) {
170                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
171                         ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
172
173                         return 0;
174                 }
175
176                 if ( ber_flush( conn->c_sb, ber, 0 ) == 0 ) {
177                         break;
178                 }
179
180                 err = errno;
181
182                 /*
183                  * we got an error.  if it's ewouldblock, we need to
184                  * wait on the socket being writable.  otherwise, figure
185                  * it's a hard error and return.
186                  */
187
188                 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
189                     err, sock_errstr(err), 0 );
190
191                 if ( err != EWOULDBLOCK && err != EAGAIN ) {
192                         connection_closing( conn );
193
194                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
195                         ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
196
197                         return( -1 );
198                 }
199
200                 /* wait for socket to be write-ready */
201                 conn->c_writewaiter = 1;
202                 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
203                 slapd_set_write( sd, 1 );
204
205                 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
206                 conn->c_writewaiter = 0;
207         }
208
209         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
210         ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
211
212         return bytes;
213 }
214
215 static int
216 send_ldap_control( BerElement *ber, LDAPControl *c )
217 {
218         int rc;
219
220         assert( c != NULL );
221
222         rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
223
224         if( c->ldctl_iscritical ) {
225                 rc = ber_printf( ber, "b",
226                         (ber_int_t) c->ldctl_iscritical ) ;
227                 if( rc == -1 ) return rc;
228         }
229
230         if( c->ldctl_value.bv_val != NULL ) {
231                 rc = ber_printf( ber, "O", &c->ldctl_value ); 
232                 if( rc == -1 ) return rc;
233         }
234
235         rc = ber_printf( ber, /*{*/"N}" );
236         if( rc == -1 ) return rc;
237
238         return 0;
239 }
240
241 static int
242 send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
243 {
244         int rc;
245
246         if( c == NULL )
247                 return 0;
248
249         rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
250         if( rc == -1 ) return rc;
251
252         for( ; *c != NULL; c++) {
253                 rc = send_ldap_control( ber, *c );
254                 if( rc == -1 ) return rc;
255         }
256
257 #ifdef LDAP_DEVEL
258         /* this is a hack to avoid having to modify op->s_ctrls */
259         if( o->o_sortedresults ) {
260                 BerElementBuffer berbuf;
261                 BerElement *sber = (BerElement *) &berbuf;
262                 LDAPControl sorted;
263                 BER_BVZERO( &sorted.ldctl_value );
264                 sorted.ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
265                 sorted.ldctl_iscritical = 0;
266
267                 ber_init2( sber, NULL, LBER_USE_DER );
268
269                 ber_printf( sber, "{i}", LDAP_UNWILLING_TO_PERFORM );
270
271                 if( ber_flatten2( ber, &sorted.ldctl_value, 0 ) == -1 ) {
272                         return -1;
273                 }
274
275                 (void) ber_free_buf( ber );
276
277                 rc = send_ldap_control( ber, &sorted );
278                 if( rc == -1 ) return rc;
279         }
280 #endif
281
282         rc = ber_printf( ber, /*{*/"N}" );
283
284         return rc;
285 }
286
287 static int
288 send_ldap_response(
289         Operation *op,
290         SlapReply *rs )
291 {
292         BerElementBuffer berbuf;
293         BerElement      *ber = (BerElement *) &berbuf;
294         int             rc = LDAP_SUCCESS;
295         long    bytes;
296
297         if ( rs->sr_err == SLAPD_ABANDON ) {
298                 rc = SLAPD_ABANDON;
299                 goto clean2;
300         }
301
302         if ( op->o_callback ) {
303                 int             first = 1;
304                 slap_callback   *sc = op->o_callback,
305                                 *sc_next = op->o_callback;
306
307                 rc = SLAP_CB_CONTINUE;
308                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
309                         sc_next = op->o_callback->sc_next;
310                         if ( op->o_callback->sc_response ) {
311                                 rc = op->o_callback->sc_response( op, rs );
312                                 if ( first && op->o_callback == NULL ) {
313                                         sc = NULL;
314                                 }
315                                 if ( rc != SLAP_CB_CONTINUE ) break;
316                         }
317                         first = 0;
318                 }
319
320                 op->o_callback = sc;
321                 if ( rc != SLAP_CB_CONTINUE ) goto clean2;
322         }
323
324 #ifdef LDAP_CONNECTIONLESS
325         if (op->o_conn && op->o_conn->c_is_udp)
326                 ber = op->o_res_ber;
327         else
328 #endif
329         {
330                 ber_init_w_nullc( ber, LBER_USE_DER );
331                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
332         }
333
334         Debug( LDAP_DEBUG_TRACE,
335                 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
336                 rs->sr_msgid, rs->sr_tag, rs->sr_err );
337
338         if( rs->sr_ref ) {
339                 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
340                         rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
341                         NULL, NULL );
342         }
343
344 #ifdef LDAP_CONNECTIONLESS
345         if (op->o_conn && op->o_conn->c_is_udp &&
346                 op->o_protocol == LDAP_VERSION2 )
347         {
348                 rc = ber_printf( ber, "t{ess" /*"}"*/,
349                         rs->sr_tag, rs->sr_err,
350                 rs->sr_matched == NULL ? "" : rs->sr_matched,
351                 rs->sr_text == NULL ? "" : rs->sr_text );
352         } else 
353 #endif
354         if ( rs->sr_type == REP_INTERMEDIATE ) {
355             rc = ber_printf( ber, "{it{" /*"}}"*/,
356                         rs->sr_msgid, rs->sr_tag );
357
358         } else {
359             rc = ber_printf( ber, "{it{ess" /*"}}"*/,
360                 rs->sr_msgid, rs->sr_tag, rs->sr_err,
361                 rs->sr_matched == NULL ? "" : rs->sr_matched,
362                 rs->sr_text == NULL ? "" : rs->sr_text );
363         }
364
365         if( rc != -1 ) {
366                 if ( rs->sr_ref != NULL ) {
367                         assert( rs->sr_err == LDAP_REFERRAL );
368                         rc = ber_printf( ber, "t{W}",
369                                 LDAP_TAG_REFERRAL, rs->sr_ref );
370                 } else {
371                         assert( rs->sr_err != LDAP_REFERRAL );
372                 }
373         }
374
375         if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
376                 rc = ber_printf( ber, "tO",
377                         LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
378         }
379
380         if( rc != -1 &&
381                 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
382         {
383                 if ( rs->sr_rspoid != NULL ) {
384                         rc = ber_printf( ber, "ts",
385                                 rs->sr_type == REP_EXTENDED
386                                         ? LDAP_TAG_EXOP_RES_OID : LDAP_TAG_IM_RES_OID,
387                                 rs->sr_rspoid );
388                 }
389                 if( rc != -1 && rs->sr_rspdata != NULL ) {
390                         rc = ber_printf( ber, "tO",
391                                 rs->sr_type == REP_EXTENDED
392                                         ? LDAP_TAG_EXOP_RES_VALUE : LDAP_TAG_IM_RES_VALUE,
393                                 rs->sr_rspdata );
394                 }
395         }
396
397         if( rc != -1 ) {
398                 rc = ber_printf( ber, /*"{"*/ "N}" );
399         }
400
401         if( rc != -1 ) {
402                 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
403         }
404
405         if( rc != -1 ) {
406                 rc = ber_printf( ber, /*"{"*/ "N}" );
407         }
408
409 #ifdef LDAP_CONNECTIONLESS
410         if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
411                 && rc != -1 )
412         {
413                 rc = ber_printf( ber, /*"{"*/ "N}" );
414         }
415 #endif
416                 
417         if ( rc == -1 ) {
418                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
419
420 #ifdef LDAP_CONNECTIONLESS
421                 if (!op->o_conn || op->o_conn->c_is_udp == 0)
422 #endif
423                 {
424                         ber_free_buf( ber );
425                 }
426                 goto cleanup;
427         }
428
429         /* send BER */
430         bytes = send_ldap_ber( op->o_conn, ber );
431 #ifdef LDAP_CONNECTIONLESS
432         if (!op->o_conn || op->o_conn->c_is_udp == 0)
433 #endif
434         {
435                 ber_free_buf( ber );
436         }
437
438         if ( bytes < 0 ) {
439                 Debug( LDAP_DEBUG_ANY,
440                         "send_ldap_response: ber write failed\n",
441                         0, 0, 0 );
442
443                 goto cleanup;
444         }
445
446         ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
447         ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
448         ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
449         ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
450
451 cleanup:;
452         /* Tell caller that we did this for real, as opposed to being
453          * overridden by a callback
454          */
455         rc = SLAP_CB_CONTINUE;
456
457 clean2:;
458         if ( op->o_callback ) {
459                 int             first = 1;
460                 slap_callback   *sc = op->o_callback, *sc_next;
461
462                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
463                         sc_next = op->o_callback->sc_next;
464                         if ( op->o_callback->sc_cleanup ) {
465                                 (void)op->o_callback->sc_cleanup( op, rs );
466                                 if ( first && op->o_callback != sc ) {
467                                         sc = op->o_callback;
468                                 }
469                         }
470                         first = 0;
471                 }
472                 op->o_callback = sc;
473         }
474
475
476         if ( rs->sr_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
477                 free( (char *)rs->sr_matched );
478                 rs->sr_matched = NULL;
479         }
480
481         if ( rs->sr_ref && rs->sr_flags & REP_REF_MUSTBEFREED ) {
482                 ber_bvarray_free( rs->sr_ref );
483                 rs->sr_ref = NULL;
484         }
485
486         return rc;
487 }
488
489
490 void
491 send_ldap_disconnect( Operation *op, SlapReply *rs )
492 {
493 #define LDAP_UNSOLICITED_ERROR(e) \
494         (  (e) == LDAP_PROTOCOL_ERROR \
495         || (e) == LDAP_STRONG_AUTH_REQUIRED \
496         || (e) == LDAP_UNAVAILABLE )
497
498         assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
499
500         rs->sr_type = REP_EXTENDED;
501
502         Debug( LDAP_DEBUG_TRACE,
503                 "send_ldap_disconnect %d:%s\n",
504                 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
505
506         if ( op->o_protocol < LDAP_VERSION3 ) {
507                 rs->sr_rspoid = NULL;
508                 rs->sr_tag = req2res( op->o_tag );
509                 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
510
511         } else {
512                 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
513                 rs->sr_tag = LDAP_RES_EXTENDED;
514                 rs->sr_msgid = 0;
515         }
516
517         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
518                 Statslog( LDAP_DEBUG_STATS,
519                         "%s DISCONNECT tag=%lu err=%d text=%s\n",
520                         op->o_log_prefix, rs->sr_tag, rs->sr_err,
521                         rs->sr_text ? rs->sr_text : "", 0 );
522         }
523 }
524
525 void
526 slap_send_ldap_result( Operation *op, SlapReply *rs )
527 {
528         char *tmp = NULL;
529         const char *otext = rs->sr_text;
530         BerVarray oref = rs->sr_ref;
531
532         rs->sr_type = REP_RESULT;
533
534         /* Propagate Abandons so that cleanup callbacks can be processed */
535         if ( rs->sr_err == SLAPD_ABANDON )
536                 goto abandon;
537
538         assert( !LDAP_API_ERROR( rs->sr_err ) );
539
540         Debug( LDAP_DEBUG_TRACE,
541                 "send_ldap_result: %s p=%d\n",
542                 op->o_log_prefix, op->o_protocol, 0 );
543
544         Debug( LDAP_DEBUG_ARGS,
545                 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
546                 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
547                 rs->sr_text ? rs->sr_text : "" );
548
549
550         if( rs->sr_ref ) {
551                 Debug( LDAP_DEBUG_ARGS,
552                         "send_ldap_result: referral=\"%s\"\n",
553                         rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
554                         NULL, NULL );
555         }
556
557         assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
558
559         if ( rs->sr_err == LDAP_REFERRAL ) {
560 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
561                 if( op->o_domain_scope ) {
562                         rs->sr_ref = NULL;
563                 }
564 #endif
565                 if( rs->sr_ref == NULL ) {
566                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
567                 } else if ( op->o_protocol < LDAP_VERSION3 ) {
568                         rs->sr_err = LDAP_PARTIAL_RESULTS;
569                 }
570         }
571
572         if ( op->o_protocol < LDAP_VERSION3 ) {
573                 tmp = v2ref( rs->sr_ref, rs->sr_text );
574                 rs->sr_text = tmp;
575                 rs->sr_ref = NULL;
576         }
577
578         rs->sr_tag = req2res( op->o_tag );
579         rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
580
581 abandon:
582         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
583                 if ( op->o_tag == LDAP_REQ_SEARCH ) {
584                         char nbuf[64];
585                         snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
586                                 rs->sr_err, rs->sr_nentries );
587
588                         Statslog( LDAP_DEBUG_STATS,
589                         "%s SEARCH RESULT tag=%lu err=%s text=%s\n",
590                                 op->o_log_prefix, rs->sr_tag, nbuf,
591                                 rs->sr_text ? rs->sr_text : "", 0 );
592                 } else {
593                         Statslog( LDAP_DEBUG_STATS,
594                                 "%s RESULT tag=%lu err=%d text=%s\n",
595                                 op->o_log_prefix, rs->sr_tag, rs->sr_err,
596                                 rs->sr_text ? rs->sr_text : "", 0 );
597                 }
598         }
599
600         if( tmp != NULL ) ch_free(tmp);
601         rs->sr_text = otext;
602         rs->sr_ref = oref;
603 }
604
605 void
606 send_ldap_sasl( Operation *op, SlapReply *rs )
607 {
608         rs->sr_type = REP_SASL;
609         Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
610                 rs->sr_err,
611                 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
612
613         rs->sr_tag = req2res( op->o_tag );
614         rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
615
616         send_ldap_response( op, rs );
617 }
618
619 void
620 slap_send_ldap_extended( Operation *op, SlapReply *rs )
621 {
622         rs->sr_type = REP_EXTENDED;
623
624         Debug( LDAP_DEBUG_TRACE,
625                 "send_ldap_extended: err=%d oid=%s len=%ld\n",
626                 rs->sr_err,
627                 rs->sr_rspoid ? rs->sr_rspoid : "",
628                 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
629
630         rs->sr_tag = req2res( op->o_tag );
631         rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
632
633         send_ldap_response( op, rs );
634 }
635
636 void
637 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
638 {
639         rs->sr_type = REP_INTERMEDIATE;
640         Debug( LDAP_DEBUG_TRACE,
641                 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
642                 rs->sr_err,
643                 rs->sr_rspoid ? rs->sr_rspoid : "",
644                 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
645         rs->sr_tag = LDAP_RES_INTERMEDIATE;
646         rs->sr_msgid = op->o_msgid;
647         send_ldap_response( op, rs );
648 }
649
650 int
651 slap_send_search_entry( Operation *op, SlapReply *rs )
652 {
653         BerElementBuffer berbuf;
654         BerElement      *ber = (BerElement *) &berbuf;
655         Attribute       *a;
656         int             i, j, rc=-1, bytes;
657         char            *edn;
658         int             userattrs;
659         AccessControlState acl_state = ACL_STATE_INIT;
660         int                      attrsonly;
661         AttributeDescription *ad_entry = slap_schema.si_ad_entry;
662
663         /* a_flags: array of flags telling if the i-th element will be
664          *          returned or filtered out
665          * e_flags: array of a_flags
666          */
667         char **e_flags = NULL;
668         
669         rs->sr_type = REP_SEARCH;
670
671         /* eventually will loop through generated operational attribute types
672          * currently implemented types include:
673          *      entryDN, subschemaSubentry, and hasSubordinates */
674         /* NOTE: moved before overlays callback circling because
675          * they may modify entry and other stuff in rs */
676         /* check for special all operational attributes ("+") type */
677         /* FIXME: maybe we could se this flag at the operation level;
678          * however, in principle the caller of send_search_entry() may
679          * change the attribute list at each call */
680         rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
681
682         rc = backend_operational( op, rs );
683         if ( rc ) {
684                 goto error_return;
685         }
686
687         if ( op->o_callback ) {
688                 int             first = 1;
689                 slap_callback   *sc = op->o_callback,
690                                 *sc_next = op->o_callback;
691
692                 rc = SLAP_CB_CONTINUE;
693                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next )
694                 {
695                         sc_next = op->o_callback->sc_next;
696                         if ( op->o_callback->sc_response ) {
697                                 rc = op->o_callback->sc_response( op, rs );
698                                 if ( first && op->o_callback == NULL ) {
699                                         sc = NULL;
700                                 }
701                                 if ( rc != SLAP_CB_CONTINUE ) break;
702                         }
703                         first = 0;
704                 }
705
706                 op->o_callback = sc;
707                 if ( rc != SLAP_CB_CONTINUE ) goto error_return;
708         }
709
710         Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
711                 op->o_connid, rs->sr_entry->e_name.bv_val,
712                 op->ors_attrsonly ? " (attrsOnly)" : "" );
713
714         attrsonly = op->ors_attrsonly;
715
716         if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
717                 Debug( LDAP_DEBUG_ACL,
718                         "send_search_entry: conn %lu access to entry (%s) not allowed\n", 
719                         op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
720
721                 rc = 1;
722                 goto error_return;
723         }
724
725         edn = rs->sr_entry->e_nname.bv_val;
726
727         if ( op->o_res_ber ) {
728                 /* read back control or LDAP_CONNECTIONLESS */
729             ber = op->o_res_ber;
730         } else {
731                 struct berval   bv;
732
733                 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
734                 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
735
736                 ber_init2( ber, &bv, LBER_USE_DER );
737                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
738         }
739
740 #ifdef LDAP_CONNECTIONLESS
741         if ( op->o_conn && op->o_conn->c_is_udp ) {
742                 /* CONNECTIONLESS */
743                 if ( op->o_protocol == LDAP_VERSION2 ) {
744                 rc = ber_printf(ber, "t{O{" /*}}*/,
745                                 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
746                 } else {
747                 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
748                                 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
749                 }
750         } else
751 #endif
752         if ( op->o_res_ber ) {
753                 /* read back control */
754             rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
755         } else {
756             rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
757                         LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
758         }
759
760         if ( rc == -1 ) {
761                 Debug( LDAP_DEBUG_ANY, 
762                         "send_search_entry: conn %lu  ber_printf failed\n", 
763                         op->o_connid, 0, 0 );
764
765                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
766                 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
767                 goto error_return;
768         }
769
770         /* check for special all user attributes ("*") type */
771         userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
772
773         /* create an array of arrays of flags. Each flag corresponds
774          * to particular value of attribute and equals 1 if value matches
775          * to ValuesReturnFilter or 0 if not
776          */     
777         if ( op->o_vrFilter != NULL ) {
778                 int     k = 0;
779                 size_t  size;
780
781                 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
782                         for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
783                 }
784
785                 size = i * sizeof(char *) + k;
786                 if ( size > 0 ) {
787                         char    *a_flags;
788                         e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
789                         if( e_flags == NULL ) {
790                         Debug( LDAP_DEBUG_ANY, 
791                                         "send_search_entry: conn %lu slap_sl_calloc failed\n",
792                                         op->o_connid ? op->o_connid : 0, 0, 0 );
793                                 ber_free( ber, 1 );
794         
795                                 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
796                                 goto error_return;
797                         }
798                         a_flags = (char *)(e_flags + i);
799                         memset( a_flags, 0, k );
800                         for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
801                                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
802                                 e_flags[i] = a_flags;
803                                 a_flags += j;
804                         }
805         
806                         rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ; 
807                         if ( rc == -1 ) {
808                                 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
809                                         "conn %lu matched values filtering failed\n",
810                                         op->o_connid ? op->o_connid : 0, 0, 0 );
811                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
812                                 send_ldap_error( op, rs, LDAP_OTHER,
813                                         "matched values filtering error" );
814                                 goto error_return;
815                         }
816                 }
817         }
818
819         for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
820                 AttributeDescription *desc = a->a_desc;
821                 int finish = 0;
822
823                 if ( rs->sr_attrs == NULL ) {
824                         /* all attrs request, skip operational attributes */
825                         if( is_at_operational( desc->ad_type ) ) {
826                                 continue;
827                         }
828
829                 } else {
830                         /* specific attrs requested */
831                         if ( is_at_operational( desc->ad_type ) ) {
832                                 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
833                                                 !ad_inlist( desc, rs->sr_attrs ) )
834                                 {
835                                         continue;
836                                 }
837
838                         } else {
839                                 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) )
840                                 {
841                                         continue;
842                                 }
843                         }
844                 }
845
846                 if ( attrsonly ) {
847                         if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
848                                 ACL_READ, &acl_state ) )
849                         {
850                                 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
851                                         "conn %lu access to attribute %s not allowed\n",
852                                         op->o_connid, desc->ad_cname.bv_val, 0 );
853                                 continue;
854                         }
855
856                         if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
857                                 Debug( LDAP_DEBUG_ANY, 
858                                         "send_search_entry: conn %lu  ber_printf failed\n", 
859                                         op->o_connid, 0, 0 );
860
861                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
862                                 send_ldap_error( op, rs, LDAP_OTHER,
863                                         "encoding description error");
864                                 goto error_return;
865                         }
866                         finish = 1;
867
868                 } else {
869                         int first = 1;
870                         for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
871                                 if ( ! access_allowed( op, rs->sr_entry,
872                                         desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
873                                 {
874                                         Debug( LDAP_DEBUG_ACL,
875                                                 "send_search_entry: conn %lu "
876                                                 "access to attribute %s, value #%d not allowed\n",
877                                                 op->o_connid, desc->ad_cname.bv_val, i );
878
879                                         continue;
880                                 }
881
882                                 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
883                                         continue;
884                                 }
885
886                                 if ( first ) {
887                                         first = 0;
888                                         finish = 1;
889                                         if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
890                                                 Debug( LDAP_DEBUG_ANY,
891                                                         "send_search_entry: conn %lu  ber_printf failed\n", 
892                                                         op->o_connid, 0, 0 );
893
894                                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
895                                                 send_ldap_error( op, rs, LDAP_OTHER,
896                                                         "encoding description error");
897                                                 goto error_return;
898                                         }
899                                 }
900                                 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
901                                         Debug( LDAP_DEBUG_ANY,
902                                                 "send_search_entry: conn %lu  "
903                                                 "ber_printf failed.\n", op->o_connid, 0, 0 );
904
905                                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
906                                         send_ldap_error( op, rs, LDAP_OTHER,
907                                                 "encoding values error" );
908                                         goto error_return;
909                                 }
910                         }
911                 }
912
913                 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
914                         Debug( LDAP_DEBUG_ANY,
915                                 "send_search_entry: conn %lu ber_printf failed\n", 
916                                 op->o_connid, 0, 0 );
917
918                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
919                         send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
920                         goto error_return;
921                 }
922         }
923
924         /* NOTE: moved before overlays callback circling because
925          * they may modify entry and other stuff in rs */
926         if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
927                 int     k = 0;
928                 size_t  size;
929
930                 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
931                         for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
932                 }
933
934                 size = i * sizeof(char *) + k;
935                 if ( size > 0 ) {
936                         char    *a_flags, **tmp;
937                 
938                         /*
939                          * Reuse previous memory - we likely need less space
940                          * for operational attributes
941                          */
942                         tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
943                                 op->o_tmpmemctx );
944                         if ( tmp == NULL ) {
945                                 Debug( LDAP_DEBUG_ANY,
946                                         "send_search_entry: conn %lu "
947                                         "not enough memory "
948                                         "for matched values filtering\n",
949                                         op->o_connid, 0, 0 );
950                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
951                                 send_ldap_error( op, rs, LDAP_OTHER,
952                                         "not enough memory for matched values filtering" );
953                                 goto error_return;
954                         }
955                         e_flags = tmp;
956                         a_flags = (char *)(e_flags + i);
957                         memset( a_flags, 0, k );
958                         for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
959                                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
960                                 e_flags[i] = a_flags;
961                                 a_flags += j;
962                         }
963                         rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ; 
964                     
965                         if ( rc == -1 ) {
966                                 Debug( LDAP_DEBUG_ANY,
967                                         "send_search_entry: conn %lu "
968                                         "matched values filtering failed\n", 
969                                         op->o_connid ? op->o_connid : 0, 0, 0);
970                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
971                                 send_ldap_error( op, rs, LDAP_OTHER,
972                                         "matched values filtering error" );
973                                 goto error_return;
974                         }
975                 }
976         }
977
978         for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
979                 AttributeDescription *desc = a->a_desc;
980
981                 if ( rs->sr_attrs == NULL ) {
982                         /* all attrs request, skip operational attributes */
983                         if( is_at_operational( desc->ad_type ) ) {
984                                 continue;
985                         }
986
987                 } else {
988                         /* specific attrs requested */
989                         if( is_at_operational( desc->ad_type ) ) {
990                                 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) && 
991                                                 !ad_inlist( desc, rs->sr_attrs ) )
992                                 {
993                                         continue;
994                                 }
995                         } else {
996                                 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
997                                         continue;
998                                 }
999                         }
1000                 }
1001
1002                 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1003                         ACL_READ, &acl_state ) )
1004                 {
1005                         Debug( LDAP_DEBUG_ACL,
1006                                 "send_search_entry: conn %lu "
1007                                 "access to attribute %s not allowed\n",
1008                                 op->o_connid, desc->ad_cname.bv_val, 0 );
1009
1010                         continue;
1011                 }
1012
1013                 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1014                 if ( rc == -1 ) {
1015                         Debug( LDAP_DEBUG_ANY,
1016                                 "send_search_entry: conn %lu  "
1017                                 "ber_printf failed\n", op->o_connid, 0, 0 );
1018
1019                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1020                         send_ldap_error( op, rs, LDAP_OTHER,
1021                                 "encoding description error" );
1022                         goto error_return;
1023                 }
1024
1025                 if ( ! attrsonly ) {
1026                         for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1027                                 if ( ! access_allowed( op, rs->sr_entry,
1028                                         desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1029                                 {
1030                                         Debug( LDAP_DEBUG_ACL,
1031                                                 "send_search_entry: conn %lu "
1032                                                 "access to %s, value %d not allowed\n",
1033                                                 op->o_connid, desc->ad_cname.bv_val, i );
1034
1035                                         continue;
1036                                 }
1037
1038                                 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1039                                         continue;
1040                                 }
1041
1042                                 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1043                                         Debug( LDAP_DEBUG_ANY,
1044                                                 "send_search_entry: conn %lu  ber_printf failed\n", 
1045                                                 op->o_connid, 0, 0 );
1046
1047                                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1048                                         send_ldap_error( op, rs, LDAP_OTHER,
1049                                                 "encoding values error" );
1050                                         goto error_return;
1051                                 }
1052                         }
1053                 }
1054
1055                 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1056                         Debug( LDAP_DEBUG_ANY,
1057                                 "send_search_entry: conn %lu  ber_printf failed\n",
1058                                 op->o_connid, 0, 0 );
1059
1060                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1061                         send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1062                         goto error_return;
1063                 }
1064         }
1065
1066         /* free e_flags */
1067         if ( e_flags ) {
1068                 slap_sl_free( e_flags, op->o_tmpmemctx );
1069                 e_flags = NULL;
1070         }
1071
1072         rc = ber_printf( ber, /*{{*/ "}N}" );
1073
1074         if( rc != -1 ) {
1075                 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1076         }
1077
1078         if( rc != -1 ) {
1079 #ifdef LDAP_CONNECTIONLESS
1080                 if( op->o_conn && op->o_conn->c_is_udp ) {
1081                         if ( op->o_protocol != LDAP_VERSION2 ) {
1082                                 rc = ber_printf( ber, /*{*/ "N}" );
1083                         }
1084                 } else
1085 #endif
1086                 if ( op->o_res_ber == NULL ) {
1087                         rc = ber_printf( ber, /*{*/ "N}" );
1088                 }
1089         }
1090
1091         if ( rc == -1 ) {
1092                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1093
1094                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1095                 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1096                 rc = 1;
1097                 goto error_return;
1098         }
1099
1100         if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1101                 be_entry_release_rw( op, rs->sr_entry, 0 );
1102                 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1103                 rs->sr_entry = NULL;
1104         }
1105
1106         if ( op->o_res_ber == NULL ) {
1107                 bytes = send_ldap_ber( op->o_conn, ber );
1108                 ber_free_buf( ber );
1109
1110                 if ( bytes < 0 ) {
1111                         Debug( LDAP_DEBUG_ANY,
1112                                 "send_search_entry: conn %lu  ber write failed.\n", 
1113                                 op->o_connid, 0, 0 );
1114
1115                         rc = -1;
1116                         goto error_return;
1117                 }
1118                 rs->sr_nentries++;
1119
1120                 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1121                 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1122                 ldap_pvt_mp_add_ulong( slap_counters.sc_entries, 1 );
1123                 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1124                 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1125         }
1126
1127         Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1128             op->o_log_prefix, edn, 0, 0, 0 );
1129
1130         Debug( LDAP_DEBUG_TRACE,
1131                 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1132
1133         rc = 0;
1134
1135 error_return:;
1136         if ( op->o_callback ) {
1137                 int             first = 1;
1138                 slap_callback   *sc = op->o_callback, *sc_next;
1139
1140                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1141                         sc_next = op->o_callback->sc_next;
1142                         if ( op->o_callback->sc_cleanup ) {
1143                                 (void)op->o_callback->sc_cleanup( op, rs );
1144                                 if ( first && op->o_callback != sc ) {
1145                                         sc = op->o_callback;
1146                                 }
1147                         }
1148                         first = 0;
1149                 }
1150                 op->o_callback = sc;
1151         }
1152
1153         if ( e_flags ) {
1154                 slap_sl_free( e_flags, op->o_tmpmemctx );
1155         }
1156
1157         if ( rs->sr_operational_attrs ) {
1158                 attrs_free( rs->sr_operational_attrs );
1159                 rs->sr_operational_attrs = NULL;
1160         }
1161         rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1162
1163         /* FIXME: I think rs->sr_type should be explicitly set to
1164          * REP_SEARCH here. That's what it was when we entered this
1165          * function. send_ldap_error may have changed it, but we
1166          * should set it back so that the cleanup functions know
1167          * what they're doing.
1168          */
1169         if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH 
1170                 && rs->sr_entry 
1171                 && ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) ) 
1172         {
1173                 entry_free( rs->sr_entry );
1174                 rs->sr_entry = NULL;
1175                 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1176         }
1177
1178         return( rc );
1179 }
1180
1181 int
1182 slap_send_search_reference( Operation *op, SlapReply *rs )
1183 {
1184         BerElementBuffer berbuf;
1185         BerElement      *ber = (BerElement *) &berbuf;
1186         int rc = 0;
1187         int bytes;
1188
1189         AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1190         AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1191
1192         rs->sr_type = REP_SEARCHREF;
1193         if ( op->o_callback ) {
1194                 int             first = 1;
1195                 slap_callback   *sc = op->o_callback,
1196                                 *sc_next = op->o_callback;
1197
1198                 rc = SLAP_CB_CONTINUE;
1199                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1200                         sc_next = op->o_callback->sc_next;
1201                         if ( op->o_callback->sc_response ) {
1202                                 rc = op->o_callback->sc_response( op, rs );
1203                                 if ( first && op->o_callback == NULL ) {
1204                                         sc = NULL;
1205                                 }
1206                                 if ( rc != SLAP_CB_CONTINUE ) break;
1207                         }
1208                         first = 0;
1209                 }
1210
1211                 op->o_callback = sc;
1212                 if ( rc != SLAP_CB_CONTINUE ) goto rel;
1213         }
1214
1215         Debug( LDAP_DEBUG_TRACE,
1216                 "=> send_search_reference: dn=\"%s\"\n",
1217                 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1218
1219         if (  rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1220                 ad_entry, NULL, ACL_READ, NULL ) )
1221         {
1222                 Debug( LDAP_DEBUG_ACL,
1223                         "send_search_reference: access to entry not allowed\n",
1224                     0, 0, 0 );
1225                 rc = 1;
1226                 goto rel;
1227         }
1228
1229         if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1230                 ad_ref, NULL, ACL_READ, NULL ) )
1231         {
1232                 Debug( LDAP_DEBUG_ACL,
1233                         "send_search_reference: access "
1234                         "to reference not allowed\n",
1235                     0, 0, 0 );
1236                 rc = 1;
1237                 goto rel;
1238         }
1239
1240 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1241         if( op->o_domain_scope ) {
1242                 Debug( LDAP_DEBUG_ANY,
1243                         "send_search_reference: domainScope control in (%s)\n", 
1244                         rs->sr_entry->e_dn, 0, 0 );
1245                 rc = 0;
1246                 goto rel;
1247         }
1248 #endif
1249
1250         if( rs->sr_ref == NULL ) {
1251                 Debug( LDAP_DEBUG_ANY,
1252                         "send_search_reference: null ref in (%s)\n", 
1253                         rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1254                 rc = 1;
1255                 goto rel;
1256         }
1257
1258         if( op->o_protocol < LDAP_VERSION3 ) {
1259                 rc = 0;
1260                 /* save the references for the result */
1261                 if( rs->sr_ref[0].bv_val != NULL ) {
1262                         if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1263                                 rc = LDAP_OTHER;
1264                 }
1265                 goto rel;
1266         }
1267
1268 #ifdef LDAP_CONNECTIONLESS
1269         if( op->o_conn && op->o_conn->c_is_udp ) {
1270                 ber = op->o_res_ber;
1271         } else
1272 #endif
1273         {
1274                 ber_init_w_nullc( ber, LBER_USE_DER );
1275                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1276         }
1277
1278         rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1279                 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1280
1281         if( rc != -1 ) {
1282                 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1283         }
1284
1285         if( rc != -1 ) {
1286                 rc = ber_printf( ber, /*"{"*/ "N}" );
1287         }
1288
1289         if ( rc == -1 ) {
1290                 Debug( LDAP_DEBUG_ANY,
1291                         "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1292
1293 #ifdef LDAP_CONNECTIONLESS
1294                 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1295 #endif
1296                 ber_free_buf( ber );
1297                 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1298                 goto rel;
1299         }
1300
1301         rc = 0;
1302         if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1303                 be_entry_release_rw( op, rs->sr_entry, 0 );
1304                 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1305                 rs->sr_entry = NULL;
1306         }
1307
1308 #ifdef LDAP_CONNECTIONLESS
1309         if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1310 #endif
1311         bytes = send_ldap_ber( op->o_conn, ber );
1312         ber_free_buf( ber );
1313
1314         ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1315         ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1316         ldap_pvt_mp_add_ulong( slap_counters.sc_refs, 1 );
1317         ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1318         ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1319 #ifdef LDAP_CONNECTIONLESS
1320         }
1321 #endif
1322
1323         Statslog( LDAP_DEBUG_STATS2, "%s REF dn=\"%s\"\n",
1324                 op->o_log_prefix, rs->sr_entry ? rs->sr_entry->e_dn : "(null)",
1325                 0, 0, 0 );
1326
1327         Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1328
1329 rel:
1330         if ( op->o_callback ) {
1331                 int             first = 1;
1332                 slap_callback   *sc = op->o_callback, *sc_next;
1333
1334                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1335                         sc_next = op->o_callback->sc_next;
1336                         if ( op->o_callback->sc_cleanup ) {
1337                                 (void)op->o_callback->sc_cleanup( op, rs );
1338                                 if ( first && op->o_callback != sc ) {
1339                                         sc = op->o_callback;
1340                                 }
1341                         }
1342                         first = 0;
1343                 }
1344                 op->o_callback = sc;
1345         }
1346
1347         return rc;
1348 }
1349
1350 int
1351 str2result(
1352     char        *s,
1353     int         *code,
1354     char        **matched,
1355     char        **info )
1356 {
1357         int     rc;
1358         char    *c;
1359
1360         *code = LDAP_SUCCESS;
1361         *matched = NULL;
1362         *info = NULL;
1363
1364         if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1365                 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1366                     s, 0, 0 );
1367
1368                 return( -1 );
1369         }
1370
1371         rc = 0;
1372         while ( (s = strchr( s, '\n' )) != NULL ) {
1373                 *s++ = '\0';
1374                 if ( *s == '\0' ) {
1375                         break;
1376                 }
1377                 if ( (c = strchr( s, ':' )) != NULL ) {
1378                         c++;
1379                 }
1380
1381                 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1382                         if ( c != NULL ) {
1383                                 *code = atoi( c );
1384                         }
1385                 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1386                         if ( c != NULL ) {
1387                                 *matched = c;
1388                         }
1389                 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1390                         if ( c != NULL ) {
1391                                 *info = c;
1392                         }
1393                 } else {
1394                         Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1395                             s, 0, 0 );
1396
1397                         rc = -1;
1398                 }
1399         }
1400
1401         return( rc );
1402 }
1403
1404 int slap_read_controls(
1405         Operation *op,
1406         SlapReply *rs,
1407         Entry *e,
1408         const struct berval *oid,
1409         LDAPControl **ctrl )
1410 {
1411         int rc;
1412         struct berval bv;
1413         BerElementBuffer berbuf;
1414         BerElement *ber = (BerElement *) &berbuf;
1415         LDAPControl c;
1416         Operation myop;
1417
1418         Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1419                 oid->bv_val, e->e_dn, 0 );
1420
1421         rs->sr_entry = e;
1422         rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1423                 op->o_preread_attrs : op->o_postread_attrs; 
1424
1425         bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1426         bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1427
1428         ber_init2( ber, &bv, LBER_USE_DER );
1429         ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1430
1431         /* create new operation */
1432         myop = *op;
1433         myop.o_bd = NULL;
1434         myop.o_res_ber = ber;
1435         myop.o_callback = NULL;
1436
1437         rc = slap_send_search_entry( &myop, rs );
1438         if( rc ) return rc;
1439
1440         rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1441
1442         if( rc == LBER_ERROR ) return LDAP_OTHER;
1443
1444         c.ldctl_oid = oid->bv_val;
1445         c.ldctl_iscritical = 0;
1446
1447         if ( *ctrl == NULL ) {
1448                 /* first try */
1449                 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1450         } else {
1451                 /* retry: free previous try */
1452                 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1453         }
1454
1455         **ctrl = c;
1456         return LDAP_SUCCESS;
1457 }
1458
1459 /* Map API errors to protocol errors... */
1460 int
1461 slap_map_api2result( SlapReply *rs )
1462 {
1463         switch(rs->sr_err) {
1464         case LDAP_SERVER_DOWN:
1465                 return LDAP_UNAVAILABLE;
1466         case LDAP_LOCAL_ERROR:
1467                 return LDAP_OTHER;
1468         case LDAP_ENCODING_ERROR:
1469         case LDAP_DECODING_ERROR:
1470                 return LDAP_PROTOCOL_ERROR;
1471         case LDAP_TIMEOUT:
1472                 return LDAP_UNAVAILABLE;
1473         case LDAP_AUTH_UNKNOWN:
1474                 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1475         case LDAP_FILTER_ERROR:
1476                 rs->sr_text = "Filter error";
1477                 return LDAP_OTHER;
1478         case LDAP_USER_CANCELLED:
1479                 rs->sr_text = "User cancelled";
1480                 return LDAP_OTHER;
1481         case LDAP_PARAM_ERROR:
1482                 return LDAP_PROTOCOL_ERROR;
1483         case LDAP_NO_MEMORY:
1484                 return LDAP_OTHER;
1485         case LDAP_CONNECT_ERROR:
1486                 return LDAP_UNAVAILABLE;
1487         case LDAP_NOT_SUPPORTED:
1488                 return LDAP_UNWILLING_TO_PERFORM;
1489         case LDAP_CONTROL_NOT_FOUND:
1490                 return LDAP_PROTOCOL_ERROR;
1491         case LDAP_NO_RESULTS_RETURNED:
1492                 return LDAP_NO_SUCH_OBJECT;
1493         case LDAP_MORE_RESULTS_TO_RETURN:
1494                 rs->sr_text = "More results to return";
1495                 return LDAP_OTHER;
1496         case LDAP_CLIENT_LOOP:
1497         case LDAP_REFERRAL_LIMIT_EXCEEDED:
1498                 return LDAP_LOOP_DETECT;
1499         default:
1500                 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1501                 return rs->sr_err;
1502         }
1503 }
1504
1505
1506 slap_mask_t
1507 slap_attr_flags( AttributeName *an )
1508 {
1509         slap_mask_t     flags = SLAP_ATTRS_UNDEFINED;
1510
1511         if ( an == NULL ) {
1512                 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1513
1514         } else {
1515                 flags |= an_find( an, &AllOper )
1516                         ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1517                 flags |= an_find( an, &AllUser )
1518                         ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;
1519         }
1520
1521         return flags;
1522 }
1523