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