简体   繁体   English

SQL Server 2008 R2跨数据库所有权链接不起作用?

[英]SQL Server 2008 R2 Cross Database Ownership Chaining Not working?

I have restored two SQL Server 2005 dbs (DB1 & DB2) to a new box running SQL Server 2008 R2. 我已将两个SQL Server 2005数据库(DB1和DB2)还原到运行SQL Server 2008 R2的新盒子中。

All objects are owned by dbo 所有对象均归dbo所有

I have a stored procedure DB1.dbo.mp_SPTest . 我有一个存储过程DB1.dbo.mp_SPTest I have given execute permissions to SQLUser1 . 我已授予SQLUser1执行权限。

CREATE PROCEDURE mp_SPTest
AS
SELECT DB2.dbo.mf_UserHasAccess("BasicUser", "bob")

mp_SPTest calls a scalar function in DB2 DB2.dbo.mf_UserHasAccess() , this function checks if the username passed is a member of a SQL Role..... mp_SPTest在DB2 DB2.dbo.mf_UserHasAccess()调用一个标量函数,此函数检查传递的用户名是否是SQL角色的成员。

CREATE FUNCTION [dbo].[mf_UserHasAccess] (@RoleName varchar(50), @UserName varchar(128))  
RETURNS bit
AS  
BEGIN 
    DECLARE @Result bit

    SELECT @Result = 1
    WHERE @RoleName IN (
    SELECT CASE
        WHEN (usg.uid is null) THEN 'public'
        ELSE usg.name
        END AS RoleName
    FROM    dbo.sysusers usu
    LEFT OUTER JOIN (dbo.sysmembers mem 
         INNER JOIN dbo.sysusers usg 
         ON mem.groupuid = usg.uid)
    ON  usu.uid = mem.memberuid
    LEFT OUTER JOIN master.dbo.syslogins lo 
    ON  usu.sid = lo.sid
    WHERE   
        (usu.islogin = 1 AND usu.isaliased = 0 AND usu.hasdbaccess = 1)
    AND (usg.issqlrole = 1 OR usg.uid is NULL)
    AND usu.name = @UserName)

    IF @Result <> 1
    BEGIN
        SET @Result = 0
    END

    RETURN @Result
END

When I run this procedure as "SQLUser1" it tells me that bob is not a member of BasicUser but when I run it as "sa" it tells me that he IS a member. 当我以“ SQLUser1”运行此过程时,它告诉我bob不是BasicUser的成员,但是当我以“ sa”运行它时,则告诉我他是BasicUser的成员。

As I understand it... because both procedure and function are owned by dbo then that is the context that the function in test2 db would run, therefore it should have access to the same user and login tables. 据我了解...由于dbo拥有过程和函数,因此test2 db中的函数将运行,因此它应该有权访问相同的用户和登录表。

This worked fine on SQL Server 2005, cant figure it out. 这在SQL Server 2005上运行良好,无法解决。

Hope this makes sense, thanks in advance. 希望这有意义,在此先感谢。

Most likely the old SQL Server 2005 had the cross db ownership chaining option turned on, while the new SQL Server 2008 R2 instance has the option left at its default value (off). 很可能旧的SQL Server 2005启用了跨数据库所有权链接选项,而新的SQL Server 2008 R2实例将选项保留为默认值(关闭)。

But your assumption that 'dbo' in DB1 equate to 'dbo' in DB2 is wrong. 但是您认为DB1中的“ dbo”等于DB2中的“ dbo”的假设是错误的。 'dbo' in DB1 is the login who corresponds to the owner_sid of DB1 in sys.databases . DB1中的'dbo'是与sys.databases中DB1的owner_sid相对应的登录名。 'dbo' in DB2 is, likewise, the login that corrsponds to the onwer_sid in sys.databases for DB2. 同样,DB2中的“ dbo”是与DB2的sys.databases中的onwer_sid相对应的登录名。 If the two logins are different (if the owner_sid of the two databases is different) then very likely 'dbo' of DB1 will map to some other user and the ownership chain is broken, even if enabled to cross databases. 如果两个登录名不同(如果两个数据库的owner_sid不同),那么即使启用跨数据库访问,DB1的“ dbo”很有可能会映射到其他用户,并且所有权链断开。 Running ALTER AUTHORIZATION ON DATABASE::[DB..] TO [sa] would fix this problem (ie. it would force the owner_sid to match). ALTER AUTHORIZATION ON DATABASE::[DB..] TO [sa]运行ALTER AUTHORIZATION ON DATABASE::[DB..] TO [sa]将解决此问题(即,它将迫使owner_sid匹配)。

And finally, what you're doing is fundamentally flawed, as it relies on activating ownership chaining across databases, which is a huge security hole, see Potential Threats . 最后,您所做的工作从根本上来说是有缺陷的,因为它依赖于激活跨数据库的所有权链,这是一个巨大的安全漏洞,请参阅“ 潜在威胁” A much better avenue is to use code signing . 更好的方法是使用代码签名

I solved this identical problem by making the schema the view or procedure is running under the owner of the schemas in the databases it needs to access. 我通过使架构在需要访问的数据库中的架构所有者下运行视图或过程来解决了相同的问题。

USE [TargetDB] USE [TargetDB]

ALTER AUTHORIZATION ON SCHEMA::[TargetSchema] TO [SourceSchema] 更改架构:: [TargetSchema]到[SourceSchema]

For example USE [DB1] 例如USE [DB1]

ALTER AUTHORIZATION ON SCHEMA::[mem] TO [dbo] GO SCHEMA上的更改授权:: [mem]到[dbo] GO

Would allow a view run as DB2.DBO.view assuming chaining is turned on, to access a table in DB1.mem.table. 假设启用了链接,将允许以DB2.DBO.view的身份运行的视图访问DB1.mem.table中的表。 Essentially cross db chaining causes it to access the target DB AS the schema the view is under, not the user who owns the database. 本质上,跨数据库链接使它可以访问视图所在的模式的目标数据库,而不是拥有数据库的用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM