From 304a68c565d58ae52dd39cf6e2d6ef52cada7d19 Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Wed, 14 Apr 2021 13:47:28 +0200 Subject: [PATCH] rpc: prepare to support more operating systems --- rpc/dial.go | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/rpc/dial.go b/rpc/dial.go index db25cb7..680cb87 100644 --- a/rpc/dial.go +++ b/rpc/dial.go @@ -98,6 +98,25 @@ func (c *Conn) dialSSH(stdin io.Writer, stdout_ io.Reader) error { path := fmt.Sprintf("/tmp/safcm-remote-%d", uid) c.debugf("DialSSH: probing remote at %q", path) + + // Compatibility for different operating systems + var compat string + switch goos { + case "linux": + compat = ` +dir_stat='drwxrwxrwt 0 0' +file_stat="-rwx------ $(id -u) $(id -g)" +compat_stat() { + stat -c '%A %u %g' "$1" +} +compat_sha512sum() { + sha512sum "$1" +} +` + default: + return fmt.Errorf("internal error: no support for %q", goos) + } + // Use a function so the shell cannot execute the input line-wise. // This is important because we're also using stdin to send data to // the script. If the shell executes the input line-wise then our @@ -116,18 +135,19 @@ func (c *Conn) dialSSH(stdin io.Writer, stdout_ io.Reader) error { // `test -e` is only used to prevent error messages if the file // doesn't exist. It does not guard against any races. _, err = fmt.Fprintf(stdin, ` +%s f() { x=%q dir="$(dirname "$x")" - if ! test "$(stat -c '%%A %%u %%g' "$dir")" = 'drwxrwxrwt 0 0'; then + if ! test "$(compat_stat "$dir")" = "$dir_stat"; then echo "unsafe permissions on $dir, aborting" >&2 exit 1 fi - if test -e "$x" && test "$(stat -c '%%A %%u' "$x")" = "-rwx------ $(id -u)"; then + if test -e "$x" && test "$(compat_stat "$x")" = "$file_stat"; then # Report checksum - sha512sum "$x" + compat_sha512sum "$x" else # Empty checksum to request upload echo @@ -155,7 +175,7 @@ f() { exec "$x" sync } f -`, path) +`, compat, path) if err != nil { return err } -- 2.44.1