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