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