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