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