简体   繁体   English

如何创建具有授予每个人的所有权限的目录

[英]How to create directory with all rights granted to everyone

I need to programmatically create a directory that grants "Full Control" to the group "Everyone". 我需要以编程方式创建一个目录,为“Everyone”组授予“完全控制”权限。 If I use 如果我使用

CreateDirectory(path, NULL);

This will, according to the Win32 SDK documentation , create a directory that inherits from its parent directory. 根据Win32 SDK 文档 ,这将创建一个从其父目录继承的目录。 I do not want to inherit the access rights of the parent directory I need to ensure that "Everyone" has full control over the directory. 我不想继承我需要的父目录的访问权限,以确保“Everyone”完全控制目录。

Obviously, this will require setting up the SECURITY_ATTRIBUTES structure with the appropriate security descriptor. 显然,这需要使用适当的安全描述符设置SECURITY_ATTRIBUTES结构。 How do I do that? 我怎么做?

Here's one technique that seems to work: 这是一种似乎有效的技术:

SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY;
PSID everyone_sid = NULL;
AllocateAndInitializeSid(&SIDAuthWorld, 1, SECURITY_WORLD_RID, 
   0, 0, 0, 0, 0, 0, 0, &everyone_sid);

EXPLICIT_ACCESS ea;
ZeroMemory(&ea, sizeof(EXPLICIT_ACCESS));
ea.grfAccessPermissions = SPECIFIC_RIGHTS_ALL | STANDARD_RIGHTS_ALL;
ea.grfAccessMode = SET_ACCESS;
ea.grfInheritance = NO_INHERITANCE;
ea.Trustee.TrusteeForm = TRUSTEE_IS_SID;
ea.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
ea.Trustee.ptstrName  = (LPWSTR)everyone_sid;

PACL acl = NULL;
SetEntriesInAcl(1, &ea, NULL, &acl);

PSECURITY_DESCRIPTOR sd = (PSECURITY_DESCRIPTOR)LocalAlloc(LPTR, 
                                   SECURITY_DESCRIPTOR_MIN_LENGTH);
InitializeSecurityDescriptor(sd, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(sd, TRUE, acl, FALSE);

SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = sd;
sa.bInheritHandle = FALSE;

CreateDirectory(path, &sa);

FreeSid(everyone_sid);
LocalFree(sd);
LocalFree(acl);

Note that this sample code has absolutely no error checking -- you'll have to supply that yourself. 请注意,此示例代码绝对没有错误检查 - 您必须自己提供。

I prefer the following code snippet as it creates a folder inheriting default rights - which seems the right thing to do - other software / the user might have setup specific inheritable rights on a directory for a legitimate reason - then adds a Full Control explicit access entry for the built in "Users" group. 我更喜欢以下代码片段,因为它创建了一个继承默认权限的文件夹 - 这似乎是正确的事情 - 其他软件/用户可能出于合法原因在目录上设置了特定的可继承权限 - 然后添加一个完全控制显式访问条目用于内置的“用户”组。

BOOL CreateDirectoryWithUserFullControlACL(LPCTSTR lpPath)
{
  if(!CreateDirectory(lpPath,NULL))
    return FALSE;

  HANDLE hDir = CreateFile(lpPath,READ_CONTROL|WRITE_DAC,0,NULL,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS,NULL);
  if(hDir == INVALID_HANDLE_VALUE)
    return FALSE; 

  ACL* pOldDACL;
  SECURITY_DESCRIPTOR* pSD = NULL;
  GetSecurityInfo(hDir, SE_FILE_OBJECT , DACL_SECURITY_INFORMATION,NULL, NULL, &pOldDACL, NULL, (void**)&pSD);

  PSID pSid = NULL;
  SID_IDENTIFIER_AUTHORITY authNt = SECURITY_NT_AUTHORITY;
  AllocateAndInitializeSid(&authNt,2,SECURITY_BUILTIN_DOMAIN_RID,DOMAIN_ALIAS_RID_USERS,0,0,0,0,0,0,&pSid);

  EXPLICIT_ACCESS ea={0};
  ea.grfAccessMode = GRANT_ACCESS;
  ea.grfAccessPermissions = GENERIC_ALL;
  ea.grfInheritance = CONTAINER_INHERIT_ACE|OBJECT_INHERIT_ACE;
  ea.Trustee.TrusteeType = TRUSTEE_IS_GROUP;
  ea.Trustee.TrusteeForm = TRUSTEE_IS_SID;
  ea.Trustee.ptstrName = (LPTSTR)pSid;

  ACL* pNewDACL = 0;
  DWORD err = SetEntriesInAcl(1,&ea,pOldDACL,&pNewDACL);

  if(pNewDACL)
    SetSecurityInfo(hDir,SE_FILE_OBJECT,DACL_SECURITY_INFORMATION,NULL, NULL, pNewDACL, NULL);

  FreeSid(pSid);
  LocalFree(pNewDACL);
  LocalFree(pSD);
  LocalFree(pOldDACL);
  CloseHandle(hDir);

  return TRUE;
}

See if you can use SetSecurityInfo() 看看你是否可以使用SetSecurityInfo()

In the description of the optional pDacl argument: 在可选的pDacl参数的描述中:

... If the value of the SecurityInfo parameter includes the DACL-SECURITY-INFORMATION flag and the value of this parameter is set to NULL, full access to the object is granted to everyone. ...如果SecurityInfo参数的值包含DACL-SECURITY-INFORMATION标志,并且此参数的值设置为NULL,则会向所有人授予对该对象的完全访问权限。

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

相关问题 将“Everyone”组添加到目录及其所有子目录中 - Add group “Everyone” to directory and all of it's sub-directories 存储具有所有用户的读/写权限的应用程序数据的最佳目录? - Best directory to store application data with read\write rights for all users? 我在哪里可以创建没有管理员权限的目录? - Where can I create a directory without admin rights? 在哪里可以创建没有管理员权限/权限的目录? - Where can I create a directory without Administrator Rights / Privileges? 如何从C ++代码创建具有管理员权限的服务 - How to create a service with administrator rights from c++ code 如何在Windows中使用用户权限创建用于PDF打印的打印机? - How to create a Printer for PDF printing with user rights in windows? 使用 PowerShell 提升当前目录中的权限 - Elevate rights in current directory with PowerShell 如何创建批处理文件以运行同一目录中的所有python文件 - How to create a batch file to run all python files in the same directory 授予每个人读取文件的权限,但 SQL Server 无法批量插入 - granted read access to file to everyone, yet SQL Server cannot bulk insert 即使授予“所有人”访问权限,ASP.NET应用程序也无法访问网络共享 - ASP.NET application cannot access network share even with “Everyone” granted access
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM