hBifTs

山自高兮水自深!當塵霧消散,唯事實留傳.荣辱不惊, 看庭前花开花落; 去留随意, 望天上云展云舒.
posts - 82, comments - 437, trackbacks - 38, articles - 27

创建一个EveryOne SECURITY_ATTRIBUTES.

Posted on 2004-07-12 21:29 hbifts 阅读(4564) 评论(13)  编辑 收藏 网摘 所属分类: .NET

在前面的内存映射文件与用户权限 :) 文章中,我提到了使用IIS的身份模拟来使用通过SharedMemoryEx生成的内存映射文件...

这种做法可以解决一时的问题,不能真正长久的解决问题.
在前一个文章MutexEx 中,Mutex的创建,使用也要对其权限进行设置..同样的,如果我们想在不同的帐号使用这个Mutex,我们可能也不得不使用另一种方式的身份模拟.

在Win32中,我们可以通过创建 NULL DACL来创建一个Everyone的ACL列表,通过这个列表,我们就可以创建一个对系统的Everyone都可以访问/使用的内存映射文件/Mutex了:)

在Win32中,我们要创建这样的一结构,代码通常是下面这样:

    SECURITY_ATTRIBUTES sa;
    SECURITY_DESCRIPTOR sd;

    InitializeSecurityDescriptor(
&sd,SECURITY_DESCRIPTOR_REVISION);
    SetSecurityDescriptorDacl(
&sd,TRUE,NULL,FALSE);
    sa.nLength 
= sizeof(SECURITY_ATTRIBUTES);
    sa.bInheritHandle 
= TRUE;
    sa.lpSecurityDescriptor 
= &sd;
    
return &sa;


在.NET中,我们同样要创建这样一个结构,我们先得通过P/Invoke定义相应的结构..
对于SECURITY_ATTRIBUTES:

    [StructLayout(LayoutKind.Sequential)]
    
public struct SECURITY_ATTRIBUTES
    
{
        
public uint nLength;
        
public IntPtr lpSecurityDescriptor; // point to SECURITY_DESCRIPTOR
        public int bInheritHandle;
    }

对于.SECURITY_DESCRIPTOR:
    [StructLayout(LayoutKind.Sequential)]
    
public struct SECURITY_DESCRIPTOR
    
{
        
public byte Revision;
        
public byte Sbz1;
        
public int Control;   // point to SECURITY_DESCRIPTOR_CONTROL  typedef WORD   SECURITY_DESCRIPTOR_CONTROL
        public IntPtr Owner;  // point to PSID   typedef PVOID PSID
        public IntPtr Group;  // point to PSID   typedef PVOID PSID
        public IntPtr Sacl;   // point to ACL
        public IntPtr Dacl;      // point to ACL
    }

由于SECURITY_DESCRIPTOR结构的定义使用到了其它的一些结构,这里,我们也一并定义出来:
    [StructLayout(LayoutKind.Sequential)]
    
public struct ACL
    
{
        
public byte AclRevision;
        
public byte Sbz1;
        
public uint AclSize;
        
public uint AceCount;
        
public uint Sbz2;
    }


结构定义好了,准备工作做好了一半了:)
下面就是定义我们要的函数了:)
        private const int SECURITY_DESCRIPTOR_REVISION = 1;

        [DllImport(
"advapi32.dll", SetLastError=true)]
        
static extern bool InitializeSecurityDescriptor(IntPtr pSecurityDescriptor, uint dwRevision);

        [DllImport(
"advapi32.dll", SetLastError=true)]
        
static extern bool SetSecurityDescriptorDacl( IntPtr pSecurityDescriptor,int bDaclPresent,IntPtr pDacl,int bDaclDefaulted);

由于InitializeSecurityDescriptor函数的第二个参数必需是1,所以定义了一个const int SECURITY_DESCRIPTOR_REVISION来表明..

下面的就是使用代码了.
        public IntPtr AnonymousSA(bool bInheritHandle)
        
