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