#!/bin/bash
# 配置 Windows SSH Server - administrators_authorized_keys

ADMIN_KEYS="/c/ProgramData/ssh/administrators_authorized_keys"
SERVER_KEY="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCm/leXfpZ1Ycm7uL3sXYU1Hp5TO42EqUnD5H7SwveTIusF14e5oZf5P72fttGLIxQMFgGdXzu4+coWPmpGsUHjqm4g0FbeL+mjM+0cnqeggoueoFm3dk9y+9jaogjuR2VY9oqbdOvD+L0IfvS+t6WJrYif1MdXOYnH5MfNYtrLb/MWCDiTT4w/Cw0QKSSB3S3+nvYqxD1Ep+wdLrd+SfgiMi2qU/bO7ccEgXwjqiI/4LDHh2r5oajkvdRSuQ75PLSRdFfR+aq/NcV/NJE3YOcb8wVhvVjZZSXQyy8PlqLn6N58qckharwAj6vRxC9CbWB+XDrLOMWOtdq7T7iBPJmy1wVDDyva9wEnKiprdXf+2F/Q57SJjcbPTCt6TqXggQ0hVf7tOK6Wefk5dwPW0P3mdzPTp91s9iU96kM+9VSY0dhKEnim+6IncxEK+hnbAbGOX66MuRmK9u7dYlsjhFq345rNXmkXIIY3VU2GuCBLB4bk88pNubrd8y9S03JbZIU= root@vps21249823.hosteons.com"

echo "=== 配置 Windows SSH Server 公钥认证 ==="
echo ""

# 1. 写入公钥
echo "步骤 1/3: 写入服务器公钥到 administrators_authorized_keys"
echo "$SERVER_KEY" > "$ADMIN_KEYS"
echo "✓ 公钥已写入"
echo ""

# 2. 设置权限
echo "步骤 2/3: 设置文件权限"
WIN_PATH="C:\\ProgramData\\ssh\\administrators_authorized_keys"
icacls.exe "$WIN_PATH" //inheritance:r
icacls.exe "$WIN_PATH" //grant "SYSTEM:(F)"
icacls.exe "$WIN_PATH" //grant "BUILTIN\\Administrators:(F)"
echo "✓ 权限已设置"
echo ""

# 3. 重启 SSH Server
echo "步骤 3/3: 重启 SSH Server"
net.exe stop sshd 2>/dev/null
sleep 2
net.exe start sshd
echo "✓ SSH Server 已重启"
echo ""

# 验证
echo "=== 验证配置 ==="
echo "文件内容："
cat "$ADMIN_KEYS"
echo ""
echo "配置完成！"
