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