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