refactor: finish moving ssh-* scripts to own installers
[webi-installers/.git] / ssh-pubkey / ssh-pubkey.ps1
1 #!/usr/bin/env pwsh
2
3 # TODO: can we use some of this?
4 # https://github.com/PowerShell/openssh-portable/blob/latestw_all/contrib/win32/openssh/FixUserFilePermissions.ps1
5
6 if (!(Test-Path -Path "$Env:USERPROFILE/.ssh"))
7 {
8     New-Item -Path "$Env:USERPROFILE/.ssh" -ItemType Directory -Force | out-null
9     #& icacls "$Env:USERPROFILE/.ssh" /inheritance:r
10     #& icacls "$Env:USERPROFILE/.ssh" /grant:r "$Env:USERNAME":"(F)"
11 }
12
13 if (!(Test-Path -Path "$Env:USERPROFILE/.ssh/config"))
14 {
15     New-Item -Path "$Env:USERPROFILE/.ssh/config" -ItemType "file" -Value ""
16     #& icacls "$Env:USERPROFILE/.ssh/config" /inheritance:r
17     #& icacls "$Env:USERPROFILE/.ssh/config" /grant:r "$Env:USERNAME":"(F)"
18 }
19
20 #if (!(Test-Path -Path "$Env:USERPROFILE/.ssh/authorized_keys"))
21 #{
22 #    New-Item -Path "$Env:USERPROFILE/.ssh/authorized_keys" -ItemType "file" -Value ""
23 #    #& icacls "$Env:USERPROFILE/.ssh/authorized_keys" /inheritance:r
24 #    #& icacls "$Env:USERPROFILE/.ssh/authorized_keys" /grant:r "$Env:USERNAME":"(F)"
25 #}
26
27 if (!(Test-Path -Path "$Env:USERPROFILE/.ssh/id_rsa"))
28 {
29     & ssh-keygen -b 2048 -t rsa -f "$Env:USERPROFILE/.ssh/id_rsa" -q -N """"
30     echo ""
31 }
32
33 if (!(Test-Path -Path "$Env:USERPROFILE/.ssh/id_rsa.pub"))
34 {
35     & ssh-keygen -y -f "$Env:USERPROFILE/.ssh/id_rsa" > "$Env:USERPROFILE/.ssh/id_rsa.pub"
36     echo ""
37 }
38
39 # TODO use the comment (if any) for the name of the file
40 echo ""
41 echo "~/Downloads/id_rsa.$Env:USERNAME.pub":
42 echo ""
43 #rm -f "$Env:USERPROFILE/Downloads/id_rsa.$Env:USERNAME.pub":
44 Copy-Item -Path "$Env:USERPROFILE/.ssh/id_rsa.pub" -Destination "$Env:USERPROFILE/Downloads/id_rsa.$Env:USERNAME.pub"
45 & type "$Env:USERPROFILE/Downloads/id_rsa.$Env:USERNAME.pub"
46 echo ""