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, NULL );
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, 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(
102 struct monitorinfo *mi;
103 Entry *e_database, **ep;
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 Debug( LDAP_DEBUG_ANY,
116 "monitor_subsys_database_init: "
117 "unable to get entry \"%s\"\n",
118 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0, 0 );
122 (void)init_readOnly( mi, e_database, frontendDB->be_restrictops );
123 (void)init_restrictedOperation( mi, e_database, frontendDB->be_restrictops );
125 mp = ( struct monitorentrypriv * )e_database->e_private;
126 mp->mp_children = NULL;
127 ep = &mp->mp_children;
129 for ( i = 0; i < nBackendDB; i++ ) {
130 char buf[ BACKMONITOR_BUFSIZE ];
132 slap_overinfo *oi = NULL;
140 if ( strcmp( be->bd_info->bi_type, "over" ) == 0 ) {
141 oi = (slap_overinfo *)be->bd_info->bi_private;
145 /* Subordinates are not exposed as their own naming context */
146 if ( SLAP_GLUE_SUBORDINATE( be ) ) {
150 snprintf( buf, sizeof( buf ),
151 "dn: cn=Database %d,%s\n"
153 "structuralObjectClass: %s\n"
155 "description: This object contains the type of the database.\n"
158 "modifiersName: %s\n"
159 "createTimestamp: %s\n"
160 "modifyTimestamp: %s\n",
162 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_dn.bv_val,
163 mi->mi_oc_monitoredObject->soc_cname.bv_val,
164 mi->mi_oc_monitoredObject->soc_cname.bv_val,
166 mi->mi_ad_monitoredInfo->ad_cname.bv_val,
168 mi->mi_creatorsName.bv_val,
169 mi->mi_creatorsName.bv_val,
170 mi->mi_startTime.bv_val,
171 mi->mi_startTime.bv_val );
173 e = str2entry( buf );
175 Debug( LDAP_DEBUG_ANY,
176 "monitor_subsys_database_init: "
177 "unable to create entry \"cn=Database %d,%s\"\n",
178 i, monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0 );
182 if ( SLAP_MONITOR(be) ) {
183 attr_merge( e, slap_schema.si_ad_monitorContext,
184 be->be_suffix, be->be_nsuffix );
185 attr_merge( e_database, slap_schema.si_ad_monitorContext,
186 be->be_suffix, be->be_nsuffix );
188 attr_merge( e, slap_schema.si_ad_namingContexts,
189 be->be_suffix, be->be_nsuffix );
190 attr_merge( e_database, slap_schema.si_ad_namingContexts,
191 be->be_suffix, be->be_nsuffix );
194 (void)init_readOnly( mi, e, be->be_restrictops );
195 (void)init_restrictedOperation( mi, e, be->be_restrictops );
198 slap_overinst *on = oi->oi_list;
200 for ( ; on; on = on->on_next ) {
204 bv.bv_val = on->on_bi.bi_type;
205 bv.bv_len = strlen( bv.bv_val );
206 attr_merge_normalize_one( e, mi->mi_ad_monitorOverlay,
209 for ( on2 = overlay_next( NULL ), j = 0; on2; on2 = overlay_next( on2 ), j++ ) {
210 if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
216 snprintf( buf, sizeof( buf ),
218 j, monitor_subsys[SLAPD_MONITOR_OVERLAY].mss_dn.bv_val );
220 bv.bv_len = strlen( buf );
221 attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
226 #if defined(SLAPD_LDAP)
227 if ( strcmp( bi->bi_type, "ldap" ) == 0 ) {
228 struct ldapinfo *li = (struct ldapinfo *)be->be_private;
232 bv.bv_len = strlen( bv.bv_val );
233 attr_merge_normalize_one( e, mi->mi_ad_labeledURI,
236 #endif /* defined(SLAPD_LDAP) */
238 for ( j = 0; j < nBackendInfo; j++ ) {
239 if ( backendInfo[ j ].bi_type == bi->bi_type ) {
242 snprintf( buf, sizeof( buf ),
244 j, monitor_subsys[SLAPD_MONITOR_BACKEND].mss_dn.bv_val );
246 bv.bv_len = strlen( buf );
247 attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
252 /* we must find it! */
255 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
256 e->e_private = ( void * )mp;
258 mp->mp_children = NULL;
259 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_DATABASE];
260 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_DATABASE].mss_flags
263 if ( monitor_cache_add( mi, e ) ) {
264 Debug( LDAP_DEBUG_ANY,
265 "monitor_subsys_database_init: "
266 "unable to add entry \"cn=Database %d,%s\"\n",
267 i, monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0 );
271 #if defined(LDAP_SLAPI)
272 monitor_back_add_plugin( be, e );
273 #endif /* defined(LDAP_SLAPI) */
279 monitor_cache_release( mi, e_database );
286 * cur: must not contain the tags corresponding to the values in v
287 * delta: will contain the tags corresponding to the values in v
290 value_mask( BerVarray v, slap_mask_t cur, slap_mask_t *delta )
292 for ( ; !BER_BVISNULL( v ); v++ ) {
293 struct restricted_ops_t *rops;
296 if ( OID_LEADCHAR( v->bv_val[ 0 ] ) ) {
297 rops = restricted_exops;
300 rops = restricted_ops;
303 for ( i = 0; !BER_BVISNULL( &rops[ i ].op ); i++ ) {
304 if ( ber_bvstrcasecmp( v, &rops[ i ].op ) != 0 ) {
308 if ( rops[ i ].tag & *delta ) {
312 if ( rops[ i ].tag & cur ) {
316 cur |= rops[ i ].tag;
317 *delta |= rops[ i ].tag;
322 if ( BER_BVISNULL( &rops[ i ].op ) ) {
323 return LDAP_INVALID_SYNTAX;
331 monitor_subsys_database_modify(
336 struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
338 Attribute *save_attrs, *a;
339 Modifications *modlist = op->oq_modify.rs_modlist;
342 int ro_gotval = 1, i, n;
343 slap_mask_t rp_add = 0, rp_delete = 0, rp_cur;
346 i = sscanf( e->e_nname.bv_val, "cn=database %d,", &n );
348 return LDAP_UNWILLING_TO_PERFORM;
350 if ( n < 0 || n >= nBackendDB )
351 return LDAP_NO_SUCH_OBJECT;
353 /* do not allow some changes on back-monitor (needs work)... */
355 if ( SLAP_MONITOR( be ) )
356 return LDAP_UNWILLING_TO_PERFORM;
358 rp_cur = be->be_restrictops;
360 save_attrs = e->e_attrs;
361 e->e_attrs = attrs_dup( e->e_attrs );
363 for ( ml=modlist; ml; ml=ml->sml_next ) {
364 Modification *mod = &ml->sml_mod;
366 if ( mod->sm_desc == mi->mi_ad_readOnly ) {
369 if ( mod->sm_values ) {
370 if ( !BER_BVISNULL( &mod->sm_values[ 1 ] ) ) {
371 rc = LDAP_CONSTRAINT_VIOLATION;
375 if ( bvmatch( &slap_true_bv, mod->sm_values )) {
378 } else if ( bvmatch( &slap_false_bv, mod->sm_values )) {
382 rc = LDAP_INVALID_SYNTAX;
387 switch ( mod->sm_op ) {
388 case LDAP_MOD_DELETE:
389 if ( ro_gotval < 1 ) {
390 rc = LDAP_CONSTRAINT_VIOLATION;
395 if ( val == 0 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) {
396 rc = LDAP_NO_SUCH_ATTRIBUTE;
400 if ( val == 1 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) != SLAP_RESTRICT_OP_WRITES ) {
401 rc = LDAP_NO_SUCH_ATTRIBUTE;
407 case LDAP_MOD_REPLACE:
412 if ( ro_gotval > 0 ) {
413 rc = LDAP_CONSTRAINT_VIOLATION;
419 rp_add |= (~rp_cur) & SLAP_RESTRICT_OP_WRITES;
420 rp_cur |= SLAP_RESTRICT_OP_WRITES;
421 rp_delete &= ~SLAP_RESTRICT_OP_WRITES;
423 } else if ( val == 0 ) {
424 rp_delete |= rp_cur & SLAP_RESTRICT_OP_WRITES;
425 rp_cur &= ~SLAP_RESTRICT_OP_WRITES;
426 rp_add &= ~SLAP_RESTRICT_OP_WRITES;
435 } else if ( mod->sm_desc == mi->mi_ad_restrictedOperation ) {
436 slap_mask_t mask = 0;
438 switch ( mod->sm_op ) {
439 case LDAP_MOD_DELETE:
440 if ( mod->sm_values == NULL ) {
446 rc = value_mask( mod->sm_values, ~rp_cur, &mask );
447 if ( rc == LDAP_SUCCESS ) {
452 } else if ( rc == LDAP_OTHER ) {
453 rc = LDAP_NO_SUCH_ATTRIBUTE;
457 case LDAP_MOD_REPLACE:
464 rc = value_mask( mod->sm_values, rp_cur, &mask );
465 if ( rc == LDAP_SUCCESS ) {
470 } else if ( rc == LDAP_OTHER ) {
471 rc = LDAP_TYPE_OR_VALUE_EXISTS;
480 if ( rc != LDAP_SUCCESS ) {
484 } else if ( is_at_operational( mod->sm_desc->ad_type )) {
485 /* accept all operational attributes */
486 attr_delete( &e->e_attrs, mod->sm_desc );
487 rc = attr_merge( e, mod->sm_desc, mod->sm_values,
495 rc = LDAP_UNWILLING_TO_PERFORM;
501 if ( ro_gotval < 1 ) {
502 rc = LDAP_CONSTRAINT_VIOLATION;
506 if ( ( rp_cur & SLAP_RESTRICT_OP_EXTENDED ) && ( rp_cur & SLAP_RESTRICT_EXOP_MASK ) ) {
507 rc = LDAP_CONSTRAINT_VIOLATION;
511 if ( rp_delete & rp_add ) {
516 /* check current value of readOnly */
517 if ( ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) {
518 tf = (struct berval *)&slap_true_bv;
521 tf = (struct berval *)&slap_false_bv;
524 a = attr_find( e->e_attrs, mi->mi_ad_readOnly );
530 if ( !bvmatch( &a->a_vals[0], tf ) ) {
531 attr_delete( &e->e_attrs, mi->mi_ad_readOnly );
532 rc = attr_merge_one( e, mi->mi_ad_readOnly, tf, NULL );
535 if ( rc == LDAP_SUCCESS ) {
537 if ( rp_delete == be->be_restrictops ) {
538 attr_delete( &e->e_attrs, mi->mi_ad_restrictedOperation );
541 a = attr_find( e->e_attrs, mi->mi_ad_restrictedOperation );
547 for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) {
548 if ( rp_delete & restricted_ops[ i ].tag ) {
551 for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) {
554 if ( !bvmatch( &a->a_nvals[ j ], &restricted_ops[ i ].op ) ) {
558 ch_free( a->a_vals[ j ].bv_val );
559 ch_free( a->a_nvals[ j ].bv_val );
561 for ( k = j + 1; !BER_BVISNULL( &a->a_nvals[ k ] ); k++ ) {
562 a->a_vals[ k - 1 ] = a->a_vals[ k ];
563 a->a_nvals[ k - 1 ] = a->a_nvals[ k ];
566 BER_BVZERO( &a->a_vals[ k - 1 ] );
567 BER_BVZERO( &a->a_nvals[ k - 1 ] );
572 for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) {
573 if ( rp_delete & restricted_exops[ i ].tag ) {
576 for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) {
579 if ( !bvmatch( &a->a_nvals[ j ], &restricted_exops[ i ].op ) ) {
583 ch_free( a->a_vals[ j ].bv_val );
584 ch_free( a->a_nvals[ j ].bv_val );
586 for ( k = j + 1; !BER_BVISNULL( &a->a_nvals[ k ] ); k++ ) {
587 a->a_vals[ k - 1 ] = a->a_vals[ k ];
588 a->a_nvals[ k - 1 ] = a->a_nvals[ k ];
591 BER_BVZERO( &a->a_vals[ k - 1 ] );
592 BER_BVZERO( &a->a_nvals[ k - 1 ] );
600 for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) {
601 if ( rp_add & restricted_ops[ i ].tag ) {
602 attr_merge_one( e, mi->mi_ad_restrictedOperation,
603 &restricted_ops[ i ].op, NULL );
607 for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) {
608 if ( rp_add & restricted_exops[ i ].tag ) {
609 attr_merge_one( e, mi->mi_ad_restrictedOperation,
610 &restricted_exops[ i ].op, NULL );
616 be->be_restrictops = rp_cur;
619 if ( rc == LDAP_SUCCESS ) {
620 attrs_free( save_attrs );
623 Attribute *tmp = e->e_attrs;
624 e->e_attrs = save_attrs;
630 #if defined(LDAP_SLAPI)
632 monitor_back_add_plugin( Backend *be, Entry *e_database )
634 Slapi_PBlock *pCurrentPB;
635 int i, rc = LDAP_SUCCESS;
636 struct monitorinfo *mi = ( struct monitorinfo * )be->be_private;
638 if ( slapi_int_pblock_get_first( be, &pCurrentPB ) != LDAP_SUCCESS ) {
640 * LDAP_OTHER is returned if no plugins are installed
648 Slapi_PluginDesc *srchdesc;
649 char buf[ BACKMONITOR_BUFSIZE ];
652 rc = slapi_pblock_get( pCurrentPB, SLAPI_PLUGIN_DESCRIPTION,
654 if ( rc != LDAP_SUCCESS ) {
658 snprintf( buf, sizeof(buf),
659 "plugin %d name: %s; "
665 srchdesc->spd_vendor,
666 srchdesc->spd_version,
667 srchdesc->spd_description );
670 bv.bv_len = strlen( buf );
671 attr_merge_normalize_one( e_database,
672 mi->mi_ad_monitoredInfo, &bv, NULL );
676 } while ( ( slapi_int_pblock_get_next( &pCurrentPB ) == LDAP_SUCCESS )
677 && ( pCurrentPB != NULL ) );
682 #endif /* defined(LDAP_SLAPI) */