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