]> git.sur5r.net Git - openldap/blob - servers/slapd/result.c
ITS#4237 don't update sc_prev after a callback has been freed.
[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                                 slap_callback *sc2 = op->o_callback;
312                                 rc = op->o_callback->sc_response( op, rs );
313                                 if ( op->o_callback != sc2 ) {
314                                         *sc_prev = op->o_callback;
315                                 }
316                                 if ( rc != SLAP_CB_CONTINUE || !op->o_callback ) break;
317                                 if ( op->o_callback != sc2 ) continue;
318                         }
319                         sc_prev = &op->o_callback->sc_next;
320                 }
321
322                 op->o_callback = sc;
323                 if ( rc != SLAP_CB_CONTINUE ) goto clean2;
324         }
325
326 #ifdef LDAP_CONNECTIONLESS
327         if (op->o_conn && op->o_conn->c_is_udp)
328                 ber = op->o_res_ber;
329         else
330 #endif
331         {
332                 ber_init_w_nullc( ber, LBER_USE_DER );
333                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
334         }
335
336         Debug( LDAP_DEBUG_TRACE,
337                 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
338                 rs->sr_msgid, rs->sr_tag, rs->sr_err );
339
340         if( rs->sr_ref ) {
341                 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
342                         rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
343                         NULL, NULL );
344         }
345
346 #ifdef LDAP_CONNECTIONLESS
347         if (op->o_conn && op->o_conn->c_is_udp &&
348                 op->o_protocol == LDAP_VERSION2 )
349         {
350                 rc = ber_printf( ber, "t{ess" /*"}"*/,
351                         rs->sr_tag, rs->sr_err,
352                 rs->sr_matched == NULL ? "" : rs->sr_matched,
353                 rs->sr_text == NULL ? "" : rs->sr_text );
354         } else 
355 #endif
356         if ( rs->sr_type == REP_INTERMEDIATE ) {
357             rc = ber_printf( ber, "{it{" /*"}}"*/,
358                         rs->sr_msgid, rs->sr_tag );
359
360         } else {
361             rc = ber_printf( ber, "{it{ess" /*"}}"*/,
362                 rs->sr_msgid, rs->sr_tag, rs->sr_err,
363                 rs->sr_matched == NULL ? "" : rs->sr_matched,
364                 rs->sr_text == NULL ? "" : rs->sr_text );
365         }
366
367         if( rc != -1 ) {
368                 if ( rs->sr_ref != NULL ) {
369                         assert( rs->sr_err == LDAP_REFERRAL );
370                         rc = ber_printf( ber, "t{W}",
371                                 LDAP_TAG_REFERRAL, rs->sr_ref );
372                 } else {
373                         assert( rs->sr_err != LDAP_REFERRAL );
374                 }
375         }
376
377         if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
378                 rc = ber_printf( ber, "tO",
379                         LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
380         }
381
382         if( rc != -1 &&
383                 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
384         {
385                 if ( rs->sr_rspoid != NULL ) {
386                         rc = ber_printf( ber, "ts",
387                                 rs->sr_type == REP_EXTENDED
388                                         ? LDAP_TAG_EXOP_RES_OID : LDAP_TAG_IM_RES_OID,
389                                 rs->sr_rspoid );
390                 }
391                 if( rc != -1 && rs->sr_rspdata != NULL ) {
392                         rc = ber_printf( ber, "tO",
393                                 rs->sr_type == REP_EXTENDED
394                                         ? LDAP_TAG_EXOP_RES_VALUE : LDAP_TAG_IM_RES_VALUE,
395                                 rs->sr_rspdata );
396                 }
397         }
398
399         if( rc != -1 ) {
400                 rc = ber_printf( ber, /*"{"*/ "N}" );
401         }
402
403         if( rc != -1 ) {
404                 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
405         }
406
407         if( rc != -1 ) {
408                 rc = ber_printf( ber, /*"{"*/ "N}" );
409         }
410
411 #ifdef LDAP_CONNECTIONLESS
412         if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
413                 && rc != -1 )
414         {
415                 rc = ber_printf( ber, /*"{"*/ "N}" );
416         }
417 #endif
418                 
419         if ( rc == -1 ) {
420                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
421
422 #ifdef LDAP_CONNECTIONLESS
423                 if (!op->o_conn || op->o_conn->c_is_udp == 0)
424 #endif
425                 {
426                         ber_free_buf( ber );
427                 }
428                 goto cleanup;
429         }
430
431         /* send BER */
432         bytes = send_ldap_ber( op->o_conn, ber );
433 #ifdef LDAP_CONNECTIONLESS
434         if (!op->o_conn || op->o_conn->c_is_udp == 0)
435 #endif
436         {
437                 ber_free_buf( ber );
438         }
439
440         if ( bytes < 0 ) {
441                 Debug( LDAP_DEBUG_ANY,
442                         "send_ldap_response: ber write failed\n",
443                         0, 0, 0 );
444
445                 goto cleanup;
446         }
447
448         ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
449         ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
450         ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
451         ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
452
453 cleanup:;
454         /* Tell caller that we did this for real, as opposed to being
455          * overridden by a callback
456          */
457         rc = SLAP_CB_CONTINUE;
458
459 clean2:;
460         if ( op->o_callback ) {
461                 slap_callback   *sc = op->o_callback, **sc_prev = &sc, *sc_next;
462
463                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
464                         sc_next = op->o_callback->sc_next;
465                         if ( op->o_callback->sc_cleanup ) {
466                                 slap_callback *sc2 = op->o_callback;
467                                 (void)op->o_callback->sc_cleanup( op, rs );
468                                 if ( op->o_callback != sc2 ) {
469                                         *sc_prev = op->o_callback;
470                                 }
471                                 if ( !op->o_callback ) break;
472                                 if ( op->o_callback != sc2 ) continue;
473                         }
474                         sc_prev = &op->o_callback->sc_next;
475                 }
476                 op->o_callback = sc;
477         }
478
479
480         if ( rs->sr_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
481                 free( (char *)rs->sr_matched );
482                 rs->sr_matched = NULL;
483         }
484
485         if ( rs->sr_ref && rs->sr_flags & REP_REF_MUSTBEFREED ) {
486                 ber_bvarray_free( rs->sr_ref );
487                 rs->sr_ref = NULL;
488         }
489
490         return rc;
491 }
492
493
494 void
495 send_ldap_disconnect( Operation *op, SlapReply *rs )
496 {
497 #define LDAP_UNSOLICITED_ERROR(e) \
498         (  (e) == LDAP_PROTOCOL_ERROR \
499         || (e) == LDAP_STRONG_AUTH_REQUIRED \
500         || (e) == LDAP_UNAVAILABLE )
501
502         assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
503
504         rs->sr_type = REP_EXTENDED;
505
506         Debug( LDAP_DEBUG_TRACE,
507                 "send_ldap_disconnect %d:%s\n",
508                 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
509
510         if ( op->o_protocol < LDAP_VERSION3 ) {
511                 rs->sr_rspoid = NULL;
512                 rs->sr_tag = slap_req2res( op->o_tag );
513                 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
514
515         } else {
516                 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
517                 rs->sr_tag = LDAP_RES_EXTENDED;
518                 rs->sr_msgid = 0;
519         }
520
521         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
522                 Statslog( LDAP_DEBUG_STATS,
523                         "%s DISCONNECT tag=%lu err=%d text=%s\n",
524                         op->o_log_prefix, rs->sr_tag, rs->sr_err,
525                         rs->sr_text ? rs->sr_text : "", 0 );
526         }
527 }
528
529 void
530 slap_send_ldap_result( Operation *op, SlapReply *rs )
531 {
532         char *tmp = NULL;
533         const char *otext = rs->sr_text;
534         BerVarray oref = rs->sr_ref;
535
536         rs->sr_type = REP_RESULT;
537
538         /* Propagate Abandons so that cleanup callbacks can be processed */
539         if ( rs->sr_err == SLAPD_ABANDON )
540                 goto abandon;
541
542         assert( !LDAP_API_ERROR( rs->sr_err ) );
543
544         Debug( LDAP_DEBUG_TRACE,
545                 "send_ldap_result: %s p=%d\n",
546                 op->o_log_prefix, op->o_protocol, 0 );
547
548         Debug( LDAP_DEBUG_ARGS,
549                 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
550                 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
551                 rs->sr_text ? rs->sr_text : "" );
552
553
554         if( rs->sr_ref ) {
555                 Debug( LDAP_DEBUG_ARGS,
556                         "send_ldap_result: referral=\"%s\"\n",
557                         rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
558                         NULL, NULL );
559         }
560
561         assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
562
563         if ( rs->sr_err == LDAP_REFERRAL ) {
564                 if( op->o_domain_scope ) rs->sr_ref = NULL;
565
566                 if( rs->sr_ref == NULL ) {
567                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
568                 } else if ( op->o_protocol < LDAP_VERSION3 ) {
569                         rs->sr_err = LDAP_PARTIAL_RESULTS;
570                 }
571         }
572
573         if ( op->o_protocol < LDAP_VERSION3 ) {
574                 tmp = v2ref( rs->sr_ref, rs->sr_text );
575                 rs->sr_text = tmp;
576                 rs->sr_ref = NULL;
577         }
578
579         rs->sr_tag = slap_req2res( op->o_tag );
580         rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
581
582 abandon:
583         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
584                 if ( op->o_tag == LDAP_REQ_SEARCH ) {
585                         char nbuf[64];
586                         snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
587                                 rs->sr_err, rs->sr_nentries );
588
589                         Statslog( LDAP_DEBUG_STATS,
590                         "%s SEARCH RESULT tag=%lu err=%s text=%s\n",
591                                 op->o_log_prefix, rs->sr_tag, nbuf,
592                                 rs->sr_text ? rs->sr_text : "", 0 );
593                 } else {
594                         Statslog( LDAP_DEBUG_STATS,
595                                 "%s RESULT tag=%lu err=%d text=%s\n",
596                                 op->o_log_prefix, rs->sr_tag, rs->sr_err,
597                                 rs->sr_text ? rs->sr_text : "", 0 );
598                 }
599         }
600
601         if( tmp != NULL ) ch_free(tmp);
602         rs->sr_text = otext;
603         rs->sr_ref = oref;
604 }
605
606 void
607 send_ldap_sasl( Operation *op, SlapReply *rs )
608 {
609         rs->sr_type = REP_SASL;
610         Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
611                 rs->sr_err,
612                 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
613
614         rs->sr_tag = slap_req2res( op->o_tag );
615         rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
616
617         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
618                 Statslog( LDAP_DEBUG_STATS,
619                         "%s RESULT tag=%lu err=%d text=%s\n",
620                         op->o_log_prefix, rs->sr_tag, rs->sr_err,
621                         rs->sr_text ? rs->sr_text : "", 0 );
622         }
623 }
624
625 void
626 slap_send_ldap_extended( Operation *op, SlapReply *rs )
627 {
628         rs->sr_type = REP_EXTENDED;
629
630         Debug( LDAP_DEBUG_TRACE,
631                 "send_ldap_extended: err=%d oid=%s len=%ld\n",
632                 rs->sr_err,
633                 rs->sr_rspoid ? rs->sr_rspoid : "",
634                 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
635
636         rs->sr_tag = slap_req2res( op->o_tag );
637         rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
638
639         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
640                 Statslog( LDAP_DEBUG_STATS,
641                         "%s RESULT oid=%s err=%d text=%s\n",
642                         op->o_log_prefix, rs->sr_rspoid ? rs->sr_rspoid : "",
643                         rs->sr_err, rs->sr_text ? rs->sr_text : "", 0 );
644         }
645 }
646
647 void
648 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
649 {
650         rs->sr_type = REP_INTERMEDIATE;
651         Debug( LDAP_DEBUG_TRACE,
652                 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
653                 rs->sr_err,
654                 rs->sr_rspoid ? rs->sr_rspoid : "",
655                 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
656         rs->sr_tag = LDAP_RES_INTERMEDIATE;
657         rs->sr_msgid = op->o_msgid;
658         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
659                 Statslog( LDAP_DEBUG_STATS2,
660                         "%s INTERM oid=%s\n",
661                         op->o_log_prefix,
662                         rs->sr_rspoid ? rs->sr_rspoid : "", 0, 0, 0 );
663         }
664 }
665
666 /*
667  * returns:
668  *
669  * LDAP_SUCCESS                 entry sent
670  * LDAP_OTHER                   entry not sent (other)
671  * LDAP_INSUFFICIENT_ACCESS     entry not sent (ACL)
672  * LDAP_UNAVAILABLE             entry not sent (connection closed)
673  * LDAP_SIZELIMIT_EXCEEDED      entry not sent (caller must send sizelimitExceeded)
674  */
675
676 int
677 slap_send_search_entry( Operation *op, SlapReply *rs )
678 {
679         BerElementBuffer berbuf;
680         BerElement      *ber = (BerElement *) &berbuf;
681         Attribute       *a;
682         int             i, j, rc = LDAP_UNAVAILABLE, bytes;
683         char            *edn;
684         int             userattrs;
685         AccessControlState acl_state = ACL_STATE_INIT;
686         int                      attrsonly;
687         AttributeDescription *ad_entry = slap_schema.si_ad_entry;
688
689         /* a_flags: array of flags telling if the i-th element will be
690          *          returned or filtered out
691          * e_flags: array of a_flags
692          */
693         char **e_flags = NULL;
694
695         if ( op->ors_slimit >= 0 && rs->sr_nentries >= op->ors_slimit ) {
696                 return LDAP_SIZELIMIT_EXCEEDED;
697         }
698
699         rs->sr_type = REP_SEARCH;
700
701         /* eventually will loop through generated operational attribute types
702          * currently implemented types include:
703          *      entryDN, subschemaSubentry, and hasSubordinates */
704         /* NOTE: moved before overlays callback circling because
705          * they may modify entry and other stuff in rs */
706         /* check for special all operational attributes ("+") type */
707         /* FIXME: maybe we could se this flag at the operation level;
708          * however, in principle the caller of send_search_entry() may
709          * change the attribute list at each call */
710         rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
711
712         rc = backend_operational( op, rs );
713         if ( rc ) {
714                 goto error_return;
715         }
716
717         if ( op->o_callback ) {
718                 slap_callback   *sc = op->o_callback, **sc_prev = &sc, *sc_next;
719
720                 rc = SLAP_CB_CONTINUE;
721                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next )
722                 {
723                         sc_next = op->o_callback->sc_next;
724                         if ( op->o_callback->sc_response ) {
725                                 slap_callback *sc2 = op->o_callback;
726                                 rc = op->o_callback->sc_response( op, rs );
727                                 if ( op->o_callback != sc2 ) {
728                                         *sc_prev = op->o_callback;
729                                 }
730                                 if ( rc != SLAP_CB_CONTINUE || !op->o_callback ) break;
731                                 if ( op->o_callback != sc2 ) continue;
732                         }
733                         sc_prev = &op->o_callback->sc_next;
734                 }
735
736                 op->o_callback = sc;
737                 if ( rc != SLAP_CB_CONTINUE ) goto error_return;
738         }
739
740         Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
741                 op->o_connid, rs->sr_entry->e_name.bv_val,
742                 op->ors_attrsonly ? " (attrsOnly)" : "" );
743
744         attrsonly = op->ors_attrsonly;
745
746         if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
747                 Debug( LDAP_DEBUG_ACL,
748                         "send_search_entry: conn %lu access to entry (%s) not allowed\n", 
749                         op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
750
751                 rc = LDAP_INSUFFICIENT_ACCESS;
752                 goto error_return;
753         }
754
755         edn = rs->sr_entry->e_nname.bv_val;
756
757         if ( op->o_res_ber ) {
758                 /* read back control or LDAP_CONNECTIONLESS */
759             ber = op->o_res_ber;
760         } else {
761                 struct berval   bv;
762
763                 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
764                 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
765
766                 ber_init2( ber, &bv, LBER_USE_DER );
767                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
768         }
769
770 #ifdef LDAP_CONNECTIONLESS
771         if ( op->o_conn && op->o_conn->c_is_udp ) {
772                 /* CONNECTIONLESS */
773                 if ( op->o_protocol == LDAP_VERSION2 ) {
774                 rc = ber_printf(ber, "t{O{" /*}}*/,
775                                 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
776                 } else {
777                 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
778                                 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
779                 }
780         } else
781 #endif
782         if ( op->o_res_ber ) {
783                 /* read back control */
784             rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
785         } else {
786             rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
787                         LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
788         }
789
790         if ( rc == -1 ) {
791                 Debug( LDAP_DEBUG_ANY, 
792                         "send_search_entry: conn %lu  ber_printf failed\n", 
793                         op->o_connid, 0, 0 );
794
795                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
796                 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
797                 rc = rs->sr_err;
798                 goto error_return;
799         }
800
801         /* check for special all user attributes ("*") type */
802         userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
803
804         /* create an array of arrays of flags. Each flag corresponds
805          * to particular value of attribute and equals 1 if value matches
806          * to ValuesReturnFilter or 0 if not
807          */     
808         if ( op->o_vrFilter != NULL ) {
809                 int     k = 0;
810                 size_t  size;
811
812                 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
813                         for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
814                 }
815
816                 size = i * sizeof(char *) + k;
817                 if ( size > 0 ) {
818                         char    *a_flags;
819                         e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
820                         if( e_flags == NULL ) {
821                         Debug( LDAP_DEBUG_ANY, 
822                                         "send_search_entry: conn %lu slap_sl_calloc failed\n",
823                                         op->o_connid ? op->o_connid : 0, 0, 0 );
824                                 ber_free( ber, 1 );
825         
826                                 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
827                                 goto error_return;
828                         }
829                         a_flags = (char *)(e_flags + i);
830                         memset( a_flags, 0, k );
831                         for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
832                                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
833                                 e_flags[i] = a_flags;
834                                 a_flags += j;
835                         }
836         
837                         rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ; 
838                         if ( rc == -1 ) {
839                                 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
840                                         "conn %lu matched values filtering failed\n",
841                                         op->o_connid ? op->o_connid : 0, 0, 0 );
842                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
843                                 send_ldap_error( op, rs, LDAP_OTHER,
844                                         "matched values filtering error" );
845                                 rc = rs->sr_err;
846                                 goto error_return;
847                         }
848                 }
849         }
850
851         for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
852                 AttributeDescription *desc = a->a_desc;
853                 int finish = 0;
854
855                 if ( rs->sr_attrs == NULL ) {
856                         /* all user attrs request, skip operational attributes */
857                         if( is_at_operational( desc->ad_type ) ) {
858                                 continue;
859                         }
860
861                 } else {
862                         /* specific attrs requested */
863                         if ( is_at_operational( desc->ad_type ) ) {
864                                 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
865                                         !ad_inlist( desc, rs->sr_attrs ) )
866                                 {
867                                         continue;
868                                 }
869
870                         } else {
871                                 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
872                                         continue;
873                                 }
874                         }
875                 }
876
877                 if ( attrsonly ) {
878                         if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
879                                 ACL_READ, &acl_state ) )
880                         {
881                                 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
882                                         "conn %lu access to attribute %s not allowed\n",
883                                         op->o_connid, desc->ad_cname.bv_val, 0 );
884                                 continue;
885                         }
886
887                         if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
888                                 Debug( LDAP_DEBUG_ANY, 
889                                         "send_search_entry: conn %lu  ber_printf failed\n", 
890                                         op->o_connid, 0, 0 );
891
892                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
893                                 send_ldap_error( op, rs, LDAP_OTHER,
894                                         "encoding description error");
895                                 rc = rs->sr_err;
896                                 goto error_return;
897                         }
898                         finish = 1;
899
900                 } else {
901                         int first = 1;
902                         for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
903                                 if ( ! access_allowed( op, rs->sr_entry,
904                                         desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
905                                 {
906                                         Debug( LDAP_DEBUG_ACL,
907                                                 "send_search_entry: conn %lu "
908                                                 "access to attribute %s, value #%d not allowed\n",
909                                                 op->o_connid, desc->ad_cname.bv_val, i );
910
911                                         continue;
912                                 }
913
914                                 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
915                                         continue;
916                                 }
917
918                                 if ( first ) {
919                                         first = 0;
920                                         finish = 1;
921                                         if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
922                                                 Debug( LDAP_DEBUG_ANY,
923                                                         "send_search_entry: conn %lu  ber_printf failed\n", 
924                                                         op->o_connid, 0, 0 );
925
926                                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
927                                                 send_ldap_error( op, rs, LDAP_OTHER,
928                                                         "encoding description error");
929                                                 rc = rs->sr_err;
930                                                 goto error_return;
931                                         }
932                                 }
933                                 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
934                                         Debug( LDAP_DEBUG_ANY,
935                                                 "send_search_entry: conn %lu  "
936                                                 "ber_printf failed.\n", op->o_connid, 0, 0 );
937
938                                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
939                                         send_ldap_error( op, rs, LDAP_OTHER,
940                                                 "encoding values error" );
941                                         rc = rs->sr_err;
942                                         goto error_return;
943                                 }
944                         }
945                 }
946
947                 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
948                         Debug( LDAP_DEBUG_ANY,
949                                 "send_search_entry: conn %lu ber_printf failed\n", 
950                                 op->o_connid, 0, 0 );
951
952                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
953                         send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
954                         rc = rs->sr_err;
955                         goto error_return;
956                 }
957         }
958
959         /* NOTE: moved before overlays callback circling because
960          * they may modify entry and other stuff in rs */
961         if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
962                 int     k = 0;
963                 size_t  size;
964
965                 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
966                         for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
967                 }
968
969                 size = i * sizeof(char *) + k;
970                 if ( size > 0 ) {
971                         char    *a_flags, **tmp;
972                 
973                         /*
974                          * Reuse previous memory - we likely need less space
975                          * for operational attributes
976                          */
977                         tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
978                                 op->o_tmpmemctx );
979                         if ( tmp == NULL ) {
980                                 Debug( LDAP_DEBUG_ANY,
981                                         "send_search_entry: conn %lu "
982                                         "not enough memory "
983                                         "for matched values filtering\n",
984                                         op->o_connid, 0, 0 );
985                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
986                                 send_ldap_error( op, rs, LDAP_OTHER,
987                                         "not enough memory for matched values filtering" );
988                                 goto error_return;
989                         }
990                         e_flags = tmp;
991                         a_flags = (char *)(e_flags + i);
992                         memset( a_flags, 0, k );
993                         for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
994                                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
995                                 e_flags[i] = a_flags;
996                                 a_flags += j;
997                         }
998                         rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ; 
999                     
1000                         if ( rc == -1 ) {
1001                                 Debug( LDAP_DEBUG_ANY,
1002                                         "send_search_entry: conn %lu "
1003                                         "matched values filtering failed\n", 
1004                                         op->o_connid ? op->o_connid : 0, 0, 0);
1005                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1006                                 send_ldap_error( op, rs, LDAP_OTHER,
1007                                         "matched values filtering error" );
1008                                 rc = rs->sr_err;
1009                                 goto error_return;
1010                         }
1011                 }
1012         }
1013
1014         for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1015                 AttributeDescription *desc = a->a_desc;
1016
1017                 if ( rs->sr_attrs == NULL ) {
1018                         /* all user attrs request, skip operational attributes */
1019                         if( is_at_operational( desc->ad_type ) ) {
1020                                 continue;
1021                         }
1022
1023                 } else {
1024                         /* specific attrs requested */
1025                         if( is_at_operational( desc->ad_type ) ) {
1026                                 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) && 
1027                                         !ad_inlist( desc, rs->sr_attrs ) )
1028                                 {
1029                                         continue;
1030                                 }
1031                         } else {
1032                                 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1033                                         continue;
1034                                 }
1035                         }
1036                 }
1037
1038                 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1039                         ACL_READ, &acl_state ) )
1040                 {
1041                         Debug( LDAP_DEBUG_ACL,
1042                                 "send_search_entry: conn %lu "
1043                                 "access to attribute %s not allowed\n",
1044                                 op->o_connid, desc->ad_cname.bv_val, 0 );
1045
1046                         continue;
1047                 }
1048
1049                 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1050                 if ( rc == -1 ) {
1051                         Debug( LDAP_DEBUG_ANY,
1052                                 "send_search_entry: conn %lu  "
1053                                 "ber_printf failed\n", op->o_connid, 0, 0 );
1054
1055                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1056                         send_ldap_error( op, rs, LDAP_OTHER,
1057                                 "encoding description error" );
1058                         rc = rs->sr_err;
1059                         goto error_return;
1060                 }
1061
1062                 if ( ! attrsonly ) {
1063                         for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1064                                 if ( ! access_allowed( op, rs->sr_entry,
1065                                         desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1066                                 {
1067                                         Debug( LDAP_DEBUG_ACL,
1068                                                 "send_search_entry: conn %lu "
1069                                                 "access to %s, value %d not allowed\n",
1070                                                 op->o_connid, desc->ad_cname.bv_val, i );
1071
1072                                         continue;
1073                                 }
1074
1075                                 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1076                                         continue;
1077                                 }
1078
1079                                 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1080                                         Debug( LDAP_DEBUG_ANY,
1081                                                 "send_search_entry: conn %lu  ber_printf failed\n", 
1082                                                 op->o_connid, 0, 0 );
1083
1084                                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1085                                         send_ldap_error( op, rs, LDAP_OTHER,
1086                                                 "encoding values error" );
1087                                         rc = rs->sr_err;
1088                                         goto error_return;
1089                                 }
1090                         }
1091                 }
1092
1093                 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1094                         Debug( LDAP_DEBUG_ANY,
1095                                 "send_search_entry: conn %lu  ber_printf failed\n",
1096                                 op->o_connid, 0, 0 );
1097
1098                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1099                         send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1100                         rc = rs->sr_err;
1101                         goto error_return;
1102                 }
1103         }
1104
1105         /* free e_flags */
1106         if ( e_flags ) {
1107                 slap_sl_free( e_flags, op->o_tmpmemctx );
1108                 e_flags = NULL;
1109         }
1110
1111         rc = ber_printf( ber, /*{{*/ "}N}" );
1112
1113         if( rc != -1 ) {
1114                 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1115         }
1116
1117         if( rc != -1 ) {
1118 #ifdef LDAP_CONNECTIONLESS
1119                 if( op->o_conn && op->o_conn->c_is_udp ) {
1120                         if ( op->o_protocol != LDAP_VERSION2 ) {
1121                                 rc = ber_printf( ber, /*{*/ "N}" );
1122                         }
1123                 } else
1124 #endif
1125                 if ( op->o_res_ber == NULL ) {
1126                         rc = ber_printf( ber, /*{*/ "N}" );
1127                 }
1128         }
1129
1130         if ( rc == -1 ) {
1131                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1132
1133                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1134                 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1135                 rc = rs->sr_err;
1136                 goto error_return;
1137         }
1138
1139         if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1140                 be_entry_release_rw( op, rs->sr_entry, 0 );
1141                 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1142                 rs->sr_entry = NULL;
1143         }
1144
1145         if ( op->o_res_ber == NULL ) {
1146                 bytes = send_ldap_ber( op->o_conn, ber );
1147                 ber_free_buf( ber );
1148
1149                 if ( bytes < 0 ) {
1150                         Debug( LDAP_DEBUG_ANY,
1151                                 "send_search_entry: conn %lu  ber write failed.\n", 
1152                                 op->o_connid, 0, 0 );
1153
1154                         rc = LDAP_UNAVAILABLE;
1155                         goto error_return;
1156                 }
1157                 rs->sr_nentries++;
1158
1159                 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1160                 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1161                 ldap_pvt_mp_add_ulong( slap_counters.sc_entries, 1 );
1162                 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1163                 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1164         }
1165
1166         Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1167             op->o_log_prefix, edn, 0, 0, 0 );
1168
1169         Debug( LDAP_DEBUG_TRACE,
1170                 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1171
1172         rc = LDAP_SUCCESS;
1173
1174 error_return:;
1175         if ( op->o_callback ) {
1176                 slap_callback   *sc = op->o_callback, **sc_prev = &sc, *sc_next;
1177
1178                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1179                         sc_next = op->o_callback->sc_next;
1180                         if ( op->o_callback->sc_cleanup ) {
1181                                 slap_callback *sc2 = op->o_callback;
1182                                 (void)op->o_callback->sc_cleanup( op, rs );
1183                                 if ( op->o_callback != sc2 ) {
1184                                         *sc_prev = op->o_callback;
1185                                 }
1186                                 if ( !op->o_callback ) break;
1187                                 if ( op->o_callback != sc2 ) continue;
1188                         }
1189                         sc_prev = &op->o_callback->sc_next;
1190                 }
1191                 op->o_callback = sc;
1192         }
1193
1194         if ( e_flags ) {
1195                 slap_sl_free( e_flags, op->o_tmpmemctx );
1196         }
1197
1198         if ( rs->sr_operational_attrs ) {
1199                 attrs_free( rs->sr_operational_attrs );
1200                 rs->sr_operational_attrs = NULL;
1201         }
1202         rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1203
1204         /* FIXME: I think rs->sr_type should be explicitly set to
1205          * REP_SEARCH here. That's what it was when we entered this
1206          * function. send_ldap_error may have changed it, but we
1207          * should set it back so that the cleanup functions know
1208          * what they're doing.
1209          */
1210         if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH 
1211                 && rs->sr_entry 
1212                 && ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) ) 
1213         {
1214                 entry_free( rs->sr_entry );
1215                 rs->sr_entry = NULL;
1216                 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1217         }
1218
1219         return( rc );
1220 }
1221
1222 int
1223 slap_send_search_reference( Operation *op, SlapReply *rs )
1224 {
1225         BerElementBuffer berbuf;
1226         BerElement      *ber = (BerElement *) &berbuf;
1227         int rc = 0;
1228         int bytes;
1229
1230         AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1231         AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1232
1233         rs->sr_type = REP_SEARCHREF;
1234         if ( op->o_callback ) {
1235                 slap_callback   *sc = op->o_callback, **sc_prev = &sc, *sc_next;
1236
1237                 rc = SLAP_CB_CONTINUE;
1238                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1239                         sc_next = op->o_callback->sc_next;
1240                         if ( op->o_callback->sc_response ) {
1241                                 slap_callback *sc2 = op->o_callback;
1242                                 rc = op->o_callback->sc_response( op, rs );
1243                                 if ( op->o_callback != sc2 ) {
1244                                         *sc_prev = op->o_callback;
1245                                 }
1246                                 if ( rc != SLAP_CB_CONTINUE || !op->o_callback ) break;
1247                                 if ( op->o_callback != sc2 ) continue;
1248                         }
1249                         sc_prev = &op->o_callback->sc_next;
1250                 }
1251
1252                 op->o_callback = sc;
1253                 if ( rc != SLAP_CB_CONTINUE ) goto rel;
1254         }
1255
1256         Debug( LDAP_DEBUG_TRACE,
1257                 "=> send_search_reference: dn=\"%s\"\n",
1258                 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1259
1260         if (  rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1261                 ad_entry, NULL, ACL_READ, NULL ) )
1262         {
1263                 Debug( LDAP_DEBUG_ACL,
1264                         "send_search_reference: access to entry not allowed\n",
1265                     0, 0, 0 );
1266                 rc = 1;
1267                 goto rel;
1268         }
1269
1270         if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1271                 ad_ref, NULL, ACL_READ, NULL ) )
1272         {
1273                 Debug( LDAP_DEBUG_ACL,
1274                         "send_search_reference: access "
1275                         "to reference not allowed\n",
1276                     0, 0, 0 );
1277                 rc = 1;
1278                 goto rel;
1279         }
1280
1281         if( op->o_domain_scope ) {
1282                 Debug( LDAP_DEBUG_ANY,
1283                         "send_search_reference: domainScope control in (%s)\n", 
1284                         rs->sr_entry->e_dn, 0, 0 );
1285                 rc = 0;
1286                 goto rel;
1287         }
1288
1289         if( rs->sr_ref == NULL ) {
1290                 Debug( LDAP_DEBUG_ANY,
1291                         "send_search_reference: null ref in (%s)\n", 
1292                         rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1293                 rc = 1;
1294                 goto rel;
1295         }
1296
1297         if( op->o_protocol < LDAP_VERSION3 ) {
1298                 rc = 0;
1299                 /* save the references for the result */
1300                 if( rs->sr_ref[0].bv_val != NULL ) {
1301                         if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1302                                 rc = LDAP_OTHER;
1303                 }
1304                 goto rel;
1305         }
1306
1307 #ifdef LDAP_CONNECTIONLESS
1308         if( op->o_conn && op->o_conn->c_is_udp ) {
1309                 ber = op->o_res_ber;
1310         } else
1311 #endif
1312         {
1313                 ber_init_w_nullc( ber, LBER_USE_DER );
1314                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1315         }
1316
1317         rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1318                 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1319
1320         if( rc != -1 ) {
1321                 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1322         }
1323
1324         if( rc != -1 ) {
1325                 rc = ber_printf( ber, /*"{"*/ "N}" );
1326         }
1327
1328         if ( rc == -1 ) {
1329                 Debug( LDAP_DEBUG_ANY,
1330                         "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1331
1332 #ifdef LDAP_CONNECTIONLESS
1333                 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1334 #endif
1335                 ber_free_buf( ber );
1336                 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1337                 goto rel;
1338         }
1339
1340         rc = 0;
1341         if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1342                 be_entry_release_rw( op, rs->sr_entry, 0 );
1343                 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1344                 rs->sr_entry = NULL;
1345         }
1346
1347 #ifdef LDAP_CONNECTIONLESS
1348         if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1349 #endif
1350         bytes = send_ldap_ber( op->o_conn, ber );
1351         ber_free_buf( ber );
1352
1353         ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1354         ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1355         ldap_pvt_mp_add_ulong( slap_counters.sc_refs, 1 );
1356         ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1357         ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1358 #ifdef LDAP_CONNECTIONLESS
1359         }
1360 #endif
1361         if ( rs->sr_ref != NULL ) {
1362                 int     r;
1363
1364                 for ( r = 0; !BER_BVISNULL( &rs->sr_ref[ r ] ); r++ ) {
1365                         Statslog( LDAP_DEBUG_STATS2, "%s REF #%d \"%s\"\n",
1366                                 op->o_log_prefix, r, rs->sr_ref[0].bv_val,
1367                                 0, 0 );
1368                 }
1369
1370         } else {
1371                 Statslog( LDAP_DEBUG_STATS2, "%s REF \"(null)\"\n",
1372                         op->o_log_prefix, 0, 0, 0, 0 );
1373         }
1374
1375         Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1376
1377 rel:
1378         if ( op->o_callback ) {
1379                 slap_callback   *sc = op->o_callback, **sc_prev = &sc, *sc_next;
1380
1381                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1382                         sc_next = op->o_callback->sc_next;
1383                         if ( op->o_callback->sc_cleanup ) {
1384                                 slap_callback *sc2 = op->o_callback;
1385                                 (void)op->o_callback->sc_cleanup( op, rs );
1386                                 if ( op->o_callback != sc2 ) {
1387                                         *sc_prev = op->o_callback;
1388                                 }
1389                                 if ( rc != SLAP_CB_CONTINUE || !op->o_callback ) break;
1390                                 if ( op->o_callback != sc2 ) continue;
1391                         }
1392                         sc_prev = &op->o_callback->sc_next;
1393                 }
1394                 op->o_callback = sc;
1395         }
1396
1397         return rc;
1398 }
1399
1400 int
1401 str2result(
1402     char        *s,
1403     int         *code,
1404     char        **matched,
1405     char        **info )
1406 {
1407         int     rc;
1408         char    *c;
1409
1410         *code = LDAP_SUCCESS;
1411         *matched = NULL;
1412         *info = NULL;
1413
1414         if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1415                 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1416                     s, 0, 0 );
1417
1418                 return( -1 );
1419         }
1420
1421         rc = 0;
1422         while ( (s = strchr( s, '\n' )) != NULL ) {
1423                 *s++ = '\0';
1424                 if ( *s == '\0' ) {
1425                         break;
1426                 }
1427                 if ( (c = strchr( s, ':' )) != NULL ) {
1428                         c++;
1429                 }
1430
1431                 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1432                         if ( c != NULL && lutil_atoi( code, c ) != 0 ) {
1433                                 goto bailout;
1434                         }
1435                 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1436                         if ( c != NULL ) {
1437                                 *matched = c;
1438                         }
1439                 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1440                         if ( c != NULL ) {
1441                                 *info = c;
1442                         }
1443                 } else {
1444 bailout:;
1445                         Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1446                             s, 0, 0 );
1447
1448                         rc = -1;
1449                 }
1450         }
1451
1452         return( rc );
1453 }
1454
1455 int slap_read_controls(
1456         Operation *op,
1457         SlapReply *rs,
1458         Entry *e,
1459         const struct berval *oid,
1460         LDAPControl **ctrl )
1461 {
1462         int rc;
1463         struct berval bv;
1464         BerElementBuffer berbuf;
1465         BerElement *ber = (BerElement *) &berbuf;
1466         LDAPControl c;
1467         Operation myop;
1468
1469         Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1470                 oid->bv_val, e->e_dn, 0 );
1471
1472         rs->sr_entry = e;
1473         rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1474                 op->o_preread_attrs : op->o_postread_attrs; 
1475
1476         bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1477         bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1478
1479         ber_init2( ber, &bv, LBER_USE_DER );
1480         ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1481
1482         /* create new operation */
1483         myop = *op;
1484         myop.o_bd = NULL;
1485         myop.o_res_ber = ber;
1486         myop.o_callback = NULL;
1487
1488         rc = slap_send_search_entry( &myop, rs );
1489         if( rc ) return rc;
1490
1491         rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1492
1493         if( rc == LBER_ERROR ) return LDAP_OTHER;
1494
1495         c.ldctl_oid = oid->bv_val;
1496         c.ldctl_iscritical = 0;
1497
1498         if ( *ctrl == NULL ) {
1499                 /* first try */
1500                 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1501         } else {
1502                 /* retry: free previous try */
1503                 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1504         }
1505
1506         **ctrl = c;
1507         return LDAP_SUCCESS;
1508 }
1509
1510 /* Map API errors to protocol errors... */
1511 int
1512 slap_map_api2result( SlapReply *rs )
1513 {
1514         switch(rs->sr_err) {
1515         case LDAP_SERVER_DOWN:
1516                 return LDAP_UNAVAILABLE;
1517         case LDAP_LOCAL_ERROR:
1518                 return LDAP_OTHER;
1519         case LDAP_ENCODING_ERROR:
1520         case LDAP_DECODING_ERROR:
1521                 return LDAP_PROTOCOL_ERROR;
1522         case LDAP_TIMEOUT:
1523                 return LDAP_UNAVAILABLE;
1524         case LDAP_AUTH_UNKNOWN:
1525                 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1526         case LDAP_FILTER_ERROR:
1527                 rs->sr_text = "Filter error";
1528                 return LDAP_OTHER;
1529         case LDAP_USER_CANCELLED:
1530                 rs->sr_text = "User cancelled";
1531                 return LDAP_OTHER;
1532         case LDAP_PARAM_ERROR:
1533                 return LDAP_PROTOCOL_ERROR;
1534         case LDAP_NO_MEMORY:
1535                 return LDAP_OTHER;
1536         case LDAP_CONNECT_ERROR:
1537                 return LDAP_UNAVAILABLE;
1538         case LDAP_NOT_SUPPORTED:
1539                 return LDAP_UNWILLING_TO_PERFORM;
1540         case LDAP_CONTROL_NOT_FOUND:
1541                 return LDAP_PROTOCOL_ERROR;
1542         case LDAP_NO_RESULTS_RETURNED:
1543                 return LDAP_NO_SUCH_OBJECT;
1544         case LDAP_MORE_RESULTS_TO_RETURN:
1545                 rs->sr_text = "More results to return";
1546                 return LDAP_OTHER;
1547         case LDAP_CLIENT_LOOP:
1548         case LDAP_REFERRAL_LIMIT_EXCEEDED:
1549                 return LDAP_LOOP_DETECT;
1550         default:
1551                 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1552                 return rs->sr_err;
1553         }
1554 }
1555
1556
1557 slap_mask_t
1558 slap_attr_flags( AttributeName *an )
1559 {
1560         slap_mask_t     flags = SLAP_ATTRS_UNDEFINED;
1561
1562         if ( an == NULL ) {
1563                 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1564
1565         } else {
1566                 flags |= an_find( an, &AllOper )
1567                         ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1568                 flags |= an_find( an, &AllUser )
1569                         ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;
1570         }
1571
1572         return flags;
1573 }
1574