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>
26 #include <ac/unistd.h>
29 #include "back-monitor.h"
31 #if defined(LDAP_SLAPI)
33 static int monitor_back_add_plugin( Backend *be, Entry *e );
34 #endif /* defined(LDAP_SLAPI) */
36 #if defined(SLAPD_BDB)
37 #include "../back-bdb/back-bdb.h"
38 #endif /* defined(SLAPD_BDB) */
39 #if defined(SLAPD_HDB)
40 #include "../back-hdb/back-bdb.h"
41 #endif /* defined(SLAPD_HDB) */
42 #if defined(SLAPD_LDAP)
43 #include "../back-ldap/back-ldap.h"
44 #endif /* defined(SLAPD_LDAP) */
45 #if 0 && defined(SLAPD_LDBM)
46 #include "../back-ldbm/back-ldbm.h"
47 #endif /* defined(SLAPD_LDBM) */
49 static struct restricted_ops_t {
52 } restricted_ops[] = {
53 { BER_BVC( "add" ), SLAP_RESTRICT_OP_ADD },
54 { BER_BVC( "bind" ), SLAP_RESTRICT_OP_BIND },
55 { BER_BVC( "compare" ), SLAP_RESTRICT_OP_COMPARE },
56 { BER_BVC( "delete" ), SLAP_RESTRICT_OP_DELETE },
57 { BER_BVC( "extended" ), SLAP_RESTRICT_OP_EXTENDED },
58 { BER_BVC( "modify" ), SLAP_RESTRICT_OP_MODIFY },
59 { BER_BVC( "rename" ), SLAP_RESTRICT_OP_RENAME },
60 { BER_BVC( "search" ), SLAP_RESTRICT_OP_SEARCH },
62 }, restricted_exops[] = {
63 { BER_BVC( LDAP_EXOP_START_TLS ), SLAP_RESTRICT_EXOP_START_TLS },
64 { BER_BVC( LDAP_EXOP_MODIFY_PASSWD ), SLAP_RESTRICT_EXOP_MODIFY_PASSWD },
65 { BER_BVC( LDAP_EXOP_X_WHO_AM_I ), SLAP_RESTRICT_EXOP_WHOAMI },
66 { BER_BVC( LDAP_EXOP_X_CANCEL ), SLAP_RESTRICT_EXOP_CANCEL },
71 init_readOnly( monitor_info_t *mi, Entry *e, slap_mask_t restrictops )
73 struct berval *tf = ( ( restrictops & SLAP_RESTRICT_OP_MASK ) == SLAP_RESTRICT_OP_WRITES ) ?
74 (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv;
76 return attr_merge_one( e, mi->mi_ad_readOnly, tf, NULL );
80 init_restrictedOperation( monitor_info_t *mi, Entry *e, slap_mask_t restrictops )
84 for ( i = 0; restricted_ops[ i ].op.bv_val; i++ ) {
85 if ( restrictops & restricted_ops[ i ].tag ) {
86 rc = attr_merge_one( e, mi->mi_ad_restrictedOperation,
87 &restricted_ops[ i ].op, NULL );
94 for ( i = 0; restricted_exops[ i ].op.bv_val; i++ ) {
95 if ( restrictops & restricted_exops[ i ].tag ) {
96 rc = attr_merge_one( e, mi->mi_ad_restrictedOperation,
97 &restricted_exops[ i ].op, NULL );
108 monitor_subsys_database_init(
114 Entry *e_database, **ep;
117 monitor_subsys_t *ms_backend,
120 assert( be != NULL );
122 mi = ( monitor_info_t * )be->be_private;
124 ms_backend = monitor_back_get_subsys( SLAPD_MONITOR_BACKEND_NAME );
125 if ( ms_backend == NULL ) {
126 Debug( LDAP_DEBUG_ANY,
127 "monitor_subsys_database_init: "
129 "\"" SLAPD_MONITOR_BACKEND_NAME "\" "
135 ms_overlay = monitor_back_get_subsys( SLAPD_MONITOR_OVERLAY_NAME );
136 if ( ms_overlay == NULL ) {
137 Debug( LDAP_DEBUG_ANY,
138 "monitor_subsys_database_init: "
140 "\"" SLAPD_MONITOR_OVERLAY_NAME "\" "
146 if ( monitor_cache_get( mi, &ms->mss_ndn, &e_database ) ) {
147 Debug( LDAP_DEBUG_ANY,
148 "monitor_subsys_database_init: "
149 "unable to get entry \"%s\"\n",
150 ms->mss_ndn.bv_val, 0, 0 );
154 (void)init_readOnly( mi, e_database, frontendDB->be_restrictops );
155 (void)init_restrictedOperation( mi, e_database, frontendDB->be_restrictops );
157 mp = ( monitor_entry_t * )e_database->e_private;
158 mp->mp_children = NULL;
159 ep = &mp->mp_children;
161 for ( i = 0; i < nBackendDB; i++ ) {
162 char buf[ BACKMONITOR_BUFSIZE ];
164 slap_overinfo *oi = NULL;
168 be = &backendDB[ i ];
172 if ( overlay_is_over( be ) ) {
173 oi = (slap_overinfo *)be->bd_info->bi_private;
177 /* Subordinates are not exposed as their own naming context */
178 if ( SLAP_GLUE_SUBORDINATE( be ) ) {
182 snprintf( buf, sizeof( buf ),
183 "dn: cn=Database %d,%s\n"
185 "structuralObjectClass: %s\n"
187 "description: This object contains the type of the database.\n"
190 "modifiersName: %s\n"
191 "createTimestamp: %s\n"
192 "modifyTimestamp: %s\n",
195 mi->mi_oc_monitoredObject->soc_cname.bv_val,
196 mi->mi_oc_monitoredObject->soc_cname.bv_val,
198 mi->mi_ad_monitoredInfo->ad_cname.bv_val,
200 mi->mi_creatorsName.bv_val,
201 mi->mi_creatorsName.bv_val,
202 mi->mi_startTime.bv_val,
203 mi->mi_startTime.bv_val );
205 e = str2entry( buf );
207 Debug( LDAP_DEBUG_ANY,
208 "monitor_subsys_database_init: "
209 "unable to create entry \"cn=Database %d,%s\"\n",
210 i, ms->mss_dn.bv_val, 0 );
214 if ( SLAP_MONITOR(be) ) {
215 attr_merge( e, slap_schema.si_ad_monitorContext,
216 be->be_suffix, be->be_nsuffix );
217 attr_merge( e_database, slap_schema.si_ad_monitorContext,
218 be->be_suffix, be->be_nsuffix );
221 attr_merge( e, slap_schema.si_ad_namingContexts,
222 be->be_suffix, be->be_nsuffix );
223 attr_merge( e_database, slap_schema.si_ad_namingContexts,
224 be->be_suffix, be->be_nsuffix );
227 (void)init_readOnly( mi, e, be->be_restrictops );
228 (void)init_restrictedOperation( mi, e, be->be_restrictops );
231 slap_overinst *on = oi->oi_list,
234 for ( ; on; on = on->on_next ) {
238 for ( on2 = on1; on2 != on; on2 = on2->on_next ) {
239 if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
248 bv.bv_val = on->on_bi.bi_type;
249 bv.bv_len = strlen( bv.bv_val );
250 attr_merge_normalize_one( e, mi->mi_ad_monitorOverlay,
253 /* find the overlay number, j */
254 for ( on2 = overlay_next( NULL ), j = 0; on2; on2 = overlay_next( on2 ), j++ ) {
255 if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
261 snprintf( buf, sizeof( buf ),
263 j, ms_overlay->mss_dn.bv_val );
265 bv.bv_len = strlen( buf );
266 attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
271 #if defined(SLAPD_BDB) || defined(SLAPD_HDB)
272 if ( strcmp( bi->bi_type, "bdb" ) == 0
273 || strcmp( bi->bi_type, "hdb" ) == 0 )
276 ber_len_t pathlen = 0, len = 0;
277 char path[ PATH_MAX ] = { '\0' };
280 if ( strcmp( bi->bi_type, "bdb" ) == 0
281 || strcmp( bi->bi_type, "hdb" ) == 0 )
283 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
285 fname = bdb->bi_dbenv_home;
287 } else if ( strcmp( bi->bi_type, "ldbm" ) == 0 ) {
288 struct ldbminfo *ldbm = (struct ldbminfo *) be->be_private;
290 /* FIXME: there's a conflict
291 * between back-bdb.h and back.ldbm.h;
292 * anyway, this code will be moved
293 * to the backends as soon as the
294 * issue with filtering on namingContexts
296 fname = ldbm->li_directory;
300 len = strlen( fname );
301 if ( fname[ 0 ] != '/' ) {
302 /* get full path name */
303 getcwd( path, sizeof( path ) );
304 pathlen = strlen( path );
306 if ( fname[ 0 ] == '.' && fname[ 1 ] == '/' ) {
312 bv.bv_len = STRLENOF( "file://" ) + pathlen
313 + STRLENOF( "/" ) + len;
314 bv.bv_val = ch_malloc( bv.bv_len + STRLENOF( "/" ) + 1 );
315 AC_MEMCPY( bv.bv_val, "file://", STRLENOF( "file://" ) );
317 AC_MEMCPY( &bv.bv_val[ STRLENOF( "file://" ) ],
319 bv.bv_val[ STRLENOF( "file://" ) + pathlen ] = '/';
322 AC_MEMCPY( &bv.bv_val[ STRLENOF( "file://" ) + pathlen ],
324 if ( bv.bv_val[ bv.bv_len - 1 ] != '/' ) {
325 bv.bv_val[ bv.bv_len ] = '/';
328 bv.bv_val[ bv.bv_len ] = '\0';
330 attr_merge_normalize_one( e, slap_schema.si_ad_labeledURI,
333 ch_free( bv.bv_val );
335 #endif /* defined(SLAPD_LDAP) || defined(SLAPD_HDB) */
337 #if defined(SLAPD_LDAP)
338 if ( strcmp( bi->bi_type, "ldap" ) == 0 ) {
339 struct ldapinfo *li = (struct ldapinfo *)be->be_private;
343 bv.bv_len = strlen( bv.bv_val );
344 attr_merge_normalize_one( e, slap_schema.si_ad_labeledURI,
347 #endif /* defined(SLAPD_LDAP) */
349 for ( j = 0; j < nBackendInfo; j++ ) {
350 if ( backendInfo[ j ].bi_type == bi->bi_type ) {
353 snprintf( buf, sizeof( buf ),
355 j, ms_backend->mss_dn.bv_val );
357 bv.bv_len = strlen( buf );
358 attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
363 /* we must find it! */
366 mp = monitor_entrypriv_create();
370 e->e_private = ( void * )mp;
372 mp->mp_flags = ms->mss_flags
375 if ( monitor_cache_add( mi, e ) ) {
376 Debug( LDAP_DEBUG_ANY,
377 "monitor_subsys_database_init: "
378 "unable to add entry \"cn=Database %d,%s\"\n",
379 i, ms->mss_dn.bv_val, 0 );
383 #if defined(LDAP_SLAPI)
384 monitor_back_add_plugin( be, e );
385 #endif /* defined(LDAP_SLAPI) */
388 Entry **ep_overlay = &mp->mp_children;
389 monitor_entry_t *mp_overlay;
390 slap_overinst *on = oi->oi_list;
393 for ( o = 0; on; o++, on = on->on_next ) {
397 /* find the overlay number, j */
398 for ( on2 = overlay_next( NULL ), j = 0; on2; on2 = overlay_next( on2 ), j++ ) {
399 if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
405 snprintf( buf, sizeof( buf ),
406 "dn: cn=Overlay %d,cn=Database %d,%s\n"
408 "structuralObjectClass: %s\n"
410 "description: This object contains the type of the overlay.\n"
412 "seeAlso: cn=Overlay %d,%s\n"
414 "modifiersName: %s\n"
415 "createTimestamp: %s\n"
416 "modifyTimestamp: %s\n",
420 mi->mi_oc_monitoredObject->soc_cname.bv_val,
421 mi->mi_oc_monitoredObject->soc_cname.bv_val,
423 mi->mi_ad_monitoredInfo->ad_cname.bv_val,
426 ms_overlay->mss_dn.bv_val,
427 mi->mi_creatorsName.bv_val,
428 mi->mi_creatorsName.bv_val,
429 mi->mi_startTime.bv_val,
430 mi->mi_startTime.bv_val );
432 e_overlay = str2entry( buf );
433 if ( e_overlay == NULL ) {
434 Debug( LDAP_DEBUG_ANY,
435 "monitor_subsys_database_init: "
436 "unable to create entry "
437 "\"cn=Overlay %d,cn=Database %d,%s\"\n",
438 o, i, ms->mss_dn.bv_val );
442 mp_overlay = monitor_entrypriv_create();
443 if ( mp_overlay == NULL ) {
446 e_overlay->e_private = ( void * )mp_overlay;
447 mp_overlay->mp_info = ms;
448 mp_overlay->mp_flags = ms->mss_flags
451 if ( monitor_cache_add( mi, e_overlay ) ) {
452 Debug( LDAP_DEBUG_ANY,
453 "monitor_subsys_database_init: "
454 "unable to add entry "
455 "\"cn=Overlay %d,cn=Database %d,%s\"\n",
456 o, i, ms->mss_dn.bv_val );
460 *ep_overlay = e_overlay;
461 ep_overlay = &mp_overlay->mp_next;
469 monitor_cache_release( mi, e_database );
476 * cur: must not contain the tags corresponding to the values in v
477 * delta: will contain the tags corresponding to the values in v
480 value_mask( BerVarray v, slap_mask_t cur, slap_mask_t *delta )
482 for ( ; !BER_BVISNULL( v ); v++ ) {
483 struct restricted_ops_t *rops;
486 if ( OID_LEADCHAR( v->bv_val[ 0 ] ) ) {
487 rops = restricted_exops;
490 rops = restricted_ops;
493 for ( i = 0; !BER_BVISNULL( &rops[ i ].op ); i++ ) {
494 if ( ber_bvstrcasecmp( v, &rops[ i ].op ) != 0 ) {
498 if ( rops[ i ].tag & *delta ) {
502 if ( rops[ i ].tag & cur ) {
506 cur |= rops[ i ].tag;
507 *delta |= rops[ i ].tag;
512 if ( BER_BVISNULL( &rops[ i ].op ) ) {
513 return LDAP_INVALID_SYNTAX;
521 monitor_subsys_database_modify(
526 monitor_info_t *mi = (monitor_info_t *)op->o_bd->be_private;
528 Attribute *save_attrs, *a;
529 Modifications *modlist = op->oq_modify.rs_modlist;
532 int ro_gotval = 1, i, n;
533 slap_mask_t rp_add = 0, rp_delete = 0, rp_cur;
536 i = sscanf( e->e_nname.bv_val, "cn=database %d,", &n );
538 return LDAP_UNWILLING_TO_PERFORM;
540 if ( n < 0 || n >= nBackendDB )
541 return LDAP_NO_SUCH_OBJECT;
543 /* do not allow some changes on back-monitor (needs work)... */
544 be = &backendDB[ n ];
545 if ( SLAP_MONITOR( be ) )
546 return LDAP_UNWILLING_TO_PERFORM;
548 rp_cur = be->be_restrictops;
550 save_attrs = e->e_attrs;
551 e->e_attrs = attrs_dup( e->e_attrs );
553 for ( ml=modlist; ml; ml=ml->sml_next ) {
554 Modification *mod = &ml->sml_mod;
556 if ( mod->sm_desc == mi->mi_ad_readOnly ) {
559 if ( mod->sm_values ) {
560 if ( !BER_BVISNULL( &mod->sm_values[ 1 ] ) ) {
561 rc = LDAP_CONSTRAINT_VIOLATION;
565 if ( bvmatch( &slap_true_bv, mod->sm_values )) {
568 } else if ( bvmatch( &slap_false_bv, mod->sm_values )) {
572 rc = LDAP_INVALID_SYNTAX;
577 switch ( mod->sm_op ) {
578 case LDAP_MOD_DELETE:
579 if ( ro_gotval < 1 ) {
580 rc = LDAP_CONSTRAINT_VIOLATION;
585 if ( val == 0 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) {
586 rc = LDAP_NO_SUCH_ATTRIBUTE;
590 if ( val == 1 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) != SLAP_RESTRICT_OP_WRITES ) {
591 rc = LDAP_NO_SUCH_ATTRIBUTE;
597 case LDAP_MOD_REPLACE:
602 if ( ro_gotval > 0 ) {
603 rc = LDAP_CONSTRAINT_VIOLATION;
609 rp_add |= (~rp_cur) & SLAP_RESTRICT_OP_WRITES;
610 rp_cur |= SLAP_RESTRICT_OP_WRITES;
611 rp_delete &= ~SLAP_RESTRICT_OP_WRITES;
613 } else if ( val == 0 ) {
614 rp_delete |= rp_cur & SLAP_RESTRICT_OP_WRITES;
615 rp_cur &= ~SLAP_RESTRICT_OP_WRITES;
616 rp_add &= ~SLAP_RESTRICT_OP_WRITES;
625 } else if ( mod->sm_desc == mi->mi_ad_restrictedOperation ) {
626 slap_mask_t mask = 0;
628 switch ( mod->sm_op ) {
629 case LDAP_MOD_DELETE:
630 if ( mod->sm_values == NULL ) {
636 rc = value_mask( mod->sm_values, ~rp_cur, &mask );
637 if ( rc == LDAP_SUCCESS ) {
642 } else if ( rc == LDAP_OTHER ) {
643 rc = LDAP_NO_SUCH_ATTRIBUTE;
647 case LDAP_MOD_REPLACE:
654 rc = value_mask( mod->sm_values, rp_cur, &mask );
655 if ( rc == LDAP_SUCCESS ) {
660 } else if ( rc == LDAP_OTHER ) {
661 rc = LDAP_TYPE_OR_VALUE_EXISTS;
670 if ( rc != LDAP_SUCCESS ) {
674 } else if ( is_at_operational( mod->sm_desc->ad_type )) {
675 /* accept all operational attributes */
676 attr_delete( &e->e_attrs, mod->sm_desc );
677 rc = attr_merge( e, mod->sm_desc, mod->sm_values,
685 rc = LDAP_UNWILLING_TO_PERFORM;
691 if ( ro_gotval < 1 ) {
692 rc = LDAP_CONSTRAINT_VIOLATION;
696 if ( ( rp_cur & SLAP_RESTRICT_OP_EXTENDED ) && ( rp_cur & SLAP_RESTRICT_EXOP_MASK ) ) {
697 rc = LDAP_CONSTRAINT_VIOLATION;
701 if ( rp_delete & rp_add ) {
706 /* check current value of readOnly */
707 if ( ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) {
708 tf = (struct berval *)&slap_true_bv;
711 tf = (struct berval *)&slap_false_bv;
714 a = attr_find( e->e_attrs, mi->mi_ad_readOnly );
720 if ( !bvmatch( &a->a_vals[ 0 ], tf ) ) {
721 attr_delete( &e->e_attrs, mi->mi_ad_readOnly );
722 rc = attr_merge_one( e, mi->mi_ad_readOnly, tf, NULL );
725 if ( rc == LDAP_SUCCESS ) {
727 if ( rp_delete == be->be_restrictops ) {
728 attr_delete( &e->e_attrs, mi->mi_ad_restrictedOperation );
731 a = attr_find( e->e_attrs, mi->mi_ad_restrictedOperation );
737 for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) {
738 if ( rp_delete & restricted_ops[ i ].tag ) {
741 for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) {
744 if ( !bvmatch( &a->a_nvals[ j ], &restricted_ops[ i ].op ) ) {
748 ch_free( a->a_vals[ j ].bv_val );
749 ch_free( a->a_nvals[ j ].bv_val );
751 for ( k = j + 1; !BER_BVISNULL( &a->a_nvals[ k ] ); k++ ) {
752 a->a_vals[ k - 1 ] = a->a_vals[ k ];
753 a->a_nvals[ k - 1 ] = a->a_nvals[ k ];
756 BER_BVZERO( &a->a_vals[ k - 1 ] );
757 BER_BVZERO( &a->a_nvals[ k - 1 ] );
762 for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) {
763 if ( rp_delete & restricted_exops[ i ].tag ) {
766 for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) {
769 if ( !bvmatch( &a->a_nvals[ j ], &restricted_exops[ i ].op ) ) {
773 ch_free( a->a_vals[ j ].bv_val );
774 ch_free( a->a_nvals[ j ].bv_val );
776 for ( k = j + 1; !BER_BVISNULL( &a->a_nvals[ k ] ); k++ ) {
777 a->a_vals[ k - 1 ] = a->a_vals[ k ];
778 a->a_nvals[ k - 1 ] = a->a_nvals[ k ];
781 BER_BVZERO( &a->a_vals[ k - 1 ] );
782 BER_BVZERO( &a->a_nvals[ k - 1 ] );
790 for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) {
791 if ( rp_add & restricted_ops[ i ].tag ) {
792 attr_merge_one( e, mi->mi_ad_restrictedOperation,
793 &restricted_ops[ i ].op, NULL );
797 for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) {
798 if ( rp_add & restricted_exops[ i ].tag ) {
799 attr_merge_one( e, mi->mi_ad_restrictedOperation,
800 &restricted_exops[ i ].op, NULL );
806 be->be_restrictops = rp_cur;
809 if ( rc == LDAP_SUCCESS ) {
810 attrs_free( save_attrs );
813 Attribute *tmp = e->e_attrs;
814 e->e_attrs = save_attrs;
820 #if defined(LDAP_SLAPI)
822 monitor_back_add_plugin( Backend *be, Entry *e_database )
824 Slapi_PBlock *pCurrentPB;
825 int i, rc = LDAP_SUCCESS;
826 monitor_info_t *mi = ( monitor_info_t * )be->be_private;
828 if ( slapi_int_pblock_get_first( be, &pCurrentPB ) != LDAP_SUCCESS ) {
830 * LDAP_OTHER is returned if no plugins are installed
838 Slapi_PluginDesc *srchdesc;
839 char buf[ BACKMONITOR_BUFSIZE ];
842 rc = slapi_pblock_get( pCurrentPB, SLAPI_PLUGIN_DESCRIPTION,
844 if ( rc != LDAP_SUCCESS ) {
848 snprintf( buf, sizeof(buf),
849 "plugin %d name: %s; "
855 srchdesc->spd_vendor,
856 srchdesc->spd_version,
857 srchdesc->spd_description );
860 bv.bv_len = strlen( buf );
861 attr_merge_normalize_one( e_database,
862 mi->mi_ad_monitoredInfo, &bv, NULL );
866 } while ( ( slapi_int_pblock_get_next( &pCurrentPB ) == LDAP_SUCCESS )
867 && ( pCurrentPB != NULL ) );
872 #endif /* defined(LDAP_SLAPI) */