]> git.sur5r.net Git - openldap/blob - servers/slapd/result.c
group rewrite/map stuff in one structure and optimize more function calls
[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         ber_init_w_nullc( ber, LBER_USE_DER );
242
243 #ifdef NEW_LOGGING
244         LDAP_LOG( OPERATION, ENTRY, 
245                 "send_ldap_response:  msgid=%d tag=%lu err=%d\n",
246                 rs->sr_msgid, rs->sr_tag, rs->sr_err );
247 #else
248         Debug( LDAP_DEBUG_TRACE,
249                 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
250                 rs->sr_msgid, rs->sr_tag, rs->sr_err );
251 #endif
252
253         if( rs->sr_ref ) {
254 #ifdef NEW_LOGGING
255                 LDAP_LOG( OPERATION, ARGS, 
256                         "send_ldap_response: conn %lu  ref=\"%s\"\n",
257                         op->o_connid,
258                         rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL" , 0 );
259 #else
260                 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
261                         rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
262                         NULL, NULL );
263 #endif
264         }
265
266 #ifdef LDAP_CONNECTIONLESS
267         if (op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2) {
268                 rc = ber_printf( ber, "t{ess" /*"}}"*/,
269                         rs->sr_tag, rs->sr_err,
270                 rs->sr_matched == NULL ? "" : rs->sr_matched,
271                 rs->sr_text == NULL ? "" : rs->sr_text );
272         } else 
273 #endif
274         {
275             rc = ber_printf( ber, "{it{ess" /*"}}"*/,
276                 rs->sr_msgid, rs->sr_tag, rs->sr_err,
277                 rs->sr_matched == NULL ? "" : rs->sr_matched,
278                 rs->sr_text == NULL ? "" : rs->sr_text );
279         }
280
281         if( rc != -1 ) {
282                 if ( rs->sr_ref != NULL ) {
283                         assert( rs->sr_err == LDAP_REFERRAL );
284                         rc = ber_printf( ber, "t{W}",
285                                 LDAP_TAG_REFERRAL, rs->sr_ref );
286                 } else {
287                         assert( rs->sr_err != LDAP_REFERRAL );
288                 }
289         }
290
291         if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
292                 rc = ber_printf( ber, "tO",
293                         LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
294         }
295
296         if( rc != -1 && rs->sr_type == REP_EXTENDED ) {
297                 if ( rs->sr_rspoid != NULL ) {
298                         rc = ber_printf( ber, "ts",
299                                 LDAP_TAG_EXOP_RES_OID, rs->sr_rspoid );
300                 }
301                 if( rc != -1 && rs->sr_rspdata != NULL ) {
302                         rc = ber_printf( ber, "tO",
303                                 LDAP_TAG_EXOP_RES_VALUE, rs->sr_rspdata );
304                 }
305         }
306
307         if( rc != -1 ) {
308                 rc = ber_printf( ber, /*"{"*/ "N}" );
309         }
310
311         if( rc != -1 && rs->sr_ctrls != NULL ) {
312                 rc = send_ldap_controls( ber, rs->sr_ctrls );
313         }
314
315         if( rc != -1 ) {
316                 rc = ber_printf( ber, /*"{"*/ "N}" );
317         }
318
319 #ifdef LDAP_CONNECTIONLESS
320         if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2 && rc != -1 ) {
321                 rc = ber_printf( ber, /*"{"*/ "N}" );
322         }
323 #endif
324                 
325         if ( rc == -1 ) {
326 #ifdef NEW_LOGGING
327                 LDAP_LOG( OPERATION, ERR, 
328                         "send_ldap_response: conn %lu  ber_printf failed\n",
329                         op->o_connid, 0, 0 );
330 #else
331                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
332 #endif
333
334 #ifdef LDAP_CONNECTIONLESS
335                 if (!op->o_conn || op->o_conn->c_is_udp == 0)
336 #endif
337                 ber_free_buf( ber );
338                 return;
339         }
340
341         /* send BER */
342         bytes = send_ldap_ber( op->o_conn, ber );
343 #ifdef LDAP_CONNECTIONLESS
344         if (!op->o_conn || op->o_conn->c_is_udp == 0)
345 #endif
346         ber_free_buf( ber );
347
348         if ( bytes < 0 ) {
349 #ifdef NEW_LOGGING
350                 LDAP_LOG( OPERATION, ERR, 
351                         "send_ldap_response: conn %lu ber write failed\n",
352                         op->o_connid : 0, 0, 0 );
353 #else
354                 Debug( LDAP_DEBUG_ANY,
355                         "send_ldap_response: ber write failed\n",
356                         0, 0, 0 );
357 #endif
358
359                 return;
360         }
361
362 #ifdef LDAP_SLAPI
363         slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
364         slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED, ( rs->sr_matched != NULL ) ? (void *)ch_strdup( rs->sr_matched ) : NULL );
365         slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, ( rs->sr_text != NULL ) ? (void *)ch_strdup( rs->sr_text ) : NULL );
366 #endif /* LDAP_SLAPI */
367
368         ldap_pvt_thread_mutex_lock( &num_sent_mutex );
369         num_bytes_sent += bytes;
370         num_pdu_sent++;
371         ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
372         return;
373 }
374
375
376 void
377 send_ldap_disconnect( Operation *op, SlapReply *rs )
378 {
379 #define LDAP_UNSOLICITED_ERROR(e) \
380         (  (e) == LDAP_PROTOCOL_ERROR \
381         || (e) == LDAP_STRONG_AUTH_REQUIRED \
382         || (e) == LDAP_UNAVAILABLE )
383
384         assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
385
386         rs->sr_type = REP_EXTENDED;
387
388 #ifdef NEW_LOGGING
389         LDAP_LOG( OPERATION, ENTRY, 
390                 "send_ldap_disconnect: conn %lu  %d:%s\n",
391                 op->o_connid, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
392 #else
393         Debug( LDAP_DEBUG_TRACE,
394                 "send_ldap_disconnect %d:%s\n",
395                 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
396 #endif
397
398
399         if ( op->o_protocol < LDAP_VERSION3 ) {
400                 rs->sr_rspoid = NULL;
401                 rs->sr_tag = req2res( op->o_tag );
402                 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
403
404         } else {
405                 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
406                 rs->sr_tag = LDAP_RES_EXTENDED;
407                 rs->sr_msgid = 0;
408         }
409
410         send_ldap_response( op, rs );
411
412         Statslog( LDAP_DEBUG_STATS,
413             "conn=%lu op=%lu DISCONNECT tag=%lu err=%d text=%s\n",
414                 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
415 }
416
417 void
418 slap_send_ldap_result( Operation *op, SlapReply *rs )
419 {
420         char *tmp = NULL;
421         const char *otext = rs->sr_text;
422         BerVarray oref = rs->sr_ref;
423
424         rs->sr_type = REP_RESULT;
425
426         assert( !LDAP_API_ERROR( rs->sr_err ) && ( rs->sr_err >= 0 ));
427
428 #ifdef NEW_LOGGING
429         LDAP_LOG( OPERATION, ENTRY, 
430                 "send_ldap_result: conn %lu op=%lu p=%d\n",
431                 op->o_connid, op->o_opid, op->o_protocol );
432 #else
433         Debug( LDAP_DEBUG_TRACE,
434                 "send_ldap_result: conn=%lu op=%lu p=%d\n",
435                 op->o_connid, op->o_opid, op->o_protocol );
436 #endif
437
438 #ifdef NEW_LOGGING
439         LDAP_LOG( OPERATION, ARGS, 
440                 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
441                 rs->sr_err, rs->sr_matched ? rs->sr_matched : "", rs->sr_text ? rs->sr_text : "" );
442 #else
443         Debug( LDAP_DEBUG_ARGS,
444                 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
445                 rs->sr_err, rs->sr_matched ? rs->sr_matched : "", rs->sr_text ? rs->sr_text : "" );
446 #endif
447
448
449         if( rs->sr_ref ) {
450 #ifdef NEW_LOGGING
451                 LDAP_LOG( OPERATION, ARGS, 
452                         "send_ldap_result: referral=\"%s\"\n",
453                         rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL", 0, 0 );
454 #else
455                 Debug( LDAP_DEBUG_ARGS,
456                         "send_ldap_result: referral=\"%s\"\n",
457                         rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
458                         NULL, NULL );
459 #endif
460         }
461
462         assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
463
464         if ( rs->sr_err == LDAP_REFERRAL ) {
465 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
466                 if( op->o_domain_scope ) {
467                         rs->sr_ref = NULL;
468                 }
469 #endif
470                 if( rs->sr_ref == NULL ) {
471                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
472                 } else if ( op->o_protocol < LDAP_VERSION3 ) {
473                         rs->sr_err = LDAP_PARTIAL_RESULTS;
474                 }
475         }
476
477         if ( op->o_protocol < LDAP_VERSION3 ) {
478                 tmp = v2ref( rs->sr_ref, rs->sr_text );
479                 rs->sr_text = tmp;
480                 rs->sr_ref = NULL;
481         }
482
483         rs->sr_tag = req2res( op->o_tag );
484         rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
485
486         send_ldap_response( op, rs );
487
488         if ( op->o_tag == LDAP_REQ_SEARCH ) {
489                 char nbuf[64];
490                 snprintf( nbuf, sizeof nbuf, "%d nentries=%d", rs->sr_err, rs->sr_nentries );
491
492                 Statslog( LDAP_DEBUG_STATS,
493                         "conn=%lu op=%lu SEARCH RESULT tag=%lu err=%s text=%s\n",
494                         op->o_connid, op->o_opid, rs->sr_tag, nbuf, rs->sr_text ? rs->sr_text : "" );
495         } else {
496                 Statslog( LDAP_DEBUG_STATS,
497                         "conn=%lu op=%lu RESULT tag=%lu err=%d text=%s\n",
498                         op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
499         }
500
501         if( tmp != NULL ) {
502                 ch_free(tmp);
503         }
504         rs->sr_text = otext;
505         rs->sr_ref = oref;
506 }
507
508 void
509 send_ldap_sasl( Operation *op, SlapReply *rs )
510 {
511         rs->sr_type = REP_SASL;
512 #ifdef NEW_LOGGING
513         LDAP_LOG( OPERATION, ENTRY, 
514                 "send_ldap_sasl: conn %lu err=%d len=%lu\n",
515                 op->o_connid, rs->sr_err, rs->sr_sasldata ? rs->sr_sasldata->bv_len : -1 );
516 #else
517         Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
518                 rs->sr_err, rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
519 #endif
520
521         rs->sr_tag = req2res( op->o_tag );
522         rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
523
524         send_ldap_response( op, rs );
525 }
526
527 void
528 slap_send_ldap_extended( Operation *op, SlapReply *rs )
529 {
530         rs->sr_type = REP_EXTENDED;
531
532 #ifdef NEW_LOGGING
533         LDAP_LOG( OPERATION, ENTRY, 
534                 "send_ldap_extended: err=%d oid=%s len=%ld\n",
535                 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
536                 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
537 #else
538         Debug( LDAP_DEBUG_TRACE,
539                 "send_ldap_extended: err=%d oid=%s len=%ld\n",
540                 rs->sr_err,
541                 rs->sr_rspoid ? rs->sr_rspoid : "",
542                 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
543 #endif
544
545         rs->sr_tag = req2res( op->o_tag );
546         rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
547
548         send_ldap_response( op, rs );
549 }
550
551 #ifdef LDAP_RES_INTERMEDIATE_RESP
552 void
553 slap_send_ldap_intermediate_resp( Operation *op, SlapReply *rs )
554 {
555         rs->sr_type = REP_EXTENDED;
556 #ifdef NEW_LOGGING
557         LDAP_LOG( OPERATION, ENTRY,
558                 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
559                 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
560                 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
561 #else
562         Debug( LDAP_DEBUG_TRACE,
563                 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
564                 rs->sr_err,
565                 rs->sr_rspoid ? rs->sr_rspoid : "",
566                 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
567 #endif
568         rs->sr_tag = LDAP_RES_INTERMEDIATE_RESP;
569         rs->sr_msgid = op->o_msgid;
570         send_ldap_response( op, rs );
571 }
572 #endif
573
574 int
575 slap_send_search_entry( Operation *op, SlapReply *rs )
576 {
577         char berbuf[LBER_ELEMENT_SIZEOF];
578         BerElement      *ber = (BerElement *)berbuf;
579         Attribute       *a, *aa;
580         int             i, j, rc=-1, bytes;
581         char            *edn;
582         int             userattrs;
583         int             opattrs;
584         AccessControlState acl_state = ACL_STATE_INIT;
585 #ifdef LDAP_SLAPI
586         /* Support for computed attribute plugins */
587         computed_attr_context    ctx;
588         AttributeName   *anp;
589 #endif
590
591         AttributeDescription *ad_entry = slap_schema.si_ad_entry;
592
593         /* a_flags: array of flags telling if the i-th element will be
594          *          returned or filtered out
595          * e_flags: array of a_flags
596          */
597         char **e_flags = NULL;
598
599         rs->sr_type = REP_SEARCH;
600         if (op->o_callback && op->o_callback->sc_response) {
601                 return op->o_callback->sc_response( op, rs );
602         }
603
604 #ifdef NEW_LOGGING
605         LDAP_LOG( OPERATION, ENTRY, 
606                 "send_search_entry: conn %lu    dn=\"%s\"%s\n",
607                 op->o_connid, rs->sr_entry->e_name.bv_val, op->ors_attrsonly ? " (attrsOnly)" : "" );
608 #else
609         Debug( LDAP_DEBUG_TRACE,
610                 "=> send_search_entry: dn=\"%s\"%s\n",
611                 rs->sr_entry->e_name.bv_val, op->ors_attrsonly ? " (attrsOnly)" : "", 0 );
612 #endif
613
614         if ( ! access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL ) )
615         {
616 #ifdef NEW_LOGGING
617                 LDAP_LOG( ACL, INFO, 
618                         "send_search_entry: conn %lu access to entry (%s) not allowed\n", 
619                         op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
620 #else
621                 Debug( LDAP_DEBUG_ACL,
622                         "send_search_entry: access to entry not allowed\n",
623                     0, 0, 0 );
624 #endif
625
626                 return( 1 );
627         }
628
629         edn = rs->sr_entry->e_nname.bv_val;
630
631 #ifdef LDAP_CONNECTIONLESS
632         if (op->o_conn && op->o_conn->c_is_udp)
633             ber = op->o_res_ber;
634         else
635 #endif
636         ber_init_w_nullc( ber, LBER_USE_DER );
637
638 #ifdef LDAP_CONNECTIONLESS
639         if (op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2) {
640             rc = ber_printf(ber, "t{O{" /*}}*/,
641                 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name);
642         } else
643 #endif
644         {
645             rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
646                 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
647         }
648
649         if ( rc == -1 ) {
650 #ifdef NEW_LOGGING
651                 LDAP_LOG( OPERATION, ERR, 
652                         "send_search_entry: conn %lu  ber_printf failed\n", 
653                         op->o_connid, 0, 0 );
654 #else
655                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
656 #endif
657
658 #ifdef LDAP_CONNECTIONLESS
659                 if (!op->o_conn || op->o_conn->c_is_udp == 0)
660 #endif
661                 ber_free_buf( ber );
662                 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
663                 goto error_return;
664         }
665
666         /* check for special all user attributes ("*") type */
667         userattrs = ( rs->sr_attrs == NULL ) ? 1
668                 : an_find( rs->sr_attrs, &AllUser );
669
670         /* check for special all operational attributes ("+") type */
671         opattrs = ( rs->sr_attrs == NULL ) ? 0
672                 : an_find( rs->sr_attrs, &AllOper );
673
674         /* create an array of arrays of flags. Each flag corresponds
675          * to particular value of attribute and equals 1 if value matches
676          * to ValuesReturnFilter or 0 if not
677          */     
678         if ( op->vrFilter != NULL ) {
679                 int     k = 0;
680                 size_t  size;
681
682                 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
683                         for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
684                 }
685
686                 size = i * sizeof(char *) + k;
687                 if ( size > 0 ) {
688                         char    *a_flags;
689                         e_flags = SLAP_CALLOC ( 1, i * sizeof(char *) + k );
690                         if( e_flags == NULL ) {
691 #ifdef NEW_LOGGING
692                                 LDAP_LOG( OPERATION, ERR, 
693                                         "send_search_entry: conn %lu SLAP_CALLOC failed\n",
694                                         op->o_connid : 0, 0, 0 );
695 #else
696                         Debug( LDAP_DEBUG_ANY, 
697                                         "send_search_entry: SLAP_CALLOC failed\n", 0, 0, 0 );
698 #endif
699                                 ber_free( ber, 1 );
700         
701                                 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
702                                 goto error_return;
703                         }
704                         a_flags = (char *)(e_flags + i);
705                         memset( a_flags, 0, k );
706                         for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
707                                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
708                                 e_flags[i] = a_flags;
709                                 a_flags += j;
710                         }
711         
712                         rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ; 
713                         if ( rc == -1 ) {
714 #ifdef NEW_LOGGING
715                                 LDAP_LOG( OPERATION, ERR, 
716                                         "send_search_entry: conn %lu matched values filtering failed\n",
717                                         op->o_connid : 0, 0, 0 );
718 #else
719                         Debug( LDAP_DEBUG_ANY,
720                                         "matched values filtering failed\n", 0, 0, 0 );
721 #endif
722 #ifdef LDAP_CONNECTIONLESS
723                         if (!op->o_conn || op->o_conn->c_is_udp == 0)
724 #endif
725                                 ber_free( ber, 1 );
726                                 send_ldap_error( op, rs, LDAP_OTHER, "matched values filtering error" );
727                                 goto error_return;
728                         }
729                 }
730         }
731
732         for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
733                 AttributeDescription *desc = a->a_desc;
734
735                 if ( rs->sr_attrs == NULL ) {
736                         /* all attrs request, skip operational attributes */
737                         if( is_at_operational( desc->ad_type ) ) {
738                                 continue;
739                         }
740
741                 } else {
742                         /* specific attrs requested */
743                         if ( is_at_operational( desc->ad_type ) ) {
744                                 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
745                                         continue;
746                                 }
747
748                         } else {
749                                 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
750                                         continue;
751                                 }
752                         }
753                 }
754
755                 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
756                         ACL_READ, &acl_state ) )
757                 {
758 #ifdef NEW_LOGGING
759                         LDAP_LOG( ACL, INFO, 
760                                 "send_search_entry: conn %lu  access to attribute %s not "
761                                 "allowed\n", op->o_connid, desc->ad_cname.bv_val, 0 );
762 #else
763                         Debug( LDAP_DEBUG_ACL, "acl: "
764                                 "access to attribute %s not allowed\n",
765                             desc->ad_cname.bv_val, 0, 0 );
766 #endif
767                         continue;
768                 }
769
770                 if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
771 #ifdef NEW_LOGGING
772                         LDAP_LOG( OPERATION, ERR, 
773                                 "send_search_entry: conn %lu  ber_printf failed\n", 
774                                 op->o_connid, 0, 0 );
775 #else
776                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
777 #endif
778
779 #ifdef LDAP_CONNECTIONLESS
780                         if (!op->o_conn || op->o_conn->c_is_udp == 0)
781 #endif
782                         ber_free_buf( ber );
783                         send_ldap_error( op, rs, LDAP_OTHER, "encoding description error");
784                         goto error_return;
785                 }
786
787                 if ( ! op->ors_attrsonly ) {
788                         for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
789                                 if ( ! access_allowed( op, rs->sr_entry,
790                                         desc, &a->a_vals[i], ACL_READ, &acl_state ) )
791                                 {
792 #ifdef NEW_LOGGING
793                                         LDAP_LOG( ACL, INFO, 
794                                                 "send_search_entry: conn %lu "
795                                                 "access to attribute %s, value %d not allowed\n",
796                                                 op->o_connid, desc->ad_cname.bv_val, i );
797 #else
798                                         Debug( LDAP_DEBUG_ACL,
799                                                 "acl: access to attribute %s, "
800                                                 "value %d not allowed\n",
801                                                 desc->ad_cname.bv_val, i, 0 );
802 #endif
803
804                                         continue;
805                                 }
806
807                                 if ( op->vrFilter && e_flags[j][i] == 0 ){
808                                         continue;
809                                 }
810
811                                 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
812 #ifdef NEW_LOGGING
813                                         LDAP_LOG( OPERATION, ERR, 
814                                                 "send_search_entry: conn %lu  "
815                                                 "ber_printf failed.\n", op->o_connid, 0, 0 );
816 #else
817                                         Debug( LDAP_DEBUG_ANY,
818                                             "ber_printf failed\n", 0, 0, 0 );
819 #endif
820
821 #ifdef LDAP_CONNECTIONLESS
822                                         if (!op->o_conn || op->o_conn->c_is_udp == 0)
823 #endif
824                                         ber_free_buf( ber );
825                                         send_ldap_error( op, rs, LDAP_OTHER, "encoding values error" );
826                                         goto error_return;
827                                 }
828                         }
829                 }
830
831                 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
832 #ifdef NEW_LOGGING
833                         LDAP_LOG( OPERATION, ERR, 
834                                 "send_search_entry: conn %lu ber_printf failed\n", 
835                                 op->o_connid, 0, 0 );
836 #else
837                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
838 #endif
839
840 #ifdef LDAP_CONNECTIONLESS
841                         if (!op->o_conn || op->o_conn->c_is_udp == 0)
842 #endif
843                         ber_free_buf( ber );
844                         send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
845                         goto error_return;
846                 }
847         }
848
849         /* eventually will loop through generated operational attributes */
850         /* only have subschemaSubentry implemented */
851         aa = backend_operational( op, rs, opattrs );
852
853         if ( aa != NULL && op->vrFilter != NULL ) {
854                 int     k = 0;
855                 size_t  size;
856
857                 for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
858                         for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
859                 }
860
861                 size = i * sizeof(char *) + k;
862                 if ( size > 0 ) {
863                         char    *a_flags, **tmp;
864                 
865                         /*
866                          * Reuse previous memory - we likely need less space
867                          * for operational attributes
868                          */
869                         tmp = SLAP_REALLOC ( e_flags, i * sizeof(char *) + k );
870                         if ( tmp == NULL ) {
871 #ifdef NEW_LOGGING
872                                 LDAP_LOG( OPERATION, ERR, 
873                                         "send_search_entry: conn %lu "
874                                         "not enough memory "
875                                         "for matched values filtering\n", 
876                                         op->o_connid, 0, 0);
877 #else
878                                 Debug( LDAP_DEBUG_ANY,
879                                         "send_search_entry: conn %lu "
880                                         "not enough memory "
881                                         "for matched values filtering\n",
882                                         op->o_connid, 0, 0 );
883 #endif
884                                 ber_free( ber, 1 );
885         
886                                 send_ldap_error( op, rs, LDAP_OTHER, "not enough memory for matched values filtering" );
887                                 goto error_return;
888                         }
889                         e_flags = tmp;
890                         a_flags = (char *)(e_flags + i);
891                         memset( a_flags, 0, k );
892                         for ( a = aa, i=0; a != NULL; a = a->a_next, i++ ) {
893                                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
894                                 e_flags[i] = a_flags;
895                                 a_flags += j;
896                         }
897                         rc = filter_matched_values(op, aa, &e_flags) ; 
898                     
899                         if ( rc == -1 ) {
900 #ifdef NEW_LOGGING
901                                 LDAP_LOG( OPERATION, ERR, 
902                                         "send_search_entry: conn %lu "
903                                         "matched values filtering failed\n", 
904                                         op->o_connid : 0, 0, 0);
905 #else
906                                 Debug( LDAP_DEBUG_ANY,
907                                         "matched values filtering failed\n", 0, 0, 0 );
908 #endif
909 #ifdef LDAP_CONNECTIONLESS
910                         if (!op->o_conn || op->o_conn->c_is_udp == 0)
911 #endif
912                                 ber_free( ber, 1 );
913         
914                                 send_ldap_error( op, rs, LDAP_OTHER, "matched values filtering error" );
915                                 goto error_return;
916                         }
917                 }
918         }
919
920         for (a = aa, j=0; a != NULL; a = a->a_next, j++ ) {
921                 AttributeDescription *desc = a->a_desc;
922
923                 if ( rs->sr_attrs == NULL ) {
924                         /* all attrs request, skip operational attributes */
925                         if( is_at_operational( desc->ad_type ) ) {
926                                 continue;
927                         }
928
929                 } else {
930                         /* specific attrs requested */
931                         if( is_at_operational( desc->ad_type ) ) {
932                                 if( !opattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
933                                         continue;
934                                 }
935                         } else {
936                                 if (!userattrs && !ad_inlist( desc, rs->sr_attrs ) )
937                                 {
938                                         continue;
939                                 }
940                         }
941                 }
942
943                 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
944                         ACL_READ, &acl_state ) )
945                 {
946 #ifdef NEW_LOGGING
947                         LDAP_LOG( ACL, INFO, 
948                                 "send_search_entry: conn %lu "
949                                 "access to attribute %s not allowed\n",
950                                 op->o_connid, desc->ad_cname.bv_val, 0 );
951 #else
952                         Debug( LDAP_DEBUG_ACL, "send_search_entry: access to attribute %s "
953                                         "not allowed\n",
954                                         desc->ad_cname.bv_val, 0, 0 );
955 #endif
956
957                         continue;
958                 }
959
960                 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
961                 if ( rc == -1 ) {
962 #ifdef NEW_LOGGING
963                         LDAP_LOG( OPERATION, ERR, 
964                                 "send_search_entry: conn %lu  "
965                                 "ber_printf failed\n", op->o_connid, 0, 0 );
966 #else
967                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
968 #endif
969
970 #ifdef LDAP_CONNECTIONLESS
971                         if (!op->o_conn || op->o_conn->c_is_udp == 0)
972 #endif
973                         ber_free_buf( ber );
974                         send_ldap_error( op, rs, LDAP_OTHER, "encoding description error" );
975
976                         attrs_free( aa );
977                         goto error_return;
978                 }
979
980                 if ( ! op->ors_attrsonly ) {
981                         for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
982                                 if ( ! access_allowed( op, rs->sr_entry,
983                                         desc, &a->a_vals[i], ACL_READ, &acl_state ) )
984                                 {
985 #ifdef NEW_LOGGING
986                                         LDAP_LOG( ACL, INFO, 
987                                                 "send_search_entry: conn %lu "
988                                                 "access to %s, value %d not allowed\n",
989                                                 op->o_connid, desc->ad_cname.bv_val, i );
990 #else
991                                         Debug( LDAP_DEBUG_ACL,
992                                                 "send_search_entry: access to attribute %s, "
993                                                 "value %d not allowed\n",
994                                                 desc->ad_cname.bv_val, i, 0 );
995 #endif
996
997                                         continue;
998                                 }
999
1000                                 if ( op->vrFilter && e_flags[j][i] == 0 ){
1001                                         continue;
1002                                 }
1003
1004                                 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1005 #ifdef NEW_LOGGING
1006                                         LDAP_LOG( OPERATION, ERR, 
1007                                                 "send_search_entry: conn %lu  ber_printf failed\n", 
1008                                                 op->o_connid, 0, 0 );
1009 #else
1010                                         Debug( LDAP_DEBUG_ANY,
1011                                             "ber_printf failed\n", 0, 0, 0 );
1012 #endif
1013
1014 #ifdef LDAP_CONNECTIONLESS
1015                                         if (!op->o_conn || op->o_conn->c_is_udp == 0)
1016 #endif
1017                                         ber_free_buf( ber );
1018                                         send_ldap_error( op, rs, LDAP_OTHER, "encoding values error" );
1019                                         attrs_free( aa );
1020                                         goto error_return;
1021                                 }
1022                         }
1023                 }
1024
1025                 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1026 #ifdef NEW_LOGGING
1027                         LDAP_LOG( OPERATION, ERR, 
1028                                 "send_search_entry: conn %lu  ber_printf failed\n",
1029                                 op->o_connid, 0, 0 );
1030 #else
1031                         Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1032 #endif
1033
1034 #ifdef LDAP_CONNECTIONLESS
1035                         if (!op->o_conn || op->o_conn->c_is_udp == 0)
1036 #endif
1037                         ber_free_buf( ber );
1038                         send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1039
1040                         attrs_free( aa );
1041                         goto error_return;
1042                 }
1043         }
1044
1045 #ifdef LDAP_SLAPI
1046         /*
1047          * First, setup the computed attribute context that is
1048          * passed to all plugins.
1049          */
1050         ctx.cac_pb = op->o_pb;
1051         ctx.cac_attrs = rs->sr_attrs;
1052         ctx.cac_attrsonly = op->ors_attrsonly;
1053         ctx.cac_userattrs = userattrs;
1054         ctx.cac_opattrs = opattrs;
1055         ctx.cac_acl_state = acl_state;
1056         ctx.cac_private = (void *)ber;
1057
1058         /*
1059          * For each client requested attribute, call the plugins.
1060          */
1061         if ( rs->sr_attrs != NULL ) {
1062                 for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
1063                         rc = compute_evaluator( &ctx, anp->an_name.bv_val, rs->sr_entry, slapi_x_compute_output_ber );
1064                         if ( rc == 1 ) {
1065                                 break;
1066                         }
1067                 }
1068         } else {
1069                 /*
1070                  * Technically we shouldn't be returning operational attributes
1071                  * when the user requested only user attributes. We'll let the
1072                  * plugin decide whether to be naughty or not.
1073                  */
1074                 rc = compute_evaluator( &ctx, "*", rs->sr_entry, slapi_x_compute_output_ber );
1075         }
1076         if ( rc == 1 ) {
1077                 ber_free_buf( ber );
1078                 send_ldap_error( op, rs, LDAP_OTHER, "computed attribute error" );
1079                 goto error_return;
1080         }
1081 #endif /* LDAP_SLAPI */
1082
1083         /* free e_flags */
1084         if ( e_flags ) {
1085                 free( e_flags );
1086                 e_flags = NULL;
1087         }
1088
1089         attrs_free( aa );
1090         rc = ber_printf( ber, /*{{*/ "}N}" );
1091
1092         if( rc != -1 && rs->sr_ctrls != NULL ) {
1093                 rc = send_ldap_controls( ber, rs->sr_ctrls );
1094         }
1095
1096 #ifdef LDAP_CONNECTIONLESS
1097         if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2 ) {
1098                 ; /* empty, skip following if */
1099         } else
1100 #endif
1101         if( rc != -1 ) {
1102                 rc = ber_printf( ber, /*{*/ "N}" );
1103         }
1104
1105         if ( rc == -1 ) {
1106 #ifdef NEW_LOGGING
1107                 LDAP_LOG( OPERATION, ERR, 
1108                         "send_search_entry: conn %lu ber_printf failed\n", 
1109                         op->o_connid, 0, 0 );
1110 #else
1111                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1112 #endif
1113
1114 #ifdef LDAP_CONNECTIONLESS
1115                 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1116 #endif
1117                 ber_free_buf( ber );
1118                 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1119                 return( 1 );
1120         }
1121
1122 #ifdef LDAP_CONNECTIONLESS
1123         if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1124 #endif
1125         bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
1126         ber_free_buf( ber );
1127
1128         if ( bytes < 0 ) {
1129 #ifdef NEW_LOGGING
1130                 LDAP_LOG( OPERATION, ERR, 
1131                            "send_search_entry: conn %lu  ber write failed.\n", 
1132                            op->o_connid, 0, 0 );
1133 #else
1134                 Debug( LDAP_DEBUG_ANY,
1135                         "send_search_entry: ber write failed\n",
1136                         0, 0, 0 );
1137 #endif
1138
1139                 return -1;
1140         }
1141         rs->sr_nentries++;
1142
1143         ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1144         num_bytes_sent += bytes;
1145         num_entries_sent++;
1146         num_pdu_sent++;
1147         ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1148
1149 #ifdef LDAP_CONNECTIONLESS
1150         }
1151 #endif
1152
1153         Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu ENTRY dn=\"%s\"\n",
1154             op->o_connid, op->o_opid, rs->sr_entry->e_dn, 0, 0 );
1155
1156 #ifdef NEW_LOGGING
1157         LDAP_LOG( OPERATION, ENTRY, 
1158                 "send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1159 #else
1160         Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );
1161 #endif
1162
1163         rc = 0;
1164
1165 error_return:;
1166         if ( e_flags ) free( e_flags );
1167         return( rc );
1168 }
1169
1170 int
1171 slap_send_search_reference( Operation *op, SlapReply *rs )
1172 {
1173         char berbuf[LBER_ELEMENT_SIZEOF];
1174         BerElement      *ber = (BerElement *)berbuf;
1175         int rc;
1176         int bytes;
1177
1178         AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1179         AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1180
1181         rs->sr_type = REP_SEARCHREF;
1182         if (op->o_callback && op->o_callback->sc_response) {
1183                 return op->o_callback->sc_response( op, rs );
1184         }
1185
1186 #ifdef NEW_LOGGING
1187         LDAP_LOG( OPERATION, ENTRY, 
1188                 "send_search_reference: conn %lu  dn=\"%s\"\n", 
1189                 op->o_connid, rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0 );
1190 #else
1191         Debug( LDAP_DEBUG_TRACE,
1192                 "=> send_search_reference: dn=\"%s\"\n",
1193                 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1194 #endif
1195
1196         if (  rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1197                 ad_entry, NULL, ACL_READ, NULL ) )
1198         {
1199 #ifdef NEW_LOGGING
1200                 LDAP_LOG( ACL, INFO, 
1201                         "send_search_reference: conn %lu        "
1202                         "access to entry %s not allowed\n",
1203                         op->o_connid, rs->sr_entry->e_dn, 0 );
1204 #else
1205                 Debug( LDAP_DEBUG_ACL,
1206                         "send_search_reference: access to entry not allowed\n",
1207                     0, 0, 0 );
1208 #endif
1209
1210                 return( 1 );
1211         }
1212
1213         if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1214                 ad_ref, NULL, ACL_READ, NULL ) )
1215         {
1216 #ifdef NEW_LOGGING
1217                 LDAP_LOG( ACL, INFO, 
1218                         "send_search_reference: conn %lu access "
1219                         "to reference not allowed.\n", op->o_connid, 0, 0 );
1220 #else
1221                 Debug( LDAP_DEBUG_ACL,
1222                         "send_search_reference: access "
1223                         "to reference not allowed\n",
1224                     0, 0, 0 );
1225 #endif
1226
1227                 return( 1 );
1228         }
1229
1230 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1231         if( op->o_domain_scope ) {
1232 #ifdef NEW_LOGGING
1233                 LDAP_LOG( OPERATION, ERR, 
1234                         "send_search_reference: conn %lu domainScope control in (%s).\n",
1235                         op->o_connid, rs->sr_entry->e_dn, 0 );
1236 #else
1237                 Debug( LDAP_DEBUG_ANY,
1238                         "send_search_reference: domainScope control in (%s)\n", 
1239                         rs->sr_entry->e_dn, 0, 0 );
1240 #endif
1241
1242                 return( 0 );
1243         }
1244 #endif
1245
1246         if( rs->sr_ref == NULL ) {
1247 #ifdef NEW_LOGGING
1248                 LDAP_LOG( OPERATION, ERR, 
1249                         "send_search_reference: conn %lu null ref in (%s).\n",
1250                         op->o_connid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0 );
1251 #else
1252                 Debug( LDAP_DEBUG_ANY,
1253                         "send_search_reference: null ref in (%s)\n", 
1254                         rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1255 #endif
1256
1257                 return( 1 );
1258         }
1259
1260         if( op->o_protocol < LDAP_VERSION3 ) {
1261                 /* save the references for the result */
1262                 if( rs->sr_ref[0].bv_val != NULL ) {
1263                         if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1264                                 return LDAP_OTHER;
1265                 }
1266                 return 0;
1267         }
1268
1269 #ifdef LDAP_CONNECTIONLESS
1270         if (op->o_conn && op->o_conn->c_is_udp)
1271                 ber = op->o_res_ber;
1272         else
1273 #endif
1274         ber_init_w_nullc( ber, LBER_USE_DER );
1275
1276         rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1277                 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1278
1279         if( rc != -1 && rs->sr_ctrls != NULL ) {
1280                 rc = send_ldap_controls( ber, rs->sr_ctrls );
1281         }
1282
1283         if( rc != -1 ) {
1284                 rc = ber_printf( ber, /*"{"*/ "N}" );
1285         }
1286
1287         if ( rc == -1 ) {
1288 #ifdef NEW_LOGGING
1289                 LDAP_LOG( OPERATION, ERR, 
1290                         "send_search_reference: conn %lu        "
1291                         "ber_printf failed.\n", op->o_connid, 0, 0 );
1292 #else
1293                 Debug( LDAP_DEBUG_ANY,
1294                         "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1295 #endif
1296
1297 #ifdef LDAP_CONNECTIONLESS
1298                 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1299 #endif
1300                 ber_free_buf( ber );
1301                 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1302                 return -1;
1303         }
1304
1305 #ifdef LDAP_CONNECTIONLESS
1306         if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1307 #endif
1308         bytes = op->o_noop ? 0 : send_ldap_ber( op->o_conn, ber );
1309         ber_free_buf( ber );
1310
1311         ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1312         num_bytes_sent += bytes;
1313         num_refs_sent++;
1314         num_pdu_sent++;
1315         ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1316 #ifdef LDAP_CONNECTIONLESS
1317         }
1318 #endif
1319
1320         Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu REF dn=\"%s\"\n",
1321                 op->o_connid, op->o_opid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1322
1323 #ifdef NEW_LOGGING
1324         LDAP_LOG( OPERATION, ENTRY, 
1325                 "send_search_reference: conn %lu exit.\n", op->o_connid, 0, 0 );
1326 #else
1327         Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1328 #endif
1329
1330         return 0;
1331 }
1332
1333 int
1334 str2result(
1335     char        *s,
1336     int         *code,
1337     char        **matched,
1338     char        **info
1339 )
1340 {
1341         int     rc;
1342         char    *c;
1343
1344         *code = LDAP_SUCCESS;
1345         *matched = NULL;
1346         *info = NULL;
1347
1348         if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
1349 #ifdef NEW_LOGGING
1350                 LDAP_LOG( OPERATION, INFO, 
1351                         "str2result: (%s), expecting \"RESULT\"\n", s, 0, 0 );
1352 #else
1353                 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1354                     s, 0, 0 );
1355 #endif
1356
1357                 return( -1 );
1358         }
1359
1360         rc = 0;
1361         while ( (s = strchr( s, '\n' )) != NULL ) {
1362                 *s++ = '\0';
1363                 if ( *s == '\0' ) {
1364                         break;
1365                 }
1366                 if ( (c = strchr( s, ':' )) != NULL ) {
1367                         c++;
1368                 }
1369
1370                 if ( strncasecmp( s, "code", 4 ) == 0 ) {
1371                         if ( c != NULL ) {
1372                                 *code = atoi( c );
1373                         }
1374                 } else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
1375                         if ( c != NULL ) {
1376                                 *matched = c;
1377                         }
1378                 } else if ( strncasecmp( s, "info", 4 ) == 0 ) {
1379                         if ( c != NULL ) {
1380                                 *info = c;
1381                         }
1382                 } else {
1383 #ifdef NEW_LOGGING
1384                         LDAP_LOG( OPERATION, INFO, "str2result: (%s) unknown.\n", s, 0, 0 );
1385 #else
1386                         Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1387                             s, 0, 0 );
1388 #endif
1389
1390                         rc = -1;
1391                 }
1392         }
1393
1394         return( rc );
1395 }