Skip to content

Commit

Permalink
Add ownership fix
Browse files Browse the repository at this point in the history
  • Loading branch information
countzero committed Mar 16, 2021
1 parent f0269b5 commit 0916418
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ The best practice configuration for the personal .ssh directory is to restrict a
This PowerShell script automatically repairs the permissions of the .ssh directory and files. It does the following for all items within and including the given .ssh directory:

* Disables inheritance
* Sets owenership to one user
* Removes all permissions
* Grants one user full access
* Grants one user full control

## Installation
Download and unpack the [latest release](https://github.com/countzero/repair_ssh_permissions/releases/latest) to your machine.
Expand Down
15 changes: 14 additions & 1 deletion repair_ssh_permissions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ function Disable-Inheritance([String] $item) {
Set-Acl -Path $item -AclObject $acl
}

function Set-UserOwnership([String] $item) {

Write-Host "Let '$user' own '${item}'..." -ForegroundColor "DarkYellow"

$acl = Get-Acl -Path $item

$userAccount = New-Object System.Security.Principal.NTAccount($user)

$acl.SetOwner($userAccount)

Set-Acl -Path $item -AclObject $acl
}

function Remove-AllAccessPermissions([String] $item) {

Write-Host "Removing all access permissions on '${item}'..." -ForegroundColor "DarkYellow"
Expand Down Expand Up @@ -84,7 +97,6 @@ function Grant-UserFullControl([String] $item) {
Set-Acl -Path $item -AclObject $acl
}


Write-Host "Fixing directory and file permissions of '${path}'..." -ForegroundColor "Yellow"

# We are repairing the .ssh directory and everything within it.
Expand All @@ -93,6 +105,7 @@ $items = @($path) + @($(Get-ChildItem -Path $path -Force -Recurse).FullName)
foreach ($item in $items) {

Disable-Inheritance -item $item
Set-UserOwnership -item $item
Remove-AllAccessPermissions -item $item
Grant-UserFullControl -item $item
}

0 comments on commit 0916418

Please sign in to comment.