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