]> git.sur5r.net Git - openldap/blob - servers/slapd/result.c
6cfa575586d6a93ca834748e3c554432fd2651c7
[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-2005 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
413                 && rc != -1 )
414         {
415                 rc = ber_printf( ber, /*"{"*/ "N}" );
416         }
417 #endif
418                 
419         if ( rc == -1 ) {
420 #ifdef NEW_LOGGING
421                 LDAP_LOG( OPERATION, ERR, 
422                         "send_ldap_response: conn %lu  ber_printf failed\n",
423                         op->o_connid, 0, 0 );
424 #else
425                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
426 #endif
427
428 #ifdef LDAP_CONNECTIONLESS
429                 if (!op->o_conn || op->o_conn->c_is_udp == 0)
430 #endif
431                 {
432                         ber_free_buf( ber );
433                 }
434                 goto cleanup;
435         }
436
437         /* send BER */
438         bytes = send_ldap_ber( op->o_conn, ber );
439 #ifdef LDAP_CONNECTIONLESS
440         if (!op->o_conn || op->o_conn->c_is_udp == 0)
441 #endif
442         {
443                 ber_free_buf( ber );
444         }
445
446         if ( bytes < 0 ) {
447 #ifdef NEW_LOGGING
448                 LDAP_LOG( OPERATION, ERR, 
449                         "send_ldap_response: conn %lu ber write failed\n",
450                         op->o_connid ? op->o_connid : 0, 0, 0 );
451 #else
452                 Debug( LDAP_DEBUG_ANY,
453                         "send_ldap_response: ber write failed\n",
454                         0, 0, 0 );
455 #endif
456
457                 goto cleanup;
458         }
459
460 #ifdef LDAP_SLAPI
461         if ( op->o_pb ) {
462                 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
463                 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED,
464                         (void *)rs->sr_matched );
465                 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, (void *)rs->sr_text );
466         }
467 #endif /* LDAP_SLAPI */
468
469         ldap_pvt_thread_mutex_lock( &num_sent_mutex );
470         num_bytes_sent += bytes;
471         num_pdu_sent++;
472         ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
473
474 cleanup:
475         /* Tell caller that we did this for real, as opposed to being
476          * overridden by a callback
477          */
478         rc = SLAP_CB_CONTINUE;
479
480 clean2:;
481         if ( op->o_callback ) {
482                 int             first = 1;
483                 slap_callback   *sc = op->o_callback, *sc_next;
484
485                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
486                         sc_next = op->o_callback->sc_next;
487                         if ( op->o_callback->sc_cleanup ) {
488                                 (void)op->o_callback->sc_cleanup( op, rs );
489                                 if ( first && op->o_callback != sc ) {
490                                         sc = op->o_callback;
491                                 }
492                         }
493                         first = 0;
494                 }
495                 op->o_callback = sc;
496         }
497
498         if ( rs->sr_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
499                 free( (char *)rs->sr_matched );
500                 rs->sr_matched = NULL;
501         }
502
503         if ( rs->sr_ref && rs->sr_flags & REP_REF_MUSTBEFREED ) {
504                 ber_bvarray_free( rs->sr_ref );
505                 rs->sr_ref = NULL;
506         }
507
508         return rc;
509 }
510
511
512 void
513 send_ldap_disconnect( Operation *op, SlapReply *rs )
514 {
515 #define LDAP_UNSOLICITED_ERROR(e) \
516         (  (e) == LDAP_PROTOCOL_ERROR \
517         || (e) == LDAP_STRONG_AUTH_REQUIRED \
518         || (e) == LDAP_UNAVAILABLE )
519
520         assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
521
522         rs->sr_type = REP_EXTENDED;
523
524 #ifdef NEW_LOGGING
525         LDAP_LOG( OPERATION, ENTRY, 
526                 "send_ldap_disconnect: conn %lu  %d:%s\n",
527                 op->o_connid, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
528 #else
529         Debug( LDAP_DEBUG_TRACE,
530                 "send_ldap_disconnect %d:%s\n",
531                 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
532 #endif
533
534         if ( op->o_protocol < LDAP_VERSION3 ) {
535                 rs->sr_rspoid = NULL;
536                 rs->sr_tag = req2res( op->o_tag );
537                 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
538
539         } else {
540                 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
541                 rs->sr_tag = LDAP_RES_EXTENDED;
542                 rs->sr_msgid = 0;
543         }
544
545         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
546                 Statslog( LDAP_DEBUG_STATS,
547                         "conn=%lu op=%lu DISCONNECT tag=%lu err=%d text=%s\n",
548                         op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err,
549                         rs->sr_text ? rs->sr_text : "" );
550         }
551 }
552
553 void
554 slap_send_ldap_result( Operation *op, SlapReply *rs )
555 {
556         char *tmp = NULL;
557         const char *otext = rs->sr_text;
558         BerVarray oref = rs->sr_ref;
559
560         rs->sr_type = REP_RESULT;
561
562         assert( !LDAP_API_ERROR( rs->sr_err ));
563
564 #ifdef NEW_LOGGING
565         LDAP_LOG( OPERATION, ENTRY, 
566                 "send_ldap_result: conn=%lu op=%lu p=%d\n",
567                 op->o_connid, op->o_opid, op->o_protocol );
568 #else
569         Debug( LDAP_DEBUG_TRACE,
570                 "send_ldap_result: conn=%lu op=%lu p=%d\n",
571                 op->o_connid, op->o_opid, op->o_protocol );
572 #endif
573
574 #ifdef NEW_LOGGING
575         LDAP_LOG( OPERATION, ARGS, 
576                 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
577                 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
578                 rs->sr_text ? rs->sr_text : "" );
579 #else
580         Debug( LDAP_DEBUG_ARGS,
581                 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
582                 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
583                 rs->sr_text ? rs->sr_text : "" );
584 #endif
585
586
587         if( rs->sr_ref ) {
588 #ifdef NEW_LOGGING
589                 LDAP_LOG( OPERATION, ARGS, 
590                         "send_ldap_result: referral=\"%s\"\n",
591                         rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL", 0, 0 );
592 #else
593                 Debug( LDAP_DEBUG_ARGS,
594                         "send_ldap_result: referral=\"%s\"\n",
595                         rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
596                         NULL, NULL );
597 #endif
598         }
599
600         assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
601
602         if ( rs->sr_err == LDAP_REFERRAL ) {
603 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
604                 if( op->o_domain_scope ) {
605                         rs->sr_ref = NULL;
606                 }
607 #endif
608                 if( rs->sr_ref == NULL ) {
609                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
610                 } else if ( op->o_protocol < LDAP_VERSION3 ) {
611                         rs->sr_err = LDAP_PARTIAL_RESULTS;
612                 }
613         }
614
615 #ifdef LDAP_SLAPI
616         /*
617          * Call pre-result plugins. To avoid infinite recursion plugins
618          * should just set SLAPI_RESULT_CODE rather than sending a
619          * result if they wish to change the result.
620          */
621         if ( op->o_callback == NULL && op->o_pb != NULL ) {
622                 slapi_int_pblock_set_operation( op->o_pb, op );
623                 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE,
624                         (void *)rs->sr_err );
625                 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT,
626                         (void *)rs->sr_text );
627                 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED,
628                         (void *)rs->sr_matched );
629
630                 (void) slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_RESULT_FN,
631                         op->o_pb );
632         }
633 #endif /* LDAP_SLAPI */
634
635         if ( op->o_protocol < LDAP_VERSION3 ) {
636                 tmp = v2ref( rs->sr_ref, rs->sr_text );
637                 rs->sr_text = tmp;
638                 rs->sr_ref = NULL;
639         }
640
641         rs->sr_tag = req2res( op->o_tag );
642         rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
643
644         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
645                 if ( op->o_tag == LDAP_REQ_SEARCH ) {
646                         char nbuf[64];
647                         snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
648                                 rs->sr_err, rs->sr_nentries );
649
650                         Statslog( LDAP_DEBUG_STATS,
651                         "conn=%lu op=%lu SEARCH RESULT tag=%lu err=%s text=%s\n",
652                                 op->o_connid, op->o_opid, rs->sr_tag, nbuf,
653                                 rs->sr_text ? rs->sr_text : "" );
654                 } else {
655                         Statslog( LDAP_DEBUG_STATS,
656                                 "conn=%lu op=%lu RESULT tag=%lu err=%d text=%s\n",
657                                 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err,
658                                 rs->sr_text ? rs->sr_text : "" );
659                 }
660         }
661
662         if( tmp != NULL ) ch_free(tmp);
663         rs->sr_text = otext;
664         rs->sr_ref = oref;
665 }
666
667 void
668 send_ldap_sasl( Operation *op, SlapReply *rs )
669 {
670         rs->sr_type = REP_SASL;
671 #ifdef NEW_LOGGING
672         LDAP_LOG( OPERATION, ENTRY, 
673                 "send_ldap_sasl: conn %lu err=%d len=%lu\n",
674                 op->o_connid, rs->sr_err,
675                 rs->sr_sasldata ? rs->sr_sasldata->bv_len : -1 );
676 #else
677         Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
678                 rs->sr_err,
679                 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
680 #endif
681
682         rs->sr_tag = req2res( op->o_tag );
683         rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
684
685         send_ldap_response( op, rs );
686 }
687
688 void
689 slap_send_ldap_extended( Operation *op, SlapReply *rs )
690 {
691         rs->sr_type = REP_EXTENDED;
692
693 #ifdef NEW_LOGGING
694         LDAP_LOG( OPERATION, ENTRY, 
695                 "send_ldap_extended: 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_extended: 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
706         rs->sr_tag = req2res( op->o_tag );
707         rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
708
709         send_ldap_response( op, rs );
710 }
711
712 void
713 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
714 {
715         rs->sr_type = REP_INTERMEDIATE;
716 #ifdef NEW_LOGGING
717         LDAP_LOG( OPERATION, ENTRY,
718                 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
719                 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
720                 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
721 #else
722         Debug( LDAP_DEBUG_TRACE,
723                 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
724                 rs->sr_err,
725                 rs->sr_rspoid ? rs->sr_rspoid : "",
726                 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
727 #endif
728         rs->sr_tag = LDAP_RES_INTERMEDIATE;
729         rs->sr_msgid = op->o_msgid;
730         send_ldap_response( op, rs );
731 }
732
733 int
734 slap_send_search_entry( Operation *op, SlapReply *rs )
735 {
736         BerElementBuffer berbuf;
737         BerElement      *ber = (BerElement *) &berbuf;
738         Attribute       *a, *aa;
739         int             i, j, rc=-1, bytes;
740         char            *edn;
741         int             userattrs;
742         int             opattrs;
743         AccessControlState acl_state = ACL_STATE_INIT;
744 #ifdef LDAP_SLAPI
745         /* Support for computed attribute plugins */
746         computed_attr_context    ctx;
747         AttributeName   *anp;
748 #endif
749         AttributeDescription *ad_entry = slap_schema.si_ad_entry;
750
751         /* a_flags: array of flags telling if the i-th element will be
752          *          returned or filtered out
753          * e_flags: array of a_flags
754          */
755         char **e_flags = NULL;
756
757         rs->sr_type = REP_SEARCH;
758         if (op->o_callback) {
759                 slap_callback *sc = op->o_callback;
760                 rc = SLAP_CB_CONTINUE;
761                 for ( ; op->o_callback; ) {
762                         if ( op->o_callback->sc_response ) {
763                                 rc = op->o_callback->sc_response( op, rs );
764                                 if ( rc != SLAP_CB_CONTINUE ) break;
765                         }
766                         op->o_callback = op->o_callback->sc_next;
767                 }
768                 op->o_callback = sc;
769                 if ( rc != SLAP_CB_CONTINUE ) goto error_return;
770         }
771
772 #ifdef NEW_LOGGING
773         LDAP_LOG( OPERATION, ENTRY, "send_search_entry: conn %lu        dn=\"%s\"%s\n",
774                 op->o_connid, rs->sr_entry->e_name.bv_val,
775                 op->ors_attrsonly ? " (attrsOnly)" : "" );
776 #else
777         Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: dn=\"%s\"%s\n",
778                 rs->sr_entry->e_name.bv_val,
779                 op->ors_attrsonly ? " (attrsOnly)" : "", 0 );
780 #endif
781
782         if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
783 #ifdef NEW_LOGGING
784                 LDAP_LOG( ACL, INFO, 
785                         "send_search_entry: conn %lu access to entry (%s) not allowed\n", 
786                         op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
787 #else
788                 Debug( LDAP_DEBUG_ACL,
789                         "send_search_entry: access to entry not allowed\n",
790                     0, 0, 0 );
791 #endif
792
793                 rc = 1;
794                 goto error_return;
795         }
796
797         edn = rs->sr_entry->e_nname.bv_val;
798
799         if ( op->o_res_ber ) {
800                 /* read back control or LDAP_CONNECTIONLESS */
801             ber = op->o_res_ber;
802         } else {
803                 ber_len_t       siz, len;
804                 struct berval   bv;
805
806                 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
807                 bv.bv_len = siz + len;
808                 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
809
810                 ber_init2( ber, &bv, LBER_USE_DER );
811                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
812         }
813
814 #ifdef LDAP_CONNECTIONLESS
815         if ( op->o_conn && op->o_conn->c_is_udp ) {
816                 /* CONNECTIONLESS */
817                 if ( op->o_protocol == LDAP_VERSION2 ) {
818                 rc = ber_printf(ber, "t{O{" /*}}*/,
819                                 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
820                 } else {
821                 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
822                                 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
823                 }
824         } else
825 #endif
826         if ( op->o_res_ber ) {
827                 /* read back control */
828             rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
829         } else {
830             rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
831                         LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
832         }
833
834         if ( rc == -1 ) {
835 #ifdef NEW_LOGGING
836                 LDAP_LOG( OPERATION, ERR, 
837                         "send_search_entry: conn %lu  ber_printf failed\n", 
838                         op->o_connid, 0, 0 );
839 #else
840                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
841 #endif
842
843                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
844                 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
845                 goto error_return;
846         }
847
848         /* check for special all user attributes ("*") type */
849         userattrs = ( rs->sr_attrs == NULL ) ? 1
850                 : an_find( rs->sr_attrs, &AllUser );
851
852         /* check for special all operational attributes ("+") type */
853         opattrs = ( rs->sr_attrs == NULL ) ? 0
854                 : an_find( rs->sr_attrs, &AllOper );
855
856         /* create an array of arrays of flags. Each flag corresponds
857          * to particular value of attribute and equals 1 if value matches
858          * to ValuesReturnFilter or 0 if not
859          */     
860         if ( op->o_vrFilter != NULL ) {
861                 int     k = 0;
862                 size_t  size;
863
864                 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
865                         for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
866                 }
867
868                 size = i * sizeof(char *) + k;
869                 if ( size > 0 ) {
870                         char    *a_flags;
871                         e_flags = sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
872                         if( e_flags == NULL ) {
873 #ifdef NEW_LOGGING
874                                 LDAP_LOG( OPERATION, ERR, 
875                                         "send_search_entry: conn %lu sl_calloc failed\n",
876                                         op->o_connid ? op->o_connid : 0, 0, 0 );
877 #else
878                         Debug( LDAP_DEBUG_ANY, 
879                                         "send_search_entry: sl_calloc failed\n", 0, 0, 0 );
880 #endif
881                                 ber_free( ber, 1 );
882         
883                                 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
884                                 goto error_return;
885                         }
886                         a_flags = (char *)(e_flags + i);
887                         memset( a_flags, 0, k );
888                         for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
889                                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
890                                 e_flags[i] = a_flags;
891                                 a_flags += j;
892                         }
893         
894                         rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ; 
895                         if ( rc == -1 ) {
896 #ifdef NEW_LOGGING
897                                 LDAP_LOG( OPERATION, ERR, "send_search_entry: "
898                                         "conn %lu matched values filtering failed\n",
899                                         op->o_connid ? op->o_connid : 0, 0, 0 );
900 #else
901                         Debug( LDAP_DEBUG_ANY,
902                                         "matched values filtering failed\n", 0, 0, 0 );
903 #endif
904                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
905                                 send_ldap_error( op, rs, LDAP_OTHER,
906                                         "matched values filtering error" );
907                                 goto error_return;
908                         }
909                 }
910         }
911
912         for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
913                 AttributeDescription *desc = a->a_desc;
914                 int finish = 0;
915
916                 if ( rs->sr_attrs == NULL ) {
917                         /* all attrs request, skip operational attributes */
918                         if( is_at_operational( desc->ad_type ) ) {
919                                 continue;
920                         }
921
922                 } else {
923                         /* specific attrs requested */
924                         if ( is_at_operational( desc->ad_type ) ) {
925                                 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
926                                         continue;
927                                 }
928
929                         } else {
930                                 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
931                                         continue;
932                                 }
933                         }
934                 }
935
936                 if ( op->ors_attrsonly ) {
937                         if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
938                                 ACL_READ, &acl_state ) )
939                         {
940 #ifdef NEW_LOGGING
941                                 LDAP_LOG( ACL, INFO, 
942                                         "send_search_entry: conn %lu access to attribute %s not "
943                                         "allowed\n", op->o_connid, desc->ad_cname.bv_val, 0 );
944 #else
945                                 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
946                                         "conn %lu access to attribute %s not allowed\n",
947                                         op->o_connid, desc->ad_cname.bv_val, 0 );
948 #endif
949                                 continue;
950                         }
951
952                         if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
953 #ifdef NEW_LOGGING
954                                 LDAP_LOG( OPERATION, ERR, 
955                                         "send_search_entry: conn %lu  ber_printf failed\n", 
956                                         op->o_connid, 0, 0 );
957 #else
958                                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
959 #endif
960
961                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
962                                 send_ldap_error( op, rs, LDAP_OTHER,
963                                         "encoding description error");
964                                 goto error_return;
965                         }
966                         finish = 1;
967
968                 } else {
969                         int first = 1;
970                         for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
971                                 if ( ! access_allowed( op, rs->sr_entry,
972                                         desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
973                                 {
974 #ifdef NEW_LOGGING
975                                         LDAP_LOG( ACL, INFO, 
976                                                 "send_search_entry: conn %lu "
977                                                 "access to attribute %s, value #%d not allowed\n",
978                                                 op->o_connid, desc->ad_cname.bv_val, i );
979 #else
980                                         Debug( LDAP_DEBUG_ACL,
981                                                 "send_search_entry: conn %lu "
982                                                 "access to attribute %s, value #%d not allowed\n",
983                                                 op->o_connid, desc->ad_cname.bv_val, i );
984 #endif
985
986                                         continue;
987                                 }
988
989                                 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
990                                         continue;
991                                 }
992
993                                 if ( first ) {
994                                         first = 0;
995                                         finish = 1;
996                                         if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
997 #ifdef NEW_LOGGING
998                                                 LDAP_LOG( OPERATION, ERR, 
999                                                         "send_search_entry: conn %lu  ber_printf failed\n", 
1000                                                         op->o_connid, 0, 0 );
1001 #else
1002                                                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1003 #endif
1004
1005                                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1006                                                 send_ldap_error( op, rs, LDAP_OTHER,
1007                                                         "encoding description error");
1008                                                 goto error_return;
1009                                         }
1010                                 }
1011                                 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1012 #ifdef NEW_LOGGING
1013                                         LDAP_LOG( OPERATION, ERR, 
1014                                                 "send_search_entry: conn %lu  "
1015                                                 "ber_printf failed.\n", op->o_connid, 0, 0 );
1016 #else
1017                                         Debug( LDAP_DEBUG_ANY,
1018                                             "ber_printf failed\n", 0, 0, 0 );
1019 #endif
1020
1021                                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1022                                         send_ldap_error( op, rs, LDAP_OTHER,
1023                                                 "encoding values error" );
1024                                         goto error_return;
1025                                 }
1026                         }
1027                 }
1028
1029                 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1030 #ifdef NEW_LOGGING
1031                         LDAP_LOG( OPERATION, ERR, 
1032                                 "send_search_entry: conn %lu ber_printf failed\n", 
1033                                 op->o_connid, 0, 0 );
1034 #else
1035                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1036 #endif
1037
1038                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1039                         send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1040                         goto error_return;
1041                 }
1042         }
1043
1044         /* eventually will loop through generated operational attributes */
1045         /* only have subschemaSubentry and numSubordinates are implemented */
1046         aa = backend_operational( op, rs, opattrs );
1047
1048         if ( aa != NULL && op->o_vrFilter != NULL ) {
1049                 int     k = 0;
1050                 size_t  size;
1051
1052                 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
1053                         for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1054                 }
1055
1056                 size = i * sizeof(char *) + k;
1057                 if ( size > 0 ) {
1058                         char    *a_flags, **tmp;
1059                 
1060                         /*
1061                          * Reuse previous memory - we likely need less space
1062                          * for operational attributes
1063                          */
1064                         tmp = sl_realloc( e_flags, i * sizeof(char *) + k,
1065                                 op->o_tmpmemctx );
1066                         if ( tmp == NULL ) {
1067 #ifdef NEW_LOGGING
1068                                 LDAP_LOG( OPERATION, ERR, 
1069                                         "send_search_entry: conn %lu "
1070                                         "not enough memory "
1071                                         "for matched values filtering\n", 
1072                                         op->o_connid, 0, 0);
1073 #else
1074                                 Debug( LDAP_DEBUG_ANY,
1075                                         "send_search_entry: conn %lu "
1076                                         "not enough memory "
1077                                         "for matched values filtering\n",
1078                                         op->o_connid, 0, 0 );
1079 #endif
1080                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1081                                 send_ldap_error( op, rs, LDAP_OTHER,
1082                                         "not enough memory for matched values filtering" );
1083                                 goto error_return;
1084                         }
1085                         e_flags = tmp;
1086                         a_flags = (char *)(e_flags + i);
1087                         memset( a_flags, 0, k );
1088                         for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
1089                                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1090                                 e_flags[i] = a_flags;
1091                                 a_flags += j;
1092                         }
1093                         rc = filter_matched_values(op, aa, &e_flags) ; 
1094                     
1095                         if ( rc == -1 ) {
1096 #ifdef NEW_LOGGING
1097                                 LDAP_LOG( OPERATION, ERR, 
1098                                         "send_search_entry: conn %lu "
1099                                         "matched values filtering failed\n", 
1100                                         op->o_connid ? op->o_connid : 0, 0, 0);
1101 #else
1102                                 Debug( LDAP_DEBUG_ANY,
1103                                         "matched values filtering failed\n", 0, 0, 0 );
1104 #endif
1105                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1106                                 send_ldap_error( op, rs, LDAP_OTHER,
1107                                         "matched values filtering error" );
1108                                 goto error_return;
1109                         }
1110                 }
1111         }
1112
1113         for (a = aa, j=0; a != NULL; a = a->a_next, j++ ) {
1114                 AttributeDescription *desc = a->a_desc;
1115
1116                 if ( rs->sr_attrs == NULL ) {
1117                         /* all attrs request, skip operational attributes */
1118                         if( is_at_operational( desc->ad_type ) ) {
1119                                 continue;
1120                         }
1121
1122                 } else {
1123                         /* specific attrs requested */
1124                         if( is_at_operational( desc->ad_type ) ) {
1125                                 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1126                                         continue;
1127                                 }
1128                         } else {
1129                                 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1130                                         continue;
1131                                 }
1132                         }
1133                 }
1134
1135                 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1136                         ACL_READ, &acl_state ) )
1137                 {
1138 #ifdef NEW_LOGGING
1139                         LDAP_LOG( ACL, INFO, 
1140                                 "send_search_entry: conn %lu "
1141                                 "access to attribute %s not allowed\n",
1142                                 op->o_connid, desc->ad_cname.bv_val, 0 );
1143 #else
1144                         Debug( LDAP_DEBUG_ACL,
1145                                 "send_search_entry: conn %lu "
1146                                 "access to attribute %s not allowed\n",
1147                                 op->o_connid, desc->ad_cname.bv_val, 0 );
1148 #endif
1149
1150                         continue;
1151                 }
1152
1153                 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1154                 if ( rc == -1 ) {
1155 #ifdef NEW_LOGGING
1156                         LDAP_LOG( OPERATION, ERR, 
1157                                 "send_search_entry: conn %lu  "
1158                                 "ber_printf failed\n", op->o_connid, 0, 0 );
1159 #else
1160                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1161 #endif
1162
1163                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1164                         send_ldap_error( op, rs, LDAP_OTHER,
1165                                 "encoding description error" );
1166                         attrs_free( aa );
1167                         goto error_return;
1168                 }
1169
1170                 if ( ! op->ors_attrsonly ) {
1171                         for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1172                                 if ( ! access_allowed( op, rs->sr_entry,
1173                                         desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1174                                 {
1175 #ifdef NEW_LOGGING
1176                                         LDAP_LOG( ACL, INFO, 
1177                                                 "send_search_entry: conn %lu "
1178                                                 "access to %s, value %d not allowed\n",
1179                                                 op->o_connid, desc->ad_cname.bv_val, i );
1180 #else
1181                                         Debug( LDAP_DEBUG_ACL,
1182                                                 "send_search_entry: access to attribute %s, "
1183                                                 "value %d not allowed\n",
1184                                                 desc->ad_cname.bv_val, i, 0 );
1185 #endif
1186
1187                                         continue;
1188                                 }
1189
1190                                 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1191                                         continue;
1192                                 }
1193
1194                                 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1195 #ifdef NEW_LOGGING
1196                                         LDAP_LOG( OPERATION, ERR, 
1197                                                 "send_search_entry: conn %lu  ber_printf failed\n", 
1198                                                 op->o_connid, 0, 0 );
1199 #else
1200                                         Debug( LDAP_DEBUG_ANY,
1201                                             "ber_printf failed\n", 0, 0, 0 );
1202 #endif
1203
1204                                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1205                                         send_ldap_error( op, rs, LDAP_OTHER,
1206                                                 "encoding values error" );
1207                                         attrs_free( aa );
1208                                         goto error_return;
1209                                 }
1210                         }
1211                 }
1212
1213                 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1214 #ifdef NEW_LOGGING
1215                         LDAP_LOG( OPERATION, ERR, 
1216                                 "send_search_entry: conn %lu  ber_printf failed\n",
1217                                 op->o_connid, 0, 0 );
1218 #else
1219                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1220 #endif
1221
1222                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1223                         send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1224                         attrs_free( aa );
1225                         goto error_return;
1226                 }
1227         }
1228
1229 #ifdef LDAP_SLAPI
1230         /*
1231          * First, setup the computed attribute context that is
1232          * passed to all plugins.
1233          */
1234         if ( op->o_pb ) {
1235                 ctx.cac_pb = op->o_pb;
1236                 ctx.cac_attrs = rs->sr_attrs;
1237                 ctx.cac_attrsonly = op->ors_attrsonly;
1238                 ctx.cac_userattrs = userattrs;
1239                 ctx.cac_opattrs = opattrs;
1240                 ctx.cac_acl_state = acl_state;
1241                 ctx.cac_private = (void *)ber;
1242
1243                 /*
1244                  * For each client requested attribute, call the plugins.
1245                  */
1246                 if ( rs->sr_attrs != NULL ) {
1247                         for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
1248                                 rc = compute_evaluator( &ctx, anp->an_name.bv_val,
1249                                         rs->sr_entry, slapi_int_compute_output_ber );
1250                                 if ( rc == 1 ) break;
1251                         }
1252                 } else {
1253                         /*
1254                          * Technically we shouldn't be returning operational attributes
1255                          * when the user requested only user attributes. We'll let the
1256                          * plugin decide whether to be naughty or not.
1257                          */
1258                         rc = compute_evaluator( &ctx, "*",
1259                                 rs->sr_entry, slapi_int_compute_output_ber );
1260                 }
1261                 if ( rc == 1 ) {
1262                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1263                         send_ldap_error( op, rs, LDAP_OTHER, "computed attribute error" );
1264                         goto error_return;
1265                 }
1266         }
1267 #endif /* LDAP_SLAPI */
1268
1269         /* free e_flags */
1270         if ( e_flags ) {
1271                 sl_free( e_flags, op->o_tmpmemctx );
1272                 e_flags = NULL;
1273         }
1274
1275         attrs_free( aa );
1276         rc = ber_printf( ber, /*{{*/ "}N}" );
1277
1278         if( rc != -1 ) {
1279                 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1280         }
1281
1282         if( rc != -1 ) {
1283 #ifdef LDAP_CONNECTIONLESS
1284                 if( op->o_conn && op->o_conn->c_is_udp ) {
1285                         if ( op->o_protocol != LDAP_VERSION2 ) {
1286                                 rc = ber_printf( ber, /*{*/ "N}" );
1287                         }
1288                 } else
1289 #endif
1290                 if ( op->o_res_ber == NULL ) {
1291                         rc = ber_printf( ber, /*{*/ "N}" );
1292                 }
1293         }
1294
1295         if ( rc == -1 ) {
1296 #ifdef NEW_LOGGING
1297                 LDAP_LOG( OPERATION, ERR, 
1298                         "send_search_entry: conn %lu ber_printf failed\n", 
1299                         op->o_connid, 0, 0 );
1300 #else
1301                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1302 #endif
1303
1304                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1305                 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1306                 rc = 1;
1307                 goto error_return;
1308         }
1309
1310         if ( op->o_res_ber == NULL ) {
1311                 bytes = send_ldap_ber( op->o_conn, ber );
1312                 ber_free_buf( ber );
1313
1314                 if ( bytes < 0 ) {
1315 #ifdef NEW_LOGGING
1316                         LDAP_LOG( OPERATION, ERR, 
1317                                 "send_search_entry: conn %lu  ber write failed.\n", 
1318                                 op->o_connid, 0, 0 );
1319 #else
1320                         Debug( LDAP_DEBUG_ANY,
1321                                 "send_search_entry: ber write failed\n",
1322                                 0, 0, 0 );
1323 #endif
1324
1325                         rc = -1;
1326                         goto error_return;
1327                 }
1328                 rs->sr_nentries++;
1329
1330                 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1331                 num_bytes_sent += bytes;
1332                 num_entries_sent++;
1333                 num_pdu_sent++;
1334                 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1335         }
1336
1337         Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu ENTRY dn=\"%s\"\n",
1338             op->o_connid, op->o_opid, rs->sr_entry->e_dn, 0, 0 );
1339
1340 #ifdef NEW_LOGGING
1341         LDAP_LOG( OPERATION, ENTRY, 
1342                 "send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1343 #else
1344         Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
1345 #endif
1346
1347         rc = 0;
1348
1349 error_return:;
1350         if ( op->o_callback ) {
1351                 int             first = 1;
1352                 slap_callback   *sc = op->o_callback, *sc_next;
1353
1354                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1355                         sc_next = op->o_callback->sc_next;
1356                         if ( op->o_callback->sc_cleanup ) {
1357                                 (void)op->o_callback->sc_cleanup( op, rs );
1358                                 if ( first && op->o_callback != sc ) {
1359                                         sc = op->o_callback;
1360                                 }
1361                         }
1362                         first = 0;
1363                 }
1364                 op->o_callback = sc;
1365         }
1366
1367         if ( e_flags ) {
1368                 slap_sl_free( e_flags, op->o_tmpmemctx );
1369         }
1370
1371         /* FIXME: I think rs->sr_type should be explicitly set to
1372          * REP_SEARCH here. That's what it was when we entered this
1373          * function. send_ldap_error may have changed it, but we
1374          * should set it back so that the cleanup functions know
1375          * what they're doing.
1376          */
1377         if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH 
1378                 && rs->sr_entry 
1379                 && (rs->sr_flags & REP_ENTRY_MUSTBEFREED) ) 
1380         {
1381                 entry_free( rs->sr_entry );
1382                 rs->sr_entry = NULL;
1383                 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1384         }
1385
1386         return( rc );
1387 }
1388
1389 int
1390 slap_send_search_reference( Operation *op, SlapReply *rs )
1391 {
1392         BerElementBuffer berbuf;
1393         BerElement      *ber = (BerElement *) &berbuf;
1394         int rc = 0;
1395         int bytes;
1396
1397         AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1398         AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1399
1400         rs->sr_type = REP_SEARCHREF;
1401         if (op->o_callback) {
1402                 slap_callback *sc = op->o_callback;
1403                 rc = SLAP_CB_CONTINUE;
1404                 for ( ; op->o_callback; ) {
1405                         if ( op->o_callback->sc_response ) {
1406                                 rc = op->o_callback->sc_response( op, rs );
1407                                 if ( rc != SLAP_CB_CONTINUE ) break;
1408                         }
1409                         op->o_callback = op->o_callback->sc_next;
1410                 }
1411                 op->o_callback = sc;
1412                 if ( rc != SLAP_CB_CONTINUE ) goto rel;
1413         }
1414
1415 #ifdef NEW_LOGGING
1416         LDAP_LOG( OPERATION, ENTRY, 
1417                 "send_search_reference: conn %lu  dn=\"%s\"\n", 
1418                 op->o_connid,
1419                 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0 );
1420 #else
1421         Debug( LDAP_DEBUG_TRACE,
1422                 "=> send_search_reference: dn=\"%s\"\n",
1423                 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1424 #endif
1425
1426         if (  rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1427                 ad_entry, NULL, ACL_READ, NULL ) )
1428         {
1429 #ifdef NEW_LOGGING
1430                 LDAP_LOG( ACL, INFO, 
1431                         "send_search_reference: conn %lu        "
1432                         "access to entry %s not allowed\n",
1433                         op->o_connid, rs->sr_entry->e_dn, 0 );
1434 #else
1435                 Debug( LDAP_DEBUG_ACL,
1436                         "send_search_reference: access to entry not allowed\n",
1437                     0, 0, 0 );
1438 #endif
1439                 rc = 1;
1440                 goto rel;
1441         }
1442
1443         if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1444                 ad_ref, NULL, ACL_READ, NULL ) )
1445         {
1446 #ifdef NEW_LOGGING
1447                 LDAP_LOG( ACL, INFO, 
1448                         "send_search_reference: conn %lu access "
1449                         "to reference not allowed.\n", op->o_connid, 0, 0 );
1450 #else
1451                 Debug( LDAP_DEBUG_ACL,
1452                         "send_search_reference: access "
1453                         "to reference not allowed\n",
1454                     0, 0, 0 );
1455 #endif
1456                 rc = 1;
1457                 goto rel;
1458         }
1459
1460 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1461         if( op->o_domain_scope ) {
1462 #ifdef NEW_LOGGING
1463                 LDAP_LOG( OPERATION, ERR, 
1464                         "send_search_reference: conn %lu domainScope control in (%s).\n",
1465                         op->o_connid, rs->sr_entry->e_dn, 0 );
1466 #else
1467                 Debug( LDAP_DEBUG_ANY,
1468                         "send_search_reference: domainScope control in (%s)\n", 
1469                         rs->sr_entry->e_dn, 0, 0 );
1470 #endif
1471                 rc = 0;
1472                 goto rel;
1473         }
1474 #endif
1475
1476         if( rs->sr_ref == NULL ) {
1477 #ifdef NEW_LOGGING
1478                 LDAP_LOG( OPERATION, ERR, 
1479                         "send_search_reference: conn %lu null ref in (%s).\n",
1480                         op->o_connid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0 );
1481 #else
1482                 Debug( LDAP_DEBUG_ANY,
1483                         "send_search_reference: null ref in (%s)\n", 
1484                         rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1485 #endif
1486                 rc = 1;
1487                 goto rel;
1488         }
1489
1490         if( op->o_protocol < LDAP_VERSION3 ) {
1491                 rc = 0;
1492                 /* save the references for the result */
1493                 if( rs->sr_ref[0].bv_val != NULL ) {
1494                         if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1495                                 rc = LDAP_OTHER;
1496                 }
1497                 goto rel;
1498         }
1499
1500 #ifdef LDAP_CONNECTIONLESS
1501         if( op->o_conn && op->o_conn->c_is_udp ) {
1502                 ber = op->o_res_ber;
1503         } else
1504 #endif
1505         {
1506                 ber_init_w_nullc( ber, LBER_USE_DER );
1507                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1508         }
1509
1510         rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1511                 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1512
1513         if( rc != -1 ) {
1514                 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1515         }
1516
1517         if( rc != -1 ) {
1518                 rc = ber_printf( ber, /*"{"*/ "N}" );
1519         }
1520
1521         if ( rc == -1 ) {
1522 #ifdef NEW_LOGGING
1523                 LDAP_LOG( OPERATION, ERR, 
1524                         "send_search_reference: conn %lu        "
1525                         "ber_printf failed.\n", op->o_connid, 0, 0 );
1526 #else
1527                 Debug( LDAP_DEBUG_ANY,
1528                         "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1529 #endif
1530
1531 #ifdef LDAP_CONNECTIONLESS
1532                 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1533 #endif
1534                 ber_free_buf( ber );
1535                 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1536                 goto rel;
1537         }
1538
1539         rc = 0;
1540 #ifdef LDAP_CONNECTIONLESS
1541         if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1542 #endif
1543         bytes = send_ldap_ber( op->o_conn, ber );
1544         ber_free_buf( ber );
1545
1546         ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1547         num_bytes_sent += bytes;
1548         num_refs_sent++;
1549         num_pdu_sent++;
1550         ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1551 #ifdef LDAP_CONNECTIONLESS
1552         }
1553 #endif
1554
1555         Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu REF dn=\"%s\"\n",
1556                 op->o_connid, op->o_opid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1557
1558 #ifdef NEW_LOGGING
1559         LDAP_LOG( OPERATION, ENTRY, 
1560                 "send_search_reference: conn %lu exit.\n", op->o_connid, 0, 0 );
1561 #else
1562         Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1563 #endif
1564
1565 rel:
1566         if ( op->o_callback ) {
1567                 int             first = 1;
1568                 slap_callback *sc = op->o_callback, *sc_next;
1569
1570                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next ) {
1571                         sc_next = op->o_callback->sc_next;
1572                         if ( op->o_callback->sc_cleanup ) {
1573                                 (void)op->o_callback->sc_cleanup( op, rs );
1574                                 if ( first && op->o_callback != sc ) {
1575                                         sc = op->o_callback;
1576                                 }
1577                         }
1578                         first = 0;
1579                 }
1580                 op->o_callback = sc;
1581         }
1582         return rc;
1583 }
1584
1585 int
1586 str2result(
1587     char        *s,
1588     int         *code,
1589     char        **matched,
1590     char        **info
1591 )
1592 {
1593         int     rc;
1594         char    *c;
1595
1596         *code = LDAP_SUCCESS;
1597         *matched = NULL;
1598         *info = NULL;
1599
1600         if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
1601 #ifdef NEW_LOGGING
1602                 LDAP_LOG( OPERATION, INFO, 
1603                         "str2result: (%s), expecting \"RESULT\"\n", s, 0, 0 );
1604 #else
1605                 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1606                     s, 0, 0 );
1607 #endif
1608
1609                 return( -1 );
1610         }
1611
1612         rc = 0;
1613         while ( (s = strchr( s, '\n' )) != NULL ) {
1614                 *s++ = '\0';
1615                 if ( *s == '\0' ) {
1616                         break;
1617                 }
1618                 if ( (c = strchr( s, ':' )) != NULL ) {
1619                         c++;
1620                 }
1621
1622                 if ( strncasecmp( s, "code", 4 ) == 0 ) {
1623                         if ( c != NULL ) {
1624                                 *code = atoi( c );
1625                         }
1626                 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
1627                         if ( c != NULL ) {
1628                                 *matched = c;
1629                         }
1630                 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
1631                         if ( c != NULL ) {
1632                                 *info = c;
1633                         }
1634                 } else {
1635 #ifdef NEW_LOGGING
1636                         LDAP_LOG( OPERATION, INFO, "str2result: (%s) unknown.\n", s, 0, 0 );
1637 #else
1638                         Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1639                             s, 0, 0 );
1640 #endif
1641
1642                         rc = -1;
1643                 }
1644         }
1645
1646         return( rc );
1647 }
1648
1649 int slap_read_controls(
1650         Operation *op,
1651         SlapReply *rs,
1652         Entry *e,
1653         const struct berval *oid,
1654         LDAPControl **ctrl )
1655 {
1656         int rc;
1657         struct berval bv;
1658         BerElementBuffer berbuf;
1659         BerElement *ber = (BerElement *) &berbuf;
1660         LDAPControl c;
1661         ber_len_t       siz, len;
1662         Operation myop;
1663
1664 #ifdef NEW_LOGGING
1665         LDAP_LOG( OPERATION, INFO, "slap_read_controls: (%s) %s\n",
1666                 oid->bv_val, e->e_dn, 0 );
1667 #else
1668         Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1669                 oid->bv_val, e->e_dn, 0 );
1670 #endif
1671
1672         rs->sr_entry = e;
1673         rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1674                 op->o_preread_attrs : op->o_postread_attrs; 
1675
1676         entry_flatsize( rs->sr_entry, &siz, &len, 0 );
1677         bv.bv_len = siz + len;
1678         bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1679
1680         ber_init2( ber, &bv, LBER_USE_DER );
1681         ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1682
1683         /* create new operation */
1684         myop = *op;
1685         myop.o_bd = NULL;
1686         myop.o_res_ber = ber;
1687
1688         rc = slap_send_search_entry( &myop, rs );
1689         if( rc ) return rc;
1690
1691         rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1692
1693         if( rc == LBER_ERROR ) return LDAP_OTHER;
1694
1695         c.ldctl_oid = oid->bv_val;
1696         c.ldctl_iscritical = 0;
1697
1698         if ( ctrl == NULL ) {
1699                 /* first try */
1700                 *ctrl = (LDAPControl *) sl_calloc( 1, sizeof(LDAPControl), NULL );
1701         } else {
1702                 /* retry: free previous try */
1703                 slap_sl_free( (*ctrl)->ldctl_value.bv_val, &op->o_tmpmemctx );
1704         }
1705
1706         **ctrl = c;
1707         return LDAP_SUCCESS;
1708 }
1709
1710 /* Map API errors to protocol errors... */
1711 int
1712 slap_map_api2result( SlapReply *rs )
1713 {
1714         switch(rs->sr_err) {
1715         case LDAP_SERVER_DOWN:
1716                 return LDAP_UNAVAILABLE;
1717         case LDAP_LOCAL_ERROR:
1718                 return LDAP_OTHER;
1719         case LDAP_ENCODING_ERROR:
1720         case LDAP_DECODING_ERROR:
1721                 return LDAP_PROTOCOL_ERROR;
1722         case LDAP_TIMEOUT:
1723                 return LDAP_UNAVAILABLE;
1724         case LDAP_AUTH_UNKNOWN:
1725                 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1726         case LDAP_FILTER_ERROR:
1727                 rs->sr_text = "Filter error";
1728                 return LDAP_OTHER;
1729         case LDAP_USER_CANCELLED:
1730                 rs->sr_text = "User cancelled";
1731                 return LDAP_OTHER;
1732         case LDAP_PARAM_ERROR:
1733                 return LDAP_PROTOCOL_ERROR;
1734         case LDAP_NO_MEMORY:
1735                 return LDAP_OTHER;
1736         case LDAP_CONNECT_ERROR:
1737                 return LDAP_UNAVAILABLE;
1738         case LDAP_NOT_SUPPORTED:
1739                 return LDAP_UNWILLING_TO_PERFORM;
1740         case LDAP_CONTROL_NOT_FOUND:
1741                 return LDAP_PROTOCOL_ERROR;
1742         case LDAP_NO_RESULTS_RETURNED:
1743                 return LDAP_NO_SUCH_OBJECT;
1744         case LDAP_MORE_RESULTS_TO_RETURN:
1745                 rs->sr_text = "More results to return";
1746                 return LDAP_OTHER;
1747         case LDAP_CLIENT_LOOP:
1748         case LDAP_REFERRAL_LIMIT_EXCEEDED:
1749                 return LDAP_LOOP_DETECT;
1750         default:
1751                 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1752                 return rs->sr_err;
1753         }
1754 }
1755