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