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 restrict_ops_t {
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 }, restrict_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; restrict_ops[ i ].op.bv_val; i++ ) {
75 if ( restrictops & restrict_ops[ i ].tag ) {
76 rc = attr_merge_one( e, mi->mi_ad_restrictedOperation,
77 &restrict_ops[ i ].op, &restrict_ops[ i ].op );
84 for ( i = 0; restrict_exops[ i ].op.bv_val; i++ ) {
85 if ( restrictops & restrict_exops[ i ].tag ) {
86 rc = attr_merge_one( e, mi->mi_ad_restrictedOperation,
87 &restrict_exops[ i ].op, &restrict_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;
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"
160 "createTimestamp: %s\n"
161 "modifyTimestamp: %s\n",
163 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_dn.bv_val,
164 mi->mi_oc_monitoredObject->soc_cname.bv_val,
165 mi->mi_oc_monitoredObject->soc_cname.bv_val,
167 mi->mi_ad_monitoredInfo->ad_cname.bv_val,
169 mi->mi_startTime.bv_val,
170 mi->mi_startTime.bv_val );
172 e = str2entry( buf );
175 LDAP_LOG( OPERATION, CRIT,
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 );
180 Debug( LDAP_DEBUG_ANY,
181 "monitor_subsys_database_init: "
182 "unable to create entry 'cn=Database %d,%s'\n%s",
184 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val,
190 if ( SLAP_MONITOR(be) ) {
191 attr_merge( e, slap_schema.si_ad_monitorContext,
192 be->be_suffix, be->be_nsuffix );
193 attr_merge( e_database, slap_schema.si_ad_monitorContext,
194 be->be_suffix, be->be_nsuffix );
196 attr_merge( e, slap_schema.si_ad_namingContexts,
197 be->be_suffix, be->be_nsuffix );
198 attr_merge( e_database, slap_schema.si_ad_namingContexts,
199 be->be_suffix, be->be_nsuffix );
202 (void)init_readOnly( mi, e, be->be_restrictops );
203 (void)init_restrictedOperation( mi, e, be->be_restrictops );
206 slap_overinst *on = oi->oi_list;
208 for ( ; on; on = on->on_next ) {
212 bv.bv_val = on->on_bi.bi_type;
213 bv.bv_len = strlen( bv.bv_val );
214 attr_merge_normalize_one( e, mi->mi_ad_monitorOverlay,
217 for ( on2 = overlay_next( NULL ), j = 0; on2; on2 = overlay_next( on2 ), j++ ) {
218 if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
224 snprintf( buf, sizeof( buf ),
226 j, monitor_subsys[SLAPD_MONITOR_OVERLAY].mss_dn.bv_val );
228 bv.bv_len = strlen( buf );
229 attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
234 #if defined(SLAPD_LDAP)
235 if ( strcmp( bi->bi_type, "ldap" ) == 0 ) {
236 struct ldapinfo *li = (struct ldapinfo *)be->be_private;
240 bv.bv_len = strlen( bv.bv_val );
241 attr_merge_normalize_one( e, mi->mi_ad_labeledURI,
244 #endif /* defined(SLAPD_LDAP) */
246 for ( j = nBackendInfo; j--; ) {
247 if ( backendInfo[ j ].bi_type == bi->bi_type ) {
250 snprintf( buf, sizeof( buf ),
252 j, monitor_subsys[SLAPD_MONITOR_BACKEND].mss_dn.bv_val );
254 bv.bv_len = strlen( buf );
255 attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
260 /* we must find it! */
263 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
264 e->e_private = ( void * )mp;
266 mp->mp_children = NULL;
267 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_DATABASE];
268 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_DATABASE].mss_flags
271 if ( monitor_cache_add( mi, e ) ) {
273 LDAP_LOG( OPERATION, CRIT,
274 "monitor_subsys_database_init: "
275 "unable to add entry 'cn=Database %d,%s'\n",
276 i, monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0 );
278 Debug( LDAP_DEBUG_ANY,
279 "monitor_subsys_database_init: "
280 "unable to add entry 'cn=Database %d,%s'\n",
282 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val,
288 #if defined(LDAP_SLAPI)
289 monitor_back_add_plugin( be, e );
290 #endif /* defined(LDAP_SLAPI) */
295 mp = ( struct monitorentrypriv * )e_database->e_private;
296 mp->mp_children = e_tmp;
298 monitor_cache_release( mi, e_database );
304 monitor_subsys_database_modify(
309 struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
311 Attribute *save_attrs;
312 Modifications *modlist = op->oq_modify.rs_modlist;
315 int gotval = 1, i, n, cur;
317 i = sscanf( e->e_nname.bv_val, "cn=database %d,", &n );
319 return LDAP_UNWILLING_TO_PERFORM;
321 if ( n < 0 || n >= nBackendDB )
322 return LDAP_NO_SUCH_OBJECT;
325 if ( SLAP_MONITOR(be) )
326 return LDAP_UNWILLING_TO_PERFORM;
328 cur = (be->be_restrictops & SLAP_RESTRICT_OP_WRITES) ? 1 : 0;
330 save_attrs = e->e_attrs;
331 e->e_attrs = attrs_dup( e->e_attrs );
333 for ( ml=modlist; ml; ml=ml->sml_next ) {
334 Modification *mod = &ml->sml_mod;
336 if ( mod->sm_desc == mi->mi_ad_readOnly ) {
339 if ( mod->sm_values ) {
341 if ( !BER_BVISNULL(&mod->sm_values[1]) ) {
342 rc = LDAP_CONSTRAINT_VIOLATION;
345 if ( bvmatch( &slap_true_bv, mod->sm_values )) {
347 } else if ( bvmatch( &slap_false_bv, mod->sm_values )) {
351 switch( mod->sm_op ) {
352 case LDAP_MOD_DELETE:
353 if ( val < 0 || val == cur ) {
357 rc = LDAP_NO_SUCH_ATTRIBUTE;
360 case LDAP_MOD_REPLACE:
366 rc = LDAP_INVALID_SYNTAX;
380 } else if ( mod->sm_desc == mi->mi_ad_restrictedOperation ) {
383 } else if ( is_at_operational( mod->sm_desc->ad_type )) {
384 /* accept all operational attributes */
385 attr_delete( &e->e_attrs, mod->sm_desc );
386 rc = attr_merge( e, mod->sm_desc, mod->sm_values,
394 rc = LDAP_UNWILLING_TO_PERFORM;
399 if ( gotval == 1 && cur >= 0 ) {
401 tf = cur ? (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv;
402 attr_delete( &e->e_attrs, mi->mi_ad_readOnly );
403 rc = attr_merge_one( e, mi->mi_ad_readOnly, tf, tf );
404 if ( rc == LDAP_SUCCESS ) {
406 be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
408 be->be_restrictops &= ~SLAP_RESTRICT_OP_WRITES;
414 rc = LDAP_CONSTRAINT_VIOLATION;
416 if ( rc == LDAP_SUCCESS ) {
417 attrs_free( save_attrs );
419 Attribute *tmp = e->e_attrs;
420 e->e_attrs = save_attrs;
426 #if defined(LDAP_SLAPI)
428 monitor_back_add_plugin( Backend *be, Entry *e_database )
430 Slapi_PBlock *pCurrentPB;
431 int i, rc = LDAP_SUCCESS;
432 struct monitorinfo *mi = ( struct monitorinfo * )be->be_private;
434 if ( slapi_int_pblock_get_first( be, &pCurrentPB ) != LDAP_SUCCESS ) {
436 * LDAP_OTHER is returned if no plugins are installed
444 Slapi_PluginDesc *srchdesc;
445 char buf[ BACKMONITOR_BUFSIZE ];
448 rc = slapi_pblock_get( pCurrentPB, SLAPI_PLUGIN_DESCRIPTION,
450 if ( rc != LDAP_SUCCESS ) {
454 snprintf( buf, sizeof(buf),
455 "plugin %d name: %s; "
461 srchdesc->spd_vendor,
462 srchdesc->spd_version,
463 srchdesc->spd_description );
466 bv.bv_len = strlen( buf );
467 attr_merge_normalize_one( e_database,
468 mi->mi_ad_monitoredInfo, &bv, NULL );
472 } while ( ( slapi_int_pblock_get_next( &pCurrentPB ) == LDAP_SUCCESS )
473 && ( pCurrentPB != NULL ) );
478 #endif /* defined(LDAP_SLAPI) */