static void getTreeAcl(List <PathACL> acl, string path, PathACL parentACL) { PathACL pathACL = getPathACL(path, false); if ((pathACL != null) && (parentACL == null || !pathACL.Equals(parentACL))) { acl.Add(pathACL); } if (checkFiles) { try { foreach (string filePath in Directory.GetFiles(path)) { PathACL fileACL = getPathACL(filePath, true); if (fileACL != null && !fileACL.Equals(pathACL)) { acl.Add(fileACL); } } } catch { } } try { foreach (string subPath in Directory.GetDirectories(path)) { getTreeAcl(acl, subPath, pathACL); } } catch { } }
private static void addPrincipalACL(PathACL pathACL, FileSystemAccessRule ar) { Subject subject = subjectsCache[ar.IdentityReference.Value]; if (subject == null) { Principal principal = null; try { principal = Principal.FindByIdentity(ar.IdentityReference.Value.StartsWith("BUILTIN\\") ? machinePrincipalContext : domainPrincipalContext, ar.IdentityReference.Value); } catch { } if (principal == null && machinePrincipalContext != domainPrincipalContext) { try { principal = Principal.FindByIdentity(machinePrincipalContext, ar.IdentityReference.Value); } catch { } } if (principal == null) { subject = new Subject(ar.IdentityReference.Value); } else { if (principal is UserPrincipal) { subject = new Subject(principal as UserPrincipal); } if (principal is GroupPrincipal) { subject = new Subject(principal as GroupPrincipal); } } } if (subject.PrincipalType == 'G') { groupsCache[subject.SubjectPrincipal as GroupPrincipal].ForEach(u => { subjectsCache.Add(ar.IdentityReference.Value, subject); pathACL.AddEntry(u, ar.AccessControlType, ar.FileSystemRights); }); } else { subjectsCache.Add(ar.IdentityReference.Value, subject); pathACL.AddEntry(subject, ar.AccessControlType, ar.FileSystemRights); } }
private static PathACL getPathACL(string path, bool isFile) { to(path); PathACL pathACL = new PathACL(path); try { AuthorizationRuleCollection arc = (isFile ? new FileSecurity(path, AccessControlSections.Access).GetAccessRules(true, true, typeof(NTAccount)) : new DirectorySecurity(path, AccessControlSections.Access).GetAccessRules(true, true, typeof(NTAccount))); foreach (FileSystemAccessRule ar in arc) { addPrincipalACL(pathACL, ar); } } catch { } return(pathACL); }