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