You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
2.2 KiB
76 lines
2.2 KiB
<#
|
|
Crea el link: C:\Users\ofgar\.ssh\config -> (ruta actual de ssh_config)
|
|
Opcional: repara permisos de la llave id_rsa (ACL) para evitar "permissions too open".
|
|
#>
|
|
|
|
param(
|
|
[string]$RepoConfig = "C:\Users\ofgar\Workspace\revelation\ssh_config",
|
|
[switch]$FixKeyPermissions
|
|
)
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
$userSshDir = Join-Path $env:USERPROFILE ".ssh"
|
|
$userConfig = Join-Path $userSshDir "config"
|
|
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
|
|
|
|
function Backup-File([string]$Path) {
|
|
if (Test-Path -LiteralPath $Path) {
|
|
$bak = "$Path.bak.$timestamp"
|
|
Copy-Item -LiteralPath $Path -Destination $bak -Force
|
|
}
|
|
}
|
|
|
|
function Repair-PrivateKeyAcl([string]$KeyPath) {
|
|
if (-not (Test-Path -LiteralPath $KeyPath)) { return }
|
|
icacls $KeyPath /inheritance:r | Out-Null
|
|
icacls $KeyPath /grant:r "$($env:USERNAME):F" | Out-Null
|
|
}
|
|
|
|
# Validar origen
|
|
if (-not (Test-Path -LiteralPath $RepoConfig)) {
|
|
throw "No existe el ssh_config en: $RepoConfig"
|
|
}
|
|
|
|
# Asegurar .ssh
|
|
New-Item -ItemType Directory -Force -Path $userSshDir | Out-Null
|
|
|
|
# Si existe config, manejarlo
|
|
if (Test-Path -LiteralPath $userConfig) {
|
|
$item = Get-Item -LiteralPath $userConfig -Force
|
|
$isLink = $null -ne $item.LinkType
|
|
|
|
if ($isLink) {
|
|
try {
|
|
if ($item.Target -eq $RepoConfig) {
|
|
Write-Host "OK: ya está enlazado -> $RepoConfig"
|
|
} else {
|
|
Remove-Item -LiteralPath $userConfig -Force
|
|
}
|
|
} catch {
|
|
Remove-Item -LiteralPath $userConfig -Force
|
|
}
|
|
} else {
|
|
Backup-File $userConfig
|
|
Remove-Item -LiteralPath $userConfig -Force
|
|
}
|
|
}
|
|
|
|
# Crear symlink (o fallback a copia)
|
|
if (-not (Test-Path -LiteralPath $userConfig)) {
|
|
try {
|
|
New-Item -ItemType SymbolicLink -Path $userConfig -Target $RepoConfig -Force | Out-Null
|
|
Write-Host "OK: config -> $RepoConfig"
|
|
} catch {
|
|
Write-Warning "No se pudo crear symlink. Se hará copia (NO queda sincronizado con el archivo original)."
|
|
Copy-Item -LiteralPath $RepoConfig -Destination $userConfig -Force
|
|
Write-Host "OK: config copiado desde $RepoConfig"
|
|
}
|
|
}
|
|
|
|
# Reparar permisos de llave si se pide
|
|
if ($FixKeyPermissions) {
|
|
$key = Join-Path $userSshDir "id_rsa"
|
|
Repair-PrivateKeyAcl $key
|
|
Write-Host "OK: permisos ajustados para id_rsa"
|
|
} |