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