2018-11-16 11:03:10 +08:00
|
|
|
/* $OpenBSD: scp.c,v 1.198 2018/11/16 03:03:10 djm Exp $ */
|
1999-10-27 11:42:43 +08:00
|
|
|
/*
|
2000-09-16 10:29:08 +08: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 09:18:38 +08:00
|
|
|
*
|
2000-09-16 10:29:08 +08: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 09:18:38 +08:00
|
|
|
*
|
1999-11-24 21:26:21 +08:00
|
|
|
* 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi>
|
2000-04-16 09:18:38 +08:00
|
|
|
*
|
2000-09-16 10:29:08 +08: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 14:59:27 +08:00
|
|
|
* Copyright (c) 1999 Theo de Raadt. All rights reserved.
|
|
|
|
* Copyright (c) 1999 Aaron Campbell. All rights reserved.
|
2000-09-16 10:29:08 +08: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 11:42:43 +08:00
|
|
|
|
|
|
|
/*
|
2000-08-23 08:46:23 +08:00
|
|
|
* Parts from:
|
|
|
|
*
|
1999-10-27 11:42:43 +08: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 11:06:18 +08:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1999-10-27 11:42:43 +08: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 08:29:24 +08:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
2006-03-15 08:45:54 +08:00
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
# include <sys/stat.h>
|
|
|
|
#endif
|
2008-03-14 08:59:50 +08:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
#include <poll.h>
|
|
|
|
#else
|
|
|
|
# ifdef HAVE_SYS_POLL_H
|
|
|
|
# include <sys/poll.h>
|
|
|
|
# endif
|
2008-02-29 12:07:01 +08:00
|
|
|
#endif
|
2006-08-05 08:57:45 +08:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
# include <sys/time.h>
|
|
|
|
#endif
|
2006-03-15 08:29:24 +08:00
|
|
|
#include <sys/wait.h>
|
2006-08-05 10:39:39 +08:00
|
|
|
#include <sys/uio.h>
|
2006-03-15 08:25:13 +08:00
|
|
|
|
2006-03-15 08:53:45 +08:00
|
|
|
#include <ctype.h>
|
2006-03-15 08:25:13 +08:00
|
|
|
#include <dirent.h>
|
2006-07-12 20:22:46 +08:00
|
|
|
#include <errno.h>
|
2006-07-10 19:13:46 +08:00
|
|
|
#include <fcntl.h>
|
2015-01-16 14:40:12 +08:00
|
|
|
#include <limits.h>
|
2016-05-26 07:48:45 +08:00
|
|
|
#include <locale.h>
|
2006-07-10 18:53:08 +08:00
|
|
|
#include <pwd.h>
|
2006-03-15 08:52:09 +08:00
|
|
|
#include <signal.h>
|
2006-07-12 20:15:16 +08:00
|
|
|
#include <stdarg.h>
|
2017-05-01 11:19:41 +08:00
|
|
|
#ifdef HAVE_STDINT_H
|
2017-04-28 11:21:12 +08:00
|
|
|
#include <stdint.h>
|
2017-05-01 11:19:41 +08:00
|
|
|
#endif
|
2006-08-05 09:37:59 +08:00
|
|
|
#include <stdio.h>
|
2006-08-05 09:34:19 +08:00
|
|
|
#include <stdlib.h>
|
2006-07-24 12:13:33 +08:00
|
|
|
#include <string.h>
|
2006-07-24 12:09:40 +08:00
|
|
|
#include <time.h>
|
2006-07-24 12:01:23 +08:00
|
|
|
#include <unistd.h>
|
2013-03-20 09:55:14 +08:00
|
|
|
#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
|
2007-06-12 22:02:07 +08:00
|
|
|
#include <vis.h>
|
|
|
|
#endif
|
1999-10-27 11:42:43 +08:00
|
|
|
|
|
|
|
#include "xmalloc.h"
|
2017-10-22 07:06:24 +08:00
|
|
|
#include "ssh.h"
|
2001-01-22 13:34:40 +08:00
|
|
|
#include "atomicio.h"
|
|
|
|
#include "pathnames.h"
|
|
|
|
#include "log.h"
|
2001-02-18 11:55:16 +08:00
|
|
|
#include "misc.h"
|
2003-01-10 18:43:24 +08:00
|
|
|
#include "progressmeter.h"
|
2016-05-26 07:48:45 +08:00
|
|
|
#include "utf8.h"
|
1999-10-27 11:42:43 +08:00
|
|
|
|
2000-11-17 11:47:20 +08:00
|
|
|
extern char *__progname;
|
|
|
|
|
2007-10-26 13:39:15 +08:00
|
|
|
#define COPY_BUFLEN 16384
|
|
|
|
|
2017-10-22 07:06:24 +08:00
|
|
|
int do_cmd(char *host, char *remuser, int port, char *cmd, int *fdin, int *fdout);
|
|
|
|
int do_cmd2(char *host, char *remuser, int port, char *cmd, int fdin, int fdout);
|
2006-04-23 10:04:27 +08:00
|
|
|
|
2001-05-09 04:27:25 +08:00
|
|
|
/* Struct for addargs */
|
|
|
|
arglist args;
|
2010-12-01 09:03:19 +08:00
|
|
|
arglist remote_remote_args;
|
2000-10-14 13:23:11 +08:00
|
|
|
|
2003-01-24 08:36:58 +08:00
|
|
|
/* Bandwidth limit */
|
2010-09-24 20:15:11 +08:00
|
|
|
long long limit_kbps = 0;
|
|
|
|
struct bwlimit bwlimit;
|
2003-01-24 08:36:58 +08:00
|
|
|
|
1999-10-27 11:42:43 +08:00
|
|
|
/* Name of current file being transferred. */
|
|
|
|
char *curfile;
|
|
|
|
|
|
|
|
/* This is set to non-zero to enable verbose mode. */
|
1999-11-24 21:26:21 +08:00
|
|
|
int verbose_mode = 0;
|
1999-10-27 11:42:43 +08:00
|
|
|
|
|
|
|
/* This is set to zero if the progressmeter is not desired. */
|
|
|
|
int showprogress = 1;
|
|
|
|
|
2011-01-06 19:40:30 +08:00
|
|
|
/*
|
|
|
|
* This is set to non-zero if remote-remote copy should be piped
|
|
|
|
* through this process.
|
|
|
|
*/
|
|
|
|
int throughlocal = 0;
|
|
|
|
|
2017-10-22 07:06:24 +08:00
|
|
|
/* Non-standard port to use for the ssh connection or -1. */
|
|
|
|
int sshport = -1;
|
|
|
|
|
2000-08-23 08:46:23 +08:00
|
|
|
/* This is the program to execute for the secured connection. ("ssh" or -S) */
|
2007-12-02 19:48:40 +08:00
|
|
|
char *ssh_program = _PATH_SSH_PROGRAM;
|
2000-08-23 08:46:23 +08:00
|
|
|
|
2002-11-09 23:54:08 +08:00
|
|
|
/* This is used to store the pid of ssh_program */
|
2003-06-04 20:51:24 +08:00
|
|
|
pid_t do_cmd_pid = -1;
|
|
|
|
|
|
|
|
static void
|
|
|
|
killchild(int signo)
|
|
|
|
{
|
2005-01-24 18:57:40 +08:00
|
|
|
if (do_cmd_pid > 1) {
|
2005-06-01 21:01:12 +08:00
|
|
|
kill(do_cmd_pid, signo ? signo : SIGTERM);
|
2005-01-24 18:57:40 +08:00
|
|
|
waitpid(do_cmd_pid, NULL, 0);
|
|
|
|
}
|
2003-06-04 20:51:24 +08:00
|
|
|
|
2005-06-01 21:01:12 +08:00
|
|
|
if (signo)
|
|
|
|
_exit(1);
|
|
|
|
exit(1);
|
2003-06-04 20:51:24 +08:00
|
|
|
}
|
2002-11-09 23:54:08 +08:00
|
|
|
|
2010-07-02 11:37:33 +08:00
|
|
|
static void
|
|
|
|
suspchild(int signo)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
|
|
|
if (do_cmd_pid > 1) {
|
|
|
|
kill(do_cmd_pid, signo);
|
|
|
|
while (waitpid(do_cmd_pid, &status, WUNTRACED) == -1 &&
|
|
|
|
errno == EINTR)
|
|
|
|
;
|
|
|
|
kill(getpid(), SIGSTOP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-31 18:49:27 +08: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-26 07:48:45 +08:00
|
|
|
fmprintf(stderr, " %s", a->list[i]);
|
2006-01-31 18:49:27 +08: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;
|
|
|
|
signal(SIGTERM, killchild);
|
|
|
|
signal(SIGINT, killchild);
|
|
|
|
signal(SIGHUP, killchild);
|
|
|
|
|
|
|
|
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 08:54:57 +08: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 11:42:43 +08:00
|
|
|
|
2000-04-16 09:18:38 +08:00
|
|
|
int
|
2017-10-22 07:06:24 +08:00
|
|
|
do_cmd(char *host, char *remuser, int port, char *cmd, int *fdin, int *fdout)
|
1999-10-27 11:42:43 +08:00
|
|
|
{
|
1999-11-24 21:26:21 +08:00
|
|
|
int pin[2], pout[2], reserved[2];
|
|
|
|
|
|
|
|
if (verbose_mode)
|
2016-05-26 07:48:45 +08:00
|
|
|
fmprintf(stderr,
|
2001-08-07 05:17:12 +08:00
|
|
|
"Executing: program %s host %s, user %s, command %s\n",
|
|
|
|
ssh_program, host,
|
|
|
|
remuser ? remuser : "(unspecified)", cmd);
|
1999-11-24 21:26:21 +08:00
|
|
|
|
2017-10-22 07:06:24 +08:00
|
|
|
if (port == -1)
|
|
|
|
port = sshport;
|
|
|
|
|
1999-11-25 08:54:57 +08:00
|
|
|
/*
|
|
|
|
* Reserve two descriptors so that the real pipes won't get
|
|
|
|
* descriptors 0 and 1 because that will screw up dup2 below.
|
|
|
|
*/
|
2006-06-13 11:00:25 +08:00
|
|
|
if (pipe(reserved) < 0)
|
|
|
|
fatal("pipe: %s", strerror(errno));
|
1999-11-24 21:26:21 +08:00
|
|
|
|
|
|
|
/* Create a socket pair for communicating with ssh. */
|
|
|
|
if (pipe(pin) < 0)
|
|
|
|
fatal("pipe: %s", strerror(errno));
|
|
|
|
if (pipe(pout) < 0)
|
|
|
|
fatal("pipe: %s", strerror(errno));
|
|
|
|
|
|
|
|
/* Free the reserved descriptors. */
|
|
|
|
close(reserved[0]);
|
|
|
|
close(reserved[1]);
|
|
|
|
|
2010-07-02 11:37:33 +08:00
|
|
|
signal(SIGTSTP, suspchild);
|
|
|
|
signal(SIGTTIN, suspchild);
|
|
|
|
signal(SIGTTOU, suspchild);
|
|
|
|
|
2003-06-18 18:26:34 +08:00
|
|
|
/* Fork a child to execute the command on the remote host using ssh. */
|
2002-11-09 23:54:08 +08:00
|
|
|
do_cmd_pid = fork();
|
|
|
|
if (do_cmd_pid == 0) {
|
1999-11-24 21:26:21 +08:00
|
|
|
/* Child. */
|
|
|
|
close(pin[1]);
|
|
|
|
close(pout[0]);
|
|
|
|
dup2(pin[0], 0);
|
|
|
|
dup2(pout[1], 1);
|
|
|
|
close(pin[0]);
|
|
|
|
close(pout[1]);
|
|
|
|
|
2006-01-31 18:49:27 +08:00
|
|
|
replacearg(&args, 0, "%s", ssh_program);
|
2017-10-22 07:06:24 +08:00
|
|
|
if (port != -1) {
|
|
|
|
addargs(&args, "-p");
|
|
|
|
addargs(&args, "%d", port);
|
|
|
|
}
|
2010-01-08 15:53:43 +08:00
|
|
|
if (remuser != NULL) {
|
|
|
|
addargs(&args, "-l");
|
|
|
|
addargs(&args, "%s", remuser);
|
|
|
|
}
|
|
|
|
addargs(&args, "--");
|
2001-05-09 04:27:25 +08:00
|
|
|
addargs(&args, "%s", host);
|
|
|
|
addargs(&args, "%s", cmd);
|
1999-11-24 21:26:21 +08:00
|
|
|
|
2000-10-14 13:23:11 +08:00
|
|
|
execvp(ssh_program, args.list);
|
2000-08-23 08:46:23 +08:00
|
|
|
perror(ssh_program);
|
1999-11-24 21:26:21 +08:00
|
|
|
exit(1);
|
2002-11-09 23:54:08 +08:00
|
|
|
} else if (do_cmd_pid == -1) {
|
|
|
|
fatal("fork: %s", strerror(errno));
|
1999-10-27 11:42:43 +08:00
|
|
|
}
|
1999-11-24 21:26:21 +08:00
|
|
|
/* Parent. Close the other side, and return the local side. */
|
|
|
|
close(pin[0]);
|
|
|
|
*fdout = pin[1];
|
|
|
|
close(pout[1]);
|
|
|
|
*fdin = pout[0];
|
2003-06-04 20:51:24 +08:00
|
|
|
signal(SIGTERM, killchild);
|
|
|
|
signal(SIGINT, killchild);
|
|
|
|
signal(SIGHUP, killchild);
|
1999-11-24 21:26:21 +08:00
|
|
|
return 0;
|
1999-10-27 11:42:43 +08:00
|
|
|
}
|
|
|
|
|
2011-01-06 19:40:30 +08:00
|
|
|
/*
|
2018-04-10 08:10:49 +08:00
|
|
|
* This function executes a command similar to do_cmd(), but expects the
|
2011-01-06 19:40:30 +08: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
|
2017-10-22 07:06:24 +08:00
|
|
|
do_cmd2(char *host, char *remuser, int port, char *cmd, int fdin, int fdout)
|
2011-01-06 19:40:30 +08:00
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
if (verbose_mode)
|
2016-05-26 07:48:45 +08:00
|
|
|
fmprintf(stderr,
|
2011-01-06 19:40:30 +08:00
|
|
|
"Executing: 2nd program %s host %s, user %s, command %s\n",
|
|
|
|
ssh_program, host,
|
|
|
|
remuser ? remuser : "(unspecified)", cmd);
|
|
|
|
|
2017-10-22 07:06:24 +08:00
|
|
|
if (port == -1)
|
|
|
|
port = sshport;
|
|
|
|
|
2011-01-06 19:40:30 +08: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-22 07:06:24 +08:00
|
|
|
if (port != -1) {
|
|
|
|
addargs(&args, "-p");
|
|
|
|
addargs(&args, "%d", port);
|
|
|
|
}
|
2011-01-06 19:40:30 +08:00
|
|
|
if (remuser != NULL) {
|
|
|
|
addargs(&args, "-l");
|
|
|
|
addargs(&args, "%s", remuser);
|
|
|
|
}
|
|
|
|
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 11:42:43 +08:00
|
|
|
typedef struct {
|
2005-06-17 10:59:34 +08:00
|
|
|
size_t cnt;
|
1999-10-27 11:42:43 +08:00
|
|
|
char *buf;
|
|
|
|
} BUF;
|
|
|
|
|
1999-11-24 21:26:21 +08:00
|
|
|
BUF *allocbuf(BUF *, int, int);
|
|
|
|
void lostconn(int);
|
|
|
|
int okname(char *);
|
|
|
|
void run_err(const char *,...);
|
|
|
|
void verifydir(char *);
|
1999-10-27 11:42:43 +08:00
|
|
|
|
|
|
|
struct passwd *pwd;
|
1999-11-24 21:26:21 +08:00
|
|
|
uid_t userid;
|
1999-10-27 11:42:43 +08:00
|
|
|
int errs, remin, remout;
|
|
|
|
int pflag, iamremote, iamrecursive, targetshouldbedirectory;
|
|
|
|
|
|
|
|
#define CMDNEEDS 64
|
|
|
|
char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
|
|
|
|
|
1999-11-24 21:26:21 +08:00
|
|
|
int response(void);
|
|
|
|
void rsource(char *, struct stat *);
|
|
|
|
void sink(int, char *[]);
|
|
|
|
void source(int, char *[]);
|
|
|
|
void tolocal(int, char *[]);
|
2017-10-22 07:06:24 +08:00
|
|
|
void toremote(int, char *[]);
|
1999-11-24 21:26:21 +08:00
|
|
|
void usage(void);
|
1999-10-27 11:42:43 +08:00
|
|
|
|
|
|
|
int
|
2003-06-04 20:51:08 +08:00
|
|
|
main(int argc, char **argv)
|
1999-10-27 11:42:43 +08:00
|
|
|
{
|
2006-07-10 20:19:53 +08:00
|
|
|
int ch, fflag, tflag, status, n;
|
2017-10-22 07:06:24 +08:00
|
|
|
char **newargv;
|
2010-09-24 20:15:11 +08:00
|
|
|
const char *errstr;
|
1999-10-27 11:42:43 +08:00
|
|
|
extern char *optarg;
|
|
|
|
extern int optind;
|
|
|
|
|
2005-10-03 16:11:24 +08:00
|
|
|
/* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
|
|
|
|
sanitise_stdfd();
|
|
|
|
|
2016-12-12 10:57:10 +08:00
|
|
|
msetlocale();
|
2016-05-26 07:48:45 +08:00
|
|
|
|
2006-07-10 20:19:53 +08:00
|
|
|
/* Copy argv, because we modify it */
|
2016-09-12 09:22:38 +08:00
|
|
|
newargv = xcalloc(MAXIMUM(argc + 1, 1), sizeof(*newargv));
|
2006-07-10 20:19:53 +08:00
|
|
|
for (n = 0; n < argc; n++)
|
|
|
|
newargv[n] = xstrdup(argv[n]);
|
|
|
|
argv = newargv;
|
|
|
|
|
2003-08-22 07:34:41 +08:00
|
|
|
__progname = ssh_get_progname(argv[0]);
|
2000-11-17 11:47:20 +08:00
|
|
|
|
2006-01-31 18:49:27 +08:00
|
|
|
memset(&args, '\0', sizeof(args));
|
2010-12-01 09:03:19 +08:00
|
|
|
memset(&remote_remote_args, '\0', sizeof(remote_remote_args));
|
|
|
|
args.list = remote_remote_args.list = NULL;
|
2006-01-31 18:49:27 +08:00
|
|
|
addargs(&args, "%s", ssh_program);
|
2001-05-09 04:27:25 +08:00
|
|
|
addargs(&args, "-x");
|
2010-12-01 09:03:19 +08:00
|
|
|
addargs(&args, "-oForwardAgent=no");
|
|
|
|
addargs(&args, "-oPermitLocalCommand=no");
|
|
|
|
addargs(&args, "-oClearAllForwardings=yes");
|
2018-02-10 14:15:12 +08:00
|
|
|
addargs(&args, "-oRemoteCommand=none");
|
|
|
|
addargs(&args, "-oRequestTTY=no");
|
2000-10-14 13:23:11 +08:00
|
|
|
|
1999-10-27 11:42:43 +08:00
|
|
|
fflag = tflag = 0;
|
2011-01-06 19:40:30 +08:00
|
|
|
while ((ch = getopt(argc, argv, "dfl:prtvBCc:i:P:q12346S:o:F:")) != -1)
|
1999-11-24 21:26:21 +08:00
|
|
|
switch (ch) {
|
|
|
|
/* User-visible flags. */
|
2003-01-24 08:37:38 +08:00
|
|
|
case '1':
|
2017-05-01 07:28:12 +08:00
|
|
|
fatal("SSH protocol v.1 is no longer supported");
|
|
|
|
break;
|
2003-01-24 08:37:38 +08:00
|
|
|
case '2':
|
2017-05-01 07:28:12 +08:00
|
|
|
/* Ignored */
|
|
|
|
break;
|
2000-01-14 12:45:46 +08:00
|
|
|
case '4':
|
|
|
|
case '6':
|
2000-10-14 13:23:11 +08:00
|
|
|
case 'C':
|
2001-05-09 04:27:25 +08:00
|
|
|
addargs(&args, "-%c", ch);
|
2010-12-01 09:03:19 +08:00
|
|
|
addargs(&remote_remote_args, "-%c", ch);
|
2000-01-14 12:45:46 +08:00
|
|
|
break;
|
2011-01-06 19:40:30 +08:00
|
|
|
case '3':
|
|
|
|
throughlocal = 1;
|
|
|
|
break;
|
2000-10-14 13:23:11 +08:00
|
|
|
case 'o':
|
|
|
|
case 'c':
|
|
|
|
case 'i':
|
2001-09-18 13:38:44 +08:00
|
|
|
case 'F':
|
2010-12-01 09:03:19 +08:00
|
|
|
addargs(&remote_remote_args, "-%c", ch);
|
|
|
|
addargs(&remote_remote_args, "%s", optarg);
|
2010-01-08 15:53:43 +08:00
|
|
|
addargs(&args, "-%c", ch);
|
|
|
|
addargs(&args, "%s", optarg);
|
1999-10-27 11:42:43 +08:00
|
|
|
break;
|
|
|
|
case 'P':
|
2017-10-22 07:06:24 +08:00
|
|
|
sshport = a2port(optarg);
|
|
|
|
if (sshport <= 0)
|
|
|
|
fatal("bad port \"%s\"\n", optarg);
|
2000-10-14 13:23:11 +08:00
|
|
|
break;
|
|
|
|
case 'B':
|
2010-12-01 09:03:19 +08:00
|
|
|
addargs(&remote_remote_args, "-oBatchmode=yes");
|
|
|
|
addargs(&args, "-oBatchmode=yes");
|
2000-10-14 13:23:11 +08:00
|
|
|
break;
|
2003-01-24 08:36:58 +08:00
|
|
|
case 'l':
|
2010-09-24 20:15:11 +08:00
|
|
|
limit_kbps = strtonum(optarg, 1, 100 * 1024 * 1024,
|
|
|
|
&errstr);
|
|
|
|
if (errstr != NULL)
|
2003-01-24 08:36:58 +08:00
|
|
|
usage();
|
2010-09-24 20:15:11 +08:00
|
|
|
limit_kbps *= 1024; /* kbps */
|
|
|
|
bandwidth_limit_init(&bwlimit, limit_kbps, COPY_BUFLEN);
|
2003-01-24 08:36:58 +08:00
|
|
|
break;
|
2000-10-14 13:23:11 +08:00
|
|
|
case 'p':
|
|
|
|
pflag = 1;
|
1999-11-24 21:26:21 +08:00
|
|
|
break;
|
1999-10-27 11:42:43 +08:00
|
|
|
case 'r':
|
|
|
|
iamrecursive = 1;
|
|
|
|
break;
|
2000-08-23 08:46:23 +08:00
|
|
|
case 'S':
|
2000-10-14 13:23:11 +08:00
|
|
|
ssh_program = xstrdup(optarg);
|
|
|
|
break;
|
|
|
|
case 'v':
|
2001-06-09 09:15:11 +08:00
|
|
|
addargs(&args, "-v");
|
2010-12-01 09:03:19 +08:00
|
|
|
addargs(&remote_remote_args, "-v");
|
2000-10-14 13:23:11 +08:00
|
|
|
verbose_mode = 1;
|
|
|
|
break;
|
|
|
|
case 'q':
|
2003-11-17 18:19:05 +08:00
|
|
|
addargs(&args, "-q");
|
2010-12-01 09:03:19 +08:00
|
|
|
addargs(&remote_remote_args, "-q");
|
2000-10-14 13:23:11 +08:00
|
|
|
showprogress = 0;
|
2000-08-23 08:46:23 +08:00
|
|
|
break;
|
|
|
|
|
1999-11-24 21:26:21 +08:00
|
|
|
/* Server options. */
|
1999-10-27 11:42:43 +08:00
|
|
|
case 'd':
|
|
|
|
targetshouldbedirectory = 1;
|
|
|
|
break;
|
1999-11-24 21:26:21 +08:00
|
|
|
case 'f': /* "from" */
|
1999-10-27 11:42:43 +08:00
|
|
|
iamremote = 1;
|
|
|
|
fflag = 1;
|
|
|
|
break;
|
1999-11-24 21:26:21 +08:00
|
|
|
case 't': /* "to" */
|
1999-10-27 11:42:43 +08:00
|
|
|
iamremote = 1;
|
|
|
|
tflag = 1;
|
2001-04-13 22:28:42 +08:00
|
|
|
#ifdef HAVE_CYGWIN
|
|
|
|
setmode(0, O_BINARY);
|
|
|
|
#endif
|
1999-10-27 11:42:43 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if ((pwd = getpwuid(userid = getuid())) == NULL)
|
2003-07-19 18:07:45 +08:00
|
|
|
fatal("unknown user %u", (u_int) userid);
|
1999-10-27 11:42:43 +08:00
|
|
|
|
2007-02-19 19:14:11 +08:00
|
|
|
if (!isatty(STDOUT_FILENO))
|
1999-10-27 11:42:43 +08:00
|
|
|
showprogress = 0;
|
|
|
|
|
2015-11-27 08:49:31 +08: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 11:42:43 +08:00
|
|
|
remin = STDIN_FILENO;
|
|
|
|
remout = STDOUT_FILENO;
|
|
|
|
|
2001-02-05 20:42:17 +08:00
|
|
|
if (fflag) {
|
1999-11-24 21:26:21 +08:00
|
|
|
/* Follow "protocol", send data. */
|
|
|
|
(void) response();
|
1999-10-27 11:42:43 +08:00
|
|
|
source(argc, argv);
|
|
|
|
exit(errs != 0);
|
|
|
|
}
|
1999-11-24 21:26:21 +08:00
|
|
|
if (tflag) {
|
|
|
|
/* Receive data. */
|
1999-10-27 11:42:43 +08:00
|
|
|
sink(argc, argv);
|
|
|
|
exit(errs != 0);
|
|
|
|
}
|
|
|
|
if (argc < 2)
|
|
|
|
usage();
|
|
|
|
if (argc > 2)
|
|
|
|
targetshouldbedirectory = 1;
|
|
|
|
|
|
|
|
remin = remout = -1;
|
2002-11-09 23:54:08 +08:00
|
|
|
do_cmd_pid = -1;
|
1999-10-27 11:42:43 +08:00
|
|
|
/* Command to be executed on remote system using "ssh". */
|
2001-02-09 09:23:39 +08:00
|
|
|
(void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
|
|
|
|
verbose_mode ? " -v" : "",
|
2000-08-23 08:46:23 +08:00
|
|
|
iamrecursive ? " -r" : "", pflag ? " -p" : "",
|
|
|
|
targetshouldbedirectory ? " -d" : "");
|
1999-10-27 11:42:43 +08:00
|
|
|
|
1999-11-24 21:26:21 +08:00
|
|
|
(void) signal(SIGPIPE, lostconn);
|
1999-10-27 11:42:43 +08:00
|
|
|
|
2017-10-22 07:06:24 +08:00
|
|
|
if (colon(argv[argc - 1])) /* Dest is remote host. */
|
|
|
|
toremote(argc, argv);
|
1999-10-27 11:42:43 +08:00
|
|
|
else {
|
|
|
|
if (targetshouldbedirectory)
|
|
|
|
verifydir(argv[argc - 1]);
|
2006-01-31 19:11:37 +08:00
|
|
|
tolocal(argc, argv); /* Dest is local host. */
|
1999-10-27 11:42:43 +08:00
|
|
|
}
|
2002-11-09 23:54:08 +08:00
|
|
|
/*
|
|
|
|
* Finally check the exit status of the ssh process, if one was forked
|
2008-11-03 16:23:45 +08:00
|
|
|
* and no error has occurred yet
|
2002-11-09 23:54:08 +08:00
|
|
|
*/
|
|
|
|
if (do_cmd_pid != -1 && errs == 0) {
|
|
|
|
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 11:42:43 +08:00
|
|
|
exit(errs != 0);
|
|
|
|
}
|
|
|
|
|
2010-09-24 20:15:11 +08:00
|
|
|
/* Callback from atomicio6 to update progress meter and limit bandwidth */
|
|
|
|
static int
|
|
|
|
scpio(void *_cnt, size_t s)
|
2007-10-26 13:39:15 +08:00
|
|
|
{
|
2010-09-24 20:15:11 +08:00
|
|
|
off_t *cnt = (off_t *)_cnt;
|
|
|
|
|
|
|
|
*cnt += s;
|
|
|
|
if (limit_kbps > 0)
|
|
|
|
bandwidth_limit(&bwlimit, s);
|
|
|
|
return 0;
|
2007-10-26 13:39:15 +08:00
|
|
|
}
|
|
|
|
|
2013-07-18 14:09:25 +08: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-19 01:28:54 +08:00
|
|
|
static int
|
|
|
|
parse_scp_uri(const char *uri, char **userp, char **hostp, int *portp,
|
|
|
|
char **pathp)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = parse_uri("scp", uri, userp, hostp, portp, pathp);
|
|
|
|
if (r == 0 && *pathp == NULL)
|
|
|
|
*pathp = xstrdup(".");
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
1999-10-27 11:42:43 +08:00
|
|
|
void
|
2017-10-22 07:06:24 +08:00
|
|
|
toremote(int argc, char **argv)
|
1999-10-27 11:42:43 +08:00
|
|
|
{
|
2017-10-22 07:06:24 +08:00
|
|
|
char *suser = NULL, *host = NULL, *src = NULL;
|
|
|
|
char *bp, *tuser, *thost, *targ;
|
|
|
|
int sport = -1, tport = -1;
|
2006-01-31 18:49:27 +08:00
|
|
|
arglist alist;
|
2017-10-22 07:06:24 +08:00
|
|
|
int i, r;
|
2010-12-01 09:03:19 +08:00
|
|
|
u_int j;
|
2006-01-31 18:49:27 +08:00
|
|
|
|
|
|
|
memset(&alist, '\0', sizeof(alist));
|
|
|
|
alist.list = NULL;
|
1999-10-27 11:42:43 +08:00
|
|
|
|
2017-10-22 07:06:24 +08:00
|
|
|
/* Parse target */
|
2017-12-19 01:28:54 +08: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-22 07:06:24 +08:00
|
|
|
if (r != 0) {
|
|
|
|
if (parse_user_host_path(argv[argc - 1], &tuser, &thost,
|
2017-12-19 01:28:54 +08:00
|
|
|
&targ) == -1) {
|
|
|
|
fmprintf(stderr, "%s: invalid target\n", argv[argc - 1]);
|
|
|
|
++errs;
|
2017-10-22 07:06:24 +08:00
|
|
|
goto out;
|
2017-12-19 01:28:54 +08:00
|
|
|
}
|
2006-01-31 18:49:27 +08:00
|
|
|
}
|
2017-12-19 01:28:54 +08:00
|
|
|
if (tuser != NULL && !okname(tuser)) {
|
|
|
|
++errs;
|
2017-10-22 07:06:24 +08:00
|
|
|
goto out;
|
2017-12-19 01:28:54 +08:00
|
|
|
}
|
2006-01-31 18:49:27 +08:00
|
|
|
|
2017-10-22 07:06:24 +08:00
|
|
|
/* Parse source files */
|
1999-10-27 11:42:43 +08:00
|
|
|
for (i = 0; i < argc - 1; i++) {
|
2017-10-22 07:06:24 +08:00
|
|
|
free(suser);
|
|
|
|
free(host);
|
|
|
|
free(src);
|
2017-12-19 01:28:54 +08: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-22 07:06:24 +08:00
|
|
|
parse_user_host_path(argv[i], &suser, &host, &src);
|
2017-12-19 01:28:54 +08:00
|
|
|
}
|
2017-10-22 07:06:24 +08:00
|
|
|
if (suser != NULL && !okname(suser)) {
|
|
|
|
++errs;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (host && throughlocal) { /* extended remote to remote */
|
2011-09-22 19:38:00 +08:00
|
|
|
xasprintf(&bp, "%s -f %s%s", cmd,
|
|
|
|
*src == '-' ? "-- " : "", src);
|
2017-10-22 07:06:24 +08:00
|
|
|
if (do_cmd(host, suser, sport, bp, &remin, &remout) < 0)
|
2011-01-06 19:40:30 +08:00
|
|
|
exit(1);
|
2013-06-02 05:31:17 +08:00
|
|
|
free(bp);
|
2011-09-22 19:38:00 +08:00
|
|
|
xasprintf(&bp, "%s -t %s%s", cmd,
|
|
|
|
*targ == '-' ? "-- " : "", targ);
|
2017-10-22 07:06:24 +08:00
|
|
|
if (do_cmd2(thost, tuser, tport, bp, remin, remout) < 0)
|
2011-01-06 19:40:30 +08:00
|
|
|
exit(1);
|
2013-06-02 05:31:17 +08:00
|
|
|
free(bp);
|
2011-01-06 19:40:30 +08:00
|
|
|
(void) close(remin);
|
|
|
|
(void) close(remout);
|
|
|
|
remin = remout = -1;
|
2017-10-22 07:06:24 +08:00
|
|
|
} else if (host) { /* standard remote to remote */
|
|
|
|
if (tport != -1 && tport != SSH_DEFAULT_PORT) {
|
|
|
|
/* This would require the remote support URIs */
|
|
|
|
fatal("target port not supported with two "
|
|
|
|
"remote hosts without the -3 option");
|
|
|
|
}
|
|
|
|
|
2006-01-31 18:49:27 +08:00
|
|
|
freeargs(&alist);
|
|
|
|
addargs(&alist, "%s", ssh_program);
|
|
|
|
addargs(&alist, "-x");
|
2010-12-01 09:03:19 +08:00
|
|
|
addargs(&alist, "-oClearAllForwardings=yes");
|
2006-01-31 18:49:27 +08:00
|
|
|
addargs(&alist, "-n");
|
2010-12-01 09:03:19 +08:00
|
|
|
for (j = 0; j < remote_remote_args.num; j++) {
|
|
|
|
addargs(&alist, "%s",
|
|
|
|
remote_remote_args.list[j]);
|
|
|
|
}
|
2017-10-22 07:06:24 +08:00
|
|
|
|
|
|
|
if (sport != -1) {
|
|
|
|
addargs(&alist, "-p");
|
|
|
|
addargs(&alist, "%d", sport);
|
|
|
|
}
|
|
|
|
if (suser) {
|
2006-01-31 18:49:27 +08:00
|
|
|
addargs(&alist, "-l");
|
|
|
|
addargs(&alist, "%s", suser);
|
2000-01-14 12:45:46 +08:00
|
|
|
}
|
2010-01-08 15:53:43 +08:00
|
|
|
addargs(&alist, "--");
|
2006-01-31 18:49:27 +08: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 19:14:55 +08:00
|
|
|
errs = 1;
|
1999-11-24 21:26:21 +08:00
|
|
|
} else { /* local to remote */
|
1999-10-27 11:42:43 +08:00
|
|
|
if (remin == -1) {
|
2011-09-22 19:38:00 +08:00
|
|
|
xasprintf(&bp, "%s -t %s%s", cmd,
|
|
|
|
*targ == '-' ? "-- " : "", targ);
|
2017-10-22 07:06:24 +08:00
|
|
|
if (do_cmd(thost, tuser, tport, bp, &remin,
|
2006-04-23 10:04:27 +08:00
|
|
|
&remout) < 0)
|
1999-11-24 21:26:21 +08:00
|
|
|
exit(1);
|
1999-10-27 11:42:43 +08:00
|
|
|
if (response() < 0)
|
|
|
|
exit(1);
|
2013-06-02 05:31:17 +08:00
|
|
|
free(bp);
|
1999-10-27 11:42:43 +08:00
|
|
|
}
|
1999-11-24 21:26:21 +08:00
|
|
|
source(1, argv + i);
|
1999-10-27 11:42:43 +08:00
|
|
|
}
|
|
|
|
}
|
2017-10-22 07:06:24 +08:00
|
|
|
out:
|
|
|
|
free(tuser);
|
|
|
|
free(thost);
|
|
|
|
free(targ);
|
|
|
|
free(suser);
|
|
|
|
free(host);
|
|
|
|
free(src);
|
1999-10-27 11:42:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-06-04 20:51:08 +08:00
|
|
|
tolocal(int argc, char **argv)
|
1999-10-27 11:42:43 +08:00
|
|
|
{
|
2017-10-22 07:06:24 +08:00
|
|
|
char *bp, *host = NULL, *src = NULL, *suser = NULL;
|
2006-01-31 18:49:27 +08:00
|
|
|
arglist alist;
|
2017-10-22 07:06:24 +08:00
|
|
|
int i, r, sport = -1;
|
2006-01-31 18:49:27 +08:00
|
|
|
|
|
|
|
memset(&alist, '\0', sizeof(alist));
|
|
|
|
alist.list = NULL;
|
1999-10-27 11:42:43 +08:00
|
|
|
|
|
|
|
for (i = 0; i < argc - 1; i++) {
|
2017-10-22 07:06:24 +08:00
|
|
|
free(suser);
|
|
|
|
free(host);
|
|
|
|
free(src);
|
2017-12-19 01:28:54 +08:00
|
|
|
r = parse_scp_uri(argv[i], &suser, &host, &sport, &src);
|
2017-10-22 07:06:24 +08:00
|
|
|
if (r == -1) {
|
2017-12-19 01:28:54 +08:00
|
|
|
fmprintf(stderr, "%s: invalid uri\n", argv[i]);
|
2017-10-22 07:06:24 +08: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 18:49:27 +08:00
|
|
|
freeargs(&alist);
|
|
|
|
addargs(&alist, "%s", _PATH_CP);
|
|
|
|
if (iamrecursive)
|
|
|
|
addargs(&alist, "-r");
|
|
|
|
if (pflag)
|
|
|
|
addargs(&alist, "-p");
|
2010-01-08 15:53:43 +08:00
|
|
|
addargs(&alist, "--");
|
2006-01-31 18:49:27 +08:00
|
|
|
addargs(&alist, "%s", argv[i]);
|
|
|
|
addargs(&alist, "%s", argv[argc-1]);
|
|
|
|
if (do_local_cmd(&alist))
|
1999-10-27 11:42:43 +08:00
|
|
|
++errs;
|
|
|
|
continue;
|
|
|
|
}
|
2017-10-22 07:06:24 +08:00
|
|
|
/* Remote to local. */
|
2011-09-22 19:38:00 +08:00
|
|
|
xasprintf(&bp, "%s -f %s%s",
|
|
|
|
cmd, *src == '-' ? "-- " : "", src);
|
2017-10-22 07:06:24 +08:00
|
|
|
if (do_cmd(host, suser, sport, bp, &remin, &remout) < 0) {
|
2013-06-02 05:31:17 +08:00
|
|
|
free(bp);
|
1999-11-24 21:26:21 +08:00
|
|
|
++errs;
|
|
|
|
continue;
|
1999-10-27 11:42:43 +08:00
|
|
|
}
|
2013-06-02 05:31:17 +08:00
|
|
|
free(bp);
|
1999-10-27 11:42:43 +08:00
|
|
|
sink(1, argv + argc - 1);
|
1999-11-24 21:26:21 +08:00
|
|
|
(void) close(remin);
|
1999-10-27 11:42:43 +08:00
|
|
|
remin = remout = -1;
|
|
|
|
}
|
2017-10-22 07:06:24 +08:00
|
|
|
free(suser);
|
|
|
|
free(host);
|
|
|
|
free(src);
|
1999-10-27 11:42:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-06-04 20:51:08 +08:00
|
|
|
source(int argc, char **argv)
|
1999-10-27 11:42:43 +08:00
|
|
|
{
|
|
|
|
struct stat stb;
|
|
|
|
static BUF buffer;
|
|
|
|
BUF *bp;
|
2008-06-14 07:02:25 +08:00
|
|
|
off_t i, statbytes;
|
2014-07-02 13:29:01 +08:00
|
|
|
size_t amt, nr;
|
2005-08-02 15:07:07 +08:00
|
|
|
int fd = -1, haderr, indx;
|
2015-01-16 14:40:12 +08:00
|
|
|
char *last, *name, buf[2048], encname[PATH_MAX];
|
2001-04-07 09:14:38 +08:00
|
|
|
int len;
|
1999-10-27 11:42:43 +08:00
|
|
|
|
|
|
|
for (indx = 0; indx < argc; ++indx) {
|
1999-11-24 21:26:21 +08:00
|
|
|
name = argv[indx];
|
1999-10-27 11:42:43 +08:00
|
|
|
statbytes = 0;
|
2001-04-07 09:14:38 +08:00
|
|
|
len = strlen(name);
|
|
|
|
while (len > 1 && name[len-1] == '/')
|
|
|
|
name[--len] = '\0';
|
2007-06-12 21:41:06 +08:00
|
|
|
if ((fd = open(name, O_RDONLY|O_NONBLOCK, 0)) < 0)
|
1999-10-27 11:42:43 +08:00
|
|
|
goto syserr;
|
2007-06-12 22:02:07 +08:00
|
|
|
if (strchr(name, '\n') != NULL) {
|
|
|
|
strnvis(encname, name, sizeof(encname), VIS_NL);
|
|
|
|
name = encname;
|
|
|
|
}
|
1999-10-27 11:42:43 +08:00
|
|
|
if (fstat(fd, &stb) < 0) {
|
|
|
|
syserr: run_err("%s: %s", name, strerror(errno));
|
|
|
|
goto next;
|
|
|
|
}
|
2008-06-14 07:02:25 +08:00
|
|
|
if (stb.st_size < 0) {
|
|
|
|
run_err("%s: %s", name, "Negative file size");
|
|
|
|
goto next;
|
|
|
|
}
|
2007-06-12 21:41:06 +08:00
|
|
|
unset_nonblock(fd);
|
1999-10-27 11:42:43 +08: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 14:09:25 +08:00
|
|
|
if (do_times(remout, verbose_mode, &stb) < 0)
|
1999-10-27 11:42:43 +08:00
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
|
2001-03-17 08:10:20 +08:00
|
|
|
snprintf(buf, sizeof buf, "C%04o %lld %s\n",
|
2000-12-22 09:43:59 +08:00
|
|
|
(u_int) (stb.st_mode & FILEMODEMASK),
|
2005-11-25 11:44:55 +08:00
|
|
|
(long long)stb.st_size, last);
|
2016-05-26 07:48:45 +08:00
|
|
|
if (verbose_mode)
|
|
|
|
fmprintf(stderr, "Sending file modes: %s", buf);
|
2003-07-03 11:46:56 +08:00
|
|
|
(void) atomicio(vwrite, remout, buf, strlen(buf));
|
1999-10-27 11:42:43 +08:00
|
|
|
if (response() < 0)
|
|
|
|
goto next;
|
2007-10-26 13:39:15 +08:00
|
|
|
if ((bp = allocbuf(&buffer, fd, COPY_BUFLEN)) == NULL) {
|
2005-11-22 16:38:06 +08:00
|
|
|
next: if (fd != -1) {
|
|
|
|
(void) close(fd);
|
|
|
|
fd = -1;
|
|
|
|
}
|
1999-10-27 11:42:43 +08:00
|
|
|
continue;
|
|
|
|
}
|
2003-01-10 18:43:24 +08:00
|
|
|
if (showprogress)
|
|
|
|
start_progress_meter(curfile, stb.st_size, &statbytes);
|
2007-10-26 13:39:15 +08:00
|
|
|
set_nonblock(remout);
|
1999-10-27 11:42:43 +08:00
|
|
|
for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
|
|
|
|
amt = bp->cnt;
|
2008-06-14 07:02:25 +08:00
|
|
|
if (i + (off_t)amt > stb.st_size)
|
1999-10-27 11:42:43 +08:00
|
|
|
amt = stb.st_size - i;
|
|
|
|
if (!haderr) {
|
2014-07-02 13:29:01 +08:00
|
|
|
if ((nr = atomicio(read, fd,
|
|
|
|
bp->buf, amt)) != amt) {
|
2005-05-26 10:23:44 +08:00
|
|
|
haderr = errno;
|
2014-07-02 13:29:01 +08:00
|
|
|
memset(bp->buf + nr, 0, amt - nr);
|
|
|
|
}
|
1999-10-27 11:42:43 +08:00
|
|
|
}
|
2007-10-26 13:39:15 +08:00
|
|
|
/* Keep writing after error to retain sync */
|
|
|
|
if (haderr) {
|
|
|
|
(void)atomicio(vwrite, remout, bp->buf, amt);
|
2014-07-02 13:29:01 +08:00
|
|
|
memset(bp->buf, 0, amt);
|
2007-10-26 13:39:15 +08:00
|
|
|
continue;
|
1999-10-27 11:42:43 +08:00
|
|
|
}
|
2010-09-24 20:15:11 +08:00
|
|
|
if (atomicio6(vwrite, remout, bp->buf, amt, scpio,
|
2007-10-26 13:39:15 +08:00
|
|
|
&statbytes) != amt)
|
|
|
|
haderr = errno;
|
1999-10-27 11:42:43 +08:00
|
|
|
}
|
2007-10-26 13:39:15 +08:00
|
|
|
unset_nonblock(remout);
|
1999-10-27 11:42:43 +08:00
|
|
|
|
2005-11-22 16:38:06 +08:00
|
|
|
if (fd != -1) {
|
|
|
|
if (close(fd) < 0 && !haderr)
|
|
|
|
haderr = errno;
|
|
|
|
fd = -1;
|
|
|
|
}
|
1999-10-27 11:42:43 +08:00
|
|
|
if (!haderr)
|
2003-07-03 11:46:56 +08:00
|
|
|
(void) atomicio(vwrite, remout, "", 1);
|
1999-10-27 11:42:43 +08:00
|
|
|
else
|
|
|
|
run_err("%s: %s", name, strerror(haderr));
|
1999-11-24 21:26:21 +08:00
|
|
|
(void) response();
|
2016-03-03 06:43:52 +08:00
|
|
|
if (showprogress)
|
|
|
|
stop_progress_meter();
|
1999-10-27 11:42:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-06-04 20:51:08 +08:00
|
|
|
rsource(char *name, struct stat *statp)
|
1999-10-27 11:42:43 +08:00
|
|
|
{
|
|
|
|
DIR *dirp;
|
|
|
|
struct dirent *dp;
|
2015-01-16 14:40:12 +08:00
|
|
|
char *last, *vect[1], path[PATH_MAX];
|
1999-10-27 11:42:43 +08:00
|
|
|
|
|
|
|
if (!(dirp = opendir(name))) {
|
|
|
|
run_err("%s: %s", name, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
last = strrchr(name, '/');
|
2015-10-17 01:07:24 +08:00
|
|
|
if (last == NULL)
|
1999-10-27 11:42:43 +08:00
|
|
|
last = name;
|
|
|
|
else
|
|
|
|
last++;
|
|
|
|
if (pflag) {
|
2013-07-18 14:09:25 +08:00
|
|
|
if (do_times(remout, verbose_mode, statp) < 0) {
|
1999-10-27 11:42:43 +08:00
|
|
|
closedir(dirp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2001-02-09 09:23:39 +08:00
|
|
|
(void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
|
2000-12-22 09:43:59 +08:00
|
|
|
(u_int) (statp->st_mode & FILEMODEMASK), 0, last);
|
1999-11-24 21:26:21 +08:00
|
|
|
if (verbose_mode)
|
2016-05-26 07:48:45 +08:00
|
|
|
fmprintf(stderr, "Entering directory: %s", path);
|
2003-07-03 11:46:56 +08:00
|
|
|
(void) atomicio(vwrite, remout, path, strlen(path));
|
1999-10-27 11:42:43 +08:00
|
|
|
if (response() < 0) {
|
|
|
|
closedir(dirp);
|
|
|
|
return;
|
|
|
|
}
|
2001-03-29 08:39:55 +08:00
|
|
|
while ((dp = readdir(dirp)) != NULL) {
|
1999-10-27 11:42:43 +08: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 09:23:39 +08:00
|
|
|
(void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
|
1999-10-27 11:42:43 +08:00
|
|
|
vect[0] = path;
|
|
|
|
source(1, vect);
|
|
|
|
}
|
1999-11-24 21:26:21 +08:00
|
|
|
(void) closedir(dirp);
|
2003-07-03 11:46:56 +08:00
|
|
|
(void) atomicio(vwrite, remout, "E\n", 2);
|
1999-11-24 21:26:21 +08:00
|
|
|
(void) response();
|
1999-10-27 11:42:43 +08:00
|
|
|
}
|
|
|
|
|
2017-04-28 11:21:12 +08: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 11:42:43 +08:00
|
|
|
void
|
2003-06-04 20:51:08 +08:00
|
|
|
sink(int argc, char **argv)
|
1999-10-27 11:42:43 +08:00
|
|
|
{
|
|
|
|
static BUF buffer;
|
|
|
|
struct stat stb;
|
1999-11-24 21:26:21 +08:00
|
|
|
enum {
|
|
|
|
YES, NO, DISPLAYED
|
|
|
|
} wrerr;
|
1999-10-27 11:42:43 +08:00
|
|
|
BUF *bp;
|
2005-05-26 10:23:44 +08:00
|
|
|
off_t i;
|
2005-06-17 10:59:34 +08:00
|
|
|
size_t j, count;
|
2006-04-23 10:04:27 +08:00
|
|
|
int amt, exists, first, ofd;
|
|
|
|
mode_t mode, omode, mask;
|
2003-01-10 18:43:24 +08:00
|
|
|
off_t size, statbytes;
|
2013-07-18 14:09:25 +08:00
|
|
|
unsigned long long ull;
|
2000-08-29 08:33:50 +08:00
|
|
|
int setimes, targisdir, wrerrno = 0;
|
2016-05-26 07:48:45 +08:00
|
|
|
char ch, *cp, *np, *targ, *why, *vect[1], buf[2048], visbuf[2048];
|
2000-09-23 14:15:56 +08:00
|
|
|
struct timeval tv[2];
|
1999-10-27 11:42:43 +08:00
|
|
|
|
2001-04-15 07:14:22 +08:00
|
|
|
#define atime tv[0]
|
|
|
|
#define mtime tv[1]
|
2004-11-05 17:10:02 +08:00
|
|
|
#define SCREWUP(str) { why = str; goto screwup; }
|
1999-10-27 11:42:43 +08:00
|
|
|
|
2017-04-28 11:21:12 +08:00
|
|
|
if (TYPE_OVERFLOW(time_t, 0) || TYPE_OVERFLOW(off_t, 0))
|
|
|
|
SCREWUP("Unexpected off_t/time_t size");
|
|
|
|
|
1999-10-27 11:42:43 +08:00
|
|
|
setimes = targisdir = 0;
|
|
|
|
mask = umask(0);
|
|
|
|
if (!pflag)
|
1999-11-24 21:26:21 +08:00
|
|
|
(void) umask(mask);
|
1999-10-27 11:42:43 +08:00
|
|
|
if (argc != 1) {
|
|
|
|
run_err("ambiguous target");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
targ = *argv;
|
|
|
|
if (targetshouldbedirectory)
|
|
|
|
verifydir(targ);
|
1999-11-24 21:26:21 +08:00
|
|
|
|
2003-07-03 11:46:56 +08:00
|
|
|
(void) atomicio(vwrite, remout, "", 1);
|
1999-10-27 11:42:43 +08:00
|
|
|
if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
|
|
|
|
targisdir = 1;
|
|
|
|
for (first = 1;; first = 0) {
|
|
|
|
cp = buf;
|
2005-05-26 10:23:44 +08:00
|
|
|
if (atomicio(read, remin, cp, 1) != 1)
|
1999-10-27 11:42:43 +08:00
|
|
|
return;
|
|
|
|
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 10:03:14 +08:00
|
|
|
if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
|
1999-10-27 11:42:43 +08:00
|
|
|
SCREWUP("lost connection");
|
|
|
|
*cp++ = ch;
|
|
|
|
} while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
|
|
|
|
*cp = 0;
|
2004-04-19 21:50:16 +08:00
|
|
|
if (verbose_mode)
|
2016-05-26 07:48:45 +08:00
|
|
|
fmprintf(stderr, "Sink: %s", buf);
|
1999-10-27 11:42:43 +08:00
|
|
|
|
|
|
|
if (buf[0] == '\01' || buf[0] == '\02') {
|
2016-05-26 07:48:45 +08:00
|
|
|
if (iamremote == 0) {
|
|
|
|
(void) snmprintf(visbuf, sizeof(visbuf),
|
|
|
|
NULL, "%s", buf + 1);
|
2003-07-03 11:46:56 +08:00
|
|
|
(void) atomicio(vwrite, STDERR_FILENO,
|
2016-05-26 07:48:45 +08:00
|
|
|
visbuf, strlen(visbuf));
|
|
|
|
}
|
1999-10-27 11:42:43 +08:00
|
|
|
if (buf[0] == '\02')
|
|
|
|
exit(1);
|
|
|
|
++errs;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (buf[0] == 'E') {
|
2003-07-03 11:46:56 +08:00
|
|
|
(void) atomicio(vwrite, remout, "", 1);
|
1999-10-27 11:42:43 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ch == '\n')
|
|
|
|
*--cp = 0;
|
|
|
|
|
|
|
|
cp = buf;
|
|
|
|
if (*cp == 'T') {
|
|
|
|
setimes++;
|
|
|
|
cp++;
|
2013-07-18 14:09:25 +08:00
|
|
|
if (!isdigit((unsigned char)*cp))
|
|
|
|
SCREWUP("mtime.sec not present");
|
|
|
|
ull = strtoull(cp, &cp, 10);
|
2001-04-15 07:14:22 +08:00
|
|
|
if (!cp || *cp++ != ' ')
|
1999-10-27 11:42:43 +08:00
|
|
|
SCREWUP("mtime.sec not delimited");
|
2017-04-28 11:21:12 +08:00
|
|
|
if (TYPE_OVERFLOW(time_t, ull))
|
2013-07-18 14:09:25 +08:00
|
|
|
setimes = 0; /* out of range */
|
|
|
|
mtime.tv_sec = ull;
|
2001-04-15 07:14:22 +08:00
|
|
|
mtime.tv_usec = strtol(cp, &cp, 10);
|
2013-07-18 14:09:25 +08:00
|
|
|
if (!cp || *cp++ != ' ' || mtime.tv_usec < 0 ||
|
|
|
|
mtime.tv_usec > 999999)
|
1999-10-27 11:42:43 +08:00
|
|
|
SCREWUP("mtime.usec not delimited");
|
2013-07-18 14:09:25 +08:00
|
|
|
if (!isdigit((unsigned char)*cp))
|
|
|
|
SCREWUP("atime.sec not present");
|
|
|
|
ull = strtoull(cp, &cp, 10);
|
2001-04-15 07:14:22 +08:00
|
|
|
if (!cp || *cp++ != ' ')
|
1999-10-27 11:42:43 +08:00
|
|
|
SCREWUP("atime.sec not delimited");
|
2017-04-28 11:21:12 +08:00
|
|
|
if (TYPE_OVERFLOW(time_t, ull))
|
2013-07-18 14:09:25 +08:00
|
|
|
setimes = 0; /* out of range */
|
|
|
|
atime.tv_sec = ull;
|
2001-04-15 07:14:22 +08:00
|
|
|
atime.tv_usec = strtol(cp, &cp, 10);
|
2013-07-18 14:09:25 +08:00
|
|
|
if (!cp || *cp++ != '\0' || atime.tv_usec < 0 ||
|
|
|
|
atime.tv_usec > 999999)
|
1999-10-27 11:42:43 +08:00
|
|
|
SCREWUP("atime.usec not delimited");
|
2003-07-03 11:46:56 +08:00
|
|
|
(void) atomicio(vwrite, remout, "", 1);
|
1999-10-27 11:42:43 +08: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 12:31:48 +08:00
|
|
|
if (!pflag)
|
|
|
|
mode &= ~mask;
|
1999-10-27 11:42:43 +08:00
|
|
|
if (*cp++ != ' ')
|
|
|
|
SCREWUP("mode not delimited");
|
|
|
|
|
2017-04-27 19:53:12 +08:00
|
|
|
if (!isdigit((unsigned char)*cp))
|
|
|
|
SCREWUP("size not present");
|
|
|
|
ull = strtoull(cp, &cp, 10);
|
|
|
|
if (!cp || *cp++ != ' ')
|
1999-10-27 11:42:43 +08:00
|
|
|
SCREWUP("size not delimited");
|
2017-04-28 11:21:12 +08:00
|
|
|
if (TYPE_OVERFLOW(off_t, ull))
|
2017-04-27 19:53:12 +08:00
|
|
|
SCREWUP("size out of range");
|
|
|
|
size = (off_t)ull;
|
|
|
|
|
2018-11-16 11:03:10 +08:00
|
|
|
if (*cp == '\0' || strchr(cp, '/') != NULL ||
|
|
|
|
strcmp(cp, ".") == 0 || strcmp(cp, "..") == 0) {
|
2004-04-19 21:50:16 +08:00
|
|
|
run_err("error: unexpected filename: %s", cp);
|
|
|
|
exit(1);
|
|
|
|
}
|
1999-10-27 11:42:43 +08:00
|
|
|
if (targisdir) {
|
|
|
|
static char *namebuf;
|
2005-06-17 10:59:34 +08:00
|
|
|
static size_t cursize;
|
1999-10-27 11:42:43 +08:00
|
|
|
size_t need;
|
|
|
|
|
|
|
|
need = strlen(targ) + strlen(cp) + 250;
|
2001-02-09 09:23:39 +08:00
|
|
|
if (need > cursize) {
|
2013-06-02 05:31:17 +08:00
|
|
|
free(namebuf);
|
1999-11-24 21:26:21 +08:00
|
|
|
namebuf = xmalloc(need);
|
2001-02-09 09:23:39 +08:00
|
|
|
cursize = need;
|
|
|
|
}
|
|
|
|
(void) snprintf(namebuf, need, "%s%s%s", targ,
|
2002-04-07 02:29:59 +08:00
|
|
|
strcmp(targ, "/") ? "/" : "", cp);
|
1999-10-27 11:42:43 +08:00
|
|
|
np = namebuf;
|
|
|
|
} else
|
|
|
|
np = targ;
|
|
|
|
curfile = cp;
|
|
|
|
exists = stat(np, &stb) == 0;
|
|
|
|
if (buf[0] == 'D') {
|
|
|
|
int mod_flag = pflag;
|
2004-04-19 21:50:16 +08:00
|
|
|
if (!iamrecursive)
|
|
|
|
SCREWUP("received directory without -r");
|
1999-10-27 11:42:43 +08:00
|
|
|
if (exists) {
|
|
|
|
if (!S_ISDIR(stb.st_mode)) {
|
|
|
|
errno = ENOTDIR;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
if (pflag)
|
1999-11-24 21:26:21 +08:00
|
|
|
(void) chmod(np, mode);
|
1999-10-27 11:42:43 +08:00
|
|
|
} else {
|
1999-11-24 21:26:21 +08:00
|
|
|
/* Handle copying from a read-only
|
|
|
|
directory */
|
1999-10-27 11:42:43 +08:00
|
|
|
mod_flag = 1;
|
|
|
|
if (mkdir(np, mode | S_IRWXU) < 0)
|
|
|
|
goto bad;
|
|
|
|
}
|
2001-02-11 05:50:00 +08:00
|
|
|
vect[0] = xstrdup(np);
|
1999-10-27 11:42:43 +08:00
|
|
|
sink(1, vect);
|
|
|
|
if (setimes) {
|
|
|
|
setimes = 0;
|
2001-02-19 18:51:08 +08:00
|
|
|
if (utimes(vect[0], tv) < 0)
|
1999-11-24 21:26:21 +08:00
|
|
|
run_err("%s: set times: %s",
|
2001-02-19 18:51:08 +08:00
|
|
|
vect[0], strerror(errno));
|
1999-10-27 11:42:43 +08:00
|
|
|
}
|
|
|
|
if (mod_flag)
|
2001-02-19 18:51:08 +08:00
|
|
|
(void) chmod(vect[0], mode);
|
2013-06-02 05:31:17 +08:00
|
|
|
free(vect[0]);
|
1999-10-27 11:42:43 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
omode = mode;
|
2013-06-02 05:52:21 +08:00
|
|
|
mode |= S_IWUSR;
|
2001-06-06 03:31:41 +08:00
|
|
|
if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
|
1999-10-27 11:42:43 +08:00
|
|
|
bad: run_err("%s: %s", np, strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
2003-07-03 11:46:56 +08:00
|
|
|
(void) atomicio(vwrite, remout, "", 1);
|
2007-10-26 13:39:15 +08:00
|
|
|
if ((bp = allocbuf(&buffer, ofd, COPY_BUFLEN)) == NULL) {
|
1999-11-24 21:26:21 +08:00
|
|
|
(void) close(ofd);
|
1999-10-27 11:42:43 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cp = bp->buf;
|
|
|
|
wrerr = NO;
|
|
|
|
|
|
|
|
statbytes = 0;
|
2003-01-10 18:43:24 +08:00
|
|
|
if (showprogress)
|
|
|
|
start_progress_meter(curfile, size, &statbytes);
|
2007-10-26 13:39:15 +08:00
|
|
|
set_nonblock(remin);
|
|
|
|
for (count = i = 0; i < size; i += bp->cnt) {
|
|
|
|
amt = bp->cnt;
|
1999-10-27 11:42:43 +08:00
|
|
|
if (i + amt > size)
|
|
|
|
amt = size - i;
|
|
|
|
count += amt;
|
|
|
|
do {
|
2010-09-24 20:15:11 +08:00
|
|
|
j = atomicio6(read, remin, cp, amt,
|
|
|
|
scpio, &statbytes);
|
2005-05-26 10:23:44 +08:00
|
|
|
if (j == 0) {
|
2007-10-26 13:39:15 +08:00
|
|
|
run_err("%s", j != EPIPE ?
|
|
|
|
strerror(errno) :
|
2001-03-29 08:39:55 +08:00
|
|
|
"dropped connection");
|
1999-10-27 11:42:43 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
amt -= j;
|
|
|
|
cp += j;
|
|
|
|
} while (amt > 0);
|
2003-11-21 20:56:47 +08:00
|
|
|
|
1999-10-27 11:42:43 +08:00
|
|
|
if (count == bp->cnt) {
|
|
|
|
/* Keep reading so we stay sync'd up. */
|
|
|
|
if (wrerr == NO) {
|
2005-05-26 10:23:44 +08:00
|
|
|
if (atomicio(vwrite, ofd, bp->buf,
|
|
|
|
count) != count) {
|
1999-10-27 11:42:43 +08:00
|
|
|
wrerr = YES;
|
2005-05-26 10:23:44 +08:00
|
|
|
wrerrno = errno;
|
1999-10-27 11:42:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
count = 0;
|
|
|
|
cp = bp->buf;
|
|
|
|
}
|
|
|
|
}
|
2007-10-26 13:39:15 +08:00
|
|
|
unset_nonblock(remin);
|
1999-10-27 11:42:43 +08:00
|
|
|
if (count != 0 && wrerr == NO &&
|
2005-05-26 10:23:44 +08:00
|
|
|
atomicio(vwrite, ofd, bp->buf, count) != count) {
|
1999-10-27 11:42:43 +08:00
|
|
|
wrerr = YES;
|
2005-05-26 10:23:44 +08:00
|
|
|
wrerrno = errno;
|
1999-10-27 11:42:43 +08:00
|
|
|
}
|
2007-06-25 16:32:33 +08:00
|
|
|
if (wrerr == NO && (!exists || S_ISREG(stb.st_mode)) &&
|
|
|
|
ftruncate(ofd, size) != 0) {
|
1999-10-27 11:42:43 +08:00
|
|
|
run_err("%s: truncate: %s", np, strerror(errno));
|
|
|
|
wrerr = DISPLAYED;
|
|
|
|
}
|
|
|
|
if (pflag) {
|
|
|
|
if (exists || omode != mode)
|
2000-09-29 20:01:36 +08:00
|
|
|
#ifdef HAVE_FCHMOD
|
2004-07-08 21:11:44 +08:00
|
|
|
if (fchmod(ofd, omode)) {
|
2000-09-29 20:01:36 +08:00
|
|
|
#else /* HAVE_FCHMOD */
|
2004-07-08 21:11:44 +08:00
|
|
|
if (chmod(np, omode)) {
|
2000-09-29 20:01:36 +08:00
|
|
|
#endif /* HAVE_FCHMOD */
|
1999-10-27 11:42:43 +08:00
|
|
|
run_err("%s: set mode: %s",
|
2001-03-29 08:39:55 +08:00
|
|
|
np, strerror(errno));
|
2004-07-08 21:11:44 +08:00
|
|
|
wrerr = DISPLAYED;
|
|
|
|
}
|
1999-10-27 11:42:43 +08:00
|
|
|
} else {
|
|
|
|
if (!exists && omode != mode)
|
2000-09-29 20:01:36 +08:00
|
|
|
#ifdef HAVE_FCHMOD
|
2004-07-08 21:11:44 +08:00
|
|
|
if (fchmod(ofd, omode & ~mask)) {
|
2000-09-29 20:01:36 +08:00
|
|
|
#else /* HAVE_FCHMOD */
|
2004-07-08 21:11:44 +08:00
|
|
|
if (chmod(np, omode & ~mask)) {
|
2000-09-29 20:01:36 +08:00
|
|
|
#endif /* HAVE_FCHMOD */
|
1999-10-27 11:42:43 +08:00
|
|
|
run_err("%s: set mode: %s",
|
2001-03-29 08:39:55 +08:00
|
|
|
np, strerror(errno));
|
2004-07-08 21:11:44 +08:00
|
|
|
wrerr = DISPLAYED;
|
|
|
|
}
|
1999-10-27 11:42:43 +08:00
|
|
|
}
|
2000-07-15 12:14:16 +08:00
|
|
|
if (close(ofd) == -1) {
|
|
|
|
wrerr = YES;
|
|
|
|
wrerrno = errno;
|
|
|
|
}
|
1999-11-24 21:26:21 +08:00
|
|
|
(void) response();
|
2016-03-03 06:43:52 +08:00
|
|
|
if (showprogress)
|
|
|
|
stop_progress_meter();
|
1999-10-27 11:42:43 +08:00
|
|
|
if (setimes && wrerr == NO) {
|
|
|
|
setimes = 0;
|
2000-09-23 14:15:56 +08:00
|
|
|
if (utimes(np, tv) < 0) {
|
1999-10-27 11:42:43 +08:00
|
|
|
run_err("%s: set times: %s",
|
2001-03-29 08:39:55 +08:00
|
|
|
np, strerror(errno));
|
1999-10-27 11:42:43 +08:00
|
|
|
wrerr = DISPLAYED;
|
|
|
|
}
|
|
|
|
}
|
1999-11-24 21:26:21 +08:00
|
|
|
switch (wrerr) {
|
1999-10-27 11:42:43 +08:00
|
|
|
case YES:
|
|
|
|
run_err("%s: %s", np, strerror(wrerrno));
|
|
|
|
break;
|
|
|
|
case NO:
|
2003-07-03 11:46:56 +08:00
|
|
|
(void) atomicio(vwrite, remout, "", 1);
|
1999-10-27 11:42:43 +08:00
|
|
|
break;
|
|
|
|
case DISPLAYED:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
screwup:
|
|
|
|
run_err("protocol error: %s", why);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-12-07 01:55:26 +08:00
|
|
|
response(void)
|
1999-10-27 11:42:43 +08:00
|
|
|
{
|
2016-05-26 07:48:45 +08:00
|
|
|
char ch, *cp, resp, rbuf[2048], visbuf[2048];
|
1999-10-27 11:42:43 +08: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 10:03:14 +08:00
|
|
|
if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
|
1999-10-27 11:42:43 +08:00
|
|
|
lostconn(0);
|
|
|
|
|
|
|
|
cp = rbuf;
|
1999-11-24 21:26:21 +08:00
|
|
|
switch (resp) {
|
|
|
|
case 0: /* ok */
|
1999-10-27 11:42:43 +08:00
|
|
|
return (0);
|
|
|
|
default:
|
|
|
|
*cp++ = resp;
|
|
|
|
/* FALLTHROUGH */
|
1999-11-24 21:26:21 +08:00
|
|
|
case 1: /* error, followed by error msg */
|
|
|
|
case 2: /* fatal error, "" */
|
1999-10-27 11:42:43 +08: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 10:03:14 +08:00
|
|
|
if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
|
1999-10-27 11:42:43 +08:00
|
|
|
lostconn(0);
|
|
|
|
*cp++ = ch;
|
|
|
|
} while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
|
|
|
|
|
2016-05-26 07:48:45 +08: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 11:42:43 +08:00
|
|
|
++errs;
|
|
|
|
if (resp == 1)
|
|
|
|
return (-1);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-12-07 01:55:26 +08:00
|
|
|
usage(void)
|
1999-10-27 11:42:43 +08:00
|
|
|
{
|
2001-09-18 13:38:44 +08:00
|
|
|
(void) fprintf(stderr,
|
2017-05-02 16:06:33 +08:00
|
|
|
"usage: scp [-346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
|
2017-10-22 07:06:24 +08:00
|
|
|
" [-l limit] [-o ssh_option] [-P port] [-S program] source ... target\n");
|
1999-10-27 11:42:43 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-11-24 21:26:21 +08:00
|
|
|
run_err(const char *fmt,...)
|
1999-10-27 11:42:43 +08:00
|
|
|
{
|
|
|
|
static FILE *fp;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
++errs;
|
2006-03-26 10:49:43 +08: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 21:26:21 +08:00
|
|
|
|
|
|
|
if (!iamremote) {
|
1999-12-31 06:16:40 +08:00
|
|
|
va_start(ap, fmt);
|
2016-05-26 07:48:45 +08:00
|
|
|
vfmprintf(stderr, fmt, ap);
|
1999-12-31 06:16:40 +08:00
|
|
|
va_end(ap);
|
1999-11-24 21:26:21 +08:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
1999-10-27 11:42:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-06-04 20:51:08 +08:00
|
|
|
verifydir(char *cp)
|
1999-10-27 11:42:43 +08: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 21:01:12 +08:00
|
|
|
killchild(0);
|
1999-10-27 11:42:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2003-06-04 20:51:08 +08:00
|
|
|
okname(char *cp0)
|
1999-10-27 11:42:43 +08:00
|
|
|
{
|
|
|
|
int c;
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
cp = cp0;
|
|
|
|
do {
|
2001-06-25 12:28:30 +08:00
|
|
|
c = (int)*cp;
|
1999-10-27 11:42:43 +08:00
|
|
|
if (c & 0200)
|
|
|
|
goto bad;
|
2013-11-21 10:56:49 +08:00
|
|
|
if (!isalpha(c) && !isdigit((unsigned char)c)) {
|
2003-02-24 08:52:58 +08:00
|
|
|
switch (c) {
|
|
|
|
case '\'':
|
|
|
|
case '"':
|
|
|
|
case '`':
|
|
|
|
case ' ':
|
|
|
|
case '#':
|
|
|
|
goto bad;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1999-10-27 11:42:43 +08:00
|
|
|
} while (*++cp);
|
|
|
|
return (1);
|
|
|
|
|
2016-05-26 07:48:45 +08:00
|
|
|
bad: fmprintf(stderr, "%s: invalid user name\n", cp0);
|
1999-10-27 11:42:43 +08:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
BUF *
|
2003-06-04 20:51:08 +08:00
|
|
|
allocbuf(BUF *bp, int fd, int blksize)
|
1999-10-27 11:42:43 +08:00
|
|
|
{
|
|
|
|
size_t size;
|
2001-10-22 08:53:58 +08:00
|
|
|
#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
|
1999-10-27 11:42:43 +08:00
|
|
|
struct stat stb;
|
|
|
|
|
|
|
|
if (fstat(fd, &stb) < 0) {
|
|
|
|
run_err("fstat: %s", strerror(errno));
|
|
|
|
return (0);
|
|
|
|
}
|
2016-09-12 09:22:38 +08:00
|
|
|
size = ROUNDUP(stb.st_blksize, blksize);
|
2002-12-23 10:22:09 +08:00
|
|
|
if (size == 0)
|
1999-11-24 21:26:21 +08:00
|
|
|
size = blksize;
|
2001-10-22 08:53:58 +08:00
|
|
|
#else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
|
2001-02-05 20:42:17 +08:00
|
|
|
size = blksize;
|
2001-10-22 08:53:58 +08:00
|
|
|
#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
|
1999-10-27 11:42:43 +08:00
|
|
|
if (bp->cnt >= size)
|
|
|
|
return (bp);
|
2017-05-31 17:15:42 +08:00
|
|
|
bp->buf = xrecallocarray(bp->buf, bp->cnt, size, 1);
|
1999-10-27 11:42:43 +08:00
|
|
|
bp->cnt = size;
|
|
|
|
return (bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-06-04 20:51:08 +08:00
|
|
|
lostconn(int signo)
|
1999-10-27 11:42:43 +08:00
|
|
|
{
|
|
|
|
if (!iamremote)
|
2013-05-16 18:32:29 +08:00
|
|
|
(void)write(STDERR_FILENO, "lost connection\n", 16);
|
2001-06-21 11:08:58 +08:00
|
|
|
if (signo)
|
|
|
|
_exit(1);
|
|
|
|
else
|
|
|
|
exit(1);
|
1999-10-27 11:42:43 +08:00
|
|
|
}
|