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 /* for PATH_MAX on some systems (e.g. Solaris) */
52 #endif /* HAVE_LIMITS_H */
55 #endif /* ! PATH_MAX */
57 static struct restricted_ops_t {
60 } restricted_ops[] = {
61 { BER_BVC( "add" ), SLAP_RESTRICT_OP_ADD },
62 { BER_BVC( "bind" ), SLAP_RESTRICT_OP_BIND },
63 { BER_BVC( "compare" ), SLAP_RESTRICT_OP_COMPARE },
64 { BER_BVC( "delete" ), SLAP_RESTRICT_OP_DELETE },
65 { BER_BVC( "extended" ), SLAP_RESTRICT_OP_EXTENDED },
66 { BER_BVC( "modify" ), SLAP_RESTRICT_OP_MODIFY },
67 { BER_BVC( "rename" ), SLAP_RESTRICT_OP_RENAME },
68 { BER_BVC( "search" ), SLAP_RESTRICT_OP_SEARCH },
70 }, restricted_exops[] = {
71 { BER_BVC( LDAP_EXOP_START_TLS ), SLAP_RESTRICT_EXOP_START_TLS },
72 { BER_BVC( LDAP_EXOP_MODIFY_PASSWD ), SLAP_RESTRICT_EXOP_MODIFY_PASSWD },
73 { BER_BVC( LDAP_EXOP_X_WHO_AM_I ), SLAP_RESTRICT_EXOP_WHOAMI },
74 { BER_BVC( LDAP_EXOP_X_CANCEL ), SLAP_RESTRICT_EXOP_CANCEL },
79 init_readOnly( monitor_info_t *mi, Entry *e, slap_mask_t restrictops )
81 struct berval *tf = ( ( restrictops & SLAP_RESTRICT_OP_MASK ) == SLAP_RESTRICT_OP_WRITES ) ?
82 (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv;
84 return attr_merge_one( e, mi->mi_ad_readOnly, tf, NULL );
88 init_restrictedOperation( monitor_info_t *mi, Entry *e, slap_mask_t restrictops )
92 for ( i = 0; restricted_ops[ i ].op.bv_val; i++ ) {
93 if ( restrictops & restricted_ops[ i ].tag ) {
94 rc = attr_merge_one( e, mi->mi_ad_restrictedOperation,
95 &restricted_ops[ i ].op, NULL );
102 for ( i = 0; restricted_exops[ i ].op.bv_val; i++ ) {
103 if ( restrictops & restricted_exops[ i ].tag ) {
104 rc = attr_merge_one( e, mi->mi_ad_restrictedOperation,
105 &restricted_exops[ i ].op, NULL );
116 monitor_subsys_database_init(
122 Entry *e_database, **ep;
125 monitor_subsys_t *ms_backend,
128 assert( be != NULL );
130 mi = ( monitor_info_t * )be->be_private;
132 ms_backend = monitor_back_get_subsys( SLAPD_MONITOR_BACKEND_NAME );
133 if ( ms_backend == NULL ) {
134 Debug( LDAP_DEBUG_ANY,
135 "monitor_subsys_database_init: "
137 "\"" SLAPD_MONITOR_BACKEND_NAME "\" "
143 ms_overlay = monitor_back_get_subsys( SLAPD_MONITOR_OVERLAY_NAME );
144 if ( ms_overlay == NULL ) {
145 Debug( LDAP_DEBUG_ANY,
146 "monitor_subsys_database_init: "
148 "\"" SLAPD_MONITOR_OVERLAY_NAME "\" "
154 if ( monitor_cache_get( mi, &ms->mss_ndn, &e_database ) ) {
155 Debug( LDAP_DEBUG_ANY,
156 "monitor_subsys_database_init: "
157 "unable to get entry \"%s\"\n",
158 ms->mss_ndn.bv_val, 0, 0 );
162 (void)init_readOnly( mi, e_database, frontendDB->be_restrictops );
163 (void)init_restrictedOperation( mi, e_database, frontendDB->be_restrictops );
165 mp = ( monitor_entry_t * )e_database->e_private;
166 mp->mp_children = NULL;
167 ep = &mp->mp_children;
169 for ( i = 0; i < nBackendDB; i++ ) {
170 char buf[ BACKMONITOR_BUFSIZE ];
172 slap_overinfo *oi = NULL;
176 be = &backendDB[ i ];
180 if ( overlay_is_over( be ) ) {
181 oi = (slap_overinfo *)be->bd_info->bi_private;
185 /* Subordinates are not exposed as their own naming context */
186 if ( SLAP_GLUE_SUBORDINATE( be ) ) {
190 snprintf( buf, sizeof( buf ),
191 "dn: cn=Database %d,%s\n"
193 "structuralObjectClass: %s\n"
195 "description: This object contains the type of the database.\n"
198 "modifiersName: %s\n"
199 "createTimestamp: %s\n"
200 "modifyTimestamp: %s\n",
203 mi->mi_oc_monitoredObject->soc_cname.bv_val,
204 mi->mi_oc_monitoredObject->soc_cname.bv_val,
206 mi->mi_ad_monitoredInfo->ad_cname.bv_val,
208 mi->mi_creatorsName.bv_val,
209 mi->mi_creatorsName.bv_val,
210 mi->mi_startTime.bv_val,
211 mi->mi_startTime.bv_val );
213 e = str2entry( buf );
215 Debug( LDAP_DEBUG_ANY,
216 "monitor_subsys_database_init: "
217 "unable to create entry \"cn=Database %d,%s\"\n",
218 i, ms->mss_dn.bv_val, 0 );
222 if ( SLAP_MONITOR(be) ) {
223 attr_merge( e, slap_schema.si_ad_monitorContext,
224 be->be_suffix, be->be_nsuffix );
225 attr_merge( e_database, slap_schema.si_ad_monitorContext,
226 be->be_suffix, be->be_nsuffix );
229 attr_merge( e, slap_schema.si_ad_namingContexts,
230 be->be_suffix, be->be_nsuffix );
231 attr_merge( e_database, slap_schema.si_ad_namingContexts,
232 be->be_suffix, be->be_nsuffix );
235 (void)init_readOnly( mi, e, be->be_restrictops );
236 (void)init_restrictedOperation( mi, e, be->be_restrictops );
239 slap_overinst *on = oi->oi_list,
242 for ( ; on; on = on->on_next ) {
246 for ( on2 = on1; on2 != on; on2 = on2->on_next ) {
247 if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
256 bv.bv_val = on->on_bi.bi_type;
257 bv.bv_len = strlen( bv.bv_val );
258 attr_merge_normalize_one( e, mi->mi_ad_monitorOverlay,
261 /* find the overlay number, j */
262 for ( on2 = overlay_next( NULL ), j = 0; on2; on2 = overlay_next( on2 ), j++ ) {
263 if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
269 snprintf( buf, sizeof( buf ),
271 j, ms_overlay->mss_dn.bv_val );
273 bv.bv_len = strlen( buf );
274 attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
279 #if defined(SLAPD_BDB) || defined(SLAPD_HDB)
280 if ( strcmp( bi->bi_type, "bdb" ) == 0
281 || strcmp( bi->bi_type, "hdb" ) == 0 )
284 ber_len_t pathlen = 0, len = 0;
285 char path[ PATH_MAX ] = { '\0' };
288 if ( strcmp( bi->bi_type, "bdb" ) == 0
289 || strcmp( bi->bi_type, "hdb" ) == 0 )
291 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
293 fname = bdb->bi_dbenv_home;
295 } else if ( strcmp( bi->bi_type, "ldbm" ) == 0 ) {
296 struct ldbminfo *ldbm = (struct ldbminfo *) be->be_private;
298 /* FIXME: there's a conflict
299 * between back-bdb.h and back.ldbm.h;
300 * anyway, this code will be moved
301 * to the backends as soon as the
302 * issue with filtering on namingContexts
304 fname = ldbm->li_directory;
308 len = strlen( fname );
309 if ( fname[ 0 ] != '/' ) {
310 /* get full path name */
311 getcwd( path, sizeof( path ) );
312 pathlen = strlen( path );
314 if ( fname[ 0 ] == '.' && fname[ 1 ] == '/' ) {
320 bv.bv_len = STRLENOF( "file://" ) + pathlen
321 + STRLENOF( "/" ) + len;
322 bv.bv_val = ch_malloc( bv.bv_len + STRLENOF( "/" ) + 1 );
323 AC_MEMCPY( bv.bv_val, "file://", STRLENOF( "file://" ) );
325 AC_MEMCPY( &bv.bv_val[ STRLENOF( "file://" ) ],
327 bv.bv_val[ STRLENOF( "file://" ) + pathlen ] = '/';
330 AC_MEMCPY( &bv.bv_val[ STRLENOF( "file://" ) + pathlen ],
332 if ( bv.bv_val[ bv.bv_len - 1 ] != '/' ) {
333 bv.bv_val[ bv.bv_len ] = '/';
336 bv.bv_val[ bv.bv_len ] = '\0';
338 attr_merge_normalize_one( e, slap_schema.si_ad_labeledURI,
341 ch_free( bv.bv_val );
343 #endif /* defined(SLAPD_LDAP) || defined(SLAPD_HDB) */
345 #if defined(SLAPD_LDAP)
346 if ( strcmp( bi->bi_type, "ldap" ) == 0 ) {
347 struct ldapinfo *li = (struct ldapinfo *)be->be_private;
351 bv.bv_len = strlen( bv.bv_val );
352 attr_merge_normalize_one( e, slap_schema.si_ad_labeledURI,
355 #endif /* defined(SLAPD_LDAP) */
357 for ( j = 0; j < nBackendInfo; j++ ) {
358 if ( backendInfo[ j ].bi_type == bi->bi_type ) {
361 snprintf( buf, sizeof( buf ),
363 j, ms_backend->mss_dn.bv_val );
365 bv.bv_len = strlen( buf );
366 attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
371 /* we must find it! */
374 mp = monitor_entrypriv_create();
378 e->e_private = ( void * )mp;
380 mp->mp_flags = ms->mss_flags
383 if ( monitor_cache_add( mi, e ) ) {
384 Debug( LDAP_DEBUG_ANY,
385 "monitor_subsys_database_init: "
386 "unable to add entry \"cn=Database %d,%s\"\n",
387 i, ms->mss_dn.bv_val, 0 );
391 #if defined(LDAP_SLAPI)
392 monitor_back_add_plugin( be, e );
393 #endif /* defined(LDAP_SLAPI) */
396 Entry **ep_overlay = &mp->mp_children;
397 monitor_entry_t *mp_overlay;
398 slap_overinst *on = oi->oi_list;
401 for ( o = 0; on; o++, on = on->on_next ) {
405 /* find the overlay number, j */
406 for ( on2 = overlay_next( NULL ), j = 0; on2; on2 = overlay_next( on2 ), j++ ) {
407 if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
413 snprintf( buf, sizeof( buf ),
414 "dn: cn=Overlay %d,cn=Database %d,%s\n"
416 "structuralObjectClass: %s\n"
418 "description: This object contains the type of the overlay.\n"
420 "seeAlso: cn=Overlay %d,%s\n"
422 "modifiersName: %s\n"
423 "createTimestamp: %s\n"
424 "modifyTimestamp: %s\n",
428 mi->mi_oc_monitoredObject->soc_cname.bv_val,
429 mi->mi_oc_monitoredObject->soc_cname.bv_val,
431 mi->mi_ad_monitoredInfo->ad_cname.bv_val,
434 ms_overlay->mss_dn.bv_val,
435 mi->mi_creatorsName.bv_val,
436 mi->mi_creatorsName.bv_val,
437 mi->mi_startTime.bv_val,
438 mi->mi_startTime.bv_val );
440 e_overlay = str2entry( buf );
441 if ( e_overlay == NULL ) {
442 Debug( LDAP_DEBUG_ANY,
443 "monitor_subsys_database_init: "
444 "unable to create entry "
445 "\"cn=Overlay %d,cn=Database %d,%s\"\n",
446 o, i, ms->mss_dn.bv_val );
450 mp_overlay = monitor_entrypriv_create();
451 if ( mp_overlay == NULL ) {
454 e_overlay->e_private = ( void * )mp_overlay;
455 mp_overlay->mp_info = ms;
456 mp_overlay->mp_flags = ms->mss_flags
459 if ( monitor_cache_add( mi, e_overlay ) ) {
460 Debug( LDAP_DEBUG_ANY,
461 "monitor_subsys_database_init: "
462 "unable to add entry "
463 "\"cn=Overlay %d,cn=Database %d,%s\"\n",
464 o, i, ms->mss_dn.bv_val );
468 *ep_overlay = e_overlay;
469 ep_overlay = &mp_overlay->mp_next;
477 monitor_cache_release( mi, e_database );
484 * cur: must not contain the tags corresponding to the values in v
485 * delta: will contain the tags corresponding to the values in v
488 value_mask( BerVarray v, slap_mask_t cur, slap_mask_t *delta )
490 for ( ; !BER_BVISNULL( v ); v++ ) {
491 struct restricted_ops_t *rops;
494 if ( OID_LEADCHAR( v->bv_val[ 0 ] ) ) {
495 rops = restricted_exops;
498 rops = restricted_ops;
501 for ( i = 0; !BER_BVISNULL( &rops[ i ].op ); i++ ) {
502 if ( ber_bvstrcasecmp( v, &rops[ i ].op ) != 0 ) {
506 if ( rops[ i ].tag & *delta ) {
510 if ( rops[ i ].tag & cur ) {
514 cur |= rops[ i ].tag;
515 *delta |= rops[ i ].tag;
520 if ( BER_BVISNULL( &rops[ i ].op ) ) {
521 return LDAP_INVALID_SYNTAX;
529 monitor_subsys_database_modify(
534 monitor_info_t *mi = (monitor_info_t *)op->o_bd->be_private;
536 Attribute *save_attrs, *a;
537 Modifications *modlist = op->oq_modify.rs_modlist;
540 int ro_gotval = 1, i, n;
541 slap_mask_t rp_add = 0, rp_delete = 0, rp_cur;
544 i = sscanf( e->e_nname.bv_val, "cn=database %d,", &n );
546 return LDAP_UNWILLING_TO_PERFORM;
548 if ( n < 0 || n >= nBackendDB )
549 return LDAP_NO_SUCH_OBJECT;
551 /* do not allow some changes on back-monitor (needs work)... */
552 be = &backendDB[ n ];
553 if ( SLAP_MONITOR( be ) )
554 return LDAP_UNWILLING_TO_PERFORM;
556 rp_cur = be->be_restrictops;
558 save_attrs = e->e_attrs;
559 e->e_attrs = attrs_dup( e->e_attrs );
561 for ( ml=modlist; ml; ml=ml->sml_next ) {
562 Modification *mod = &ml->sml_mod;
564 if ( mod->sm_desc == mi->mi_ad_readOnly ) {
567 if ( mod->sm_values ) {
568 if ( !BER_BVISNULL( &mod->sm_values[ 1 ] ) ) {
569 rc = LDAP_CONSTRAINT_VIOLATION;
573 if ( bvmatch( &slap_true_bv, mod->sm_values )) {
576 } else if ( bvmatch( &slap_false_bv, mod->sm_values )) {
580 rc = LDAP_INVALID_SYNTAX;
585 switch ( mod->sm_op ) {
586 case LDAP_MOD_DELETE:
587 if ( ro_gotval < 1 ) {
588 rc = LDAP_CONSTRAINT_VIOLATION;
593 if ( val == 0 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) {
594 rc = LDAP_NO_SUCH_ATTRIBUTE;
598 if ( val == 1 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) != SLAP_RESTRICT_OP_WRITES ) {
599 rc = LDAP_NO_SUCH_ATTRIBUTE;
605 case LDAP_MOD_REPLACE:
610 if ( ro_gotval > 0 ) {
611 rc = LDAP_CONSTRAINT_VIOLATION;
617 rp_add |= (~rp_cur) & SLAP_RESTRICT_OP_WRITES;
618 rp_cur |= SLAP_RESTRICT_OP_WRITES;
619 rp_delete &= ~SLAP_RESTRICT_OP_WRITES;
621 } else if ( val == 0 ) {
622 rp_delete |= rp_cur & SLAP_RESTRICT_OP_WRITES;
623 rp_cur &= ~SLAP_RESTRICT_OP_WRITES;
624 rp_add &= ~SLAP_RESTRICT_OP_WRITES;
633 } else if ( mod->sm_desc == mi->mi_ad_restrictedOperation ) {
634 slap_mask_t mask = 0;
636 switch ( mod->sm_op ) {
637 case LDAP_MOD_DELETE:
638 if ( mod->sm_values == NULL ) {
644 rc = value_mask( mod->sm_values, ~rp_cur, &mask );
645 if ( rc == LDAP_SUCCESS ) {
650 } else if ( rc == LDAP_OTHER ) {
651 rc = LDAP_NO_SUCH_ATTRIBUTE;
655 case LDAP_MOD_REPLACE:
662 rc = value_mask( mod->sm_values, rp_cur, &mask );
663 if ( rc == LDAP_SUCCESS ) {
668 } else if ( rc == LDAP_OTHER ) {
669 rc = LDAP_TYPE_OR_VALUE_EXISTS;
678 if ( rc != LDAP_SUCCESS ) {
682 } else if ( is_at_operational( mod->sm_desc->ad_type )) {
683 /* accept all operational attributes */
684 attr_delete( &e->e_attrs, mod->sm_desc );
685 rc = attr_merge( e, mod->sm_desc, mod->sm_values,
693 rc = LDAP_UNWILLING_TO_PERFORM;
699 if ( ro_gotval < 1 ) {
700 rc = LDAP_CONSTRAINT_VIOLATION;
704 if ( ( rp_cur & SLAP_RESTRICT_OP_EXTENDED ) && ( rp_cur & SLAP_RESTRICT_EXOP_MASK ) ) {
705 rc = LDAP_CONSTRAINT_VIOLATION;
709 if ( rp_delete & rp_add ) {
714 /* check current value of readOnly */
715 if ( ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) {
716 tf = (struct berval *)&slap_true_bv;
719 tf = (struct berval *)&slap_false_bv;
722 a = attr_find( e->e_attrs, mi->mi_ad_readOnly );
728 if ( !bvmatch( &a->a_vals[ 0 ], tf ) ) {
729 attr_delete( &e->e_attrs, mi->mi_ad_readOnly );
730 rc = attr_merge_one( e, mi->mi_ad_readOnly, tf, NULL );
733 if ( rc == LDAP_SUCCESS ) {
735 if ( rp_delete == be->be_restrictops ) {
736 attr_delete( &e->e_attrs, mi->mi_ad_restrictedOperation );
739 a = attr_find( e->e_attrs, mi->mi_ad_restrictedOperation );
745 for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) {
746 if ( rp_delete & restricted_ops[ i ].tag ) {
749 for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) {
752 if ( !bvmatch( &a->a_nvals[ j ], &restricted_ops[ i ].op ) ) {
756 ch_free( a->a_vals[ j ].bv_val );
757 ch_free( a->a_nvals[ j ].bv_val );
759 for ( k = j + 1; !BER_BVISNULL( &a->a_nvals[ k ] ); k++ ) {
760 a->a_vals[ k - 1 ] = a->a_vals[ k ];
761 a->a_nvals[ k - 1 ] = a->a_nvals[ k ];
764 BER_BVZERO( &a->a_vals[ k - 1 ] );
765 BER_BVZERO( &a->a_nvals[ k - 1 ] );
770 for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) {
771 if ( rp_delete & restricted_exops[ i ].tag ) {
774 for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) {
777 if ( !bvmatch( &a->a_nvals[ j ], &restricted_exops[ i ].op ) ) {
781 ch_free( a->a_vals[ j ].bv_val );
782 ch_free( a->a_nvals[ j ].bv_val );
784 for ( k = j + 1; !BER_BVISNULL( &a->a_nvals[ k ] ); k++ ) {
785 a->a_vals[ k - 1 ] = a->a_vals[ k ];
786 a->a_nvals[ k - 1 ] = a->a_nvals[ k ];
789 BER_BVZERO( &a->a_vals[ k - 1 ] );
790 BER_BVZERO( &a->a_nvals[ k - 1 ] );
798 for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) {
799 if ( rp_add & restricted_ops[ i ].tag ) {
800 attr_merge_one( e, mi->mi_ad_restrictedOperation,
801 &restricted_ops[ i ].op, NULL );
805 for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) {
806 if ( rp_add & restricted_exops[ i ].tag ) {
807 attr_merge_one( e, mi->mi_ad_restrictedOperation,
808 &restricted_exops[ i ].op, NULL );
814 be->be_restrictops = rp_cur;
817 if ( rc == LDAP_SUCCESS ) {
818 attrs_free( save_attrs );
821 Attribute *tmp = e->e_attrs;
822 e->e_attrs = save_attrs;
828 #if defined(LDAP_SLAPI)
830 monitor_back_add_plugin( Backend *be, Entry *e_database )
832 Slapi_PBlock *pCurrentPB;
833 int i, rc = LDAP_SUCCESS;
834 monitor_info_t *mi = ( monitor_info_t * )be->be_private;
836 if ( slapi_int_pblock_get_first( be, &pCurrentPB ) != LDAP_SUCCESS ) {
838 * LDAP_OTHER is returned if no plugins are installed
846 Slapi_PluginDesc *srchdesc;
847 char buf[ BACKMONITOR_BUFSIZE ];
850 rc = slapi_pblock_get( pCurrentPB, SLAPI_PLUGIN_DESCRIPTION,
852 if ( rc != LDAP_SUCCESS ) {
856 snprintf( buf, sizeof(buf),
857 "plugin %d name: %s; "
863 srchdesc->spd_vendor,
864 srchdesc->spd_version,
865 srchdesc->spd_description );
868 bv.bv_len = strlen( buf );
869 attr_merge_normalize_one( e_database,
870 mi->mi_ad_monitoredInfo, &bv, NULL );
874 } while ( ( slapi_int_pblock_get_next( &pCurrentPB ) == LDAP_SUCCESS )
875 && ( pCurrentPB != NULL ) );
880 #endif /* defined(LDAP_SLAPI) */