Re: SFTPing a File From stdin



In article <pan.2006.04.12.19.33.22.738616@xxxxxxxx> Me <Me@xxxxxxxx> writes:
Is there a way to securely transfer a file from standard in? I know it's
possible when connecting to a UNIX server by doing this:

cat localFile | ssh user@host cat \> remoteFile

But I'm connecting to WinSSHD and there's no cat or dd command. I've tried
using the DOS "copy con:" command with no success. Anybody have any ideas?
I can't use temp files for reasons of security. Thanks!

Well, assuming the server really supports sftp, as suggested by your
Subject line, it should be possible without doing anything special on
the server side (scp can't do it, since it wants to send the file size
before the content). For the client side, on typical *nix boxes it
*should* be just a matter of

echo "put /dev/stdin remoteFile" > /tmp/sftp.bat
sftp -b /tmp/sftp.bat user@host

However, for reasons that I'm not sure about, OpenSSH's sftp client
doesn't like that. First, it insists that the source should be a regular
file - very un-*nix-ish and certainly not required by the protocol - see
diff 1 below how to get rid of that. Second, in a semi-recent version
someone decided that using -b with sftp implies a non-interactive
session, limiting your choices of authentication methods - might be a
sensible default, I'm not sure, but in any case it should be possible to
override by using

sftp -b /tmp/sftp.bat -o "batchmode no" user@host

- I haven't verified this though - see diff 2 below if you just don't
like the idea. (And btw, the man page is wrong - 'sftp -b' doesn't "lack
user interaction" *for authentication*, but you might want to use it in
a scenario that can't provide user interaction. Maybe that semi-recent
change was just an attempt to make the code agree with the man page?:-)

--Per Hedeland
per@xxxxxxxxxxxx


Diff 1 =====================================================

diff -ru ssh.orig/sftp-client.c ssh/sftp-client.c
--- ssh.orig/sftp-client.c Mon Mar 14 13:08:12 2005
+++ ssh/sftp-client.c Tue Aug 23 17:03:11 2005
@@ -1009,12 +1009,16 @@
close(local_fd);
return(-1);
}
+ stat_to_attrib(&sb, &a);
if (!S_ISREG(sb.st_mode)) {
+ /* Don't be so squeamish - /dev/stdin works fine... */
+#if 0
error("%s is not a regular file", local_path);
close(local_fd);
return(-1);
+#endif
+ a.perm = 0644;
}
- stat_to_attrib(&sb, &a);

a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
diff -ru ssh.orig/sftp.c ssh/sftp.c
--- ssh.orig/sftp.c Mon Mar 14 13:08:12 2005
+++ ssh/sftp.c Tue Aug 23 18:08:38 2005
@@ -600,11 +600,14 @@
}

for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
+ /* Don't be so squeamish - /dev/stdin works fine... */
+#if 0
if (!is_reg(g.gl_pathv[i])) {
error("skipping non-regular file %s",
g.gl_pathv[i]);
continue;
}
+#endif
if (infer_path(g.gl_pathv[i], &tmp)) {
err = -1;
goto out;


Diff 2 =====================================================

diff -ru ssh.orig/sftp.c ssh/sftp.c
--- ssh.orig/sftp.c Mon Mar 14 13:08:12 2005
+++ ssh/sftp.c Tue Aug 23 18:08:38 2005
@@ -1479,7 +1482,9 @@
fatal("%s (%s).", strerror(errno), optarg);
showprogress = 0;
batchmode = 1;
+#if 0
addargs(&args, "-obatchmode yes");
+#endif
break;
case 'P':
sftp_direct = optarg;
.



Relevant Pages

  • Re: SPAM sudden increase
    ... > Dude was on a tech call with f-secure and the tech asked, "So, ... dude is trying to FTP to their server using WSFTP. ... but I think he is talking about sftp protocol - FTP via ...
    (alt.2600)
  • using java with sftp
    ... JCraft JSch package to SFTP files. ... //First Create a JSch session ... System.err.println("Unable to connect to FTP server. ...
    (comp.lang.java.programmer)
  • Re: using java with sftp
    ... JCraft JSch package to SFTP files. ... //First Create a JSch session ... System.err.println("Unable to connect to FTP server. ...
    (comp.lang.java.programmer)
  • Re: sftp password authentication question
    ... > I have a W2k server machine (equippend with SSH Secure Shell) which ... > NetworkSimplicity SSH server). ... > can be apparently easily accomplished with normal FTP. ... > this to work with SFTP or SFTP2 ??? ...
    (comp.security.ssh)
  • Re: Pasting via ssh causes data loss
    ... sftp of the file to the remote and ... I then opened an ssh session to a FreeBSD ... errors shown on the interface of the server? ...
    (freebsd-questions)

Loading