]> git.sur5r.net Git - openldap/blob - servers/slapd/result.c
Merge remote branch 'origin/mdb.master' into OPENLDAP_REL_ENG_2_4
[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 #define set_ldap_error( rs, err, text ) do { \
949                 (rs)->sr_err = err; (rs)->sr_text = text; } while(0)
950
951 /*
952  * returns:
953  *
954  * LDAP_SUCCESS                 entry sent
955  * LDAP_OTHER                   entry not sent (other)
956  * LDAP_INSUFFICIENT_ACCESS     entry not sent (ACL)
957  * LDAP_UNAVAILABLE             entry not sent (connection closed)
958  * LDAP_SIZELIMIT_EXCEEDED      entry not sent (caller must send sizelimitExceeded)
959  */
960
961 int
962 slap_send_search_entry( Operation *op, SlapReply *rs )
963 {
964         BerElementBuffer berbuf;
965         BerElement      *ber = (BerElement *) &berbuf;
966         Attribute       *a;
967         int             i, j, rc = LDAP_UNAVAILABLE, bytes;
968         int             userattrs;
969         AccessControlState acl_state = ACL_STATE_INIT;
970         int                      attrsonly;
971         AttributeDescription *ad_entry = slap_schema.si_ad_entry;
972
973         /* a_flags: array of flags telling if the i-th element will be
974          *          returned or filtered out
975          * e_flags: array of a_flags
976          */
977         char **e_flags = NULL;
978
979         rs->sr_type = REP_SEARCH;
980
981         if ( op->ors_slimit >= 0 && rs->sr_nentries >= op->ors_slimit ) {
982                 rc = LDAP_SIZELIMIT_EXCEEDED;
983                 goto error_return;
984         }
985
986         /* Every 64 entries, check for thread pool pause */
987         if ( ( ( rs->sr_nentries & 0x3f ) == 0x3f ) &&
988                 ldap_pvt_thread_pool_pausing( &connection_pool ) > 0 )
989         {
990                 rc = LDAP_BUSY;
991                 goto error_return;
992         }
993
994         /* eventually will loop through generated operational attribute types
995          * currently implemented types include:
996          *      entryDN, subschemaSubentry, and hasSubordinates */
997         /* NOTE: moved before overlays callback circling because
998          * they may modify entry and other stuff in rs */
999         /* check for special all operational attributes ("+") type */
1000         /* FIXME: maybe we could set this flag at the operation level;
1001          * however, in principle the caller of send_search_entry() may
1002          * change the attribute list at each call */
1003         rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
1004
1005         rc = backend_operational( op, rs );
1006         if ( rc ) {
1007                 goto error_return;
1008         }
1009
1010         if ( op->o_callback ) {
1011                 rc = slap_response_play( op, rs );
1012                 if ( rc != SLAP_CB_CONTINUE ) {
1013                         goto error_return;
1014                 }
1015         }
1016
1017         Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
1018                 op->o_connid, rs->sr_entry->e_name.bv_val,
1019                 op->ors_attrsonly ? " (attrsOnly)" : "" );
1020
1021         attrsonly = op->ors_attrsonly;
1022
1023         if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
1024                 Debug( LDAP_DEBUG_ACL,
1025                         "send_search_entry: conn %lu access to entry (%s) not allowed\n", 
1026                         op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
1027
1028                 rc = LDAP_INSUFFICIENT_ACCESS;
1029                 goto error_return;
1030         }
1031
1032         if ( op->o_res_ber ) {
1033                 /* read back control or LDAP_CONNECTIONLESS */
1034             ber = op->o_res_ber;
1035         } else {
1036                 struct berval   bv;
1037
1038                 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1039                 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
1040
1041                 ber_init2( ber, &bv, LBER_USE_DER );
1042                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1043         }
1044
1045 #ifdef LDAP_CONNECTIONLESS
1046         if ( op->o_conn && op->o_conn->c_is_udp ) {
1047                 /* CONNECTIONLESS */
1048                 if ( op->o_protocol == LDAP_VERSION2 ) {
1049                 rc = ber_printf(ber, "t{O{" /*}}*/,
1050                                 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1051                 } else {
1052                 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
1053                                 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1054                 }
1055         } else
1056 #endif
1057         if ( op->o_res_ber ) {
1058                 /* read back control */
1059             rc = ber_printf( ber, "t{O{" /*}}*/,
1060                         LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1061         } else {
1062             rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
1063                         LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
1064         }
1065
1066         if ( rc == -1 ) {
1067                 Debug( LDAP_DEBUG_ANY, 
1068                         "send_search_entry: conn %lu  ber_printf failed\n", 
1069                         op->o_connid, 0, 0 );
1070
1071                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1072                 set_ldap_error( rs, LDAP_OTHER, "encoding DN error" );
1073                 rc = rs->sr_err;
1074                 goto error_return;
1075         }
1076
1077         /* check for special all user attributes ("*") type */
1078         userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
1079
1080         /* create an array of arrays of flags. Each flag corresponds
1081          * to particular value of attribute and equals 1 if value matches
1082          * to ValuesReturnFilter or 0 if not
1083          */     
1084         if ( op->o_vrFilter != NULL ) {
1085                 int     k = 0;
1086                 size_t  size;
1087
1088                 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1089                         for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1090                 }
1091
1092                 size = i * sizeof(char *) + k;
1093                 if ( size > 0 ) {
1094                         char    *a_flags;
1095                         e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
1096                         if( e_flags == NULL ) {
1097                         Debug( LDAP_DEBUG_ANY, 
1098                                         "send_search_entry: conn %lu slap_sl_calloc failed\n",
1099                                         op->o_connid, 0, 0 );
1100                                 ber_free( ber, 1 );
1101         
1102                                 set_ldap_error( rs, LDAP_OTHER, "out of memory" );
1103                                 goto error_return;
1104                         }
1105                         a_flags = (char *)(e_flags + i);
1106                         memset( a_flags, 0, k );
1107                         for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
1108                                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1109                                 e_flags[i] = a_flags;
1110                                 a_flags += j;
1111                         }
1112         
1113                         rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ; 
1114                         if ( rc == -1 ) {
1115                                 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
1116                                         "conn %lu matched values filtering failed\n",
1117                                         op->o_connid, 0, 0 );
1118                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1119                                 set_ldap_error( rs, LDAP_OTHER,
1120                                         "matched values filtering error" );
1121                                 rc = rs->sr_err;
1122                                 goto error_return;
1123                         }
1124                 }
1125         }
1126
1127         for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
1128                 AttributeDescription *desc = a->a_desc;
1129                 int finish = 0;
1130
1131                 if ( rs->sr_attrs == NULL ) {
1132                         /* all user attrs request, skip operational attributes */
1133                         if( is_at_operational( desc->ad_type ) ) {
1134                                 continue;
1135                         }
1136
1137                 } else {
1138                         /* specific attrs requested */
1139                         if ( is_at_operational( desc->ad_type ) ) {
1140                                 /* if not explicitly requested */
1141                                 if ( !ad_inlist( desc, rs->sr_attrs )) {
1142                                         /* if not all op attrs requested, skip */
1143                                         if ( !SLAP_OPATTRS( rs->sr_attr_flags ))
1144                                                 continue;
1145                                         /* if DSA-specific and replicating, skip */
1146                                         if ( op->o_sync != SLAP_CONTROL_NONE &&
1147                                                 desc->ad_type->sat_usage == LDAP_SCHEMA_DSA_OPERATION )
1148                                                 continue;
1149                                 }
1150                         } else {
1151                                 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1152                                         continue;
1153                                 }
1154                         }
1155                 }
1156
1157                 if ( attrsonly ) {
1158                         if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1159                                 ACL_READ, &acl_state ) )
1160                         {
1161                                 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
1162                                         "conn %lu access to attribute %s not allowed\n",
1163                                         op->o_connid, desc->ad_cname.bv_val, 0 );
1164                                 continue;
1165                         }
1166
1167                         if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
1168                                 Debug( LDAP_DEBUG_ANY, 
1169                                         "send_search_entry: conn %lu  ber_printf failed\n", 
1170                                         op->o_connid, 0, 0 );
1171
1172                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1173                                 set_ldap_error( rs, LDAP_OTHER,
1174                                         "encoding description error");
1175                                 rc = rs->sr_err;
1176                                 goto error_return;
1177                         }
1178                         finish = 1;
1179
1180                 } else {
1181                         int first = 1;
1182                         for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
1183                                 if ( ! access_allowed( op, rs->sr_entry,
1184                                         desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
1185                                 {
1186                                         Debug( LDAP_DEBUG_ACL,
1187                                                 "send_search_entry: conn %lu "
1188                                                 "access to attribute %s, value #%d not allowed\n",
1189                                                 op->o_connid, desc->ad_cname.bv_val, i );
1190
1191                                         continue;
1192                                 }
1193
1194                                 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1195                                         continue;
1196                                 }
1197
1198                                 if ( first ) {
1199                                         first = 0;
1200                                         finish = 1;
1201                                         if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
1202                                                 Debug( LDAP_DEBUG_ANY,
1203                                                         "send_search_entry: conn %lu  ber_printf failed\n", 
1204                                                         op->o_connid, 0, 0 );
1205
1206                                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1207                                                 set_ldap_error( rs, LDAP_OTHER,
1208                                                         "encoding description error");
1209                                                 rc = rs->sr_err;
1210                                                 goto error_return;
1211                                         }
1212                                 }
1213                                 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1214                                         Debug( LDAP_DEBUG_ANY,
1215                                                 "send_search_entry: conn %lu  "
1216                                                 "ber_printf failed.\n", op->o_connid, 0, 0 );
1217
1218                                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1219                                         set_ldap_error( rs, LDAP_OTHER,
1220                                                 "encoding values error" );
1221                                         rc = rs->sr_err;
1222                                         goto error_return;
1223                                 }
1224                         }
1225                 }
1226
1227                 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1228                         Debug( LDAP_DEBUG_ANY,
1229                                 "send_search_entry: conn %lu ber_printf failed\n", 
1230                                 op->o_connid, 0, 0 );
1231
1232                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1233                         set_ldap_error( rs, LDAP_OTHER, "encode end error" );
1234                         rc = rs->sr_err;
1235                         goto error_return;
1236                 }
1237         }
1238
1239         /* NOTE: moved before overlays callback circling because
1240          * they may modify entry and other stuff in rs */
1241         if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
1242                 int     k = 0;
1243                 size_t  size;
1244
1245                 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1246                         for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1247                 }
1248
1249                 size = i * sizeof(char *) + k;
1250                 if ( size > 0 ) {
1251                         char    *a_flags, **tmp;
1252                 
1253                         /*
1254                          * Reuse previous memory - we likely need less space
1255                          * for operational attributes
1256                          */
1257                         tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
1258                                 op->o_tmpmemctx );
1259                         if ( tmp == NULL ) {
1260                                 Debug( LDAP_DEBUG_ANY,
1261                                         "send_search_entry: conn %lu "
1262                                         "not enough memory "
1263                                         "for matched values filtering\n",
1264                                         op->o_connid, 0, 0 );
1265                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1266                                 set_ldap_error( rs, LDAP_OTHER,
1267                                         "not enough memory for matched values filtering" );
1268                                 goto error_return;
1269                         }
1270                         e_flags = tmp;
1271                         a_flags = (char *)(e_flags + i);
1272                         memset( a_flags, 0, k );
1273                         for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1274                                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1275                                 e_flags[i] = a_flags;
1276                                 a_flags += j;
1277                         }
1278                         rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ; 
1279                     
1280                         if ( rc == -1 ) {
1281                                 Debug( LDAP_DEBUG_ANY,
1282                                         "send_search_entry: conn %lu "
1283                                         "matched values filtering failed\n", 
1284                                         op->o_connid, 0, 0);
1285                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1286                                 set_ldap_error( rs, LDAP_OTHER,
1287                                         "matched values filtering error" );
1288                                 rc = rs->sr_err;
1289                                 goto error_return;
1290                         }
1291                 }
1292         }
1293
1294         for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1295                 AttributeDescription *desc = a->a_desc;
1296
1297                 if ( rs->sr_attrs == NULL ) {
1298                         /* all user attrs request, skip operational attributes */
1299                         if( is_at_operational( desc->ad_type ) ) {
1300                                 continue;
1301                         }
1302
1303                 } else {
1304                         /* specific attrs requested */
1305                         if( is_at_operational( desc->ad_type ) ) {
1306                                 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) && 
1307                                         !ad_inlist( desc, rs->sr_attrs ) )
1308                                 {
1309                                         continue;
1310                                 }
1311                                 /* if DSA-specific and replicating, skip */
1312                                 if ( op->o_sync != SLAP_CONTROL_NONE &&
1313                                         desc->ad_type->sat_usage == LDAP_SCHEMA_DSA_OPERATION )
1314                                         continue;
1315                         } else {
1316                                 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1317                                         continue;
1318                                 }
1319                         }
1320                 }
1321
1322                 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1323                         ACL_READ, &acl_state ) )
1324                 {
1325                         Debug( LDAP_DEBUG_ACL,
1326                                 "send_search_entry: conn %lu "
1327                                 "access to attribute %s not allowed\n",
1328                                 op->o_connid, desc->ad_cname.bv_val, 0 );
1329
1330                         continue;
1331                 }
1332
1333                 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1334                 if ( rc == -1 ) {
1335                         Debug( LDAP_DEBUG_ANY,
1336                                 "send_search_entry: conn %lu  "
1337                                 "ber_printf failed\n", op->o_connid, 0, 0 );
1338
1339                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1340                         set_ldap_error( rs, LDAP_OTHER,
1341                                 "encoding description error" );
1342                         rc = rs->sr_err;
1343                         goto error_return;
1344                 }
1345
1346                 if ( ! attrsonly ) {
1347                         for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1348                                 if ( ! access_allowed( op, rs->sr_entry,
1349                                         desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1350                                 {
1351                                         Debug( LDAP_DEBUG_ACL,
1352                                                 "send_search_entry: conn %lu "
1353                                                 "access to %s, value %d not allowed\n",
1354                                                 op->o_connid, desc->ad_cname.bv_val, i );
1355
1356                                         continue;
1357                                 }
1358
1359                                 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1360                                         continue;
1361                                 }
1362
1363                                 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1364                                         Debug( LDAP_DEBUG_ANY,
1365                                                 "send_search_entry: conn %lu  ber_printf failed\n", 
1366                                                 op->o_connid, 0, 0 );
1367
1368                                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1369                                         set_ldap_error( rs, LDAP_OTHER,
1370                                                 "encoding values error" );
1371                                         rc = rs->sr_err;
1372                                         goto error_return;
1373                                 }
1374                         }
1375                 }
1376
1377                 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1378                         Debug( LDAP_DEBUG_ANY,
1379                                 "send_search_entry: conn %lu  ber_printf failed\n",
1380                                 op->o_connid, 0, 0 );
1381
1382                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1383                         set_ldap_error( rs, LDAP_OTHER, "encode end error" );
1384                         rc = rs->sr_err;
1385                         goto error_return;
1386                 }
1387         }
1388
1389         /* free e_flags */
1390         if ( e_flags ) {
1391                 slap_sl_free( e_flags, op->o_tmpmemctx );
1392                 e_flags = NULL;
1393         }
1394
1395         rc = ber_printf( ber, /*{{*/ "}N}" );
1396
1397         if( rc != -1 ) {
1398                 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1399         }
1400
1401         if( rc != -1 ) {
1402 #ifdef LDAP_CONNECTIONLESS
1403                 if( op->o_conn && op->o_conn->c_is_udp ) {
1404                         if ( op->o_protocol != LDAP_VERSION2 ) {
1405                                 rc = ber_printf( ber, /*{*/ "N}" );
1406                         }
1407                 } else
1408 #endif
1409                 if ( op->o_res_ber == NULL ) {
1410                         rc = ber_printf( ber, /*{*/ "N}" );
1411                 }
1412         }
1413
1414         if ( rc == -1 ) {
1415                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1416
1417                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1418                 set_ldap_error( rs, LDAP_OTHER, "encode entry end error" );
1419                 rc = rs->sr_err;
1420                 goto error_return;
1421         }
1422
1423         Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1424             op->o_log_prefix, rs->sr_entry->e_nname.bv_val, 0, 0, 0 );
1425
1426         rs_flush_entry( op, rs, NULL );
1427
1428         if ( op->o_res_ber == NULL ) {
1429                 bytes = send_ldap_ber( op, ber );
1430                 ber_free_buf( ber );
1431
1432                 if ( bytes < 0 ) {
1433                         Debug( LDAP_DEBUG_ANY,
1434                                 "send_search_entry: conn %lu  ber write failed.\n", 
1435                                 op->o_connid, 0, 0 );
1436
1437                         rc = LDAP_UNAVAILABLE;
1438                         goto error_return;
1439                 }
1440                 rs->sr_nentries++;
1441
1442                 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1443                 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
1444                 ldap_pvt_mp_add_ulong( op->o_counters->sc_entries, 1 );
1445                 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
1446                 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1447         }
1448
1449         Debug( LDAP_DEBUG_TRACE,
1450                 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1451
1452         rc = LDAP_SUCCESS;
1453
1454 error_return:;
1455         if ( op->o_callback ) {
1456                 (void)slap_cleanup_play( op, rs );
1457         }
1458
1459         if ( e_flags ) {
1460                 slap_sl_free( e_flags, op->o_tmpmemctx );
1461         }
1462
1463         /* FIXME: Can break if rs now contains an extended response */
1464         if ( rs->sr_operational_attrs ) {
1465                 attrs_free( rs->sr_operational_attrs );
1466                 rs->sr_operational_attrs = NULL;
1467         }
1468         rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1469
1470         if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
1471                 rs_flush_entry( op, rs, NULL );
1472         } else {
1473                 RS_ASSERT( (rs->sr_flags & REP_ENTRY_MASK) == 0 );
1474         }
1475
1476         if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
1477                 rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
1478                 if ( rs->sr_ctrls ) {
1479                         slap_free_ctrls( op, rs->sr_ctrls );
1480                         rs->sr_ctrls = NULL;
1481                 }
1482         }
1483
1484         return( rc );
1485 }
1486
1487 int
1488 slap_send_search_reference( Operation *op, SlapReply *rs )
1489 {
1490         BerElementBuffer berbuf;
1491         BerElement      *ber = (BerElement *) &berbuf;
1492         int rc = 0;
1493         int bytes;
1494         char *edn = rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)";
1495
1496         AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1497         AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1498
1499         rs->sr_type = REP_SEARCHREF;
1500         if ( op->o_callback ) {
1501                 rc = slap_response_play( op, rs );
1502                 if ( rc != SLAP_CB_CONTINUE ) {
1503                         goto rel;
1504                 }
1505         }
1506
1507         Debug( LDAP_DEBUG_TRACE,
1508                 "=> send_search_reference: dn=\"%s\"\n",
1509                 edn, 0, 0 );
1510
1511         if (  rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1512                 ad_entry, NULL, ACL_READ, NULL ) )
1513         {
1514                 Debug( LDAP_DEBUG_ACL,
1515                         "send_search_reference: access to entry not allowed\n",
1516                     0, 0, 0 );
1517                 rc = 1;
1518                 goto rel;
1519         }
1520
1521         if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1522                 ad_ref, NULL, ACL_READ, NULL ) )
1523         {
1524                 Debug( LDAP_DEBUG_ACL,
1525                         "send_search_reference: access "
1526                         "to reference not allowed\n",
1527                     0, 0, 0 );
1528                 rc = 1;
1529                 goto rel;
1530         }
1531
1532         if( op->o_domain_scope ) {
1533                 Debug( LDAP_DEBUG_ANY,
1534                         "send_search_reference: domainScope control in (%s)\n", 
1535                         edn, 0, 0 );
1536                 rc = 0;
1537                 goto rel;
1538         }
1539
1540         if( rs->sr_ref == NULL ) {
1541                 Debug( LDAP_DEBUG_ANY,
1542                         "send_search_reference: null ref in (%s)\n", 
1543                         edn, 0, 0 );
1544                 rc = 1;
1545                 goto rel;
1546         }
1547
1548         if( op->o_protocol < LDAP_VERSION3 ) {
1549                 rc = 0;
1550                 /* save the references for the result */
1551                 if( rs->sr_ref[0].bv_val != NULL ) {
1552                         if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1553                                 rc = LDAP_OTHER;
1554                 }
1555                 goto rel;
1556         }
1557
1558 #ifdef LDAP_CONNECTIONLESS
1559         if( op->o_conn && op->o_conn->c_is_udp ) {
1560                 ber = op->o_res_ber;
1561         } else
1562 #endif
1563         {
1564                 ber_init_w_nullc( ber, LBER_USE_DER );
1565                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1566         }
1567
1568         rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1569                 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1570
1571         if( rc != -1 ) {
1572                 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1573         }
1574
1575         if( rc != -1 ) {
1576                 rc = ber_printf( ber, /*"{"*/ "N}" );
1577         }
1578
1579         if ( rc == -1 ) {
1580                 Debug( LDAP_DEBUG_ANY,
1581                         "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1582
1583 #ifdef LDAP_CONNECTIONLESS
1584                 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1585 #endif
1586                 ber_free_buf( ber );
1587                 set_ldap_error( rs, LDAP_OTHER, "encode DN error" );
1588                 goto rel;
1589         }
1590
1591         rc = 0;
1592         rs_flush_entry( op, rs, NULL );
1593
1594 #ifdef LDAP_CONNECTIONLESS
1595         if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1596 #endif
1597         bytes = send_ldap_ber( op, ber );
1598         ber_free_buf( ber );
1599
1600         if ( bytes < 0 ) {
1601                 rc = LDAP_UNAVAILABLE;
1602         } else {
1603                 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1604                 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
1605                 ldap_pvt_mp_add_ulong( op->o_counters->sc_refs, 1 );
1606                 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
1607                 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1608         }
1609 #ifdef LDAP_CONNECTIONLESS
1610         }
1611 #endif
1612         if ( rs->sr_ref != NULL ) {
1613                 int     r;
1614
1615                 for ( r = 0; !BER_BVISNULL( &rs->sr_ref[ r ] ); r++ ) {
1616                         Statslog( LDAP_DEBUG_STATS2, "%s REF #%d \"%s\"\n",
1617                                 op->o_log_prefix, r, rs->sr_ref[0].bv_val,
1618                                 0, 0 );
1619                 }
1620
1621         } else {
1622                 Statslog( LDAP_DEBUG_STATS2, "%s REF \"(null)\"\n",
1623                         op->o_log_prefix, 0, 0, 0, 0 );
1624         }
1625
1626         Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1627
1628         if ( 0 ) {
1629 rel:
1630             rs_flush_entry( op, rs, NULL );
1631         }
1632
1633         if ( op->o_callback ) {
1634                 (void)slap_cleanup_play( op, rs );
1635         }
1636
1637         if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
1638                 rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
1639                 if ( rs->sr_ctrls ) {
1640                         slap_free_ctrls( op, rs->sr_ctrls );
1641                         rs->sr_ctrls = NULL;
1642                 }
1643         }
1644
1645         return rc;
1646 }
1647
1648 int
1649 str2result(
1650     char        *s,
1651     int         *code,
1652     char        **matched,
1653     char        **info )
1654 {
1655         int     rc;
1656         char    *c;
1657
1658         *code = LDAP_SUCCESS;
1659         *matched = NULL;
1660         *info = NULL;
1661
1662         if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1663                 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1664                     s, 0, 0 );
1665
1666                 return( -1 );
1667         }
1668
1669         rc = 0;
1670         while ( (s = strchr( s, '\n' )) != NULL ) {
1671                 *s++ = '\0';
1672                 if ( *s == '\0' ) {
1673                         break;
1674                 }
1675                 if ( (c = strchr( s, ':' )) != NULL ) {
1676                         c++;
1677                 }
1678
1679                 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1680                         char    *next = NULL;
1681                         long    retcode;
1682
1683                         if ( c == NULL ) {
1684                                 Debug( LDAP_DEBUG_ANY, "str2result (%s) missing value\n",
1685                                     s, 0, 0 );
1686                                 rc = -1;
1687                                 continue;
1688                         }
1689
1690                         while ( isspace( (unsigned char) c[ 0 ] ) ) c++;
1691                         if ( c[ 0 ] == '\0' ) {
1692                                 Debug( LDAP_DEBUG_ANY, "str2result (%s) missing or empty value\n",
1693                                     s, 0, 0 );
1694                                 rc = -1;
1695                                 continue;
1696                         }
1697
1698                         retcode = strtol( c, &next, 10 );
1699                         if ( next == NULL || next == c ) {
1700                                 Debug( LDAP_DEBUG_ANY, "str2result (%s) unable to parse value\n",
1701                                     s, 0, 0 );
1702                                 rc = -1;
1703                                 continue;
1704                         }
1705
1706                         while ( isspace( (unsigned char) next[ 0 ] ) && next[ 0 ] != '\n' )
1707                                 next++;
1708                         if ( next[ 0 ] != '\0' && next[ 0 ] != '\n' ) {
1709                                 Debug( LDAP_DEBUG_ANY, "str2result (%s) extra cruft after value\n",
1710                                     s, 0, 0 );
1711                                 rc = -1;
1712                                 continue;
1713                         }
1714
1715                         /* FIXME: what if it's larger than max int? */
1716                         *code = (int)retcode;
1717
1718                 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1719                         if ( c != NULL ) {
1720                                 *matched = c;
1721                         }
1722                 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1723                         if ( c != NULL ) {
1724                                 *info = c;
1725                         }
1726                 } else {
1727                         Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1728                             s, 0, 0 );
1729
1730                         rc = -1;
1731                 }
1732         }
1733
1734         return( rc );
1735 }
1736
1737 int slap_read_controls(
1738         Operation *op,
1739         SlapReply *rs,
1740         Entry *e,
1741         const struct berval *oid,
1742         LDAPControl **ctrl )
1743 {
1744         int rc;
1745         struct berval bv;
1746         BerElementBuffer berbuf;
1747         BerElement *ber = (BerElement *) &berbuf;
1748         LDAPControl c;
1749         Operation myop;
1750
1751         Debug( LDAP_DEBUG_ANY, "%s slap_read_controls: (%s) %s\n",
1752                 op->o_log_prefix, oid->bv_val, e->e_dn );
1753
1754         rs->sr_entry = e;
1755         rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1756                 op->o_preread_attrs : op->o_postread_attrs; 
1757
1758         bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1759         bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
1760
1761         ber_init2( ber, &bv, LBER_USE_DER );
1762         ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1763
1764         /* create new operation */
1765         myop = *op;
1766         /* FIXME: o_bd needed for ACL */
1767         myop.o_bd = op->o_bd;
1768         myop.o_res_ber = ber;
1769         myop.o_callback = NULL;
1770         myop.ors_slimit = 1;
1771         myop.ors_attrsonly = 0;
1772
1773         rc = slap_send_search_entry( &myop, rs );
1774         if( rc ) return rc;
1775
1776         rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1777
1778         if( rc == -1 ) return LDAP_OTHER;
1779
1780         c.ldctl_oid = oid->bv_val;
1781         c.ldctl_iscritical = 0;
1782
1783         if ( *ctrl == NULL ) {
1784                 /* first try */
1785                 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1786         } else {
1787                 /* retry: free previous try */
1788                 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1789         }
1790
1791         **ctrl = c;
1792         return LDAP_SUCCESS;
1793 }
1794
1795 /* Map API errors to protocol errors... */
1796 int
1797 slap_map_api2result( SlapReply *rs )
1798 {
1799         switch(rs->sr_err) {
1800         case LDAP_SERVER_DOWN:
1801                 return LDAP_UNAVAILABLE;
1802         case LDAP_LOCAL_ERROR:
1803                 return LDAP_OTHER;
1804         case LDAP_ENCODING_ERROR:
1805         case LDAP_DECODING_ERROR:
1806                 return LDAP_PROTOCOL_ERROR;
1807         case LDAP_TIMEOUT:
1808                 return LDAP_UNAVAILABLE;
1809         case LDAP_AUTH_UNKNOWN:
1810                 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1811         case LDAP_FILTER_ERROR:
1812                 rs->sr_text = "Filter error";
1813                 return LDAP_OTHER;
1814         case LDAP_USER_CANCELLED:
1815                 rs->sr_text = "User cancelled";
1816                 return LDAP_OTHER;
1817         case LDAP_PARAM_ERROR:
1818                 return LDAP_PROTOCOL_ERROR;
1819         case LDAP_NO_MEMORY:
1820                 return LDAP_OTHER;
1821         case LDAP_CONNECT_ERROR:
1822                 return LDAP_UNAVAILABLE;
1823         case LDAP_NOT_SUPPORTED:
1824                 return LDAP_UNWILLING_TO_PERFORM;
1825         case LDAP_CONTROL_NOT_FOUND:
1826                 return LDAP_PROTOCOL_ERROR;
1827         case LDAP_NO_RESULTS_RETURNED:
1828                 return LDAP_NO_SUCH_OBJECT;
1829         case LDAP_MORE_RESULTS_TO_RETURN:
1830                 rs->sr_text = "More results to return";
1831                 return LDAP_OTHER;
1832         case LDAP_CLIENT_LOOP:
1833         case LDAP_REFERRAL_LIMIT_EXCEEDED:
1834                 return LDAP_LOOP_DETECT;
1835         default:
1836                 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1837                 return rs->sr_err;
1838         }
1839 }
1840
1841
1842 slap_mask_t
1843 slap_attr_flags( AttributeName *an )
1844 {
1845         slap_mask_t     flags = SLAP_ATTRS_UNDEFINED;
1846
1847         if ( an == NULL ) {
1848                 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1849
1850         } else {
1851                 flags |= an_find( an, slap_bv_all_operational_attrs )
1852                         ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1853                 flags |= an_find( an, slap_bv_all_user_attrs )
1854                         ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;
1855         }
1856
1857         return flags;
1858 }