示例#1
0
        static void Main(string[] args)
        {
            //Console.WriteLine(WindowsPrincipal.Current.Identity.Name);
            //var currentIdentity = WindowsIdentity.GetCurrent();
            //Console.WriteLine($"Current identity: {currentIdentity.NameClaimType} - token: {currentIdentity.Token}");
            //Console.ReadLine();

            Parser.Default.ParseArguments <Option>(args)
            .WithParsed <Option>(opt =>
            {
                var ownershipTake = new OwnershipTaker();
                //var identity = new NTAccount(Environment.UserName); // doesn't seems to work
                var identity = new NTAccount(Environment.UserDomainName, string.IsNullOrWhiteSpace(opt.Owner) ? Environment.UserName : opt.Owner);
                Console.WriteLine($"Setting new file owner to: {identity.Value}");

                // create context
                var context = new TakeOwnershipContext
                {
                    Options = opt,
                    Owner   = identity,
                };

                var ownerShipTaker = new OwnershipTaker();
                ownerShipTaker.TakeOwnerShip(opt.Target, context);
                Console.WriteLine("Done...");

                //System.Security.Principal.WindowsIdentity
            });
        }
示例#2
0
        public void TakeOwnerShip(string path, TakeOwnershipContext context)
        {
            if (!(context.Options.File || context.Options.Directory))
            {
                return;
            }

            var securityId = typeof(SecurityIdentifier);

            foreach (var directory in Directory.GetDirectories(path))
            {
                // child directories must be processed aswell
                if (context.Options.IsRecursive)
                {
                    Task.Run(() =>
                    {
                        TakeOwnerShip(directory, context);
                    }).ConfigureAwait(false);
                }

                // allow set directory ownership
                if (context.Options.Directory)
                {
                    // get dir ownership
                    var dirAccessCtrl = Directory.GetAccessControl(directory);

                    if (context.Options.PurgeAllOtherAccess)
                    {
                        dirAccessCtrl.PurgeAllAccess <SecurityIdentifier>();
                    }

                    // set new owner
                    dirAccessCtrl.SetOwner(context.Owner);

                    try
                    {
                        // set access rule async
                        Directory.SetAccessControl(directory, dirAccessCtrl);
                        Debug.WriteLine($"Access set to {Path.GetFileName(directory)}");
                    }
                    catch (UnauthorizedAccessException uae)
                    {
                        Debug.WriteLine(uae.Message);
#if !DEBUG
                        throw uae;
#endif
                    }
                }
            }

            // take file ownership
            if (context.Options.File)
            {
                // get all file in current directory
                foreach (string file in Directory.GetFiles(path, "*.*"))
                {
                    var fileAccessCtrl = File.GetAccessControl(file);

                    // remove all other owners of type SecurityIdentifier access
                    if (context.Options.PurgeAllOtherAccess)
                    {
                        fileAccessCtrl.PurgeAllAccess <SecurityIdentifier>();
                    }

                    fileAccessCtrl.SetOwner(context.Owner);

                    // set access rule async
                    try
                    {
                        File.SetAccessControl(file, fileAccessCtrl);
                        Debug.WriteLine($"Access set to {Path.GetFileName(file)}");
                    }
                    catch (UnauthorizedAccessException uae)
                    {
                        // notify the debug listener
                        Debug.WriteLine(uae.Message);
#if !DEBUG
                        throw uae;
#endif
                    }
                }
            }
        }