Skip to content

Commit

Permalink
Add recursive SSH item repair
Browse files Browse the repository at this point in the history
  • Loading branch information
countzero committed Apr 26, 2021
1 parent 3323527 commit 540cfc6
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions repair_ssh_permissions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ Param (
$user
)

# We are breaking on the first exception.
Set-StrictMode -Version latest
$ErrorActionPreference = "Stop"

# Default the target path to '%USERPROFILE%/.ssh'.
if (!$path) {
Expand Down Expand Up @@ -97,15 +100,50 @@ function Grant-UserFullControl([String] $item) {
Set-Acl -Path $item -AclObject $acl
}

Write-Host "Fixing directory and file permissions of '${path}'..." -ForegroundColor "Yellow"
function Repair-Item([String] $item) {

# We are repairing the .ssh directory and everything within it.
$items = @($path) + @($(Get-ChildItem -Path $path -Force -Recurse).FullName)

foreach ($item in $items) {
Write-Host "Repairing SSH item '$item'..." -ForegroundColor "White"

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

function Repair-DirectoryAndFiles([String] $directoryPath) {

Repair-Item -item $directoryPath

$directoryIsEmpty = $($(Get-ChildItem $directoryPath) | Measure-Object ).Count -eq 0

if ($directoryIsEmpty) {
return;
}

$files = @($(Get-ChildItem -File -Path $directoryPath -Force).FullName)
foreach ($file in $files) {
Repair-Item -item $file
}

$directories = @($(Get-ChildItem -Directory -Path $directoryPath -Force))
foreach ($directory in $directories) {
Repair-DirectoryAndFiles($directory.FullName)
}
}

Write-Host "Repairing SSH directory and file permissions..." -ForegroundColor "Green"

# We have to repair the .ssh directory first to gain read permissions.
Repair-Item -item $path

# If the entry point directory is a symbolic link we
# are also repairing its target and proceed from there.
if ($(Get-Item $path).LinkType -eq 'SymbolicLink') {
$path = $(Get-Item $path).Target
Repair-Item -item $path
}

# We are recursively reparining all directories and files.
Repair-DirectoryAndFiles($path)

Write-Host "Successfully repaired SSH directory and file permissions." -ForegroundColor "Green"

0 comments on commit 540cfc6

Please sign in to comment.