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