]> git.sur5r.net Git - openldap/blob - servers/slapd/result.c
Merge remote-tracking branch 'origin/mdb.master'
[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-2014 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 /*
136  * SlapReply debugging enabled by USE_RS_ASSERT.
137  *
138  * Disabled by default, but compiled in (but still unused) when
139  * LDAP_TEST.  #define USE_RS_ASSERT as nonzero to enable some
140  * assertions which check the SlapReply.  USE_RS_ASSERT = 2 or higher
141  * check aggressively, currently some code fail these tests.
142  *
143  * Environment variable $NO_RS_ASSERT controls how USE_RS_ASSERT handles
144  * errors.  > 0: ignore errors, 0: abort (the default), < 0: just warn.
145  *
146  * Wrap LDAP operation calls in macros SLAP_OP() & co from proto-slap.h
147  * to check the SlapReply.  contrib/slapd-tools/wrap_slap_ops converts
148  * source code to use the macros.
149  */
150 #if defined(LDAP_TEST) || (defined(USE_RS_ASSERT) && (USE_RS_ASSERT))
151
152 int rs_suppress_assert = 0;
153
154 /* RS_ASSERT() helper function */
155 void rs_assert_(const char*file, unsigned line, const char*fn, const char*cond)
156 {
157         int no_assert = rs_suppress_assert, save_errno = errno;
158         const char *s;
159
160         if ( no_assert >= 0 ) {
161                 if ( no_assert == 0 && (s = getenv( "NO_RS_ASSERT" )) && *s ) {
162                         no_assert = rs_suppress_assert = atoi( s );
163                 }
164                 if ( no_assert > 0 ) {
165                         errno = save_errno;
166                         return;
167                 }
168         }
169
170 #ifdef rs_assert_       /* proto-slap.h #defined away the fn parameter */
171         fprintf( stderr,"%s:%u: "  "RS_ASSERT(%s) failed.\n", file,line,cond );
172 #else
173         fprintf( stderr,"%s:%u: %s: RS_ASSERT(%s) failed.\n", file,line,fn,cond );
174 #endif
175         fflush( stderr );
176
177         errno = save_errno;
178         /* $NO_RS_ASSERT > 0: ignore rs_asserts, 0: abort, < 0: just warn */
179         if ( !no_assert /* from $NO_RS_ASSERT */ ) abort();
180 }
181
182 /* SlapReply is consistent */
183 void
184 (rs_assert_ok)( const SlapReply *rs )
185 {
186         const slap_mask_t flags = rs->sr_flags;
187
188         if ( flags & REP_ENTRY_MASK ) {
189                 RS_ASSERT( !(flags & REP_ENTRY_MUSTRELEASE)
190                         || !(flags & (REP_ENTRY_MASK ^ REP_ENTRY_MUSTRELEASE)) );
191                 RS_ASSERT( rs->sr_entry != NULL );
192                 RS_ASSERT( (1 << rs->sr_type) &
193                         ((1 << REP_SEARCH) | (1 << REP_SEARCHREF) |
194                          (1 << REP_RESULT) | (1 << REP_GLUE_RESULT)) );
195         }
196 #if defined(USE_RS_ASSERT) && (USE_RS_ASSERT) > 1 /* TODO: Enable when safe */
197         if ( (flags & (REP_MATCHED_MASK | REP_REF_MASK | REP_CTRLS_MASK)) ) {
198                 RS_ASSERT( !(flags & REP_MATCHED_MASK) || rs->sr_matched );
199                 RS_ASSERT( !(flags & REP_CTRLS_MASK  ) || rs->sr_ctrls   );
200                 /* Note: LDAP_REFERRAL + !sr_ref is OK, becomes LDAP_NO_SUCH_OBJECT */
201         }
202 #if (USE_RS_ASSERT) > 2
203         if ( rs->sr_err == LDAP_SUCCESS ) {
204                 RS_ASSERT( rs->sr_text == NULL );
205                 RS_ASSERT( rs->sr_matched == NULL );
206         }
207 #endif
208 #endif
209 }
210
211 /* Ready for calling a new backend operation */
212 void
213 (rs_assert_ready)( const SlapReply *rs )
214 {
215         RS_ASSERT( !rs->sr_entry   );
216 #if defined(USE_RS_ASSERT) && (USE_RS_ASSERT) > 1 /* TODO: Enable when safe */
217         RS_ASSERT( !rs->sr_text    );
218         RS_ASSERT( !rs->sr_ref     );
219         RS_ASSERT( !rs->sr_matched );
220         RS_ASSERT( !rs->sr_ctrls   );
221         RS_ASSERT( !rs->sr_flags   );
222 #if (USE_RS_ASSERT) > 2
223         RS_ASSERT( rs->sr_err == LDAP_SUCCESS );
224 #endif
225 #else
226         RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
227 #endif
228 }
229
230 /* Backend operation done */
231 void
232 (rs_assert_done)( const SlapReply *rs )
233 {
234 #if defined(USE_RS_ASSERT) && (USE_RS_ASSERT) > 1 /* TODO: Enable when safe */
235         RS_ASSERT( !(rs->sr_flags & ~(REP_ENTRY_MODIFIABLE|REP_NO_OPERATIONALS)) );
236         rs_assert_ok( rs );
237 #else
238         RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MUSTFLUSH) );
239 #endif
240 }
241
242 #endif /* LDAP_TEST || USE_RS_ASSERT */
243
244 /* Reset a used SlapReply whose contents has been flushed (freed/released) */
245 void
246 (rs_reinit)( SlapReply *rs, slap_reply_t type )
247 {
248         rs_reinit( rs, type );          /* proto-slap.h macro */
249 }
250
251 /* Obey and clear rs->sr_flags & REP_ENTRY_MASK.  Clear sr_entry if freed. */
252 void
253 rs_flush_entry( Operation *op, SlapReply *rs, slap_overinst *on )
254 {
255         rs_assert_ok( rs );
256
257         if ( (rs->sr_flags & REP_ENTRY_MUSTFLUSH) && rs->sr_entry != NULL ) {
258                 if ( !(rs->sr_flags & REP_ENTRY_MUSTRELEASE) ) {
259                         entry_free( rs->sr_entry );
260                 } else if ( on != NULL ) {
261                         overlay_entry_release_ov( op, rs->sr_entry, 0, on );
262                 } else {
263                         be_entry_release_rw( op, rs->sr_entry, 0 );
264                 }
265                 rs->sr_entry = NULL;
266         }
267
268         rs->sr_flags &= ~REP_ENTRY_MASK;
269 }
270
271 /* Set rs->sr_entry after obeying and clearing sr_flags & REP_ENTRY_MASK. */
272 void
273 rs_replace_entry( Operation *op, SlapReply *rs, slap_overinst *on, Entry *e )
274 {
275         rs_flush_entry( op, rs, on );
276         rs->sr_entry = e;
277 }
278
279 /*
280  * Ensure rs->sr_entry is modifiable, by duplicating it if necessary.
281  * Obey sr_flags.  Set REP_ENTRY_<MODIFIABLE, and MUSTBEFREED if duplicated>.
282  * Return nonzero if rs->sr_entry was replaced.
283  */
284 int
285 rs_entry2modifiable( Operation *op, SlapReply *rs, slap_overinst *on )
286 {
287         if ( rs->sr_flags & REP_ENTRY_MODIFIABLE ) {
288                 rs_assert_ok( rs );
289                 return 0;
290         }
291         rs_replace_entry( op, rs, on, entry_dup( rs->sr_entry ));
292         rs->sr_flags |= REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED;
293         return 1;
294 }
295
296 static long send_ldap_ber(
297         Operation *op,
298         BerElement *ber )
299 {
300         Connection *conn = op->o_conn;
301         ber_len_t bytes;
302         long ret = 0;
303         char *close_reason;
304
305         ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
306
307         /* write only one pdu at a time - wait til it's our turn */
308         ldap_pvt_thread_mutex_lock( &conn->c_write1_mutex );
309         if (( op->o_abandon && !op->o_cancel ) || !connection_valid( conn ) ||
310                 conn->c_writers < 0 ) {
311                 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
312                 return 0;
313         }
314
315         conn->c_writers++;
316
317         while ( conn->c_writers > 0 && conn->c_writing ) {
318                 ldap_pvt_thread_pool_idle( &connection_pool );
319                 ldap_pvt_thread_cond_wait( &conn->c_write1_cv, &conn->c_write1_mutex );
320                 ldap_pvt_thread_pool_unidle( &connection_pool );
321         }
322
323         /* connection was closed under us */
324         if ( conn->c_writers < 0 ) {
325                 /* we're the last waiter, let the closer continue */
326                 if ( conn->c_writers == -1 )
327                         ldap_pvt_thread_cond_signal( &conn->c_write1_cv );
328                 conn->c_writers++;
329                 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
330                 return 0;
331         }
332
333         /* Our turn */
334         conn->c_writing = 1;
335
336         /* write the pdu */
337         while( 1 ) {
338                 int err;
339
340                 if ( ber_flush2( conn->c_sb, ber, LBER_FLUSH_FREE_NEVER ) == 0 ) {
341                         ret = bytes;
342                         break;
343                 }
344
345                 err = sock_errno();
346
347                 /*
348                  * we got an error.  if it's ewouldblock, we need to
349                  * wait on the socket being writable.  otherwise, figure
350                  * it's a hard error and return.
351                  */
352
353                 Debug( LDAP_DEBUG_CONNS, "ber_flush2 failed errno=%d reason=\"%s\"\n",
354                     err, sock_errstr(err), 0 );
355
356                 if ( err != EWOULDBLOCK && err != EAGAIN ) {
357                         close_reason = "connection lost on write";
358 fail:
359                         conn->c_writers--;
360                         conn->c_writing = 0;
361                         ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
362                         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
363                         connection_closing( conn, close_reason );
364                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
365                         return -1;
366                 }
367
368                 /* wait for socket to be write-ready */
369                 conn->c_writewaiter = 1;
370                 ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
371                 ldap_pvt_thread_pool_idle( &connection_pool );
372                 err = slapd_wait_writer( conn->c_sd );
373                 conn->c_writewaiter = 0;
374                 ldap_pvt_thread_pool_unidle( &connection_pool );
375                 ldap_pvt_thread_mutex_lock( &conn->c_write1_mutex );
376                 /* 0 is timeout, so we close it.
377                  * -1 is an error, close it.
378                  */
379                 if ( err <= 0 ) {
380                         if ( err == 0 )
381                                 close_reason = "writetimeout";
382                         else
383                                 close_reason = "connection lost on writewait";
384                         goto fail;
385                 }
386
387                 if ( conn->c_writers < 0 ) {
388                         ret = 0;
389                         break;
390                 }
391         }
392
393         conn->c_writing = 0;
394         if ( conn->c_writers < 0 ) {
395                 conn->c_writers++;
396                 if ( !conn->c_writers )
397                         ldap_pvt_thread_cond_signal( &conn->c_write1_cv );
398         } else {
399                 conn->c_writers--;
400                 ldap_pvt_thread_cond_signal( &conn->c_write1_cv );
401         }
402         ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
403
404         return ret;
405 }
406
407 static int
408 send_ldap_control( BerElement *ber, LDAPControl *c )
409 {
410         int rc;
411
412         assert( c != NULL );
413
414         rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
415
416         if( c->ldctl_iscritical ) {
417                 rc = ber_printf( ber, "b",
418                         (ber_int_t) c->ldctl_iscritical ) ;
419                 if( rc == -1 ) return rc;
420         }
421
422         if( c->ldctl_value.bv_val != NULL ) {
423                 rc = ber_printf( ber, "O", &c->ldctl_value ); 
424                 if( rc == -1 ) return rc;
425         }
426
427         rc = ber_printf( ber, /*{*/"N}" );
428         if( rc == -1 ) return rc;
429
430         return 0;
431 }
432
433 static int
434 send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
435 {
436         int rc;
437
438         if( c == NULL )
439                 return 0;
440
441         rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
442         if( rc == -1 ) return rc;
443
444         for( ; *c != NULL; c++) {
445                 rc = send_ldap_control( ber, *c );
446                 if( rc == -1 ) return rc;
447         }
448
449 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
450         /* this is a hack to avoid having to modify op->s_ctrls */
451         if( o->o_sortedresults ) {
452                 BerElementBuffer berbuf;
453                 BerElement *sber = (BerElement *) &berbuf;
454                 LDAPControl sorted;
455                 BER_BVZERO( &sorted.ldctl_value );
456                 sorted.ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
457                 sorted.ldctl_iscritical = 0;
458
459                 ber_init2( sber, NULL, LBER_USE_DER );
460
461                 ber_printf( sber, "{e}", LDAP_UNWILLING_TO_PERFORM );
462
463                 if( ber_flatten2( sber, &sorted.ldctl_value, 0 ) == -1 ) {
464                         return -1;
465                 }
466
467                 (void) ber_free_buf( sber );
468
469                 rc = send_ldap_control( ber, &sorted );
470                 if( rc == -1 ) return rc;
471         }
472 #endif
473
474         rc = ber_printf( ber, /*{*/"N}" );
475
476         return rc;
477 }
478
479 /*
480  * slap_response_play()
481  *
482  * plays the callback list; rationale: a callback can
483  *   - remove itself from the list, by setting op->o_callback = NULL;
484  *     malloc()'ed callbacks should free themselves from inside the
485  *     sc_response() function.
486  *   - replace itself with another (list of) callback(s), by setting
487  *     op->o_callback = a new (list of) callback(s); in this case, it
488  *     is the callback's responsibility to to append existing subsequent
489  *     callbacks to the end of the list that is passed to the sc_response()
490  *     function.
491  *   - modify the list of subsequent callbacks by modifying the value
492  *     of the sc_next field from inside the sc_response() function; this
493  *     case does not require any handling from inside slap_response_play()
494  *
495  * To stop execution of the playlist, the sc_response() function must return
496  * a value different from SLAP_SC_CONTINUE.
497  *
498  * The same applies to slap_cleanup_play(); only, there is no means to stop
499  * execution of the playlist, since all cleanup functions must be called.
500  */
501 static int
502 slap_response_play(
503         Operation *op,
504         SlapReply *rs )
505 {
506         int rc;
507
508         slap_callback   *sc = op->o_callback, **scp;
509
510         rc = SLAP_CB_CONTINUE;
511         for ( scp = &sc; *scp; ) {
512                 slap_callback *sc_next = (*scp)->sc_next, **sc_nextp = &(*scp)->sc_next;
513
514                 op->o_callback = *scp;
515                 if ( op->o_callback->sc_response ) {
516                         rc = op->o_callback->sc_response( op, rs );
517                         if ( op->o_callback == NULL ) {
518                                 /* the callback has been removed;
519                                  * repair the list */
520                                 *scp = sc_next;
521                                 sc_nextp = scp;
522
523                         } else if ( op->o_callback != *scp ) {
524                                 /* a new callback has been inserted
525                                  * in place of the existing one; repair the list */
526                                 *scp = op->o_callback;
527                                 sc_nextp = scp;
528                         }
529                         if ( rc != SLAP_CB_CONTINUE ) break;
530                 }
531                 scp = sc_nextp;
532         }
533
534         op->o_callback = sc;
535         return rc;
536 }
537
538 static int
539 slap_cleanup_play(
540         Operation *op,
541         SlapReply *rs )
542 {
543         slap_callback   *sc = op->o_callback, **scp;
544
545         for ( scp = &sc; *scp; ) {
546                 slap_callback *sc_next = (*scp)->sc_next, **sc_nextp = &(*scp)->sc_next;
547
548                 op->o_callback = *scp;
549                 if ( op->o_callback->sc_cleanup ) {
550                         (void)op->o_callback->sc_cleanup( op, rs );
551                         if ( op->o_callback == NULL ) {
552                                 /* the callback has been removed;
553                                  * repair the list */
554                                 *scp = sc_next;
555                                 sc_nextp = scp;
556
557                         } else if ( op->o_callback != *scp ) {
558                                 /* a new callback has been inserted
559                                  * after the existing one; repair the list */
560                                 /* a new callback has been inserted
561                                  * in place of the existing one; repair the list */
562                                 *scp = op->o_callback;
563                                 sc_nextp = scp;
564                         }
565                         /* don't care about the result; do all cleanup */
566                 }
567                 scp = sc_nextp;
568         }
569
570         op->o_callback = sc;
571         return LDAP_SUCCESS;
572 }
573
574 static int
575 send_ldap_response(
576         Operation *op,
577         SlapReply *rs )
578 {
579         BerElementBuffer berbuf;
580         BerElement      *ber = (BerElement *) &berbuf;
581         int             rc = LDAP_SUCCESS;
582         long    bytes;
583
584         /* op was actually aborted, bypass everything if client didn't Cancel */
585         if (( rs->sr_err == SLAPD_ABANDON ) && !op->o_cancel ) {
586                 rc = SLAPD_ABANDON;
587                 goto clean2;
588         }
589
590         if ( op->o_callback ) {
591                 rc = slap_response_play( op, rs );
592                 if ( rc != SLAP_CB_CONTINUE ) {
593                         goto clean2;
594                 }
595         }
596
597         /* op completed, connection aborted, bypass sending response */
598         if ( op->o_abandon && !op->o_cancel ) {
599                 rc = SLAPD_ABANDON;
600                 goto clean2;
601         }
602
603 #ifdef LDAP_CONNECTIONLESS
604         if (op->o_conn && op->o_conn->c_is_udp)
605                 ber = op->o_res_ber;
606         else
607 #endif
608         {
609                 ber_init_w_nullc( ber, LBER_USE_DER );
610                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
611         }
612
613         rc = rs->sr_err;
614         if ( rc == SLAPD_ABANDON && op->o_cancel )
615                 rc = LDAP_CANCELLED;
616
617         Debug( LDAP_DEBUG_TRACE,
618                 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
619                 rs->sr_msgid, rs->sr_tag, rc );
620
621         if( rs->sr_ref ) {
622                 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
623                         rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
624                         NULL, NULL );
625         }
626
627 #ifdef LDAP_CONNECTIONLESS
628         if (op->o_conn && op->o_conn->c_is_udp &&
629                 op->o_protocol == LDAP_VERSION2 )
630         {
631                 rc = ber_printf( ber, "t{ess" /*"}"*/,
632                         rs->sr_tag, rc,
633                 rs->sr_matched == NULL ? "" : rs->sr_matched,
634                 rs->sr_text == NULL ? "" : rs->sr_text );
635         } else 
636 #endif
637         if ( rs->sr_type == REP_INTERMEDIATE ) {
638             rc = ber_printf( ber, "{it{" /*"}}"*/,
639                         rs->sr_msgid, rs->sr_tag );
640
641         } else {
642             rc = ber_printf( ber, "{it{ess" /*"}}"*/,
643                 rs->sr_msgid, rs->sr_tag, rc,
644                 rs->sr_matched == NULL ? "" : rs->sr_matched,
645                 rs->sr_text == NULL ? "" : rs->sr_text );
646         }
647
648         if( rc != -1 ) {
649                 if ( rs->sr_ref != NULL ) {
650                         assert( rs->sr_err == LDAP_REFERRAL );
651                         rc = ber_printf( ber, "t{W}",
652                                 LDAP_TAG_REFERRAL, rs->sr_ref );
653                 } else {
654                         assert( rs->sr_err != LDAP_REFERRAL );
655                 }
656         }
657
658         if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
659                 rc = ber_printf( ber, "tO",
660                         LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
661         }
662
663         if( rc != -1 &&
664                 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
665         {
666                 if ( rs->sr_rspoid != NULL ) {
667                         rc = ber_printf( ber, "ts",
668                                 rs->sr_type == REP_EXTENDED
669                                         ? LDAP_TAG_EXOP_RES_OID : LDAP_TAG_IM_RES_OID,
670                                 rs->sr_rspoid );
671                 }
672                 if( rc != -1 && rs->sr_rspdata != NULL ) {
673                         rc = ber_printf( ber, "tO",
674                                 rs->sr_type == REP_EXTENDED
675                                         ? LDAP_TAG_EXOP_RES_VALUE : LDAP_TAG_IM_RES_VALUE,
676                                 rs->sr_rspdata );
677                 }
678         }
679
680         if( rc != -1 ) {
681                 rc = ber_printf( ber, /*"{"*/ "N}" );
682         }
683
684         if( rc != -1 ) {
685                 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
686         }
687
688         if( rc != -1 ) {
689                 rc = ber_printf( ber, /*"{"*/ "N}" );
690         }
691
692 #ifdef LDAP_CONNECTIONLESS
693         if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
694                 && rc != -1 )
695         {
696                 rc = ber_printf( ber, /*"{"*/ "N}" );
697         }
698 #endif
699                 
700         if ( rc == -1 ) {
701                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
702
703 #ifdef LDAP_CONNECTIONLESS
704                 if (!op->o_conn || op->o_conn->c_is_udp == 0)
705 #endif
706                 {
707                         ber_free_buf( ber );
708                 }
709                 goto cleanup;
710         }
711
712         /* send BER */
713         bytes = send_ldap_ber( op, ber );
714 #ifdef LDAP_CONNECTIONLESS
715         if (!op->o_conn || op->o_conn->c_is_udp == 0)
716 #endif
717         {
718                 ber_free_buf( ber );
719         }
720
721         if ( bytes < 0 ) {
722                 Debug( LDAP_DEBUG_ANY,
723                         "send_ldap_response: ber write failed\n",
724                         0, 0, 0 );
725
726                 goto cleanup;
727         }
728
729         ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
730         ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
731         ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
732         ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
733
734 cleanup:;
735         /* Tell caller that we did this for real, as opposed to being
736          * overridden by a callback
737          */
738         rc = SLAP_CB_CONTINUE;
739
740 clean2:;
741         if ( op->o_callback ) {
742                 (void)slap_cleanup_play( op, rs );
743         }
744
745         if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
746                 rs->sr_flags ^= REP_MATCHED_MUSTBEFREED; /* paranoia */
747                 if ( rs->sr_matched ) {
748                         free( (char *)rs->sr_matched );
749                         rs->sr_matched = NULL;
750                 }
751         }
752
753         if ( rs->sr_flags & REP_REF_MUSTBEFREED ) {
754                 rs->sr_flags ^= REP_REF_MUSTBEFREED; /* paranoia */
755                 if ( rs->sr_ref ) {
756                         ber_bvarray_free( rs->sr_ref );
757                         rs->sr_ref = NULL;
758                 }
759         }
760
761         if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
762                 rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
763                 if ( rs->sr_ctrls ) {
764                         slap_free_ctrls( op, rs->sr_ctrls );
765                         rs->sr_ctrls = NULL;
766                 }
767         }
768
769         return rc;
770 }
771
772
773 void
774 send_ldap_disconnect( Operation *op, SlapReply *rs )
775 {
776 #define LDAP_UNSOLICITED_ERROR(e) \
777         (  (e) == LDAP_PROTOCOL_ERROR \
778         || (e) == LDAP_STRONG_AUTH_REQUIRED \
779         || (e) == LDAP_UNAVAILABLE )
780
781         Debug( LDAP_DEBUG_TRACE,
782                 "send_ldap_disconnect %d:%s\n",
783                 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
784         assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
785
786         /* TODO: Flush the entry if sr_type == REP_SEARCH/REP_SEARCHREF? */
787         RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
788         rs->sr_flags &= ~REP_ENTRY_MASK;        /* paranoia */
789
790         rs->sr_type = REP_EXTENDED;
791         rs->sr_rspdata = NULL;
792
793         if ( op->o_protocol < LDAP_VERSION3 ) {
794                 rs->sr_rspoid = NULL;
795                 rs->sr_tag = slap_req2res( op->o_tag );
796                 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
797
798         } else {
799                 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
800                 rs->sr_tag = LDAP_RES_EXTENDED;
801                 rs->sr_msgid = LDAP_RES_UNSOLICITED;
802         }
803
804         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
805                 Statslog( LDAP_DEBUG_STATS,
806                         "%s DISCONNECT tag=%lu err=%d text=%s\n",
807                         op->o_log_prefix, rs->sr_tag, rs->sr_err,
808                         rs->sr_text ? rs->sr_text : "", 0 );
809         }
810 }
811
812 void
813 slap_send_ldap_result( Operation *op, SlapReply *rs )
814 {
815         char *tmp = NULL;
816         const char *otext = rs->sr_text;
817         BerVarray oref = rs->sr_ref;
818
819         rs->sr_type = REP_RESULT;
820
821         /* Propagate Abandons so that cleanup callbacks can be processed */
822         if ( rs->sr_err == SLAPD_ABANDON || op->o_abandon )
823                 goto abandon;
824
825         Debug( LDAP_DEBUG_TRACE,
826                 "send_ldap_result: %s p=%d\n",
827                 op->o_log_prefix, op->o_protocol, 0 );
828         Debug( LDAP_DEBUG_ARGS,
829                 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
830                 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
831                 rs->sr_text ? rs->sr_text : "" );
832         if( rs->sr_ref ) {
833                 Debug( LDAP_DEBUG_ARGS,
834                         "send_ldap_result: referral=\"%s\"\n",
835                         rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
836                         NULL, NULL );
837         }
838         assert( !LDAP_API_ERROR( rs->sr_err ) );
839         assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
840
841         if ( rs->sr_err == LDAP_REFERRAL ) {
842                 if( op->o_domain_scope ) rs->sr_ref = NULL;
843
844                 if( rs->sr_ref == NULL ) {
845                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
846                 } else if ( op->o_protocol < LDAP_VERSION3 ) {
847                         rs->sr_err = LDAP_PARTIAL_RESULTS;
848                 }
849         }
850
851         if ( op->o_protocol < LDAP_VERSION3 ) {
852                 tmp = v2ref( rs->sr_ref, rs->sr_text );
853                 rs->sr_text = tmp;
854                 rs->sr_ref = NULL;
855         }
856
857 abandon:
858         rs->sr_tag = slap_req2res( op->o_tag );
859         rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
860
861         if ( rs->sr_flags & REP_REF_MUSTBEFREED ) {
862                 if ( rs->sr_ref == NULL ) {
863                         rs->sr_flags ^= REP_REF_MUSTBEFREED;
864                         ber_bvarray_free( oref );
865                 }
866                 oref = NULL; /* send_ldap_response() will free rs->sr_ref if != NULL */
867         }
868
869         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
870                 if ( op->o_tag == LDAP_REQ_SEARCH ) {
871                         Statslog( LDAP_DEBUG_STATS,
872                                 "%s SEARCH RESULT tag=%lu err=%d nentries=%d text=%s\n",
873                                 op->o_log_prefix, rs->sr_tag, rs->sr_err,
874                                 rs->sr_nentries, rs->sr_text ? rs->sr_text : "" );
875                 } else {
876                         Statslog( LDAP_DEBUG_STATS,
877                                 "%s RESULT tag=%lu err=%d text=%s\n",
878                                 op->o_log_prefix, rs->sr_tag, rs->sr_err,
879                                 rs->sr_text ? rs->sr_text : "", 0 );
880                 }
881         }
882
883         if( tmp != NULL ) ch_free(tmp);
884         rs->sr_text = otext;
885         rs->sr_ref = oref;
886 }
887
888 void
889 send_ldap_sasl( Operation *op, SlapReply *rs )
890 {
891         Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
892                 rs->sr_err,
893                 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
894
895         RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
896         rs->sr_flags &= ~REP_ENTRY_MASK;        /* paranoia */
897
898         rs->sr_type = REP_SASL;
899         rs->sr_tag = slap_req2res( op->o_tag );
900         rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
901
902         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
903                 Statslog( LDAP_DEBUG_STATS,
904                         "%s RESULT tag=%lu err=%d text=%s\n",
905                         op->o_log_prefix, rs->sr_tag, rs->sr_err,
906                         rs->sr_text ? rs->sr_text : "", 0 );
907         }
908 }
909
910 void
911 slap_send_ldap_extended( Operation *op, SlapReply *rs )
912 {
913         Debug( LDAP_DEBUG_TRACE,
914                 "send_ldap_extended: err=%d oid=%s len=%ld\n",
915                 rs->sr_err,
916                 rs->sr_rspoid ? rs->sr_rspoid : "",
917                 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
918
919         RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
920         rs->sr_flags &= ~REP_ENTRY_MASK;        /* paranoia */
921
922         rs->sr_type = REP_EXTENDED;
923         rs->sr_tag = slap_req2res( op->o_tag );
924         rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
925
926         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
927                 Statslog( LDAP_DEBUG_STATS,
928                         "%s RESULT oid=%s err=%d text=%s\n",
929                         op->o_log_prefix, rs->sr_rspoid ? rs->sr_rspoid : "",
930                         rs->sr_err, rs->sr_text ? rs->sr_text : "", 0 );
931         }
932 }
933
934 void
935 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
936 {
937         Debug( LDAP_DEBUG_TRACE,
938                 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
939                 rs->sr_err,
940                 rs->sr_rspoid ? rs->sr_rspoid : "",
941                 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
942
943         RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
944         rs->sr_flags &= ~REP_ENTRY_MASK;        /* paranoia */
945
946         rs->sr_type = REP_INTERMEDIATE;
947         rs->sr_tag = LDAP_RES_INTERMEDIATE;
948         rs->sr_msgid = op->o_msgid;
949         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
950                 Statslog( LDAP_DEBUG_STATS2,
951                         "%s INTERM oid=%s\n",
952                         op->o_log_prefix,
953                         rs->sr_rspoid ? rs->sr_rspoid : "", 0, 0, 0 );
954         }
955 }
956
957 #define set_ldap_error( rs, err, text ) do { \
958                 (rs)->sr_err = err; (rs)->sr_text = text; } while(0)
959
960 /*
961  * returns:
962  *
963  * LDAP_SUCCESS                 entry sent
964  * LDAP_OTHER                   entry not sent (other)
965  * LDAP_INSUFFICIENT_ACCESS     entry not sent (ACL)
966  * LDAP_UNAVAILABLE             entry not sent (connection closed)
967  * LDAP_SIZELIMIT_EXCEEDED      entry not sent (caller must send sizelimitExceeded)
968  */
969
970 int
971 slap_send_search_entry( Operation *op, SlapReply *rs )
972 {
973         BerElementBuffer berbuf;
974         BerElement      *ber = (BerElement *) &berbuf;
975         Attribute       *a;
976         int             i, j, rc = LDAP_UNAVAILABLE, bytes;
977         int             userattrs;
978         AccessControlState acl_state = ACL_STATE_INIT;
979         int                      attrsonly;
980         AttributeDescription *ad_entry = slap_schema.si_ad_entry;
981
982         /* a_flags: array of flags telling if the i-th element will be
983          *          returned or filtered out
984          * e_flags: array of a_flags
985          */
986         char **e_flags = NULL;
987
988         rs->sr_type = REP_SEARCH;
989
990         if ( op->ors_slimit >= 0 && rs->sr_nentries >= op->ors_slimit ) {
991                 rc = LDAP_SIZELIMIT_EXCEEDED;
992                 goto error_return;
993         }
994
995         /* Every 64 entries, check for thread pool pause */
996         if ( ( ( rs->sr_nentries & 0x3f ) == 0x3f ) &&
997                 ldap_pvt_thread_pool_pausing( &connection_pool ) > 0 )
998         {
999                 rc = LDAP_BUSY;
1000                 goto error_return;
1001         }
1002
1003         /* eventually will loop through generated operational attribute types
1004          * currently implemented types include:
1005          *      entryDN, subschemaSubentry, and hasSubordinates */
1006         /* NOTE: moved before overlays callback circling because
1007          * they may modify entry and other stuff in rs */
1008         /* check for special all operational attributes ("+") type */
1009         /* FIXME: maybe we could set this flag at the operation level;
1010          * however, in principle the caller of send_search_entry() may
1011          * change the attribute list at each call */
1012         rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
1013
1014         rc = backend_operational( op, rs );
1015         if ( rc ) {
1016                 goto error_return;
1017         }
1018
1019         if ( op->o_callback ) {
1020                 rc = slap_response_play( op, rs );
1021                 if ( rc != SLAP_CB_CONTINUE ) {
1022                         goto error_return;
1023                 }
1024         }
1025
1026         Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
1027                 op->o_connid, rs->sr_entry->e_name.bv_val,
1028                 op->ors_attrsonly ? " (attrsOnly)" : "" );
1029
1030         attrsonly = op->ors_attrsonly;
1031
1032         if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
1033                 Debug( LDAP_DEBUG_ACL,
1034                         "send_search_entry: conn %lu access to entry (%s) not allowed\n", 
1035                         op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
1036
1037                 rc = LDAP_INSUFFICIENT_ACCESS;
1038                 goto error_return;
1039         }
1040
1041         if ( op->o_res_ber ) {
1042                 /* read back control or LDAP_CONNECTIONLESS */
1043             ber = op->o_res_ber;
1044         } else {
1045                 struct berval   bv;
1046
1047                 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1048                 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
1049
1050                 ber_init2( ber, &bv, LBER_USE_DER );
1051                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1052         }
1053
1054 #ifdef LDAP_CONNECTIONLESS
1055         if ( op->o_conn && op->o_conn->c_is_udp ) {
1056                 /* CONNECTIONLESS */
1057                 if ( op->o_protocol == LDAP_VERSION2 ) {
1058                 rc = ber_printf(ber, "t{O{" /*}}*/,
1059                                 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1060                 } else {
1061                 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
1062                                 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1063                 }
1064         } else
1065 #endif
1066         if ( op->o_res_ber ) {
1067                 /* read back control */
1068             rc = ber_printf( ber, "t{O{" /*}}*/,
1069                         LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1070         } else {
1071             rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
1072                         LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1073         }
1074
1075         if ( rc == -1 ) {
1076                 Debug( LDAP_DEBUG_ANY, 
1077                         "send_search_entry: conn %lu  ber_printf failed\n", 
1078                         op->o_connid, 0, 0 );
1079
1080                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1081                 set_ldap_error( rs, LDAP_OTHER, "encoding DN error" );
1082                 rc = rs->sr_err;
1083                 goto error_return;
1084         }
1085
1086         /* check for special all user attributes ("*") type */
1087         userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
1088
1089         /* create an array of arrays of flags. Each flag corresponds
1090          * to particular value of attribute and equals 1 if value matches
1091          * to ValuesReturnFilter or 0 if not
1092          */     
1093         if ( op->o_vrFilter != NULL ) {
1094                 int     k = 0;
1095                 size_t  size;
1096
1097                 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1098                         for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1099                 }
1100
1101                 size = i * sizeof(char *) + k;
1102                 if ( size > 0 ) {
1103                         char    *a_flags;
1104                         e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
1105                         if( e_flags == NULL ) {
1106                         Debug( LDAP_DEBUG_ANY, 
1107                                         "send_search_entry: conn %lu slap_sl_calloc failed\n",
1108                                         op->o_connid, 0, 0 );
1109                                 ber_free( ber, 1 );
1110         
1111                                 set_ldap_error( rs, LDAP_OTHER, "out of memory" );
1112                                 goto error_return;
1113                         }
1114                         a_flags = (char *)(e_flags + i);
1115                         memset( a_flags, 0, k );
1116                         for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
1117                                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1118                                 e_flags[i] = a_flags;
1119                                 a_flags += j;
1120                         }
1121         
1122                         rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ; 
1123                         if ( rc == -1 ) {
1124                                 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
1125                                         "conn %lu matched values filtering failed\n",
1126                                         op->o_connid, 0, 0 );
1127                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1128                                 set_ldap_error( rs, LDAP_OTHER,
1129                                         "matched values filtering error" );
1130                                 rc = rs->sr_err;
1131                                 goto error_return;
1132                         }
1133                 }
1134         }
1135
1136         for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
1137                 AttributeDescription *desc = a->a_desc;
1138                 int finish = 0;
1139
1140                 if ( rs->sr_attrs == NULL ) {
1141                         /* all user attrs request, skip operational attributes */
1142                         if( is_at_operational( desc->ad_type ) ) {
1143                                 continue;
1144                         }
1145
1146                 } else {
1147                         /* specific attrs requested */
1148                         if ( is_at_operational( desc->ad_type ) ) {
1149                                 /* if not explicitly requested */
1150                                 if ( !ad_inlist( desc, rs->sr_attrs )) {
1151                                         /* if not all op attrs requested, skip */
1152                                         if ( !SLAP_OPATTRS( rs->sr_attr_flags ))
1153                                                 continue;
1154                                         /* if DSA-specific and replicating, skip */
1155                                         if ( op->o_sync != SLAP_CONTROL_NONE &&
1156                                                 desc->ad_type->sat_usage == LDAP_SCHEMA_DSA_OPERATION )
1157                                                 continue;
1158                                 }
1159                         } else {
1160                                 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1161                                         continue;
1162                                 }
1163                         }
1164                 }
1165
1166                 if ( attrsonly ) {
1167                         if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1168                                 ACL_READ, &acl_state ) )
1169                         {
1170                                 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
1171                                         "conn %lu access to attribute %s not allowed\n",
1172                                         op->o_connid, desc->ad_cname.bv_val, 0 );
1173                                 continue;
1174                         }
1175
1176                         if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
1177                                 Debug( LDAP_DEBUG_ANY, 
1178                                         "send_search_entry: conn %lu  ber_printf failed\n", 
1179                                         op->o_connid, 0, 0 );
1180
1181                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1182                                 set_ldap_error( rs, LDAP_OTHER,
1183                                         "encoding description error");
1184                                 rc = rs->sr_err;
1185                                 goto error_return;
1186                         }
1187                         finish = 1;
1188
1189                 } else {
1190                         int first = 1;
1191                         for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
1192                                 if ( ! access_allowed( op, rs->sr_entry,
1193                                         desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
1194                                 {
1195                                         Debug( LDAP_DEBUG_ACL,
1196                                                 "send_search_entry: conn %lu "
1197                                                 "access to attribute %s, value #%d not allowed\n",
1198                                                 op->o_connid, desc->ad_cname.bv_val, i );
1199
1200                                         continue;
1201                                 }
1202
1203                                 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1204                                         continue;
1205                                 }
1206
1207                                 if ( first ) {
1208                                         first = 0;
1209                                         finish = 1;
1210                                         if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
1211                                                 Debug( LDAP_DEBUG_ANY,
1212                                                         "send_search_entry: conn %lu  ber_printf failed\n", 
1213                                                         op->o_connid, 0, 0 );
1214
1215                                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1216                                                 set_ldap_error( rs, LDAP_OTHER,
1217                                                         "encoding description error");
1218                                                 rc = rs->sr_err;
1219                                                 goto error_return;
1220                                         }
1221                                 }
1222                                 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1223                                         Debug( LDAP_DEBUG_ANY,
1224                                                 "send_search_entry: conn %lu  "
1225                                                 "ber_printf failed.\n", op->o_connid, 0, 0 );
1226
1227                                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1228                                         set_ldap_error( rs, LDAP_OTHER,
1229                                                 "encoding values error" );
1230                                         rc = rs->sr_err;
1231                                         goto error_return;
1232                                 }
1233                         }
1234                 }
1235
1236                 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1237                         Debug( LDAP_DEBUG_ANY,
1238                                 "send_search_entry: conn %lu ber_printf failed\n", 
1239                                 op->o_connid, 0, 0 );
1240
1241                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1242                         set_ldap_error( rs, LDAP_OTHER, "encode end error" );
1243                         rc = rs->sr_err;
1244                         goto error_return;
1245                 }
1246         }
1247
1248         /* NOTE: moved before overlays callback circling because
1249          * they may modify entry and other stuff in rs */
1250         if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
1251                 int     k = 0;
1252                 size_t  size;
1253
1254                 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1255                         for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1256                 }
1257
1258                 size = i * sizeof(char *) + k;
1259                 if ( size > 0 ) {
1260                         char    *a_flags, **tmp;
1261                 
1262                         /*
1263                          * Reuse previous memory - we likely need less space
1264                          * for operational attributes
1265                          */
1266                         tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
1267                                 op->o_tmpmemctx );
1268                         if ( tmp == NULL ) {
1269                                 Debug( LDAP_DEBUG_ANY,
1270                                         "send_search_entry: conn %lu "
1271                                         "not enough memory "
1272                                         "for matched values filtering\n",
1273                                         op->o_connid, 0, 0 );
1274                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1275                                 set_ldap_error( rs, LDAP_OTHER,
1276                                         "not enough memory for matched values filtering" );
1277                                 goto error_return;
1278                         }
1279                         e_flags = tmp;
1280                         a_flags = (char *)(e_flags + i);
1281                         memset( a_flags, 0, k );
1282                         for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1283                                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1284                                 e_flags[i] = a_flags;
1285                                 a_flags += j;
1286                         }
1287                         rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ; 
1288                     
1289                         if ( rc == -1 ) {
1290                                 Debug( LDAP_DEBUG_ANY,
1291                                         "send_search_entry: conn %lu "
1292                                         "matched values filtering failed\n", 
1293                                         op->o_connid, 0, 0);
1294                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1295                                 set_ldap_error( rs, LDAP_OTHER,
1296                                         "matched values filtering error" );
1297                                 rc = rs->sr_err;
1298                                 goto error_return;
1299                         }
1300                 }
1301         }
1302
1303         for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1304                 AttributeDescription *desc = a->a_desc;
1305
1306                 if ( rs->sr_attrs == NULL ) {
1307                         /* all user attrs request, skip operational attributes */
1308                         if( is_at_operational( desc->ad_type ) ) {
1309                                 continue;
1310                         }
1311
1312                 } else {
1313                         /* specific attrs requested */
1314                         if( is_at_operational( desc->ad_type ) ) {
1315                                 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) && 
1316                                         !ad_inlist( desc, rs->sr_attrs ) )
1317                                 {
1318                                         continue;
1319                                 }
1320                                 /* if DSA-specific and replicating, skip */
1321                                 if ( op->o_sync != SLAP_CONTROL_NONE &&
1322                                         desc->ad_type->sat_usage == LDAP_SCHEMA_DSA_OPERATION )
1323                                         continue;
1324                         } else {
1325                                 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1326                                         continue;
1327                                 }
1328                         }
1329                 }
1330
1331                 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1332                         ACL_READ, &acl_state ) )
1333                 {
1334                         Debug( LDAP_DEBUG_ACL,
1335                                 "send_search_entry: conn %lu "
1336                                 "access to attribute %s not allowed\n",
1337                                 op->o_connid, desc->ad_cname.bv_val, 0 );
1338
1339                         continue;
1340                 }
1341
1342                 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1343                 if ( rc == -1 ) {
1344                         Debug( LDAP_DEBUG_ANY,
1345                                 "send_search_entry: conn %lu  "
1346                                 "ber_printf failed\n", op->o_connid, 0, 0 );
1347
1348                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1349                         set_ldap_error( rs, LDAP_OTHER,
1350                                 "encoding description error" );
1351                         rc = rs->sr_err;
1352                         goto error_return;
1353                 }
1354
1355                 if ( ! attrsonly ) {
1356                         for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1357                                 if ( ! access_allowed( op, rs->sr_entry,
1358                                         desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1359                                 {
1360                                         Debug( LDAP_DEBUG_ACL,
1361                                                 "send_search_entry: conn %lu "
1362                                                 "access to %s, value %d not allowed\n",
1363                                                 op->o_connid, desc->ad_cname.bv_val, i );
1364
1365                                         continue;
1366                                 }
1367
1368                                 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1369                                         continue;
1370                                 }
1371
1372                                 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1373                                         Debug( LDAP_DEBUG_ANY,
1374                                                 "send_search_entry: conn %lu  ber_printf failed\n", 
1375                                                 op->o_connid, 0, 0 );
1376
1377                                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1378                                         set_ldap_error( rs, LDAP_OTHER,
1379                                                 "encoding values error" );
1380                                         rc = rs->sr_err;
1381                                         goto error_return;
1382                                 }
1383                         }
1384                 }
1385
1386                 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1387                         Debug( LDAP_DEBUG_ANY,
1388                                 "send_search_entry: conn %lu  ber_printf failed\n",
1389                                 op->o_connid, 0, 0 );
1390
1391                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1392                         set_ldap_error( rs, LDAP_OTHER, "encode end error" );
1393                         rc = rs->sr_err;
1394                         goto error_return;
1395                 }
1396         }
1397
1398         /* free e_flags */
1399         if ( e_flags ) {
1400                 slap_sl_free( e_flags, op->o_tmpmemctx );
1401                 e_flags = NULL;
1402         }
1403
1404         rc = ber_printf( ber, /*{{*/ "}N}" );
1405
1406         if( rc != -1 ) {
1407                 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1408         }
1409
1410         if( rc != -1 ) {
1411 #ifdef LDAP_CONNECTIONLESS
1412                 if( op->o_conn && op->o_conn->c_is_udp ) {
1413                         if ( op->o_protocol != LDAP_VERSION2 ) {
1414                                 rc = ber_printf( ber, /*{*/ "N}" );
1415                         }
1416                 } else
1417 #endif
1418                 if ( op->o_res_ber == NULL ) {
1419                         rc = ber_printf( ber, /*{*/ "N}" );
1420                 }
1421         }
1422
1423         if ( rc == -1 ) {
1424                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1425
1426                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1427                 set_ldap_error( rs, LDAP_OTHER, "encode entry end error" );
1428                 rc = rs->sr_err;
1429                 goto error_return;
1430         }
1431
1432         Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1433             op->o_log_prefix, rs->sr_entry->e_nname.bv_val, 0, 0, 0 );
1434
1435         rs_flush_entry( op, rs, NULL );
1436
1437         if ( op->o_res_ber == NULL ) {
1438                 bytes = send_ldap_ber( op, ber );
1439                 ber_free_buf( ber );
1440
1441                 if ( bytes < 0 ) {
1442                         Debug( LDAP_DEBUG_ANY,
1443                                 "send_search_entry: conn %lu  ber write failed.\n", 
1444                                 op->o_connid, 0, 0 );
1445
1446                         rc = LDAP_UNAVAILABLE;
1447                         goto error_return;
1448                 }
1449                 rs->sr_nentries++;
1450
1451                 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1452                 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
1453                 ldap_pvt_mp_add_ulong( op->o_counters->sc_entries, 1 );
1454                 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
1455                 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1456         }
1457
1458         Debug( LDAP_DEBUG_TRACE,
1459                 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1460
1461         rc = LDAP_SUCCESS;
1462
1463 error_return:;
1464         if ( op->o_callback ) {
1465                 (void)slap_cleanup_play( op, rs );
1466         }
1467
1468         if ( e_flags ) {
1469                 slap_sl_free( e_flags, op->o_tmpmemctx );
1470         }
1471
1472         /* FIXME: Can break if rs now contains an extended response */
1473         if ( rs->sr_operational_attrs ) {
1474                 attrs_free( rs->sr_operational_attrs );
1475                 rs->sr_operational_attrs = NULL;
1476         }
1477         rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1478
1479         if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
1480                 rs_flush_entry( op, rs, NULL );
1481         } else {
1482                 RS_ASSERT( (rs->sr_flags & REP_ENTRY_MASK) == 0 );
1483         }
1484
1485         if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
1486                 rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
1487                 if ( rs->sr_ctrls ) {
1488                         slap_free_ctrls( op, rs->sr_ctrls );
1489                         rs->sr_ctrls = NULL;
1490                 }
1491         }
1492
1493         return( rc );
1494 }
1495
1496 int
1497 slap_send_search_reference( Operation *op, SlapReply *rs )
1498 {
1499         BerElementBuffer berbuf;
1500         BerElement      *ber = (BerElement *) &berbuf;
1501         int rc = 0;
1502         int bytes;
1503         char *edn = rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)";
1504
1505         AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1506         AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1507
1508         rs->sr_type = REP_SEARCHREF;
1509         if ( op->o_callback ) {
1510                 rc = slap_response_play( op, rs );
1511                 if ( rc != SLAP_CB_CONTINUE ) {
1512                         goto rel;
1513                 }
1514         }
1515
1516         Debug( LDAP_DEBUG_TRACE,
1517                 "=> send_search_reference: dn=\"%s\"\n",
1518                 edn, 0, 0 );
1519
1520         if (  rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1521                 ad_entry, NULL, ACL_READ, NULL ) )
1522         {
1523                 Debug( LDAP_DEBUG_ACL,
1524                         "send_search_reference: access to entry not allowed\n",
1525                     0, 0, 0 );
1526                 rc = 1;
1527                 goto rel;
1528         }
1529
1530         if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1531                 ad_ref, NULL, ACL_READ, NULL ) )
1532         {
1533                 Debug( LDAP_DEBUG_ACL,
1534                         "send_search_reference: access "
1535                         "to reference not allowed\n",
1536                     0, 0, 0 );
1537                 rc = 1;
1538                 goto rel;
1539         }
1540
1541         if( op->o_domain_scope ) {
1542                 Debug( LDAP_DEBUG_ANY,
1543                         "send_search_reference: domainScope control in (%s)\n", 
1544                         edn, 0, 0 );
1545                 rc = 0;
1546                 goto rel;
1547         }
1548
1549         if( rs->sr_ref == NULL ) {
1550                 Debug( LDAP_DEBUG_ANY,
1551                         "send_search_reference: null ref in (%s)\n", 
1552                         edn, 0, 0 );
1553                 rc = 1;
1554                 goto rel;
1555         }
1556
1557         if( op->o_protocol < LDAP_VERSION3 ) {
1558                 rc = 0;
1559                 /* save the references for the result */
1560                 if( rs->sr_ref[0].bv_val != NULL ) {
1561                         if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1562                                 rc = LDAP_OTHER;
1563                 }
1564                 goto rel;
1565         }
1566
1567 #ifdef LDAP_CONNECTIONLESS
1568         if( op->o_conn && op->o_conn->c_is_udp ) {
1569                 ber = op->o_res_ber;
1570         } else
1571 #endif
1572         {
1573                 ber_init_w_nullc( ber, LBER_USE_DER );
1574                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1575         }
1576
1577         rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1578                 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1579
1580         if( rc != -1 ) {
1581                 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1582         }
1583
1584         if( rc != -1 ) {
1585                 rc = ber_printf( ber, /*"{"*/ "N}" );
1586         }
1587
1588         if ( rc == -1 ) {
1589                 Debug( LDAP_DEBUG_ANY,
1590                         "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1591
1592 #ifdef LDAP_CONNECTIONLESS
1593                 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1594 #endif
1595                 ber_free_buf( ber );
1596                 set_ldap_error( rs, LDAP_OTHER, "encode DN error" );
1597                 goto rel;
1598         }
1599
1600         rc = 0;
1601         rs_flush_entry( op, rs, NULL );
1602
1603 #ifdef LDAP_CONNECTIONLESS
1604         if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1605 #endif
1606         bytes = send_ldap_ber( op, ber );
1607         ber_free_buf( ber );
1608
1609         if ( bytes < 0 ) {
1610                 rc = LDAP_UNAVAILABLE;
1611         } else {
1612                 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1613                 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
1614                 ldap_pvt_mp_add_ulong( op->o_counters->sc_refs, 1 );
1615                 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
1616                 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1617         }
1618 #ifdef LDAP_CONNECTIONLESS
1619         }
1620 #endif
1621         if ( rs->sr_ref != NULL ) {
1622                 int     r;
1623
1624                 for ( r = 0; !BER_BVISNULL( &rs->sr_ref[ r ] ); r++ ) {
1625                         Statslog( LDAP_DEBUG_STATS2, "%s REF #%d \"%s\"\n",
1626                                 op->o_log_prefix, r, rs->sr_ref[0].bv_val,
1627                                 0, 0 );
1628                 }
1629
1630         } else {
1631                 Statslog( LDAP_DEBUG_STATS2, "%s REF \"(null)\"\n",
1632                         op->o_log_prefix, 0, 0, 0, 0 );
1633         }
1634
1635         Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1636
1637         if ( 0 ) {
1638 rel:
1639             rs_flush_entry( op, rs, NULL );
1640         }
1641
1642         if ( op->o_callback ) {
1643                 (void)slap_cleanup_play( op, rs );
1644         }
1645
1646         if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
1647                 rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
1648                 if ( rs->sr_ctrls ) {
1649                         slap_free_ctrls( op, rs->sr_ctrls );
1650                         rs->sr_ctrls = NULL;
1651                 }
1652         }
1653
1654         return rc;
1655 }
1656
1657 int
1658 str2result(
1659     char        *s,
1660     int         *code,
1661     char        **matched,
1662     char        **info )
1663 {
1664         int     rc;
1665         char    *c;
1666
1667         *code = LDAP_SUCCESS;
1668         *matched = NULL;
1669         *info = NULL;
1670
1671         if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1672                 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1673                     s, 0, 0 );
1674
1675                 return( -1 );
1676         }
1677
1678         rc = 0;
1679         while ( (s = strchr( s, '\n' )) != NULL ) {
1680                 *s++ = '\0';
1681                 if ( *s == '\0' ) {
1682                         break;
1683                 }
1684                 if ( (c = strchr( s, ':' )) != NULL ) {
1685                         c++;
1686                 }
1687
1688                 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1689                         char    *next = NULL;
1690                         long    retcode;
1691
1692                         if ( c == NULL ) {
1693                                 Debug( LDAP_DEBUG_ANY, "str2result (%s) missing value\n",
1694                                     s, 0, 0 );
1695                                 rc = -1;
1696                                 continue;
1697                         }
1698
1699                         while ( isspace( (unsigned char) c[ 0 ] ) ) c++;
1700                         if ( c[ 0 ] == '\0' ) {
1701                                 Debug( LDAP_DEBUG_ANY, "str2result (%s) missing or empty value\n",
1702                                     s, 0, 0 );
1703                                 rc = -1;
1704                                 continue;
1705                         }
1706
1707                         retcode = strtol( c, &next, 10 );
1708                         if ( next == NULL || next == c ) {
1709                                 Debug( LDAP_DEBUG_ANY, "str2result (%s) unable to parse value\n",
1710                                     s, 0, 0 );
1711                                 rc = -1;
1712                                 continue;
1713                         }
1714
1715                         while ( isspace( (unsigned char) next[ 0 ] ) && next[ 0 ] != '\n' )
1716                                 next++;
1717                         if ( next[ 0 ] != '\0' && next[ 0 ] != '\n' ) {
1718                                 Debug( LDAP_DEBUG_ANY, "str2result (%s) extra cruft after value\n",
1719                                     s, 0, 0 );
1720                                 rc = -1;
1721                                 continue;
1722                         }
1723
1724                         /* FIXME: what if it's larger than max int? */
1725                         *code = (int)retcode;
1726
1727                 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1728                         if ( c != NULL ) {
1729                                 *matched = c;
1730                         }
1731                 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1732                         if ( c != NULL ) {
1733                                 *info = c;
1734                         }
1735                 } else {
1736                         Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1737                             s, 0, 0 );
1738
1739                         rc = -1;
1740                 }
1741         }
1742
1743         return( rc );
1744 }
1745
1746 int slap_read_controls(
1747         Operation *op,
1748         SlapReply *rs,
1749         Entry *e,
1750         const struct berval *oid,
1751         LDAPControl **ctrl )
1752 {
1753         int rc;
1754         struct berval bv;
1755         BerElementBuffer berbuf;
1756         BerElement *ber = (BerElement *) &berbuf;
1757         LDAPControl c;
1758         Operation myop;
1759
1760         Debug( LDAP_DEBUG_ANY, "%s slap_read_controls: (%s) %s\n",
1761                 op->o_log_prefix, oid->bv_val, e->e_dn );
1762
1763         rs->sr_entry = e;
1764         rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1765                 op->o_preread_attrs : op->o_postread_attrs; 
1766
1767         bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1768         bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
1769
1770         ber_init2( ber, &bv, LBER_USE_DER );
1771         ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1772
1773         /* create new operation */
1774         myop = *op;
1775         /* FIXME: o_bd needed for ACL */
1776         myop.o_bd = op->o_bd;
1777         myop.o_res_ber = ber;
1778         myop.o_callback = NULL;
1779         myop.ors_slimit = 1;
1780         myop.ors_attrsonly = 0;
1781
1782         rc = slap_send_search_entry( &myop, rs );
1783         if( rc ) return rc;
1784
1785         rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1786
1787         if( rc == -1 ) return LDAP_OTHER;
1788
1789         c.ldctl_oid = oid->bv_val;
1790         c.ldctl_iscritical = 0;
1791
1792         if ( *ctrl == NULL ) {
1793                 /* first try */
1794                 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1795         } else {
1796                 /* retry: free previous try */
1797                 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1798         }
1799
1800         **ctrl = c;
1801         return LDAP_SUCCESS;
1802 }
1803
1804 /* Map API errors to protocol errors... */
1805 int
1806 slap_map_api2result( SlapReply *rs )
1807 {
1808         switch(rs->sr_err) {
1809         case LDAP_SERVER_DOWN:
1810                 return LDAP_UNAVAILABLE;
1811         case LDAP_LOCAL_ERROR:
1812                 return LDAP_OTHER;
1813         case LDAP_ENCODING_ERROR:
1814         case LDAP_DECODING_ERROR:
1815                 return LDAP_PROTOCOL_ERROR;
1816         case LDAP_TIMEOUT:
1817                 return LDAP_UNAVAILABLE;
1818         case LDAP_AUTH_UNKNOWN:
1819                 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1820         case LDAP_FILTER_ERROR:
1821                 rs->sr_text = "Filter error";
1822                 return LDAP_OTHER;
1823         case LDAP_USER_CANCELLED:
1824                 rs->sr_text = "User cancelled";
1825                 return LDAP_OTHER;
1826         case LDAP_PARAM_ERROR:
1827                 return LDAP_PROTOCOL_ERROR;
1828         case LDAP_NO_MEMORY:
1829                 return LDAP_OTHER;
1830         case LDAP_CONNECT_ERROR:
1831                 return LDAP_UNAVAILABLE;
1832         case LDAP_NOT_SUPPORTED:
1833                 return LDAP_UNWILLING_TO_PERFORM;
1834         case LDAP_CONTROL_NOT_FOUND:
1835                 return LDAP_PROTOCOL_ERROR;
1836         case LDAP_NO_RESULTS_RETURNED:
1837                 return LDAP_NO_SUCH_OBJECT;
1838         case LDAP_MORE_RESULTS_TO_RETURN:
1839                 rs->sr_text = "More results to return";
1840                 return LDAP_OTHER;
1841         case LDAP_CLIENT_LOOP:
1842         case LDAP_REFERRAL_LIMIT_EXCEEDED:
1843                 return LDAP_LOOP_DETECT;
1844         default:
1845                 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1846                 return rs->sr_err;
1847         }
1848 }
1849
1850
1851 slap_mask_t
1852 slap_attr_flags( AttributeName *an )
1853 {
1854         slap_mask_t     flags = SLAP_ATTRS_UNDEFINED;
1855
1856         if ( an == NULL ) {
1857                 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1858
1859         } else {
1860                 flags |= an_find( an, slap_bv_all_operational_attrs )
1861                         ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1862                 flags |= an_find( an, slap_bv_all_user_attrs )
1863                         ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;
1864         }
1865
1866         return flags;
1867 }