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