[sftp-int.c sftp.1 sftp.c]
     sftp -b batchfile; mouring@etoh.eviladmin.org
This commit is contained in:
Ben Lindstrom 2001-03-07 01:26:48 +00:00
parent e21c4adaac
commit 562c26bccf
4 changed files with 65 additions and 18 deletions

View File

@ -3,6 +3,9 @@
- deraadt@cvs.openbsd.org 2001/03/06 06:11:18 - deraadt@cvs.openbsd.org 2001/03/06 06:11:18
[ssh-keyscan.c] [ssh-keyscan.c]
appease gcc appease gcc
- deraadt@cvs.openbsd.org 2001/03/06 06:11:44
[sftp-int.c sftp.1 sftp.c]
sftp -b batchfile; mouring@etoh.eviladmin.org
20010306 20010306
- (bal) OpenBSD CVS Sync - (bal) OpenBSD CVS Sync
@ -4417,4 +4420,4 @@
- Wrote replacements for strlcpy and mkdtemp - Wrote replacements for strlcpy and mkdtemp
- Released 1.0pre1 - Released 1.0pre1
$Id: ChangeLog,v 1.919 2001/03/07 01:23:30 mouring Exp $ $Id: ChangeLog,v 1.920 2001/03/07 01:26:48 mouring Exp $

View File

