Home | History | Annotate | Line # | Download | only in back-monitor
      1  1.3  christos /*	$NetBSD: search.c,v 1.4 2025/09/05 21:16:29 christos Exp $	*/
      2  1.2  christos 
      3  1.1     lukem /* search.c - monitor backend search function */
      4  1.2  christos /* $OpenLDAP$ */
      5  1.1     lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      6  1.1     lukem  *
      7  1.4  christos  * Copyright 2001-2024 The OpenLDAP Foundation.
      8  1.1     lukem  * Portions Copyright 2001-2003 Pierangelo Masarati.
      9  1.1     lukem  * All rights reserved.
     10  1.1     lukem  *
     11  1.1     lukem  * Redistribution and use in source and binary forms, with or without
     12  1.1     lukem  * modification, are permitted only as authorized by the OpenLDAP
     13  1.1     lukem  * Public License.
     14  1.1     lukem  *
     15  1.1     lukem  * A copy of this license is available in file LICENSE in the
     16  1.1     lukem  * top-level directory of the distribution or, alternatively, at
     17  1.1     lukem  * <http://www.OpenLDAP.org/license.html>.
     18  1.1     lukem  */
     19  1.1     lukem /* ACKNOWLEDGEMENTS:
     20  1.1     lukem  * This work was initially developed by Pierangelo Masarati for inclusion
     21  1.1     lukem  * in OpenLDAP Software.
     22  1.1     lukem  */
     23  1.1     lukem 
     24  1.2  christos #include <sys/cdefs.h>
     25  1.3  christos __RCSID("$NetBSD: search.c,v 1.4 2025/09/05 21:16:29 christos Exp $");
     26  1.2  christos 
     27  1.1     lukem #include "portable.h"
     28  1.1     lukem 
     29  1.1     lukem #include <stdio.h>
     30  1.1     lukem 
     31  1.1     lukem #include <ac/string.h>
     32  1.1     lukem #include <ac/socket.h>
     33  1.1     lukem 
     34  1.1     lukem #include "slap.h"
     35  1.1     lukem #include "back-monitor.h"
     36  1.1     lukem #include "proto-back-monitor.h"
     37  1.1     lukem 
     38  1.2  christos static void
     39  1.2  christos monitor_find_children(
     40  1.2  christos 	Operation *op,
     41  1.2  christos 	SlapReply *rs,
     42  1.2  christos 	Entry *e_parent,
     43  1.2  christos 	Entry **nonv,
     44  1.2  christos 	Entry **vol
     45  1.2  christos )
     46  1.2  christos {
     47  1.2  christos 	monitor_entry_t *mp;
     48  1.2  christos 
     49  1.2  christos 	mp = ( monitor_entry_t * )e_parent->e_private;
     50  1.2  christos 	*nonv = mp->mp_children;
     51  1.2  christos 
     52  1.2  christos 	if ( MONITOR_HAS_VOLATILE_CH( mp ) ) {
     53  1.2  christos 		monitor_entry_create( op, rs, NULL, e_parent, vol );
     54  1.2  christos 	}
     55  1.2  christos }
     56  1.2  christos 
     57  1.1     lukem static int
     58  1.1     lukem monitor_send_children(
     59  1.1     lukem 	Operation	*op,
     60  1.1     lukem 	SlapReply	*rs,
     61  1.2  christos 	Entry		*e_nonvolatile,
     62  1.2  christos 	Entry		*e_ch,
     63  1.1     lukem 	int		sub )
     64  1.1     lukem {
     65  1.1     lukem 	monitor_info_t	*mi = ( monitor_info_t * )op->o_bd->be_private;
     66  1.1     lukem 	Entry 			*e,
     67  1.2  christos 				*e_tmp;
     68  1.1     lukem 	monitor_entry_t *mp;
     69  1.1     lukem 	int			rc,
     70  1.1     lukem 				nonvolatile = 0;
     71  1.1     lukem 
     72  1.2  christos 	e = e_nonvolatile;
     73  1.1     lukem 
     74  1.1     lukem 	/* no volatile entries? */
     75  1.1     lukem 	if ( e_ch == NULL ) {
     76  1.1     lukem 		/* no persistent entries? return */
     77  1.1     lukem 		if ( e == NULL ) {
     78  1.1     lukem 			return LDAP_SUCCESS;
     79  1.1     lukem 		}
     80  1.2  christos 
     81  1.1     lukem 	/* volatile entries */
     82  1.1     lukem 	} else {
     83  1.1     lukem 		/* if no persistent, return only volatile */
     84  1.1     lukem 		if ( e == NULL ) {
     85  1.1     lukem 			e = e_ch;
     86  1.1     lukem 
     87  1.1     lukem 		/* else append persistent to volatile */
     88  1.1     lukem 		} else {
     89  1.1     lukem 			e_tmp = e_ch;
     90  1.1     lukem 			do {
     91  1.1     lukem 				mp = ( monitor_entry_t * )e_tmp->e_private;
     92  1.1     lukem 				e_tmp = mp->mp_next;
     93  1.1     lukem 
     94  1.1     lukem 				if ( e_tmp == NULL ) {
     95  1.1     lukem 					mp->mp_next = e;
     96  1.1     lukem 					break;
     97  1.1     lukem 				}
     98  1.1     lukem 			} while ( e_tmp );
     99  1.1     lukem 			e = e_ch;
    100  1.1     lukem 		}
    101  1.1     lukem 	}
    102  1.1     lukem 
    103  1.1     lukem 	/* return entries */
    104  1.2  christos 	for ( ; e != NULL; e = e_tmp ) {
    105  1.4  christos 		Entry *sub_nv = NULL, *sub_ch = NULL, *locked = e;
    106  1.2  christos 
    107  1.2  christos 		monitor_cache_lock( e );
    108  1.1     lukem 		monitor_entry_update( op, rs, e );
    109  1.1     lukem 
    110  1.2  christos 		if ( e == e_nonvolatile )
    111  1.2  christos 			nonvolatile = 1;
    112  1.2  christos 
    113  1.2  christos 		mp = ( monitor_entry_t * )e->e_private;
    114  1.2  christos 		e_tmp = mp->mp_next;
    115  1.2  christos 
    116  1.1     lukem 		if ( op->o_abandon ) {
    117  1.2  christos 			rc = SLAPD_ABANDON;
    118  1.2  christos 			goto freeout;
    119  1.2  christos 		}
    120  1.1     lukem 
    121  1.2  christos 		if ( sub )
    122  1.2  christos 			monitor_find_children( op, rs, e, &sub_nv, &sub_ch );
    123  1.1     lukem 
    124  1.1     lukem 		rc = test_filter( op, e, op->oq_search.rs_filter );
    125  1.1     lukem 		if ( rc == LDAP_COMPARE_TRUE ) {
    126  1.1     lukem 			rs->sr_entry = e;
    127  1.1     lukem 			rc = send_search_entry( op, rs );
    128  1.2  christos 			if ( rc ) {
    129  1.2  christos 				for ( e = sub_ch; e != NULL; e = sub_nv ) {
    130  1.2  christos 					mp = ( monitor_entry_t * )e->e_private;
    131  1.2  christos 					sub_nv = mp->mp_next;
    132  1.2  christos 					monitor_cache_lock( e );
    133  1.2  christos 					monitor_cache_release( mi, e );
    134  1.2  christos 				}
    135  1.2  christos 				goto freeout;
    136  1.2  christos 			}
    137  1.4  christos 		}
    138  1.4  christos 		if ( sub_nv == NULL ) {
    139  1.4  christos 			monitor_cache_release( mi, locked );
    140  1.4  christos 			locked = NULL;
    141  1.1     lukem 		}
    142  1.1     lukem 
    143  1.1     lukem 		if ( sub ) {
    144  1.2  christos 			rc = monitor_send_children( op, rs, sub_nv, sub_ch, sub );
    145  1.1     lukem 			if ( rc ) {
    146  1.2  christos freeout:
    147  1.4  christos 				if ( locked ) {
    148  1.4  christos 					monitor_cache_release( mi, locked );
    149  1.4  christos 				}
    150  1.1     lukem 				if ( nonvolatile == 0 ) {
    151  1.1     lukem 					for ( ; e_tmp != NULL; ) {
    152  1.1     lukem 						mp = ( monitor_entry_t * )e_tmp->e_private;
    153  1.1     lukem 						e = e_tmp;
    154  1.1     lukem 						e_tmp = mp->mp_next;
    155  1.2  christos 						monitor_cache_lock( e );
    156  1.1     lukem 						monitor_cache_release( mi, e );
    157  1.1     lukem 
    158  1.1     lukem 						if ( e_tmp == e_nonvolatile ) {
    159  1.1     lukem 							break;
    160  1.1     lukem 						}
    161  1.1     lukem 					}
    162  1.1     lukem 				}
    163  1.1     lukem 
    164  1.1     lukem 				return( rc );
    165  1.1     lukem 			}
    166  1.1     lukem 		}
    167  1.4  christos 		if ( locked ) {
    168  1.4  christos 			monitor_cache_release( mi, locked );
    169  1.4  christos 		}
    170  1.1     lukem 	}
    171  1.1     lukem 
    172  1.1     lukem 	return LDAP_SUCCESS;
    173  1.1     lukem }
    174  1.1     lukem 
    175  1.1     lukem int
    176  1.1     lukem monitor_back_search( Operation *op, SlapReply *rs )
    177  1.1     lukem {
    178  1.1     lukem 	monitor_info_t	*mi = ( monitor_info_t * )op->o_bd->be_private;
    179  1.1     lukem 	int		rc = LDAP_SUCCESS;
    180  1.1     lukem 	Entry		*e = NULL, *matched = NULL;
    181  1.2  christos 	Entry		*e_nv = NULL, *e_ch = NULL;
    182  1.1     lukem 	slap_mask_t	mask;
    183  1.1     lukem 
    184  1.3  christos 	Debug( LDAP_DEBUG_TRACE, "=> monitor_back_search\n" );
    185  1.1     lukem 
    186  1.1     lukem 
    187  1.1     lukem 	/* get entry with reader lock */
    188  1.1     lukem 	monitor_cache_dn2entry( op, rs, &op->o_req_ndn, &e, &matched );
    189  1.1     lukem 	if ( e == NULL ) {
    190  1.1     lukem 		rs->sr_err = LDAP_NO_SUCH_OBJECT;
    191  1.1     lukem 		if ( matched ) {
    192  1.1     lukem 			if ( !access_allowed_mask( op, matched,
    193  1.1     lukem 					slap_schema.si_ad_entry,
    194  1.1     lukem 					NULL, ACL_DISCLOSE, NULL, NULL ) )
    195  1.1     lukem 			{
    196  1.1     lukem 				/* do nothing */ ;
    197  1.1     lukem 			} else {
    198  1.1     lukem 				rs->sr_matched = matched->e_dn;
    199  1.1     lukem 			}
    200  1.1     lukem 		}
    201  1.1     lukem 
    202  1.1     lukem 		send_ldap_result( op, rs );
    203  1.1     lukem 		if ( matched ) {
    204  1.1     lukem 			monitor_cache_release( mi, matched );
    205  1.1     lukem 			rs->sr_matched = NULL;
    206  1.1     lukem 		}
    207  1.1     lukem 
    208  1.1     lukem 		return rs->sr_err;
    209  1.1     lukem 	}
    210  1.1     lukem 
    211  1.1     lukem 	/* NOTE: __NEW__ "search" access is required
    212  1.1     lukem 	 * on searchBase object */
    213  1.1     lukem 	if ( !access_allowed_mask( op, e, slap_schema.si_ad_entry,
    214  1.1     lukem 				NULL, ACL_SEARCH, NULL, &mask ) )
    215  1.1     lukem 	{
    216  1.1     lukem 		monitor_cache_release( mi, e );
    217  1.1     lukem 
    218  1.1     lukem 		if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
    219  1.1     lukem 			rs->sr_err = LDAP_NO_SUCH_OBJECT;
    220  1.1     lukem 		} else {
    221  1.1     lukem 			rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
    222  1.1     lukem 		}
    223  1.1     lukem 
    224  1.1     lukem 		send_ldap_result( op, rs );
    225  1.1     lukem 
    226  1.1     lukem 		return rs->sr_err;
    227  1.1     lukem 	}
    228  1.1     lukem 
    229  1.1     lukem 	rs->sr_attrs = op->oq_search.rs_attrs;
    230  1.1     lukem 	switch ( op->oq_search.rs_scope ) {
    231  1.1     lukem 	case LDAP_SCOPE_BASE:
    232  1.1     lukem 		monitor_entry_update( op, rs, e );
    233  1.1     lukem 		rc = test_filter( op, e, op->oq_search.rs_filter );
    234  1.1     lukem  		if ( rc == LDAP_COMPARE_TRUE ) {
    235  1.1     lukem 			rs->sr_entry = e;
    236  1.2  christos 			rs->sr_flags = REP_ENTRY_MUSTRELEASE;
    237  1.1     lukem 			send_search_entry( op, rs );
    238  1.1     lukem 			rs->sr_entry = NULL;
    239  1.2  christos 		} else {
    240  1.2  christos 			monitor_cache_release( mi, e );
    241  1.1     lukem 		}
    242  1.1     lukem 		rc = LDAP_SUCCESS;
    243  1.1     lukem 		break;
    244  1.1     lukem 
    245  1.1     lukem 	case LDAP_SCOPE_ONELEVEL:
    246  1.1     lukem 	case LDAP_SCOPE_SUBORDINATE:
    247  1.2  christos 		monitor_find_children( op, rs, e, &e_nv, &e_ch );
    248  1.2  christos 		rc = monitor_send_children( op, rs, e_nv, e_ch,
    249  1.1     lukem 			op->oq_search.rs_scope == LDAP_SCOPE_SUBORDINATE );
    250  1.4  christos 		monitor_cache_release( mi, e );
    251  1.1     lukem 		break;
    252  1.1     lukem 
    253  1.1     lukem 	case LDAP_SCOPE_SUBTREE:
    254  1.1     lukem 		monitor_entry_update( op, rs, e );
    255  1.2  christos 		monitor_find_children( op, rs, e, &e_nv, &e_ch );
    256  1.1     lukem 		rc = test_filter( op, e, op->oq_search.rs_filter );
    257  1.1     lukem 		if ( rc == LDAP_COMPARE_TRUE ) {
    258  1.1     lukem 			rs->sr_entry = e;
    259  1.1     lukem 			send_search_entry( op, rs );
    260  1.1     lukem 			rs->sr_entry = NULL;
    261  1.1     lukem 		}
    262  1.1     lukem 
    263  1.2  christos 		rc = monitor_send_children( op, rs, e_nv, e_ch, 1 );
    264  1.4  christos 		monitor_cache_release( mi, e );
    265  1.1     lukem 		break;
    266  1.1     lukem 
    267  1.1     lukem 	default:
    268  1.1     lukem 		rc = LDAP_UNWILLING_TO_PERFORM;
    269  1.1     lukem 		monitor_cache_release( mi, e );
    270  1.1     lukem 	}
    271  1.1     lukem 
    272  1.1     lukem 	rs->sr_attrs = NULL;
    273  1.1     lukem 	rs->sr_err = rc;
    274  1.1     lukem 	if ( rs->sr_err != SLAPD_ABANDON ) {
    275  1.1     lukem 		send_ldap_result( op, rs );
    276  1.1     lukem 	}
    277  1.1     lukem 
    278  1.1     lukem 	return rs->sr_err;
    279  1.1     lukem }
    280  1.1     lukem 
    281