{
            

            SECURITY_ATTRIBUTES sa 
= new SECURITY_ATTRIBUTES();
            SECURITY_DESCRIPTOR sd 
= new SECURITY_DESCRIPTOR();

            lpSecurityDescriptor 
= Marshal.AllocCoTaskMem( Marshal.SizeOf( sd));
            Marshal.StructureToPtr( sd,lpSecurityDescriptor,
false);

            
if! InitializeSecurityDescriptor( lpSecurityDescriptor ,SECURITY_DESCRIPTOR_REVISION))
            
{
                
throw new ApplicationException("InitializeSecurityDescriptor invoked fail");
            }


            
if! SetSecurityDescriptorDacl( lpSecurityDescriptor ,1,IntPtr.Zero,0))
            
{
                
throw new ApplicationException("SetSecurityDescriptorDacl invoked fail");
            }


            sa.bInheritHandle 
= bInheritHandle?1:0;
            sa.nLength 
= (uint)Marshal.SizeOf( sa );

            sa.lpSecurityDescriptor 
= lpSecurityDescriptor;

            pSa 
= Marshal.AllocCoTaskMem( Marshal.SizeOf( sa));
            Marshal.StructureToPtr( sa,pSa,
false);
            
            
return pSa;
        }

考虑到这个函数的通用性,我将其定义成一个单独的类. SecurityStruct.用于创建这个NULL DACL结构.
同时,由于使用到了Marshal中的内存分配,是非托管内存,所以我把这个类从IDisposable继承,用于释放此内存..
public class SecurityStruct : IDisposable
    
{
        
Private variable & Propery


        
public void Close()
        
{
            
if( lpSecurityDescriptor != IntPtr.Zero)
            
{
                Marshal.FreeCoTaskMem( lpSecurityDescriptor);
                lpSecurityDescriptor 
= IntPtr.Zero;
            }

            
if( pSa != IntPtr.Zero)
            
{
                Marshal.FreeCoTaskMem( pSa);
                pSa 
= IntPtr.Zero;
            }

        }


        
public void Dispose()
        
{
            Close();        
        }

}


我们得到了创建了一这个Everyone的结构后,直接把其地址传给CreateFileMapping和CreateMutex的参数中就可以了.

这样,我们在ASP.NET,或是其它的用户,就可以很方便的使用这个MappingFile/Mutex了:)


完整代码下载: SecurityStruct.zip

Feedback

#1楼   回复  引用    

2004-07-12 21:41 by dudu
不错!研究得越来越深了.
这种不断深入地研究问题的精神值得学习.

#2楼   回复  引用    

2004-07-12 22:06 by hBifTs
呵呵:)

#3楼   回复  引用    

2004-07-13 21:49 by Ying-Shen
good!

#4楼   回复  引用    

2004-07-15 00:30 by Flier Lu
NULL DACL 和 Everyone DACL 是不同的,前者没有任何限制,后者好歹还限制必须是 SECURITY_WORLD_SID_AUTHORITY 的,呵呵

我把你关心的问题整理了一篇小文,你看看说清楚没有 :P

DACL, NULL or not NULL

http://www.cnblogs.com/flier/archive/2004/07/15/24299.aspx

#5楼   回复  引用    

2004-07-15 16:04 by hBifTs
Good.谢谢Flier Lu :)

#6楼   回复  引用    

2004-09-15 13:09 by 似的冻豆腐
WO  看不动啊

#7楼   回复  引用    

2005-08-18 03:40 by chen[未注册用户]
┊感情.其實ぺωǒブ輸不起 ╰→愛の情 →這ぐ東西いωǒ玩メ不起

#8楼   回复  引用    

2006-11-02 02:42 by 李东[未注册用户]
我要用来,装我重要是软件或下载文件.

#9楼   回复  引用    

2006-11-02 02:56 by 李东[未注册用户]
我用以存储电脑软件或下载文件,游戏等等



发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 23550




相关文章:

相关链接: