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