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