1 /* database.c - deals with database subsystem */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2001-2004 The OpenLDAP Foundation.
6 * Portions Copyright 2001-2003 Pierangelo Masarati.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
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>.
18 * This work was initially developed by Pierangelo Masarati for inclusion
19 * in OpenLDAP Software.
25 #include <ac/string.h>
28 #include "back-monitor.h"
30 #if defined(LDAP_SLAPI)
32 static int monitor_back_add_plugin( Backend *be, Entry *e );
33 #endif /* defined(LDAP_SLAPI) */
35 #if defined(SLAPD_LDAP)
36 #include "../back-ldap/back-ldap.h"
37 #endif /* defined(SLAPD_LDAP) */
39 static struct restricted_ops_t {
42 } restricted_ops[] = {
43 { BER_BVC( "add" ), SLAP_RESTRICT_OP_ADD },
44 { BER_BVC( "bind" ), SLAP_RESTRICT_OP_BIND },
45 { BER_BVC( "compare" ), SLAP_RESTRICT_OP_COMPARE },
46 { BER_BVC( "delete" ), SLAP_RESTRICT_OP_DELETE },
47 { BER_BVC( "extended" ), SLAP_RESTRICT_OP_EXTENDED },
48 { BER_BVC( "modify" ), SLAP_RESTRICT_OP_MODIFY },
49 { BER_BVC( "rename" ), SLAP_RESTRICT_OP_RENAME },
50 { BER_BVC( "search" ), SLAP_RESTRICT_OP_SEARCH },
52 }, restricted_exops[] = {
53 { BER_BVC( LDAP_EXOP_START_TLS ), SLAP_RESTRICT_EXOP_START_TLS },
54 { BER_BVC( LDAP_EXOP_MODIFY_PASSWD ), SLAP_RESTRICT_EXOP_MODIFY_PASSWD },
55 { BER_BVC( LDAP_EXOP_X_WHO_AM_I ), SLAP_RESTRICT_EXOP_WHOAMI },
56 { BER_BVC( LDAP_EXOP_X_CANCEL ), SLAP_RESTRICT_EXOP_CANCEL },
61 init_readOnly( monitor_info_t *mi, Entry *e, slap_mask_t restrictops )
63 struct berval *tf = ( ( restrictops & SLAP_RESTRICT_OP_MASK ) == SLAP_RESTRICT_OP_WRITES ) ?
64 (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv;
66 return attr_merge_one( e, mi->mi_ad_readOnly, tf, NULL );
70 init_restrictedOperation( monitor_info_t *mi, Entry *e, slap_mask_t restrictops )
74 for ( i = 0; restricted_ops[ i ].op.bv_val; i++ ) {
75 if ( restrictops & restricted_ops[ i ].tag ) {
76 rc = attr_merge_one( e, mi->mi_ad_restrictedOperation,
77 &restricted_ops[ i ].op, NULL );
84 for ( i = 0; restricted_exops[ i ].op.bv_val; i++ ) {
85 if ( restrictops & restricted_exops[ i ].tag ) {
86 rc = attr_merge_one( e, mi->mi_ad_restrictedOperation,
87 &restricted_exops[ i ].op, NULL );
98 monitor_subsys_database_init(
104 Entry *e_database, **ep;
107 monitor_subsys_t *ms_backend,
110 assert( be != NULL );
112 mi = ( monitor_info_t * )be->be_private;
114 ms_backend = monitor_back_get_subsys( SLAPD_MONITOR_BACKEND_NAME );
115 if ( ms_backend == NULL ) {
116 Debug( LDAP_DEBUG_ANY,
117 "monitor_subsys_database_init: "
119 "\"" SLAPD_MONITOR_BACKEND_NAME "\" "
125 ms_overlay = monitor_back_get_subsys( SLAPD_MONITOR_OVERLAY_NAME );
126 if ( ms_overlay == NULL ) {
127 Debug( LDAP_DEBUG_ANY,
128 "monitor_subsys_database_init: "
130 "\"" SLAPD_MONITOR_OVERLAY_NAME "\" "
136 if ( monitor_cache_get( mi, &ms->mss_ndn, &e_database ) ) {
137 Debug( LDAP_DEBUG_ANY,
138 "monitor_subsys_database_init: "
139 "unable to get entry \"%s\"\n",
140 ms->mss_ndn.bv_val, 0, 0 );
144 (void)init_readOnly( mi, e_database, frontendDB->be_restrictops );
145 (void)init_restrictedOperation( mi, e_database, frontendDB->be_restrictops );
147 mp = ( monitor_entry_t * )e_database->e_private;
148 mp->mp_children = NULL;
149 ep = &mp->mp_children;
151 for ( i = 0; i < nBackendDB; i++ ) {
152 char buf[ BACKMONITOR_BUFSIZE ];
154 slap_overinfo *oi = NULL;
158 be = &backendDB[ i ];
162 if ( overlay_is_over( be ) ) {
163 oi = (slap_overinfo *)be->bd_info->bi_private;
167 /* Subordinates are not exposed as their own naming context */
168 if ( SLAP_GLUE_SUBORDINATE( be ) ) {
172 snprintf( buf, sizeof( buf ),
173 "dn: cn=Database %d,%s\n"
175 "structuralObjectClass: %s\n"
177 "description: This object contains the type of the database.\n"
180 "modifiersName: %s\n"
181 "createTimestamp: %s\n"
182 "modifyTimestamp: %s\n",
185 mi->mi_oc_monitoredObject->soc_cname.bv_val,
186 mi->mi_oc_monitoredObject->soc_cname.bv_val,
188 mi->mi_ad_monitoredInfo->ad_cname.bv_val,
190 mi->mi_creatorsName.bv_val,
191 mi->mi_creatorsName.bv_val,
192 mi->mi_startTime.bv_val,
193 mi->mi_startTime.bv_val );
195 e = str2entry( buf );
197 Debug( LDAP_DEBUG_ANY,
198 "monitor_subsys_database_init: "
199 "unable to create entry \"cn=Database %d,%s\"\n",
200 i, ms->mss_ndn.bv_val, 0 );
204 if ( SLAP_MONITOR(be) ) {
205 attr_merge( e, slap_schema.si_ad_monitorContext,
206 be->be_suffix, be->be_nsuffix );
207 attr_merge( e_database, slap_schema.si_ad_monitorContext,
208 be->be_suffix, be->be_nsuffix );
210 attr_merge( e, slap_schema.si_ad_namingContexts,
211 be->be_suffix, be->be_nsuffix );
212 attr_merge( e_database, slap_schema.si_ad_namingContexts,
213 be->be_suffix, be->be_nsuffix );
216 (void)init_readOnly( mi, e, be->be_restrictops );
217 (void)init_restrictedOperation( mi, e, be->be_restrictops );
220 slap_overinst *on = oi->oi_list;
222 for ( ; on; on = on->on_next ) {
226 bv.bv_val = on->on_bi.bi_type;
227 bv.bv_len = strlen( bv.bv_val );
228 attr_merge_normalize_one( e, mi->mi_ad_monitorOverlay,
231 /* find the overlay number, j */
232 for ( on2 = overlay_next( NULL ), j = 0; on2; on2 = overlay_next( on2 ), j++ ) {
233 if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
239 snprintf( buf, sizeof( buf ),
241 j, ms_overlay->mss_dn.bv_val );
243 bv.bv_len = strlen( buf );
244 attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
249 #if defined(SLAPD_LDAP)
250 if ( strcmp( bi->bi_type, "ldap" ) == 0 ) {
251 struct ldapinfo *li = (struct ldapinfo *)be->be_private;
255 bv.bv_len = strlen( bv.bv_val );
256 attr_merge_normalize_one( e, slap_schema.si_ad_labeledURI,
259 #endif /* defined(SLAPD_LDAP) */
261 for ( j = 0; j < nBackendInfo; j++ ) {
262 if ( backendInfo[ j ].bi_type == bi->bi_type ) {
265 snprintf( buf, sizeof( buf ),
267 j, ms_backend->mss_dn.bv_val );
269 bv.bv_len = strlen( buf );
270 attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
275 /* we must find it! */
278 mp = monitor_entrypriv_create();
282 e->e_private = ( void * )mp;
284 mp->mp_flags = ms->mss_flags
287 if ( monitor_cache_add( mi, e ) ) {
288 Debug( LDAP_DEBUG_ANY,
289 "monitor_subsys_database_init: "
290 "unable to add entry \"cn=Database %d,%s\"\n",
291 i, ms->mss_ndn.bv_val, 0 );
295 #if defined(LDAP_SLAPI)
296 monitor_back_add_plugin( be, e );
297 #endif /* defined(LDAP_SLAPI) */
303 monitor_cache_release( mi, e_database );
310 * cur: must not contain the tags corresponding to the values in v
311 * delta: will contain the tags corresponding to the values in v
314 value_mask( BerVarray v, slap_mask_t cur, slap_mask_t *delta )
316 for ( ; !BER_BVISNULL( v ); v++ ) {
317 struct restricted_ops_t *rops;
320 if ( OID_LEADCHAR( v->bv_val[ 0 ] ) ) {
321 rops = restricted_exops;
324 rops = restricted_ops;
327 for ( i = 0; !BER_BVISNULL( &rops[ i ].op ); i++ ) {
328 if ( ber_bvstrcasecmp( v, &rops[ i ].op ) != 0 ) {
332 if ( rops[ i ].tag & *delta ) {
336 if ( rops[ i ].tag & cur ) {
340 cur |= rops[ i ].tag;
341 *delta |= rops[ i ].tag;
346 if ( BER_BVISNULL( &rops[ i ].op ) ) {
347 return LDAP_INVALID_SYNTAX;
355 monitor_subsys_database_modify(
360 monitor_info_t *mi = (monitor_info_t *)op->o_bd->be_private;
362 Attribute *save_attrs, *a;
363 Modifications *modlist = op->oq_modify.rs_modlist;
366 int ro_gotval = 1, i, n;
367 slap_mask_t rp_add = 0, rp_delete = 0, rp_cur;
370 i = sscanf( e->e_nname.bv_val, "cn=database %d,", &n );
372 return LDAP_UNWILLING_TO_PERFORM;
374 if ( n < 0 || n >= nBackendDB )
375 return LDAP_NO_SUCH_OBJECT;
377 /* do not allow some changes on back-monitor (needs work)... */
378 be = &backendDB[ n ];
379 if ( SLAP_MONITOR( be ) )
380 return LDAP_UNWILLING_TO_PERFORM;
382 rp_cur = be->be_restrictops;
384 save_attrs = e->e_attrs;
385 e->e_attrs = attrs_dup( e->e_attrs );
387 for ( ml=modlist; ml; ml=ml->sml_next ) {
388 Modification *mod = &ml->sml_mod;
390 if ( mod->sm_desc == mi->mi_ad_readOnly ) {
393 if ( mod->sm_values ) {
394 if ( !BER_BVISNULL( &mod->sm_values[ 1 ] ) ) {
395 rc = LDAP_CONSTRAINT_VIOLATION;
399 if ( bvmatch( &slap_true_bv, mod->sm_values )) {
402 } else if ( bvmatch( &slap_false_bv, mod->sm_values )) {
406 rc = LDAP_INVALID_SYNTAX;
411 switch ( mod->sm_op ) {
412 case LDAP_MOD_DELETE:
413 if ( ro_gotval < 1 ) {
414 rc = LDAP_CONSTRAINT_VIOLATION;
419 if ( val == 0 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) {
420 rc = LDAP_NO_SUCH_ATTRIBUTE;
424 if ( val == 1 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) != SLAP_RESTRICT_OP_WRITES ) {
425 rc = LDAP_NO_SUCH_ATTRIBUTE;
431 case LDAP_MOD_REPLACE:
436 if ( ro_gotval > 0 ) {
437 rc = LDAP_CONSTRAINT_VIOLATION;
443 rp_add |= (~rp_cur) & SLAP_RESTRICT_OP_WRITES;
444 rp_cur |= SLAP_RESTRICT_OP_WRITES;
445 rp_delete &= ~SLAP_RESTRICT_OP_WRITES;
447 } else if ( val == 0 ) {
448 rp_delete |= rp_cur & SLAP_RESTRICT_OP_WRITES;
449 rp_cur &= ~SLAP_RESTRICT_OP_WRITES;
450 rp_add &= ~SLAP_RESTRICT_OP_WRITES;
459 } else if ( mod->sm_desc == mi->mi_ad_restrictedOperation ) {
460 slap_mask_t mask = 0;
462 switch ( mod->sm_op ) {
463 case LDAP_MOD_DELETE:
464 if ( mod->sm_values == NULL ) {
470 rc = value_mask( mod->sm_values, ~rp_cur, &mask );
471 if ( rc == LDAP_SUCCESS ) {
476 } else if ( rc == LDAP_OTHER ) {
477 rc = LDAP_NO_SUCH_ATTRIBUTE;
481 case LDAP_MOD_REPLACE:
488 rc = value_mask( mod->sm_values, rp_cur, &mask );
489 if ( rc == LDAP_SUCCESS ) {
494 } else if ( rc == LDAP_OTHER ) {
495 rc = LDAP_TYPE_OR_VALUE_EXISTS;
504 if ( rc != LDAP_SUCCESS ) {
508 } else if ( is_at_operational( mod->sm_desc->ad_type )) {
509 /* accept all operational attributes */
510 attr_delete( &e->e_attrs, mod->sm_desc );
511 rc = attr_merge( e, mod->sm_desc, mod->sm_values,
519 rc = LDAP_UNWILLING_TO_PERFORM;
525 if ( ro_gotval < 1 ) {
526 rc = LDAP_CONSTRAINT_VIOLATION;
530 if ( ( rp_cur & SLAP_RESTRICT_OP_EXTENDED ) && ( rp_cur & SLAP_RESTRICT_EXOP_MASK ) ) {
531 rc = LDAP_CONSTRAINT_VIOLATION;
535 if ( rp_delete & rp_add ) {
540 /* check current value of readOnly */
541 if ( ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) {
542 tf = (struct berval *)&slap_true_bv;
545 tf = (struct berval *)&slap_false_bv;
548 a = attr_find( e->e_attrs, mi->mi_ad_readOnly );
554 if ( !bvmatch( &a->a_vals[ 0 ], tf ) ) {
555 attr_delete( &e->e_attrs, mi->mi_ad_readOnly );
556 rc = attr_merge_one( e, mi->mi_ad_readOnly, tf, NULL );
559 if ( rc == LDAP_SUCCESS ) {
561 if ( rp_delete == be->be_restrictops ) {
562 attr_delete( &e->e_attrs, mi->mi_ad_restrictedOperation );
565 a = attr_find( e->e_attrs, mi->mi_ad_restrictedOperation );
571 for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) {
572 if ( rp_delete & restricted_ops[ i ].tag ) {
575 for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) {
578 if ( !bvmatch( &a->a_nvals[ j ], &restricted_ops[ i ].op ) ) {
582 ch_free( a->a_vals[ j ].bv_val );
583 ch_free( a->a_nvals[ j ].bv_val );
585 for ( k = j + 1; !BER_BVISNULL( &a->a_nvals[ k ] ); k++ ) {
586 a->a_vals[ k - 1 ] = a->a_vals[ k ];
587 a->a_nvals[ k - 1 ] = a->a_nvals[ k ];
590 BER_BVZERO( &a->a_vals[ k - 1 ] );
591 BER_BVZERO( &a->a_nvals[ k - 1 ] );
596 for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) {
597 if ( rp_delete & restricted_exops[ i ].tag ) {
600 for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) {
603 if ( !bvmatch( &a->a_nvals[ j ], &restricted_exops[ i ].op ) ) {
607 ch_free( a->a_vals[ j ].bv_val );
608 ch_free( a->a_nvals[ j ].bv_val );
610 for ( k = j + 1; !BER_BVISNULL( &a->a_nvals[ k ] ); k++ ) {
611 a->a_vals[ k - 1 ] = a->a_vals[ k ];
612 a->a_nvals[ k - 1 ] = a->a_nvals[ k ];
615 BER_BVZERO( &a->a_vals[ k - 1 ] );
616 BER_BVZERO( &a->a_nvals[ k - 1 ] );
624 for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) {
625 if ( rp_add & restricted_ops[ i ].tag ) {
626 attr_merge_one( e, mi->mi_ad_restrictedOperation,
627 &restricted_ops[ i ].op, NULL );
631 for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) {
632 if ( rp_add & restricted_exops[ i ].tag ) {
633 attr_merge_one( e, mi->mi_ad_restrictedOperation,
634 &restricted_exops[ i ].op, NULL );
640 be->be_restrictops = rp_cur;
643 if ( rc == LDAP_SUCCESS ) {
644 attrs_free( save_attrs );
647 Attribute *tmp = e->e_attrs;
648 e->e_attrs = save_attrs;
654 #if defined(LDAP_SLAPI)
656 monitor_back_add_plugin( Backend *be, Entry *e_database )
658 Slapi_PBlock *pCurrentPB;
659 int i, rc = LDAP_SUCCESS;
660 monitor_info_t *mi = ( monitor_info_t * )be->be_private;
662 if ( slapi_int_pblock_get_first( be, &pCurrentPB ) != LDAP_SUCCESS ) {
664 * LDAP_OTHER is returned if no plugins are installed
672 Slapi_PluginDesc *srchdesc;
673 char buf[ BACKMONITOR_BUFSIZE ];
676 rc = slapi_pblock_get( pCurrentPB, SLAPI_PLUGIN_DESCRIPTION,
678 if ( rc != LDAP_SUCCESS ) {
682 snprintf( buf, sizeof(buf),
683 "plugin %d name: %s; "
689 srchdesc->spd_vendor,
690 srchdesc->spd_version,
691 srchdesc->spd_description );
694 bv.bv_len = strlen( buf );
695 attr_merge_normalize_one( e_database,
696 mi->mi_ad_monitoredInfo, &bv, NULL );
700 } while ( ( slapi_int_pblock_get_next( &pCurrentPB ) == LDAP_SUCCESS )
701 && ( pCurrentPB != NULL ) );
706 #endif /* defined(LDAP_SLAPI) */