]> git.sur5r.net Git - openldap/blob - servers/slapd/result.c
use lutil_ato*() whenever appropriate
[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 #include "lutil.h"
40
41 const struct berval slap_dummy_bv = BER_BVNULL;
42
43 int slap_null_cb( Operation *op, SlapReply *rs )
44 {
45         return 0;
46 }
47
48 int slap_freeself_cb( Operation *op, SlapReply *rs )
49 {
50         assert( op->o_callback != NULL );
51
52         op->o_tmpfree( op->o_callback, op->o_tmpmemctx );
53         op->o_callback = NULL;
54
55         return SLAP_CB_CONTINUE;
56 }
57
58 int slap_replog_cb( Operation *op, SlapReply *rs )
59 {
60         if ( rs->sr_err == LDAP_SUCCESS ) {
61                 replog( op );
62         }
63         return SLAP_CB_CONTINUE;
64 }
65
66 static char *v2ref( BerVarray ref, const char *text )
67 {
68         size_t len = 0, i = 0;
69         char *v2;
70
71         if(ref == NULL) {
72                 if (text) {
73                         return ch_strdup(text);
74                 } else {
75                         return NULL;
76                 }
77         }
78         
79         if ( text != NULL ) {
80                 len = strlen( text );
81                 if (text[len-1] != '\n') {
82                     i = 1;
83                 }
84         }
85
86         v2 = SLAP_MALLOC( len+i+sizeof("Referral:") );
87         if( v2 == NULL ) {
88                 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
89                 return NULL;
90         }
91
92         if( text != NULL ) {
93                 strcpy(v2, text);
94                 if( i ) {
95                         v2[len++] = '\n';
96                 }
97         }
98         strcpy( v2+len, "Referral:" );
99         len += sizeof("Referral:");
100
101         for( i=0; ref[i].bv_val != NULL; i++ ) {
102                 v2 = SLAP_REALLOC( v2, len + ref[i].bv_len + 1 );
103                 if( v2 == NULL ) {
104                         Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
105                         return NULL;
106                 }
107                 v2[len-1] = '\n';
108                 AC_MEMCPY(&v2[len], ref[i].bv_val, ref[i].bv_len );
109                 len += ref[i].bv_len;
110                 if (ref[i].bv_val[ref[i].bv_len-1] != '/') {
111                         ++len;
112                 }
113         }
114
115         v2[len-1] = '\0';
116         return v2;
117 }
118
119 ber_tag_t
120 slap_req2res( ber_tag_t tag )
121 {
122         switch( tag ) {
123         case LDAP_REQ_ADD:
124         case LDAP_REQ_BIND:
125         case LDAP_REQ_COMPARE:
126         case LDAP_REQ_EXTENDED:
127         case LDAP_REQ_MODIFY:
128         case LDAP_REQ_MODRDN:
129                 tag++;
130                 break;
131
132         case LDAP_REQ_DELETE:
133                 tag = LDAP_RES_DELETE;
134                 break;
135
136         case LDAP_REQ_ABANDON:
137         case LDAP_REQ_UNBIND:
138                 tag = LBER_SEQUENCE;
139                 break;
140
141         case LDAP_REQ_SEARCH:
142                 tag = LDAP_RES_SEARCH_RESULT;
143                 break;
144
145         default:
146                 tag = LBER_SEQUENCE;
147         }
148
149         return tag;
150 }
151
152 static long send_ldap_ber(
153         Connection *conn,
154         BerElement *ber )
155 {
156         ber_len_t bytes;
157
158         ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
159
160         /* write only one pdu at a time - wait til it's our turn */
161         ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
162
163         /* lock the connection */ 
164         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
165
166         /* write the pdu */
167         while( 1 ) {
168                 int err;
169                 ber_socket_t    sd;
170
171                 if ( connection_state_closing( conn ) ) {
172                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
173                         ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
174
175                         return 0;
176                 }
177
178                 if ( ber_flush( conn->c_sb, ber, 0 ) == 0 ) {
179                         break;
180                 }
181
182                 err = errno;
183
184                 /*
185                  * we got an error.  if it's ewouldblock, we need to
186                  * wait on the socket being writable.  otherwise, figure
187                  * it's a hard error and return.
188                  */
189
190                 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
191                     err, sock_errstr(err), 0 );
192
193                 if ( err != EWOULDBLOCK && err != EAGAIN ) {
194                         connection_closing( conn, "connection lost on write" );
195
196                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
197                         ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
198
199                         return( -1 );
200                 }
201
202                 /* wait for socket to be write-ready */
203                 conn->c_writewaiter = 1;
204                 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
205                 slapd_set_write( sd, 1 );
206
207                 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
208                 conn->c_writewaiter = 0;
209         }
210
211         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
212         ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
213
214         return bytes;
215 }
216
217 static int
218 send_ldap_control( BerElement *ber, LDAPControl *c )
219 {
220         int rc;
221
222         assert( c != NULL );
223
224         rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
225
226         if( c->ldctl_iscritical ) {
227                 rc = ber_printf( ber, "b",
228                         (ber_int_t) c->ldctl_iscritical ) ;
229                 if( rc == -1 ) return rc;
230         }
231
232         if( c->ldctl_value.bv_val != NULL ) {
233                 rc = ber_printf( ber, "O", &c->ldctl_value ); 
234                 if( rc == -1 ) return rc;
235         }
236
237         rc = ber_printf( ber, /*{*/"N}" );
238         if( rc == -1 ) return rc;
239
240         return 0;
241 }
242
243 static int
244 send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
245 {
246         int rc;
247
248         if( c == NULL )
249                 return 0;
250
251         rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
252         if( rc == -1 ) return rc;
253
254         for( ; *c != NULL; c++) {
255                 rc = send_ldap_control( ber, *c );
256                 if( rc == -1 ) return rc;
257         }
258
259 #ifdef LDAP_DEVEL
260         /* this is a hack to avoid having to modify op->s_ctrls */
261         if( o->o_sortedresults ) {
262                 BerElementBuffer berbuf;
263                 BerElement *sber = (BerElement *) &berbuf;
264                 LDAPControl sorted;
265                 BER_BVZERO( &sorted.ldctl_value );
266                 sorted.ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
267                 sorted.ldctl_iscritical = 0;
268
269                 ber_init2( sber, NULL, LBER_USE_DER );
270
271                 ber_printf( sber, "{e}", LDAP_UNWILLING_TO_PERFORM );
272
273                 if( ber_flatten2( ber, &sorted.ldctl_value, 0 ) == -1 ) {
274                         return -1;
275                 }
276
277                 (void) ber_free_buf( ber );
278
279                 rc = send_ldap_control( ber, &sorted );
280                 if( rc == -1 ) return rc;
281         }
282 #endif
283
284         rc = ber_printf( ber, /*{*/"N}" );
285
286         return rc;
287 }
288
289 static int
290 send_ldap_response(
291         Operation *op,
292         SlapReply *rs )
293 {
294         BerElementBuffer berbuf;
295         BerElement      *ber = (BerElement *) &berbuf;
296         int             rc = LDAP_SUCCESS;
297         long    bytes;
298
299         if ( rs->sr_err == SLAPD_ABANDON ) {
300                 rc = SLAPD_ABANDON;
301                 goto clean2;
302         }
303
304         if ( op->o_callback ) {
305                 slap_callback   *sc = op->o_callback, **sc_prev = &sc, *sc_next;
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 ( op->o_callback != *sc_prev ) {
313                                         *sc_prev = op->o_callback;
314                                 }
315                                 if ( rc != SLAP_CB_CONTINUE || !op->o_callback ) break;
316                         }
317                         sc_prev = &op->o_callback->sc_next;
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                 slap_callback   *sc = op->o_callback, **sc_prev = &sc, *sc_next;
460
461                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
462                         sc_next = op->o_callback->sc_next;
463                         if ( op->o_callback->sc_cleanup ) {
464                                 (void)op->o_callback->sc_cleanup( op, rs );
465                                 if ( op->o_callback != *sc_prev ) {
466                                         *sc_prev = op->o_callback;
467                                 }
468                                 if ( !op->o_callback ) break;
469                         }
470                         sc_prev = &op->o_callback->sc_next;
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 = slap_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 = slap_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 = slap_req2res( op->o_tag );
614         rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
615
616         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
617                 Statslog( LDAP_DEBUG_STATS,
618                         "%s RESULT tag=%lu err=%d text=%s\n",
619                         op->o_log_prefix, rs->sr_tag, rs->sr_err,
620                         rs->sr_text ? rs->sr_text : "", 0 );
621         }
622 }
623
624 void
625 slap_send_ldap_extended( Operation *op, SlapReply *rs )
626 {
627         rs->sr_type = REP_EXTENDED;
628
629         Debug( LDAP_DEBUG_TRACE,
630                 "send_ldap_extended: err=%d oid=%s len=%ld\n",
631                 rs->sr_err,
632                 rs->sr_rspoid ? rs->sr_rspoid : "",
633                 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
634
635         rs->sr_tag = slap_req2res( op->o_tag );
636         rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
637
638         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
639                 Statslog( LDAP_DEBUG_STATS,
640                         "%s RESULT oid=%s err=%d text=%s\n",
641                         op->o_log_prefix, rs->sr_rspoid ? rs->sr_rspoid : "",
642                         rs->sr_err, rs->sr_text ? rs->sr_text : "", 0 );
643         }
644 }
645
646 void
647 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
648 {
649         rs->sr_type = REP_INTERMEDIATE;
650         Debug( LDAP_DEBUG_TRACE,
651                 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
652                 rs->sr_err,
653                 rs->sr_rspoid ? rs->sr_rspoid : "",
654                 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
655         rs->sr_tag = LDAP_RES_INTERMEDIATE;
656         rs->sr_msgid = op->o_msgid;
657         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
658                 Statslog( LDAP_DEBUG_STATS2,
659                         "%s INTERM oid=%s\n",
660                         op->o_log_prefix,
661                         rs->sr_rspoid ? rs->sr_rspoid : "", 0, 0, 0 );
662         }
663 }
664
665 int
666 slap_send_search_entry( Operation *op, SlapReply *rs )
667 {
668         BerElementBuffer berbuf;
669         BerElement      *ber = (BerElement *) &berbuf;
670         Attribute       *a;
671         int             i, j, rc=-1, bytes;
672         char            *edn;
673         int             userattrs;
674         AccessControlState acl_state = ACL_STATE_INIT;
675         int                      attrsonly;
676         AttributeDescription *ad_entry = slap_schema.si_ad_entry;
677
678         /* a_flags: array of flags telling if the i-th element will be
679          *          returned or filtered out
680          * e_flags: array of a_flags
681          */
682         char **e_flags = NULL;
683         
684         rs->sr_type = REP_SEARCH;
685
686         /* eventually will loop through generated operational attribute types
687          * currently implemented types include:
688          *      entryDN, subschemaSubentry, and hasSubordinates */
689         /* NOTE: moved before overlays callback circling because
690          * they may modify entry and other stuff in rs */
691         /* check for special all operational attributes ("+") type */
692         /* FIXME: maybe we could se this flag at the operation level;
693          * however, in principle the caller of send_search_entry() may
694          * change the attribute list at each call */
695         rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
696
697         rc = backend_operational( op, rs );
698         if ( rc ) {
699                 goto error_return;
700         }
701
702         if ( op->o_callback ) {
703                 slap_callback   *sc = op->o_callback, **sc_prev = &sc, *sc_next;
704
705                 rc = SLAP_CB_CONTINUE;
706                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next )
707                 {
708                         sc_next = op->o_callback->sc_next;
709                         if ( op->o_callback->sc_response ) {
710                                 rc = op->o_callback->sc_response( op, rs );
711                                 if ( op->o_callback != *sc_prev ) {
712                                         *sc_prev = op->o_callback;
713                                 }
714                                 if ( rc != SLAP_CB_CONTINUE || !op->o_callback ) break;
715                         }
716                         sc_prev = &op->o_callback->sc_next;
717                 }
718
719                 op->o_callback = sc;
720                 if ( rc != SLAP_CB_CONTINUE ) goto error_return;
721         }
722
723         Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
724                 op->o_connid, rs->sr_entry->e_name.bv_val,
725                 op->ors_attrsonly ? " (attrsOnly)" : "" );
726
727         attrsonly = op->ors_attrsonly;
728
729         if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
730                 Debug( LDAP_DEBUG_ACL,
731                         "send_search_entry: conn %lu access to entry (%s) not allowed\n", 
732                         op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
733
734                 rc = 1;
735                 goto error_return;
736         }
737
738         edn = rs->sr_entry->e_nname.bv_val;
739
740         if ( op->o_res_ber ) {
741                 /* read back control or LDAP_CONNECTIONLESS */
742             ber = op->o_res_ber;
743         } else {
744                 struct berval   bv;
745
746                 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
747                 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
748
749                 ber_init2( ber, &bv, LBER_USE_DER );
750                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
751         }
752
753 #ifdef LDAP_CONNECTIONLESS
754         if ( op->o_conn && op->o_conn->c_is_udp ) {
755                 /* CONNECTIONLESS */
756                 if ( op->o_protocol == LDAP_VERSION2 ) {
757                 rc = ber_printf(ber, "t{O{" /*}}*/,
758                                 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
759                 } else {
760                 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
761                                 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
762                 }
763         } else
764 #endif
765         if ( op->o_res_ber ) {
766                 /* read back control */
767             rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
768         } else {
769             rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
770                         LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
771         }
772
773         if ( rc == -1 ) {
774                 Debug( LDAP_DEBUG_ANY, 
775                         "send_search_entry: conn %lu  ber_printf failed\n", 
776                         op->o_connid, 0, 0 );
777
778                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
779                 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
780                 goto error_return;
781         }
782
783         /* check for special all user attributes ("*") type */
784         userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
785
786         /* create an array of arrays of flags. Each flag corresponds
787          * to particular value of attribute and equals 1 if value matches
788          * to ValuesReturnFilter or 0 if not
789          */     
790         if ( op->o_vrFilter != NULL ) {
791                 int     k = 0;
792                 size_t  size;
793
794                 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
795                         for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
796                 }
797
798                 size = i * sizeof(char *) + k;
799                 if ( size > 0 ) {
800                         char    *a_flags;
801                         e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
802                         if( e_flags == NULL ) {
803                         Debug( LDAP_DEBUG_ANY, 
804                                         "send_search_entry: conn %lu slap_sl_calloc failed\n",
805                                         op->o_connid ? op->o_connid : 0, 0, 0 );
806                                 ber_free( ber, 1 );
807         
808                                 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
809                                 goto error_return;
810                         }
811                         a_flags = (char *)(e_flags + i);
812                         memset( a_flags, 0, k );
813                         for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
814                                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
815                                 e_flags[i] = a_flags;
816                                 a_flags += j;
817                         }
818         
819                         rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ; 
820                         if ( rc == -1 ) {
821                                 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
822                                         "conn %lu matched values filtering failed\n",
823                                         op->o_connid ? op->o_connid : 0, 0, 0 );
824                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
825                                 send_ldap_error( op, rs, LDAP_OTHER,
826                                         "matched values filtering error" );
827                                 goto error_return;
828                         }
829                 }
830         }
831
832         for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
833                 AttributeDescription *desc = a->a_desc;
834                 int finish = 0;
835
836                 if ( rs->sr_attrs == NULL ) {
837                         /* all user attrs request, skip operational attributes */
838                         if( is_at_operational( desc->ad_type ) ) {
839                                 continue;
840                         }
841
842                 } else {
843                         /* specific attrs requested */
844                         if ( is_at_operational( desc->ad_type ) ) {
845                                 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
846                                         !ad_inlist( desc, rs->sr_attrs ) )
847                                 {
848                                         continue;
849                                 }
850
851                         } else {
852                                 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
853                                         continue;
854                                 }
855                         }
856                 }
857
858                 if ( attrsonly ) {
859                         if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
860                                 ACL_READ, &acl_state ) )
861                         {
862                                 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
863                                         "conn %lu access to attribute %s not allowed\n",
864                                         op->o_connid, desc->ad_cname.bv_val, 0 );
865                                 continue;
866                         }
867
868                         if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
869                                 Debug( LDAP_DEBUG_ANY, 
870                                         "send_search_entry: conn %lu  ber_printf failed\n", 
871                                         op->o_connid, 0, 0 );
872
873                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
874                                 send_ldap_error( op, rs, LDAP_OTHER,
875                                         "encoding description error");
876                                 goto error_return;
877                         }
878                         finish = 1;
879
880                 } else {
881                         int first = 1;
882                         for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
883                                 if ( ! access_allowed( op, rs->sr_entry,
884                                         desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
885                                 {
886                                         Debug( LDAP_DEBUG_ACL,
887                                                 "send_search_entry: conn %lu "
888                                                 "access to attribute %s, value #%d not allowed\n",
889                                                 op->o_connid, desc->ad_cname.bv_val, i );
890
891                                         continue;
892                                 }
893
894                                 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
895                                         continue;
896                                 }
897
898                                 if ( first ) {
899                                         first = 0;
900                                         finish = 1;
901                                         if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
902                                                 Debug( LDAP_DEBUG_ANY,
903                                                         "send_search_entry: conn %lu  ber_printf failed\n", 
904                                                         op->o_connid, 0, 0 );
905
906                                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
907                                                 send_ldap_error( op, rs, LDAP_OTHER,
908                                                         "encoding description error");
909                                                 goto error_return;
910                                         }
911                                 }
912                                 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
913                                         Debug( LDAP_DEBUG_ANY,
914                                                 "send_search_entry: conn %lu  "
915                                                 "ber_printf failed.\n", op->o_connid, 0, 0 );
916
917                                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
918                                         send_ldap_error( op, rs, LDAP_OTHER,
919                                                 "encoding values error" );
920                                         goto error_return;
921                                 }
922                         }
923                 }
924
925                 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
926                         Debug( LDAP_DEBUG_ANY,
927                                 "send_search_entry: conn %lu ber_printf failed\n", 
928                                 op->o_connid, 0, 0 );
929
930                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
931                         send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
932                         goto error_return;
933                 }
934         }
935
936         /* NOTE: moved before overlays callback circling because
937          * they may modify entry and other stuff in rs */
938         if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
939                 int     k = 0;
940                 size_t  size;
941
942                 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
943                         for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
944                 }
945
946                 size = i * sizeof(char *) + k;
947                 if ( size > 0 ) {
948                         char    *a_flags, **tmp;
949                 
950                         /*
951                          * Reuse previous memory - we likely need less space
952                          * for operational attributes
953                          */
954                         tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
955                                 op->o_tmpmemctx );
956                         if ( tmp == NULL ) {
957                                 Debug( LDAP_DEBUG_ANY,
958                                         "send_search_entry: conn %lu "
959                                         "not enough memory "
960                                         "for matched values filtering\n",
961                                         op->o_connid, 0, 0 );
962                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
963                                 send_ldap_error( op, rs, LDAP_OTHER,
964                                         "not enough memory for matched values filtering" );
965                                 goto error_return;
966                         }
967                         e_flags = tmp;
968                         a_flags = (char *)(e_flags + i);
969                         memset( a_flags, 0, k );
970                         for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
971                                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
972                                 e_flags[i] = a_flags;
973                                 a_flags += j;
974                         }
975                         rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ; 
976                     
977                         if ( rc == -1 ) {
978                                 Debug( LDAP_DEBUG_ANY,
979                                         "send_search_entry: conn %lu "
980                                         "matched values filtering failed\n", 
981                                         op->o_connid ? op->o_connid : 0, 0, 0);
982                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
983                                 send_ldap_error( op, rs, LDAP_OTHER,
984                                         "matched values filtering error" );
985                                 goto error_return;
986                         }
987                 }
988         }
989
990         for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
991                 AttributeDescription *desc = a->a_desc;
992
993                 if ( rs->sr_attrs == NULL ) {
994                         /* all user attrs request, skip operational attributes */
995                         if( is_at_operational( desc->ad_type ) ) {
996                                 continue;
997                         }
998
999                 } else {
1000                         /* specific attrs requested */
1001                         if( is_at_operational( desc->ad_type ) ) {
1002                                 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) && 
1003                                         !ad_inlist( desc, rs->sr_attrs ) )
1004                                 {
1005                                         continue;
1006                                 }
1007                         } else {
1008                                 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1009                                         continue;
1010                                 }
1011                         }
1012                 }
1013
1014                 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1015                         ACL_READ, &acl_state ) )
1016                 {
1017                         Debug( LDAP_DEBUG_ACL,
1018                                 "send_search_entry: conn %lu "
1019                                 "access to attribute %s not allowed\n",
1020                                 op->o_connid, desc->ad_cname.bv_val, 0 );
1021
1022                         continue;
1023                 }
1024
1025                 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1026                 if ( rc == -1 ) {
1027                         Debug( LDAP_DEBUG_ANY,
1028                                 "send_search_entry: conn %lu  "
1029                                 "ber_printf failed\n", op->o_connid, 0, 0 );
1030
1031                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1032                         send_ldap_error( op, rs, LDAP_OTHER,
1033                                 "encoding description error" );
1034                         goto error_return;
1035                 }
1036
1037                 if ( ! attrsonly ) {
1038                         for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1039                                 if ( ! access_allowed( op, rs->sr_entry,
1040                                         desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1041                                 {
1042                                         Debug( LDAP_DEBUG_ACL,
1043                                                 "send_search_entry: conn %lu "
1044                                                 "access to %s, value %d not allowed\n",
1045                                                 op->o_connid, desc->ad_cname.bv_val, i );
1046
1047                                         continue;
1048                                 }
1049
1050                                 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1051                                         continue;
1052                                 }
1053
1054                                 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1055                                         Debug( LDAP_DEBUG_ANY,
1056                                                 "send_search_entry: conn %lu  ber_printf failed\n", 
1057                                                 op->o_connid, 0, 0 );
1058
1059                                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1060                                         send_ldap_error( op, rs, LDAP_OTHER,
1061                                                 "encoding values error" );
1062                                         goto error_return;
1063                                 }
1064                         }
1065                 }
1066
1067                 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1068                         Debug( LDAP_DEBUG_ANY,
1069                                 "send_search_entry: conn %lu  ber_printf failed\n",
1070                                 op->o_connid, 0, 0 );
1071
1072                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1073                         send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1074                         goto error_return;
1075                 }
1076         }
1077
1078         /* free e_flags */
1079         if ( e_flags ) {
1080                 slap_sl_free( e_flags, op->o_tmpmemctx );
1081                 e_flags = NULL;
1082         }
1083
1084         rc = ber_printf( ber, /*{{*/ "}N}" );
1085
1086         if( rc != -1 ) {
1087                 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1088         }
1089
1090         if( rc != -1 ) {
1091 #ifdef LDAP_CONNECTIONLESS
1092                 if( op->o_conn && op->o_conn->c_is_udp ) {
1093                         if ( op->o_protocol != LDAP_VERSION2 ) {
1094                                 rc = ber_printf( ber, /*{*/ "N}" );
1095                         }
1096                 } else
1097 #endif
1098                 if ( op->o_res_ber == NULL ) {
1099                         rc = ber_printf( ber, /*{*/ "N}" );
1100                 }
1101         }
1102
1103         if ( rc == -1 ) {
1104                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1105
1106                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1107                 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1108                 rc = 1;
1109                 goto error_return;
1110         }
1111
1112         if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1113                 be_entry_release_rw( op, rs->sr_entry, 0 );
1114                 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1115                 rs->sr_entry = NULL;
1116         }
1117
1118         if ( op->o_res_ber == NULL ) {
1119                 bytes = send_ldap_ber( op->o_conn, ber );
1120                 ber_free_buf( ber );
1121
1122                 if ( bytes < 0 ) {
1123                         Debug( LDAP_DEBUG_ANY,
1124                                 "send_search_entry: conn %lu  ber write failed.\n", 
1125                                 op->o_connid, 0, 0 );
1126
1127                         rc = -1;
1128                         goto error_return;
1129                 }
1130                 rs->sr_nentries++;
1131
1132                 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1133                 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1134                 ldap_pvt_mp_add_ulong( slap_counters.sc_entries, 1 );
1135                 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1136                 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1137         }
1138
1139         Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1140             op->o_log_prefix, edn, 0, 0, 0 );
1141
1142         Debug( LDAP_DEBUG_TRACE,
1143                 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1144
1145         rc = 0;
1146
1147 error_return:;
1148         if ( op->o_callback ) {
1149                 slap_callback   *sc = op->o_callback, **sc_prev = &sc, *sc_next;
1150
1151                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1152                         sc_next = op->o_callback->sc_next;
1153                         if ( op->o_callback->sc_cleanup ) {
1154                                 (void)op->o_callback->sc_cleanup( op, rs );
1155                                 if ( op->o_callback != *sc_prev ) {
1156                                         *sc_prev = op->o_callback;
1157                                 }
1158                                 if ( !op->o_callback ) break;
1159                         }
1160                         sc_prev = &op->o_callback->sc_next;
1161                 }
1162                 op->o_callback = sc;
1163         }
1164
1165         if ( e_flags ) {
1166                 slap_sl_free( e_flags, op->o_tmpmemctx );
1167         }
1168
1169         if ( rs->sr_operational_attrs ) {
1170                 attrs_free( rs->sr_operational_attrs );
1171                 rs->sr_operational_attrs = NULL;
1172         }
1173         rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1174
1175         /* FIXME: I think rs->sr_type should be explicitly set to
1176          * REP_SEARCH here. That's what it was when we entered this
1177          * function. send_ldap_error may have changed it, but we
1178          * should set it back so that the cleanup functions know
1179          * what they're doing.
1180          */
1181         if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH 
1182                 && rs->sr_entry 
1183                 && ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) ) 
1184         {
1185                 entry_free( rs->sr_entry );
1186                 rs->sr_entry = NULL;
1187                 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1188         }
1189
1190         return( rc );
1191 }
1192
1193 int
1194 slap_send_search_reference( Operation *op, SlapReply *rs )
1195 {
1196         BerElementBuffer berbuf;
1197         BerElement      *ber = (BerElement *) &berbuf;
1198         int rc = 0;
1199         int bytes;
1200
1201         AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1202         AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1203
1204         rs->sr_type = REP_SEARCHREF;
1205         if ( op->o_callback ) {
1206                 slap_callback   *sc = op->o_callback, **sc_prev = &sc, *sc_next;
1207
1208                 rc = SLAP_CB_CONTINUE;
1209                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1210                         sc_next = op->o_callback->sc_next;
1211                         if ( op->o_callback->sc_response ) {
1212                                 rc = op->o_callback->sc_response( op, rs );
1213                                 if ( op->o_callback != *sc_prev ) {
1214                                         *sc_prev = op->o_callback;
1215                                 }
1216                                 if ( rc != SLAP_CB_CONTINUE || !op->o_callback ) break;
1217                         }
1218                         sc_prev = &op->o_callback->sc_next;
1219                 }
1220
1221                 op->o_callback = sc;
1222                 if ( rc != SLAP_CB_CONTINUE ) goto rel;
1223         }
1224
1225         Debug( LDAP_DEBUG_TRACE,
1226                 "=> send_search_reference: dn=\"%s\"\n",
1227                 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1228
1229         if (  rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1230                 ad_entry, NULL, ACL_READ, NULL ) )
1231         {
1232                 Debug( LDAP_DEBUG_ACL,
1233                         "send_search_reference: access to entry not allowed\n",
1234                     0, 0, 0 );
1235                 rc = 1;
1236                 goto rel;
1237         }
1238
1239         if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1240                 ad_ref, NULL, ACL_READ, NULL ) )
1241         {
1242                 Debug( LDAP_DEBUG_ACL,
1243                         "send_search_reference: access "
1244                         "to reference not allowed\n",
1245                     0, 0, 0 );
1246                 rc = 1;
1247                 goto rel;
1248         }
1249
1250 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1251         if( op->o_domain_scope ) {
1252                 Debug( LDAP_DEBUG_ANY,
1253                         "send_search_reference: domainScope control in (%s)\n", 
1254                         rs->sr_entry->e_dn, 0, 0 );
1255                 rc = 0;
1256                 goto rel;
1257         }
1258 #endif
1259
1260         if( rs->sr_ref == NULL ) {
1261                 Debug( LDAP_DEBUG_ANY,
1262                         "send_search_reference: null ref in (%s)\n", 
1263                         rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1264                 rc = 1;
1265                 goto rel;
1266         }
1267
1268         if( op->o_protocol < LDAP_VERSION3 ) {
1269                 rc = 0;
1270                 /* save the references for the result */
1271                 if( rs->sr_ref[0].bv_val != NULL ) {
1272                         if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1273                                 rc = LDAP_OTHER;
1274                 }
1275                 goto rel;
1276         }
1277
1278 #ifdef LDAP_CONNECTIONLESS
1279         if( op->o_conn && op->o_conn->c_is_udp ) {
1280                 ber = op->o_res_ber;
1281         } else
1282 #endif
1283         {
1284                 ber_init_w_nullc( ber, LBER_USE_DER );
1285                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1286         }
1287
1288         rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1289                 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1290
1291         if( rc != -1 ) {
1292                 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1293         }
1294
1295         if( rc != -1 ) {
1296                 rc = ber_printf( ber, /*"{"*/ "N}" );
1297         }
1298
1299         if ( rc == -1 ) {
1300                 Debug( LDAP_DEBUG_ANY,
1301                         "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1302
1303 #ifdef LDAP_CONNECTIONLESS
1304                 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1305 #endif
1306                 ber_free_buf( ber );
1307                 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1308                 goto rel;
1309         }
1310
1311         rc = 0;
1312         if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1313                 be_entry_release_rw( op, rs->sr_entry, 0 );
1314                 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1315                 rs->sr_entry = NULL;
1316         }
1317
1318 #ifdef LDAP_CONNECTIONLESS
1319         if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1320 #endif
1321         bytes = send_ldap_ber( op->o_conn, ber );
1322         ber_free_buf( ber );
1323
1324         ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1325         ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1326         ldap_pvt_mp_add_ulong( slap_counters.sc_refs, 1 );
1327         ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1328         ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1329 #ifdef LDAP_CONNECTIONLESS
1330         }
1331 #endif
1332         if ( rs->sr_ref != NULL ) {
1333                 int     r;
1334
1335                 for ( r = 0; !BER_BVISNULL( &rs->sr_ref[ r ] ); r++ ) {
1336                         Statslog( LDAP_DEBUG_STATS2, "%s REF #%d \"%s\"\n",
1337                                 op->o_log_prefix, r, rs->sr_ref[0].bv_val,
1338                                 0, 0 );
1339                 }
1340
1341         } else {
1342                 Statslog( LDAP_DEBUG_STATS2, "%s REF \"(null)\"\n",
1343                         op->o_log_prefix, 0, 0, 0, 0 );
1344         }
1345
1346         Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1347
1348 rel:
1349         if ( op->o_callback ) {
1350                 slap_callback   *sc = op->o_callback, **sc_prev = &sc, *sc_next;
1351
1352                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1353                         sc_next = op->o_callback->sc_next;
1354                         if ( op->o_callback->sc_cleanup ) {
1355                                 (void)op->o_callback->sc_cleanup( op, rs );
1356                                 if ( op->o_callback != *sc_prev ) {
1357                                         *sc_prev = op->o_callback;
1358                                 }
1359                                 if ( rc != SLAP_CB_CONTINUE || !op->o_callback ) break;
1360                         }
1361                         sc_prev = &op->o_callback->sc_next;
1362                 }
1363                 op->o_callback = sc;
1364         }
1365
1366         return rc;
1367 }
1368
1369 int
1370 str2result(
1371     char        *s,
1372     int         *code,
1373     char        **matched,
1374     char        **info )
1375 {
1376         int     rc;
1377         char    *c;
1378
1379         *code = LDAP_SUCCESS;
1380         *matched = NULL;
1381         *info = NULL;
1382
1383         if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1384                 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1385                     s, 0, 0 );
1386
1387                 return( -1 );
1388         }
1389
1390         rc = 0;
1391         while ( (s = strchr( s, '\n' )) != NULL ) {
1392                 *s++ = '\0';
1393                 if ( *s == '\0' ) {
1394                         break;
1395                 }
1396                 if ( (c = strchr( s, ':' )) != NULL ) {
1397                         c++;
1398                 }
1399
1400                 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1401                         if ( c != NULL && lutil_atoi( code, c ) != 0 ) {
1402                                 goto bailout;
1403                         }
1404                 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1405                         if ( c != NULL ) {
1406                                 *matched = c;
1407                         }
1408                 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1409                         if ( c != NULL ) {
1410                                 *info = c;
1411                         }
1412                 } else {
1413 bailout:;
1414                         Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1415                             s, 0, 0 );
1416
1417                         rc = -1;
1418                 }
1419         }
1420
1421         return( rc );
1422 }
1423
1424 int slap_read_controls(
1425         Operation *op,
1426         SlapReply *rs,
1427         Entry *e,
1428         const struct berval *oid,
1429         LDAPControl **ctrl )
1430 {
1431         int rc;
1432         struct berval bv;
1433         BerElementBuffer berbuf;
1434         BerElement *ber = (BerElement *) &berbuf;
1435         LDAPControl c;
1436         Operation myop;
1437
1438         Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1439                 oid->bv_val, e->e_dn, 0 );
1440
1441         rs->sr_entry = e;
1442         rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1443                 op->o_preread_attrs : op->o_postread_attrs; 
1444
1445         bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1446         bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1447
1448         ber_init2( ber, &bv, LBER_USE_DER );
1449         ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1450
1451         /* create new operation */
1452         myop = *op;
1453         myop.o_bd = NULL;
1454         myop.o_res_ber = ber;
1455         myop.o_callback = NULL;
1456
1457         rc = slap_send_search_entry( &myop, rs );
1458         if( rc ) return rc;
1459
1460         rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1461
1462         if( rc == LBER_ERROR ) return LDAP_OTHER;
1463
1464         c.ldctl_oid = oid->bv_val;
1465         c.ldctl_iscritical = 0;
1466
1467         if ( *ctrl == NULL ) {
1468                 /* first try */
1469                 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1470         } else {
1471                 /* retry: free previous try */
1472                 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1473         }
1474
1475         **ctrl = c;
1476         return LDAP_SUCCESS;
1477 }
1478
1479 /* Map API errors to protocol errors... */
1480 int
1481 slap_map_api2result( SlapReply *rs )
1482 {
1483         switch(rs->sr_err) {
1484         case LDAP_SERVER_DOWN:
1485                 return LDAP_UNAVAILABLE;
1486         case LDAP_LOCAL_ERROR:
1487                 return LDAP_OTHER;
1488         case LDAP_ENCODING_ERROR:
1489         case LDAP_DECODING_ERROR:
1490                 return LDAP_PROTOCOL_ERROR;
1491         case LDAP_TIMEOUT:
1492                 return LDAP_UNAVAILABLE;
1493         case LDAP_AUTH_UNKNOWN:
1494                 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1495         case LDAP_FILTER_ERROR:
1496                 rs->sr_text = "Filter error";
1497                 return LDAP_OTHER;
1498         case LDAP_USER_CANCELLED:
1499                 rs->sr_text = "User cancelled";
1500                 return LDAP_OTHER;
1501         case LDAP_PARAM_ERROR:
1502                 return LDAP_PROTOCOL_ERROR;
1503         case LDAP_NO_MEMORY:
1504                 return LDAP_OTHER;
1505         case LDAP_CONNECT_ERROR:
1506                 return LDAP_UNAVAILABLE;
1507         case LDAP_NOT_SUPPORTED:
1508                 return LDAP_UNWILLING_TO_PERFORM;
1509         case LDAP_CONTROL_NOT_FOUND:
1510                 return LDAP_PROTOCOL_ERROR;
1511         case LDAP_NO_RESULTS_RETURNED:
1512                 return LDAP_NO_SUCH_OBJECT;
1513         case LDAP_MORE_RESULTS_TO_RETURN:
1514                 rs->sr_text = "More results to return";
1515                 return LDAP_OTHER;
1516         case LDAP_CLIENT_LOOP:
1517         case LDAP_REFERRAL_LIMIT_EXCEEDED:
1518                 return LDAP_LOOP_DETECT;
1519         default:
1520                 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1521                 return rs->sr_err;
1522         }
1523 }
1524
1525
1526 slap_mask_t
1527 slap_attr_flags( AttributeName *an )
1528 {
1529         slap_mask_t     flags = SLAP_ATTRS_UNDEFINED;
1530
1531         if ( an == NULL ) {
1532                 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1533
1534         } else {
1535                 flags |= an_find( an, &AllOper )
1536                         ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1537                 flags |= an_find( an, &AllUser )
1538                         ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;
1539         }
1540
1541         return flags;
1542 }
1543