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