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) */
40 monitor_subsys_database_init(
44 struct monitorinfo *mi;
45 Entry *e, *e_database, *e_tmp;
47 struct monitorentrypriv *mp;
52 mi = ( struct monitorinfo * )be->be_private;
54 if ( monitor_cache_get( mi,
55 &monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn,
58 LDAP_LOG( OPERATION, CRIT,
59 "monitor_subsys_database_init: "
60 "unable to get entry '%s'\n",
61 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0, 0 );
63 Debug( LDAP_DEBUG_ANY,
64 "monitor_subsys_database_init: "
65 "unable to get entry '%s'\n%s%s",
66 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val,
71 tf = (global_restrictops & SLAP_RESTRICT_OP_WRITES) ?
72 (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv ;
73 attr_merge_one( e_database, mi->mi_ad_readOnly, tf, tf );
76 for ( i = nBackendDB; i--; ) {
77 char buf[ BACKMONITOR_BUFSIZE ];
79 slap_overinfo *oi = NULL;
86 if ( strcmp( be->bd_info->bi_type, "over" ) == 0 ) {
87 oi = (slap_overinfo *)be->bd_info;
91 /* Subordinates are not exposed as their own naming context */
92 if ( SLAP_GLUE_SUBORDINATE( be ) ) {
96 snprintf( buf, sizeof( buf ),
97 "dn: cn=Database %d,%s\n"
99 "structuralObjectClass: %s\n"
101 "description: This object contains the type of the database.\n"
103 "createTimestamp: %s\n"
104 "modifyTimestamp: %s\n",
106 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_dn.bv_val,
107 mi->mi_oc_monitoredObject->soc_cname.bv_val,
108 mi->mi_oc_monitoredObject->soc_cname.bv_val,
110 mi->mi_ad_monitoredInfo->ad_cname.bv_val,
112 mi->mi_startTime.bv_val,
113 mi->mi_startTime.bv_val );
115 e = str2entry( buf );
118 LDAP_LOG( OPERATION, CRIT,
119 "monitor_subsys_database_init: "
120 "unable to create entry 'cn=Database %d,%s'\n",
121 i, monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0 );
123 Debug( LDAP_DEBUG_ANY,
124 "monitor_subsys_database_init: "
125 "unable to create entry 'cn=Database %d,%s'\n%s",
127 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val,
133 if ( SLAP_MONITOR(be) ) {
134 attr_merge( e, slap_schema.si_ad_monitorContext,
135 be->be_suffix, be->be_nsuffix );
136 attr_merge( e_database, slap_schema.si_ad_monitorContext,
137 be->be_suffix, be->be_nsuffix );
139 attr_merge( e, slap_schema.si_ad_namingContexts,
140 be->be_suffix, be->be_nsuffix );
141 attr_merge( e_database, slap_schema.si_ad_namingContexts,
142 be->be_suffix, be->be_nsuffix );
144 tf = (be->be_restrictops & SLAP_RESTRICT_OP_WRITES) ?
145 (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv ;
146 attr_merge_one( e, mi->mi_ad_readOnly, tf, tf );
149 slap_overinst *on = oi->oi_list;
151 for ( ; on; on = on->on_next ) {
155 bv.bv_val = on->on_bi.bi_type;
156 bv.bv_len = strlen( bv.bv_val );
157 attr_merge_normalize_one( e, mi->mi_ad_monitorOverlay,
160 for ( on2 = overlay_next( NULL ), j = 0; on2; on2 = overlay_next( on2 ), j++ ) {
161 if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
167 snprintf( buf, sizeof( buf ),
169 j, monitor_subsys[SLAPD_MONITOR_OVERLAY].mss_dn.bv_val );
171 bv.bv_len = strlen( buf );
172 attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
177 #if defined(SLAPD_LDAP)
178 if ( strcmp( bi->bi_type, "ldap" ) == 0 ) {
179 struct ldapinfo *li = (struct ldapinfo *)be->be_private;
183 bv.bv_len = strlen( bv.bv_val );
184 attr_merge_normalize_one( e, mi->mi_ad_labeledURI,
187 #endif /* defined(SLAPD_LDAP) */
189 for ( j = nBackendInfo; j--; ) {
190 if ( backendInfo[ j ].bi_type == bi->bi_type ) {
193 snprintf( buf, sizeof( buf ),
195 j, monitor_subsys[SLAPD_MONITOR_BACKEND].mss_dn.bv_val );
197 bv.bv_len = strlen( buf );
198 attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
203 /* we must find it! */
206 mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
207 e->e_private = ( void * )mp;
209 mp->mp_children = NULL;
210 mp->mp_info = &monitor_subsys[SLAPD_MONITOR_DATABASE];
211 mp->mp_flags = monitor_subsys[SLAPD_MONITOR_DATABASE].mss_flags
214 if ( monitor_cache_add( mi, e ) ) {
216 LDAP_LOG( OPERATION, CRIT,
217 "monitor_subsys_database_init: "
218 "unable to add entry 'cn=Database %d,%s'\n",
219 i, monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val, 0 );
221 Debug( LDAP_DEBUG_ANY,
222 "monitor_subsys_database_init: "
223 "unable to add entry 'cn=Database %d,%s'\n",
225 monitor_subsys[SLAPD_MONITOR_DATABASE].mss_ndn.bv_val,
231 #if defined(LDAP_SLAPI)
232 monitor_back_add_plugin( be, e );
233 #endif /* defined(LDAP_SLAPI) */
238 mp = ( struct monitorentrypriv * )e_database->e_private;
239 mp->mp_children = e_tmp;
241 monitor_cache_release( mi, e_database );
247 monitor_subsys_database_modify(
252 struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
254 Attribute *save_attrs;
255 Modifications *modlist = op->oq_modify.rs_modlist;
258 int gotval = 1, i, n, cur;
260 i = sscanf( e->e_nname.bv_val, "cn=database %d,", &n );
262 return LDAP_UNWILLING_TO_PERFORM;
264 if ( n < 0 || n >= nBackendDB )
265 return LDAP_NO_SUCH_OBJECT;
268 cur = (be->be_restrictops & SLAP_RESTRICT_OP_WRITES) ? 1 : 0;
270 save_attrs = e->e_attrs;
271 e->e_attrs = attrs_dup( e->e_attrs );
273 for ( ml=modlist; ml; ml=ml->sml_next ) {
274 Modification *mod = &ml->sml_mod;
276 if ( mod->sm_desc == mi->mi_ad_readOnly ) {
279 if ( mod->sm_values ) {
281 if ( !BER_BVISNULL(&mod->sm_values[1]) ) {
282 rc = LDAP_CONSTRAINT_VIOLATION;
285 if ( bvmatch( &slap_true_bv, mod->sm_values )) {
287 } else if ( bvmatch( &slap_false_bv, mod->sm_values )) {
291 switch( mod->sm_op ) {
292 case LDAP_MOD_DELETE:
293 if ( val < 0 || val == cur ) {
297 rc = LDAP_NO_SUCH_ATTRIBUTE;
300 case LDAP_MOD_REPLACE:
306 rc = LDAP_INVALID_SYNTAX;
319 } else if ( is_at_operational( mod->sm_desc->ad_type )) {
320 /* accept all operational attributes */
321 attr_delete( &e->e_attrs, mod->sm_desc );
322 rc = attr_merge( e, mod->sm_desc, mod->sm_values,
329 rc = LDAP_UNWILLING_TO_PERFORM;
333 if ( gotval == 1 && cur >= 0 ) {
335 tf = cur ? (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv;
336 attr_delete( &e->e_attrs, mi->mi_ad_readOnly );
337 rc = attr_merge_one( e, mi->mi_ad_readOnly, tf, tf );
338 if ( rc == LDAP_SUCCESS ) {
340 be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
342 be->be_restrictops &= ~SLAP_RESTRICT_OP_WRITES;
348 rc = LDAP_CONSTRAINT_VIOLATION;
350 if ( rc == LDAP_SUCCESS ) {
351 attrs_free( save_attrs );
353 Attribute *tmp = e->e_attrs;
354 e->e_attrs = save_attrs;
360 #if defined(LDAP_SLAPI)
362 monitor_back_add_plugin( Backend *be, Entry *e_database )
364 Slapi_PBlock *pCurrentPB;
365 int i, rc = LDAP_SUCCESS;
366 struct monitorinfo *mi = ( struct monitorinfo * )be->be_private;
368 if ( slapi_int_pblock_get_first( be, &pCurrentPB ) != LDAP_SUCCESS ) {
370 * LDAP_OTHER is returned if no plugins are installed
378 Slapi_PluginDesc *srchdesc;
379 char buf[ BACKMONITOR_BUFSIZE ];
382 rc = slapi_pblock_get( pCurrentPB, SLAPI_PLUGIN_DESCRIPTION,
384 if ( rc != LDAP_SUCCESS ) {
388 snprintf( buf, sizeof(buf),
389 "plugin %d name: %s; "
395 srchdesc->spd_vendor,
396 srchdesc->spd_version,
397 srchdesc->spd_description );
400 bv.bv_len = strlen( buf );
401 attr_merge_normalize_one( e_database,
402 mi->mi_ad_monitoredInfo, &bv, NULL );
406 } while ( ( slapi_int_pblock_get_next( &pCurrentPB ) == LDAP_SUCCESS )
407 && ( pCurrentPB != NULL ) );
412 #endif /* defined(LDAP_SLAPI) */