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( struct monitorinfo *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, tf );
70 init_restrictedOperation( struct monitorinfo *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, &restricted_ops[ i ].op );
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, &restricted_exops[ i ].op );
98 monitor_subsys_database_init(
102 struct monitorinfo *mi;
103 Entry *e, *e_database, *e_tmp;
105 struct monitorentrypriv *mp;
107 assert( be != NULL );
109 mi = ( struct monitorinfo * )be->be_private;
111 if ( monitor_cache_get( mi,
112 &monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn,
115 LDAP_LOG( OPERATION, CRIT,
116 "monitor_subsys_database_init: "
117 "unable to get entry '%s'\n",
118 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0, 0 );
120 Debug( LDAP_DEBUG_ANY,
121 "monitor_subsys_database_init: "
122 "unable to get entry '%s'\n%s%s",
123 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val,
129 (void)init_readOnly( mi, e_database, global_restrictops );
130 (void)init_restrictedOperation( mi, e_database, global_restrictops );
133 for ( i = nBackendDB; i--; ) {
134 char buf[ BACKMONITOR_BUFSIZE ];
136 slap_overinfo *oi = NULL;
143 if ( strcmp( be->bd_info->bi_type, "over" ) == 0 ) {
144 oi = (slap_overinfo *)be->bd_info->bi_private;
148 /* Subordinates are not exposed as their own naming context */
149 if ( SLAP_GLUE_SUBORDINATE( be ) ) {
153 snprintf( buf, sizeof( buf ),
154 "dn: cn=Database %d,%s\n"
156 "structuralObjectClass: %s\n"
158 "description: This object contains the type of the database.\n"
161 "modifiersName: %s\n"
162 "createTimestamp: %s\n"
163 "modifyTimestamp: %s\n",
165 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_dn.bv_val,
166 mi->mi_oc_monitoredObject->soc_cname.bv_val,
167 mi->mi_oc_monitoredObject->soc_cname.bv_val,
169 mi->mi_ad_monitoredInfo->ad_cname.bv_val,
171 mi->mi_creatorsName.bv_val,
172 mi->mi_creatorsName.bv_val,
173 mi->mi_startTime.bv_val,
174 mi->mi_startTime.bv_val );
176 e = str2entry( buf );
179 LDAP_LOG( OPERATION, CRIT,
180 "monitor_subsys_database_init: "
181 "unable to create entry 'cn=Database %d,%s'\n",
182 i, monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0 );
184 Debug( LDAP_DEBUG_ANY,
185 "monitor_subsys_database_init: "
186 "unable to create entry 'cn=Database %d,%s'\n%s",
188 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val,
194 if ( SLAP_MONITOR(be) ) {
195 attr_merge( e, slap_schema.si_ad_monitorContext,
196 be->be_suffix, be->be_nsuffix );
197 attr_merge( e_database, slap_schema.si_ad_monitorContext,
198 be->be_suffix, be->be_nsuffix );
200 attr_merge( e, slap_schema.si_ad_namingContexts,
201 be->be_suffix, be->be_nsuffix );
202 attr_merge( e_database, slap_schema.si_ad_namingContexts,
203 be->be_suffix, be->be_nsuffix );
206 (void)init_readOnly( mi, e, be->be_restrictops );
207 (void)init_restrictedOperation( mi, e, be->be_restrictops );
210 slap_overinst *on = oi->oi_list;
212 for ( ; on; on = on->on_next ) {
216 bv.bv_val = on->on_bi.bi_type;
217 bv.bv_len = strlen( bv.bv_val );
218 attr_merge_normalize_one( e, mi->mi_ad_monitorOverlay,
221 for ( on2 = overlay_next( NULL ), j = 0; on2; on2 = overlay_next( on2 ), j++ ) {
222 if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
228 snprintf( buf, sizeof( buf ),
230 j, monitor_subsys[SLAPD_MONITOR_OVERLAY].mss_dn.bv_val );
232 bv.bv_len = strlen( buf );
233 attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
238 #if defined(SLAPD_LDAP)
239 if ( strcmp( bi->bi_type, "ldap" ) == 0 ) {
240 struct ldapinfo *li = (struct ldapinfo *)be->be_private;
244 bv.bv_len = strlen( bv.bv_val );
245 attr_merge_normalize_one( e, mi->mi_ad_labeledURI,
248 #endif /* defined(SLAPD_LDAP) */
250 for ( j = nBackendInfo; j--; ) {
251 if ( backendInfo[ j ].bi_type == bi->bi_type ) {
254 snprintf( buf, sizeof( buf ),
256 j, monitor_subsys[SLAPD_MONITOR_BACKEND].mss_dn.bv_val );
258 bv.bv_len = strlen( buf );
259 attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
264 /* we must find it! */
267 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
268 e->e_private = ( void * )mp;
270 mp->mp_children = NULL;
271 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_DATABASE];
272 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_DATABASE].mss_flags
275 if ( monitor_cache_add( mi, e ) ) {
277 LDAP_LOG( OPERATION, CRIT,
278 "monitor_subsys_database_init: "
279 "unable to add entry 'cn=Database %d,%s'\n",
280 i, monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0 );
282 Debug( LDAP_DEBUG_ANY,
283 "monitor_subsys_database_init: "
284 "unable to add entry 'cn=Database %d,%s'\n",
286 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val,
292 #if defined(LDAP_SLAPI)
293 monitor_back_add_plugin( be, e );
294 #endif /* defined(LDAP_SLAPI) */
299 mp = ( struct monitorentrypriv * )e_database->e_private;
300 mp->mp_children = e_tmp;
302 monitor_cache_release( mi, e_database );
309 * cur: must not contain the tags corresponding to the values in v
310 * delta: will contain the tags corresponding to the values in v
313 value_mask( BerVarray v, slap_mask_t cur, slap_mask_t *delta )
315 for ( ; !BER_BVISNULL( v ); v++ ) {
316 struct restricted_ops_t *rops;
319 if ( OID_LEADCHAR( v->bv_val[ 0 ] ) ) {
320 rops = restricted_exops;
323 rops = restricted_ops;
326 for ( i = 0; !BER_BVISNULL( &rops[ i ].op ); i++ ) {
327 if ( ber_bvstrcasecmp( v, &rops[ i ].op ) != 0 ) {
331 if ( rops[ i ].tag & *delta ) {
335 if ( rops[ i ].tag & cur ) {
339 cur |= rops[ i ].tag;
340 *delta |= rops[ i ].tag;
345 if ( BER_BVISNULL( &rops[ i ].op ) ) {
346 return LDAP_INVALID_SYNTAX;
354 monitor_subsys_database_modify(
359 struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
361 Attribute *save_attrs, *a;
362 Modifications *modlist = op->oq_modify.rs_modlist;
365 int ro_gotval = 1, i, n;
366 slap_mask_t rp_add = 0, rp_delete = 0, rp_cur;
369 i = sscanf( e->e_nname.bv_val, "cn=database %d,", &n );
371 return LDAP_UNWILLING_TO_PERFORM;
373 if ( n < 0 || n >= nBackendDB )
374 return LDAP_NO_SUCH_OBJECT;
376 /* do not allow some changes on back-monitor (needs work)... */
378 if ( SLAP_MONITOR( be ) )
379 return LDAP_UNWILLING_TO_PERFORM;
381 rp_cur = be->be_restrictops;
383 save_attrs = e->e_attrs;
384 e->e_attrs = attrs_dup( e->e_attrs );
386 for ( ml=modlist; ml; ml=ml->sml_next ) {
387 Modification *mod = &ml->sml_mod;
389 if ( mod->sm_desc == mi->mi_ad_readOnly ) {
392 if ( mod->sm_values ) {
393 if ( !BER_BVISNULL( &mod->sm_values[ 1 ] ) ) {
394 rc = LDAP_CONSTRAINT_VIOLATION;
398 if ( bvmatch( &slap_true_bv, mod->sm_values )) {
401 } else if ( bvmatch( &slap_false_bv, mod->sm_values )) {
405 rc = LDAP_INVALID_SYNTAX;
410 switch ( mod->sm_op ) {
411 case LDAP_MOD_DELETE:
412 if ( ro_gotval < 1 ) {
413 rc = LDAP_CONSTRAINT_VIOLATION;
418 if ( val == 0 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) {
419 rc = LDAP_NO_SUCH_ATTRIBUTE;
423 if ( val == 1 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) != SLAP_RESTRICT_OP_WRITES ) {
424 rc = LDAP_NO_SUCH_ATTRIBUTE;
430 case LDAP_MOD_REPLACE:
435 if ( ro_gotval > 0 ) {
436 rc = LDAP_CONSTRAINT_VIOLATION;
442 rp_add |= (~rp_cur) & SLAP_RESTRICT_OP_WRITES;
443 rp_cur |= SLAP_RESTRICT_OP_WRITES;
444 rp_delete &= ~SLAP_RESTRICT_OP_WRITES;
446 } else if ( val == 0 ) {
447 rp_delete |= rp_cur & SLAP_RESTRICT_OP_WRITES;
448 rp_cur &= ~SLAP_RESTRICT_OP_WRITES;
449 rp_add &= ~SLAP_RESTRICT_OP_WRITES;
458 } else if ( mod->sm_desc == mi->mi_ad_restrictedOperation ) {
459 slap_mask_t mask = 0;
461 switch ( mod->sm_op ) {
462 case LDAP_MOD_DELETE:
463 if ( mod->sm_values == NULL ) {
469 rc = value_mask( mod->sm_values, ~rp_cur, &mask );
470 if ( rc == LDAP_SUCCESS ) {
475 } else if ( rc == LDAP_OTHER ) {
476 rc = LDAP_NO_SUCH_ATTRIBUTE;
480 case LDAP_MOD_REPLACE:
487 rc = value_mask( mod->sm_values, rp_cur, &mask );
488 if ( rc == LDAP_SUCCESS ) {
493 } else if ( rc == LDAP_OTHER ) {
494 rc = LDAP_TYPE_OR_VALUE_EXISTS;
503 if ( rc != LDAP_SUCCESS ) {
507 } else if ( is_at_operational( mod->sm_desc->ad_type )) {
508 /* accept all operational attributes */
509 attr_delete( &e->e_attrs, mod->sm_desc );
510 rc = attr_merge( e, mod->sm_desc, mod->sm_values,
518 rc = LDAP_UNWILLING_TO_PERFORM;
524 if ( ro_gotval < 1 ) {
525 rc = LDAP_CONSTRAINT_VIOLATION;
529 if ( ( rp_cur & SLAP_RESTRICT_OP_EXTENDED ) && ( rp_cur & SLAP_RESTRICT_EXOP_MASK ) ) {
530 rc = LDAP_CONSTRAINT_VIOLATION;
534 if ( rp_delete & rp_add ) {
539 /* check current value of readOnly */
540 if ( ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) {
541 tf = (struct berval *)&slap_true_bv;
544 tf = (struct berval *)&slap_false_bv;
547 a = attr_find( e->e_attrs, mi->mi_ad_readOnly );
553 if ( !bvmatch( &a->a_vals[0], tf ) ) {
554 attr_delete( &e->e_attrs, mi->mi_ad_readOnly );
555 rc = attr_merge_one( e, mi->mi_ad_readOnly, tf, tf );
558 if ( rc == LDAP_SUCCESS ) {
560 if ( rp_delete == be->be_restrictops ) {
561 attr_delete( &e->e_attrs, mi->mi_ad_restrictedOperation );
564 a = attr_find( e->e_attrs, mi->mi_ad_restrictedOperation );
570 for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) {
571 if ( rp_delete & restricted_ops[ i ].tag ) {
574 for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) {
577 if ( !bvmatch( &a->a_nvals[ j ], &restricted_ops[ i ].op ) ) {
581 ch_free( a->a_vals[ j ].bv_val );
582 ch_free( a->a_nvals[ j ].bv_val );
584 for ( k = j + 1; !BER_BVISNULL( &a->a_nvals[ k ] ); k++ ) {
585 a->a_vals[ k - 1 ] = a->a_vals[ k ];
586 a->a_nvals[ k - 1 ] = a->a_nvals[ k ];
589 BER_BVZERO( &a->a_vals[ k - 1 ] );
590 BER_BVZERO( &a->a_nvals[ k - 1 ] );
595 for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) {
596 if ( rp_delete & restricted_exops[ i ].tag ) {
599 for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) {
602 if ( !bvmatch( &a->a_nvals[ j ], &restricted_exops[ i ].op ) ) {
606 ch_free( a->a_vals[ j ].bv_val );
607 ch_free( a->a_nvals[ j ].bv_val );
609 for ( k = j + 1; !BER_BVISNULL( &a->a_nvals[ k ] ); k++ ) {
610 a->a_vals[ k - 1 ] = a->a_vals[ k ];
611 a->a_nvals[ k - 1 ] = a->a_nvals[ k ];
614 BER_BVZERO( &a->a_vals[ k - 1 ] );
615 BER_BVZERO( &a->a_nvals[ k - 1 ] );
623 for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) {
624 if ( rp_add & restricted_ops[ i ].tag ) {
625 attr_merge_one( e, mi->mi_ad_restrictedOperation,
626 &restricted_ops[ i ].op, &restricted_ops[ i ].op );
630 for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) {
631 if ( rp_add & restricted_exops[ i ].tag ) {
632 attr_merge_one( e, mi->mi_ad_restrictedOperation,
633 &restricted_exops[ i ].op, &restricted_exops[ i ].op );
639 be->be_restrictops = rp_cur;
642 if ( rc == LDAP_SUCCESS ) {
643 attrs_free( save_attrs );
646 Attribute *tmp = e->e_attrs;
647 e->e_attrs = save_attrs;
653 #if defined(LDAP_SLAPI)
655 monitor_back_add_plugin( Backend *be, Entry *e_database )
657 Slapi_PBlock *pCurrentPB;
658 int i, rc = LDAP_SUCCESS;
659 struct monitorinfo *mi = ( struct monitorinfo * )be->be_private;
661 if ( slapi_int_pblock_get_first( be, &pCurrentPB ) != LDAP_SUCCESS ) {
663 * LDAP_OTHER is returned if no plugins are installed
671 Slapi_PluginDesc *srchdesc;
672 char buf[ BACKMONITOR_BUFSIZE ];
675 rc = slapi_pblock_get( pCurrentPB, SLAPI_PLUGIN_DESCRIPTION,
677 if ( rc != LDAP_SUCCESS ) {
681 snprintf( buf, sizeof(buf),
682 "plugin %d name: %s; "
688 srchdesc->spd_vendor,
689 srchdesc->spd_version,
690 srchdesc->spd_description );
693 bv.bv_len = strlen( buf );
694 attr_merge_normalize_one( e_database,
695 mi->mi_ad_monitoredInfo, &bv, NULL );
699 } while ( ( slapi_int_pblock_get_next( &pCurrentPB ) == LDAP_SUCCESS )
700 && ( pCurrentPB != NULL ) );
705 #endif /* defined(LDAP_SLAPI) */