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