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