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