Example #1
0
        public static void Main(string[] args)
        {
            Console.Out.WriteLine(string.Format("{0} {1}\r\n{2}", ((AssemblyTitleAttribute)Attribute.GetCustomAttribute(System.Reflection.Assembly.GetExecutingAssembly(), typeof(AssemblyTitleAttribute))).Title, Assembly.GetExecutingAssembly().GetName().Version, ((AssemblyCopyrightAttribute)Attribute.GetCustomAttribute(System.Reflection.Assembly.GetExecutingAssembly(), typeof(AssemblyCopyrightAttribute))).Copyright));

            if (args.Length < 2)
            {
                Console.Out.WriteLine("\r\nUsage: ACLMatrix.exe root_path output_file.xlsx [HideUsers] [HideGroups] [HideUnrecognized] [ShowAccountNames] [BypassACL] [CheckFiles]");
                return;
            }

            bool hideUsers        = false;
            bool hideGroups       = false;
            bool hideUnrecognized = false;
            bool addAccountNames  = false;
            bool bypassACL        = false;

            checkFiles = false;

            for (int i = 2; i < args.Length; i++)
            {
                if (args[i].Equals("hideusers", StringComparison.InvariantCultureIgnoreCase))
                {
                    hideUsers = true;
                }
                if (args[i].Equals("hidegroups", StringComparison.InvariantCultureIgnoreCase))
                {
                    hideGroups = true;
                }
                if (args[i].Equals("hideunrecognized", StringComparison.InvariantCultureIgnoreCase))
                {
                    hideUnrecognized = true;
                }
                if (args[i].Equals("showaccountnames", StringComparison.InvariantCultureIgnoreCase))
                {
                    addAccountNames = true;
                }
                if (args[i].Equals("bypassacl", StringComparison.InvariantCultureIgnoreCase))
                {
                    bypassACL = true;
                }
                if (args[i].Equals("checkfiles", StringComparison.InvariantCultureIgnoreCase))
                {
                    checkFiles = true;
                }
            }

            if (bypassACL)
            {
                if (TokenPrivileges.SetBackupPrivilege())
                {
                    Console.Out.WriteLine("\r\nBypassACL: SUCCESS");
                }
                else
                {
                    Console.Out.WriteLine("\r\nBypassACL: FAILURE");
                }
            }

            groupsCache   = new GroupsCache();
            subjectsCache = new SubjectsCache();

            Console.Out.WriteLine();

            toHere();
            to("Evaluating: ");
            toHere();

            List <PathACL> acls = new List <PathACL>();

            try
            {
                machinePrincipalContext = new PrincipalContext(ContextType.Machine);
            }
            catch
            {
                Console.Out.WriteLine("\r\nInternal error $01.");
                return;
            }

            try
            {
                domainPrincipalContext = new PrincipalContext(ContextType.Domain);
            }
            catch
            {
                domainPrincipalContext = machinePrincipalContext;
            }

            try
            {
                getTreeAcl(acls, args[0], null);
            }
            catch
            {
                Console.Out.WriteLine("\r\nInternal error $02.");
                return;
            }

            List <Subject> subjects = new List <Subject>();

            foreach (PathACL pathACL in acls)
            {
                List <Subject> pathSubjects = pathACL.AllSubjects;

                foreach (Subject subject in pathSubjects)
                {
                    if (!subjects.Contains(subject))
                    {
                        switch (subject.PrincipalType)
                        {
                        case 'G':
                            if (!hideGroups)
                            {
                                subjects.Add(subject);
                            }
                            break;

                        case 'U':
                            if (!hideUsers)
                            {
                                subjects.Add(subject);
                            }
                            break;

                        default:
                            if (!hideUnrecognized)
                            {
                                subjects.Add(subject);
                            }
                            break;
                        }
                    }
                }
            }

            subjects.Sort();

            to("done.");

            Console.Out.WriteLine();

            toHere();
            to("Exporting: ");
            toHere();
            to("0%");

            if (subjects.Count > 50000)
            {
                to("Error: too many users!");
                return;
            }

            if (acls.Count > 1000000)
            {
                to("Error: too many folders!");
                return;
            }

            ExcelPackage   excelPackage = new ExcelPackage();
            ExcelWorksheet ws           = excelPackage.Workbook.Worksheets.Add("ACLMatrix");

            for (int i = 0; i < acls.Count; i++)
            {
                ws.Cells[i + 2, 1].Value = acls[i].Path;
                ws.Cells[i + 2, 1].Style.Border.BorderAround(OfficeOpenXml.Style.ExcelBorderStyle.Thin);
                ws.Cells[i + 2, 1].Style.Font.Bold        = true;
                ws.Cells[i + 2, 1].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
                ws.Cells[i + 2, 1].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightSteelBlue);
            }

            for (int j = 0; j < subjects.Count; j++)
            {
                ws.Cells[1, j + 2].Value = subjects[j].PrincipalType + ": " + subjects[j].DisplayName + (addAccountNames ? " (" + subjects[j].AccountName + ")" : "");
                ws.Cells[1, j + 2].Style.TextRotation = 90;
                ws.Cells[1, j + 2].Style.Border.BorderAround(OfficeOpenXml.Style.ExcelBorderStyle.Thin);
                ws.Cells[1, j + 2].Style.Font.Bold        = true;
                ws.Cells[1, j + 2].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
                ws.Cells[1, j + 2].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightSteelBlue);
                ws.Column(j + 2).Width = 3;
            }

            for (int i = 0; i < acls.Count; i++)
            {
                for (int j = 0; j < subjects.Count; j++)
                {
                    AccessRights ar = acls[i][subjects[j]];

                    if (ar != null)
                    {
                        string str = ar.ToString();

                        ws.Cells[i + 2, j + 2].Value = str;
                        ws.Cells[i + 2, j + 2].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
                        ws.Cells[i + 2, j + 2].Style.Fill.BackgroundColor.SetColor(ar.Color);
                        ws.Cells[i + 2, j + 2].Style.Font.Color.SetColor(ar.Color);
                        ws.Cells[i + 2, j + 2].Style.WrapText    = false;
                        ws.Cells[i + 2, j + 2].Style.ShrinkToFit = true;
                    }

                    ws.Cells[i + 2, j + 2].Style.Border.BorderAround(OfficeOpenXml.Style.ExcelBorderStyle.Thin);

                    to(string.Format("{0}%", (int)(((double)(i * subjects.Count + j)) / ((double)(acls.Count * subjects.Count)) * 100.0)));
                }
            }

            ws.Row(1).Height   = 200;
            ws.Column(1).Width = 100;
            ws.View.FreezePanes(2, 2);

            using (FileStream fs = new FileStream(args[1], FileMode.Create, FileAccess.ReadWrite))
            {
                excelPackage.SaveAs(fs);
            }

            to("done.");
        }