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