@ -28,7 +28,7 @@
/* XXX: recursive operations */ /* XXX: recursive operations */
#include "includes.h" #include "includes.h"
RCSID("$OpenBSD: sftp-int.c,v 1.24 2001/03/04 17:42:28 millert Exp $"); RCSID("$OpenBSD: sftp-int.c,v 1.25 2001/03/06 06:11:44 deraadt Exp $");
#include "buffer.h" #include "buffer.h"
#include "xmalloc.h" #include "xmalloc.h"
@ -40,6 +40,8 @@ RCSID("$OpenBSD: sftp-int.c,v 1.24 2001/03/04 17:42:28 millert Exp $");
#include "sftp-client.h" #include "sftp-client.h"
#include "sftp-int.h" #include "sftp-int.h"
extern FILE* infile;
/* Seperators for interactive commands */ /* Seperators for interactive commands */
#define WHITESPACE " \t\r\n" #define WHITESPACE " \t\r\n"
@ -444,6 +446,7 @@ parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
unsigned long n_arg; unsigned long n_arg;
Attrib a, *aa; Attrib a, *aa;
char path_buf[MAXPATHLEN]; char path_buf[MAXPATHLEN];
int err = 0;
path1 = path2 = NULL; path1 = path2 = NULL;
cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2); cmdnum = parse_args(&cmd, &pflag, &n_arg, &path1, &path2);
@ -454,49 +457,54 @@ parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
break; break;
case I_GET: case I_GET:
path1 = make_absolute(path1, *pwd); path1 = make_absolute(path1, *pwd);
do_download(in, out, path1, path2, pflag); err = do_download(in, out, path1, path2, pflag);
break; break;
case I_PUT: case I_PUT:
path2 = make_absolute(path2, *pwd); path2 = make_absolute(path2, *pwd);
do_upload(in, out, path1, path2, pflag); err = do_upload(in, out, path1, path2, pflag);
break; break;
case I_RENAME: case I_RENAME:
path1 = make_absolute(path1, *pwd); path1 = make_absolute(path1, *pwd);
path2 = make_absolute(path2, *pwd); path2 = make_absolute(path2, *pwd);
do_rename(in, out, path1, path2); err = do_rename(in, out, path1, path2);
break; break;
case I_RM: case I_RM:
path1 = make_absolute(path1, *pwd); path1 = make_absolute(path1, *pwd);
do_rm(in, out, path1); err = do_rm(in, out, path1);
break; break;
case I_MKDIR: case I_MKDIR:
path1 = make_absolute(path1, *pwd); path1 = make_absolute(path1, *pwd);
attrib_clear(&a); attrib_clear(&a);
a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS;
a.perm = 0777; a.perm = 0777;
do_mkdir(in, out, path1, &a); err = do_mkdir(in, out, path1, &a);
break; break;
case I_RMDIR: case I_RMDIR:
path1 = make_absolute(path1, *pwd); path1 = make_absolute(path1, *pwd);
do_rmdir(in, out, path1); err = do_rmdir(in, out, path1);
break; break;
case I_CHDIR: case I_CHDIR:
path1 = make_absolute(path1, *pwd); path1 = make_absolute(path1, *pwd);
if ((tmp = do_realpath(in, out, path1)) == NULL) if ((tmp = do_realpath(in, out, path1)) == NULL) {
err = 1;
break; break;
}
if ((aa = do_stat(in, out, tmp)) == NULL) { if ((aa = do_stat(in, out, tmp)) == NULL) {
xfree(tmp); xfree(tmp);
err = 1;
break; break;
} }
if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) { if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) {
error("Can't change directory: Can't check target"); error("Can't change directory: Can't check target");
xfree(tmp); xfree(tmp);
err = 1;
break; break;
} }
if (!S_ISDIR(aa->perm)) { if (!S_ISDIR(aa->perm)) {
error("Can't change directory: \"%s\" is not " error("Can't change directory: \"%s\" is not "
"a directory", tmp); "a directory", tmp);
xfree(tmp); xfree(tmp);
err = 1;
break; break;
} }
xfree(*pwd); xfree(*pwd);
@ -522,14 +530,18 @@ parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
do_ls(in, out, path1); do_ls(in, out, path1);
break; break;
case I_LCHDIR: case I_LCHDIR:
if (chdir(path1) == -1) if (chdir(path1) == -1) {
error("Couldn't change local directory to " error("Couldn't change local directory to "
"\"%s\": %s", path1, strerror(errno)); "\"%s\": %s", path1, strerror(errno));
err = 1;
}
break; break;
case I_LMKDIR: case I_LMKDIR:
if (mkdir(path1, 0777) == -1) if (mkdir(path1, 0777) == -1) {
error("Couldn't create local directory " error("Couldn't create local directory "
"\"%s\": %s", path1, strerror(errno)); "\"%s\": %s", path1, strerror(errno));
err = 1;
}
break; break;
case I_LLS: case I_LLS:
local_do_ls(cmd); local_do_ls(cmd);
@ -598,6 +610,11 @@ parse_dispatch_command(int in, int out, const char *cmd, char **pwd)
xfree(path1); xfree(path1);
if (path2) if (path2)
xfree(path2); xfree(path2);
/* If an error occurs in batch mode we should abort. */
if (infile != stdin && err > 0)
return -1;
return(0); return(0);
} }
@ -612,7 +629,7 @@ interactive_loop(int fd_in, int fd_out)
fatal("Need cwd"); fatal("Need cwd");
setvbuf(stdout, NULL, _IOLBF, 0); setvbuf(stdout, NULL, _IOLBF, 0);
setvbuf(stdin, NULL, _IOLBF, 0); setvbuf(infile, NULL, _IOLBF, 0);
for(;;) { for(;;) {
char *cp; char *cp;
@ -620,13 +637,16 @@ interactive_loop(int fd_in, int fd_out)
printf("sftp> "); printf("sftp> ");
/* XXX: use libedit */ /* XXX: use libedit */
if (fgets(cmd, sizeof(cmd), stdin) == NULL) { if (fgets(cmd, sizeof(cmd), infile) == NULL) {
printf("\n"); printf("\n");
break; break;
} } else if (infile != stdin) /* Bluff typing */
printf("%s", cmd);
cp = strrchr(cmd, '\n'); cp = strrchr(cmd, '\n');
if (cp) if (cp)
*cp = '\0'; *cp = '\0';
if (parse_dispatch_command(fd_in, fd_out, cmd, &pwd)) if (parse_dispatch_command(fd_in, fd_out, cmd, &pwd))
break; break;
} }

14
sftp.1
View File

