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