Commit 687553a2 authored by Yuanle Song's avatar Yuanle Song
Browse files

enable host key check for ssh.exe

specify known hosts file explicitly, so wrong home doesn't matter.
parent 97216b59
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -173,6 +173,7 @@ exception PrivateKeyNotFoundException of string
let addOptionsForRemoteBackup (results: ParseResults<CLIArguments>) (logger: Logger) (rsyncCmd: string list) =
    let sshExeFile = mbackupProgramDir + "rsync-w64/usr/bin/ssh.exe"
    let sshConfigFile = userHome + ".ssh/config"
    let sshKnownHostsFile = userHome + ".ssh/known_hosts"
    let sshPrivateKeyFile = results.GetResult(Ssh_Key, defaultValue = userHome + ".ssh/id_rsa") |> toMingwPath
    let sshPrivateKeyFileWin = toWinPath sshPrivateKeyFile
    if not (File.Exists(sshPrivateKeyFileWin)) then
@@ -184,8 +185,8 @@ let addOptionsForRemoteBackup (results: ParseResults<CLIArguments>) (logger: Log

        let rsyncCmd =
            List.append rsyncCmd
                [ sprintf "-e \"'%s'%s -i %s -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null\"" sshExeFile
                      sshConfigFileOption sshPrivateKeyFile ]
                [ sprintf "-e \"'%s'%s -i %s -o StrictHostKeyChecking=ask -o UserKnownHostsFile=%s\""
                          sshExeFile sshConfigFileOption sshPrivateKeyFile sshKnownHostsFile]

        let nodeName = results.GetResult(Node_Name, defaultValue = Net.Dns.GetHostName())
        let remoteLogFile = sprintf "/var/log/mbackup/%s.log" nodeName
+1 −1
Original line number Diff line number Diff line
@@ -6,7 +6,7 @@
    <RuntimeIdentifiers>win10-x64</RuntimeIdentifiers>
    <RootNamespace>Mbackup</RootNamespace>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    <Version>0.2.3.0</Version>
    <Version>0.2.4.0</Version>
  </PropertyGroup>

  <ItemGroup>
+88 −2
Original line number Diff line number Diff line
@@ -143,6 +143,22 @@ C:\ProgramData\mbackup\mbackup.txt
** 2019-11-14 vscode f# doesn't support open a module
it can only support open a namespace.
using the vscode Ionide-fsharp extension.
** 2019-11-16 ssh.exe Could not create directory '/home/IEUser/.ssh' error.
when rsync.exe or ssh.exe is run from cmd.exe, it will always show this error.
HOME env var is defined properly.

- create /etc/nsswitch.conf with the following content didn't fix it.
  db_home: windows

- openssh get user's home dir from getpwuid call.
  Check how ssh get home dir. maybe from /etc/passwd and similar syscall.
  check ssh-keygen source code.
  https://github.com/openssh/openssh-portable/blob/master/ssh-keygen.c
  pw = getpwuid(getuid());
  https://manpages.ubuntu.com/manpages/bionic/en/man3/getpwuid.3.html
  indeed it read config for passwd entry.
  how to bundle passwd file in mingw application?

* current                                                             :entry:
** 
** 2019-11-15 f# indent is difficult in vscode.
@@ -178,6 +194,9 @@ yes. I should support this.

Both local.list and local.exclude.

** 2019-11-16 support reading remote-user=xxx option from config file.
cli arg > env var > config file.
same with target option.
** 2019-11-14 learn how to create an msi installer.
- WiX Toolset
  https://wixtoolset.org/
@@ -223,7 +242,7 @@ Both local.list and local.exclude.
    maybe show a installer window and let user click Next and Finish.
  - TODO how to add some dir to PATH?
    I only have one exe. Maybe just create a start menu or desktop shortcut?
    create desktop shortcut to mbackup.exe
    DONE create desktop shortcut to mbackup.exe
    installer - Create shortcut to desktop using WiX - Stack Overflow
    https://stackoverflow.com/questions/11868499/create-shortcut-to-desktop-using-wix
    it works.
@@ -321,7 +340,74 @@ Both local.list and local.exclude.
        // set HOME dir on system level works on B75I3. maybe when rsync invoke ssh, env variable is lost.
        // if system level env variable is required, set HOME when msi is installed if HOME is not set.

        *now*
        in admin powershell,
        setx HOME ^%USERPROFILE^%
        // try mbackup to remote node, nope.
        setx HOME $env:userprofile
        // try mbackup to remote node, nope.

        how to make HOME work in the Win 10 GUEST?
        TODO Maybe I need to create the rsync zip file using a Dockerfile. LATER.
        And include bash or getenv tool for debugging. Does my rsync tool include bash? yes.

        on B75I3,
        .\bash.exe
        echo $HOME
        /cygdrive/c/Users/sylecn

        on Win10 Guest,
        .\bash.exe
        bash.exe: warning: could not find /tmp, please create!
        echo $HOME
        /cygdrive/c/Users/IEUser
        // looks HOME var is alright. ssh is not using HOME env variable.
        ssh-keygen works fine in bash shell. so bash setup some variable for use with ssh tools.
        after reboot, run ssh-keygen directly in cmd.exe also works.
        When run ssh.exe from cmd.exe, it still complains about HOME dir.
        Could not create directory '/home/IEUser/.ssh'.

        I think I know the problem. When start bash.exe, it will convert all path in PATH, HOME to cygdrive style.
        When start ssh.exe directly, HOME dir is still in windows style.

        Try set HOME to cygdrive style and run ssh.exe.
        how to set env var for current shell only in windows?
        set HOME=/cygdrive/c/Users/IEUser
        ssh.exe ...
        Could not create directory '/home/IEUser/.ssh'.

        search: mingw HOME
        HOWTO Set the HOME variable for cmd exe | MinGW
        http://www.mingw.org/wiki/HOWTO_Set_the_HOME_variable_for_cmd_exe
        Ubuntu Manpage: ssh — OpenSSH SSH client (remote login program)
        https://manpages.ubuntu.com/manpages/bionic/en/man1/ssh.1.html
        ssh will set HOME env var, not read it.
        Check how ssh get home dir. maybe from /etc/passwd and similar syscall.
        check ssh-keygen source code.
        https://github.com/openssh/openssh-portable/blob/master/ssh-keygen.c
        pw = getpwuid(getuid());
        https://manpages.ubuntu.com/manpages/bionic/en/man3/getpwuid.3.html
        indeed it read config for passwd entry.
        how to bundle passwd file in mingw application?

        Configure sshd on MSYS2 and run it as a Windows service · GitHub
        https://gist.github.com/samhocevar/00eec26d9e9988d080ac
        /etc/nsswitch.conf
        db_home: windows
        // what does this do? I think it only change HOME, doesn't work for getpwuid call.
        http://manpages.ubuntu.com/manpages/precise/man5/nsswitch.conf.5.html
        nsswitch can manage how system read passwd db. cool.

        after updating /etc/nsswitch.conf, all MSYS applications and terminals must be
        closed, as the configuration is only read once at MSYS DLL load time.
        try db_home: windows again. reboot and test.
        it doesn't work.
        why it works on B75I3? what's different?

        this is all about UserKnownHostsFile? just specify a path for this option.
        after adding UserKnownHostsFile to point to existing file, ssh still wants to create /home/<user>/.ssh dir.

        I will leave this problem. It's harmless for my use case.

      - System.ArgumentException: Value does not fall within the expected range.
        this happens when I run dotnet run on B75I3 dev node.
        what is this error?