]> git.sur5r.net Git - openldap/blob - servers/slapd/result.c
fix previous commit (ITS#5819)
[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                         Statslog( LDAP_DEBUG_STATS,
653                                 "%s SEARCH RESULT tag=%lu err=%d nentries=%d text=%s\n",
654                                 op->o_log_prefix, rs->sr_tag, rs->sr_err,
655                                 rs->sr_nentries, rs->sr_text ? rs->sr_text : "" );
656                 } else {
657                         Statslog( LDAP_DEBUG_STATS,
658                                 "%s RESULT tag=%lu err=%d text=%s\n",
659                                 op->o_log_prefix, rs->sr_tag, rs->sr_err,
660                                 rs->sr_text ? rs->sr_text : "", 0 );
661                 }
662         }
663
664         if( tmp != NULL ) ch_free(tmp);
665         rs->sr_text = otext;
666         rs->sr_ref = oref;
667 }
668
669 void
670 send_ldap_sasl( Operation *op, SlapReply *rs )
671 {
672         rs->sr_type = REP_SASL;
673         Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
674                 rs->sr_err,
675                 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
676
677         rs->sr_tag = slap_req2res( op->o_tag );
678         rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
679
680         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
681                 Statslog( LDAP_DEBUG_STATS,
682                         "%s RESULT tag=%lu err=%d text=%s\n",
683                         op->o_log_prefix, rs->sr_tag, rs->sr_err,
684                         rs->sr_text ? rs->sr_text : "", 0 );
685         }
686 }
687
688 void
689 slap_send_ldap_extended( Operation *op, SlapReply *rs )
690 {
691         rs->sr_type = REP_EXTENDED;
692
693         Debug( LDAP_DEBUG_TRACE,
694                 "send_ldap_extended: err=%d oid=%s len=%ld\n",
695                 rs->sr_err,
696                 rs->sr_rspoid ? rs->sr_rspoid : "",
697                 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
698
699         rs->sr_tag = slap_req2res( op->o_tag );
700         rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
701
702         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
703                 Statslog( LDAP_DEBUG_STATS,
704                         "%s RESULT oid=%s err=%d text=%s\n",
705                         op->o_log_prefix, rs->sr_rspoid ? rs->sr_rspoid : "",
706                         rs->sr_err, rs->sr_text ? rs->sr_text : "", 0 );
707         }
708 }
709
710 void
711 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
712 {
713         rs->sr_type = REP_INTERMEDIATE;
714         Debug( LDAP_DEBUG_TRACE,
715                 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
716                 rs->sr_err,
717                 rs->sr_rspoid ? rs->sr_rspoid : "",
718                 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
719         rs->sr_tag = LDAP_RES_INTERMEDIATE;
720         rs->sr_msgid = op->o_msgid;
721         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
722                 Statslog( LDAP_DEBUG_STATS2,
723                         "%s INTERM oid=%s\n",
724                         op->o_log_prefix,
725                         rs->sr_rspoid ? rs->sr_rspoid : "", 0, 0, 0 );
726         }
727 }
728
729 /*
730  * returns:
731  *
732  * LDAP_SUCCESS                 entry sent
733  * LDAP_OTHER                   entry not sent (other)
734  * LDAP_INSUFFICIENT_ACCESS     entry not sent (ACL)
735  * LDAP_UNAVAILABLE             entry not sent (connection closed)
736  * LDAP_SIZELIMIT_EXCEEDED      entry not sent (caller must send sizelimitExceeded)
737  */
738
739 int
740 slap_send_search_entry( Operation *op, SlapReply *rs )
741 {
742         BerElementBuffer berbuf;
743         BerElement      *ber = (BerElement *) &berbuf;
744         Attribute       *a;
745         int             i, j, rc = LDAP_UNAVAILABLE, bytes;
746         int             userattrs;
747         AccessControlState acl_state = ACL_STATE_INIT;
748         int                      attrsonly;
749         AttributeDescription *ad_entry = slap_schema.si_ad_entry;
750
751         /* a_flags: array of flags telling if the i-th element will be
752          *          returned or filtered out
753          * e_flags: array of a_flags
754          */
755         char **e_flags = NULL;
756
757         if ( op->ors_slimit >= 0 && rs->sr_nentries >= op->ors_slimit ) {
758                 return LDAP_SIZELIMIT_EXCEEDED;
759         }
760
761         /* Every 64 entries, check for thread pool pause */
762         if ( ( ( rs->sr_nentries & 0x3f ) == 0x3f ) &&
763                 ldap_pvt_thread_pool_pausing( &connection_pool ) > 0 )
764         {
765                 return LDAP_BUSY;
766         }
767
768         rs->sr_type = REP_SEARCH;
769
770         /* eventually will loop through generated operational attribute types
771          * currently implemented types include:
772          *      entryDN, subschemaSubentry, and hasSubordinates */
773         /* NOTE: moved before overlays callback circling because
774          * they may modify entry and other stuff in rs */
775         /* check for special all operational attributes ("+") type */
776         /* FIXME: maybe we could set this flag at the operation level;
777          * however, in principle the caller of send_search_entry() may
778          * change the attribute list at each call */
779         rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
780
781         rc = backend_operational( op, rs );
782         if ( rc ) {
783                 goto error_return;
784         }
785
786         if ( op->o_callback ) {
787                 rc = slap_response_play( op, rs );
788                 if ( rc != SLAP_CB_CONTINUE ) {
789                         goto error_return;
790                 }
791         }
792
793         Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
794                 op->o_connid, rs->sr_entry->e_name.bv_val,
795                 op->ors_attrsonly ? " (attrsOnly)" : "" );
796
797         attrsonly = op->ors_attrsonly;
798
799         if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
800                 Debug( LDAP_DEBUG_ACL,
801                         "send_search_entry: conn %lu access to entry (%s) not allowed\n", 
802                         op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
803
804                 rc = LDAP_INSUFFICIENT_ACCESS;
805                 goto error_return;
806         }
807
808         if ( op->o_res_ber ) {
809                 /* read back control or LDAP_CONNECTIONLESS */
810             ber = op->o_res_ber;
811         } else {
812                 struct berval   bv;
813
814                 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
815                 bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
816
817                 ber_init2( ber, &bv, LBER_USE_DER );
818                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
819         }
820
821 #ifdef LDAP_CONNECTIONLESS
822         if ( op->o_conn && op->o_conn->c_is_udp ) {
823                 /* CONNECTIONLESS */
824                 if ( op->o_protocol == LDAP_VERSION2 ) {
825                 rc = ber_printf(ber, "t{O{" /*}}*/,
826                                 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
827                 } else {
828                 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
829                                 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
830                 }
831         } else
832 #endif
833         if ( op->o_res_ber ) {
834                 /* read back control */
835             rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
836         } else {
837             rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
838                         LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
839         }
840
841         if ( rc == -1 ) {
842                 Debug( LDAP_DEBUG_ANY, 
843                         "send_search_entry: conn %lu  ber_printf failed\n", 
844                         op->o_connid, 0, 0 );
845
846                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
847                 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
848                 rc = rs->sr_err;
849                 goto error_return;
850         }
851
852         /* check for special all user attributes ("*") type */
853         userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
854
855         /* create an array of arrays of flags. Each flag corresponds
856          * to particular value of attribute and equals 1 if value matches
857          * to ValuesReturnFilter or 0 if not
858          */     
859         if ( op->o_vrFilter != NULL ) {
860                 int     k = 0;
861                 size_t  size;
862
863                 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
864                         for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
865                 }
866
867                 size = i * sizeof(char *) + k;
868                 if ( size > 0 ) {
869                         char    *a_flags;
870                         e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
871                         if( e_flags == NULL ) {
872                         Debug( LDAP_DEBUG_ANY, 
873                                         "send_search_entry: conn %lu slap_sl_calloc failed\n",
874                                         op->o_connid ? op->o_connid : 0, 0, 0 );
875                                 ber_free( ber, 1 );
876         
877                                 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
878                                 goto error_return;
879                         }
880                         a_flags = (char *)(e_flags + i);
881                         memset( a_flags, 0, k );
882                         for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
883                                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
884                                 e_flags[i] = a_flags;
885                                 a_flags += j;
886                         }
887         
888                         rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ; 
889                         if ( rc == -1 ) {
890                                 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
891                                         "conn %lu matched values filtering failed\n",
892                                         op->o_connid ? op->o_connid : 0, 0, 0 );
893                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
894                                 send_ldap_error( op, rs, LDAP_OTHER,
895                                         "matched values filtering error" );
896                                 rc = rs->sr_err;
897                                 goto error_return;
898                         }
899                 }
900         }
901
902         for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
903                 AttributeDescription *desc = a->a_desc;
904                 int finish = 0;
905
906                 if ( rs->sr_attrs == NULL ) {
907                         /* all user attrs request, skip operational attributes */
908                         if( is_at_operational( desc->ad_type ) ) {
909                                 continue;
910                         }
911
912                 } else {
913                         /* specific attrs requested */
914                         if ( is_at_operational( desc->ad_type ) ) {
915                                 /* if not explicitly requested */
916                                 if ( !ad_inlist( desc, rs->sr_attrs )) {
917                                         /* if not all op attrs requested, skip */
918                                         if ( !SLAP_OPATTRS( rs->sr_attr_flags ))
919                                                 continue;
920                                         /* if DSA-specific and replicating, skip */
921                                         if ( op->o_sync != SLAP_CONTROL_NONE &&
922                                                 desc->ad_type->sat_usage == LDAP_SCHEMA_DSA_OPERATION )
923                                                 continue;
924                                 }
925                         } else {
926                                 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
927                                         continue;
928                                 }
929                         }
930                 }
931
932                 if ( attrsonly ) {
933                         if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
934                                 ACL_READ, &acl_state ) )
935                         {
936                                 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
937                                         "conn %lu access to attribute %s not allowed\n",
938                                         op->o_connid, desc->ad_cname.bv_val, 0 );
939                                 continue;
940                         }
941
942                         if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
943                                 Debug( LDAP_DEBUG_ANY, 
944                                         "send_search_entry: conn %lu  ber_printf failed\n", 
945                                         op->o_connid, 0, 0 );
946
947                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
948                                 send_ldap_error( op, rs, LDAP_OTHER,
949                                         "encoding description error");
950                                 rc = rs->sr_err;
951                                 goto error_return;
952                         }
953                         finish = 1;
954
955                 } else {
956                         int first = 1;
957                         for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
958                                 if ( ! access_allowed( op, rs->sr_entry,
959                                         desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
960                                 {
961                                         Debug( LDAP_DEBUG_ACL,
962                                                 "send_search_entry: conn %lu "
963                                                 "access to attribute %s, value #%d not allowed\n",
964                                                 op->o_connid, desc->ad_cname.bv_val, i );
965
966                                         continue;
967                                 }
968
969                                 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
970                                         continue;
971                                 }
972
973                                 if ( first ) {
974                                         first = 0;
975                                         finish = 1;
976                                         if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
977                                                 Debug( LDAP_DEBUG_ANY,
978                                                         "send_search_entry: conn %lu  ber_printf failed\n", 
979                                                         op->o_connid, 0, 0 );
980
981                                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
982                                                 send_ldap_error( op, rs, LDAP_OTHER,
983                                                         "encoding description error");
984                                                 rc = rs->sr_err;
985                                                 goto error_return;
986                                         }
987                                 }
988                                 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
989                                         Debug( LDAP_DEBUG_ANY,
990                                                 "send_search_entry: conn %lu  "
991                                                 "ber_printf failed.\n", op->o_connid, 0, 0 );
992
993                                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
994                                         send_ldap_error( op, rs, LDAP_OTHER,
995                                                 "encoding values error" );
996                                         rc = rs->sr_err;
997                                         goto error_return;
998                                 }
999                         }
1000                 }
1001
1002                 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1003                         Debug( LDAP_DEBUG_ANY,
1004                                 "send_search_entry: conn %lu ber_printf failed\n", 
1005                                 op->o_connid, 0, 0 );
1006
1007                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1008                         send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1009                         rc = rs->sr_err;
1010                         goto error_return;
1011                 }
1012         }
1013
1014         /* NOTE: moved before overlays callback circling because
1015          * they may modify entry and other stuff in rs */
1016         if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
1017                 int     k = 0;
1018                 size_t  size;
1019
1020                 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1021                         for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1022                 }
1023
1024                 size = i * sizeof(char *) + k;
1025                 if ( size > 0 ) {
1026                         char    *a_flags, **tmp;
1027                 
1028                         /*
1029                          * Reuse previous memory - we likely need less space
1030                          * for operational attributes
1031                          */
1032                         tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
1033                                 op->o_tmpmemctx );
1034                         if ( tmp == NULL ) {
1035                                 Debug( LDAP_DEBUG_ANY,
1036                                         "send_search_entry: conn %lu "
1037                                         "not enough memory "
1038                                         "for matched values filtering\n",
1039                                         op->o_connid, 0, 0 );
1040                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1041                                 send_ldap_error( op, rs, LDAP_OTHER,
1042                                         "not enough memory for matched values filtering" );
1043                                 goto error_return;
1044                         }
1045                         e_flags = tmp;
1046                         a_flags = (char *)(e_flags + i);
1047                         memset( a_flags, 0, k );
1048                         for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1049                                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1050                                 e_flags[i] = a_flags;
1051                                 a_flags += j;
1052                         }
1053                         rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ; 
1054                     
1055                         if ( rc == -1 ) {
1056                                 Debug( LDAP_DEBUG_ANY,
1057                                         "send_search_entry: conn %lu "
1058                                         "matched values filtering failed\n", 
1059                                         op->o_connid ? op->o_connid : 0, 0, 0);
1060                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1061                                 send_ldap_error( op, rs, LDAP_OTHER,
1062                                         "matched values filtering error" );
1063                                 rc = rs->sr_err;
1064                                 goto error_return;
1065                         }
1066                 }
1067         }
1068
1069         for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1070                 AttributeDescription *desc = a->a_desc;
1071
1072                 if ( rs->sr_attrs == NULL ) {
1073                         /* all user attrs request, skip operational attributes */
1074                         if( is_at_operational( desc->ad_type ) ) {
1075                                 continue;
1076                         }
1077
1078                 } else {
1079                         /* specific attrs requested */
1080                         if( is_at_operational( desc->ad_type ) ) {
1081                                 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) && 
1082                                         !ad_inlist( desc, rs->sr_attrs ) )
1083                                 {
1084                                         continue;
1085                                 }
1086                         } else {
1087                                 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1088                                         continue;
1089                                 }
1090                         }
1091                 }
1092
1093                 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1094                         ACL_READ, &acl_state ) )
1095                 {
1096                         Debug( LDAP_DEBUG_ACL,
1097                                 "send_search_entry: conn %lu "
1098                                 "access to attribute %s not allowed\n",
1099                                 op->o_connid, desc->ad_cname.bv_val, 0 );
1100
1101                         continue;
1102                 }
1103
1104                 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1105                 if ( rc == -1 ) {
1106                         Debug( LDAP_DEBUG_ANY,
1107                                 "send_search_entry: conn %lu  "
1108                                 "ber_printf failed\n", op->o_connid, 0, 0 );
1109
1110                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1111                         send_ldap_error( op, rs, LDAP_OTHER,
1112                                 "encoding description error" );
1113                         rc = rs->sr_err;
1114                         goto error_return;
1115                 }
1116
1117                 if ( ! attrsonly ) {
1118                         for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1119                                 if ( ! access_allowed( op, rs->sr_entry,
1120                                         desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1121                                 {
1122                                         Debug( LDAP_DEBUG_ACL,
1123                                                 "send_search_entry: conn %lu "
1124                                                 "access to %s, value %d not allowed\n",
1125                                                 op->o_connid, desc->ad_cname.bv_val, i );
1126
1127                                         continue;
1128                                 }
1129
1130                                 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1131                                         continue;
1132                                 }
1133
1134                                 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1135                                         Debug( LDAP_DEBUG_ANY,
1136                                                 "send_search_entry: conn %lu  ber_printf failed\n", 
1137                                                 op->o_connid, 0, 0 );
1138
1139                                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1140                                         send_ldap_error( op, rs, LDAP_OTHER,
1141                                                 "encoding values error" );
1142                                         rc = rs->sr_err;
1143                                         goto error_return;
1144                                 }
1145                         }
1146                 }
1147
1148                 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1149                         Debug( LDAP_DEBUG_ANY,
1150                                 "send_search_entry: conn %lu  ber_printf failed\n",
1151                                 op->o_connid, 0, 0 );
1152
1153                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1154                         send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1155                         rc = rs->sr_err;
1156                         goto error_return;
1157                 }
1158         }
1159
1160         /* free e_flags */
1161         if ( e_flags ) {
1162                 slap_sl_free( e_flags, op->o_tmpmemctx );
1163                 e_flags = NULL;
1164         }
1165
1166         rc = ber_printf( ber, /*{{*/ "}N}" );
1167
1168         if( rc != -1 ) {
1169                 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1170         }
1171
1172         if( rc != -1 ) {
1173 #ifdef LDAP_CONNECTIONLESS
1174                 if( op->o_conn && op->o_conn->c_is_udp ) {
1175                         if ( op->o_protocol != LDAP_VERSION2 ) {
1176                                 rc = ber_printf( ber, /*{*/ "N}" );
1177                         }
1178                 } else
1179 #endif
1180                 if ( op->o_res_ber == NULL ) {
1181                         rc = ber_printf( ber, /*{*/ "N}" );
1182                 }
1183         }
1184
1185         if ( rc == -1 ) {
1186                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1187
1188                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1189                 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1190                 rc = rs->sr_err;
1191                 goto error_return;
1192         }
1193
1194         Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1195             op->o_log_prefix, rs->sr_entry->e_nname.bv_val, 0, 0, 0 );
1196
1197         if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1198                 be_entry_release_rw( op, rs->sr_entry, 0 );
1199                 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1200                 rs->sr_entry = NULL;
1201         }
1202
1203         if ( op->o_res_ber == NULL ) {
1204                 bytes = send_ldap_ber( op->o_conn, ber );
1205                 ber_free_buf( ber );
1206
1207                 if ( bytes < 0 ) {
1208                         Debug( LDAP_DEBUG_ANY,
1209                                 "send_search_entry: conn %lu  ber write failed.\n", 
1210                                 op->o_connid, 0, 0 );
1211
1212                         rc = LDAP_UNAVAILABLE;
1213                         goto error_return;
1214                 }
1215                 rs->sr_nentries++;
1216
1217                 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1218                 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
1219                 ldap_pvt_mp_add_ulong( op->o_counters->sc_entries, 1 );
1220                 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
1221                 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1222         }
1223
1224         Debug( LDAP_DEBUG_TRACE,
1225                 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1226
1227         rc = LDAP_SUCCESS;
1228
1229 error_return:;
1230         if ( op->o_callback ) {
1231                 (void)slap_cleanup_play( op, rs );
1232         }
1233
1234         if ( e_flags ) {
1235                 slap_sl_free( e_flags, op->o_tmpmemctx );
1236         }
1237
1238         if ( rs->sr_operational_attrs ) {
1239                 attrs_free( rs->sr_operational_attrs );
1240                 rs->sr_operational_attrs = NULL;
1241         }
1242         rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1243
1244         /* FIXME: I think rs->sr_type should be explicitly set to
1245          * REP_SEARCH here. That's what it was when we entered this
1246          * function. send_ldap_error may have changed it, but we
1247          * should set it back so that the cleanup functions know
1248          * what they're doing.
1249          */
1250         if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH 
1251                 && rs->sr_entry 
1252                 && ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) ) 
1253         {
1254                 entry_free( rs->sr_entry );
1255                 rs->sr_entry = NULL;
1256                 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1257         }
1258
1259         return( rc );
1260 }
1261
1262 int
1263 slap_send_search_reference( Operation *op, SlapReply *rs )
1264 {
1265         BerElementBuffer berbuf;
1266         BerElement      *ber = (BerElement *) &berbuf;
1267         int rc = 0;
1268         int bytes;
1269         char *edn = rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)";
1270
1271         AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1272         AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1273
1274         rs->sr_type = REP_SEARCHREF;
1275         if ( op->o_callback ) {
1276                 rc = slap_response_play( op, rs );
1277                 if ( rc != SLAP_CB_CONTINUE ) {
1278                         goto rel;
1279                 }
1280         }
1281
1282         Debug( LDAP_DEBUG_TRACE,
1283                 "=> send_search_reference: dn=\"%s\"\n",
1284                 edn, 0, 0 );
1285
1286         if (  rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1287                 ad_entry, NULL, ACL_READ, NULL ) )
1288         {
1289                 Debug( LDAP_DEBUG_ACL,
1290                         "send_search_reference: access to entry not allowed\n",
1291                     0, 0, 0 );
1292                 rc = 1;
1293                 goto rel;
1294         }
1295
1296         if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1297                 ad_ref, NULL, ACL_READ, NULL ) )
1298         {
1299                 Debug( LDAP_DEBUG_ACL,
1300                         "send_search_reference: access "
1301                         "to reference not allowed\n",
1302                     0, 0, 0 );
1303                 rc = 1;
1304                 goto rel;
1305         }
1306
1307         if( op->o_domain_scope ) {
1308                 Debug( LDAP_DEBUG_ANY,
1309                         "send_search_reference: domainScope control in (%s)\n", 
1310                         edn, 0, 0 );
1311                 rc = 0;
1312                 goto rel;
1313         }
1314
1315         if( rs->sr_ref == NULL ) {
1316                 Debug( LDAP_DEBUG_ANY,
1317                         "send_search_reference: null ref in (%s)\n", 
1318                         edn, 0, 0 );
1319                 rc = 1;
1320                 goto rel;
1321         }
1322
1323         if( op->o_protocol < LDAP_VERSION3 ) {
1324                 rc = 0;
1325                 /* save the references for the result */
1326                 if( rs->sr_ref[0].bv_val != NULL ) {
1327                         if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1328                                 rc = LDAP_OTHER;
1329                 }
1330                 goto rel;
1331         }
1332
1333 #ifdef LDAP_CONNECTIONLESS
1334         if( op->o_conn && op->o_conn->c_is_udp ) {
1335                 ber = op->o_res_ber;
1336         } else
1337 #endif
1338         {
1339                 ber_init_w_nullc( ber, LBER_USE_DER );
1340                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1341         }
1342
1343         rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1344                 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1345
1346         if( rc != -1 ) {
1347                 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1348         }
1349
1350         if( rc != -1 ) {
1351                 rc = ber_printf( ber, /*"{"*/ "N}" );
1352         }
1353
1354         if ( rc == -1 ) {
1355                 Debug( LDAP_DEBUG_ANY,
1356                         "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1357
1358 #ifdef LDAP_CONNECTIONLESS
1359                 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1360 #endif
1361                 ber_free_buf( ber );
1362                 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1363                 goto rel;
1364         }
1365
1366         rc = 0;
1367         if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1368                 assert( rs->sr_entry != NULL );
1369                 be_entry_release_rw( op, rs->sr_entry, 0 );
1370                 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1371                 rs->sr_entry = NULL;
1372         }
1373
1374 #ifdef LDAP_CONNECTIONLESS
1375         if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1376 #endif
1377         bytes = send_ldap_ber( op->o_conn, ber );
1378         ber_free_buf( ber );
1379
1380         if ( bytes < 0 ) {
1381                 rc = LDAP_UNAVAILABLE;
1382         } else {
1383                 ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
1384                 ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
1385                 ldap_pvt_mp_add_ulong( op->o_counters->sc_refs, 1 );
1386                 ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
1387                 ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
1388         }
1389 #ifdef LDAP_CONNECTIONLESS
1390         }
1391 #endif
1392         if ( rs->sr_ref != NULL ) {
1393                 int     r;
1394
1395                 for ( r = 0; !BER_BVISNULL( &rs->sr_ref[ r ] ); r++ ) {
1396                         Statslog( LDAP_DEBUG_STATS2, "%s REF #%d \"%s\"\n",
1397                                 op->o_log_prefix, r, rs->sr_ref[0].bv_val,
1398                                 0, 0 );
1399                 }
1400
1401         } else {
1402                 Statslog( LDAP_DEBUG_STATS2, "%s REF \"(null)\"\n",
1403                         op->o_log_prefix, 0, 0, 0, 0 );
1404         }
1405
1406         Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1407
1408 rel:
1409         if ( op->o_callback ) {
1410                 (void)slap_cleanup_play( op, rs );
1411         }
1412
1413         return rc;
1414 }
1415
1416 int
1417 str2result(
1418     char        *s,
1419     int         *code,
1420     char        **matched,
1421     char        **info )
1422 {
1423         int     rc;
1424         char    *c;
1425
1426         *code = LDAP_SUCCESS;
1427         *matched = NULL;
1428         *info = NULL;
1429
1430         if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1431                 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1432                     s, 0, 0 );
1433
1434                 return( -1 );
1435         }
1436
1437         rc = 0;
1438         while ( (s = strchr( s, '\n' )) != NULL ) {
1439                 *s++ = '\0';
1440                 if ( *s == '\0' ) {
1441                         break;
1442                 }
1443                 if ( (c = strchr( s, ':' )) != NULL ) {
1444                         c++;
1445                 }
1446
1447                 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1448                         char    *next = NULL;
1449                         long    retcode;
1450
1451                         if ( c == NULL ) {
1452                                 Debug( LDAP_DEBUG_ANY, "str2result (%s) missing value\n",
1453                                     s, 0, 0 );
1454                                 rc = -1;
1455                                 continue;
1456                         }
1457
1458                         while ( isspace( (unsigned char) c[ 0 ] ) ) c++;
1459                         if ( c[ 0 ] == '\0' ) {
1460                                 Debug( LDAP_DEBUG_ANY, "str2result (%s) missing or empty value\n",
1461                                     s, 0, 0 );
1462                                 rc = -1;
1463                                 continue;
1464                         }
1465
1466                         retcode = strtol( c, &next, 10 );
1467                         if ( next == NULL || next == c ) {
1468                                 Debug( LDAP_DEBUG_ANY, "str2result (%s) unable to parse value\n",
1469                                     s, 0, 0 );
1470                                 rc = -1;
1471                                 continue;
1472                         }
1473
1474                         while ( isspace( (unsigned char) next[ 0 ] ) ) next++;
1475                         if ( next[ 0 ] != '\0' ) {
1476                                 Debug( LDAP_DEBUG_ANY, "str2result (%s) extra cruft after value\n",
1477                                     s, 0, 0 );
1478                                 rc = -1;
1479                                 continue;
1480                         }
1481
1482                         /* FIXME: what if it's larger that max int? */
1483                         *code = (int)retcode;
1484
1485                 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1486                         if ( c != NULL ) {
1487                                 *matched = c;
1488                         }
1489                 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1490                         if ( c != NULL ) {
1491                                 *info = c;
1492                         }
1493                 } else {
1494                         Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1495                             s, 0, 0 );
1496
1497                         rc = -1;
1498                 }
1499         }
1500
1501         return( rc );
1502 }
1503
1504 int slap_read_controls(
1505         Operation *op,
1506         SlapReply *rs,
1507         Entry *e,
1508         const struct berval *oid,
1509         LDAPControl **ctrl )
1510 {
1511         int rc;
1512         struct berval bv;
1513         BerElementBuffer berbuf;
1514         BerElement *ber = (BerElement *) &berbuf;
1515         LDAPControl c;
1516         Operation myop;
1517
1518         Debug( LDAP_DEBUG_ANY, "%s slap_read_controls: (%s) %s\n",
1519                 op->o_log_prefix, oid->bv_val, e->e_dn );
1520
1521         rs->sr_entry = e;
1522         rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1523                 op->o_preread_attrs : op->o_postread_attrs; 
1524
1525         bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1526         bv.bv_val = op->o_tmpalloc( bv.bv_len, op->o_tmpmemctx );
1527
1528         ber_init2( ber, &bv, LBER_USE_DER );
1529         ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1530
1531         /* create new operation */
1532         myop = *op;
1533         /* FIXME: o_bd needed for ACL */
1534         myop.o_bd = op->o_bd;
1535         myop.o_res_ber = ber;
1536         myop.o_callback = NULL;
1537         myop.ors_slimit = 1;
1538
1539         rc = slap_send_search_entry( &myop, rs );
1540         if( rc ) return rc;
1541
1542         rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1543
1544         if( rc == -1 ) return LDAP_OTHER;
1545
1546         c.ldctl_oid = oid->bv_val;
1547         c.ldctl_iscritical = 0;
1548
1549         if ( *ctrl == NULL ) {
1550                 /* first try */
1551                 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1552         } else {
1553                 /* retry: free previous try */
1554                 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1555         }
1556
1557         **ctrl = c;
1558         return LDAP_SUCCESS;
1559 }
1560
1561 /* Map API errors to protocol errors... */
1562 int
1563 slap_map_api2result( SlapReply *rs )
1564 {
1565         switch(rs->sr_err) {
1566         case LDAP_SERVER_DOWN:
1567                 return LDAP_UNAVAILABLE;
1568         case LDAP_LOCAL_ERROR:
1569                 return LDAP_OTHER;
1570         case LDAP_ENCODING_ERROR:
1571         case LDAP_DECODING_ERROR:
1572                 return LDAP_PROTOCOL_ERROR;
1573         case LDAP_TIMEOUT:
1574                 return LDAP_UNAVAILABLE;
1575         case LDAP_AUTH_UNKNOWN:
1576                 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1577         case LDAP_FILTER_ERROR:
1578                 rs->sr_text = "Filter error";
1579                 return LDAP_OTHER;
1580         case LDAP_USER_CANCELLED:
1581                 rs->sr_text = "User cancelled";
1582                 return LDAP_OTHER;
1583         case LDAP_PARAM_ERROR:
1584                 return LDAP_PROTOCOL_ERROR;
1585         case LDAP_NO_MEMORY:
1586                 return LDAP_OTHER;
1587         case LDAP_CONNECT_ERROR:
1588                 return LDAP_UNAVAILABLE;
1589         case LDAP_NOT_SUPPORTED:
1590                 return LDAP_UNWILLING_TO_PERFORM;
1591         case LDAP_CONTROL_NOT_FOUND:
1592                 return LDAP_PROTOCOL_ERROR;
1593         case LDAP_NO_RESULTS_RETURNED:
1594                 return LDAP_NO_SUCH_OBJECT;
1595         case LDAP_MORE_RESULTS_TO_RETURN:
1596                 rs->sr_text = "More results to return";
1597                 return LDAP_OTHER;
1598         case LDAP_CLIENT_LOOP:
1599         case LDAP_REFERRAL_LIMIT_EXCEEDED:
1600                 return LDAP_LOOP_DETECT;
1601         default:
1602                 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1603                 return rs->sr_err;
1604         }
1605 }
1606
1607
1608 slap_mask_t
1609 slap_attr_flags( AttributeName *an )
1610 {
1611         slap_mask_t     flags = SLAP_ATTRS_UNDEFINED;
1612
1613         if ( an == NULL ) {
1614                 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1615
1616         } else {
1617                 flags |= an_find( an, &AllOper )
1618                         ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1619                 flags |= an_find( an, &AllUser )
1620                         ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;
1621         }
1622
1623         return flags;
1624 }
1625