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