| 1 |
/* database.c - deals with database subsystem */ |
| 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 |
#include <ac/string.h> |
| 26 |
#include <ac/unistd.h> |
| 27 |
|
| 28 |
#include "slap.h" |
| 29 |
#include "back-monitor.h" |
| 30 |
|
| 31 |
#if defined(LDAP_SLAPI) |
| 32 |
#include "slapi.h" |
| 33 |
static int monitor_back_add_plugin( monitor_info_t *mi, Backend *be, Entry *e ); |
| 34 |
#endif /* defined(LDAP_SLAPI) */ |
| 35 |
|
| 36 |
static int |
| 37 |
monitor_subsys_database_modify( |
| 38 |
Operation *op, |
| 39 |
SlapReply *rs, |
| 40 |
Entry *e ); |
| 41 |
|
| 42 |
static struct restricted_ops_t { |
| 43 |
struct berval op; |
| 44 |
unsigned int tag; |
| 45 |
} restricted_ops[] = { |
| 46 |
{ BER_BVC( "add" ), SLAP_RESTRICT_OP_ADD }, |
| 47 |
{ BER_BVC( "bind" ), SLAP_RESTRICT_OP_BIND }, |
| 48 |
{ BER_BVC( "compare" ), SLAP_RESTRICT_OP_COMPARE }, |
| 49 |
{ BER_BVC( "delete" ), SLAP_RESTRICT_OP_DELETE }, |
| 50 |
{ BER_BVC( "extended" ), SLAP_RESTRICT_OP_EXTENDED }, |
| 51 |
{ BER_BVC( "modify" ), SLAP_RESTRICT_OP_MODIFY }, |
| 52 |
{ BER_BVC( "rename" ), SLAP_RESTRICT_OP_RENAME }, |
| 53 |
{ BER_BVC( "search" ), SLAP_RESTRICT_OP_SEARCH }, |
| 54 |
{ BER_BVNULL, 0 } |
| 55 |
}, restricted_exops[] = { |
| 56 |
{ BER_BVC( LDAP_EXOP_START_TLS ), SLAP_RESTRICT_EXOP_START_TLS }, |
| 57 |
{ BER_BVC( LDAP_EXOP_MODIFY_PASSWD ), SLAP_RESTRICT_EXOP_MODIFY_PASSWD }, |
| 58 |
{ BER_BVC( LDAP_EXOP_WHO_AM_I ), SLAP_RESTRICT_EXOP_WHOAMI }, |
| 59 |
{ BER_BVC( LDAP_EXOP_CANCEL ), SLAP_RESTRICT_EXOP_CANCEL }, |
| 60 |
{ BER_BVNULL, 0 } |
| 61 |
}; |
| 62 |
|
| 63 |
static int |
| 64 |
init_readOnly( monitor_info_t *mi, Entry *e, slap_mask_t restrictops ) |
| 65 |
{ |
| 66 |
struct berval *tf = ( ( restrictops & SLAP_RESTRICT_OP_MASK ) == SLAP_RESTRICT_OP_WRITES ) ? |
| 67 |
(struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv; |
| 68 |
|
| 69 |
return attr_merge_one( e, mi->mi_ad_readOnly, tf, NULL ); |
| 70 |
} |
| 71 |
|
| 72 |
static int |
| 73 |
init_restrictedOperation( monitor_info_t *mi, Entry *e, slap_mask_t restrictops ) |
| 74 |
{ |
| 75 |
int i, rc; |
| 76 |
|
| 77 |
for ( i = 0; restricted_ops[ i ].op.bv_val; i++ ) { |
| 78 |
if ( restrictops & restricted_ops[ i ].tag ) { |
| 79 |
rc = attr_merge_one( e, mi->mi_ad_restrictedOperation, |
| 80 |
&restricted_ops[ i ].op, |
| 81 |
&restricted_ops[ i ].op ); |
| 82 |
if ( rc ) { |
| 83 |
return rc; |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
for ( i = 0; restricted_exops[ i ].op.bv_val; i++ ) { |
| 89 |
if ( restrictops & restricted_exops[ i ].tag ) { |
| 90 |
rc = attr_merge_one( e, mi->mi_ad_restrictedOperation, |
| 91 |
&restricted_exops[ i ].op, |
| 92 |
&restricted_exops[ i ].op ); |
| 93 |
if ( rc ) { |
| 94 |
return rc; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
return LDAP_SUCCESS; |
| 100 |
} |
| 101 |
|
| 102 |
static int |
| 103 |
monitor_subsys_overlay_init_one( |
| 104 |
monitor_info_t *mi, |
| 105 |
BackendDB *be, |
| 106 |
monitor_subsys_t *ms, |
| 107 |
monitor_subsys_t *ms_overlay, |
| 108 |
slap_overinst *on, |
| 109 |
Entry *e_database, |
| 110 |
Entry **ep_overlay ) |
| 111 |
{ |
| 112 |
char buf[ BACKMONITOR_BUFSIZE ]; |
| 113 |
int j, o; |
| 114 |
Entry *e_overlay; |
| 115 |
slap_overinst *on2; |
| 116 |
slap_overinfo *oi = NULL; |
| 117 |
BackendInfo *bi; |
| 118 |
monitor_entry_t *mp_overlay; |
| 119 |
struct berval bv; |
| 120 |
|
| 121 |
assert( overlay_is_over( be ) ); |
| 122 |
|
| 123 |
oi = (slap_overinfo *)be->bd_info->bi_private; |
| 124 |
bi = oi->oi_orig; |
| 125 |
|
| 126 |
/* find the overlay number, o */ |
| 127 |
for ( o = 0, on2 = oi->oi_list; on2 && on2 != on; on2 = on2->on_next, o++ ) |
| 128 |
; |
| 129 |
|
| 130 |
if ( on2 == NULL ) { |
| 131 |
return -1; |
| 132 |
} |
| 133 |
|
| 134 |
/* find the overlay type number, j */ |
| 135 |
for ( on2 = overlay_next( NULL ), j = 0; on2; on2 = overlay_next( on2 ), j++ ) { |
| 136 |
if ( on2->on_bi.bi_type == on->on_bi.bi_type ) { |
| 137 |
break; |
| 138 |
} |
| 139 |
} |
| 140 |
assert( on2 != NULL ); |
| 141 |
|
| 142 |
bv.bv_len = snprintf( buf, sizeof( buf ), "cn=Overlay %d", o ); |
| 143 |
bv.bv_val = buf; |
| 144 |
|
| 145 |
e_overlay = monitor_entry_stub( &e_database->e_name, &e_database->e_nname, &bv, |
| 146 |
mi->mi_oc_monitoredObject, NULL, NULL ); |
| 147 |
|
| 148 |
if ( e_overlay == NULL ) { |
| 149 |
Debug( LDAP_DEBUG_ANY, |
| 150 |
"monitor_subsys_overlay_init_one: " |
| 151 |
"unable to create entry " |
| 152 |
"\"cn=Overlay %d,%s\"\n", |
| 153 |
o, e_database->e_name.bv_val ); |
| 154 |
return( -1 ); |
| 155 |
} |
| 156 |
ber_str2bv( on->on_bi.bi_type, 0, 0, &bv ); |
| 157 |
attr_merge_normalize_one( e_overlay, mi->mi_ad_monitoredInfo, &bv, NULL ); |
| 158 |
|
| 159 |
bv.bv_len = snprintf( buf, sizeof( buf ), "cn=Overlay %d,%s", |
| 160 |
j, ms_overlay->mss_dn.bv_val ); |
| 161 |
bv.bv_val = buf; |
| 162 |
attr_merge_normalize_one( e_overlay, slap_schema.si_ad_seeAlso, |
| 163 |
&bv, NULL ); |
| 164 |
|
| 165 |
if ( SLAP_MONITOR( be ) ) { |
| 166 |
attr_merge( e_overlay, slap_schema.si_ad_monitorContext, |
| 167 |
be->be_suffix, be->be_nsuffix ); |
| 168 |
|
| 169 |
} else { |
| 170 |
attr_merge( e_overlay, slap_schema.si_ad_namingContexts, |
| 171 |
be->be_suffix, be->be_nsuffix ); |
| 172 |
} |
| 173 |
|
| 174 |
mp_overlay = monitor_entrypriv_create(); |
| 175 |
if ( mp_overlay == NULL ) { |
| 176 |
return -1; |
| 177 |
} |
| 178 |
e_overlay->e_private = ( void * )mp_overlay; |
| 179 |
mp_overlay->mp_info = ms; |
| 180 |
mp_overlay->mp_flags = ms->mss_flags | MONITOR_F_SUB; |
| 181 |
|
| 182 |
if ( monitor_cache_add( mi, e_overlay, e_database ) ) { |
| 183 |
Debug( LDAP_DEBUG_ANY, |
| 184 |
"monitor_subsys_overlay_init_one: " |
| 185 |
"unable to add entry " |
| 186 |
"\"cn=Overlay %d,%s\"\n", |
| 187 |
o, e_database->e_name.bv_val ); |
| 188 |
return -1; |
| 189 |
} |
| 190 |
|
| 191 |
*ep_overlay = e_overlay; |
| 192 |
return 0; |
| 193 |
} |
| 194 |
|
| 195 |
static int |
| 196 |
monitor_subsys_database_init_one( |
| 197 |
monitor_info_t *mi, |
| 198 |
BackendDB *be, |
| 199 |
monitor_subsys_t *ms, |
| 200 |
monitor_subsys_t *ms_backend, |
| 201 |
monitor_subsys_t *ms_overlay, |
| 202 |
struct berval *rdn, |
| 203 |
Entry *e_database, |
| 204 |
struct slap_overinst *overlay, |
| 205 |
Entry **ep ) |
| 206 |
{ |
| 207 |
char buf[ BACKMONITOR_BUFSIZE ]; |
| 208 |
int j; |
| 209 |
slap_overinfo *oi = NULL; |
| 210 |
BackendInfo *bi, *bi2; |
| 211 |
Entry *e; |
| 212 |
monitor_entry_t *mp; |
| 213 |
char *rdnval = strchr( rdn->bv_val, '=' ) + 1; |
| 214 |
struct berval bv; |
| 215 |
|
| 216 |
bi = be->bd_info; |
| 217 |
|
| 218 |
if ( be->be_suffix == NULL ) { |
| 219 |
Debug( LDAP_DEBUG_ANY, |
| 220 |
"monitor_subsys_database_init_one: " |
| 221 |
"missing suffix for %s\n", |
| 222 |
rdnval ); |
| 223 |
return( -1 ); |
| 224 |
} |
| 225 |
|
| 226 |
if ( overlay_is_over( be ) ) { |
| 227 |
oi = (slap_overinfo *)be->bd_info->bi_private; |
| 228 |
bi = oi->oi_orig; |
| 229 |
} |
| 230 |
|
| 231 |
e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, rdn, |
| 232 |
mi->mi_oc_monitoredObject, NULL, NULL ); |
| 233 |
|
| 234 |
if ( e == NULL ) { |
| 235 |
Debug( LDAP_DEBUG_ANY, |
| 236 |
"monitor_subsys_database_init_one: " |
| 237 |
"unable to create entry \"%s,%s\"\n", |
| 238 |
rdn->bv_val, ms->mss_dn.bv_val ); |
| 239 |
return( -1 ); |
| 240 |
} |
| 241 |
|
| 242 |
ber_str2bv( bi->bi_type, 0, 0, &bv ); |
| 243 |
attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo, &bv, NULL ); |
| 244 |
attr_merge_one( e, mi->mi_ad_monitorIsShadow, |
| 245 |
SLAP_SHADOW( be ) ? (struct berval *)&slap_true_bv : |
| 246 |
(struct berval *)&slap_false_bv, NULL ); |
| 247 |
|
| 248 |
if ( SLAP_MONITOR( be ) ) { |
| 249 |
attr_merge( e, slap_schema.si_ad_monitorContext, |
| 250 |
be->be_suffix, be->be_nsuffix ); |
| 251 |
attr_merge( e_database, slap_schema.si_ad_monitorContext, |
| 252 |
be->be_suffix, be->be_nsuffix ); |
| 253 |
|
| 254 |
} else { |
| 255 |
attr_merge( e, slap_schema.si_ad_namingContexts, |
| 256 |
be->be_suffix, be->be_nsuffix ); |
| 257 |
attr_merge( e_database, slap_schema.si_ad_namingContexts, |
| 258 |
be->be_suffix, be->be_nsuffix ); |
| 259 |
|
| 260 |
if ( SLAP_GLUE_SUBORDINATE( be ) ) { |
| 261 |
BackendDB *sup_be = select_backend( &be->be_nsuffix[ 0 ], 1 ); |
| 262 |
if ( sup_be == NULL ) { |
| 263 |
Debug( LDAP_DEBUG_ANY, |
| 264 |
"monitor_subsys_database_init: " |
| 265 |
"unable to get superior for %s\n", |
| 266 |
be->be_suffix[ 0 ].bv_val ); |
| 267 |
|
| 268 |
} else { |
| 269 |
attr_merge( e, mi->mi_ad_monitorSuperiorDN, |
| 270 |
sup_be->be_suffix, sup_be->be_nsuffix ); |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
(void)init_readOnly( mi, e, be->be_restrictops ); |
| 276 |
(void)init_restrictedOperation( mi, e, be->be_restrictops ); |
| 277 |
|
| 278 |
if ( SLAP_SHADOW( be ) && be->be_update_refs ) { |
| 279 |
attr_merge_normalize( e, mi->mi_ad_monitorUpdateRef, |
| 280 |
be->be_update_refs, NULL ); |
| 281 |
} |
| 282 |
|
| 283 |
if ( oi != NULL ) { |
| 284 |
slap_overinst *on = oi->oi_list, |
| 285 |
*on1 = on; |
| 286 |
|
| 287 |
for ( ; on; on = on->on_next ) { |
| 288 |
slap_overinst *on2; |
| 289 |
|
| 290 |
for ( on2 = on1; on2 != on; on2 = on2->on_next ) { |
| 291 |
if ( on2->on_bi.bi_type == on->on_bi.bi_type ) { |
| 292 |
break; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
if ( on2 != on ) { |
| 297 |
break; |
| 298 |
} |
| 299 |
|
| 300 |
ber_str2bv( on->on_bi.bi_type, 0, 0, &bv ); |
| 301 |
attr_merge_normalize_one( e, mi->mi_ad_monitorOverlay, |
| 302 |
&bv, NULL ); |
| 303 |
|
| 304 |
/* find the overlay number, j */ |
| 305 |
for ( on2 = overlay_next( NULL ), j = 0; on2; on2 = overlay_next( on2 ), j++ ) { |
| 306 |
if ( on2->on_bi.bi_type == on->on_bi.bi_type ) { |
| 307 |
break; |
| 308 |
} |
| 309 |
} |
| 310 |
assert( on2 != NULL ); |
| 311 |
|
| 312 |
snprintf( buf, sizeof( buf ), |
| 313 |
"cn=Overlay %d,%s", |
| 314 |
j, ms_overlay->mss_dn.bv_val ); |
| 315 |
ber_str2bv( buf, 0, 0, &bv ); |
| 316 |
attr_merge_normalize_one( e, |
| 317 |
slap_schema.si_ad_seeAlso, |
| 318 |
&bv, NULL ); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
j = -1; |
| 323 |
LDAP_STAILQ_FOREACH( bi2, &backendInfo, bi_next ) { |
| 324 |
j++; |
| 325 |
if ( bi2->bi_type == bi->bi_type ) { |
| 326 |
snprintf( buf, sizeof( buf ), |
| 327 |
"cn=Backend %d,%s", |
| 328 |
j, ms_backend->mss_dn.bv_val ); |
| 329 |
bv.bv_val = buf; |
| 330 |
bv.bv_len = strlen( buf ); |
| 331 |
attr_merge_normalize_one( e, |
| 332 |
slap_schema.si_ad_seeAlso, |
| 333 |
&bv, NULL ); |
| 334 |
break; |
| 335 |
} |
| 336 |
} |
| 337 |
/* we must find it! */ |
| 338 |
assert( j >= 0 ); |
| 339 |
|
| 340 |
mp = monitor_entrypriv_create(); |
| 341 |
if ( mp == NULL ) { |
| 342 |
return -1; |
| 343 |
} |
| 344 |
e->e_private = ( void * )mp; |
| 345 |
mp->mp_info = ms; |
| 346 |
mp->mp_flags = ms->mss_flags |
| 347 |
| MONITOR_F_SUB; |
| 348 |
mp->mp_private = be; |
| 349 |
|
| 350 |
if ( monitor_cache_add( mi, e, e_database ) ) { |
| 351 |
Debug( LDAP_DEBUG_ANY, |
| 352 |
"monitor_subsys_database_init_one: " |
| 353 |
"unable to add entry \"%s,%s\"\n", |
| 354 |
rdn->bv_val, ms->mss_dn.bv_val ); |
| 355 |
return( -1 ); |
| 356 |
} |
| 357 |
|
| 358 |
#if defined(LDAP_SLAPI) |
| 359 |
monitor_back_add_plugin( mi, be, e ); |
| 360 |
#endif /* defined(LDAP_SLAPI) */ |
| 361 |
|
| 362 |
if ( oi != NULL ) { |
| 363 |
Entry *e_overlay; |
| 364 |
slap_overinst *on = oi->oi_list; |
| 365 |
|
| 366 |
for ( ; on; on = on->on_next ) { |
| 367 |
monitor_subsys_overlay_init_one( mi, be, |
| 368 |
ms, ms_overlay, on, e, &e_overlay ); |
| 369 |
if ( overlay == on ) { |
| 370 |
*ep = e_overlay; |
| 371 |
} |
| 372 |
} |
| 373 |
} |
| 374 |
if ( overlay == NULL ) { |
| 375 |
*ep = e; |
| 376 |
} |
| 377 |
|
| 378 |
return 0; |
| 379 |
} |
| 380 |
|
| 381 |
static int |
| 382 |
monitor_back_register_database_and_overlay( |
| 383 |
BackendDB *be, |
| 384 |
struct slap_overinst *on, |
| 385 |
struct berval *ndn_out ) |
| 386 |
{ |
| 387 |
monitor_info_t *mi; |
| 388 |
Entry *e_database, *e = NULL; |
| 389 |
int i, rc; |
| 390 |
monitor_entry_t *mp; |
| 391 |
monitor_subsys_t *ms_backend, |
| 392 |
*ms_database, |
| 393 |
*ms_overlay; |
| 394 |
struct berval bv; |
| 395 |
char buf[ BACKMONITOR_BUFSIZE ]; |
| 396 |
|
| 397 |
assert( be_monitor != NULL ); |
| 398 |
|
| 399 |
if ( !monitor_subsys_is_opened() ) { |
| 400 |
if ( on ) { |
| 401 |
return monitor_back_register_overlay_limbo( be, on, ndn_out ); |
| 402 |
|
| 403 |
} else { |
| 404 |
return monitor_back_register_database_limbo( be, ndn_out ); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
mi = ( monitor_info_t * )be_monitor->be_private; |
| 409 |
|
| 410 |
ms_backend = monitor_back_get_subsys( SLAPD_MONITOR_BACKEND_NAME ); |
| 411 |
if ( ms_backend == NULL ) { |
| 412 |
Debug( LDAP_DEBUG_ANY, |
| 413 |
"monitor_back_register_database: " |
| 414 |
"unable to get " |
| 415 |
"\"" SLAPD_MONITOR_BACKEND_NAME "\" " |
| 416 |
"subsystem\n" ); |
| 417 |
return -1; |
| 418 |
} |
| 419 |
|
| 420 |
ms_database = monitor_back_get_subsys( SLAPD_MONITOR_DATABASE_NAME ); |
| 421 |
if ( ms_database == NULL ) { |
| 422 |
Debug( LDAP_DEBUG_ANY, |
| 423 |
"monitor_back_register_database: " |
| 424 |
"unable to get " |
| 425 |
"\"" SLAPD_MONITOR_DATABASE_NAME "\" " |
| 426 |
"subsystem\n" ); |
| 427 |
return -1; |
| 428 |
} |
| 429 |
|
| 430 |
ms_overlay = monitor_back_get_subsys( SLAPD_MONITOR_OVERLAY_NAME ); |
| 431 |
if ( ms_overlay == NULL ) { |
| 432 |
Debug( LDAP_DEBUG_ANY, |
| 433 |
"monitor_back_register_database: " |
| 434 |
"unable to get " |
| 435 |
"\"" SLAPD_MONITOR_OVERLAY_NAME "\" " |
| 436 |
"subsystem\n" ); |
| 437 |
return -1; |
| 438 |
} |
| 439 |
|
| 440 |
if ( monitor_cache_get( mi, &ms_database->mss_ndn, &e_database ) ) { |
| 441 |
Debug( LDAP_DEBUG_ANY, |
| 442 |
"monitor_subsys_database_init: " |
| 443 |
"unable to get entry \"%s\"\n", |
| 444 |
ms_database->mss_ndn.bv_val ); |
| 445 |
return( -1 ); |
| 446 |
} |
| 447 |
|
| 448 |
/* FIXME: It's only safe since we're paused */ |
| 449 |
mp = ( monitor_entry_t * )e_database->e_private; |
| 450 |
for ( i = -1, e = mp->mp_children; e; i++ ) { |
| 451 |
mp = ( monitor_entry_t * )e->e_private; |
| 452 |
|
| 453 |
assert( mp != NULL ); |
| 454 |
if ( mp->mp_private == be->bd_self ) { |
| 455 |
rc = 0; |
| 456 |
goto done; |
| 457 |
} |
| 458 |
e = mp->mp_next; |
| 459 |
} |
| 460 |
|
| 461 |
bv.bv_val = buf; |
| 462 |
bv.bv_len = snprintf( buf, sizeof( buf ), "cn=Database %d", i ); |
| 463 |
if ( bv.bv_len >= sizeof( buf ) ) { |
| 464 |
rc = -1; |
| 465 |
goto done; |
| 466 |
} |
| 467 |
|
| 468 |
rc = monitor_subsys_database_init_one( mi, be, |
| 469 |
ms_database, ms_backend, ms_overlay, &bv, e_database, on, &e ); |
| 470 |
if ( rc != 0 ) { |
| 471 |
goto done; |
| 472 |
} |
| 473 |
|
| 474 |
done:; |
| 475 |
monitor_cache_release( mi, e_database ); |
| 476 |
if ( rc == 0 && ndn_out && e ) { |
| 477 |
*ndn_out = e->e_nname; |
| 478 |
} |
| 479 |
|
| 480 |
return rc; |
| 481 |
} |
| 482 |
|
| 483 |
int |
| 484 |
monitor_back_register_database( |
| 485 |
BackendDB *be, |
| 486 |
struct berval *ndn_out ) |
| 487 |
{ |
| 488 |
return monitor_back_register_database_and_overlay( be, NULL, ndn_out ); |
| 489 |
} |
| 490 |
|
| 491 |
int |
| 492 |
monitor_back_register_overlay( |
| 493 |
BackendDB *be, |
| 494 |
struct slap_overinst *on, |
| 495 |
struct berval *ndn_out ) |
| 496 |
{ |
| 497 |
return monitor_back_register_database_and_overlay( be, on, ndn_out ); |
| 498 |
} |
| 499 |
|
| 500 |
int |
| 501 |
monitor_subsys_database_init( |
| 502 |
BackendDB *be, |
| 503 |
monitor_subsys_t *ms ) |
| 504 |
{ |
| 505 |
monitor_info_t *mi; |
| 506 |
Entry *e_database, *e; |
| 507 |
int i, rc; |
| 508 |
monitor_subsys_t *ms_backend, |
| 509 |
*ms_overlay; |
| 510 |
struct berval bv; |
| 511 |
|
| 512 |
assert( be != NULL ); |
| 513 |
|
| 514 |
ms->mss_modify = monitor_subsys_database_modify; |
| 515 |
|
| 516 |
mi = ( monitor_info_t * )be->be_private; |
| 517 |
|
| 518 |
ms_backend = monitor_back_get_subsys( SLAPD_MONITOR_BACKEND_NAME ); |
| 519 |
if ( ms_backend == NULL ) { |
| 520 |
Debug( LDAP_DEBUG_ANY, |
| 521 |
"monitor_subsys_database_init: " |
| 522 |
"unable to get " |
| 523 |
"\"" SLAPD_MONITOR_BACKEND_NAME "\" " |
| 524 |
"subsystem\n" ); |
| 525 |
return -1; |
| 526 |
} |
| 527 |
|
| 528 |
ms_overlay = monitor_back_get_subsys( SLAPD_MONITOR_OVERLAY_NAME ); |
| 529 |
if ( ms_overlay == NULL ) { |
| 530 |
Debug( LDAP_DEBUG_ANY, |
| 531 |
"monitor_subsys_database_init: " |
| 532 |
"unable to get " |
| 533 |
"\"" SLAPD_MONITOR_OVERLAY_NAME "\" " |
| 534 |
"subsystem\n" ); |
| 535 |
return -1; |
| 536 |
} |
| 537 |
|
| 538 |
if ( monitor_cache_get( mi, &ms->mss_ndn, &e_database ) ) { |
| 539 |
Debug( LDAP_DEBUG_ANY, |
| 540 |
"monitor_subsys_database_init: " |
| 541 |
"unable to get entry \"%s\"\n", |
| 542 |
ms->mss_ndn.bv_val ); |
| 543 |
return( -1 ); |
| 544 |
} |
| 545 |
|
| 546 |
(void)init_readOnly( mi, e_database, frontendDB->be_restrictops ); |
| 547 |
(void)init_restrictedOperation( mi, e_database, frontendDB->be_restrictops ); |
| 548 |
|
| 549 |
BER_BVSTR( &bv, "cn=Frontend" ); |
| 550 |
rc = monitor_subsys_database_init_one( mi, frontendDB, |
| 551 |
ms, ms_backend, ms_overlay, &bv, e_database, NULL, &e ); |
| 552 |
if ( rc != 0 ) { |
| 553 |
return rc; |
| 554 |
} |
| 555 |
|
| 556 |
i = -1; |
| 557 |
LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) { |
| 558 |
char buf[ BACKMONITOR_BUFSIZE ]; |
| 559 |
|
| 560 |
bv.bv_val = buf; |
| 561 |
bv.bv_len = snprintf( buf, sizeof( buf ), "cn=Database %d", ++i ); |
| 562 |
if ( bv.bv_len >= sizeof( buf ) ) { |
| 563 |
return -1; |
| 564 |
} |
| 565 |
|
| 566 |
rc = monitor_subsys_database_init_one( mi, be, |
| 567 |
ms, ms_backend, ms_overlay, &bv, e_database, NULL, &e ); |
| 568 |
if ( rc != 0 ) { |
| 569 |
return rc; |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
monitor_cache_release( mi, e_database ); |
| 574 |
|
| 575 |
return( 0 ); |
| 576 |
} |
| 577 |
|
| 578 |
/* |
| 579 |
* v: array of values |
| 580 |
* cur: must not contain the tags corresponding to the values in v |
| 581 |
* delta: will contain the tags corresponding to the values in v |
| 582 |
*/ |
| 583 |
static int |
| 584 |
value_mask( BerVarray v, slap_mask_t cur, slap_mask_t *delta ) |
| 585 |
{ |
| 586 |
for ( ; !BER_BVISNULL( v ); v++ ) { |
| 587 |
struct restricted_ops_t *rops; |
| 588 |
int i; |
| 589 |
|
| 590 |
if ( OID_LEADCHAR( v->bv_val[ 0 ] ) ) { |
| 591 |
rops = restricted_exops; |
| 592 |
|
| 593 |
} else { |
| 594 |
rops = restricted_ops; |
| 595 |
} |
| 596 |
|
| 597 |
for ( i = 0; !BER_BVISNULL( &rops[ i ].op ); i++ ) { |
| 598 |
if ( ber_bvstrcasecmp( v, &rops[ i ].op ) != 0 ) { |
| 599 |
continue; |
| 600 |
} |
| 601 |
|
| 602 |
if ( rops[ i ].tag & *delta ) { |
| 603 |
return LDAP_OTHER; |
| 604 |
} |
| 605 |
|
| 606 |
if ( rops[ i ].tag & cur ) { |
| 607 |
return LDAP_OTHER; |
| 608 |
} |
| 609 |
|
| 610 |
cur |= rops[ i ].tag; |
| 611 |
*delta |= rops[ i ].tag; |
| 612 |
|
| 613 |
break; |
| 614 |
} |
| 615 |
|
| 616 |
if ( BER_BVISNULL( &rops[ i ].op ) ) { |
| 617 |
return LDAP_INVALID_SYNTAX; |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
return LDAP_SUCCESS; |
| 622 |
} |
| 623 |
|
| 624 |
static int |
| 625 |
monitor_subsys_database_modify( |
| 626 |
Operation *op, |
| 627 |
SlapReply *rs, |
| 628 |
Entry *e ) |
| 629 |
{ |
| 630 |
monitor_info_t *mi = (monitor_info_t *)op->o_bd->be_private; |
| 631 |
int rc = LDAP_OTHER; |
| 632 |
Attribute *save_attrs, *a; |
| 633 |
Modifications *ml; |
| 634 |
Backend *be; |
| 635 |
int ro_gotval = 1, i, n; |
| 636 |
slap_mask_t rp_add = 0, rp_delete = 0, rp_cur; |
| 637 |
struct berval *tf; |
| 638 |
|
| 639 |
i = sscanf( e->e_nname.bv_val, "cn=database %d,", &n ); |
| 640 |
if ( i != 1 ) { |
| 641 |
return SLAP_CB_CONTINUE; |
| 642 |
} |
| 643 |
|
| 644 |
if ( n < 0 || n >= nBackendDB ) { |
| 645 |
rs->sr_text = "invalid database index"; |
| 646 |
return ( rs->sr_err = LDAP_NO_SUCH_OBJECT ); |
| 647 |
} |
| 648 |
|
| 649 |
LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) { |
| 650 |
if ( n == 0 ) { |
| 651 |
break; |
| 652 |
} |
| 653 |
n--; |
| 654 |
} |
| 655 |
/* do not allow some changes on back-monitor (needs work)... */ |
| 656 |
if ( SLAP_MONITOR( be ) ) { |
| 657 |
rs->sr_text = "no modifications allowed to monitor database entry"; |
| 658 |
return ( rs->sr_err = LDAP_UNWILLING_TO_PERFORM ); |
| 659 |
} |
| 660 |
|
| 661 |
rp_cur = be->be_restrictops; |
| 662 |
|
| 663 |
save_attrs = e->e_attrs; |
| 664 |
e->e_attrs = attrs_dup( e->e_attrs ); |
| 665 |
|
| 666 |
for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) { |
| 667 |
Modification *mod = &ml->sml_mod; |
| 668 |
|
| 669 |
if ( mod->sm_desc == mi->mi_ad_readOnly ) { |
| 670 |
int val = -1; |
| 671 |
|
| 672 |
if ( mod->sm_values ) { |
| 673 |
if ( !BER_BVISNULL( &mod->sm_values[ 1 ] ) ) { |
| 674 |
rs->sr_text = "attempting to modify multiple values of single-valued attribute"; |
| 675 |
rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION; |
| 676 |
goto done; |
| 677 |
} |
| 678 |
|
| 679 |
if ( bvmatch( &slap_true_bv, mod->sm_values )) { |
| 680 |
val = 1; |
| 681 |
|
| 682 |
} else if ( bvmatch( &slap_false_bv, mod->sm_values )) { |
| 683 |
val = 0; |
| 684 |
|
| 685 |
} else { |
| 686 |
assert( 0 ); |
| 687 |
rc = rs->sr_err = LDAP_INVALID_SYNTAX; |
| 688 |
goto done; |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
switch ( mod->sm_op ) { |
| 693 |
case LDAP_MOD_DELETE: |
| 694 |
if ( ro_gotval < 1 ) { |
| 695 |
rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION; |
| 696 |
goto done; |
| 697 |
} |
| 698 |
ro_gotval--; |
| 699 |
|
| 700 |
if ( val == 0 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) { |
| 701 |
rc = rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE; |
| 702 |
goto done; |
| 703 |
} |
| 704 |
|
| 705 |
if ( val == 1 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) != SLAP_RESTRICT_OP_WRITES ) { |
| 706 |
rc = rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE; |
| 707 |
goto done; |
| 708 |
} |
| 709 |
|
| 710 |
break; |
| 711 |
|
| 712 |
case LDAP_MOD_REPLACE: |
| 713 |
ro_gotval = 0; |
| 714 |
/* fall thru */ |
| 715 |
|
| 716 |
case LDAP_MOD_ADD: |
| 717 |
if ( ro_gotval > 0 ) { |
| 718 |
rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION; |
| 719 |
goto done; |
| 720 |
} |
| 721 |
ro_gotval++; |
| 722 |
|
| 723 |
if ( val == 1 ) { |
| 724 |
rp_add |= (~rp_cur) & SLAP_RESTRICT_OP_WRITES; |
| 725 |
rp_cur |= SLAP_RESTRICT_OP_WRITES; |
| 726 |
rp_delete &= ~SLAP_RESTRICT_OP_WRITES; |
| 727 |
|
| 728 |
} else if ( val == 0 ) { |
| 729 |
rp_delete |= rp_cur & SLAP_RESTRICT_OP_WRITES; |
| 730 |
rp_cur &= ~SLAP_RESTRICT_OP_WRITES; |
| 731 |
rp_add &= ~SLAP_RESTRICT_OP_WRITES; |
| 732 |
} |
| 733 |
break; |
| 734 |
|
| 735 |
default: |
| 736 |
rc = rs->sr_err = LDAP_OTHER; |
| 737 |
goto done; |
| 738 |
} |
| 739 |
|
| 740 |
} else if ( mod->sm_desc == mi->mi_ad_restrictedOperation ) { |
| 741 |
slap_mask_t mask = 0; |
| 742 |
|
| 743 |
switch ( mod->sm_op ) { |
| 744 |
case LDAP_MOD_DELETE: |
| 745 |
if ( mod->sm_values == NULL ) { |
| 746 |
rp_delete = rp_cur; |
| 747 |
rp_cur = 0; |
| 748 |
rp_add = 0; |
| 749 |
break; |
| 750 |
} |
| 751 |
rc = value_mask( mod->sm_values, ~rp_cur, &mask ); |
| 752 |
if ( rc == LDAP_SUCCESS ) { |
| 753 |
rp_delete |= mask; |
| 754 |
rp_add &= ~mask; |
| 755 |
rp_cur &= ~mask; |
| 756 |
|
| 757 |
} else if ( rc == LDAP_OTHER ) { |
| 758 |
rc = LDAP_NO_SUCH_ATTRIBUTE; |
| 759 |
} |
| 760 |
break; |
| 761 |
|
| 762 |
case LDAP_MOD_REPLACE: |
| 763 |
rp_delete = rp_cur; |
| 764 |
rp_cur = 0; |
| 765 |
rp_add = 0; |
| 766 |
/* fall thru */ |
| 767 |
|
| 768 |
case LDAP_MOD_ADD: |
| 769 |
rc = value_mask( mod->sm_values, rp_cur, &mask ); |
| 770 |
if ( rc == LDAP_SUCCESS ) { |
| 771 |
rp_add |= mask; |
| 772 |
rp_cur |= mask; |
| 773 |
rp_delete &= ~mask; |
| 774 |
|
| 775 |
} else if ( rc == LDAP_OTHER ) { |
| 776 |
rc = rs->sr_err = LDAP_TYPE_OR_VALUE_EXISTS; |
| 777 |
} |
| 778 |
break; |
| 779 |
|
| 780 |
default: |
| 781 |
rc = rs->sr_err = LDAP_OTHER; |
| 782 |
break; |
| 783 |
} |
| 784 |
|
| 785 |
if ( rc != LDAP_SUCCESS ) { |
| 786 |
goto done; |
| 787 |
} |
| 788 |
|
| 789 |
} else if ( is_at_operational( mod->sm_desc->ad_type )) { |
| 790 |
/* accept all operational attributes */ |
| 791 |
attr_delete( &e->e_attrs, mod->sm_desc ); |
| 792 |
rc = attr_merge( e, mod->sm_desc, mod->sm_values, |
| 793 |
mod->sm_nvalues ); |
| 794 |
if ( rc ) { |
| 795 |
rc = rs->sr_err = LDAP_OTHER; |
| 796 |
break; |
| 797 |
} |
| 798 |
|
| 799 |
} else { |
| 800 |
rc = rs->sr_err = LDAP_UNWILLING_TO_PERFORM; |
| 801 |
break; |
| 802 |
} |
| 803 |
} |
| 804 |
|
| 805 |
/* sanity checks: */ |
| 806 |
if ( ro_gotval < 1 ) { |
| 807 |
rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION; |
| 808 |
goto done; |
| 809 |
} |
| 810 |
|
| 811 |
if ( ( rp_cur & SLAP_RESTRICT_OP_EXTENDED ) && ( rp_cur & SLAP_RESTRICT_EXOP_MASK ) ) { |
| 812 |
rc = rs->sr_err = LDAP_CONSTRAINT_VIOLATION; |
| 813 |
goto done; |
| 814 |
} |
| 815 |
|
| 816 |
if ( rp_delete & rp_add ) { |
| 817 |
rc = rs->sr_err = LDAP_OTHER; |
| 818 |
goto done; |
| 819 |
} |
| 820 |
|
| 821 |
/* check current value of readOnly */ |
| 822 |
if ( ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) { |
| 823 |
tf = (struct berval *)&slap_true_bv; |
| 824 |
|
| 825 |
} else { |
| 826 |
tf = (struct berval *)&slap_false_bv; |
| 827 |
} |
| 828 |
|
| 829 |
a = attr_find( e->e_attrs, mi->mi_ad_readOnly ); |
| 830 |
if ( a == NULL ) { |
| 831 |
rc = LDAP_OTHER; |
| 832 |
goto done; |
| 833 |
} |
| 834 |
|
| 835 |
if ( !bvmatch( &a->a_vals[ 0 ], tf ) ) { |
| 836 |
attr_delete( &e->e_attrs, mi->mi_ad_readOnly ); |
| 837 |
rc = attr_merge_one( e, mi->mi_ad_readOnly, tf, tf ); |
| 838 |
} |
| 839 |
|
| 840 |
if ( rc == LDAP_SUCCESS ) { |
| 841 |
if ( rp_delete ) { |
| 842 |
if ( rp_delete == be->be_restrictops ) { |
| 843 |
attr_delete( &e->e_attrs, mi->mi_ad_restrictedOperation ); |
| 844 |
|
| 845 |
} else { |
| 846 |
a = attr_find( e->e_attrs, mi->mi_ad_restrictedOperation ); |
| 847 |
if ( a == NULL ) { |
| 848 |
rc = rs->sr_err = LDAP_OTHER; |
| 849 |
goto done; |
| 850 |
} |
| 851 |
|
| 852 |
for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) { |
| 853 |
if ( rp_delete & restricted_ops[ i ].tag ) { |
| 854 |
int j; |
| 855 |
|
| 856 |
for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) { |
| 857 |
int k; |
| 858 |
|
| 859 |
if ( !bvmatch( &a->a_nvals[ j ], &restricted_ops[ i ].op ) ) { |
| 860 |
continue; |
| 861 |
} |
| 862 |
|
| 863 |
ch_free( a->a_vals[ j ].bv_val ); |
| 864 |
ch_free( a->a_nvals[ j ].bv_val ); |
| 865 |
|
| 866 |
for ( k = j + 1; !BER_BVISNULL( &a->a_nvals[ k ] ); k++ ) { |
| 867 |
a->a_vals[ k - 1 ] = a->a_vals[ k ]; |
| 868 |
a->a_nvals[ k - 1 ] = a->a_nvals[ k ]; |
| 869 |
} |
| 870 |
|
| 871 |
BER_BVZERO( &a->a_vals[ k - 1 ] ); |
| 872 |
BER_BVZERO( &a->a_nvals[ k - 1 ] ); |
| 873 |
a->a_numvals--; |
| 874 |
} |
| 875 |
} |
| 876 |
} |
| 877 |
|
| 878 |
for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) { |
| 879 |
if ( rp_delete & restricted_exops[ i ].tag ) { |
| 880 |
int j; |
| 881 |
|
| 882 |
for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) { |
| 883 |
int k; |
| 884 |
|
| 885 |
if ( !bvmatch( &a->a_nvals[ j ], &restricted_exops[ i ].op ) ) { |
| 886 |
continue; |
| 887 |
} |
| 888 |
|
| 889 |
ch_free( a->a_vals[ j ].bv_val ); |
| 890 |
ch_free( a->a_nvals[ j ].bv_val ); |
| 891 |
|
| 892 |
for ( k = j + 1; !BER_BVISNULL( &a->a_nvals[ k ] ); k++ ) { |
| 893 |
a->a_vals[ k - 1 ] = a->a_vals[ k ]; |
| 894 |
a->a_nvals[ k - 1 ] = a->a_nvals[ k ]; |
| 895 |
} |
| 896 |
|
| 897 |
BER_BVZERO( &a->a_vals[ k - 1 ] ); |
| 898 |
BER_BVZERO( &a->a_nvals[ k - 1 ] ); |
| 899 |
a->a_numvals--; |
| 900 |
} |
| 901 |
} |
| 902 |
} |
| 903 |
|
| 904 |
if ( a->a_vals == NULL ) { |
| 905 |
assert( a->a_numvals == 0 ); |
| 906 |
|
| 907 |
attr_delete( &e->e_attrs, mi->mi_ad_restrictedOperation ); |
| 908 |
} |
| 909 |
} |
| 910 |
} |
| 911 |
|
| 912 |
if ( rp_add ) { |
| 913 |
for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) { |
| 914 |
if ( rp_add & restricted_ops[ i ].tag ) { |
| 915 |
attr_merge_one( e, mi->mi_ad_restrictedOperation, |
| 916 |
&restricted_ops[ i ].op, |
| 917 |
&restricted_ops[ i ].op ); |
| 918 |
} |
| 919 |
} |
| 920 |
|
| 921 |
for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) { |
| 922 |
if ( rp_add & restricted_exops[ i ].tag ) { |
| 923 |
attr_merge_one( e, mi->mi_ad_restrictedOperation, |
| 924 |
&restricted_exops[ i ].op, |
| 925 |
&restricted_exops[ i ].op ); |
| 926 |
} |
| 927 |
} |
| 928 |
} |
| 929 |
} |
| 930 |
|
| 931 |
be->be_restrictops = rp_cur; |
| 932 |
|
| 933 |
done:; |
| 934 |
if ( rc == LDAP_SUCCESS ) { |
| 935 |
attrs_free( save_attrs ); |
| 936 |
rc = SLAP_CB_CONTINUE; |
| 937 |
|
| 938 |
} else { |
| 939 |
Attribute *tmp = e->e_attrs; |
| 940 |
e->e_attrs = save_attrs; |
| 941 |
attrs_free( tmp ); |
| 942 |
} |
| 943 |
return rc; |
| 944 |
} |
| 945 |
|
| 946 |
#if defined(LDAP_SLAPI) |
| 947 |
static int |
| 948 |
monitor_back_add_plugin( monitor_info_t *mi, Backend *be, Entry *e_database ) |
| 949 |
{ |
| 950 |
Slapi_PBlock *pCurrentPB; |
| 951 |
int i, rc = LDAP_SUCCESS; |
| 952 |
|
| 953 |
if ( slapi_int_pblock_get_first( be, &pCurrentPB ) != LDAP_SUCCESS ) { |
| 954 |
/* |
| 955 |
* LDAP_OTHER is returned if no plugins are installed |
| 956 |
*/ |
| 957 |
rc = LDAP_OTHER; |
| 958 |
goto done; |
| 959 |
} |
| 960 |
|
| 961 |
i = 0; |
| 962 |
do { |
| 963 |
Slapi_PluginDesc *srchdesc; |
| 964 |
char buf[ BACKMONITOR_BUFSIZE ]; |
| 965 |
struct berval bv; |
| 966 |
|
| 967 |
rc = slapi_pblock_get( pCurrentPB, SLAPI_PLUGIN_DESCRIPTION, |
| 968 |
&srchdesc ); |
| 969 |
if ( rc != LDAP_SUCCESS ) { |
| 970 |
goto done; |
| 971 |
} |
| 972 |
if ( srchdesc ) { |
| 973 |
snprintf( buf, sizeof(buf), |
| 974 |
"plugin %d name: %s; " |
| 975 |
"vendor: %s; " |
| 976 |
"version: %s; " |
| 977 |
"description: %s", |
| 978 |
i, |
| 979 |
srchdesc->spd_id, |
| 980 |
srchdesc->spd_vendor, |
| 981 |
srchdesc->spd_version, |
| 982 |
srchdesc->spd_description ); |
| 983 |
} else { |
| 984 |
snprintf( buf, sizeof(buf), |
| 985 |
"plugin %d name: <no description available>", i ); |
| 986 |
} |
| 987 |
|
| 988 |
ber_str2bv( buf, 0, 0, &bv ); |
| 989 |
attr_merge_normalize_one( e_database, |
| 990 |
mi->mi_ad_monitoredInfo, &bv, NULL ); |
| 991 |
|
| 992 |
i++; |
| 993 |
|
| 994 |
} while ( ( slapi_int_pblock_get_next( &pCurrentPB ) == LDAP_SUCCESS ) |
| 995 |
&& ( pCurrentPB != NULL ) ); |
| 996 |
|
| 997 |
done: |
| 998 |
return rc; |
| 999 |
} |
| 1000 |
#endif /* defined(LDAP_SLAPI) */ |