@ -1,4 +1,4 @@
.\" $OpenBSD: sftp.1,v 1.9 2001/03/02 18:54:31 deraadt Exp $ .\" $OpenBSD: sftp.1,v 1.10 2001/03/06 06:11:44 deraadt Exp $
.\" .\"
.\" Copyright (c) 2001 Damien Miller. All rights reserved. .\" Copyright (c) 2001 Damien Miller. All rights reserved.
.\" .\"
@ -31,6 +31,7 @@
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm sftp .Nm sftp
.Op Fl vC .Op Fl vC
.Op Fl b Ar batchfile
.Op Fl o Ar ssh_option .Op Fl o Ar ssh_option
.Op Ar hostname | user@hostname .Op Ar hostname | user@hostname
.Sh DESCRIPTION .Sh DESCRIPTION
@ -55,6 +56,17 @@ Raise logging level. This option is also passed to ssh.
Enables compression (via ssh's Enables compression (via ssh's
.Fl C .Fl C
flag) flag)
.It Fl b Ar batchfile
Batch mode reads a series of commands from an input
.Fn batchfile
instead of
.Fn stdin .
Since it lacks user interaction it should be used in conjuction with a
non-interactive authentication. Sftp will abort if any of the following
commands fail:
.Pa get, put, rename, rm, mkdir, chdir, lchdir
and
.Pa lmkdir.
.It Fl o Ar ssh_option .It Fl o Ar ssh_option
Specify an option to be directly passed to Specify an option to be directly passed to
.Xr ssh 1 . .Xr ssh 1 .

18
sftp.c
View File

@ -24,7 +24,7 @@
#include "includes.h" #include "includes.h"
RCSID("$OpenBSD: sftp.c,v 1.9 2001/03/03 23:52:22 markus Exp $"); RCSID("$OpenBSD: sftp.c,v 1.10 2001/03/06 06:11:44 deraadt Exp $");
/* XXX: commandline mode */ /* XXX: commandline mode */
/* XXX: copy between two remote hosts (commandline) */ /* XXX: copy between two remote hosts (commandline) */
@ -49,6 +49,7 @@ char *__progname;
int use_ssh1 = 0; int use_ssh1 = 0;
char *ssh_program = _PATH_SSH_PROGRAM; char *ssh_program = _PATH_SSH_PROGRAM;
char *sftp_server = NULL; char *sftp_server = NULL;
FILE* infile;
void void
connect_to_server(char **args, int *in, int *out, pid_t *sshpid) connect_to_server(char **args, int *in, int *out, pid_t *sshpid)
@ -146,7 +147,7 @@ make_ssh_args(char *add_arg)
void void
usage(void) usage(void)
{ {
fprintf(stderr, "usage: sftp [-1vC] [-osshopt=value] [user@]host\n"); fprintf(stderr, "usage: sftp [-1vC] [-b batchfile] [-osshopt=value] [user@]host\n");
exit(1); exit(1);
} }
@ -161,9 +162,10 @@ main(int argc, char **argv)
extern char *optarg; extern char *optarg;
__progname = get_progname(argv[0]); __progname = get_progname(argv[0]);
infile = stdin; /* Read from STDIN unless changed by -b */
debug_level = compress_flag = 0; debug_level = compress_flag = 0;
while ((ch = getopt(argc, argv, "1hvCo:s:S:")) != -1) { while ((ch = getopt(argc, argv, "1hvCo:s:S:b:")) != -1) {
switch (ch) { switch (ch) {
case 'C': case 'C':
compress_flag = 1; compress_flag = 1;
@ -186,6 +188,14 @@ main(int argc, char **argv)
case 'S': case 'S':
ssh_program = optarg; ssh_program = optarg;
break; break;
case 'b':
if (infile == stdin) {
infile = fopen(optarg, "r");
if (infile == NULL)
fatal("%s (%s).", strerror(errno), optarg);
} else
fatal("Filename already specified.");
break;
case 'h': case 'h':
default: default:
usage(); usage();
@ -257,6 +267,8 @@ main(int argc, char **argv)
close(in); close(in);
close(out); close(out);
if (infile != stdin)
fclose(infile);
if (waitpid(sshpid, NULL, 0) == -1) if (waitpid(sshpid, NULL, 0) == -1)
fatal("Couldn't wait for ssh process: %s", strerror(errno)); fatal("Couldn't wait for ssh process: %s", strerror(errno));