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