Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to setup SFTP (in order to make sshfs work) #307

Open
unclechu opened this issue Nov 26, 2023 · 8 comments
Open

How to setup SFTP (in order to make sshfs work) #307

unclechu opened this issue Nov 26, 2023 · 8 comments

Comments

@unclechu
Copy link

How could I setup SFTP in order to make sshfs work? I made a simple script to start an SSHD server:

{ pkgs, lib }:

let
  hostKeyPath = toString
    /data/data/com.termux.nix/files/home/.ssh/ssh-host-key;

  # Add authorized keys to: ~/.ssh/authorized_keys
  sshdConfigFile = pkgs.writeText "sshd-config" ''
    HostKey "${lib.escape ["\""] hostKeyPath}"
    Port 8044
    Subsystem sftp "${lib.escape ["\""] e.sftp-server}"
  '';

  executables = {
    bash = "${pkgs.bash}/bin/bash";
    sshd = "${pkgs.openssh}/bin/sshd";
    sftp-server = "${pkgs.openssh}/libexec/sftp-server";
  };

  e = executables;
  esc = lib.escapeShellArg;

  run-sshd = pkgs.writeTextFile rec {
    name = "run-sshd";
    executable = true;
    destination = "/bin/${name}";
    text = ''
      #! ${e.bash}
      ${esc e.sshd} -f ${esc sshdConfigFile} -e -D
    '';
    checkPhase = ''(
      set -o xtrace
      ${builtins.concatStringsSep "\n" (
        map (x: "[[ -x ${esc x} ]]") (builtins.attrValues executables)
      )}
    )'';
  };
in
  run-sshd // { inherit sshdConfigFile; }

The regular ssh works fine but if I try to mount via sshfs I get an error like “Connection closed by the user". Here is the end of the log of sshfs call:

...
debug1: Sending subsystem: sftp
debug1: pledge: fork
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: client_input_channel_req: channel 0 rtype [email protected] reply 0
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 4872, received 3844 bytes, in 0.3 seconds
Bytes per second: sent 18440.4, received 14549.4
debug1: Exit status 255
remote host has disconnected
@Gerschtli
Copy link
Collaborator

It might be that the path to the sftp-server executable is not found because it may be running outside of proot.. Not sure how to debug that though but the arch wiki says that this error message happens when the sftp-server executable is not valid: https://wiki.archlinux.org/title/SSHFS#Remote_host_has_disconnected

@unclechu
Copy link
Author

@Gerschtli I’ve also tried internal-sftp, it makes no difference.

@unclechu
Copy link
Author

Also I don’t think it should run outside proot since I run run-sshd from inside of the Nix-on-Droid shell?

@azuwis
Copy link
Contributor

azuwis commented Dec 28, 2023

Looks like a proot problem proot-me/proot#243

@SonicStark
Copy link

Looks like a proot problem proot-me/proot#243

A patched sftp-server, as a workaround, can mitigate the problem.
See proot-me/proot#243 (comment)

@unclechu
Copy link
Author

@SonicStark I tried to patch it like this:

{ pkgs ? import <nixpkgs> {}
}:

let
  openssh = pkgs.openssh.overrideAttrs (old: {
    patchPhase = (old.patchPhase or "") + ''
      sed -i 's/\(platform_disable_tracing(\)1\();\)/\10\2/' sftp-server.c
    '';
  });
in

openssh

But for some reason it failed much later like this (cannot create directory '/var': Permission denied):

