Solved

List ACL

Posted on 2014-12-11
1
85 Views
Last Modified: 2015-01-03
Ive been asked to audit a server with regards to permissions to files and folders, so Ive come up with the idea of using C# to loop through the files and folders and write to a file what permissions each file/folder has.

So looking at the ACL documentation I can use the following code:-
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;

namespace ACL
{
    class Program
    {
        static void Main(string[] args) {
            getACLInformation("C:\\Rob\\account.sbd");

        }

        static void getACLInformation(string strFile) {
            FileInfo fInfo = new FileInfo(strFile);
            FileSecurity fSecurity = fInfo.GetAccessControl();
        }
    }
}

Open in new window


However when I look at fSecurity I cannot find out how to get a list of the users/groups and then what permissions they have and if its inherited or not.

Does anyone have any links to documentation or sample code of getting ACL information so I can dump it to a text file for import into a database at a later time for reporting?

Thank you
0
Comment
Question by:tonelm54
1 Comment
 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 40494985
One of the many reasons why I love extension methods; the following code -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;

namespace EE_Q28579222
{
	class Program
	{
		static void Main(string[] args)
		{
			@"C:\_admin\test.xml".ListFileACLs();
			FileInfo file = new FileInfo(@"C:\_admin\test2.xml");
			file.ListFileACLs();
			Console.ReadLine();
		}
	}

	static class Extensions
	{
		public static void ListDirectoryACLs(this string directory)
		{
			ListDirectoryACLs(new DirectoryInfo(directory));
		}

		public static void ListDirectoryACLs(this DirectoryInfo directory)
		{
			if (directory.Exists)
			{
				DirectorySecurity acls = new DirectorySecurity(directory.FullName, AccessControlSections.Access);
				StringBuilder sb = new StringBuilder();

				sb.AppendFormat("Access Control List for Directory - {0}", directory.FullName).AppendLine();
				foreach (FileSystemAccessRule rule in acls.GetAccessRules(true, true, typeof(NTAccount)))
				{
					sb.AppendFormat("Account:     {0}", rule.IdentityReference.Value).AppendLine();
					sb.AppendFormat("Type:        {0}", rule.AccessControlType).AppendLine();
					sb.AppendFormat("Rights:      {0}", rule.FileSystemRights).AppendLine();
					sb.AppendFormat("Inherited:   {0}", rule.IsInherited).AppendLine();
					sb.AppendFormat("Inheritance: {0}", rule.InheritanceFlags).AppendLine();
					sb.AppendFormat("Propagation: {0}", rule.PropagationFlags).AppendLine();
					sb.AppendLine(new string('-', 25));
				}
				Console.WriteLine(sb.ToString());
			}
		}

		public static void ListFileACLs(this string file)
		{
			ListFileACLs(new FileInfo(file));
		}

		public static void ListFileACLs(this FileInfo file)
		{
			if (file.Exists)
			{
				FileSecurity acls = new FileSecurity(file.FullName, AccessControlSections.Access);
				StringBuilder sb = new StringBuilder();

				sb.AppendFormat("Access Control List for File - {0}", file.FullName).AppendLine();
				foreach (FileSystemAccessRule rule in acls.GetAccessRules(true, true, typeof(NTAccount)))
				{
					sb.AppendFormat("Account:     {0}", rule.IdentityReference.Value).AppendLine();
					sb.AppendFormat("Type:        {0}", rule.AccessControlType).AppendLine();
					sb.AppendFormat("Rights:      {0}", rule.FileSystemRights).AppendLine();
					sb.AppendFormat("Inherited:   {0}", rule.IsInherited).AppendLine();
					sb.AppendFormat("Inheritance: {0}", rule.InheritanceFlags).AppendLine();
					sb.AppendFormat("Propagation: {0}", rule.PropagationFlags).AppendLine();
					sb.AppendLine(new string('-', 25));
				}
				Console.WriteLine(sb.ToString());
			}
		}
	}
}

Open in new window


Provides the following output -Capture.JPG
-saige-
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now