2013-11-21 02:56:49 +00:00
|
|
|
/* $OpenBSD: scp.c,v 1.179 2013/11/20 20:53:10 deraadt 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-08-05 01:02:17 +00:00
|
|
|
#include <sys/param.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>
|
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>
|
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"
|
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"
|
1999-10-27 03:42:43 +00:00
|
|
|
|
2000-11-17 03:47:20 +00:00
|
|
|
extern char *__progname;
|
|
|
|
|
2007-10-26 05:39:15 +00:00
|
|
|
#define COPY_BUFLEN 16384
|
|
|
|
|
2006-04-23 02:04:27 +00:00
|
|
|
int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout);
|
2011-01-06 11:40:30 +00:00
|
|
|
int do_cmd2(char *host, char *remuser, char *cmd, int fdin, int fdout);
|
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;
|
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.
|
|
|
|
*/
|
|
|
|
int throughlocal = 0;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
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
|
|
|
|
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 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++)
|
|
|
|
fprintf(stderr, " %s", a->list[i]);
|
|
|
|
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 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
|
2006-04-23 02:04:27 +00:00
|
|
|
do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout)
|
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)
|
2001-08-06 21:17:12 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"Executing: program %s host %s, user %s, command %s\n",
|
|
|
|
ssh_program, host,
|
|
|
|
remuser ? remuser : "(unspecified)", cmd);
|
1999-11-24 13:26:21 +00:00
|
|
|
|
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.
|
|
|
|
*/
|
2006-06-13 03:00:25 +00:00
|
|
|
if (pipe(reserved) < 0)
|
|
|
|
fatal("pipe: %s", strerror(errno));
|
1999-11-24 13:26:21 +00: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 03:37:33 +00:00
|
|
|
signal(SIGTSTP, suspchild);
|
|
|
|
signal(SIGTTIN, suspchild);
|
|
|
|
signal(SIGTTOU, suspchild);
|
|
|
|
|
2003-06-18 10:26:34 +00:00
|
|
|
/* Fork a child to execute the command on the remote host using ssh. */
|
2002-11-09 15:54:08 +00:00
|
|
|
do_cmd_pid = fork();
|
|
|
|
if (do_cmd_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]);
|
|
|
|
|
2006-01-31 10:49:27 +00:00
|
|
|
replacearg(&args, 0, "%s", ssh_program);
|
2010-01-08 07:53:43 +00:00
|
|
|
if (remuser != NULL) {
|
|
|
|
addargs(&args, "-l");
|
|
|
|
addargs(&args, "%s", remuser);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2000-10-14 05:23:11 +00:00
|
|
|
execvp(ssh_program, args.list);
|
2000-08-23 00:46:23 +00:00
|
|
|
perror(ssh_program);
|
1999-11-24 13:26:21 +00:00
|
|
|
exit(1);
|
2002-11-09 15:54:08 +00:00
|
|
|
} else if (do_cmd_pid == -1) {
|
|
|
|
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];
|
2003-06-04 12:51:24 +00:00
|
|
|
signal(SIGTERM, killchild);
|
|
|
|
signal(SIGINT, killchild);
|
|
|
|
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
|
|
|
/*
|
|
|
|
* This functions executes a command simlar to do_cmd(), but expects the
|
|
|
|
* 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
|
|
|
|
do_cmd2(char *host, char *remuser, char *cmd, int fdin, int fdout)
|
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
if (verbose_mode)
|
|
|
|
fprintf(stderr,
|
|
|
|
"Executing: 2nd program %s host %s, user %s, command %s\n",
|
|
|
|
ssh_program, host,
|
|
|
|
remuser ? remuser : "(unspecified)", cmd);
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
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 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 *);
|
|
|
|
void run_err(const char *,...);
|
|
|
|
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;
|
1999-10-27 03:42:43 +00: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 13:26:21 +00:00
|
|
|
int response(void);
|
|
|
|
void rsource(char *, struct stat *);
|
|
|
|
void sink(int, char *[]);
|
|
|
|
void source(int, char *[]);
|
|
|
|
void tolocal(int, char *[]);
|
|
|
|
void toremote(char *, int, char *[]);
|
|
|
|
void usage(void);
|
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;
|
2010-09-24 12:15:11 +00:00
|
|
|
char *targ, **newargv;
|
|
|
|
const char *errstr;
|
1999-10-27 03:42:43 +00:00
|
|
|
extern char *optarg;
|
|
|
|
extern int optind;
|
|
|
|
|
2005-10-03 08:11:24 +00:00
|
|
|
/* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
|
|
|
|
sanitise_stdfd();
|
|
|
|
|
2006-07-10 12:19:53 +00:00
|
|
|
/* Copy argv, because we modify it */
|
|
|
|
newargv = xcalloc(MAX(argc + 1, 1), sizeof(*newargv));
|
|
|
|
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
|
|
|
|
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, "-oForwardAgent=no");
|
|
|
|
addargs(&args, "-oPermitLocalCommand=no");
|
|
|
|
addargs(&args, "-oClearAllForwardings=yes");
|
2000-10-14 05:23:11 +00:00
|
|
|
|
1999-10-27 03:42:43 +00:00
|
|
|
fflag = tflag = 0;
|
2011-01-06 11:40:30 +00:00
|
|
|
while ((ch = getopt(argc, argv, "dfl:prtvBCc:i:P:q12346S:o:F:")) != -1)
|
1999-11-24 13:26:21 +00:00
|
|
|
switch (ch) {
|
|
|
|
/* User-visible flags. */
|
2003-01-24 00:37:38 +00:00
|
|
|
case '1':
|
|
|
|
case '2':
|
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;
|
2011-01-06 11:40:30 +00:00
|
|
|
case '3':
|
|
|
|
throughlocal = 1;
|
|
|
|
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':
|
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;
|
|
|
|
case 'P':
|
2010-12-01 01:03:19 +00:00
|
|
|
addargs(&remote_remote_args, "-p");
|
|
|
|
addargs(&remote_remote_args, "%s", optarg);
|
2010-01-08 07:53:43 +00:00
|
|
|
addargs(&args, "-p");
|
|
|
|
addargs(&args, "%s", 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");
|
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;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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. */
|
1999-10-27 03:42:43 +00:00
|
|
|
sink(argc, argv);
|
|
|
|
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
|
|
|
|
1999-11-24 13:26:21 +00:00
|
|
|
(void) signal(SIGPIPE, lostconn);
|
1999-10-27 03:42:43 +00:00
|
|
|
|
|
|
|
if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
|
|
|
|
toremote(targ, argc, argv);
|
|
|
|
else {
|
|
|
|
if (targetshouldbedirectory)
|
|
|
|
verifydir(argv[argc - 1]);
|
2006-01-31 11:11:37 +00:00
|
|
|
tolocal(argc, argv); /* 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
|
|
|
*/
|
|
|
|
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 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;
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
1999-10-27 03:42:43 +00:00
|
|
|
void
|
2003-06-04 12:51:08 +00:00
|
|
|
toremote(char *targ, int argc, char **argv)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
2005-04-03 00:16:11 +00:00
|
|
|
char *bp, *host, *src, *suser, *thost, *tuser, *arg;
|
2006-01-31 10:49:27 +00:00
|
|
|
arglist alist;
|
2006-04-23 02:04:27 +00:00
|
|
|
int i;
|
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
|
|
|
|
|
|
|
*targ++ = 0;
|
|
|
|
if (*targ == 0)
|
|
|
|
targ = ".";
|
|
|
|
|
2005-04-03 00:16:39 +00:00
|
|
|
arg = xstrdup(argv[argc - 1]);
|
2005-04-03 00:16:11 +00:00
|
|
|
if ((thost = strrchr(arg, '@'))) {
|
1999-10-27 03:42:43 +00:00
|
|
|
/* user@host */
|
|
|
|
*thost++ = 0;
|
2005-04-03 00:16:11 +00:00
|
|
|
tuser = arg;
|
1999-10-27 03:42:43 +00:00
|
|
|
if (*tuser == '\0')
|
|
|
|
tuser = NULL;
|
|
|
|
} else {
|
2005-04-03 00:16:11 +00:00
|
|
|
thost = arg;
|
1999-10-27 03:42:43 +00:00
|
|
|
tuser = NULL;
|
|
|
|
}
|
|
|
|
|
2006-01-31 10:49:27 +00:00
|
|
|
if (tuser != NULL && !okname(tuser)) {
|
2013-06-01 21:31:17 +00:00
|
|
|
free(arg);
|
2006-01-31 10:49:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-10-27 03:42:43 +00:00
|
|
|
for (i = 0; i < argc - 1; i++) {
|
|
|
|
src = colon(argv[i]);
|
2011-01-06 11:40:30 +00:00
|
|
|
if (src && throughlocal) { /* extended remote to remote */
|
|
|
|
*src++ = 0;
|
|
|
|
if (*src == 0)
|
|
|
|
src = ".";
|
|
|
|
host = strrchr(argv[i], '@');
|
|
|
|
if (host) {
|
|
|
|
*host++ = 0;
|
|
|
|
host = cleanhostname(host);
|
|
|
|
suser = argv[i];
|
|
|
|
if (*suser == '\0')
|
|
|
|
suser = pwd->pw_name;
|
|
|
|
else if (!okname(suser))
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
host = cleanhostname(argv[i]);
|
|
|
|
suser = NULL;
|
|
|
|
}
|
2011-09-22 11:38:00 +00:00
|
|
|
xasprintf(&bp, "%s -f %s%s", cmd,
|
|
|
|
*src == '-' ? "-- " : "", src);
|
2011-01-06 11:40:30 +00:00
|
|
|
if (do_cmd(host, suser, bp, &remin, &remout) < 0)
|
|
|
|
exit(1);
|
2013-06-01 21:31:17 +00:00
|
|
|
free(bp);
|
2011-01-06 11:40:30 +00:00
|
|
|
host = cleanhostname(thost);
|
2011-09-22 11:38:00 +00:00
|
|
|
xasprintf(&bp, "%s -t %s%s", cmd,
|
|
|
|
*targ == '-' ? "-- " : "", targ);
|
2011-01-06 11:40:30 +00:00
|
|
|
if (do_cmd2(host, tuser, bp, remin, remout) < 0)
|
|
|
|
exit(1);
|
2013-06-01 21:31:17 +00:00
|
|
|
free(bp);
|
2011-01-06 11:40:30 +00:00
|
|
|
(void) close(remin);
|
|
|
|
(void) close(remout);
|
|
|
|
remin = remout = -1;
|
|
|
|
} else if (src) { /* standard remote to remote */
|
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]);
|
|
|
|
}
|
1999-10-27 03:42:43 +00:00
|
|
|
*src++ = 0;
|
|
|
|
if (*src == 0)
|
|
|
|
src = ".";
|
2002-12-23 02:14:51 +00:00
|
|
|
host = strrchr(argv[i], '@');
|
2006-01-31 10:49:27 +00:00
|
|
|
|
1999-10-27 03:42:43 +00:00
|
|
|
if (host) {
|
|
|
|
*host++ = 0;
|
2000-01-14 04:45:46 +00:00
|
|
|
host = cleanhostname(host);
|
1999-10-27 03:42:43 +00:00
|
|
|
suser = argv[i];
|
|
|
|
if (*suser == '\0')
|
|
|
|
suser = pwd->pw_name;
|
2006-01-31 10:49:27 +00:00
|
|
|
else if (!okname(suser))
|
2003-02-24 00:52:58 +00:00
|
|
|
continue;
|
2006-01-31 10:49:27 +00:00
|
|
|
addargs(&alist, "-l");
|
|
|
|
addargs(&alist, "%s", suser);
|
2000-01-14 04:45:46 +00:00
|
|
|
} else {
|
|
|
|
host = cleanhostname(argv[i]);
|
|
|
|
}
|
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 */
|
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);
|
2000-01-14 04:45:46 +00:00
|
|
|
host = cleanhostname(thost);
|
2000-08-23 00:46:23 +00:00
|
|
|
if (do_cmd(host, tuser, bp, &remin,
|
2006-04-23 02:04:27 +00:00
|
|
|
&remout) < 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
|
|
|
}
|
|
|
|
}
|
2013-06-01 21:31:17 +00:00
|
|
|
free(arg);
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-06-04 12:51:08 +00:00
|
|
|
tolocal(int argc, char **argv)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
|
|
|
char *bp, *host, *src, *suser;
|
2006-01-31 10:49:27 +00:00
|
|
|
arglist alist;
|
2006-04-23 02:04:27 +00:00
|
|
|
int i;
|
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++) {
|
1999-11-24 13:26:21 +00:00
|
|
|
if (!(src = colon(argv[i]))) { /* 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;
|
|
|
|
}
|
|
|
|
*src++ = 0;
|
|
|
|
if (*src == 0)
|
|
|
|
src = ".";
|
2002-12-23 02:14:51 +00:00
|
|
|
if ((host = strrchr(argv[i], '@')) == NULL) {
|
1999-10-27 03:42:43 +00:00
|
|
|
host = argv[i];
|
|
|
|
suser = NULL;
|
|
|
|
} else {
|
|
|
|
*host++ = 0;
|
|
|
|
suser = argv[i];
|
|
|
|
if (*suser == '\0')
|
|
|
|
suser = pwd->pw_name;
|
|
|
|
}
|
2000-01-14 04:45:46 +00:00
|
|
|
host = cleanhostname(host);
|
2011-09-22 11:38:00 +00:00
|
|
|
xasprintf(&bp, "%s -f %s%s",
|
|
|
|
cmd, *src == '-' ? "-- " : "", src);
|
2006-04-23 02:04:27 +00:00
|
|
|
if (do_cmd(host, suser, bp, &remin, &remout) < 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);
|
1999-10-27 03:42:43 +00:00
|
|
|
sink(1, argv + argc - 1);
|
1999-11-24 13:26:21 +00:00
|
|
|
(void) close(remin);
|
1999-10-27 03:42:43 +00:00
|
|
|
remin = remout = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
size_t amt;
|
2005-08-02 07:07:07 +00:00
|
|
|
int fd = -1, haderr, indx;
|
2007-06-12 14:02:07 +00:00
|
|
|
char *last, *name, buf[2048], encname[MAXPATHLEN];
|
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';
|
2007-06-12 13:41:06 +00:00
|
|
|
if ((fd = open(name, O_RDONLY|O_NONBLOCK, 0)) < 0)
|
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;
|
|
|
|
}
|
1999-10-27 03:42:43 +00:00
|
|
|
if (fstat(fd, &stb) < 0) {
|
|
|
|
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);
|
1999-11-24 13:26:21 +00:00
|
|
|
if (verbose_mode) {
|
|
|
|
fprintf(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) {
|
2007-10-26 05:39:15 +00:00
|
|
|
if (atomicio(read, fd, bp->buf, amt) != amt)
|
2005-05-26 02:23:44 +00:00
|
|
|
haderr = errno;
|
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);
|
|
|
|
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-11-24 13:26:21 +00:00
|
|
|
if (showprogress)
|
2003-01-10 10:43:24 +00:00
|
|
|
stop_progress_meter();
|
1999-10-27 03:42:43 +00:00
|
|
|
|
2005-11-22 08:38:06 +00:00
|
|
|
if (fd != -1) {
|
|
|
|
if (close(fd) < 0 && !haderr)
|
|
|
|
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();
|
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;
|
2013-06-05 22:16:04 +00:00
|
|
|
char *last, *vect[1], path[MAXPATHLEN];
|
1999-10-27 03:42:43 +00:00
|
|
|
|
|
|
|
if (!(dirp = opendir(name))) {
|
|
|
|
run_err("%s: %s", name, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
last = strrchr(name, '/');
|
|
|
|
if (last == 0)
|
|
|
|
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)
|
|
|
|
fprintf(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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-06-04 12:51:08 +00:00
|
|
|
sink(int argc, char **argv)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
|
|
|
static BUF buffer;
|
|
|
|
struct stat stb;
|
1999-11-24 13:26:21 +00:00
|
|
|
enum {
|
|
|
|
YES, NO, DISPLAYED
|
|
|
|
} wrerr;
|
1999-10-27 03:42:43 +00:00
|
|
|
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;
|
2000-08-29 00:33:50 +00:00
|
|
|
int setimes, targisdir, wrerrno = 0;
|
1999-10-27 03:42:43 +00:00
|
|
|
char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
|
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
|
|
|
|
|
|
|
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;
|
|
|
|
for (first = 1;; first = 0) {
|
|
|
|
cp = buf;
|
2005-05-26 02:23:44 +00:00
|
|
|
if (atomicio(read, remin, cp, 1) != 1)
|
1999-10-27 03:42:43 +00: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 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)
|
|
|
|
fprintf(stderr, "Sink: %s", buf);
|
1999-10-27 03:42:43 +00:00
|
|
|
|
|
|
|
if (buf[0] == '\01' || buf[0] == '\02') {
|
|
|
|
if (iamremote == 0)
|
2003-07-03 03:46:56 +00:00
|
|
|
(void) atomicio(vwrite, STDERR_FILENO,
|
2000-12-15 23:03:10 +00:00
|
|
|
buf + 1, strlen(buf + 1));
|
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);
|
1999-10-27 03:42:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
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");
|
2013-07-18 06:11:25 +00:00
|
|
|
if ((time_t)ull < 0 ||
|
|
|
|
(unsigned long long)(time_t)ull != 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");
|
2013-07-18 06:11:25 +00:00
|
|
|
if ((time_t)ull < 0 ||
|
|
|
|
(unsigned long long)(time_t)ull != 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');
|
|
|
|
}
|
|
|
|
if (*cp++ != ' ')
|
|
|
|
SCREWUP("mode not delimited");
|
|
|
|
|
2013-11-21 02:56:49 +00:00
|
|
|
for (size = 0; isdigit((unsigned char)*cp);)
|
1999-10-27 03:42:43 +00:00
|
|
|
size = size * 10 + (*cp++ - '0');
|
|
|
|
if (*cp++ != ' ')
|
|
|
|
SCREWUP("size not delimited");
|
2004-04-19 13:50:16 +00:00
|
|
|
if ((strchr(cp, '/') != NULL) || (strcmp(cp, "..") == 0)) {
|
|
|
|
run_err("error: unexpected filename: %s", cp);
|
|
|
|
exit(1);
|
|
|
|
}
|
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 {
|
1999-11-24 13:26:21 +00:00
|
|
|
/* Handle copying from a read-only
|
|
|
|
directory */
|
1999-10-27 03:42:43 +00:00
|
|
|
mod_flag = 1;
|
|
|
|
if (mkdir(np, mode | S_IRWXU) < 0)
|
|
|
|
goto bad;
|
|
|
|
}
|
2001-02-10 21:50:00 +00:00
|
|
|
vect[0] = xstrdup(np);
|
1999-10-27 03:42:43 +00:00
|
|
|
sink(1, vect);
|
|
|
|
if (setimes) {
|
|
|
|
setimes = 0;
|
2001-02-19 10:51:08 +00:00
|
|
|
if (utimes(vect[0], tv) < 0)
|
1999-11-24 13:26:21 +00:00
|
|
|
run_err("%s: set times: %s",
|
2001-02-19 10:51:08 +00:00
|
|
|
vect[0], strerror(errno));
|
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;
|
2001-06-05 19:31:41 +00:00
|
|
|
if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
|
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;
|
|
|
|
wrerr = NO;
|
|
|
|
|
|
|
|
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. */
|
|
|
|
if (wrerr == NO) {
|
2005-05-26 02:23:44 +00:00
|
|
|
if (atomicio(vwrite, ofd, bp->buf,
|
|
|
|
count) != count) {
|
1999-10-27 03:42:43 +00:00
|
|
|
wrerr = YES;
|
2005-05-26 02:23:44 +00:00
|
|
|
wrerrno = errno;
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
count = 0;
|
|
|
|
cp = bp->buf;
|
|
|
|
}
|
|
|
|
}
|
2007-10-26 05:39:15 +00:00
|
|
|
unset_nonblock(remin);
|
1999-10-27 03:42:43 +00:00
|
|
|
if (showprogress)
|
2003-01-10 10:43:24 +00:00
|
|
|
stop_progress_meter();
|
1999-10-27 03:42:43 +00:00
|
|
|
if (count != 0 && wrerr == NO &&
|
2005-05-26 02:23:44 +00:00
|
|
|
atomicio(vwrite, ofd, bp->buf, count) != count) {
|
1999-10-27 03:42:43 +00:00
|
|
|
wrerr = YES;
|
2005-05-26 02:23:44 +00:00
|
|
|
wrerrno = errno;
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
2007-06-25 08:32:33 +00:00
|
|
|
if (wrerr == NO && (!exists || S_ISREG(stb.st_mode)) &&
|
|
|
|
ftruncate(ofd, size) != 0) {
|
1999-10-27 03:42:43 +00:00
|
|
|
run_err("%s: truncate: %s", np, strerror(errno));
|
|
|
|
wrerr = DISPLAYED;
|
|
|
|
}
|
|
|
|
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 */
|
1999-10-27 03:42:43 +00:00
|
|
|
run_err("%s: set mode: %s",
|
2001-03-29 00:39:55 +00:00
|
|
|
np, strerror(errno));
|
2004-07-08 13:11:44 +00:00
|
|
|
wrerr = DISPLAYED;
|
|
|
|
}
|
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 */
|
1999-10-27 03:42:43 +00:00
|
|
|
run_err("%s: set mode: %s",
|
2001-03-29 00:39:55 +00:00
|
|
|
np, strerror(errno));
|
2004-07-08 13:11:44 +00:00
|
|
|
wrerr = DISPLAYED;
|
|
|
|
}
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
2000-07-15 04:14:16 +00:00
|
|
|
if (close(ofd) == -1) {
|
|
|
|
wrerr = YES;
|
|
|
|
wrerrno = errno;
|
|
|
|
}
|
1999-11-24 13:26:21 +00:00
|
|
|
(void) response();
|
1999-10-27 03:42:43 +00:00
|
|
|
if (setimes && wrerr == NO) {
|
|
|
|
setimes = 0;
|
2000-09-23 06:15:56 +00:00
|
|
|
if (utimes(np, tv) < 0) {
|
1999-10-27 03:42:43 +00:00
|
|
|
run_err("%s: set times: %s",
|
2001-03-29 00:39:55 +00:00
|
|
|
np, strerror(errno));
|
1999-10-27 03:42:43 +00:00
|
|
|
wrerr = DISPLAYED;
|
|
|
|
}
|
|
|
|
}
|
1999-11-24 13:26:21 +00:00
|
|
|
switch (wrerr) {
|
1999-10-27 03:42:43 +00:00
|
|
|
case YES:
|
|
|
|
run_err("%s: %s", np, strerror(wrerrno));
|
|
|
|
break;
|
|
|
|
case NO:
|
2003-07-03 03:46:56 +00:00
|
|
|
(void) atomicio(vwrite, remout, "", 1);
|
1999-10-27 03:42:43 +00:00
|
|
|
break;
|
|
|
|
case DISPLAYED:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
screwup:
|
|
|
|
run_err("protocol error: %s", why);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-12-06 17:55:26 +00:00
|
|
|
response(void)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
|
|
|
char ch, *cp, resp, rbuf[2048];
|
|
|
|
|
- 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');
|
|
|
|
|
|
|
|
if (!iamremote)
|
2003-07-03 03:46:56 +00:00
|
|
|
(void) atomicio(vwrite, STDERR_FILENO, rbuf, cp - rbuf);
|
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,
|
2011-01-06 11:41:21 +00:00
|
|
|
"usage: scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
|
2003-10-15 05:50:42 +00:00
|
|
|
" [-l limit] [-o ssh_option] [-P port] [-S program]\n"
|
2007-08-08 04:29:58 +00:00
|
|
|
" [[user@]host1:]file1 ... [[user@]host2:]file2\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);
|
1999-11-24 13:26:21 +00:00
|
|
|
vfprintf(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
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
- OpenBSD CVS updates to v1.2.3
[ssh.h atomicio.c]
- int atomicio -> ssize_t (for alpha). ok deraadt@
[auth-rsa.c]
- delay MD5 computation until client sends response, free() early, cleanup.
[cipher.c]
- void* -> unsigned char*, ok niels@
[hostfile.c]
- remove unused variable 'len'. fix comments.
- remove unused variable
[log-client.c log-server.c]
- rename a cpp symbol, to avoid param.h collision
[packet.c]
- missing xfree()
- getsockname() requires initialized tolen; andy@guildsoftware.com
- use getpeername() in packet_connection_is_on_socket(), fixes sshd -i;
from Holger.Trapp@Informatik.TU-Chemnitz.DE
[pty.c pty.h]
- register cleanup for pty earlier. move code for pty-owner handling to
pty.c ok provos@, dugsong@
[readconf.c]
- turn off x11-fwd for the client, too.
[rsa.c]
- PKCS#1 padding
[scp.c]
- allow '.' in usernames; from jedgar@fxp.org
[servconf.c]
- typo: ignore_user_known_hosts int->flag; naddy@mips.rhein-neckar.de
- sync with sshd_config
[ssh-keygen.c]
- enable ssh-keygen -l -f ~/.ssh/known_hosts, ok deraadt@
[ssh.1]
- Change invalid 'CHAT' loglevel to 'VERBOSE'
[ssh.c]
- suppress AAAA query host when '-4' is used; from shin@nd.net.fujitsu.co.jp
- turn off x11-fwd for the client, too.
[sshconnect.c]
- missing xfree()
- retry rresvport_af(), too. from sumikawa@ebina.hitachi.co.jp.
- read error vs. "Connection closed by remote host"
[sshd.8]
- ie. -> i.e.,
- do not link to a commercial page..
- sync with sshd_config
[sshd.c]
- no need for poll.h; from bright@wintelcom.net
- log with level log() not fatal() if peer behaves badly.
- don't panic if client behaves strange. ok deraadt@
- make no-port-forwarding for RSA keys deny both -L and -R style fwding
- delay close() of pty until the pty has been chowned back to root
- oops, fix comment, too.
- missing xfree()
- move XAUTHORITY to subdir. ok dugsong@. fixes debian bug #57907, too.
(http://cgi.debian.org/cgi-bin/bugreport.cgi?archive=no&bug=57907)
- register cleanup for pty earlier. move code for pty-owner handling to
pty.c ok provos@, dugsong@
- create x11 cookie file
- fix pr 1113, fclose() -> pclose(), todo: remote popen()
- version 1.2.3
- Cleaned up
2000-03-09 10:27:49 +00:00
|
|
|
bad: fprintf(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;
|
|
|
|
|
|
|
|
if (fstat(fd, &stb) < 0) {
|
|
|
|
run_err("fstat: %s", strerror(errno));
|
|
|
|
return (0);
|
|
|
|
}
|
2002-12-23 02:22:09 +00:00
|
|
|
size = roundup(stb.st_blksize, blksize);
|
|
|
|
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);
|
1999-11-24 13:26:21 +00:00
|
|
|
if (bp->buf == NULL)
|
|
|
|
bp->buf = xmalloc(size);
|
|
|
|
else
|
2006-03-26 03:22:47 +00:00
|
|
|
bp->buf = xrealloc(bp->buf, 1, size);
|
2001-09-12 17:49:48 +00:00
|
|
|
memset(bp->buf, 0, size);
|
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
|
|
|
}
|