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