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