1 /* database.c - deals with database subsystem */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2001-2005 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( monitor_info_t *mi, 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 =
348 (struct ldapinfo *)be->be_private;
351 ber_str2bv( li->url, 0, 0, &bv );
353 attr_merge_normalize_one( e,
354 slap_schema.si_ad_labeledURI,
357 #endif /* defined(SLAPD_LDAP) */
359 for ( j = 0; j < nBackendInfo; j++ ) {
360 if ( backendInfo[ j ].bi_type == bi->bi_type ) {
363 snprintf( buf, sizeof( buf ),
365 j, ms_backend->mss_dn.bv_val );
367 bv.bv_len = strlen( buf );
368 attr_merge_normalize_one( e, mi->mi_ad_seeAlso,
373 /* we must find it! */
376 mp = monitor_entrypriv_create();
380 e->e_private = ( void * )mp;
382 mp->mp_flags = ms->mss_flags
385 if ( monitor_cache_add( mi, e ) ) {
386 Debug( LDAP_DEBUG_ANY,
387 "monitor_subsys_database_init: "
388 "unable to add entry \"cn=Database %d,%s\"\n",
389 i, ms->mss_dn.bv_val, 0 );
393 #if defined(LDAP_SLAPI)
394 monitor_back_add_plugin( mi, be, e );
395 #endif /* defined(LDAP_SLAPI) */
398 Entry **ep_overlay = &mp->mp_children;
399 monitor_entry_t *mp_overlay;
400 slap_overinst *on = oi->oi_list;
403 for ( o = 0; on; o++, on = on->on_next ) {
407 /* find the overlay number, j */
408 for ( on2 = overlay_next( NULL ), j = 0; on2; on2 = overlay_next( on2 ), j++ ) {
409 if ( on2->on_bi.bi_type == on->on_bi.bi_type ) {
415 snprintf( buf, sizeof( buf ),
416 "dn: cn=Overlay %d,cn=Database %d,%s\n"
418 "structuralObjectClass: %s\n"
420 "description: This object contains the type of the overlay.\n"
422 "seeAlso: cn=Overlay %d,%s\n"
424 "modifiersName: %s\n"
425 "createTimestamp: %s\n"
426 "modifyTimestamp: %s\n",
430 mi->mi_oc_monitoredObject->soc_cname.bv_val,
431 mi->mi_oc_monitoredObject->soc_cname.bv_val,
433 mi->mi_ad_monitoredInfo->ad_cname.bv_val,
436 ms_overlay->mss_dn.bv_val,
437 mi->mi_creatorsName.bv_val,
438 mi->mi_creatorsName.bv_val,
439 mi->mi_startTime.bv_val,
440 mi->mi_startTime.bv_val );
442 e_overlay = str2entry( buf );
443 if ( e_overlay == NULL ) {
444 Debug( LDAP_DEBUG_ANY,
445 "monitor_subsys_database_init: "
446 "unable to create entry "
447 "\"cn=Overlay %d,cn=Database %d,%s\"\n",
448 o, i, ms->mss_dn.bv_val );
452 mp_overlay = monitor_entrypriv_create();
453 if ( mp_overlay == NULL ) {
456 e_overlay->e_private = ( void * )mp_overlay;
457 mp_overlay->mp_info = ms;
458 mp_overlay->mp_flags = ms->mss_flags
461 if ( monitor_cache_add( mi, e_overlay ) ) {
462 Debug( LDAP_DEBUG_ANY,
463 "monitor_subsys_database_init: "
464 "unable to add entry "
465 "\"cn=Overlay %d,cn=Database %d,%s\"\n",
466 o, i, ms->mss_dn.bv_val );
470 *ep_overlay = e_overlay;
471 ep_overlay = &mp_overlay->mp_next;
479 monitor_cache_release( mi, e_database );
486 * cur: must not contain the tags corresponding to the values in v
487 * delta: will contain the tags corresponding to the values in v
490 value_mask( BerVarray v, slap_mask_t cur, slap_mask_t *delta )
492 for ( ; !BER_BVISNULL( v ); v++ ) {
493 struct restricted_ops_t *rops;
496 if ( OID_LEADCHAR( v->bv_val[ 0 ] ) ) {
497 rops = restricted_exops;
500 rops = restricted_ops;
503 for ( i = 0; !BER_BVISNULL( &rops[ i ].op ); i++ ) {
504 if ( ber_bvstrcasecmp( v, &rops[ i ].op ) != 0 ) {
508 if ( rops[ i ].tag & *delta ) {
512 if ( rops[ i ].tag & cur ) {
516 cur |= rops[ i ].tag;
517 *delta |= rops[ i ].tag;
522 if ( BER_BVISNULL( &rops[ i ].op ) ) {
523 return LDAP_INVALID_SYNTAX;
531 monitor_subsys_database_modify(
536 monitor_info_t *mi = (monitor_info_t *)op->o_bd->be_private;
538 Attribute *save_attrs, *a;
539 Modifications *modlist = op->oq_modify.rs_modlist;
542 int ro_gotval = 1, i, n;
543 slap_mask_t rp_add = 0, rp_delete = 0, rp_cur;
546 i = sscanf( e->e_nname.bv_val, "cn=database %d,", &n );
548 return LDAP_UNWILLING_TO_PERFORM;
550 if ( n < 0 || n >= nBackendDB )
551 return LDAP_NO_SUCH_OBJECT;
553 /* do not allow some changes on back-monitor (needs work)... */
554 be = &backendDB[ n ];
555 if ( SLAP_MONITOR( be ) )
556 return LDAP_UNWILLING_TO_PERFORM;
558 rp_cur = be->be_restrictops;
560 save_attrs = e->e_attrs;
561 e->e_attrs = attrs_dup( e->e_attrs );
563 for ( ml=modlist; ml; ml=ml->sml_next ) {
564 Modification *mod = &ml->sml_mod;
566 if ( mod->sm_desc == mi->mi_ad_readOnly ) {
569 if ( mod->sm_values ) {
570 if ( !BER_BVISNULL( &mod->sm_values[ 1 ] ) ) {
571 rc = LDAP_CONSTRAINT_VIOLATION;
575 if ( bvmatch( &slap_true_bv, mod->sm_values )) {
578 } else if ( bvmatch( &slap_false_bv, mod->sm_values )) {
582 rc = LDAP_INVALID_SYNTAX;
587 switch ( mod->sm_op ) {
588 case LDAP_MOD_DELETE:
589 if ( ro_gotval < 1 ) {
590 rc = LDAP_CONSTRAINT_VIOLATION;
595 if ( val == 0 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) {
596 rc = LDAP_NO_SUCH_ATTRIBUTE;
600 if ( val == 1 && ( rp_cur & SLAP_RESTRICT_OP_WRITES ) != SLAP_RESTRICT_OP_WRITES ) {
601 rc = LDAP_NO_SUCH_ATTRIBUTE;
607 case LDAP_MOD_REPLACE:
612 if ( ro_gotval > 0 ) {
613 rc = LDAP_CONSTRAINT_VIOLATION;
619 rp_add |= (~rp_cur) & SLAP_RESTRICT_OP_WRITES;
620 rp_cur |= SLAP_RESTRICT_OP_WRITES;
621 rp_delete &= ~SLAP_RESTRICT_OP_WRITES;
623 } else if ( val == 0 ) {
624 rp_delete |= rp_cur & SLAP_RESTRICT_OP_WRITES;
625 rp_cur &= ~SLAP_RESTRICT_OP_WRITES;
626 rp_add &= ~SLAP_RESTRICT_OP_WRITES;
635 } else if ( mod->sm_desc == mi->mi_ad_restrictedOperation ) {
636 slap_mask_t mask = 0;
638 switch ( mod->sm_op ) {
639 case LDAP_MOD_DELETE:
640 if ( mod->sm_values == NULL ) {
646 rc = value_mask( mod->sm_values, ~rp_cur, &mask );
647 if ( rc == LDAP_SUCCESS ) {
652 } else if ( rc == LDAP_OTHER ) {
653 rc = LDAP_NO_SUCH_ATTRIBUTE;
657 case LDAP_MOD_REPLACE:
664 rc = value_mask( mod->sm_values, rp_cur, &mask );
665 if ( rc == LDAP_SUCCESS ) {
670 } else if ( rc == LDAP_OTHER ) {
671 rc = LDAP_TYPE_OR_VALUE_EXISTS;
680 if ( rc != LDAP_SUCCESS ) {
684 } else if ( is_at_operational( mod->sm_desc->ad_type )) {
685 /* accept all operational attributes */
686 attr_delete( &e->e_attrs, mod->sm_desc );
687 rc = attr_merge( e, mod->sm_desc, mod->sm_values,
695 rc = LDAP_UNWILLING_TO_PERFORM;
701 if ( ro_gotval < 1 ) {
702 rc = LDAP_CONSTRAINT_VIOLATION;
706 if ( ( rp_cur & SLAP_RESTRICT_OP_EXTENDED ) && ( rp_cur & SLAP_RESTRICT_EXOP_MASK ) ) {
707 rc = LDAP_CONSTRAINT_VIOLATION;
711 if ( rp_delete & rp_add ) {
716 /* check current value of readOnly */
717 if ( ( rp_cur & SLAP_RESTRICT_OP_WRITES ) == SLAP_RESTRICT_OP_WRITES ) {
718 tf = (struct berval *)&slap_true_bv;
721 tf = (struct berval *)&slap_false_bv;
724 a = attr_find( e->e_attrs, mi->mi_ad_readOnly );
730 if ( !bvmatch( &a->a_vals[ 0 ], tf ) ) {
731 attr_delete( &e->e_attrs, mi->mi_ad_readOnly );
732 rc = attr_merge_one( e, mi->mi_ad_readOnly, tf, NULL );
735 if ( rc == LDAP_SUCCESS ) {
737 if ( rp_delete == be->be_restrictops ) {
738 attr_delete( &e->e_attrs, mi->mi_ad_restrictedOperation );
741 a = attr_find( e->e_attrs, mi->mi_ad_restrictedOperation );
747 for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) {
748 if ( rp_delete & restricted_ops[ i ].tag ) {
751 for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) {
754 if ( !bvmatch( &a->a_nvals[ j ], &restricted_ops[ i ].op ) ) {
758 ch_free( a->a_vals[ j ].bv_val );
759 ch_free( a->a_nvals[ j ].bv_val );
761 for ( k = j + 1; !BER_BVISNULL( &a->a_nvals[ k ] ); k++ ) {
762 a->a_vals[ k - 1 ] = a->a_vals[ k ];
763 a->a_nvals[ k - 1 ] = a->a_nvals[ k ];
766 BER_BVZERO( &a->a_vals[ k - 1 ] );
767 BER_BVZERO( &a->a_nvals[ k - 1 ] );
772 for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) {
773 if ( rp_delete & restricted_exops[ i ].tag ) {
776 for ( j = 0; !BER_BVISNULL( &a->a_nvals[ j ] ); j++ ) {
779 if ( !bvmatch( &a->a_nvals[ j ], &restricted_exops[ i ].op ) ) {
783 ch_free( a->a_vals[ j ].bv_val );
784 ch_free( a->a_nvals[ j ].bv_val );
786 for ( k = j + 1; !BER_BVISNULL( &a->a_nvals[ k ] ); k++ ) {
787 a->a_vals[ k - 1 ] = a->a_vals[ k ];
788 a->a_nvals[ k - 1 ] = a->a_nvals[ k ];
791 BER_BVZERO( &a->a_vals[ k - 1 ] );
792 BER_BVZERO( &a->a_nvals[ k - 1 ] );
800 for ( i = 0; !BER_BVISNULL( &restricted_ops[ i ].op ); i++ ) {
801 if ( rp_add & restricted_ops[ i ].tag ) {
802 attr_merge_one( e, mi->mi_ad_restrictedOperation,
803 &restricted_ops[ i ].op, NULL );
807 for ( i = 0; !BER_BVISNULL( &restricted_exops[ i ].op ); i++ ) {
808 if ( rp_add & restricted_exops[ i ].tag ) {
809 attr_merge_one( e, mi->mi_ad_restrictedOperation,
810 &restricted_exops[ i ].op, NULL );
816 be->be_restrictops = rp_cur;
819 if ( rc == LDAP_SUCCESS ) {
820 attrs_free( save_attrs );
823 Attribute *tmp = e->e_attrs;
824 e->e_attrs = save_attrs;
830 #if defined(LDAP_SLAPI)
832 monitor_back_add_plugin( monitor_info_t *mi, Backend *be, Entry *e_database )
834 Slapi_PBlock *pCurrentPB;
835 int i, rc = LDAP_SUCCESS;
837 if ( slapi_int_pblock_get_first( be, &pCurrentPB ) != LDAP_SUCCESS ) {
839 * LDAP_OTHER is returned if no plugins are installed
847 Slapi_PluginDesc *srchdesc;
848 char buf[ BACKMONITOR_BUFSIZE ];
851 rc = slapi_pblock_get( pCurrentPB, SLAPI_PLUGIN_DESCRIPTION,
853 if ( rc != LDAP_SUCCESS ) {
857 snprintf( buf, sizeof(buf),
858 "plugin %d name: %s; "
864 srchdesc->spd_vendor,
865 srchdesc->spd_version,
866 srchdesc->spd_description );
869 bv.bv_len = strlen( buf );
870 attr_merge_normalize_one( e_database,
871 mi->mi_ad_monitoredInfo, &bv, NULL );
875 } while ( ( slapi_int_pblock_get_next( &pCurrentPB ) == LDAP_SUCCESS )
876 && ( pCurrentPB != NULL ) );
881 #endif /* defined(LDAP_SLAPI) */