📄 database.c
字号:
/* database.c - deals with database subsystem *//* $OpenLDAP: pkg/ldap/servers/slapd/back-monitor/database.c,v 1.61.2.10 2007/01/05 09:47:11 ando Exp $ *//* This work is part of OpenLDAP Software <http://www.openldap.org/>. * * Copyright 2001-2007 The OpenLDAP Foundation. * Portions Copyright 2001-2003 Pierangelo Masarati. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted only as authorized by the OpenLDAP * Public License. * * A copy of this license is available in file LICENSE in the * top-level directory of the distribution or, alternatively, at * <http://www.OpenLDAP.org/license.html>. *//* ACKNOWLEDGEMENTS: * This work was initially developed by Pierangelo Masarati for inclusion * in OpenLDAP Software. */#include "portable.h"#include <stdio.h>#include <ac/string.h>#include <ac/unistd.h>#include "slap.h"#include "back-monitor.h"#if defined(LDAP_SLAPI)#include "slapi.h"static int monitor_back_add_plugin( monitor_info_t *mi, Backend *be, Entry *e );#endif /* defined(LDAP_SLAPI) */#if defined(SLAPD_BDB)#include "../back-bdb/back-bdb.h"#endif /* defined(SLAPD_BDB) */#if defined(SLAPD_HDB)#include "../back-hdb/back-bdb.h"#endif /* defined(SLAPD_HDB) */#if defined(SLAPD_LDAP) #include "../back-ldap/back-ldap.h"#endif /* defined(SLAPD_LDAP) */#if 0 && defined(SLAPD_LDBM) #include "../back-ldbm/back-ldbm.h"#endif /* defined(SLAPD_LDBM) */#if defined(SLAPD_META) #include "../back-meta/back-meta.h"#endif /* defined(SLAPD_META) *//* for PATH_MAX on some systems (e.g. Solaris) */#ifdef HAVE_LIMITS_H#include <limits.h>#endif /* HAVE_LIMITS_H */#ifndef PATH_MAX#define PATH_MAX 4095#endif /* ! PATH_MAX */static intmonitor_subsys_database_modify( Operation *op, SlapReply *rs, Entry *e );static struct restricted_ops_t { struct berval op; unsigned int tag;} restricted_ops[] = { { BER_BVC( "add" ), SLAP_RESTRICT_OP_ADD }, { BER_BVC( "bind" ), SLAP_RESTRICT_OP_BIND }, { BER_BVC( "compare" ), SLAP_RESTRICT_OP_COMPARE }, { BER_BVC( "delete" ), SLAP_RESTRICT_OP_DELETE }, { BER_BVC( "extended" ), SLAP_RESTRICT_OP_EXTENDED }, { BER_BVC( "modify" ), SLAP_RESTRICT_OP_MODIFY }, { BER_BVC( "rename" ), SLAP_RESTRICT_OP_RENAME }, { BER_BVC( "search" ), SLAP_RESTRICT_OP_SEARCH }, { BER_BVNULL, 0 }}, restricted_exops[] = { { BER_BVC( LDAP_EXOP_START_TLS ), SLAP_RESTRICT_EXOP_START_TLS }, { BER_BVC( LDAP_EXOP_MODIFY_PASSWD ), SLAP_RESTRICT_EXOP_MODIFY_PASSWD }, { BER_BVC( LDAP_EXOP_X_WHO_AM_I ), SLAP_RESTRICT_EXOP_WHOAMI }, { BER_BVC( LDAP_EXOP_X_CANCEL ), SLAP_RESTRICT_EXOP_CANCEL }, { BER_BVNULL, 0 }};static intinit_readOnly( monitor_info_t *mi, Entry *e, slap_mask_t restrictops ){ struct berval *tf = ( ( restrictops & SLAP_RESTRICT_OP_MASK ) == SLAP_RESTRICT_OP_WRITES ) ? (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv; return attr_merge_one( e, mi->mi_ad_readOnly, tf, tf );}static intinit_restrictedOperation( monitor_info_t *mi, Entry *e, slap_mask_t restrictops ){ int i, rc; for ( i = 0; restricted_ops[ i ].op.bv_val; i++ ) { if ( restrictops & restricted_ops[ i ].tag ) { rc = attr_merge_one( e, mi->mi_ad_restrictedOperation, &restricted_ops[ i ].op, &restricted_ops[ i ].op ); if ( rc ) { return rc; } } } for ( i = 0; restricted_exops[ i ].op.bv_val; i++ ) { if ( restrictops & restricted_exops[ i ].tag ) { rc = attr_merge_one( e, mi->mi_ad_restrictedOperation, &restricted_exops[ i ].op, &restricted_exops[ i ].op ); if ( rc ) { return rc; } } } return LDAP_SUCCESS;}intmonitor_subsys_database_init( BackendDB *be, monitor_subsys_t *ms){ monitor_info_t *mi; Entry *e_database, **ep; int i; monitor_entry_t *mp; monitor_subsys_t *ms_backend, *ms_overlay; assert( be != NULL ); ms->mss_modify = monitor_subsys_database_modify; mi = ( monitor_info_t * )be->be_private; ms_backend = monitor_back_get_subsys( SLAPD_MONITOR_BACKEND_NAME ); if ( ms_backend == NULL ) { Debug( LDAP_DEBUG_ANY, "monitor_subsys_database_init: " "unable to get " "\"" SLAPD_MONITOR_BACKEND_NAME "\" " "subsystem\n", 0, 0, 0 ); return -1; } ms_overlay = monitor_back_get_subsys( SLAPD_MONITOR_OVERLAY_NAME ); if ( ms_overlay == NULL ) { Debug( LDAP_DEBUG_ANY, "monitor_subsys_database_init: " "unable to get " "\"" SLAPD_MONITOR_OVERLAY_NAME "\" " "subsystem\n", 0, 0, 0 ); return -1; } if ( monitor_cache_get( mi, &ms->mss_ndn, &e_database ) ) { Debug( LDAP_DEBUG_ANY, "monitor_subsys_database_init: " "unable to get entry \"%s\"\n", ms->mss_ndn.bv_val, 0, 0 ); return( -1 ); } (void)init_readOnly( mi, e_database, frontendDB->be_restrictops ); (void)init_restrictedOperation( mi, e_database, frontendDB->be_restrictops ); mp = ( monitor_entry_t * )e_database->e_private; mp->mp_children = NULL; ep = &mp->mp_children; i = -1; LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) { char buf[ BACKMONITOR_BUFSIZE ]; int j; slap_overinfo *oi = NULL; BackendInfo *bi, *bi2; Entry *e; i++; bi = be->bd_info; if ( overlay_is_over( be ) ) { oi = (slap_overinfo *)be->bd_info->bi_private; bi = oi->oi_orig; } /* Subordinates are not exposed as their own naming context */ if ( SLAP_GLUE_SUBORDINATE( be ) ) { continue; } snprintf( buf, sizeof( buf ), "dn: cn=Database %d,%s\n" "objectClass: %s\n" "structuralObjectClass: %s\n" "cn: Database %d\n" "%s: %s\n" "%s: %s\n" "creatorsName: %s\n" "modifiersName: %s\n" "createTimestamp: %s\n" "modifyTimestamp: %s\n", i, ms->mss_dn.bv_val, mi->mi_oc_monitoredObject->soc_cname.bv_val, mi->mi_oc_monitoredObject->soc_cname.bv_val, i, mi->mi_ad_monitoredInfo->ad_cname.bv_val, bi->bi_type, mi->mi_ad_monitorIsShadow->ad_cname.bv_val, SLAP_SHADOW( be ) ? slap_true_bv.bv_val : slap_false_bv.bv_val, mi->mi_creatorsName.bv_val, mi->mi_creatorsName.bv_val, mi->mi_startTime.bv_val, mi->mi_startTime.bv_val ); e = str2entry( buf ); if ( e == NULL ) { Debug( LDAP_DEBUG_ANY, "monitor_subsys_database_init: " "unable to create entry \"cn=Database %d,%s\"\n", i, ms->mss_dn.bv_val, 0 ); return( -1 ); } if ( SLAP_MONITOR( be ) ) { attr_merge( e, slap_schema.si_ad_monitorContext, be->be_suffix, be->be_nsuffix ); attr_merge( e_database, slap_schema.si_ad_monitorContext, be->be_suffix, be->be_nsuffix ); } else { if ( be->be_suffix == NULL ) { Debug( LDAP_DEBUG_ANY, "monitor_subsys_database_init: " "missing suffix for database %d\n", i, 0, 0 ); return -1; } attr_merge( e, slap_schema.si_ad_namingContexts, be->be_suffix, be->be_nsuffix ); attr_merge( e_database, slap_schema.si_ad_namingContexts, be->be_suffix, be->be_nsuffix ); } (void)init_readOnly( mi, e, be->be_restrictops ); (void)init_restrictedOperation( mi, e, be->be_restrictops ); if ( SLAP_SHADOW( be ) && be->be_update_refs ) { attr_merge_normalize( e, mi->mi_ad_monitorUpdateRef, be->be_update_refs, NULL ); } if ( oi != NULL ) { slap_overinst *on = oi->oi_list, *on1 = on; for ( ; on; on = on->on_next ) { struct berval bv; slap_overinst *on2; for ( on2 = on1; on2 != on; on2 = on2->on_next ) { if ( on2->on_bi.bi_type == on->on_bi.bi_type ) { break; } } if ( on2 != on ) { break; } ber_str2bv( on->on_bi.bi_type, 0, 0, &bv ); attr_merge_normalize_one( e, mi->mi_ad_monitorOverlay, &bv, NULL ); /* find the overlay number, j */ for ( on2 = overlay_next( NULL ), j = 0; on2; on2 = overlay_next( on2 ), j++ ) { if ( on2->on_bi.bi_type == on->on_bi.bi_type ) { break; } } assert( on2 != NULL ); snprintf( buf, sizeof( buf ), "cn=Overlay %d,%s", j, ms_overlay->mss_dn.bv_val ); ber_str2bv( buf, 0, 0, &bv ); attr_merge_normalize_one( e, slap_schema.si_ad_seeAlso, &bv, NULL ); } } if ( 0 ) { assert( 0 );#if defined(SLAPD_BDB) || defined(SLAPD_HDB) } else if ( strcmp( bi->bi_type, "bdb" ) == 0 || strcmp( bi->bi_type, "hdb" ) == 0 ) { struct berval bv; ber_len_t pathlen = 0, len = 0; char path[ PATH_MAX ] = { '\0' }; struct bdb_info *bdb = (struct bdb_info *) be->be_private; char *fname = bdb->bi_dbenv_home; len = strlen( fname ); if ( fname[ 0 ] != '/' ) { /* get full path name */ getcwd( path, sizeof( path ) ); pathlen = strlen( path ); if ( fname[ 0 ] == '.' && fname[ 1 ] == '/' ) { fname += 2; len -= 2; } } bv.bv_len = STRLENOF( "file://" ) + pathlen + STRLENOF( "/" ) + len; bv.bv_val = ch_malloc( bv.bv_len + STRLENOF( "/" ) + 1 ); AC_MEMCPY( bv.bv_val, "file://", STRLENOF( "file://" ) ); if ( pathlen ) { AC_MEMCPY( &bv.bv_val[ STRLENOF( "file://" ) ], path, pathlen ); bv.bv_val[ STRLENOF( "file://" ) + pathlen ] = '/'; pathlen++; } AC_MEMCPY( &bv.bv_val[ STRLENOF( "file://" ) + pathlen ], fname, len ); if ( bv.bv_val[ bv.bv_len - 1 ] != '/' ) { bv.bv_val[ bv.bv_len ] = '/'; bv.bv_len++; } bv.bv_val[ bv.bv_len ] = '\0'; attr_merge_normalize_one( e, slap_schema.si_ad_labeledURI, &bv, NULL ); ch_free( bv.bv_val );#endif /* defined(SLAPD_BDB) || defined(SLAPD_HDB) */#if defined(SLAPD_LDAP) } else if ( strcmp( bi->bi_type, "ldap" ) == 0 ) { ldapinfo_t *li = (ldapinfo_t *)be->be_private;#if 0 attr_merge_normalize( e, slap_schema.si_ad_labeledURI, li->li_bvuri, NULL );#else char **urls = ldap_str2charray( li->li_uri, " " ); int u; for ( u = 0; urls[ u ] != NULL; u++ ) { struct berval bv; ber_str2bv( urls[ u ], 0, 0, &bv ); attr_merge_normalize_one( e, slap_schema.si_ad_labeledURI, &bv, NULL ); } ldap_charray_free( urls );#endif#endif /* defined(SLAPD_LDAP) */#if defined(SLAPD_META) } else if ( strcmp( bi->bi_type, "meta" ) == 0 ) { metainfo_t *mi = (metainfo_t *)be->be_private; int t; for ( t = 0; t < mi->mi_ntargets; t++ ) { char **urls = ldap_str2charray( mi->mi_targets[ t ]->mt_uri, " " ); int u; for ( u = 0; urls[ u ] != NULL; u++ ) { struct berval bv; ber_str2bv( urls[ u ], 0, 0, &bv ); attr_merge_normalize_one( e, slap_schema.si_ad_labeledURI, &bv, NULL ); } ldap_charray_free( urls ); }#endif /* defined(SLAPD_META) */ } j = -1; LDAP_STAILQ_FOREACH( bi2, &backendInfo, bi_next ) { j++; if ( bi2->bi_type == bi->bi_type ) { struct berval bv; snprintf( buf, sizeof( buf ), "cn=Backend %d,%s", j, ms_backend->mss_dn.bv_val ); bv.bv_val = buf; bv.bv_len = strlen( buf ); attr_merge_normalize_one( e, slap_schema.si_ad_seeAlso, &bv, NULL ); break; } } /* we must find it! */ assert( j >= 0 ); mp = monitor_entrypriv_create(); if ( mp == NULL ) { return -1; } e->e_private = ( void * )mp; mp->mp_info = ms; mp->mp_flags = ms->mss_flags | MONITOR_F_SUB; if ( monitor_cache_add( mi, e ) ) { Debug( LDAP_DEBUG_ANY, "monitor_subsys_database_init: " "unable to add entry \"cn=Database %d,%s\"\n", i, ms->mss_dn.bv_val, 0 ); return( -1 ); }#if defined(LDAP_SLAPI) monitor_back_add_plugin( mi, be, e );#endif /* defined(LDAP_SLAPI) */ if ( oi != NULL ) { Entry **ep_overlay = &mp->mp_children; monitor_entry_t *mp_overlay; slap_overinst *on = oi->oi_list; int o; for ( o = 0; on; o++, on = on->on_next ) { Entry *e_overlay; slap_overinst *on2; /* find the overlay number, j */ for ( on2 = overlay_next( NULL ), j = 0; on2; on2 = overlay_next( on2 ), j++ ) { if ( on2->on_bi.bi_type == on->on_bi.bi_type ) { break; } } assert( on2 != NULL ); snprintf( buf, sizeof( buf ), "dn: cn=Overlay %d,cn=Database %d,%s\n" "objectClass: %s\n" "structuralObjectClass: %s\n" "cn: Overlay %d\n" "%s: %s\n" "seeAlso: cn=Overlay %d,%s\n" "creatorsName: %s\n" "modifiersName: %s\n" "createTimestamp: %s\n"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -