المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Enumerating directories/shares (and their Permissions) [modified]



C# Programming
05-18-2011, 10:22 PM
Hi,

I need to enumerate directories/shares (and their permissions), so far, I found two appraches:
OPTION 1: WMI (http://msdn.microsoft.com/en-us/library/aa394084(v=VS.85).aspx)[^ (http://msdn.microsoft.com/en-us/library/aa394084(v=VS.85).aspx)]
- advantage: You can query remote computers if you have the permission to... (http://www.csharphelp.com/2006/10/wmi-made-easy-for-c/)[^ (http://www.csharphelp.com/2006/10/wmi-made-easy-for-c/)]
- limitation is that: (a) Don't give local path, (b) I need "Permission", noth NTFS+Share, which WMI object don't provide.
This said, I did came across Win32_SecuritySettingOfObject (http://include.wutils.com/wmi/ROOT%5Ccimv2/CIM_ElementSetting/Win32_SecuritySettingOfObject.html)[^ (http://include.wutils.com/wmi/ROOT%5Ccimv2/CIM_ElementSetting/Win32_SecuritySettingOfObject.html)], but don't know how to use it. And still trying Win32_SecuritySetting (But samples are so hard to decipher, for example, ControlFlags = 32772. This means absolutely nothing to me) (http://include.wutils.com/wmi/ROOT%5Ccimv2/CIM_Setting/Win32_SecuritySetting/cs-samples.html)[^ (http://include.wutils.com/wmi/ROOT%5Ccimv2/CIM_Setting/Win32_SecuritySetting/cs-samples.html)] - how to get list of users/groups assigned to access to share folder? (http://msdn.microsoft.com/en-us/library/aa394404(v=vs.85).aspx#properties)[^ (http://msdn.microsoft.com/en-us/library/aa394404(v=vs.85).aspx#properties)]

OPTION 2: DllImport win32 API - WNetGetUniversalName and NetShareEnum (http://www.codeproject.com/KB/IP/networkshares.aspx)[^ (http://www.codeproject.com/KB/IP/networkshares.aspx)]
- advantage: gives local path of identified shares and also able to scan both local/remote shares
- limitation: Option 2 actually gives local path, but nothing on directory/share "Permission".
note SHARE_INFO_2 has an "Permission (int)" field, but after examining further doesn't appear this is what I'm looking for. Also examined new System.IO.DirectoryInfo(share.Path) --- nothing useful from there.
Any suggestion guys? I need permissions - both NTFS+Share... (Basically same as right click folder, select "Properties", look under "Security" tab and also "Sharing\Permissions")

Thanks!


OPTION 1 - sample code:


public static IList EnumerateLocalShares(ShareCheckingMode Mode)
{
IList Shares = new List();
SelectQuery query = null;
ManagementObjectSearcher searcher = null;

switch (Mode)
{
case ShareCheckingMode.Win32_LogicalShareAccess:
query = new SelectQuery("SELECT * FROM Win32_LogicalShareAccess");
break;
case ShareCheckingMode.Win32_LogicalShareAuditing:
query = new SelectQuery("SELECT * FROM Win32_LogicalShareAuditing");
break;
case ShareCheckingMode.Win32_LogicalShareSecuritySetting:
query = new SelectQuery("SELECT * FROM Win32_LogicalShareSecuritySetting");
break;
}

searcher = new ManagementObjectSearcher(query);
foreach (ManagementBaseObject share in searcher.Get())
{
Console.WriteLine("Share: " + share.ToString());
foreach (PropertyData prop in share.Properties)
{
Console.WriteLine("Property - " + prop.Name + ": " + prop.Value);
}
foreach (PropertyData prop in share.SystemProperties)
{
Console.WriteLine("SystemProperty - " + prop.Name + ": " + prop.Value);
}
foreach (QualifierData Qualifier in share.Qualifiers)
{
Console.WriteLine("Qualifier - " + Qualifier.Name + ": " + Qualifier.Value);
}
Console.WriteLine();
Console.WriteLine();
}

return Shares;
}


For NTFS folder permission (Not "Share" permission) (http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_21940372.html)[^ (http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_21940372.html)]

public static FileSystemAccessRule GetDirectoryPermissions(string user, string domainName, string folderPath)
{
if (!Directory.Exists(folderPath))
{
return (null);
}

string identityReference = ((domainName + @"\" + user) as string).ToLower();
DirectorySecurity dirSecurity = Directory.GetAccessControl(folderPath, AccessControlSections.All);
foreach (FileSystemAccessRule fsRule in dirSecurity.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)))
{
if (fsRule.IdentityReference.Value.ToLower() == identityReference)
{
return (fsRule);
}
}
return (null);
}

dev
modified on Wednesday, May 18, 2011 6:12 AM