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