2022-01-17 21:39:51 +00:00
|
|
|
/* $OpenBSD: scp.c,v 1.243 2022/01/17 21:39:51 djm Exp $ */
|
1999-10-27 03:42:43 +00:00
|
|
|
/*
|
2000-09-16 02:29:08 +00:00
|
|
|
* scp - secure remote copy. This is basically patched BSD rcp which
|
|
|
|
* uses ssh to do the data transfer (instead of using rcmd).
|
2000-04-16 01:18:38 +00:00
|
|
|
*
|
2000-09-16 02:29:08 +00:00
|
|
|
* NOTE: This version should NOT be suid root. (This uses ssh to
|
|
|
|
* do the transfer and ssh has the necessary privileges.)
|
2000-04-16 01:18:38 +00:00
|
|
|
*
|
1999-11-24 13:26:21 +00:00
|
|
|
* 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi>
|
2000-04-16 01:18:38 +00:00
|
|
|
*
|
2000-09-16 02:29:08 +00:00
|
|
|
* As far as I am concerned, the code I have written for this software
|
|
|
|
* can be used freely for any purpose. Any derived versions of this
|
|
|
|
* software must be clearly marked as such, and if the derived work is
|
|
|
|
* incompatible with the protocol description in the RFC file, it must be
|
|
|
|
* called by a name other than "ssh" or "Secure Shell".
|
|
|
|
*/
|
|
|
|
/*
|
2001-03-05 06:59:27 +00:00
|
|
|
* Copyright (c) 1999 Theo de Raadt. All rights reserved.
|
|
|
|
* Copyright (c) 1999 Aaron Campbell. All rights reserved.
|
2000-09-16 02:29:08 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
1999-10-27 03:42:43 +00:00
|
|
|
|
|
|
|
/*
|
2000-08-23 00:46:23 +00:00
|
|
|
* Parts from:
|
|
|
|
*
|
1999-10-27 03:42:43 +00:00
|
|
|
* Copyright (c) 1983, 1990, 1992, 1993, 1995
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-06-03 03:06:18 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1999-10-27 03:42:43 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
2006-03-15 00:29:24 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
2006-03-15 00:45:54 +00:00
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
# include <sys/stat.h>
|
|
|
|
#endif
|
2008-03-14 00:59:50 +00:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
#include <poll.h>
|
|
|
|
#else
|
|
|
|
# ifdef HAVE_SYS_POLL_H
|
|
|
|
# include <sys/poll.h>
|
|
|
|
# endif
|
2008-02-29 04:07:01 +00:00
|
|
|
#endif
|
2006-08-05 00:57:45 +00:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
# include <sys/time.h>
|
|
|
|
#endif
|
2006-03-15 00:29:24 +00:00
|
|
|
#include <sys/wait.h>
|
2006-08-05 02:39:39 +00:00
|
|
|
#include <sys/uio.h>
|
2006-03-15 00:25:13 +00:00
|
|
|
|
2006-03-15 00:53:45 +00:00
|
|
|
#include <ctype.h>
|
2006-03-15 00:25:13 +00:00
|
|
|
#include <dirent.h>
|
2006-07-12 12:22:46 +00:00
|
|
|
#include <errno.h>
|
2006-07-10 11:13:46 +00:00
|
|
|
#include <fcntl.h>
|
2019-11-01 07:24:29 +00:00
|
|
|
#ifdef HAVE_FNMATCH_H
|
2019-01-26 22:41:28 +00:00
|
|
|
#include <fnmatch.h>
|
2019-11-01 07:24:29 +00:00
|
|
|
#endif
|
2021-08-04 01:05:11 +00:00
|
|
|
#ifdef USE_SYSTEM_GLOB
|
|
|
|
# include <glob.h>
|
|
|
|
#else
|
|
|
|
# include "openbsd-compat/glob.h"
|
|
|
|
#endif
|
2021-08-02 23:38:27 +00:00
|
|
|
#ifdef HAVE_LIBGEN_H
|
|
|
|
#include <libgen.h>
|
|
|
|
#endif
|
2015-01-16 06:40:12 +00:00
|
|
|
#include <limits.h>
|
2016-05-25 23:48:45 +00:00
|
|
|
#include <locale.h>
|
2006-07-10 10:53:08 +00:00
|
|
|
#include <pwd.h>
|
2006-03-15 00:52:09 +00:00
|
|
|
#include <signal.h>
|
2006-07-12 12:15:16 +00:00
|
|
|
#include <stdarg.h>
|
2017-05-01 03:19:41 +00:00
|
|
|
#ifdef HAVE_STDINT_H
|
2019-10-08 22:06:35 +00:00
|
|
|
# include <stdint.h>
|
2017-05-01 03:19:41 +00:00
|
|
|
#endif
|
2006-08-05 01:37:59 +00:00
|
|
|
#include <stdio.h>
|
2006-08-05 01:34:19 +00:00
|
|
|
#include <stdlib.h>
|
2006-07-24 04:13:33 +00:00
|
|
|
#include <string.h>
|
2006-07-24 04:09:40 +00:00
|
|
|
#include <time.h>
|
2006-07-24 04:01:23 +00:00
|
|
|
#include <unistd.h>
|
2013-03-20 01:55:14 +00:00
|
|
|
#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
|
2007-06-12 14:02:07 +00:00
|
|
|
#include <vis.h>
|
|
|
|
#endif
|
1999-10-27 03:42:43 +00:00
|
|
|
|
|
|
|
#include "xmalloc.h"
|
2017-10-21 23:06:24 +00:00
|
|
|
#include "ssh.h"
|
2001-01-22 05:34:40 +00:00
|
|
|
#include "atomicio.h"
|
|
|
|
#include "pathnames.h"
|
|
|
|
#include "log.h"
|
2001-02-18 03:55:16 +00:00
|
|
|
#include "misc.h"
|
2003-01-10 10:43:24 +00:00
|
|
|
#include "progressmeter.h"
|
2016-05-25 23:48:45 +00:00
|
|
|
#include "utf8.h"
|
2022-01-17 21:39:51 +00:00
|
|
|
#include "sftp.h"
|
1999-10-27 03:42:43 +00:00
|
|
|
|
2021-08-02 23:38:27 +00:00
|
|
|
#include "sftp-common.h"
|
|
|
|
#include "sftp-client.h"
|
|
|
|
|
2000-11-17 03:47:20 +00:00
|
|
|
extern char *__progname;
|
|
|
|
|
2007-10-26 05:39:15 +00:00
|
|
|
#define COPY_BUFLEN 16384
|
|
|
|
|
2021-08-11 14:05:19 +00:00
|
|
|
int do_cmd(char *, char *, char *, int, int, char *, int *, int *, pid_t *);
|
|
|
|
int do_cmd2(char *, char *, int, char *, int, int);
|
2006-04-23 02:04:27 +00:00
|
|
|
|
2001-05-08 20:27:25 +00:00
|
|
|
/* Struct for addargs */
|
|
|
|
arglist args;
|
2010-12-01 01:03:19 +00:00
|
|
|
arglist remote_remote_args;
|
2000-10-14 05:23:11 +00:00
|
|
|
|
2003-01-24 00:36:58 +00:00
|
|
|
/* Bandwidth limit */
|
2010-09-24 12:15:11 +00:00
|
|
|
long long limit_kbps = 0;
|
|
|
|
struct bwlimit bwlimit;
|
2003-01-24 00:36:58 +00:00
|
|
|
|
1999-10-27 03:42:43 +00:00
|
|
|
/* Name of current file being transferred. */
|
|
|
|
char *curfile;
|
|
|
|
|
|
|
|
/* This is set to non-zero to enable verbose mode. */
|
1999-11-24 13:26:21 +00:00
|
|
|
int verbose_mode = 0;
|
2021-08-02 23:38:27 +00:00
|
|
|
LogLevel log_level = SYSLOG_LEVEL_INFO;
|
1999-10-27 03:42:43 +00:00
|
|
|
|
|
|
|
/* This is set to zero if the progressmeter is not desired. */
|
|
|
|
int showprogress = 1;
|
|
|
|
|
2011-01-06 11:40:30 +00:00
|
|
|
/*
|
|
|
|
* This is set to non-zero if remote-remote copy should be piped
|
|
|
|
* through this process.
|
|
|
|
*/
|
2021-08-09 23:56:36 +00:00
|
|
|
int throughlocal = 1;
|
2011-01-06 11:40:30 +00:00
|
|
|
|
2017-10-21 23:06:24 +00:00
|
|
|
/* Non-standard port to use for the ssh connection or -1. */
|
|
|
|
int sshport = -1;
|
|
|
|
|
2000-08-23 00:46:23 +00:00
|
|
|
/* This is the program to execute for the secured connection. ("ssh" or -S) */
|
2007-12-02 11:48:40 +00:00
|
|
|
char *ssh_program = _PATH_SSH_PROGRAM;
|
2000-08-23 00:46:23 +00:00
|
|
|
|
2002-11-09 15:54:08 +00:00
|
|
|
/* This is used to store the pid of ssh_program */
|
2003-06-04 12:51:24 +00:00
|
|
|
pid_t do_cmd_pid = -1;
|
2021-08-07 00:06:30 +00:00
|
|
|
pid_t do_cmd_pid2 = -1;
|
2003-06-04 12:51:24 +00:00
|
|
|
|
2021-08-02 23:38:27 +00:00
|
|
|
/* Needed for sftp */
|
|
|
|
volatile sig_atomic_t interrupted = 0;
|
|
|
|
|
|
|
|
int remote_glob(struct sftp_conn *, const char *, int,
|
|
|
|
int (*)(const char *, int), glob_t *); /* proto for sftp-glob.c */
|
|
|
|
|
2003-06-04 12:51:24 +00:00
|
|
|
static void
|
|
|
|
killchild(int signo)
|
|
|
|
{
|
2005-01-24 10:57:40 +00:00
|
|
|
if (do_cmd_pid > 1) {
|
2005-06-01 13:01:12 +00:00
|
|
|
kill(do_cmd_pid, signo ? signo : SIGTERM);
|
2005-01-24 10:57:40 +00:00
|
|
|
waitpid(do_cmd_pid, NULL, 0);
|
|
|
|
}
|
2021-08-07 00:06:30 +00:00
|
|
|
if (do_cmd_pid2 > 1) {
|
|
|
|
kill(do_cmd_pid2, signo ? signo : SIGTERM);
|
|
|
|
waitpid(do_cmd_pid2, NULL, 0);
|
|
|
|
}
|
2003-06-04 12:51:24 +00:00
|
|
|
|
2005-06-01 13:01:12 +00:00
|
|
|
if (signo)
|
|
|
|
_exit(1);
|
|
|
|
exit(1);
|
2003-06-04 12:51:24 +00:00
|
|
|
}
|
2002-11-09 15:54:08 +00:00
|
|
|
|
2010-07-02 03:37:33 +00:00
|
|
|
static void
|
2021-08-07 00:06:30 +00:00
|
|
|
suspone(int pid, int signo)
|
2010-07-02 03:37:33 +00:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
2021-08-07 00:06:30 +00:00
|
|
|
if (pid > 1) {
|
|
|
|
kill(pid, signo);
|
|
|
|
while (waitpid(pid, &status, WUNTRACED) == -1 &&
|
2010-07-02 03:37:33 +00:00
|
|
|
errno == EINTR)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-07 00:06:30 +00:00
|
|
|
static void
|
|
|
|
suspchild(int signo)
|
|
|
|
{
|
|
|
|
suspone(do_cmd_pid, signo);
|
|
|
|
suspone(do_cmd_pid2, signo);
|
|
|
|
kill(getpid(), SIGSTOP);
|
|
|
|
}
|
|
|
|
|
2006-01-31 10:49:27 +00:00
|
|
|
static int
|
|
|
|
do_local_cmd(arglist *a)
|
|
|
|
{
|
|
|
|
u_int i;
|
|
|
|
int status;
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
if (a->num == 0)
|
|
|
|
fatal("do_local_cmd: no arguments");
|
|
|
|
|
|
|
|
if (verbose_mode) {
|
|
|
|
fprintf(stderr, "Executing:");
|
|
|
|
for (i = 0; i < a->num; i++)
|
2016-05-25 23:48:45 +00:00
|
|
|
fmprintf(stderr, " %s", a->list[i]);
|
2006-01-31 10:49:27 +00:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
if ((pid = fork()) == -1)
|
|
|
|
fatal("do_local_cmd: fork: %s", strerror(errno));
|
|
|
|
|
|
|
|
if (pid == 0) {
|
|
|
|
execvp(a->list[0], a->list);
|
|
|
|
perror(a->list[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
do_cmd_pid = pid;
|
2020-01-23 07:10:22 +00:00
|
|
|
ssh_signal(SIGTERM, killchild);
|
|
|
|
ssh_signal(SIGINT, killchild);
|
|
|
|
ssh_signal(SIGHUP, killchild);
|
2006-01-31 10:49:27 +00:00
|
|
|
|
|
|
|
while (waitpid(pid, &status, 0) == -1)
|
|
|
|
if (errno != EINTR)
|
|
|
|
fatal("do_local_cmd: waitpid: %s", strerror(errno));
|
|
|
|
|
|
|
|
do_cmd_pid = -1;
|
|
|
|
|
|
|
|
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1999-11-25 00:54:57 +00:00
|
|
|
/*
|
|
|
|
* This function executes the given command as the specified user on the
|
|
|
|
* given host. This returns < 0 if execution fails, and >= 0 otherwise. This
|
|
|
|
* assigns the input and output file descriptors on success.
|
|
|
|
*/
|
1999-10-27 03:42:43 +00:00
|
|
|
|
2000-04-16 01:18:38 +00:00
|
|
|
int
|
2021-08-11 14:05:19 +00:00
|
|
|
do_cmd(char *program, char *host, char *remuser, int port, int subsystem,
|
|
|
|
char *cmd, int *fdin, int *fdout, pid_t *pid)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
1999-11-24 13:26:21 +00:00
|
|
|
int pin[2], pout[2], reserved[2];
|
|
|
|
|
|
|
|
if (verbose_mode)
|
2016-05-25 23:48:45 +00:00
|
|
|
fmprintf(stderr,
|
2001-08-06 21:17:12 +00:00
|
|
|
"Executing: program %s host %s, user %s, command %s\n",
|
2021-08-02 23:38:27 +00:00
|
|
|
program, host,
|
2001-08-06 21:17:12 +00:00
|
|
|
remuser ? remuser : "(unspecified)", cmd);
|
1999-11-24 13:26:21 +00:00
|
|
|
|
2017-10-21 23:06:24 +00:00
|
|
|
if (port == -1)
|
|
|
|
port = sshport;
|
|
|
|
|
1999-11-25 00:54:57 +00:00
|
|
|
/*
|
|
|
|
* Reserve two descriptors so that the real pipes won't get
|
|
|
|
* descriptors 0 and 1 because that will screw up dup2 below.
|
|
|
|
*/
|
2019-06-28 13:35:04 +00:00
|
|
|
if (pipe(reserved) == -1)
|
2006-06-13 03:00:25 +00:00
|
|
|
fatal("pipe: %s", strerror(errno));
|
1999-11-24 13:26:21 +00:00
|
|
|
|
|
|
|
/* Create a socket pair for communicating with ssh. */
|
2019-06-28 13:35:04 +00:00
|
|
|
if (pipe(pin) == -1)
|
1999-11-24 13:26:21 +00:00
|
|
|
fatal("pipe: %s", strerror(errno));
|
2019-06-28 13:35:04 +00:00
|
|
|
if (pipe(pout) == -1)
|
1999-11-24 13:26:21 +00:00
|
|
|
fatal("pipe: %s", strerror(errno));
|
|
|
|
|
|
|
|
/* Free the reserved descriptors. */
|
|
|
|
close(reserved[0]);
|
|
|
|
close(reserved[1]);
|
|
|
|
|
2020-01-23 07:10:22 +00:00
|
|
|
ssh_signal(SIGTSTP, suspchild);
|
|
|
|
ssh_signal(SIGTTIN, suspchild);
|
|
|
|
ssh_signal(SIGTTOU, suspchild);
|
2010-07-02 03:37:33 +00:00
|
|
|
|
2003-06-18 10:26:34 +00:00
|
|
|
/* Fork a child to execute the command on the remote host using ssh. */
|
2021-08-07 00:06:30 +00:00
|
|
|
*pid = fork();
|
|
|
|
if (*pid == 0) {
|
1999-11-24 13:26:21 +00:00
|
|
|
/* Child. */
|
|
|
|
close(pin[1]);
|
|
|
|
close(pout[0]);
|
|
|
|
dup2(pin[0], 0);
|
|
|
|
dup2(pout[1], 1);
|
|
|
|
close(pin[0]);
|
|
|
|
close(pout[1]);
|
|
|
|
|
2021-08-02 23:38:27 +00:00
|
|
|
replacearg(&args, 0, "%s", program);
|
2017-10-21 23:06:24 +00:00
|
|
|
if (port != -1) {
|
|
|
|
addargs(&args, "-p");
|
|
|
|
addargs(&args, "%d", port);
|
|
|
|
}
|
2010-01-08 07:53:43 +00:00
|
|
|
if (remuser != NULL) {
|
|
|
|
addargs(&args, "-l");
|
|
|
|
addargs(&args, "%s", remuser);
|
|
|
|
}
|
2021-08-11 14:05:19 +00:00
|
|
|
if (subsystem)
|
|
|
|
addargs(&args, "-s");
|
2010-01-08 07:53:43 +00:00
|
|
|
addargs(&args, "--");
|
2001-05-08 20:27:25 +00:00
|
|
|
addargs(&args, "%s", host);
|
|
|
|
addargs(&args, "%s", cmd);
|
1999-11-24 13:26:21 +00:00
|
|
|
|
2021-08-02 23:38:27 +00:00
|
|
|
execvp(program, args.list);
|
|
|
|
perror(program);
|
1999-11-24 13:26:21 +00:00
|
|
|
exit(1);
|
2021-08-07 00:06:30 +00:00
|
|
|
} else if (*pid == -1) {
|
2002-11-09 15:54:08 +00:00
|
|
|
fatal("fork: %s", strerror(errno));
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
1999-11-24 13:26:21 +00:00
|
|
|
/* Parent. Close the other side, and return the local side. */
|
|
|
|
close(pin[0]);
|
|
|
|
*fdout = pin[1];
|
|
|
|
close(pout[1]);
|
|
|
|
*fdin = pout[0];
|
2020-01-23 07:10:22 +00:00
|
|
|
ssh_signal(SIGTERM, killchild);
|
|
|
|
ssh_signal(SIGINT, killchild);
|
|
|
|
ssh_signal(SIGHUP, killchild);
|
1999-11-24 13:26:21 +00:00
|
|
|
return 0;
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
|
|
|
|
2011-01-06 11:40:30 +00:00
|
|
|
/*
|
2018-04-10 00:10:49 +00:00
|
|
|
* This function executes a command similar to do_cmd(), but expects the
|
2011-01-06 11:40:30 +00:00
|
|
|
* input and output descriptors to be setup by a previous call to do_cmd().
|
|
|
|
* This way the input and output of two commands can be connected.
|
|
|
|
*/
|
|
|
|
int
|
2021-08-07 00:06:30 +00:00
|
|
|
do_cmd2(char *host, char *remuser, int port, char *cmd,
|
|
|
|
int fdin, int fdout)
|
2011-01-06 11:40:30 +00:00
|
|
|
{
|
|
|
|
int status;
|
2021-08-07 00:06:30 +00:00
|
|
|
pid_t pid;
|
2011-01-06 11:40:30 +00:00
|
|
|
|
|
|
|
if (verbose_mode)
|
2016-05-25 23:48:45 +00:00
|
|
|
fmprintf(stderr,
|
2011-01-06 11:40:30 +00:00
|
|
|
"Executing: 2nd program %s host %s, user %s, command %s\n",
|
|
|
|
ssh_program, host,
|
|
|
|
remuser ? remuser : "(unspecified)", cmd);
|
|
|
|
|
2017-10-21 23:06:24 +00:00
|
|
|
if (port == -1)
|
|
|
|
port = sshport;
|
|
|
|
|
2011-01-06 11:40:30 +00:00
|
|
|
/* Fork a child to execute the command on the remote host using ssh. */
|
|
|
|
pid = fork();
|
|
|
|
if (pid == 0) {
|
|
|
|
dup2(fdin, 0);
|
|
|
|
dup2(fdout, 1);
|
|
|
|
|
|
|
|
replacearg(&args, 0, "%s", ssh_program);
|
2017-10-21 23:06:24 +00:00
|
|
|
if (port != -1) {
|
|
|
|
addargs(&args, "-p");
|
|
|
|
addargs(&args, "%d", port);
|
|
|
|
}
|
2011-01-06 11:40:30 +00:00
|
|
|
if (remuser != NULL) {
|
|
|
|
addargs(&args, "-l");
|
|
|
|
addargs(&args, "%s", remuser);
|
|
|
|
}
|
2020-04-30 17:07:10 +00:00
|
|
|
addargs(&args, "-oBatchMode=yes");
|
2011-01-06 11:40:30 +00:00
|
|
|
addargs(&args, "--");
|
|
|
|
addargs(&args, "%s", host);
|
|
|
|
addargs(&args, "%s", cmd);
|
|
|
|
|
|
|
|
execvp(ssh_program, args.list);
|
|
|
|
perror(ssh_program);
|
|
|
|
exit(1);
|
|
|
|
} else if (pid == -1) {
|
|
|
|
fatal("fork: %s", strerror(errno));
|
|
|
|
}
|
|
|
|
while (waitpid(pid, &status, 0) == -1)
|
|
|
|
if (errno != EINTR)
|
|
|
|
fatal("do_cmd2: waitpid: %s", strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-10-27 03:42:43 +00:00
|
|
|
typedef struct {
|
2005-06-17 02:59:34 +00:00
|
|
|
size_t cnt;
|
1999-10-27 03:42:43 +00:00
|
|
|
char *buf;
|
|
|
|
} BUF;
|
|
|
|
|
1999-11-24 13:26:21 +00:00
|
|
|
BUF *allocbuf(BUF *, int, int);
|
|
|
|
void lostconn(int);
|
|
|
|
int okname(char *);
|
2020-05-29 21:22:02 +00:00
|
|
|
void run_err(const char *,...)
|
|
|
|
__attribute__((__format__ (printf, 1, 2)))
|
|
|
|
__attribute__((__nonnull__ (1)));
|
|
|
|
int note_err(const char *,...)
|
|
|
|
__attribute__((__format__ (printf, 1, 2)));
|
1999-11-24 13:26:21 +00:00
|
|
|
void verifydir(char *);
|
1999-10-27 03:42:43 +00:00
|
|
|
|
|
|
|
struct passwd *pwd;
|
1999-11-24 13:26:21 +00:00
|
|
|
uid_t userid;
|
2021-08-07 00:06:30 +00:00
|
|
|
int errs, remin, remout, remin2, remout2;
|
2019-01-26 22:41:28 +00:00
|
|
|
int Tflag, pflag, iamremote, iamrecursive, targetshouldbedirectory;
|
1999-10-27 03:42:43 +00:00
|
|
|
|
|
|
|
#define CMDNEEDS 64
|
|
|
|
char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
|
|
|
|
|
2021-08-02 23:38:27 +00:00
|
|
|
enum scp_mode_e {
|
|
|
|
MODE_SCP,
|
|
|
|
MODE_SFTP
|
|
|
|
};
|
|
|
|
|
1999-11-24 13:26:21 +00:00
|
|
|
int response(void);
|
|
|
|
void rsource(char *, struct stat *);
|
2019-01-26 22:41:28 +00:00
|
|
|
void sink(int, char *[], const char *);
|
1999-11-24 13:26:21 +00:00
|
|
|
void source(int, char *[]);
|
2021-08-02 23:38:27 +00:00
|
|
|
void tolocal(int, char *[], enum scp_mode_e, char *sftp_direct);
|
|
|
|
void toremote(int, char *[], enum scp_mode_e, char *sftp_direct);
|
1999-11-24 13:26:21 +00:00
|
|
|
void usage(void);
|
1999-10-27 03:42:43 +00:00
|
|
|
|
2021-08-09 23:49:31 +00:00
|
|
|
void source_sftp(int, char *, char *, struct sftp_conn *);
|
2021-08-02 23:38:27 +00:00
|
|
|
void sink_sftp(int, char *, const char *, struct sftp_conn *);
|
2021-08-07 00:06:30 +00:00
|
|
|
void throughlocal_sftp(struct sftp_conn *, struct sftp_conn *,
|
2021-08-09 23:49:31 +00:00
|
|
|
char *, char *);
|
2021-08-02 23:38:27 +00:00
|
|
|
|
1999-10-27 03:42:43 +00:00
|
|
|
int
|
2003-06-04 12:51:08 +00:00
|
|
|
main(int argc, char **argv)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
2006-07-10 12:19:53 +00:00
|
|
|
int ch, fflag, tflag, status, n;
|
2021-08-02 23:38:27 +00:00
|
|
|
char **newargv, *argv0;
|
2010-09-24 12:15:11 +00:00
|
|
|
const char *errstr;
|
1999-10-27 03:42:43 +00:00
|
|
|
extern char *optarg;
|
|
|
|
extern int optind;
|
2021-10-15 14:46:46 +00:00
|
|
|
enum scp_mode_e mode = MODE_SFTP;
|
2021-08-02 23:38:27 +00:00
|
|
|
char *sftp_direct = NULL;
|
1999-10-27 03:42:43 +00:00
|
|
|
|
2005-10-03 08:11:24 +00:00
|
|
|
/* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
|
|
|
|
sanitise_stdfd();
|
|
|
|
|
2018-11-22 23:40:06 +00:00
|
|
|
seed_rng();
|
|
|
|
|
2016-12-12 02:57:10 +00:00
|
|
|
msetlocale();
|
2016-05-25 23:48:45 +00:00
|
|
|
|
2006-07-10 12:19:53 +00:00
|
|
|
/* Copy argv, because we modify it */
|
2021-08-02 23:38:27 +00:00
|
|
|
argv0 = argv[0];
|
2016-09-12 01:22:38 +00:00
|
|
|
newargv = xcalloc(MAXIMUM(argc + 1, 1), sizeof(*newargv));
|
2006-07-10 12:19:53 +00:00
|
|
|
for (n = 0; n < argc; n++)
|
|
|
|
newargv[n] = xstrdup(argv[n]);
|
|
|
|
argv = newargv;
|
|
|
|
|
2003-08-21 23:34:41 +00:00
|
|
|
__progname = ssh_get_progname(argv[0]);
|
2000-11-17 03:47:20 +00:00
|
|
|
|
2021-09-16 15:11:19 +00:00
|
|
|
log_init(argv0, log_level, SYSLOG_FACILITY_USER, 2);
|
2021-08-07 00:00:33 +00:00
|
|
|
|
2006-01-31 10:49:27 +00:00
|
|
|
memset(&args, '\0', sizeof(args));
|
2010-12-01 01:03:19 +00:00
|
|
|
memset(&remote_remote_args, '\0', sizeof(remote_remote_args));
|
|
|
|
args.list = remote_remote_args.list = NULL;
|
2006-01-31 10:49:27 +00:00
|
|
|
addargs(&args, "%s", ssh_program);
|
2001-05-08 20:27:25 +00:00
|
|
|
addargs(&args, "-x");
|
2010-12-01 01:03:19 +00:00
|
|
|
addargs(&args, "-oPermitLocalCommand=no");
|
|
|
|
addargs(&args, "-oClearAllForwardings=yes");
|
2018-02-10 06:15:12 +00:00
|
|
|
addargs(&args, "-oRemoteCommand=none");
|
|
|
|
addargs(&args, "-oRequestTTY=no");
|
2000-10-14 05:23:11 +00:00
|
|
|
|
2019-01-26 22:41:28 +00:00
|
|
|
fflag = Tflag = tflag = 0;
|
|
|
|
while ((ch = getopt(argc, argv,
|
2021-08-10 03:33:34 +00:00
|
|
|
"12346ABCTdfOpqRrstvD:F:J:M:P:S:c:i:l:o:")) != -1) {
|
1999-11-24 13:26:21 +00:00
|
|
|
switch (ch) {
|
|
|
|
/* User-visible flags. */
|
2003-01-24 00:37:38 +00:00
|
|
|
case '1':
|
2017-04-30 23:28:12 +00:00
|
|
|
fatal("SSH protocol v.1 is no longer supported");
|
|
|
|
break;
|
2003-01-24 00:37:38 +00:00
|
|
|
case '2':
|
2017-04-30 23:28:12 +00:00
|
|
|
/* Ignored */
|
|
|
|
break;
|
2020-08-03 02:43:41 +00:00
|
|
|
case 'A':
|
2000-01-14 04:45:46 +00:00
|
|
|
case '4':
|
|
|
|
case '6':
|
2000-10-14 05:23:11 +00:00
|
|
|
case 'C':
|
2001-05-08 20:27:25 +00:00
|
|
|
addargs(&args, "-%c", ch);
|
2010-12-01 01:03:19 +00:00
|
|
|
addargs(&remote_remote_args, "-%c", ch);
|
2000-01-14 04:45:46 +00:00
|
|
|
break;
|
2021-08-02 23:38:27 +00:00
|
|
|
case 'D':
|
|
|
|
sftp_direct = optarg;
|
|
|
|
break;
|
2011-01-06 11:40:30 +00:00
|
|
|
case '3':
|
|
|
|
throughlocal = 1;
|
|
|
|
break;
|
2021-08-09 23:56:36 +00:00
|
|
|
case 'R':
|
|
|
|
throughlocal = 0;
|
|
|
|
break;
|
2000-10-14 05:23:11 +00:00
|
|
|
case 'o':
|
|
|
|
case 'c':
|
|
|
|
case 'i':
|
2001-09-18 05:38:44 +00:00
|
|
|
case 'F':
|
2019-01-21 22:50:42 +00:00
|
|
|
case 'J':
|
2010-12-01 01:03:19 +00:00
|
|
|
addargs(&remote_remote_args, "-%c", ch);
|
|
|
|
addargs(&remote_remote_args, "%s", optarg);
|
2010-01-08 07:53:43 +00:00
|
|
|
addargs(&args, "-%c", ch);
|
|
|
|
addargs(&args, "%s", optarg);
|
1999-10-27 03:42:43 +00:00
|
|
|
break;
|
2021-08-10 03:33:34 +00:00
|
|
|
case 'O':
|
|
|
|
mode = MODE_SCP;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
mode = MODE_SFTP;
|
|
|
|
break;
|
1999-10-27 03:42:43 +00:00
|
|
|
case 'P':
|
2017-10-21 23:06:24 +00:00
|
|
|
sshport = a2port(optarg);
|
|
|
|
if (sshport <= 0)
|
|
|
|
fatal("bad port \"%s\"\n", optarg);
|
2000-10-14 05:23:11 +00:00
|
|
|
break;
|
|
|
|
case 'B':
|
2010-12-01 01:03:19 +00:00
|
|
|
addargs(&remote_remote_args, "-oBatchmode=yes");
|
|
|
|
addargs(&args, "-oBatchmode=yes");
|
2000-10-14 05:23:11 +00:00
|
|
|
break;
|
2003-01-24 00:36:58 +00:00
|
|
|
case 'l':
|
2010-09-24 12:15:11 +00:00
|
|
|
limit_kbps = strtonum(optarg, 1, 100 * 1024 * 1024,
|
|
|
|
&errstr);
|
|
|
|
if (errstr != NULL)
|
2003-01-24 00:36:58 +00:00
|
|
|
usage();
|
2010-09-24 12:15:11 +00:00
|
|
|
limit_kbps *= 1024; /* kbps */
|
|
|
|
bandwidth_limit_init(&bwlimit, limit_kbps, COPY_BUFLEN);
|
2003-01-24 00:36:58 +00:00
|
|
|
break;
|
2000-10-14 05:23:11 +00:00
|
|
|
case 'p':
|
|
|
|
pflag = 1;
|
1999-11-24 13:26:21 +00:00
|
|
|
break;
|
1999-10-27 03:42:43 +00:00
|
|
|
case 'r':
|
|
|
|
iamrecursive = 1;
|
|
|
|
break;
|
2000-08-23 00:46:23 +00:00
|
|
|
case 'S':
|
2000-10-14 05:23:11 +00:00
|
|
|
ssh_program = xstrdup(optarg);
|
|
|
|
break;
|
|
|
|
case 'v':
|
2001-06-09 01:15:11 +00:00
|
|
|
addargs(&args, "-v");
|
2010-12-01 01:03:19 +00:00
|
|
|
addargs(&remote_remote_args, "-v");
|
2021-08-02 23:38:27 +00:00
|
|
|
if (verbose_mode == 0)
|
|
|
|
log_level = SYSLOG_LEVEL_DEBUG1;
|
|
|
|
else if (log_level < SYSLOG_LEVEL_DEBUG3)
|
|
|
|
log_level++;
|
2000-10-14 05:23:11 +00:00
|
|
|
verbose_mode = 1;
|
|
|
|
break;
|
|
|
|
case 'q':
|
2003-11-17 10:19:05 +00:00
|
|
|
addargs(&args, "-q");
|
2010-12-01 01:03:19 +00:00
|
|
|
addargs(&remote_remote_args, "-q");
|
2000-10-14 05:23:11 +00:00
|
|
|
showprogress = 0;
|
2000-08-23 00:46:23 +00:00
|
|
|
break;
|
|
|
|
|
1999-11-24 13:26:21 +00:00
|
|
|
/* Server options. */
|
1999-10-27 03:42:43 +00:00
|
|
|
case 'd':
|
|
|
|
targetshouldbedirectory = 1;
|
|
|
|
break;
|
1999-11-24 13:26:21 +00:00
|
|
|
case 'f': /* "from" */
|
1999-10-27 03:42:43 +00:00
|
|
|
iamremote = 1;
|
|
|
|
fflag = 1;
|
|
|
|
break;
|
1999-11-24 13:26:21 +00:00
|
|
|
case 't': /* "to" */
|
1999-10-27 03:42:43 +00:00
|
|
|
iamremote = 1;
|
|
|
|
tflag = 1;
|
2001-04-13 14:28:42 +00:00
|
|
|
#ifdef HAVE_CYGWIN
|
|
|
|
setmode(0, O_BINARY);
|
|
|
|
#endif
|
1999-10-27 03:42:43 +00:00
|
|
|
break;
|
2019-01-26 22:41:28 +00:00
|
|
|
case 'T':
|
|
|
|
Tflag = 1;
|
|
|
|
break;
|
1999-10-27 03:42:43 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
2019-01-26 22:41:28 +00:00
|
|
|
}
|
1999-10-27 03:42:43 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2021-09-16 15:11:19 +00:00
|
|
|
log_init(argv0, log_level, SYSLOG_FACILITY_USER, 2);
|
2021-08-02 23:38:27 +00:00
|
|
|
|
2020-08-03 02:43:41 +00:00
|
|
|
/* Do this last because we want the user to be able to override it */
|
|
|
|
addargs(&args, "-oForwardAgent=no");
|
|
|
|
|
2021-08-07 00:00:33 +00:00
|
|
|
if (iamremote)
|
|
|
|
mode = MODE_SCP;
|
2021-08-02 23:38:27 +00:00
|
|
|
|
1999-10-27 03:42:43 +00:00
|
|
|
if ((pwd = getpwuid(userid = getuid())) == NULL)
|
2003-07-19 10:07:45 +00:00
|
|
|
fatal("unknown user %u", (u_int) userid);
|
1999-10-27 03:42:43 +00:00
|
|
|
|
2007-02-19 11:14:11 +00:00
|
|
|
if (!isatty(STDOUT_FILENO))
|
1999-10-27 03:42:43 +00:00
|
|
|
showprogress = 0;
|
|
|
|
|
2015-11-27 00:49:31 +00:00
|
|
|
if (pflag) {
|
|
|
|
/* Cannot pledge: -p allows setuid/setgid files... */
|
|
|
|
} else {
|
|
|
|
if (pledge("stdio rpath wpath cpath fattr tty proc exec",
|
|
|
|
NULL) == -1) {
|
|
|
|
perror("pledge");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-10-27 03:42:43 +00:00
|
|
|
remin = STDIN_FILENO;
|
|
|
|
remout = STDOUT_FILENO;
|
|
|
|
|
2001-02-05 12:42:17 +00:00
|
|
|
if (fflag) {
|
1999-11-24 13:26:21 +00:00
|
|
|
/* Follow "protocol", send data. */
|
|
|
|
(void) response();
|
1999-10-27 03:42:43 +00:00
|
|
|
source(argc, argv);
|
|
|
|
exit(errs != 0);
|
|
|
|
}
|
1999-11-24 13:26:21 +00:00
|
|
|
if (tflag) {
|
|
|
|
/* Receive data. */
|
2019-01-26 22:41:28 +00:00
|
|
|
sink(argc, argv, NULL);
|
1999-10-27 03:42:43 +00:00
|
|
|
exit(errs != 0);
|
|
|
|
}
|
|
|
|
if (argc < 2)
|
|
|
|
usage();
|
|
|
|
if (argc > 2)
|
|
|
|
targetshouldbedirectory = 1;
|
|
|
|
|
|
|
|
remin = remout = -1;
|
2002-11-09 15:54:08 +00:00
|
|
|
do_cmd_pid = -1;
|
1999-10-27 03:42:43 +00:00
|
|
|
/* Command to be executed on remote system using "ssh". */
|
2001-02-09 01:23:39 +00:00
|
|
|
(void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
|
|
|
|
verbose_mode ? " -v" : "",
|
2000-08-23 00:46:23 +00:00
|
|
|
iamrecursive ? " -r" : "", pflag ? " -p" : "",
|
|
|
|
targetshouldbedirectory ? " -d" : "");
|
1999-10-27 03:42:43 +00:00
|
|
|
|
2020-01-23 07:10:22 +00:00
|
|
|
(void) ssh_signal(SIGPIPE, lostconn);
|
1999-10-27 03:42:43 +00:00
|
|
|
|
2017-10-21 23:06:24 +00:00
|
|
|
if (colon(argv[argc - 1])) /* Dest is remote host. */
|
2021-08-02 23:38:27 +00:00
|
|
|
toremote(argc, argv, mode, sftp_direct);
|
1999-10-27 03:42:43 +00:00
|
|
|
else {
|
|
|
|
if (targetshouldbedirectory)
|
|
|
|
verifydir(argv[argc - 1]);
|
2021-08-02 23:38:27 +00:00
|
|
|
tolocal(argc, argv, mode, sftp_direct); /* Dest is local host. */
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
2002-11-09 15:54:08 +00:00
|
|
|
/*
|
|
|
|
* Finally check the exit status of the ssh process, if one was forked
|
2008-11-03 08:23:45 +00:00
|
|
|
* and no error has occurred yet
|
2002-11-09 15:54:08 +00:00
|
|
|
*/
|
2021-09-11 00:40:24 +00:00
|
|
|
if (do_cmd_pid != -1 && (mode == MODE_SFTP || errs == 0)) {
|
2002-11-09 15:54:08 +00:00
|
|
|
if (remin != -1)
|
|
|
|
(void) close(remin);
|
|
|
|
if (remout != -1)
|
|
|
|
(void) close(remout);
|
|
|
|
if (waitpid(do_cmd_pid, &status, 0) == -1)
|
|
|
|
errs = 1;
|
|
|
|
else {
|
|
|
|
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
|
|
|
|
errs = 1;
|
|
|
|
}
|
|
|
|
}
|
1999-10-27 03:42:43 +00:00
|
|
|
exit(errs != 0);
|
|
|
|
}
|
|
|
|
|
2010-09-24 12:15:11 +00:00
|
|
|
/* Callback from atomicio6 to update progress meter and limit bandwidth */
|
|
|
|
static int
|
|
|
|
scpio(void *_cnt, size_t s)
|
2007-10-26 05:39:15 +00:00
|
|
|
{
|
2010-09-24 12:15:11 +00:00
|
|
|
off_t *cnt = (off_t *)_cnt;
|
|
|
|
|
|
|
|
*cnt += s;
|
2019-01-24 16:52:17 +00:00
|
|
|
refresh_progress_meter(0);
|
2010-09-24 12:15:11 +00:00
|
|
|
if (limit_kbps > 0)
|
|
|
|
bandwidth_limit(&bwlimit, s);
|
|
|
|
return 0;
|
2007-10-26 05:39:15 +00:00
|
|
|
}
|
|
|
|
|
2013-07-18 06:09:25 +00:00
|
|
|
static int
|
|
|
|
do_times(int fd, int verb, const struct stat *sb)
|
|
|
|
{
|
|
|
|
/* strlen(2^64) == 20; strlen(10^6) == 7 */
|
|
|
|
char buf[(20 + 7 + 2) * 2 + 2];
|
|
|
|
|
|
|
|
(void)snprintf(buf, sizeof(buf), "T%llu 0 %llu 0\n",
|
|
|
|
(unsigned long long) (sb->st_mtime < 0 ? 0 : sb->st_mtime),
|
|
|
|
(unsigned long long) (sb->st_atime < 0 ? 0 : sb->st_atime));
|
|
|
|
if (verb) {
|
|
|
|
fprintf(stderr, "File mtime %lld atime %lld\n",
|
|
|
|
(long long)sb->st_mtime, (long long)sb->st_atime);
|
|
|
|
fprintf(stderr, "Sending file timestamps: %s", buf);
|
|
|
|
}
|
|
|
|
(void) atomicio(vwrite, fd, buf, strlen(buf));
|
|
|
|
return (response());
|
|
|
|
}
|
|
|
|
|
2017-12-18 17:28:54 +00:00
|
|
|
static int
|
|
|
|
parse_scp_uri(const char *uri, char **userp, char **hostp, int *portp,
|
2021-04-03 06:18:40 +00:00
|
|
|
char **pathp)
|
2017-12-18 17:28:54 +00:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = parse_uri("scp", uri, userp, hostp, portp, pathp);
|
|
|
|
if (r == 0 && *pathp == NULL)
|
|
|
|
*pathp = xstrdup(".");
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2019-02-10 11:15:52 +00:00
|
|
|
/* Appends a string to an array; returns 0 on success, -1 on alloc failure */
|
|
|
|
static int
|
|
|
|
append(char *cp, char ***ap, size_t *np)
|
|
|
|
{
|
|
|
|
char **tmp;
|
|
|
|
|
|
|
|
if ((tmp = reallocarray(*ap, *np + 1, sizeof(*tmp))) == NULL)
|
|
|
|
return -1;
|
|
|
|
tmp[(*np)] = cp;
|
|
|
|
(*np)++;
|
|
|
|
*ap = tmp;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Finds the start and end of the first brace pair in the pattern.
|
|
|
|
* returns 0 on success or -1 for invalid patterns.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
find_brace(const char *pattern, int *startp, int *endp)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int in_bracket, brace_level;
|
|
|
|
|
|
|
|
*startp = *endp = -1;
|
|
|
|
in_bracket = brace_level = 0;
|
|
|
|
for (i = 0; i < INT_MAX && *endp < 0 && pattern[i] != '\0'; i++) {
|
|
|
|
switch (pattern[i]) {
|
|
|
|
case '\\':
|
|
|
|
/* skip next character */
|
|
|
|
if (pattern[i + 1] != '\0')
|
|
|
|
i++;
|
|
|
|
break;
|
|
|
|
case '[':
|
|
|
|
in_bracket = 1;
|
|
|
|
break;
|
|
|
|
case ']':
|
|
|
|
in_bracket = 0;
|
|
|
|
break;
|
|
|
|
case '{':
|
|
|
|
if (in_bracket)
|
|
|
|
break;
|
|
|
|
if (pattern[i + 1] == '}') {
|
|
|
|
/* Protect a single {}, for find(1), like csh */
|
|
|
|
i++; /* skip */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*startp == -1)
|
|
|
|
*startp = i;
|
|
|
|
brace_level++;
|
|
|
|
break;
|
|
|
|
case '}':
|
|
|
|
if (in_bracket)
|
|
|
|
break;
|
|
|
|
if (*startp < 0) {
|
|
|
|
/* Unbalanced brace */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (--brace_level <= 0)
|
|
|
|
*endp = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* unbalanced brackets/braces */
|
|
|
|
if (*endp < 0 && (*startp >= 0 || in_bracket))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Assembles and records a successfully-expanded pattern, returns -1 on
|
|
|
|
* alloc failure.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
emit_expansion(const char *pattern, int brace_start, int brace_end,
|
|
|
|
int sel_start, int sel_end, char ***patternsp, size_t *npatternsp)
|
|
|
|
{
|
|
|
|
char *cp;
|
|
|
|
int o = 0, tail_len = strlen(pattern + brace_end + 1);
|
|
|
|
|
|
|
|
if ((cp = malloc(brace_start + (sel_end - sel_start) +
|
|
|
|
tail_len + 1)) == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Pattern before initial brace */
|
|
|
|
if (brace_start > 0) {
|
|
|
|
memcpy(cp, pattern, brace_start);
|
|
|
|
o = brace_start;
|
|
|
|
}
|
|
|
|
/* Current braced selection */
|
|
|
|
if (sel_end - sel_start > 0) {
|
|
|
|
memcpy(cp + o, pattern + sel_start,
|
|
|
|
sel_end - sel_start);
|
|
|
|
o += sel_end - sel_start;
|
|
|
|
}
|
|
|
|
/* Remainder of pattern after closing brace */
|
|
|
|
if (tail_len > 0) {
|
|
|
|
memcpy(cp + o, pattern + brace_end + 1, tail_len);
|
|
|
|
o += tail_len;
|
|
|
|
}
|
|
|
|
cp[o] = '\0';
|
|
|
|
if (append(cp, patternsp, npatternsp) != 0) {
|
|
|
|
free(cp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Expand the first encountered brace in pattern, appending the expanded
|
|
|
|
* patterns it yielded to the *patternsp array.
|
|
|
|
*
|
|
|
|
* Returns 0 on success or -1 on allocation failure.
|
|
|
|
*
|
|
|
|
* Signals whether expansion was performed via *expanded and whether
|
|
|
|
* pattern was invalid via *invalid.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
brace_expand_one(const char *pattern, char ***patternsp, size_t *npatternsp,
|
|
|
|
int *expanded, int *invalid)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int in_bracket, brace_start, brace_end, brace_level;
|
|
|
|
int sel_start, sel_end;
|
|
|
|
|
|
|
|
*invalid = *expanded = 0;
|
|
|
|
|
|
|
|
if (find_brace(pattern, &brace_start, &brace_end) != 0) {
|
|
|
|
*invalid = 1;
|
|
|
|
return 0;
|
|
|
|
} else if (brace_start == -1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
in_bracket = brace_level = 0;
|
|
|
|
for (i = sel_start = brace_start + 1; i < brace_end; i++) {
|
|
|
|
switch (pattern[i]) {
|
|
|
|
case '{':
|
|
|
|
if (in_bracket)
|
|
|
|
break;
|
|
|
|
brace_level++;
|
|
|
|
break;
|
|
|
|
case '}':
|
|
|
|
if (in_bracket)
|
|
|
|
break;
|
|
|
|
brace_level--;
|
|
|
|
break;
|
|
|
|
case '[':
|
|
|
|
in_bracket = 1;
|
|
|
|
break;
|
|
|
|
case ']':
|
|
|
|
in_bracket = 0;
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
if (i < brace_end - 1)
|
|
|
|
i++; /* skip */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (pattern[i] == ',' || i == brace_end - 1) {
|
|
|
|
if (in_bracket || brace_level > 0)
|
|
|
|
continue;
|
|
|
|
/* End of a selection, emit an expanded pattern */
|
|
|
|
|
|
|
|
/* Adjust end index for last selection */
|
|
|
|
sel_end = (i == brace_end - 1) ? brace_end : i;
|
|
|
|
if (emit_expansion(pattern, brace_start, brace_end,
|
|
|
|
sel_start, sel_end, patternsp, npatternsp) != 0)
|
|
|
|
return -1;
|
|
|
|
/* move on to the next selection */
|
|
|
|
sel_start = i + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (in_bracket || brace_level > 0) {
|
|
|
|
*invalid = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* success */
|
|
|
|
*expanded = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Expand braces from pattern. Returns 0 on success, -1 on failure */
|
|
|
|
static int
|
|
|
|
brace_expand(const char *pattern, char ***patternsp, size_t *npatternsp)
|
|
|
|
{
|
|
|
|
char *cp, *cp2, **active = NULL, **done = NULL;
|
|
|
|
size_t i, nactive = 0, ndone = 0;
|
|
|
|
int ret = -1, invalid = 0, expanded = 0;
|
|
|
|
|
|
|
|
*patternsp = NULL;
|
|
|
|
*npatternsp = 0;
|
|
|
|
|
|
|
|
/* Start the worklist with the original pattern */
|
|
|
|
if ((cp = strdup(pattern)) == NULL)
|
|
|
|
return -1;
|
|
|
|
if (append(cp, &active, &nactive) != 0) {
|
|
|
|
free(cp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
while (nactive > 0) {
|
|
|
|
cp = active[nactive - 1];
|
|
|
|
nactive--;
|
|
|
|
if (brace_expand_one(cp, &active, &nactive,
|
|
|
|
&expanded, &invalid) == -1) {
|
|
|
|
free(cp);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (invalid)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_f("invalid brace pattern \"%s\"", cp);
|
2019-02-10 11:15:52 +00:00
|
|
|
if (expanded) {
|
|
|
|
/*
|
|
|
|
* Current entry expanded to new entries on the
|
|
|
|
* active list; discard the progenitor pattern.
|
|
|
|
*/
|
|
|
|
free(cp);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Pattern did not expand; append the finename component to
|
|
|
|
* the completed list
|
|
|
|
*/
|
|
|
|
if ((cp2 = strrchr(cp, '/')) != NULL)
|
|
|
|
*cp2++ = '\0';
|
|
|
|
else
|
|
|
|
cp2 = cp;
|
|
|
|
if (append(xstrdup(cp2), &done, &ndone) != 0) {
|
|
|
|
free(cp);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
free(cp);
|
|
|
|
}
|
|
|
|
/* success */
|
|
|
|
*patternsp = done;
|
|
|
|
*npatternsp = ndone;
|
|
|
|
done = NULL;
|
|
|
|
ndone = 0;
|
|
|
|
ret = 0;
|
|
|
|
fail:
|
|
|
|
for (i = 0; i < nactive; i++)
|
|
|
|
free(active[i]);
|
|
|
|
free(active);
|
|
|
|
for (i = 0; i < ndone; i++)
|
|
|
|
free(done[i]);
|
|
|
|
free(done);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-08-02 23:38:27 +00:00
|
|
|
static struct sftp_conn *
|
2021-08-07 00:06:30 +00:00
|
|
|
do_sftp_connect(char *host, char *user, int port, char *sftp_direct,
|
|
|
|
int *reminp, int *remoutp, int *pidp)
|
2021-08-02 23:38:27 +00:00
|
|
|
{
|
|
|
|
if (sftp_direct == NULL) {
|
2021-08-11 14:05:19 +00:00
|
|
|
if (do_cmd(ssh_program, host, user, port, 1, "sftp",
|
2021-08-07 00:06:30 +00:00
|
|
|
reminp, remoutp, pidp) < 0)
|
2021-08-02 23:38:27 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
args.list = NULL;
|
|
|
|
addargs(&args, "sftp-server");
|
2021-08-11 14:05:19 +00:00
|
|
|
if (do_cmd(sftp_direct, host, NULL, -1, 0, "sftp",
|
2021-08-07 00:06:30 +00:00
|
|
|
reminp, remoutp, pidp) < 0)
|
2021-08-02 23:38:27 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2021-08-07 00:06:30 +00:00
|
|
|
return do_init(*reminp, *remoutp, 32768, 64, limit_kbps);
|
2021-08-02 23:38:27 +00:00
|
|
|
}
|
|
|
|
|
1999-10-27 03:42:43 +00:00
|
|
|
void
|
2021-08-02 23:38:27 +00:00
|
|
|
toremote(int argc, char **argv, enum scp_mode_e mode, char *sftp_direct)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
2017-10-21 23:06:24 +00:00
|
|
|
char *suser = NULL, *host = NULL, *src = NULL;
|
|
|
|
char *bp, *tuser, *thost, *targ;
|
|
|
|
int sport = -1, tport = -1;
|
2021-08-07 00:06:30 +00:00
|
|
|
struct sftp_conn *conn = NULL, *conn2 = NULL;
|
2006-01-31 10:49:27 +00:00
|
|
|
arglist alist;
|
2021-08-07 00:06:30 +00:00
|
|
|
int i, r, status;
|
2010-12-01 01:03:19 +00:00
|
|
|
u_int j;
|
2006-01-31 10:49:27 +00:00
|
|
|
|
|
|
|
memset(&alist, '\0', sizeof(alist));
|
|
|
|
alist.list = NULL;
|
1999-10-27 03:42:43 +00:00
|
|
|
|
2017-10-21 23:06:24 +00:00
|
|
|
/* Parse target */
|
2017-12-18 17:28:54 +00:00
|
|
|
r = parse_scp_uri(argv[argc - 1], &tuser, &thost, &tport, &targ);
|
|
|
|
if (r == -1) {
|
|
|
|
fmprintf(stderr, "%s: invalid uri\n", argv[argc - 1]);
|
|
|
|
++errs;
|
|
|
|
goto out;
|
|
|
|
}
|
2017-10-21 23:06:24 +00:00
|
|
|
if (r != 0) {
|
|
|
|
if (parse_user_host_path(argv[argc - 1], &tuser, &thost,
|
2017-12-18 17:28:54 +00:00
|
|
|
&targ) == -1) {
|
|
|
|
fmprintf(stderr, "%s: invalid target\n", argv[argc - 1]);
|
|
|
|
++errs;
|
2017-10-21 23:06:24 +00:00
|
|
|
goto out;
|
2017-12-18 17:28:54 +00:00
|
|
|
}
|
2006-01-31 10:49:27 +00:00
|
|
|
}
|
|
|
|
|
2017-10-21 23:06:24 +00:00
|
|
|
/* Parse source files */
|
1999-10-27 03:42:43 +00:00
|
|
|
for (i = 0; i < argc - 1; i++) {
|
2017-10-21 23:06:24 +00:00
|
|
|
free(suser);
|
|
|
|
free(host);
|
|
|
|
free(src);
|
2017-12-18 17:28:54 +00:00
|
|
|
r = parse_scp_uri(argv[i], &suser, &host, &sport, &src);
|
|
|
|
if (r == -1) {
|
|
|
|
fmprintf(stderr, "%s: invalid uri\n", argv[i]);
|
|
|
|
++errs;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (r != 0) {
|
2017-10-21 23:06:24 +00:00
|
|
|
parse_user_host_path(argv[i], &suser, &host, &src);
|
2017-12-18 17:28:54 +00:00
|
|
|
}
|
2017-10-21 23:06:24 +00:00
|
|
|
if (suser != NULL && !okname(suser)) {
|
|
|
|
++errs;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (host && throughlocal) { /* extended remote to remote */
|
2021-08-07 00:06:30 +00:00
|
|
|
if (mode == MODE_SFTP) {
|
|
|
|
if (remin == -1) {
|
|
|
|
/* Connect to dest now */
|
|
|
|
conn = do_sftp_connect(thost, tuser,
|
|
|
|
tport, sftp_direct,
|
|
|
|
&remin, &remout, &do_cmd_pid);
|
|
|
|
if (conn == NULL) {
|
|
|
|
fatal("Unable to open "
|
|
|
|
"destination connection");
|
|
|
|
}
|
|
|
|
debug3_f("origin in %d out %d pid %ld",
|
|
|
|
remin, remout, (long)do_cmd_pid);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* XXX remember suser/host/sport and only
|
|
|
|
* reconnect if they change between arguments.
|
|
|
|
* would save reconnections for cases like
|
|
|
|
* scp -3 hosta:/foo hosta:/bar hostb:
|
|
|
|
*/
|
|
|
|
/* Connect to origin now */
|
|
|
|
conn2 = do_sftp_connect(host, suser,
|
|
|
|
sport, sftp_direct,
|
|
|
|
&remin2, &remout2, &do_cmd_pid2);
|
|
|
|
if (conn2 == NULL) {
|
|
|
|
fatal("Unable to open "
|
|
|
|
"source connection");
|
|
|
|
}
|
|
|
|
debug3_f("destination in %d out %d pid %ld",
|
|
|
|
remin2, remout2, (long)do_cmd_pid2);
|
2021-08-09 23:49:31 +00:00
|
|
|
throughlocal_sftp(conn2, conn, src, targ);
|
2021-08-07 00:06:30 +00:00
|
|
|
(void) close(remin2);
|
|
|
|
(void) close(remout2);
|
|
|
|
remin2 = remout2 = -1;
|
|
|
|
if (waitpid(do_cmd_pid2, &status, 0) == -1)
|
|
|
|
++errs;
|
|
|
|
else if (!WIFEXITED(status) ||
|
|
|
|
WEXITSTATUS(status) != 0)
|
|
|
|
++errs;
|
|
|
|
do_cmd_pid2 = -1;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
xasprintf(&bp, "%s -f %s%s", cmd,
|
|
|
|
*src == '-' ? "-- " : "", src);
|
2021-08-11 14:05:19 +00:00
|
|
|
if (do_cmd(ssh_program, host, suser, sport, 0,
|
2021-08-07 00:06:30 +00:00
|
|
|
bp, &remin, &remout, &do_cmd_pid) < 0)
|
|
|
|
exit(1);
|
|
|
|
free(bp);
|
|
|
|
xasprintf(&bp, "%s -t %s%s", cmd,
|
|
|
|
*targ == '-' ? "-- " : "", targ);
|
|
|
|
if (do_cmd2(thost, tuser, tport, bp,
|
|
|
|
remin, remout) < 0)
|
|
|
|
exit(1);
|
|
|
|
free(bp);
|
|
|
|
(void) close(remin);
|
|
|
|
(void) close(remout);
|
|
|
|
remin = remout = -1;
|
|
|
|
}
|
2017-10-21 23:06:24 +00:00
|
|
|
} else if (host) { /* standard remote to remote */
|
2021-07-05 00:25:42 +00:00
|
|
|
/*
|
|
|
|
* Second remote user is passed to first remote side
|
|
|
|
* via scp command-line. Ensure it contains no obvious
|
|
|
|
* shell characters.
|
|
|
|
*/
|
|
|
|
if (tuser != NULL && !okname(tuser)) {
|
|
|
|
++errs;
|
|
|
|
continue;
|
|
|
|
}
|
2017-10-21 23:06:24 +00:00
|
|
|
if (tport != -1 && tport != SSH_DEFAULT_PORT) {
|
|
|
|
/* This would require the remote support URIs */
|
|
|
|
fatal("target port not supported with two "
|
2021-08-11 14:07:54 +00:00
|
|
|
"remote hosts and the -R option");
|
2017-10-21 23:06:24 +00:00
|
|
|
}
|
|
|
|
|
2006-01-31 10:49:27 +00:00
|
|
|
freeargs(&alist);
|
|
|
|
addargs(&alist, "%s", ssh_program);
|
|
|
|
addargs(&alist, "-x");
|
2010-12-01 01:03:19 +00:00
|
|
|
addargs(&alist, "-oClearAllForwardings=yes");
|
2006-01-31 10:49:27 +00:00
|
|
|
addargs(&alist, "-n");
|
2010-12-01 01:03:19 +00:00
|
|
|
for (j = 0; j < remote_remote_args.num; j++) {
|
|
|
|
addargs(&alist, "%s",
|
|
|
|
remote_remote_args.list[j]);
|
|
|
|
}
|
2017-10-21 23:06:24 +00:00
|
|
|
|
|
|
|
if (sport != -1) {
|
|
|
|
addargs(&alist, "-p");
|
|
|
|
addargs(&alist, "%d", sport);
|
|
|
|
}
|
|
|
|
if (suser) {
|
2006-01-31 10:49:27 +00:00
|
|
|
addargs(&alist, "-l");
|
|
|
|
addargs(&alist, "%s", suser);
|
2000-01-14 04:45:46 +00:00
|
|
|
}
|
2010-01-08 07:53:43 +00:00
|
|
|
addargs(&alist, "--");
|
2006-01-31 10:49:27 +00:00
|
|
|
addargs(&alist, "%s", host);
|
|
|
|
addargs(&alist, "%s", cmd);
|
|
|
|
addargs(&alist, "%s", src);
|
|
|
|
addargs(&alist, "%s%s%s:%s",
|
|
|
|
tuser ? tuser : "", tuser ? "@" : "",
|
|
|
|
thost, targ);
|
|
|
|
if (do_local_cmd(&alist) != 0)
|
2003-09-22 11:14:55 +00:00
|
|
|
errs = 1;
|
1999-11-24 13:26:21 +00:00
|
|
|
} else { /* local to remote */
|
2021-08-02 23:38:27 +00:00
|
|
|
if (mode == MODE_SFTP) {
|
|
|
|
if (remin == -1) {
|
|
|
|
/* Connect to remote now */
|
|
|
|
conn = do_sftp_connect(thost, tuser,
|
2021-08-07 00:06:30 +00:00
|
|
|
tport, sftp_direct,
|
|
|
|
&remin, &remout, &do_cmd_pid);
|
2021-08-02 23:38:27 +00:00
|
|
|
if (conn == NULL) {
|
|
|
|
fatal("Unable to open sftp "
|
|
|
|
"connection");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The protocol */
|
2021-08-09 23:49:31 +00:00
|
|
|
source_sftp(1, argv[i], targ, conn);
|
2021-08-02 23:38:27 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* SCP */
|
1999-10-27 03:42:43 +00:00
|
|
|
if (remin == -1) {
|
2011-09-22 11:38:00 +00:00
|
|
|
xasprintf(&bp, "%s -t %s%s", cmd,
|
|
|
|
*targ == '-' ? "-- " : "", targ);
|
2021-08-11 14:05:19 +00:00
|
|
|
if (do_cmd(ssh_program, thost, tuser, tport, 0,
|
|
|
|
bp, &remin, &remout, &do_cmd_pid) < 0)
|
1999-11-24 13:26:21 +00:00
|
|
|
exit(1);
|
1999-10-27 03:42:43 +00:00
|
|
|
if (response() < 0)
|
|
|
|
exit(1);
|
2013-06-01 21:31:17 +00:00
|
|
|
free(bp);
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
1999-11-24 13:26:21 +00:00
|
|
|
source(1, argv + i);
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-21 23:06:24 +00:00
|
|
|
out:
|
2021-08-09 23:49:31 +00:00
|
|
|
if (mode == MODE_SFTP)
|
2021-08-02 23:38:27 +00:00
|
|
|
free(conn);
|
2017-10-21 23:06:24 +00:00
|
|
|
free(tuser);
|
|
|
|
free(thost);
|
|
|
|
free(targ);
|
|
|
|
free(suser);
|
|
|
|
free(host);
|
|
|
|
free(src);
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-08-02 23:38:27 +00:00
|
|
|
tolocal(int argc, char **argv, enum scp_mode_e mode, char *sftp_direct)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
2017-10-21 23:06:24 +00:00
|
|
|
char *bp, *host = NULL, *src = NULL, *suser = NULL;
|
2006-01-31 10:49:27 +00:00
|
|
|
arglist alist;
|
2021-08-02 23:38:27 +00:00
|
|
|
struct sftp_conn *conn = NULL;
|
2017-10-21 23:06:24 +00:00
|
|
|
int i, r, sport = -1;
|
2006-01-31 10:49:27 +00:00
|
|
|
|
|
|
|
memset(&alist, '\0', sizeof(alist));
|
|
|
|
alist.list = NULL;
|
1999-10-27 03:42:43 +00:00
|
|
|
|
|
|
|
for (i = 0; i < argc - 1; i++) {
|
2017-10-21 23:06:24 +00:00
|
|
|
free(suser);
|
|
|
|
free(host);
|
|
|
|
free(src);
|
2017-12-18 17:28:54 +00:00
|
|
|
r = parse_scp_uri(argv[i], &suser, &host, &sport, &src);
|
2017-10-21 23:06:24 +00:00
|
|
|
if (r == -1) {
|
2017-12-18 17:28:54 +00:00
|
|
|
fmprintf(stderr, "%s: invalid uri\n", argv[i]);
|
2017-10-21 23:06:24 +00:00
|
|
|
++errs;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (r != 0)
|
|
|
|
parse_user_host_path(argv[i], &suser, &host, &src);
|
|
|
|
if (suser != NULL && !okname(suser)) {
|
|
|
|
++errs;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!host) { /* Local to local. */
|
2006-01-31 10:49:27 +00:00
|
|
|
freeargs(&alist);
|
|
|
|
addargs(&alist, "%s", _PATH_CP);
|
|
|
|
if (iamrecursive)
|
|
|
|
addargs(&alist, "-r");
|
|
|
|
if (pflag)
|
|
|
|
addargs(&alist, "-p");
|
2010-01-08 07:53:43 +00:00
|
|
|
addargs(&alist, "--");
|
2006-01-31 10:49:27 +00:00
|
|
|
addargs(&alist, "%s", argv[i]);
|
|
|
|
addargs(&alist, "%s", argv[argc-1]);
|
|
|
|
if (do_local_cmd(&alist))
|
1999-10-27 03:42:43 +00:00
|
|
|
++errs;
|
|
|
|
continue;
|
|
|
|
}
|
2017-10-21 23:06:24 +00:00
|
|
|
/* Remote to local. */
|
2021-08-02 23:38:27 +00:00
|
|
|
if (mode == MODE_SFTP) {
|
2021-08-07 00:06:30 +00:00
|
|
|
conn = do_sftp_connect(host, suser, sport,
|
|
|
|
sftp_direct, &remin, &remout, &do_cmd_pid);
|
2021-08-02 23:38:27 +00:00
|
|
|
if (conn == NULL) {
|
2021-09-16 15:22:22 +00:00
|
|
|
error("sftp connection failed");
|
2021-08-02 23:38:27 +00:00
|
|
|
++errs;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The protocol */
|
|
|
|
sink_sftp(1, argv[argc - 1], src, conn);
|
|
|
|
|
|
|
|
free(conn);
|
|
|
|
(void) close(remin);
|
|
|
|
(void) close(remout);
|
|
|
|
remin = remout = -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* SCP */
|
2011-09-22 11:38:00 +00:00
|
|
|
xasprintf(&bp, "%s -f %s%s",
|
|
|
|
cmd, *src == '-' ? "-- " : "", src);
|
2021-08-11 14:05:19 +00:00
|
|
|
if (do_cmd(ssh_program, host, suser, sport, 0, bp,
|
2021-08-07 00:06:30 +00:00
|
|
|
&remin, &remout, &do_cmd_pid) < 0) {
|
2013-06-01 21:31:17 +00:00
|
|
|
free(bp);
|
1999-11-24 13:26:21 +00:00
|
|
|
++errs;
|
|
|
|
continue;
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
2013-06-01 21:31:17 +00:00
|
|
|
free(bp);
|
2019-01-26 22:41:28 +00:00
|
|
|
sink(1, argv + argc - 1, src);
|
1999-11-24 13:26:21 +00:00
|
|
|
(void) close(remin);
|
1999-10-27 03:42:43 +00:00
|
|
|
remin = remout = -1;
|
|
|
|
}
|
2017-10-21 23:06:24 +00:00
|
|
|
free(suser);
|
|
|
|
free(host);
|
|
|
|
free(src);
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
|
|
|
|
2021-08-09 23:49:31 +00:00
|
|
|
/* Prepare remote path, handling ~ by assuming cwd is the homedir */
|
2021-08-09 23:44:32 +00:00
|
|
|
static char *
|
2021-08-09 23:49:31 +00:00
|
|
|
prepare_remote_path(struct sftp_conn *conn, const char *path)
|
2021-08-09 23:44:32 +00:00
|
|
|
{
|
2022-01-08 07:36:11 +00:00
|
|
|
size_t nslash;
|
|
|
|
|
2021-08-09 23:44:32 +00:00
|
|
|
/* Handle ~ prefixed paths */
|
2021-08-09 23:49:31 +00:00
|
|
|
if (*path == '\0' || strcmp(path, "~") == 0)
|
|
|
|
return xstrdup(".");
|
2022-01-08 07:36:11 +00:00
|
|
|
if (*path != '~')
|
|
|
|
return xstrdup(path);
|
|
|
|
if (strncmp(path, "~/", 2) == 0) {
|
|
|
|
if ((nslash = strspn(path + 2, "/")) == strlen(path + 2))
|
|
|
|
return xstrdup(".");
|
|
|
|
return xstrdup(path + 2 + nslash);
|
|
|
|
}
|
2021-08-09 23:49:31 +00:00
|
|
|
if (can_expand_path(conn))
|
|
|
|
return do_expand_path(conn, path);
|
|
|
|
/* No protocol extension */
|
2021-09-18 02:03:25 +00:00
|
|
|
error("server expand-path extension is required "
|
|
|
|
"for ~user paths in SFTP mode");
|
2021-08-09 23:49:31 +00:00
|
|
|
return NULL;
|
2021-08-09 23:44:32 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 23:38:27 +00:00
|
|
|
void
|
2021-08-09 23:49:31 +00:00
|
|
|
source_sftp(int argc, char *src, char *targ, struct sftp_conn *conn)
|
2021-08-02 23:38:27 +00:00
|
|
|
{
|
|
|
|
char *target = NULL, *filename = NULL, *abs_dst = NULL;
|
2022-01-17 21:39:51 +00:00
|
|
|
int src_is_dir, target_is_dir;
|
|
|
|
Attrib a;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
memset(&a, '\0', sizeof(a));
|
|
|
|
if (stat(src, &st) != 0)
|
|
|
|
fatal("stat local \"%s\": %s", src, strerror(errno));
|
|
|
|
src_is_dir = S_ISDIR(st.st_mode);
|
2021-08-02 23:38:27 +00:00
|
|
|
if ((filename = basename(src)) == NULL)
|
2022-01-17 21:39:51 +00:00
|
|
|
fatal("basename \"%s\": %s", src, strerror(errno));
|
2021-08-02 23:38:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* No need to glob here - the local shell already took care of
|
|
|
|
* the expansions
|
|
|
|
*/
|
2021-08-09 23:49:31 +00:00
|
|
|
if ((target = prepare_remote_path(conn, targ)) == NULL)
|
2021-08-09 23:44:32 +00:00
|
|
|
cleanup_exit(255);
|
2021-08-02 23:38:27 +00:00
|
|
|
target_is_dir = remote_is_dir(conn, target);
|
|
|
|
if (targetshouldbedirectory && !target_is_dir) {
|
2022-01-17 21:39:51 +00:00
|
|
|
debug("target directory \"%s\" does not exist", target);
|
|
|
|
a.flags = SSH2_FILEXFER_ATTR_PERMISSIONS;
|
|
|
|
a.perm = st.st_mode | 0700; /* ensure writable */
|
|
|
|
if (do_mkdir(conn, target, &a, 1) != 0)
|
|
|
|
cleanup_exit(255); /* error already logged */
|
|
|
|
target_is_dir = 1;
|
2021-08-02 23:38:27 +00:00
|
|
|
}
|
|
|
|
if (target_is_dir)
|
|
|
|
abs_dst = path_append(target, filename);
|
|
|
|
else {
|
|
|
|
abs_dst = target;
|
|
|
|
target = NULL;
|
|
|
|
}
|
|
|
|
debug3_f("copying local %s to remote %s", src, abs_dst);
|
|
|
|
|
2022-01-17 21:39:51 +00:00
|
|
|
if (src_is_dir && iamrecursive) {
|
2021-08-07 00:08:52 +00:00
|
|
|
if (upload_dir(conn, src, abs_dst, pflag,
|
2021-08-07 00:12:09 +00:00
|
|
|
SFTP_PROGRESS_ONLY, 0, 0, 1) != 0) {
|
2021-09-11 00:40:24 +00:00
|
|
|
error("failed to upload directory %s to %s",
|
2021-08-02 23:38:27 +00:00
|
|
|
src, abs_dst);
|
2021-09-11 00:40:24 +00:00
|
|
|
errs = 1;
|
2021-08-02 23:38:27 +00:00
|
|
|
}
|
2021-09-11 00:40:24 +00:00
|
|
|
} else if (do_upload(conn, src, abs_dst, pflag, 0, 0) != 0) {
|
|
|
|
error("failed to upload file %s to %s", src, abs_dst);
|
|
|
|
errs = 1;
|
|
|
|
}
|
2021-08-02 23:38:27 +00:00
|
|
|
|
|
|
|
free(abs_dst);
|
|
|
|
free(target);
|
|
|
|
}
|
|
|
|
|
1999-10-27 03:42:43 +00:00
|
|
|
void
|
2003-06-04 12:51:08 +00:00
|
|
|
source(int argc, char **argv)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
|
|
|
struct stat stb;
|
|
|
|
static BUF buffer;
|
|
|
|
BUF *bp;
|
2008-06-13 23:02:25 +00:00
|
|
|
off_t i, statbytes;
|
2014-07-02 05:29:01 +00:00
|
|
|
size_t amt, nr;
|
2005-08-02 07:07:07 +00:00
|
|
|
int fd = -1, haderr, indx;
|
2019-09-09 02:31:19 +00:00
|
|
|
char *last, *name, buf[PATH_MAX + 128], encname[PATH_MAX];
|
2001-04-07 01:14:38 +00:00
|
|
|
int len;
|
1999-10-27 03:42:43 +00:00
|
|
|
|
|
|
|
for (indx = 0; indx < argc; ++indx) {
|
1999-11-24 13:26:21 +00:00
|
|
|
name = argv[indx];
|
1999-10-27 03:42:43 +00:00
|
|
|
statbytes = 0;
|
2001-04-07 01:14:38 +00:00
|
|
|
len = strlen(name);
|
|
|
|
while (len > 1 && name[len-1] == '/')
|
|
|
|
name[--len] = '\0';
|
upstream: For open/openat, if the flags parameter does not contain
O_CREAT, the 3rd (variadic) mode_t parameter is irrelevant. Many developers
in the past have passed mode_t (0, 044, 0644, or such), which might lead
future people to copy this broken idiom, and perhaps even believe this
parameter has some meaning or implication or application. Delete them all.
This comes out of a conversation where tb@ noticed that a strange (but
intentional) pledge behaviour is to always knock-out high-bits from mode_t on
a number of system calls as a safety factor, and his bewilderment that this
appeared to be happening against valid modes (at least visually), but no
sorry, they are all irrelevant junk. They could all be 0xdeafbeef. ok
millert
OpenBSD-Commit-ID: 503d11633497115688c0c6952686524f01f53121
2021-10-24 21:24:17 +00:00
|
|
|
if ((fd = open(name, O_RDONLY|O_NONBLOCK)) == -1)
|
1999-10-27 03:42:43 +00:00
|
|
|
goto syserr;
|
2007-06-12 14:02:07 +00:00
|
|
|
if (strchr(name, '\n') != NULL) {
|
|
|
|
strnvis(encname, name, sizeof(encname), VIS_NL);
|
|
|
|
name = encname;
|
|
|
|
}
|
2019-06-28 13:35:04 +00:00
|
|
|
if (fstat(fd, &stb) == -1) {
|
1999-10-27 03:42:43 +00:00
|
|
|
syserr: run_err("%s: %s", name, strerror(errno));
|
|
|
|
goto next;
|
|
|
|
}
|
2008-06-13 23:02:25 +00:00
|
|
|
if (stb.st_size < 0) {
|
|
|
|
run_err("%s: %s", name, "Negative file size");
|
|
|
|
goto next;
|
|
|
|
}
|
2007-06-12 13:41:06 +00:00
|
|
|
unset_nonblock(fd);
|
1999-10-27 03:42:43 +00:00
|
|
|
switch (stb.st_mode & S_IFMT) {
|
|
|
|
case S_IFREG:
|
|
|
|
break;
|
|
|
|
case S_IFDIR:
|
|
|
|
if (iamrecursive) {
|
|
|
|
rsource(name, &stb);
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
default:
|
|
|
|
run_err("%s: not a regular file", name);
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
if ((last = strrchr(name, '/')) == NULL)
|
|
|
|
last = name;
|
|
|
|
else
|
|
|
|
++last;
|
|
|
|
curfile = last;
|
|
|
|
if (pflag) {
|
2013-07-18 06:09:25 +00:00
|
|
|
if (do_times(remout, verbose_mode, &stb) < 0)
|
1999-10-27 03:42:43 +00:00
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
|
2001-03-17 00:10:20 +00:00
|
|
|
snprintf(buf, sizeof buf, "C%04o %lld %s\n",
|
2000-12-22 01:43:59 +00:00
|
|
|
(u_int) (stb.st_mode & FILEMODEMASK),
|
2005-11-25 03:44:55 +00:00
|
|
|
(long long)stb.st_size, last);
|
2016-05-25 23:48:45 +00:00
|
|
|
if (verbose_mode)
|
|
|
|
fmprintf(stderr, "Sending file modes: %s", buf);
|
2003-07-03 03:46:56 +00:00
|
|
|
(void) atomicio(vwrite, remout, buf, strlen(buf));
|
1999-10-27 03:42:43 +00:00
|
|
|
if (response() < 0)
|
|
|
|
goto next;
|
2007-10-26 05:39:15 +00:00
|
|
|
if ((bp = allocbuf(&buffer, fd, COPY_BUFLEN)) == NULL) {
|
2005-11-22 08:38:06 +00:00
|
|
|
next: if (fd != -1) {
|
|
|
|
(void) close(fd);
|
|
|
|
fd = -1;
|
|
|
|
}
|
1999-10-27 03:42:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
2003-01-10 10:43:24 +00:00
|
|
|
if (showprogress)
|
|
|
|
start_progress_meter(curfile, stb.st_size, &statbytes);
|
2007-10-26 05:39:15 +00:00
|
|
|
set_nonblock(remout);
|
1999-10-27 03:42:43 +00:00
|
|
|
for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
|
|
|
|
amt = bp->cnt;
|
2008-06-13 23:02:25 +00:00
|
|
|
if (i + (off_t)amt > stb.st_size)
|
1999-10-27 03:42:43 +00:00
|
|
|
amt = stb.st_size - i;
|
|
|
|
if (!haderr) {
|
2014-07-02 05:29:01 +00:00
|
|
|
if ((nr = atomicio(read, fd,
|
|
|
|
bp->buf, amt)) != amt) {
|
2005-05-26 02:23:44 +00:00
|
|
|
haderr = errno;
|
2014-07-02 05:29:01 +00:00
|
|
|
memset(bp->buf + nr, 0, amt - nr);
|
|
|
|
}
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
2007-10-26 05:39:15 +00:00
|
|
|
/* Keep writing after error to retain sync */
|
|
|
|
if (haderr) {
|
|
|
|
(void)atomicio(vwrite, remout, bp->buf, amt);
|
2014-07-02 05:29:01 +00:00
|
|
|
memset(bp->buf, 0, amt);
|
2007-10-26 05:39:15 +00:00
|
|
|
continue;
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
2010-09-24 12:15:11 +00:00
|
|
|
if (atomicio6(vwrite, remout, bp->buf, amt, scpio,
|
2007-10-26 05:39:15 +00:00
|
|
|
&statbytes) != amt)
|
|
|
|
haderr = errno;
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
2007-10-26 05:39:15 +00:00
|
|
|
unset_nonblock(remout);
|
1999-10-27 03:42:43 +00:00
|
|
|
|
2005-11-22 08:38:06 +00:00
|
|
|
if (fd != -1) {
|
2019-06-28 13:35:04 +00:00
|
|
|
if (close(fd) == -1 && !haderr)
|
2005-11-22 08:38:06 +00:00
|
|
|
haderr = errno;
|
|
|
|
fd = -1;
|
|
|
|
}
|
1999-10-27 03:42:43 +00:00
|
|
|
if (!haderr)
|
2003-07-03 03:46:56 +00:00
|
|
|
(void) atomicio(vwrite, remout, "", 1);
|
1999-10-27 03:42:43 +00:00
|
|
|
else
|
|
|
|
run_err("%s: %s", name, strerror(haderr));
|
1999-11-24 13:26:21 +00:00
|
|
|
(void) response();
|
2016-03-02 22:43:52 +00:00
|
|
|
if (showprogress)
|
|
|
|
stop_progress_meter();
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-06-04 12:51:08 +00:00
|
|
|
rsource(char *name, struct stat *statp)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
|
|
|
DIR *dirp;
|
|
|
|
struct dirent *dp;
|
2015-01-16 06:40:12 +00:00
|
|
|
char *last, *vect[1], path[PATH_MAX];
|
1999-10-27 03:42:43 +00:00
|
|
|
|
|
|
|
if (!(dirp = opendir(name))) {
|
|
|
|
run_err("%s: %s", name, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
last = strrchr(name, '/');
|
2015-10-16 17:07:24 +00:00
|
|
|
if (last == NULL)
|
1999-10-27 03:42:43 +00:00
|
|
|
last = name;
|
|
|
|
else
|
|
|
|
last++;
|
|
|
|
if (pflag) {
|
2013-07-18 06:09:25 +00:00
|
|
|
if (do_times(remout, verbose_mode, statp) < 0) {
|
1999-10-27 03:42:43 +00:00
|
|
|
closedir(dirp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2001-02-09 01:23:39 +00:00
|
|
|
(void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
|
2000-12-22 01:43:59 +00:00
|
|
|
(u_int) (statp->st_mode & FILEMODEMASK), 0, last);
|
1999-11-24 13:26:21 +00:00
|
|
|
if (verbose_mode)
|
2016-05-25 23:48:45 +00:00
|
|
|
fmprintf(stderr, "Entering directory: %s", path);
|
2003-07-03 03:46:56 +00:00
|
|
|
(void) atomicio(vwrite, remout, path, strlen(path));
|
1999-10-27 03:42:43 +00:00
|
|
|
if (response() < 0) {
|
|
|
|
closedir(dirp);
|
|
|
|
return;
|
|
|
|
}
|
2001-03-29 00:39:55 +00:00
|
|
|
while ((dp = readdir(dirp)) != NULL) {
|
1999-10-27 03:42:43 +00:00
|
|
|
if (dp->d_ino == 0)
|
|
|
|
continue;
|
|
|
|
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
|
|
|
|
continue;
|
|
|
|
if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
|
|
|
|
run_err("%s/%s: name too long", name, dp->d_name);
|
|
|
|
continue;
|
|
|
|
}
|
2001-02-09 01:23:39 +00:00
|
|
|
(void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
|
1999-10-27 03:42:43 +00:00
|
|
|
vect[0] = path;
|
|
|
|
source(1, vect);
|
|
|
|
}
|
1999-11-24 13:26:21 +00:00
|
|
|
(void) closedir(dirp);
|
2003-07-03 03:46:56 +00:00
|
|
|
(void) atomicio(vwrite, remout, "E\n", 2);
|
1999-11-24 13:26:21 +00:00
|
|
|
(void) response();
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 23:38:27 +00:00
|
|
|
void
|
|
|
|
sink_sftp(int argc, char *dst, const char *src, struct sftp_conn *conn)
|
|
|
|
{
|
|
|
|
char *abs_src = NULL;
|
|
|
|
char *abs_dst = NULL;
|
|
|
|
glob_t g;
|
2021-08-09 23:49:31 +00:00
|
|
|
char *filename, *tmp = NULL;
|
2022-01-17 21:39:51 +00:00
|
|
|
int i, r, err = 0, dst_is_dir;
|
|
|
|
struct stat st;
|
2021-08-02 23:38:27 +00:00
|
|
|
|
2021-08-09 23:44:32 +00:00
|
|
|
memset(&g, 0, sizeof(g));
|
2022-01-17 21:39:51 +00:00
|
|
|
|
2021-08-02 23:38:27 +00:00
|
|
|
/*
|
|
|
|
* Here, we need remote glob as SFTP can not depend on remote shell
|
|
|
|
* expansions
|
|
|
|
*/
|
2021-08-09 23:49:31 +00:00
|
|
|
if ((abs_src = prepare_remote_path(conn, src)) == NULL) {
|
2021-08-09 23:44:32 +00:00
|
|
|
err = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
2021-08-02 23:38:27 +00:00
|
|
|
|
|
|
|
debug3_f("copying remote %s to local %s", abs_src, dst);
|
|
|
|
if ((r = remote_glob(conn, abs_src, GLOB_MARK, NULL, &g)) != 0) {
|
|
|
|
if (r == GLOB_NOSPACE)
|
2021-09-16 15:22:22 +00:00
|
|
|
error("%s: too many glob matches", abs_src);
|
2021-08-02 23:38:27 +00:00
|
|
|
else
|
2021-09-16 15:22:22 +00:00
|
|
|
error("%s: %s", abs_src, strerror(ENOENT));
|
2021-08-02 23:38:27 +00:00
|
|
|
err = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2022-01-17 21:39:51 +00:00
|
|
|
if ((r = stat(dst, &st)) != 0)
|
|
|
|
debug2_f("stat local \"%s\": %s", dst, strerror(errno));
|
|
|
|
dst_is_dir = r == 0 && S_ISDIR(st.st_mode);
|
|
|
|
|
|
|
|
if (g.gl_matchc > 1 && !dst_is_dir) {
|
|
|
|
if (r == 0) {
|
|
|
|
error("Multiple files match pattern, but destination "
|
|
|
|
"\"%s\" is not a directory", dst);
|
|
|
|
err = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
debug2_f("creating destination \"%s\"", dst);
|
|
|
|
if (mkdir(dst, 0777) != 0) {
|
|
|
|
error("local mkdir \"%s\": %s", dst, strerror(errno));
|
|
|
|
err = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
dst_is_dir = 1;
|
2021-08-02 23:38:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
|
|
|
|
tmp = xstrdup(g.gl_pathv[i]);
|
|
|
|
if ((filename = basename(tmp)) == NULL) {
|
|
|
|
error("basename %s: %s", tmp, strerror(errno));
|
|
|
|
err = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2022-01-17 21:39:51 +00:00
|
|
|
if (dst_is_dir)
|
2021-08-02 23:38:27 +00:00
|
|
|
abs_dst = path_append(dst, filename);
|
|
|
|
else
|
|
|
|
abs_dst = xstrdup(dst);
|
|
|
|
|
|
|
|
debug("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
|
|
|
|
if (globpath_is_dir(g.gl_pathv[i]) && iamrecursive) {
|
|
|
|
if (download_dir(conn, g.gl_pathv[i], abs_dst, NULL,
|
2021-08-07 00:12:09 +00:00
|
|
|
pflag, SFTP_PROGRESS_ONLY, 0, 0, 1) == -1)
|
2021-08-02 23:38:27 +00:00
|
|
|
err = -1;
|
|
|
|
} else {
|
|
|
|
if (do_download(conn, g.gl_pathv[i], abs_dst, NULL,
|
|
|
|
pflag, 0, 0) == -1)
|
|
|
|
err = -1;
|
|
|
|
}
|
|
|
|
free(abs_dst);
|
|
|
|
abs_dst = NULL;
|
2021-08-04 01:34:55 +00:00
|
|
|
free(tmp);
|
|
|
|
tmp = NULL;
|
2021-08-02 23:38:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
free(abs_src);
|
2021-08-04 01:34:55 +00:00
|
|
|
free(tmp);
|
2021-08-02 23:38:27 +00:00
|
|
|
globfree(&g);
|
2021-09-16 15:11:19 +00:00
|
|
|
if (err == -1)
|
2021-09-11 00:40:24 +00:00
|
|
|
errs = 1;
|
2021-08-02 23:38:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-28 03:21:12 +00:00
|
|
|
#define TYPE_OVERFLOW(type, val) \
|
|
|
|
((sizeof(type) == 4 && (val) > INT32_MAX) || \
|
|
|
|
(sizeof(type) == 8 && (val) > INT64_MAX) || \
|
|
|
|
(sizeof(type) != 4 && sizeof(type) != 8))
|
|
|
|
|
1999-10-27 03:42:43 +00:00
|
|
|
void
|
2019-01-26 22:41:28 +00:00
|
|
|
sink(int argc, char **argv, const char *src)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
|
|
|
static BUF buffer;
|
|
|
|
struct stat stb;
|
|
|
|
BUF *bp;
|
2005-05-26 02:23:44 +00:00
|
|
|
off_t i;
|
2005-06-17 02:59:34 +00:00
|
|
|
size_t j, count;
|
2006-04-23 02:04:27 +00:00
|
|
|
int amt, exists, first, ofd;
|
|
|
|
mode_t mode, omode, mask;
|
2003-01-10 10:43:24 +00:00
|
|
|
off_t size, statbytes;
|
2013-07-18 06:09:25 +00:00
|
|
|
unsigned long long ull;
|
2020-05-01 06:31:42 +00:00
|
|
|
int setimes, targisdir, wrerr;
|
2016-05-25 23:48:45 +00:00
|
|
|
char ch, *cp, *np, *targ, *why, *vect[1], buf[2048], visbuf[2048];
|
2019-02-10 11:15:52 +00:00
|
|
|
char **patterns = NULL;
|
|
|
|
size_t n, npatterns = 0;
|
2000-09-23 06:15:56 +00:00
|
|
|
struct timeval tv[2];
|
1999-10-27 03:42:43 +00:00
|
|
|
|
2001-04-14 23:14:22 +00:00
|
|
|
#define atime tv[0]
|
|
|
|
#define mtime tv[1]
|
2004-11-05 09:10:02 +00:00
|
|
|
#define SCREWUP(str) { why = str; goto screwup; }
|
1999-10-27 03:42:43 +00:00
|
|
|
|
2017-04-28 03:21:12 +00:00
|
|
|
if (TYPE_OVERFLOW(time_t, 0) || TYPE_OVERFLOW(off_t, 0))
|
|
|
|
SCREWUP("Unexpected off_t/time_t size");
|
|
|
|
|
1999-10-27 03:42:43 +00:00
|
|
|
setimes = targisdir = 0;
|
|
|
|
mask = umask(0);
|
|
|
|
if (!pflag)
|
1999-11-24 13:26:21 +00:00
|
|
|
(void) umask(mask);
|
1999-10-27 03:42:43 +00:00
|
|
|
if (argc != 1) {
|
|
|
|
run_err("ambiguous target");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
targ = *argv;
|
|
|
|
if (targetshouldbedirectory)
|
|
|
|
verifydir(targ);
|
1999-11-24 13:26:21 +00:00
|
|
|
|
2003-07-03 03:46:56 +00:00
|
|
|
(void) atomicio(vwrite, remout, "", 1);
|
1999-10-27 03:42:43 +00:00
|
|
|
if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
|
|
|
|
targisdir = 1;
|
2019-01-26 22:41:28 +00:00
|
|
|
if (src != NULL && !iamrecursive && !Tflag) {
|
|
|
|
/*
|
|
|
|
* Prepare to try to restrict incoming filenames to match
|
|
|
|
* the requested destination file glob.
|
|
|
|
*/
|
2019-02-10 11:15:52 +00:00
|
|
|
if (brace_expand(src, &patterns, &npatterns) != 0)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_f("could not expand pattern");
|
2019-01-26 22:41:28 +00:00
|
|
|
}
|
1999-10-27 03:42:43 +00:00
|
|
|
for (first = 1;; first = 0) {
|
|
|
|
cp = buf;
|
2005-05-26 02:23:44 +00:00
|
|
|
if (atomicio(read, remin, cp, 1) != 1)
|
2019-02-10 11:15:52 +00:00
|
|
|
goto done;
|
1999-10-27 03:42:43 +00:00
|
|
|
if (*cp++ == '\n')
|
|
|
|
SCREWUP("unexpected <newline>");
|
|
|
|
do {
|
- Remove references to SSLeay.
- Big OpenBSD CVS update
- markus@cvs.openbsd.org
[clientloop.c]
- typo
[session.c]
- update proctitle on pty alloc/dealloc, e.g. w/ windows client
[session.c]
- update proctitle for proto 1, too
[channels.h nchan.c serverloop.c session.c sshd.c]
- use c-style comments
- deraadt@cvs.openbsd.org
[scp.c]
- more atomicio
- markus@cvs.openbsd.org
[channels.c]
- set O_NONBLOCK
[ssh.1]
- update AUTHOR
[readconf.c ssh-keygen.c ssh.h]
- default DSA key file ~/.ssh/id_dsa
[clientloop.c]
- typo, rm verbose debug
- deraadt@cvs.openbsd.org
[ssh-keygen.1]
- document DSA use of ssh-keygen
[sshd.8]
- a start at describing what i understand of the DSA side
[ssh-keygen.1]
- document -X and -x
[ssh-keygen.c]
- simplify usage
- markus@cvs.openbsd.org
[sshd.8]
- there is no rhosts_dsa
[ssh-keygen.1]
- document -y, update -X,-x
[nchan.c]
- fix close for non-open ssh1 channels
[servconf.c servconf.h ssh.h sshd.8 sshd.c ]
- s/DsaKey/HostDSAKey/, document option
[sshconnect2.c]
- respect number_of_password_prompts
[channels.c channels.h servconf.c servconf.h session.c sshd.8]
- GatewayPorts for sshd, ok deraadt@
[ssh-add.1 ssh-agent.1 ssh.1]
- more doc on: DSA, id_dsa, known_hosts2, authorized_keys2
[ssh.1]
- more info on proto 2
[sshd.8]
- sync AUTHOR w/ ssh.1
[key.c key.h sshconnect.c]
- print key type when talking about host keys
[packet.c]
- clear padding in ssh2
[dsa.c key.c radix.c ssh.h sshconnect1.c uuencode.c uuencode.h]
- replace broken uuencode w/ libc b64_ntop
[auth2.c]
- log failure before sending the reply
[key.c radix.c uuencode.c]
- remote trailing comments before calling __b64_pton
[auth2.c readconf.c readconf.h servconf.c servconf.h ssh.1]
[sshconnect2.c sshd.8]
- add DSAAuthetication option to ssh/sshd, document SSH2 in sshd.8
- Bring in b64_ntop and b64_pton from OpenBSD libc (bsd-base64.[ch])
2000-05-07 02:03:14 +00:00
|
|
|
if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
|
1999-10-27 03:42:43 +00:00
|
|
|
SCREWUP("lost connection");
|
|
|
|
*cp++ = ch;
|
|
|
|
} while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
|
|
|
|
*cp = 0;
|
2004-04-19 13:50:16 +00:00
|
|
|
if (verbose_mode)
|
2016-05-25 23:48:45 +00:00
|
|
|
fmprintf(stderr, "Sink: %s", buf);
|
1999-10-27 03:42:43 +00:00
|
|
|
|
|
|
|
if (buf[0] == '\01' || buf[0] == '\02') {
|
2016-05-25 23:48:45 +00:00
|
|
|
if (iamremote == 0) {
|
|
|
|
(void) snmprintf(visbuf, sizeof(visbuf),
|
|
|
|
NULL, "%s", buf + 1);
|
2003-07-03 03:46:56 +00:00
|
|
|
(void) atomicio(vwrite, STDERR_FILENO,
|
2016-05-25 23:48:45 +00:00
|
|
|
visbuf, strlen(visbuf));
|
|
|
|
}
|
1999-10-27 03:42:43 +00:00
|
|
|
if (buf[0] == '\02')
|
|
|
|
exit(1);
|
|
|
|
++errs;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (buf[0] == 'E') {
|
2003-07-03 03:46:56 +00:00
|
|
|
(void) atomicio(vwrite, remout, "", 1);
|
2019-02-10 11:15:52 +00:00
|
|
|
goto done;
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
|
|
|
if (ch == '\n')
|
|
|
|
*--cp = 0;
|
|
|
|
|
|
|
|
cp = buf;
|
|
|
|
if (*cp == 'T') {
|
|
|
|
setimes++;
|
|
|
|
cp++;
|
2013-07-18 06:09:25 +00:00
|
|
|
if (!isdigit((unsigned char)*cp))
|
|
|
|
SCREWUP("mtime.sec not present");
|
|
|
|
ull = strtoull(cp, &cp, 10);
|
2001-04-14 23:14:22 +00:00
|
|
|
if (!cp || *cp++ != ' ')
|
1999-10-27 03:42:43 +00:00
|
|
|
SCREWUP("mtime.sec not delimited");
|
2017-04-28 03:21:12 +00:00
|
|
|
if (TYPE_OVERFLOW(time_t, ull))
|
2013-07-18 06:09:25 +00:00
|
|
|
setimes = 0; /* out of range */
|
|
|
|
mtime.tv_sec = ull;
|
2001-04-14 23:14:22 +00:00
|
|
|
mtime.tv_usec = strtol(cp, &cp, 10);
|
2013-07-18 06:09:25 +00:00
|
|
|
if (!cp || *cp++ != ' ' || mtime.tv_usec < 0 ||
|
|
|
|
mtime.tv_usec > 999999)
|
1999-10-27 03:42:43 +00:00
|
|
|
SCREWUP("mtime.usec not delimited");
|
2013-07-18 06:09:25 +00:00
|
|
|
if (!isdigit((unsigned char)*cp))
|
|
|
|
SCREWUP("atime.sec not present");
|
|
|
|
ull = strtoull(cp, &cp, 10);
|
2001-04-14 23:14:22 +00:00
|
|
|
if (!cp || *cp++ != ' ')
|
1999-10-27 03:42:43 +00:00
|
|
|
SCREWUP("atime.sec not delimited");
|
2017-04-28 03:21:12 +00:00
|
|
|
if (TYPE_OVERFLOW(time_t, ull))
|
2013-07-18 06:09:25 +00:00
|
|
|
setimes = 0; /* out of range */
|
|
|
|
atime.tv_sec = ull;
|
2001-04-14 23:14:22 +00:00
|
|
|
atime.tv_usec = strtol(cp, &cp, 10);
|
2013-07-18 06:09:25 +00:00
|
|
|
if (!cp || *cp++ != '\0' || atime.tv_usec < 0 ||
|
|
|
|
atime.tv_usec > 999999)
|
1999-10-27 03:42:43 +00:00
|
|
|
SCREWUP("atime.usec not delimited");
|
2003-07-03 03:46:56 +00:00
|
|
|
(void) atomicio(vwrite, remout, "", 1);
|
1999-10-27 03:42:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*cp != 'C' && *cp != 'D') {
|
|
|
|
/*
|
|
|
|
* Check for the case "rcp remote:foo\* local:bar".
|
|
|
|
* In this case, the line "No match." can be returned
|
|
|
|
* by the shell before the rcp command on the remote is
|
|
|
|
* executed so the ^Aerror_message convention isn't
|
|
|
|
* followed.
|
|
|
|
*/
|
|
|
|
if (first) {
|
|
|
|
run_err("%s", cp);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
SCREWUP("expected control record");
|
|
|
|
}
|
|
|
|
mode = 0;
|
|
|
|
for (++cp; cp < buf + 5; cp++) {
|
|
|
|
if (*cp < '0' || *cp > '7')
|
|
|
|
SCREWUP("bad mode");
|
|
|
|
mode = (mode << 3) | (*cp - '0');
|
|
|
|
}
|
2018-06-01 04:31:48 +00:00
|
|
|
if (!pflag)
|
|
|
|
mode &= ~mask;
|
1999-10-27 03:42:43 +00:00
|
|
|
if (*cp++ != ' ')
|
|
|
|
SCREWUP("mode not delimited");
|
|
|
|
|
2017-04-27 11:53:12 +00:00
|
|
|
if (!isdigit((unsigned char)*cp))
|
|
|
|
SCREWUP("size not present");
|
|
|
|
ull = strtoull(cp, &cp, 10);
|
|
|
|
if (!cp || *cp++ != ' ')
|
1999-10-27 03:42:43 +00:00
|
|
|
SCREWUP("size not delimited");
|
2017-04-28 03:21:12 +00:00
|
|
|
if (TYPE_OVERFLOW(off_t, ull))
|
2017-04-27 11:53:12 +00:00
|
|
|
SCREWUP("size out of range");
|
|
|
|
size = (off_t)ull;
|
|
|
|
|
2018-11-16 03:03:10 +00:00
|
|
|
if (*cp == '\0' || strchr(cp, '/') != NULL ||
|
|
|
|
strcmp(cp, ".") == 0 || strcmp(cp, "..") == 0) {
|
2004-04-19 13:50:16 +00:00
|
|
|
run_err("error: unexpected filename: %s", cp);
|
|
|
|
exit(1);
|
|
|
|
}
|
2019-02-10 11:15:52 +00:00
|
|
|
if (npatterns > 0) {
|
|
|
|
for (n = 0; n < npatterns; n++) {
|
|
|
|
if (fnmatch(patterns[n], cp, 0) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (n >= npatterns)
|
|
|
|
SCREWUP("filename does not match request");
|
|
|
|
}
|
1999-10-27 03:42:43 +00:00
|
|
|
if (targisdir) {
|
|
|
|
static char *namebuf;
|
2005-06-17 02:59:34 +00:00
|
|
|
static size_t cursize;
|
1999-10-27 03:42:43 +00:00
|
|
|
size_t need;
|
|
|
|
|
|
|
|
need = strlen(targ) + strlen(cp) + 250;
|
2001-02-09 01:23:39 +00:00
|
|
|
if (need > cursize) {
|
2013-06-01 21:31:17 +00:00
|
|
|
free(namebuf);
|
1999-11-24 13:26:21 +00:00
|
|
|
namebuf = xmalloc(need);
|
2001-02-09 01:23:39 +00:00
|
|
|
cursize = need;
|
|
|
|
}
|
|
|
|
(void) snprintf(namebuf, need, "%s%s%s", targ,
|
2002-04-06 18:29:59 +00:00
|
|
|
strcmp(targ, "/") ? "/" : "", cp);
|
1999-10-27 03:42:43 +00:00
|
|
|
np = namebuf;
|
|
|
|
} else
|
|
|
|
np = targ;
|
|
|
|
curfile = cp;
|
|
|
|
exists = stat(np, &stb) == 0;
|
|
|
|
if (buf[0] == 'D') {
|
|
|
|
int mod_flag = pflag;
|
2004-04-19 13:50:16 +00:00
|
|
|
if (!iamrecursive)
|
|
|
|
SCREWUP("received directory without -r");
|
1999-10-27 03:42:43 +00:00
|
|
|
if (exists) {
|
|
|
|
if (!S_ISDIR(stb.st_mode)) {
|
|
|
|
errno = ENOTDIR;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
if (pflag)
|
1999-11-24 13:26:21 +00:00
|
|
|
(void) chmod(np, mode);
|
1999-10-27 03:42:43 +00:00
|
|
|
} else {
|
2021-04-03 06:18:40 +00:00
|
|
|
/* Handle copying from a read-only directory */
|
1999-10-27 03:42:43 +00:00
|
|
|
mod_flag = 1;
|
2019-06-28 13:35:04 +00:00
|
|
|
if (mkdir(np, mode | S_IRWXU) == -1)
|
1999-10-27 03:42:43 +00:00
|
|
|
goto bad;
|
|
|
|
}
|
2001-02-10 21:50:00 +00:00
|
|
|
vect[0] = xstrdup(np);
|
2019-01-26 22:41:28 +00:00
|
|
|
sink(1, vect, src);
|
1999-10-27 03:42:43 +00:00
|
|
|
if (setimes) {
|
|
|
|
setimes = 0;
|
2020-05-06 20:57:38 +00:00
|
|
|
(void) utimes(vect[0], tv);
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
|
|
|
if (mod_flag)
|
2001-02-19 10:51:08 +00:00
|
|
|
(void) chmod(vect[0], mode);
|
2013-06-01 21:31:17 +00:00
|
|
|
free(vect[0]);
|
1999-10-27 03:42:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
omode = mode;
|
2013-06-01 21:52:21 +00:00
|
|
|
mode |= S_IWUSR;
|
2019-06-28 13:35:04 +00:00
|
|
|
if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) == -1) {
|
1999-10-27 03:42:43 +00:00
|
|
|
bad: run_err("%s: %s", np, strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
2003-07-03 03:46:56 +00:00
|
|
|
(void) atomicio(vwrite, remout, "", 1);
|
2007-10-26 05:39:15 +00:00
|
|
|
if ((bp = allocbuf(&buffer, ofd, COPY_BUFLEN)) == NULL) {
|
1999-11-24 13:26:21 +00:00
|
|
|
(void) close(ofd);
|
1999-10-27 03:42:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cp = bp->buf;
|
2020-05-01 06:31:42 +00:00
|
|
|
wrerr = 0;
|
1999-10-27 03:42:43 +00:00
|
|
|
|
2020-05-01 06:31:42 +00:00
|
|
|
/*
|
|
|
|
* NB. do not use run_err() unless immediately followed by
|
|
|
|
* exit() below as it may send a spurious reply that might
|
|
|
|
* desyncronise us from the peer. Use note_err() instead.
|
|
|
|
*/
|
1999-10-27 03:42:43 +00:00
|
|
|
statbytes = 0;
|
2003-01-10 10:43:24 +00:00
|
|
|
if (showprogress)
|
|
|
|
start_progress_meter(curfile, size, &statbytes);
|
2007-10-26 05:39:15 +00:00
|
|
|
set_nonblock(remin);
|
|
|
|
for (count = i = 0; i < size; i += bp->cnt) {
|
|
|
|
amt = bp->cnt;
|
1999-10-27 03:42:43 +00:00
|
|
|
if (i + amt > size)
|
|
|
|
amt = size - i;
|
|
|
|
count += amt;
|
|
|
|
do {
|
2010-09-24 12:15:11 +00:00
|
|
|
j = atomicio6(read, remin, cp, amt,
|
|
|
|
scpio, &statbytes);
|
2005-05-26 02:23:44 +00:00
|
|
|
if (j == 0) {
|
2007-10-26 05:39:15 +00:00
|
|
|
run_err("%s", j != EPIPE ?
|
|
|
|
strerror(errno) :
|
2001-03-29 00:39:55 +00:00
|
|
|
"dropped connection");
|
1999-10-27 03:42:43 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
amt -= j;
|
|
|
|
cp += j;
|
|
|
|
} while (amt > 0);
|
2003-11-21 12:56:47 +00:00
|
|
|
|
1999-10-27 03:42:43 +00:00
|
|
|
if (count == bp->cnt) {
|
|
|
|
/* Keep reading so we stay sync'd up. */
|
2020-05-01 06:31:42 +00:00
|
|
|
if (!wrerr) {
|
2005-05-26 02:23:44 +00:00
|
|
|
if (atomicio(vwrite, ofd, bp->buf,
|
|
|
|
count) != count) {
|
2020-05-01 06:31:42 +00:00
|
|
|
note_err("%s: %s", np,
|
|
|
|
strerror(errno));
|
|
|
|
wrerr = 1;
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
count = 0;
|
|
|
|
cp = bp->buf;
|
|
|
|
}
|
|
|
|
}
|
2007-10-26 05:39:15 +00:00
|
|
|
unset_nonblock(remin);
|
2020-05-01 06:31:42 +00:00
|
|
|
if (count != 0 && !wrerr &&
|
2005-05-26 02:23:44 +00:00
|
|
|
atomicio(vwrite, ofd, bp->buf, count) != count) {
|
2020-05-01 06:31:42 +00:00
|
|
|
note_err("%s: %s", np, strerror(errno));
|
|
|
|
wrerr = 1;
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
2020-05-01 06:31:42 +00:00
|
|
|
if (!wrerr && (!exists || S_ISREG(stb.st_mode)) &&
|
|
|
|
ftruncate(ofd, size) != 0)
|
|
|
|
note_err("%s: truncate: %s", np, strerror(errno));
|
1999-10-27 03:42:43 +00:00
|
|
|
if (pflag) {
|
|
|
|
if (exists || omode != mode)
|
2000-09-29 12:01:36 +00:00
|
|
|
#ifdef HAVE_FCHMOD
|
2004-07-08 13:11:44 +00:00
|
|
|
if (fchmod(ofd, omode)) {
|
2000-09-29 12:01:36 +00:00
|
|
|
#else /* HAVE_FCHMOD */
|
2004-07-08 13:11:44 +00:00
|
|
|
if (chmod(np, omode)) {
|
2000-09-29 12:01:36 +00:00
|
|
|
#endif /* HAVE_FCHMOD */
|
2020-05-01 06:31:42 +00:00
|
|
|
note_err("%s: set mode: %s",
|
2001-03-29 00:39:55 +00:00
|
|
|
np, strerror(errno));
|
2004-07-08 13:11:44 +00:00
|
|
|
}
|
1999-10-27 03:42:43 +00:00
|
|
|
} else {
|
|
|
|
if (!exists && omode != mode)
|
2000-09-29 12:01:36 +00:00
|
|
|
#ifdef HAVE_FCHMOD
|
2004-07-08 13:11:44 +00:00
|
|
|
if (fchmod(ofd, omode & ~mask)) {
|
2000-09-29 12:01:36 +00:00
|
|
|
#else /* HAVE_FCHMOD */
|
2004-07-08 13:11:44 +00:00
|
|
|
if (chmod(np, omode & ~mask)) {
|
2000-09-29 12:01:36 +00:00
|
|
|
#endif /* HAVE_FCHMOD */
|
2020-05-01 06:31:42 +00:00
|
|
|
note_err("%s: set mode: %s",
|
2001-03-29 00:39:55 +00:00
|
|
|
np, strerror(errno));
|
2004-07-08 13:11:44 +00:00
|
|
|
}
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
2020-05-01 06:31:42 +00:00
|
|
|
if (close(ofd) == -1)
|
2020-05-29 21:22:02 +00:00
|
|
|
note_err("%s: close: %s", np, strerror(errno));
|
1999-11-24 13:26:21 +00:00
|
|
|
(void) response();
|
2016-03-02 22:43:52 +00:00
|
|
|
if (showprogress)
|
|
|
|
stop_progress_meter();
|
2020-05-01 06:31:42 +00:00
|
|
|
if (setimes && !wrerr) {
|
1999-10-27 03:42:43 +00:00
|
|
|
setimes = 0;
|
2019-06-28 13:35:04 +00:00
|
|
|
if (utimes(np, tv) == -1) {
|
2020-05-01 06:31:42 +00:00
|
|
|
note_err("%s: set times: %s",
|
2001-03-29 00:39:55 +00:00
|
|
|
np, strerror(errno));
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-01 06:31:42 +00:00
|
|
|
/* If no error was noted then signal success for this file */
|
|
|
|
if (note_err(NULL) == 0)
|
2003-07-03 03:46:56 +00:00
|
|
|
(void) atomicio(vwrite, remout, "", 1);
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
2019-02-10 11:15:52 +00:00
|
|
|
done:
|
|
|
|
for (n = 0; n < npatterns; n++)
|
|
|
|
free(patterns[n]);
|
|
|
|
free(patterns);
|
|
|
|
return;
|
1999-10-27 03:42:43 +00:00
|
|
|
screwup:
|
2019-02-10 11:15:52 +00:00
|
|
|
for (n = 0; n < npatterns; n++)
|
|
|
|
free(patterns[n]);
|
|
|
|
free(patterns);
|
1999-10-27 03:42:43 +00:00
|
|
|
run_err("protocol error: %s", why);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2021-08-07 00:06:30 +00:00
|
|
|
void
|
|
|
|
throughlocal_sftp(struct sftp_conn *from, struct sftp_conn *to,
|
2021-08-09 23:49:31 +00:00
|
|
|
char *src, char *targ)
|
2021-08-07 00:06:30 +00:00
|
|
|
{
|
|
|
|
char *target = NULL, *filename = NULL, *abs_dst = NULL;
|
2021-08-09 23:49:31 +00:00
|
|
|
char *abs_src = NULL, *tmp = NULL;
|
2021-08-07 00:06:30 +00:00
|
|
|
glob_t g;
|
|
|
|
int i, r, targetisdir, err = 0;
|
|
|
|
|
|
|
|
if ((filename = basename(src)) == NULL)
|
|
|
|
fatal("basename %s: %s", src, strerror(errno));
|
|
|
|
|
2021-08-09 23:49:31 +00:00
|
|
|
if ((abs_src = prepare_remote_path(from, src)) == NULL ||
|
|
|
|
(target = prepare_remote_path(to, targ)) == NULL)
|
2021-08-09 23:44:32 +00:00
|
|
|
cleanup_exit(255);
|
2021-08-07 00:06:30 +00:00
|
|
|
memset(&g, 0, sizeof(g));
|
|
|
|
|
|
|
|
targetisdir = remote_is_dir(to, target);
|
|
|
|
if (!targetisdir && targetshouldbedirectory) {
|
2021-09-16 15:22:22 +00:00
|
|
|
error("%s: destination is not a directory", target);
|
2021-08-07 00:06:30 +00:00
|
|
|
err = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug3_f("copying remote %s to remote %s", abs_src, target);
|
|
|
|
if ((r = remote_glob(from, abs_src, GLOB_MARK, NULL, &g)) != 0) {
|
|
|
|
if (r == GLOB_NOSPACE)
|
2021-09-16 15:22:22 +00:00
|
|
|
error("%s: too many glob matches", abs_src);
|
2021-08-07 00:06:30 +00:00
|
|
|
else
|
2021-09-16 15:22:22 +00:00
|
|
|
error("%s: %s", abs_src, strerror(ENOENT));
|
2021-08-07 00:06:30 +00:00
|
|
|
err = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; g.gl_pathv[i] && !interrupted; i++) {
|
|
|
|
tmp = xstrdup(g.gl_pathv[i]);
|
|
|
|
if ((filename = basename(tmp)) == NULL) {
|
|
|
|
error("basename %s: %s", tmp, strerror(errno));
|
|
|
|
err = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (targetisdir)
|
|
|
|
abs_dst = path_append(target, filename);
|
|
|
|
else
|
|
|
|
abs_dst = xstrdup(target);
|
|
|
|
|
|
|
|
debug("Fetching %s to %s\n", g.gl_pathv[i], abs_dst);
|
|
|
|
if (globpath_is_dir(g.gl_pathv[i]) && iamrecursive) {
|
|
|
|
if (crossload_dir(from, to, g.gl_pathv[i], abs_dst,
|
2021-08-07 00:12:09 +00:00
|
|
|
NULL, pflag, SFTP_PROGRESS_ONLY, 1) == -1)
|
2021-08-07 00:06:30 +00:00
|
|
|
err = -1;
|
|
|
|
} else {
|
|
|
|
if (do_crossload(from, to, g.gl_pathv[i], abs_dst, NULL,
|
|
|
|
pflag) == -1)
|
|
|
|
err = -1;
|
|
|
|
}
|
|
|
|
free(abs_dst);
|
|
|
|
abs_dst = NULL;
|
|
|
|
free(tmp);
|
|
|
|
tmp = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
free(abs_src);
|
|
|
|
free(abs_dst);
|
|
|
|
free(target);
|
|
|
|
free(tmp);
|
|
|
|
globfree(&g);
|
|
|
|
if (err == -1)
|
2021-09-16 15:11:19 +00:00
|
|
|
errs = 1;
|
2021-08-07 00:06:30 +00:00
|
|
|
}
|
|
|
|
|
1999-10-27 03:42:43 +00:00
|
|
|
int
|
2001-12-06 17:55:26 +00:00
|
|
|
response(void)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
2016-05-25 23:48:45 +00:00
|
|
|
char ch, *cp, resp, rbuf[2048], visbuf[2048];
|
1999-10-27 03:42:43 +00:00
|
|
|
|
- Remove references to SSLeay.
- Big OpenBSD CVS update
- markus@cvs.openbsd.org
[clientloop.c]
- typo
[session.c]
- update proctitle on pty alloc/dealloc, e.g. w/ windows client
[session.c]
- update proctitle for proto 1, too
[channels.h nchan.c serverloop.c session.c sshd.c]
- use c-style comments
- deraadt@cvs.openbsd.org
[scp.c]
- more atomicio
- markus@cvs.openbsd.org
[channels.c]
- set O_NONBLOCK
[ssh.1]
- update AUTHOR
[readconf.c ssh-keygen.c ssh.h]
- default DSA key file ~/.ssh/id_dsa
[clientloop.c]
- typo, rm verbose debug
- deraadt@cvs.openbsd.org
[ssh-keygen.1]
- document DSA use of ssh-keygen
[sshd.8]
- a start at describing what i understand of the DSA side
[ssh-keygen.1]
- document -X and -x
[ssh-keygen.c]
- simplify usage
- markus@cvs.openbsd.org
[sshd.8]
- there is no rhosts_dsa
[ssh-keygen.1]
- document -y, update -X,-x
[nchan.c]
- fix close for non-open ssh1 channels
[servconf.c servconf.h ssh.h sshd.8 sshd.c ]
- s/DsaKey/HostDSAKey/, document option
[sshconnect2.c]
- respect number_of_password_prompts
[channels.c channels.h servconf.c servconf.h session.c sshd.8]
- GatewayPorts for sshd, ok deraadt@
[ssh-add.1 ssh-agent.1 ssh.1]
- more doc on: DSA, id_dsa, known_hosts2, authorized_keys2
[ssh.1]
- more info on proto 2
[sshd.8]
- sync AUTHOR w/ ssh.1
[key.c key.h sshconnect.c]
- print key type when talking about host keys
[packet.c]
- clear padding in ssh2
[dsa.c key.c radix.c ssh.h sshconnect1.c uuencode.c uuencode.h]
- replace broken uuencode w/ libc b64_ntop
[auth2.c]
- log failure before sending the reply
[key.c radix.c uuencode.c]
- remote trailing comments before calling __b64_pton
[auth2.c readconf.c readconf.h servconf.c servconf.h ssh.1]
[sshconnect2.c sshd.8]
- add DSAAuthetication option to ssh/sshd, document SSH2 in sshd.8
- Bring in b64_ntop and b64_pton from OpenBSD libc (bsd-base64.[ch])
2000-05-07 02:03:14 +00:00
|
|
|
if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
|
1999-10-27 03:42:43 +00:00
|
|
|
lostconn(0);
|
|
|
|
|
|
|
|
cp = rbuf;
|
1999-11-24 13:26:21 +00:00
|
|
|
switch (resp) {
|
|
|
|
case 0: /* ok */
|
1999-10-27 03:42:43 +00:00
|
|
|
return (0);
|
|
|
|
default:
|
|
|
|
*cp++ = resp;
|
|
|
|
/* FALLTHROUGH */
|
1999-11-24 13:26:21 +00:00
|
|
|
case 1: /* error, followed by error msg */
|
|
|
|
case 2: /* fatal error, "" */
|
1999-10-27 03:42:43 +00:00
|
|
|
do {
|
- Remove references to SSLeay.
- Big OpenBSD CVS update
- markus@cvs.openbsd.org
[clientloop.c]
- typo
[session.c]
- update proctitle on pty alloc/dealloc, e.g. w/ windows client
[session.c]
- update proctitle for proto 1, too
[channels.h nchan.c serverloop.c session.c sshd.c]
- use c-style comments
- deraadt@cvs.openbsd.org
[scp.c]
- more atomicio
- markus@cvs.openbsd.org
[channels.c]
- set O_NONBLOCK
[ssh.1]
- update AUTHOR
[readconf.c ssh-keygen.c ssh.h]
- default DSA key file ~/.ssh/id_dsa
[clientloop.c]
- typo, rm verbose debug
- deraadt@cvs.openbsd.org
[ssh-keygen.1]
- document DSA use of ssh-keygen
[sshd.8]
- a start at describing what i understand of the DSA side
[ssh-keygen.1]
- document -X and -x
[ssh-keygen.c]
- simplify usage
- markus@cvs.openbsd.org
[sshd.8]
- there is no rhosts_dsa
[ssh-keygen.1]
- document -y, update -X,-x
[nchan.c]
- fix close for non-open ssh1 channels
[servconf.c servconf.h ssh.h sshd.8 sshd.c ]
- s/DsaKey/HostDSAKey/, document option
[sshconnect2.c]
- respect number_of_password_prompts
[channels.c channels.h servconf.c servconf.h session.c sshd.8]
- GatewayPorts for sshd, ok deraadt@
[ssh-add.1 ssh-agent.1 ssh.1]
- more doc on: DSA, id_dsa, known_hosts2, authorized_keys2
[ssh.1]
- more info on proto 2
[sshd.8]
- sync AUTHOR w/ ssh.1
[key.c key.h sshconnect.c]
- print key type when talking about host keys
[packet.c]
- clear padding in ssh2
[dsa.c key.c radix.c ssh.h sshconnect1.c uuencode.c uuencode.h]
- replace broken uuencode w/ libc b64_ntop
[auth2.c]
- log failure before sending the reply
[key.c radix.c uuencode.c]
- remote trailing comments before calling __b64_pton
[auth2.c readconf.c readconf.h servconf.c servconf.h ssh.1]
[sshconnect2.c sshd.8]
- add DSAAuthetication option to ssh/sshd, document SSH2 in sshd.8
- Bring in b64_ntop and b64_pton from OpenBSD libc (bsd-base64.[ch])
2000-05-07 02:03:14 +00:00
|
|
|
if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
|
1999-10-27 03:42:43 +00:00
|
|
|
lostconn(0);
|
|
|
|
*cp++ = ch;
|
|
|
|
} while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
|
|
|
|
|
2016-05-25 23:48:45 +00:00
|
|
|
if (!iamremote) {
|
|
|
|
cp[-1] = '\0';
|
|
|
|
(void) snmprintf(visbuf, sizeof(visbuf),
|
|
|
|
NULL, "%s\n", rbuf);
|
|
|
|
(void) atomicio(vwrite, STDERR_FILENO,
|
|
|
|
visbuf, strlen(visbuf));
|
|
|
|
}
|
1999-10-27 03:42:43 +00:00
|
|
|
++errs;
|
|
|
|
if (resp == 1)
|
|
|
|
return (-1);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-12-06 17:55:26 +00:00
|
|
|
usage(void)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
2001-09-18 05:38:44 +00:00
|
|
|
(void) fprintf(stderr,
|
2021-09-20 06:53:56 +00:00
|
|
|
"usage: scp [-346ABCOpqRrsTv] [-c cipher] [-D sftp_server_path] [-F ssh_config]\n"
|
2021-08-10 03:33:34 +00:00
|
|
|
" [-i identity_file] [-J destination] [-l limit]\n"
|
2021-08-09 07:19:12 +00:00
|
|
|
" [-o ssh_option] [-P port] [-S program] source ... target\n");
|
1999-10-27 03:42:43 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-11-24 13:26:21 +00:00
|
|
|
run_err(const char *fmt,...)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
|
|
|
static FILE *fp;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
++errs;
|
2006-03-26 02:49:43 +00:00
|
|
|
if (fp != NULL || (remout != -1 && (fp = fdopen(remout, "w")))) {
|
|
|
|
(void) fprintf(fp, "%c", 0x01);
|
|
|
|
(void) fprintf(fp, "scp: ");
|
|
|
|
va_start(ap, fmt);
|
|
|
|
(void) vfprintf(fp, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
(void) fprintf(fp, "\n");
|
|
|
|
(void) fflush(fp);
|
|
|
|
}
|
1999-11-24 13:26:21 +00:00
|
|
|
|
|
|
|
if (!iamremote) {
|
1999-12-30 22:16:40 +00:00
|
|
|
va_start(ap, fmt);
|
2016-05-25 23:48:45 +00:00
|
|
|
vfmprintf(stderr, fmt, ap);
|
1999-12-30 22:16:40 +00:00
|
|
|
va_end(ap);
|
1999-11-24 13:26:21 +00:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 06:31:42 +00:00
|
|
|
/*
|
|
|
|
* Notes a sink error for sending at the end of a file transfer. Returns 0 if
|
|
|
|
* no error has been noted or -1 otherwise. Use note_err(NULL) to flush
|
|
|
|
* any active error at the end of the transfer.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
note_err(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
static char *emsg;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
/* Replay any previously-noted error */
|
|
|
|
if (fmt == NULL) {
|
|
|
|
if (emsg == NULL)
|
|
|
|
return 0;
|
|
|
|
run_err("%s", emsg);
|
|
|
|
free(emsg);
|
|
|
|
emsg = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
errs++;
|
|
|
|
/* Prefer first-noted error */
|
|
|
|
if (emsg != NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vasnmprintf(&emsg, INT_MAX, NULL, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
1999-10-27 03:42:43 +00:00
|
|
|
void
|
2003-06-04 12:51:08 +00:00
|
|
|
verifydir(char *cp)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
|
|
|
struct stat stb;
|
|
|
|
|
|
|
|
if (!stat(cp, &stb)) {
|
|
|
|
if (S_ISDIR(stb.st_mode))
|
|
|
|
return;
|
|
|
|
errno = ENOTDIR;
|
|
|
|
}
|
|
|
|
run_err("%s: %s", cp, strerror(errno));
|
2005-06-01 13:01:12 +00:00
|
|
|
killchild(0);
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2003-06-04 12:51:08 +00:00
|
|
|
okname(char *cp0)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
|
|
|
int c;
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
cp = cp0;
|
|
|
|
do {
|
2001-06-25 04:28:30 +00:00
|
|
|
c = (int)*cp;
|
1999-10-27 03:42:43 +00:00
|
|
|
if (c & 0200)
|
|
|
|
goto bad;
|
2013-11-21 02:56:49 +00:00
|
|
|
if (!isalpha(c) && !isdigit((unsigned char)c)) {
|
2003-02-24 00:52:58 +00:00
|
|
|
switch (c) {
|
|
|
|
case '\'':
|
|
|
|
case '"':
|
|
|
|
case '`':
|
|
|
|
case ' ':
|
|
|
|
case '#':
|
|
|
|
goto bad;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1999-10-27 03:42:43 +00:00
|
|
|
} while (*++cp);
|
|
|
|
return (1);
|
|
|
|
|
2016-05-25 23:48:45 +00:00
|
|
|
bad: fmprintf(stderr, "%s: invalid user name\n", cp0);
|
1999-10-27 03:42:43 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
BUF *
|
2003-06-04 12:51:08 +00:00
|
|
|
allocbuf(BUF *bp, int fd, int blksize)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
|
|
|
size_t size;
|
2001-10-22 00:53:58 +00:00
|
|
|
#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
|
1999-10-27 03:42:43 +00:00
|
|
|
struct stat stb;
|
|
|
|
|
2019-06-28 13:35:04 +00:00
|
|
|
if (fstat(fd, &stb) == -1) {
|
1999-10-27 03:42:43 +00:00
|
|
|
run_err("fstat: %s", strerror(errno));
|
|
|
|
return (0);
|
|
|
|
}
|
2016-09-12 01:22:38 +00:00
|
|
|
size = ROUNDUP(stb.st_blksize, blksize);
|
2002-12-23 02:22:09 +00:00
|
|
|
if (size == 0)
|
1999-11-24 13:26:21 +00:00
|
|
|
size = blksize;
|
2001-10-22 00:53:58 +00:00
|
|
|
#else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
|
2001-02-05 12:42:17 +00:00
|
|
|
size = blksize;
|
2001-10-22 00:53:58 +00:00
|
|
|
#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
|
1999-10-27 03:42:43 +00:00
|
|
|
if (bp->cnt >= size)
|
|
|
|
return (bp);
|
2017-05-31 09:15:42 +00:00
|
|
|
bp->buf = xrecallocarray(bp->buf, bp->cnt, size, 1);
|
1999-10-27 03:42:43 +00:00
|
|
|
bp->cnt = size;
|
|
|
|
return (bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-06-04 12:51:08 +00:00
|
|
|
lostconn(int signo)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
|
|
|
if (!iamremote)
|
2013-05-16 10:32:29 +00:00
|
|
|
(void)write(STDERR_FILENO, "lost connection\n", 16);
|
2001-06-21 03:08:58 +00:00
|
|
|
if (signo)
|
|
|
|
_exit(1);
|
|
|
|
else
|
|
|
|
exit(1);
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
2021-08-09 07:21:01 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
cleanup_exit(int i)
|
|
|
|
{
|
|
|
|
if (remin > 0)
|
|
|
|
close(remin);
|
|
|
|
if (remout > 0)
|
|
|
|
close(remout);
|
|
|
|
if (remin2 > 0)
|
|
|
|
close(remin2);
|
|
|
|
if (remout2 > 0)
|
|
|
|
close(remout2);
|
|
|
|
if (do_cmd_pid > 0)
|
|
|
|
waitpid(do_cmd_pid, NULL, 0);
|
|
|
|
if (do_cmd_pid2 > 0)
|
|
|
|
waitpid(do_cmd_pid2, NULL, 0);
|
|
|
|
exit(i);
|
|
|
|
}
|