...
make[1]: Entering directory '/build/openssh-9.7p1/openbsd-compat'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/build/openssh-9.7p1/openbsd-compat'
/nix/store/k71apxkm38m3g34k01sb6zhysi0y7gph-coreutils-9.5/bin/mkdir -p /nix/store/k1zkv58z9qgk53skli5l2ahfrcd3ljhk-openssh-9.7p1/bin
/nix/store/k71apxkm38m3g34k01sb6zhysi0y7gph-coreutils-9.5/bin/mkdir -p /nix/store/k1zkv58z9qgk53skli5l2ahfrcd3ljhk-openssh-9.7p1/etc/ssh
/nix/store/k71apxkm38m3g34k01sb6zhysi0y7gph-coreutils-9.5/bin/mkdir -p /nix/store/k1zkv58z9qgk53skli5l2ahfrcd3ljhk-openssh-9.7p1/bin
/nix/store/k71apxkm38m3g34k01sb6zhysi0y7gph-coreutils-9.5/bin/mkdir -p /nix/store/k1zkv58z9qgk53skli5l2ahfrcd3ljhk-openssh-9.7p1/share/man/man1
/nix/store/k71apxkm38m3g34k01sb6zhysi0y7gph-coreutils-9.5/bin/mkdir -p /nix/store/k1zkv58z9qgk53skli5l2ahfrcd3ljhk-openssh-9.7p1/share/man/man5
/nix/store/k71apxkm38m3g34k01sb6zhysi0y7gph-coreutils-9.5/bin/mkdir -p /nix/store/k1zkv58z9qgk53skli5l2ahfrcd3ljhk-openssh-9.7p1/share/man/man8
/nix/store/k71apxkm38m3g34k01sb6zhysi0y7gph-coreutils-9.5/bin/mkdir -p /nix/store/k1zkv58z9qgk53skli5l2ahfrcd3ljhk-openssh-9.7p1/libexec
/nix/store/k71apxkm38m3g34k01sb6zhysi0y7gph-coreutils-9.5/bin/mkdir -p -m 0755 /var/empty
/nix/store/k71apxkm38m3g34k01sb6zhysi0y7gph-coreutils-9.5/bin/mkdir: cannot create directory '/var': Permission denied
make: *** [Makefile:394: install-files] Error 1
error: builder for '/nix/store/2flc2rag0fvzymw2n3b79dqa9173lr0h-openssh-9.7p1.drv' failed with exit code 2;
       last 10 log lines:
       > /nix/store/k71apxkm38m3g34k01sb6zhysi0y7gph-coreutils-9.5/bin/mkdir -p /nix/store/k1zkv58z9qgk53skli5l2ahfrcd3ljhk-openssh-9.7p1/bin
       > /nix/store/k71apxkm38m3g34k01sb6zhysi0y7gph-coreutils-9.5/bin/mkdir -p /nix/store/k1zkv58z9qgk53skli5l2ahfrcd3ljhk-openssh-9.7p1/etc/ssh
       > /nix/store/k71apxkm38m3g34k01sb6zhysi0y7gph-coreutils-9.5/bin/mkdir -p /nix/store/k1zkv58z9qgk53skli5l2ahfrcd3ljhk-openssh-9.7p1/bin
       > /nix/store/k71apxkm38m3g34k01sb6zhysi0y7gph-coreutils-9.5/bin/mkdir -p /nix/store/k1zkv58z9qgk53skli5l2ahfrcd3ljhk-openssh-9.7p1/share/man/man1
       > /nix/store/k71apxkm38m3g34k01sb6zhysi0y7gph-coreutils-9.5/bin/mkdir -p /nix/store/k1zkv58z9qgk53skli5l2ahfrcd3ljhk-openssh-9.7p1/share/man/man5
       > /nix/store/k71apxkm38m3g34k01sb6zhysi0y7gph-coreutils-9.5/bin/mkdir -p /nix/store/k1zkv58z9qgk53skli5l2ahfrcd3ljhk-openssh-9.7p1/share/man/man8
       > /nix/store/k71apxkm38m3g34k01sb6zhysi0y7gph-coreutils-9.5/bin/mkdir -p /nix/store/k1zkv58z9qgk53skli5l2ahfrcd3ljhk-openssh-9.7p1/libexec
       > /nix/store/k71apxkm38m3g34k01sb6zhysi0y7gph-coreutils-9.5/bin/mkdir -p -m 0755 /var/empty
       > /nix/store/k71apxkm38m3g34k01sb6zhysi0y7gph-coreutils-9.5/bin/mkdir: cannot create directory '/var': Permission denied
       > make: *** [Makefile:394: install-files] Error 1
       For full logs, run 'nix log /nix/store/2flc2rag0fvzymw2n3b79dqa9173lr0h-openssh-9.7p1.drv'.

@SonicStark
Copy link

@unclechu Does it fail without the patch? Looks like /var is missing in your fs...

@unclechu
Copy link
Author

I tired to rebuild it inside Nix-on-Droid environment. Maybe it’ll actually fail without a patch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants