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