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