]> git.sur5r.net Git - openldap/blob - servers/slapd/result.c
zero out sml_managing any time a Modifications is built (use calloc?)
[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-2005 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 const struct berval slap_dummy_bv = BER_BVNULL;
41
42 int slap_null_cb( Operation *op, SlapReply *rs )
43 {
44         return 0;
45 }
46
47 int slap_freeself_cb( Operation *op, SlapReply *rs )
48 {
49         assert( op->o_callback != NULL );
50
51         op->o_tmpfree( op->o_callback, op->o_tmpmemctx );
52         op->o_callback = NULL;
53
54         return SLAP_CB_CONTINUE;
55 }
56
57 int slap_replog_cb( Operation *op, SlapReply *rs )
58 {
59         if ( rs->sr_err == LDAP_SUCCESS ) {
60                 replog( op );
61         }
62         return SLAP_CB_CONTINUE;
63 }
64
65 static char *v2ref( BerVarray ref, const char *text )
66 {
67         size_t len = 0, i = 0;
68         char *v2;
69
70         if(ref == NULL) {
71                 if (text) {
72                         return ch_strdup(text);
73                 } else {
74                         return NULL;
75                 }
76         }
77         
78         if ( text != NULL ) {
79                 len = strlen( text );
80                 if (text[len-1] != '\n') {
81                     i = 1;
82                 }
83         }
84
85         v2 = SLAP_MALLOC( len+i+sizeof("Referral:") );
86         if( v2 == NULL ) {
87                 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
88                 return NULL;
89         }
90
91         if( text != NULL ) {
92                 strcpy(v2, text);
93                 if( i ) {
94                         v2[len++] = '\n';
95                 }
96         }
97         strcpy( v2+len, "Referral:" );
98         len += sizeof("Referral:");
99
100         for( i=0; ref[i].bv_val != NULL; i++ ) {
101                 v2 = SLAP_REALLOC( v2, len + ref[i].bv_len + 1 );
102                 if( v2 == NULL ) {
103                         Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
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                 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
189                     err, sock_errstr(err), 0 );
190
191                 if ( err != EWOULDBLOCK && err != EAGAIN ) {
192                         connection_closing( conn, "connection lost on write" );
193
194                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
195                         ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
196
197                         return( -1 );
198                 }
199
200                 /* wait for socket to be write-ready */
201                 conn->c_writewaiter = 1;
202                 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
203                 slapd_set_write( sd, 1 );
204
205                 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
206                 conn->c_writewaiter = 0;
207         }
208
209         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
210         ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
211
212         return bytes;
213 }
214
215 static int
216 send_ldap_control( BerElement *ber, LDAPControl *c )
217 {
218         int rc;
219
220         assert( c != NULL );
221
222         rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
223
224         if( c->ldctl_iscritical ) {
225                 rc = ber_printf( ber, "b",
226                         (ber_int_t) c->ldctl_iscritical ) ;
227                 if( rc == -1 ) return rc;
228         }
229
230         if( c->ldctl_value.bv_val != NULL ) {
231                 rc = ber_printf( ber, "O", &c->ldctl_value ); 
232                 if( rc == -1 ) return rc;
233         }
234
235         rc = ber_printf( ber, /*{*/"N}" );
236         if( rc == -1 ) return rc;
237
238         return 0;
239 }
240
241 static int
242 send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
243 {
244         int rc;
245
246         if( c == NULL )
247                 return 0;
248
249         rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
250         if( rc == -1 ) return rc;
251
252         for( ; *c != NULL; c++) {
253                 rc = send_ldap_control( ber, *c );
254                 if( rc == -1 ) return rc;
255         }
256
257 #ifdef LDAP_DEVEL
258         /* this is a hack to avoid having to modify op->s_ctrls */
259         if( o->o_sortedresults ) {
260                 BerElementBuffer berbuf;
261                 BerElement *sber = (BerElement *) &berbuf;
262                 LDAPControl sorted;
263                 BER_BVZERO( &sorted.ldctl_value );
264                 sorted.ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
265                 sorted.ldctl_iscritical = 0;
266
267                 ber_init2( sber, NULL, LBER_USE_DER );
268
269                 ber_printf( sber, "{e}", LDAP_UNWILLING_TO_PERFORM );
270
271                 if( ber_flatten2( ber, &sorted.ldctl_value, 0 ) == -1 ) {
272                         return -1;
273                 }
274
275                 (void) ber_free_buf( ber );
276
277                 rc = send_ldap_control( ber, &sorted );
278                 if( rc == -1 ) return rc;
279         }
280 #endif
281
282         rc = ber_printf( ber, /*{*/"N}" );
283
284         return rc;
285 }
286
287 static int
288 send_ldap_response(
289         Operation *op,
290         SlapReply *rs )
291 {
292         BerElementBuffer berbuf;
293         BerElement      *ber = (BerElement *) &berbuf;
294         int             rc = LDAP_SUCCESS;
295         long    bytes;
296
297         if ( rs->sr_err == SLAPD_ABANDON ) {
298                 rc = SLAPD_ABANDON;
299                 goto clean2;
300         }
301
302         if ( op->o_callback ) {
303                 int             first = 1;
304                 slap_callback   *sc = op->o_callback,
305                                 *sc_next = op->o_callback;
306
307                 rc = SLAP_CB_CONTINUE;
308                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
309                         sc_next = op->o_callback->sc_next;
310                         if ( op->o_callback->sc_response ) {
311                                 rc = op->o_callback->sc_response( op, rs );
312                                 if ( first && op->o_callback == NULL ) {
313                                         sc = NULL;
314                                 }
315                                 if ( rc != SLAP_CB_CONTINUE ) break;
316                         }
317                         first = 0;
318                 }
319
320                 op->o_callback = sc;
321                 if ( rc != SLAP_CB_CONTINUE ) goto clean2;
322         }
323
324 #ifdef LDAP_CONNECTIONLESS
325         if (op->o_conn && op->o_conn->c_is_udp)
326                 ber = op->o_res_ber;
327         else
328 #endif
329         {
330                 ber_init_w_nullc( ber, LBER_USE_DER );
331                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
332         }
333
334         Debug( LDAP_DEBUG_TRACE,
335                 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
336                 rs->sr_msgid, rs->sr_tag, rs->sr_err );
337
338         if( rs->sr_ref ) {
339                 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
340                         rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
341                         NULL, NULL );
342         }
343
344 #ifdef LDAP_CONNECTIONLESS
345         if (op->o_conn && op->o_conn->c_is_udp &&
346                 op->o_protocol == LDAP_VERSION2 )
347         {
348                 rc = ber_printf( ber, "t{ess" /*"}"*/,
349                         rs->sr_tag, rs->sr_err,
350                 rs->sr_matched == NULL ? "" : rs->sr_matched,
351                 rs->sr_text == NULL ? "" : rs->sr_text );
352         } else 
353 #endif
354         if ( rs->sr_type == REP_INTERMEDIATE ) {
355             rc = ber_printf( ber, "{it{" /*"}}"*/,
356                         rs->sr_msgid, rs->sr_tag );
357
358         } else {
359             rc = ber_printf( ber, "{it{ess" /*"}}"*/,
360                 rs->sr_msgid, rs->sr_tag, rs->sr_err,
361                 rs->sr_matched == NULL ? "" : rs->sr_matched,
362                 rs->sr_text == NULL ? "" : rs->sr_text );
363         }
364
365         if( rc != -1 ) {
366                 if ( rs->sr_ref != NULL ) {
367                         assert( rs->sr_err == LDAP_REFERRAL );
368                         rc = ber_printf( ber, "t{W}",
369                                 LDAP_TAG_REFERRAL, rs->sr_ref );
370                 } else {
371                         assert( rs->sr_err != LDAP_REFERRAL );
372                 }
373         }
374
375         if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
376                 rc = ber_printf( ber, "tO",
377                         LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
378         }
379
380         if( rc != -1 &&
381                 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
382         {
383                 if ( rs->sr_rspoid != NULL ) {
384                         rc = ber_printf( ber, "ts",
385                                 rs->sr_type == REP_EXTENDED
386                                         ? LDAP_TAG_EXOP_RES_OID : LDAP_TAG_IM_RES_OID,
387                                 rs->sr_rspoid );
388                 }
389                 if( rc != -1 && rs->sr_rspdata != NULL ) {
390                         rc = ber_printf( ber, "tO",
391                                 rs->sr_type == REP_EXTENDED
392                                         ? LDAP_TAG_EXOP_RES_VALUE : LDAP_TAG_IM_RES_VALUE,
393                                 rs->sr_rspdata );
394                 }
395         }
396
397         if( rc != -1 ) {
398                 rc = ber_printf( ber, /*"{"*/ "N}" );
399         }
400
401         if( rc != -1 ) {
402                 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
403         }
404
405         if( rc != -1 ) {
406                 rc = ber_printf( ber, /*"{"*/ "N}" );
407         }
408
409 #ifdef LDAP_CONNECTIONLESS
410         if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
411                 && rc != -1 )
412         {
413                 rc = ber_printf( ber, /*"{"*/ "N}" );
414         }
415 #endif
416                 
417         if ( rc == -1 ) {
418                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
419
420 #ifdef LDAP_CONNECTIONLESS
421                 if (!op->o_conn || op->o_conn->c_is_udp == 0)
422 #endif
423                 {
424                         ber_free_buf( ber );
425                 }
426                 goto cleanup;
427         }
428
429         /* send BER */
430         bytes = send_ldap_ber( op->o_conn, ber );
431 #ifdef LDAP_CONNECTIONLESS
432         if (!op->o_conn || op->o_conn->c_is_udp == 0)
433 #endif
434         {
435                 ber_free_buf( ber );
436         }
437
438         if ( bytes < 0 ) {
439                 Debug( LDAP_DEBUG_ANY,
440                         "send_ldap_response: ber write failed\n",
441                         0, 0, 0 );
442
443                 goto cleanup;
444         }
445
446         ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
447         ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
448         ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
449         ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
450
451 cleanup:;
452         /* Tell caller that we did this for real, as opposed to being
453          * overridden by a callback
454          */
455         rc = SLAP_CB_CONTINUE;
456
457 clean2:;
458         if ( op->o_callback ) {
459                 int             first = 1;
460                 slap_callback   *sc = op->o_callback, *sc_next;
461
462                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
463                         sc_next = op->o_callback->sc_next;
464                         if ( op->o_callback->sc_cleanup ) {
465                                 (void)op->o_callback->sc_cleanup( op, rs );
466                                 if ( first && op->o_callback != sc ) {
467                                         sc = op->o_callback;
468                                 }
469                         }
470                         first = 0;
471                 }
472                 op->o_callback = sc;
473         }
474
475
476         if ( rs->sr_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
477                 free( (char *)rs->sr_matched );
478                 rs->sr_matched = NULL;
479         }
480
481         if ( rs->sr_ref && rs->sr_flags & REP_REF_MUSTBEFREED ) {
482                 ber_bvarray_free( rs->sr_ref );
483                 rs->sr_ref = NULL;
484         }
485
486         return rc;
487 }
488
489
490 void
491 send_ldap_disconnect( Operation *op, SlapReply *rs )
492 {
493 #define LDAP_UNSOLICITED_ERROR(e) \
494         (  (e) == LDAP_PROTOCOL_ERROR \
495         || (e) == LDAP_STRONG_AUTH_REQUIRED \
496         || (e) == LDAP_UNAVAILABLE )
497
498         assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
499
500         rs->sr_type = REP_EXTENDED;
501
502         Debug( LDAP_DEBUG_TRACE,
503                 "send_ldap_disconnect %d:%s\n",
504                 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
505
506         if ( op->o_protocol < LDAP_VERSION3 ) {
507                 rs->sr_rspoid = NULL;
508                 rs->sr_tag = req2res( op->o_tag );
509                 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
510
511         } else {
512                 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
513                 rs->sr_tag = LDAP_RES_EXTENDED;
514                 rs->sr_msgid = 0;
515         }
516
517         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
518                 Statslog( LDAP_DEBUG_STATS,
519                         "%s DISCONNECT tag=%lu err=%d text=%s\n",
520                         op->o_log_prefix, rs->sr_tag, rs->sr_err,
521                         rs->sr_text ? rs->sr_text : "", 0 );
522         }
523 }
524
525 void
526 slap_send_ldap_result( Operation *op, SlapReply *rs )
527 {
528         char *tmp = NULL;
529         const char *otext = rs->sr_text;
530         BerVarray oref = rs->sr_ref;
531
532         rs->sr_type = REP_RESULT;
533
534         /* Propagate Abandons so that cleanup callbacks can be processed */
535         if ( rs->sr_err == SLAPD_ABANDON )
536                 goto abandon;
537
538         assert( !LDAP_API_ERROR( rs->sr_err ) );
539
540         Debug( LDAP_DEBUG_TRACE,
541                 "send_ldap_result: %s p=%d\n",
542                 op->o_log_prefix, op->o_protocol, 0 );
543
544         Debug( LDAP_DEBUG_ARGS,
545                 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
546                 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
547                 rs->sr_text ? rs->sr_text : "" );
548
549
550         if( rs->sr_ref ) {
551                 Debug( LDAP_DEBUG_ARGS,
552                         "send_ldap_result: referral=\"%s\"\n",
553                         rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
554                         NULL, NULL );
555         }
556
557         assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
558
559         if ( rs->sr_err == LDAP_REFERRAL ) {
560 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
561                 if( op->o_domain_scope ) {
562                         rs->sr_ref = NULL;
563                 }
564 #endif
565                 if( rs->sr_ref == NULL ) {
566                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
567                 } else if ( op->o_protocol < LDAP_VERSION3 ) {
568                         rs->sr_err = LDAP_PARTIAL_RESULTS;
569                 }
570         }
571
572         if ( op->o_protocol < LDAP_VERSION3 ) {
573                 tmp = v2ref( rs->sr_ref, rs->sr_text );
574                 rs->sr_text = tmp;
575                 rs->sr_ref = NULL;
576         }
577
578         rs->sr_tag = req2res( op->o_tag );
579         rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
580
581 abandon:
582         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
583                 if ( op->o_tag == LDAP_REQ_SEARCH ) {
584                         char nbuf[64];
585                         snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
586                                 rs->sr_err, rs->sr_nentries );
587
588                         Statslog( LDAP_DEBUG_STATS,
589                         "%s SEARCH RESULT tag=%lu err=%s text=%s\n",
590                                 op->o_log_prefix, rs->sr_tag, nbuf,
591                                 rs->sr_text ? rs->sr_text : "", 0 );
592                 } else {
593                         Statslog( LDAP_DEBUG_STATS,
594                                 "%s RESULT tag=%lu err=%d text=%s\n",
595                                 op->o_log_prefix, rs->sr_tag, rs->sr_err,
596                                 rs->sr_text ? rs->sr_text : "", 0 );
597                 }
598         }
599
600         if( tmp != NULL ) ch_free(tmp);
601         rs->sr_text = otext;
602         rs->sr_ref = oref;
603 }
604
605 void
606 send_ldap_sasl( Operation *op, SlapReply *rs )
607 {
608         rs->sr_type = REP_SASL;
609         Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
610                 rs->sr_err,
611                 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
612
613         rs->sr_tag = req2res( op->o_tag );
614         rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
615
616         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
617                 Statslog( LDAP_DEBUG_STATS,
618                         "%s RESULT tag=%lu err=%d text=%s\n",
619                         op->o_log_prefix, rs->sr_tag, rs->sr_err,
620                         rs->sr_text ? rs->sr_text : "", 0 );
621         }
622 }
623
624 void
625 slap_send_ldap_extended( Operation *op, SlapReply *rs )
626 {
627         rs->sr_type = REP_EXTENDED;
628
629         Debug( LDAP_DEBUG_TRACE,
630                 "send_ldap_extended: err=%d oid=%s len=%ld\n",
631                 rs->sr_err,
632                 rs->sr_rspoid ? rs->sr_rspoid : "",
633                 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
634
635         rs->sr_tag = req2res( op->o_tag );
636         rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
637
638         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
639                 Statslog( LDAP_DEBUG_STATS,
640                         "%s RESULT oid=%s err=%d text=%s\n",
641                         op->o_log_prefix, rs->sr_rspoid ? rs->sr_rspoid : "",
642                         rs->sr_err, rs->sr_text ? rs->sr_text : "", 0 );
643         }
644 }
645
646 void
647 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
648 {
649         rs->sr_type = REP_INTERMEDIATE;
650         Debug( LDAP_DEBUG_TRACE,
651                 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
652                 rs->sr_err,
653                 rs->sr_rspoid ? rs->sr_rspoid : "",
654                 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
655         rs->sr_tag = LDAP_RES_INTERMEDIATE;
656         rs->sr_msgid = op->o_msgid;
657         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
658                 Statslog( LDAP_DEBUG_STATS2,
659                         "%s INTERM oid=%s\n",
660                         op->o_log_prefix,
661                         rs->sr_rspoid ? rs->sr_rspoid : "", 0, 0, 0 );
662         }
663 }
664
665 int
666 slap_send_search_entry( Operation *op, SlapReply *rs )
667 {
668         BerElementBuffer berbuf;
669         BerElement      *ber = (BerElement *) &berbuf;
670         Attribute       *a;
671         int             i, j, rc=-1, bytes;
672         char            *edn;
673         int             userattrs;
674         AccessControlState acl_state = ACL_STATE_INIT;
675         int                      attrsonly;
676         AttributeDescription *ad_entry = slap_schema.si_ad_entry;
677
678         /* a_flags: array of flags telling if the i-th element will be
679          *          returned or filtered out
680          * e_flags: array of a_flags
681          */
682         char **e_flags = NULL;
683         
684         rs->sr_type = REP_SEARCH;
685
686         /* eventually will loop through generated operational attribute types
687          * currently implemented types include:
688          *      entryDN, subschemaSubentry, and hasSubordinates */
689         /* NOTE: moved before overlays callback circling because
690          * they may modify entry and other stuff in rs */
691         /* check for special all operational attributes ("+") type */
692         /* FIXME: maybe we could se this flag at the operation level;
693          * however, in principle the caller of send_search_entry() may
694          * change the attribute list at each call */
695         rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
696
697         rc = backend_operational( op, rs );
698         if ( rc ) {
699                 goto error_return;
700         }
701
702         if ( op->o_callback ) {
703                 int             first = 1;
704                 slap_callback   *sc = op->o_callback,
705                                 *sc_next = op->o_callback;
706
707                 rc = SLAP_CB_CONTINUE;
708                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next )
709                 {
710                         sc_next = op->o_callback->sc_next;
711                         if ( op->o_callback->sc_response ) {
712                                 rc = op->o_callback->sc_response( op, rs );
713                                 if ( first && op->o_callback == NULL ) {
714                                         sc = NULL;
715                                 }
716                                 if ( rc != SLAP_CB_CONTINUE ) break;
717                         }
718                         first = 0;
719                 }
720
721                 op->o_callback = sc;
722                 if ( rc != SLAP_CB_CONTINUE ) goto error_return;
723         }
724
725         Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
726                 op->o_connid, rs->sr_entry->e_name.bv_val,
727                 op->ors_attrsonly ? " (attrsOnly)" : "" );
728
729         attrsonly = op->ors_attrsonly;
730
731         if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
732                 Debug( LDAP_DEBUG_ACL,
733                         "send_search_entry: conn %lu access to entry (%s) not allowed\n", 
734                         op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
735
736                 rc = 1;
737                 goto error_return;
738         }
739
740         edn = rs->sr_entry->e_nname.bv_val;
741
742         if ( op->o_res_ber ) {
743                 /* read back control or LDAP_CONNECTIONLESS */
744             ber = op->o_res_ber;
745         } else {
746                 struct berval   bv;
747
748                 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
749                 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
750
751                 ber_init2( ber, &bv, LBER_USE_DER );
752                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
753         }
754
755 #ifdef LDAP_CONNECTIONLESS
756         if ( op->o_conn && op->o_conn->c_is_udp ) {
757                 /* CONNECTIONLESS */
758                 if ( op->o_protocol == LDAP_VERSION2 ) {
759                 rc = ber_printf(ber, "t{O{" /*}}*/,
760                                 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
761                 } else {
762                 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
763                                 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
764                 }
765         } else
766 #endif
767         if ( op->o_res_ber ) {
768                 /* read back control */
769             rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
770         } else {
771             rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
772                         LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
773         }
774
775         if ( rc == -1 ) {
776                 Debug( LDAP_DEBUG_ANY, 
777                         "send_search_entry: conn %lu  ber_printf failed\n", 
778                         op->o_connid, 0, 0 );
779
780                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
781                 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
782                 goto error_return;
783         }
784
785         /* check for special all user attributes ("*") type */
786         userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
787
788         /* create an array of arrays of flags. Each flag corresponds
789          * to particular value of attribute and equals 1 if value matches
790          * to ValuesReturnFilter or 0 if not
791          */     
792         if ( op->o_vrFilter != NULL ) {
793                 int     k = 0;
794                 size_t  size;
795
796                 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
797                         for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
798                 }
799
800                 size = i * sizeof(char *) + k;
801                 if ( size > 0 ) {
802                         char    *a_flags;
803                         e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
804                         if( e_flags == NULL ) {
805                         Debug( LDAP_DEBUG_ANY, 
806                                         "send_search_entry: conn %lu slap_sl_calloc failed\n",
807                                         op->o_connid ? op->o_connid : 0, 0, 0 );
808                                 ber_free( ber, 1 );
809         
810                                 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
811                                 goto error_return;
812                         }
813                         a_flags = (char *)(e_flags + i);
814                         memset( a_flags, 0, k );
815                         for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
816                                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
817                                 e_flags[i] = a_flags;
818                                 a_flags += j;
819                         }
820         
821                         rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ; 
822                         if ( rc == -1 ) {
823                                 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
824                                         "conn %lu matched values filtering failed\n",
825                                         op->o_connid ? op->o_connid : 0, 0, 0 );
826                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
827                                 send_ldap_error( op, rs, LDAP_OTHER,
828                                         "matched values filtering error" );
829                                 goto error_return;
830                         }
831                 }
832         }
833
834         for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
835                 AttributeDescription *desc = a->a_desc;
836                 int finish = 0;
837
838                 if ( rs->sr_attrs == NULL ) {
839                         /* all attrs request, skip operational attributes */
840                         if( is_at_operational( desc->ad_type ) ) {
841                                 continue;
842                         }
843
844                 } else {
845                         /* specific attrs requested */
846                         if ( is_at_operational( desc->ad_type ) ) {
847                                 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
848                                                 !ad_inlist( desc, rs->sr_attrs ) )
849                                 {
850                                         continue;
851                                 }
852
853                         } else {
854                                 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) )
855                                 {
856                                         continue;
857                                 }
858                         }
859                 }
860
861                 if ( attrsonly ) {
862                         if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
863                                 ACL_READ, &acl_state ) )
864                         {
865                                 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
866                                         "conn %lu access to attribute %s not allowed\n",
867                                         op->o_connid, desc->ad_cname.bv_val, 0 );
868                                 continue;
869                         }
870
871                         if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
872                                 Debug( LDAP_DEBUG_ANY, 
873                                         "send_search_entry: conn %lu  ber_printf failed\n", 
874                                         op->o_connid, 0, 0 );
875
876                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
877                                 send_ldap_error( op, rs, LDAP_OTHER,
878                                         "encoding description error");
879                                 goto error_return;
880                         }
881                         finish = 1;
882
883                 } else {
884                         int first = 1;
885                         for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
886                                 if ( ! access_allowed( op, rs->sr_entry,
887                                         desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
888                                 {
889                                         Debug( LDAP_DEBUG_ACL,
890                                                 "send_search_entry: conn %lu "
891                                                 "access to attribute %s, value #%d not allowed\n",
892                                                 op->o_connid, desc->ad_cname.bv_val, i );
893
894                                         continue;
895                                 }
896
897                                 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
898                                         continue;
899                                 }
900
901                                 if ( first ) {
902                                         first = 0;
903                                         finish = 1;
904                                         if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
905                                                 Debug( LDAP_DEBUG_ANY,
906                                                         "send_search_entry: conn %lu  ber_printf failed\n", 
907                                                         op->o_connid, 0, 0 );
908
909                                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
910                                                 send_ldap_error( op, rs, LDAP_OTHER,
911                                                         "encoding description error");
912                                                 goto error_return;
913                                         }
914                                 }
915                                 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
916                                         Debug( LDAP_DEBUG_ANY,
917                                                 "send_search_entry: conn %lu  "
918                                                 "ber_printf failed.\n", op->o_connid, 0, 0 );
919
920                                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
921                                         send_ldap_error( op, rs, LDAP_OTHER,
922                                                 "encoding values error" );
923                                         goto error_return;
924                                 }
925                         }
926                 }
927
928                 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
929                         Debug( LDAP_DEBUG_ANY,
930                                 "send_search_entry: conn %lu ber_printf failed\n", 
931                                 op->o_connid, 0, 0 );
932
933                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
934                         send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
935                         goto error_return;
936                 }
937         }
938
939         /* NOTE: moved before overlays callback circling because
940          * they may modify entry and other stuff in rs */
941         if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
942                 int     k = 0;
943                 size_t  size;
944
945                 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
946                         for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
947                 }
948
949                 size = i * sizeof(char *) + k;
950                 if ( size > 0 ) {
951                         char    *a_flags, **tmp;
952                 
953                         /*
954                          * Reuse previous memory - we likely need less space
955                          * for operational attributes
956                          */
957                         tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
958                                 op->o_tmpmemctx );
959                         if ( tmp == NULL ) {
960                                 Debug( LDAP_DEBUG_ANY,
961                                         "send_search_entry: conn %lu "
962                                         "not enough memory "
963                                         "for matched values filtering\n",
964                                         op->o_connid, 0, 0 );
965                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
966                                 send_ldap_error( op, rs, LDAP_OTHER,
967                                         "not enough memory for matched values filtering" );
968                                 goto error_return;
969                         }
970                         e_flags = tmp;
971                         a_flags = (char *)(e_flags + i);
972                         memset( a_flags, 0, k );
973                         for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
974                                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
975                                 e_flags[i] = a_flags;
976                                 a_flags += j;
977                         }
978                         rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ; 
979                     
980                         if ( rc == -1 ) {
981                                 Debug( LDAP_DEBUG_ANY,
982                                         "send_search_entry: conn %lu "
983                                         "matched values filtering failed\n", 
984                                         op->o_connid ? op->o_connid : 0, 0, 0);
985                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
986                                 send_ldap_error( op, rs, LDAP_OTHER,
987                                         "matched values filtering error" );
988                                 goto error_return;
989                         }
990                 }
991         }
992
993         for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
994                 AttributeDescription *desc = a->a_desc;
995
996                 if ( rs->sr_attrs == NULL ) {
997                         /* all attrs request, skip operational attributes */
998                         if( is_at_operational( desc->ad_type ) ) {
999                                 continue;
1000                         }
1001
1002                 } else {
1003                         /* specific attrs requested */
1004                         if( is_at_operational( desc->ad_type ) ) {
1005                                 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) && 
1006                                                 !ad_inlist( desc, rs->sr_attrs ) )
1007                                 {
1008                                         continue;
1009                                 }
1010                         } else {
1011                                 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1012                                         continue;
1013                                 }
1014                         }
1015                 }
1016
1017                 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1018                         ACL_READ, &acl_state ) )
1019                 {
1020                         Debug( LDAP_DEBUG_ACL,
1021                                 "send_search_entry: conn %lu "
1022                                 "access to attribute %s not allowed\n",
1023                                 op->o_connid, desc->ad_cname.bv_val, 0 );
1024
1025                         continue;
1026                 }
1027
1028                 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1029                 if ( rc == -1 ) {
1030                         Debug( LDAP_DEBUG_ANY,
1031                                 "send_search_entry: conn %lu  "
1032                                 "ber_printf failed\n", op->o_connid, 0, 0 );
1033
1034                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1035                         send_ldap_error( op, rs, LDAP_OTHER,
1036                                 "encoding description error" );
1037                         goto error_return;
1038                 }
1039
1040                 if ( ! attrsonly ) {
1041                         for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1042                                 if ( ! access_allowed( op, rs->sr_entry,
1043                                         desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1044                                 {
1045                                         Debug( LDAP_DEBUG_ACL,
1046                                                 "send_search_entry: conn %lu "
1047                                                 "access to %s, value %d not allowed\n",
1048                                                 op->o_connid, desc->ad_cname.bv_val, i );
1049
1050                                         continue;
1051                                 }
1052
1053                                 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1054                                         continue;
1055                                 }
1056
1057                                 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1058                                         Debug( LDAP_DEBUG_ANY,
1059                                                 "send_search_entry: conn %lu  ber_printf failed\n", 
1060                                                 op->o_connid, 0, 0 );
1061
1062                                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1063                                         send_ldap_error( op, rs, LDAP_OTHER,
1064                                                 "encoding values error" );
1065                                         goto error_return;
1066                                 }
1067                         }
1068                 }
1069
1070                 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1071                         Debug( LDAP_DEBUG_ANY,
1072                                 "send_search_entry: conn %lu  ber_printf failed\n",
1073                                 op->o_connid, 0, 0 );
1074
1075                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1076                         send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1077                         goto error_return;
1078                 }
1079         }
1080
1081         /* free e_flags */
1082         if ( e_flags ) {
1083                 slap_sl_free( e_flags, op->o_tmpmemctx );
1084                 e_flags = NULL;
1085         }
1086
1087         rc = ber_printf( ber, /*{{*/ "}N}" );
1088
1089         if( rc != -1 ) {
1090                 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1091         }
1092
1093         if( rc != -1 ) {
1094 #ifdef LDAP_CONNECTIONLESS
1095                 if( op->o_conn && op->o_conn->c_is_udp ) {
1096                         if ( op->o_protocol != LDAP_VERSION2 ) {
1097                                 rc = ber_printf( ber, /*{*/ "N}" );
1098                         }
1099                 } else
1100 #endif
1101                 if ( op->o_res_ber == NULL ) {
1102                         rc = ber_printf( ber, /*{*/ "N}" );
1103                 }
1104         }
1105
1106         if ( rc == -1 ) {
1107                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1108
1109                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1110                 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1111                 rc = 1;
1112                 goto error_return;
1113         }
1114
1115         if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1116                 be_entry_release_rw( op, rs->sr_entry, 0 );
1117                 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1118                 rs->sr_entry = NULL;
1119         }
1120
1121         if ( op->o_res_ber == NULL ) {
1122                 bytes = send_ldap_ber( op->o_conn, ber );
1123                 ber_free_buf( ber );
1124
1125                 if ( bytes < 0 ) {
1126                         Debug( LDAP_DEBUG_ANY,
1127                                 "send_search_entry: conn %lu  ber write failed.\n", 
1128                                 op->o_connid, 0, 0 );
1129
1130                         rc = -1;
1131                         goto error_return;
1132                 }
1133                 rs->sr_nentries++;
1134
1135                 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1136                 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1137                 ldap_pvt_mp_add_ulong( slap_counters.sc_entries, 1 );
1138                 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1139                 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1140         }
1141
1142         Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1143             op->o_log_prefix, edn, 0, 0, 0 );
1144
1145         Debug( LDAP_DEBUG_TRACE,
1146                 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1147
1148         rc = 0;
1149
1150 error_return:;
1151         if ( op->o_callback ) {
1152                 int             first = 1;
1153                 slap_callback   *sc = op->o_callback, *sc_next;
1154
1155                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1156                         sc_next = op->o_callback->sc_next;
1157                         if ( op->o_callback->sc_cleanup ) {
1158                                 (void)op->o_callback->sc_cleanup( op, rs );
1159                                 if ( first && op->o_callback != sc ) {
1160                                         sc = op->o_callback;
1161                                 }
1162                         }
1163                         first = 0;
1164                 }
1165                 op->o_callback = sc;
1166         }
1167
1168         if ( e_flags ) {
1169                 slap_sl_free( e_flags, op->o_tmpmemctx );
1170         }
1171
1172         if ( rs->sr_operational_attrs ) {
1173                 attrs_free( rs->sr_operational_attrs );
1174                 rs->sr_operational_attrs = NULL;
1175         }
1176         rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1177
1178         /* FIXME: I think rs->sr_type should be explicitly set to
1179          * REP_SEARCH here. That's what it was when we entered this
1180          * function. send_ldap_error may have changed it, but we
1181          * should set it back so that the cleanup functions know
1182          * what they're doing.
1183          */
1184         if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH 
1185                 && rs->sr_entry 
1186                 && ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) ) 
1187         {
1188                 entry_free( rs->sr_entry );
1189                 rs->sr_entry = NULL;
1190                 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1191         }
1192
1193         return( rc );
1194 }
1195
1196 int
1197 slap_send_search_reference( Operation *op, SlapReply *rs )
1198 {
1199         BerElementBuffer berbuf;
1200         BerElement      *ber = (BerElement *) &berbuf;
1201         int rc = 0;
1202         int bytes;
1203
1204         AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1205         AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1206
1207         rs->sr_type = REP_SEARCHREF;
1208         if ( op->o_callback ) {
1209                 int             first = 1;
1210                 slap_callback   *sc = op->o_callback,
1211                                 *sc_next = op->o_callback;
1212
1213                 rc = SLAP_CB_CONTINUE;
1214                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1215                         sc_next = op->o_callback->sc_next;
1216                         if ( op->o_callback->sc_response ) {
1217                                 rc = op->o_callback->sc_response( op, rs );
1218                                 if ( first && op->o_callback == NULL ) {
1219                                         sc = NULL;
1220                                 }
1221                                 if ( rc != SLAP_CB_CONTINUE ) break;
1222                         }
1223                         first = 0;
1224                 }
1225
1226                 op->o_callback = sc;
1227                 if ( rc != SLAP_CB_CONTINUE ) goto rel;
1228         }
1229
1230         Debug( LDAP_DEBUG_TRACE,
1231                 "=> send_search_reference: dn=\"%s\"\n",
1232                 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1233
1234         if (  rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1235                 ad_entry, NULL, ACL_READ, NULL ) )
1236         {
1237                 Debug( LDAP_DEBUG_ACL,
1238                         "send_search_reference: access to entry not allowed\n",
1239                     0, 0, 0 );
1240                 rc = 1;
1241                 goto rel;
1242         }
1243
1244         if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1245                 ad_ref, NULL, ACL_READ, NULL ) )
1246         {
1247                 Debug( LDAP_DEBUG_ACL,
1248                         "send_search_reference: access "
1249                         "to reference not allowed\n",
1250                     0, 0, 0 );
1251                 rc = 1;
1252                 goto rel;
1253         }
1254
1255 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1256         if( op->o_domain_scope ) {
1257                 Debug( LDAP_DEBUG_ANY,
1258                         "send_search_reference: domainScope control in (%s)\n", 
1259                         rs->sr_entry->e_dn, 0, 0 );
1260                 rc = 0;
1261                 goto rel;
1262         }
1263 #endif
1264
1265         if( rs->sr_ref == NULL ) {
1266                 Debug( LDAP_DEBUG_ANY,
1267                         "send_search_reference: null ref in (%s)\n", 
1268                         rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1269                 rc = 1;
1270                 goto rel;
1271         }
1272
1273         if( op->o_protocol < LDAP_VERSION3 ) {
1274                 rc = 0;
1275                 /* save the references for the result */
1276                 if( rs->sr_ref[0].bv_val != NULL ) {
1277                         if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1278                                 rc = LDAP_OTHER;
1279                 }
1280                 goto rel;
1281         }
1282
1283 #ifdef LDAP_CONNECTIONLESS
1284         if( op->o_conn && op->o_conn->c_is_udp ) {
1285                 ber = op->o_res_ber;
1286         } else
1287 #endif
1288         {
1289                 ber_init_w_nullc( ber, LBER_USE_DER );
1290                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1291         }
1292
1293         rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1294                 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1295
1296         if( rc != -1 ) {
1297                 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1298         }
1299
1300         if( rc != -1 ) {
1301                 rc = ber_printf( ber, /*"{"*/ "N}" );
1302         }
1303
1304         if ( rc == -1 ) {
1305                 Debug( LDAP_DEBUG_ANY,
1306                         "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1307
1308 #ifdef LDAP_CONNECTIONLESS
1309                 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1310 #endif
1311                 ber_free_buf( ber );
1312                 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1313                 goto rel;
1314         }
1315
1316         rc = 0;
1317         if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1318                 be_entry_release_rw( op, rs->sr_entry, 0 );
1319                 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1320                 rs->sr_entry = NULL;
1321         }
1322
1323 #ifdef LDAP_CONNECTIONLESS
1324         if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1325 #endif
1326         bytes = send_ldap_ber( op->o_conn, ber );
1327         ber_free_buf( ber );
1328
1329         ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1330         ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1331         ldap_pvt_mp_add_ulong( slap_counters.sc_refs, 1 );
1332         ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1333         ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1334 #ifdef LDAP_CONNECTIONLESS
1335         }
1336 #endif
1337
1338         Statslog( LDAP_DEBUG_STATS2, "%s REF dn=\"%s\"\n",
1339                 op->o_log_prefix, rs->sr_entry ? rs->sr_entry->e_dn : "(null)",
1340                 0, 0, 0 );
1341
1342         Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1343
1344 rel:
1345         if ( op->o_callback ) {
1346                 int             first = 1;
1347                 slap_callback   *sc = op->o_callback, *sc_next;
1348
1349                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1350                         sc_next = op->o_callback->sc_next;
1351                         if ( op->o_callback->sc_cleanup ) {
1352                                 (void)op->o_callback->sc_cleanup( op, rs );
1353                                 if ( first && op->o_callback != sc ) {
1354                                         sc = op->o_callback;
1355                                 }
1356                         }
1357                         first = 0;
1358                 }
1359                 op->o_callback = sc;
1360         }
1361
1362         return rc;
1363 }
1364
1365 int
1366 str2result(
1367     char        *s,
1368     int         *code,
1369     char        **matched,
1370     char        **info )
1371 {
1372         int     rc;
1373         char    *c;
1374
1375         *code = LDAP_SUCCESS;
1376         *matched = NULL;
1377         *info = NULL;
1378
1379         if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1380                 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1381                     s, 0, 0 );
1382
1383                 return( -1 );
1384         }
1385
1386         rc = 0;
1387         while ( (s = strchr( s, '\n' )) != NULL ) {
1388                 *s++ = '\0';
1389                 if ( *s == '\0' ) {
1390                         break;
1391                 }
1392                 if ( (c = strchr( s, ':' )) != NULL ) {
1393                         c++;
1394                 }
1395
1396                 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1397                         if ( c != NULL ) {
1398                                 *code = atoi( c );
1399                         }
1400                 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1401                         if ( c != NULL ) {
1402                                 *matched = c;
1403                         }
1404                 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1405                         if ( c != NULL ) {
1406                                 *info = c;
1407                         }
1408                 } else {
1409                         Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1410                             s, 0, 0 );
1411
1412                         rc = -1;
1413                 }
1414         }
1415
1416         return( rc );
1417 }
1418
1419 int slap_read_controls(
1420         Operation *op,
1421         SlapReply *rs,
1422         Entry *e,
1423         const struct berval *oid,
1424         LDAPControl **ctrl )
1425 {
1426         int rc;
1427         struct berval bv;
1428         BerElementBuffer berbuf;
1429         BerElement *ber = (BerElement *) &berbuf;
1430         LDAPControl c;
1431         Operation myop;
1432
1433         Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1434                 oid->bv_val, e->e_dn, 0 );
1435
1436         rs->sr_entry = e;
1437         rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1438                 op->o_preread_attrs : op->o_postread_attrs; 
1439
1440         bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1441         bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1442
1443         ber_init2( ber, &bv, LBER_USE_DER );
1444         ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1445
1446         /* create new operation */
1447         myop = *op;
1448         myop.o_bd = NULL;
1449         myop.o_res_ber = ber;
1450         myop.o_callback = NULL;
1451
1452         rc = slap_send_search_entry( &myop, rs );
1453         if( rc ) return rc;
1454
1455         rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1456
1457         if( rc == LBER_ERROR ) return LDAP_OTHER;
1458
1459         c.ldctl_oid = oid->bv_val;
1460         c.ldctl_iscritical = 0;
1461
1462         if ( *ctrl == NULL ) {
1463                 /* first try */
1464                 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1465         } else {
1466                 /* retry: free previous try */
1467                 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1468         }
1469
1470         **ctrl = c;
1471         return LDAP_SUCCESS;
1472 }
1473
1474 /* Map API errors to protocol errors... */
1475 int
1476 slap_map_api2result( SlapReply *rs )
1477 {
1478         switch(rs->sr_err) {
1479         case LDAP_SERVER_DOWN:
1480                 return LDAP_UNAVAILABLE;
1481         case LDAP_LOCAL_ERROR:
1482                 return LDAP_OTHER;
1483         case LDAP_ENCODING_ERROR:
1484         case LDAP_DECODING_ERROR:
1485                 return LDAP_PROTOCOL_ERROR;
1486         case LDAP_TIMEOUT:
1487                 return LDAP_UNAVAILABLE;
1488         case LDAP_AUTH_UNKNOWN:
1489                 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1490         case LDAP_FILTER_ERROR:
1491                 rs->sr_text = "Filter error";
1492                 return LDAP_OTHER;
1493         case LDAP_USER_CANCELLED:
1494                 rs->sr_text = "User cancelled";
1495                 return LDAP_OTHER;
1496         case LDAP_PARAM_ERROR:
1497                 return LDAP_PROTOCOL_ERROR;
1498         case LDAP_NO_MEMORY:
1499                 return LDAP_OTHER;
1500         case LDAP_CONNECT_ERROR:
1501                 return LDAP_UNAVAILABLE;
1502         case LDAP_NOT_SUPPORTED:
1503                 return LDAP_UNWILLING_TO_PERFORM;
1504         case LDAP_CONTROL_NOT_FOUND:
1505                 return LDAP_PROTOCOL_ERROR;
1506         case LDAP_NO_RESULTS_RETURNED:
1507                 return LDAP_NO_SUCH_OBJECT;
1508         case LDAP_MORE_RESULTS_TO_RETURN:
1509                 rs->sr_text = "More results to return";
1510                 return LDAP_OTHER;
1511         case LDAP_CLIENT_LOOP:
1512         case LDAP_REFERRAL_LIMIT_EXCEEDED:
1513                 return LDAP_LOOP_DETECT;
1514         default:
1515                 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1516                 return rs->sr_err;
1517         }
1518 }
1519
1520
1521 slap_mask_t
1522 slap_attr_flags( AttributeName *an )
1523 {
1524         slap_mask_t     flags = SLAP_ATTRS_UNDEFINED;
1525
1526         if ( an == NULL ) {
1527                 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1528
1529         } else {
1530                 flags |= an_find( an, &AllOper )
1531                         ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1532                 flags |= an_find( an, &AllUser )
1533                         ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;
1534         }
1535
1536         return flags;
1537 }
1538