Home | History | Annotate | Line # | Download | only in dist
scp.c revision 1.22
      1  1.22       mrg /*	$NetBSD: scp.c,v 1.22 2019/02/04 04:36:41 mrg Exp $	*/
      2  1.20  christos /* $OpenBSD: scp.c,v 1.197 2018/06/01 04:31:48 dtucker Exp $ */
      3  1.20  christos 
      4   1.1  christos /*
      5   1.1  christos  * scp - secure remote copy.  This is basically patched BSD rcp which
      6   1.1  christos  * uses ssh to do the data transfer (instead of using rcmd).
      7   1.1  christos  *
      8   1.1  christos  * NOTE: This version should NOT be suid root.  (This uses ssh to
      9   1.1  christos  * do the transfer and ssh has the necessary privileges.)
     10   1.1  christos  *
     11   1.1  christos  * 1995 Timo Rinne <tri (at) iki.fi>, Tatu Ylonen <ylo (at) cs.hut.fi>
     12   1.1  christos  *
     13   1.1  christos  * As far as I am concerned, the code I have written for this software
     14   1.1  christos  * can be used freely for any purpose.  Any derived versions of this
     15   1.1  christos  * software must be clearly marked as such, and if the derived work is
     16   1.1  christos  * incompatible with the protocol description in the RFC file, it must be
     17   1.1  christos  * called by a name other than "ssh" or "Secure Shell".
     18   1.1  christos  */
     19   1.1  christos /*
     20   1.1  christos  * Copyright (c) 1999 Theo de Raadt.  All rights reserved.
     21   1.1  christos  * Copyright (c) 1999 Aaron Campbell.  All rights reserved.
     22   1.1  christos  *
     23   1.1  christos  * Redistribution and use in source and binary forms, with or without
     24   1.1  christos  * modification, are permitted provided that the following conditions
     25   1.1  christos  * are met:
     26   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     27   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     28   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     29   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     30   1.1  christos  *    documentation and/or other materials provided with the distribution.
     31   1.1  christos  *
     32   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     33   1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     34   1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     35   1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     36   1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     37   1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     38   1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     39   1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     40   1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     41   1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     42   1.1  christos  */
     43   1.1  christos 
     44   1.1  christos /*
     45   1.1  christos  * Parts from:
     46   1.1  christos  *
     47   1.1  christos  * Copyright (c) 1983, 1990, 1992, 1993, 1995
     48   1.1  christos  *	The Regents of the University of California.  All rights reserved.
     49   1.1  christos  *
     50   1.1  christos  * Redistribution and use in source and binary forms, with or without
     51   1.1  christos  * modification, are permitted provided that the following conditions
     52   1.1  christos  * are met:
     53   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     54   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     55   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     56   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     57   1.1  christos  *    documentation and/or other materials provided with the distribution.
     58   1.1  christos  * 3. Neither the name of the University nor the names of its contributors
     59   1.1  christos  *    may be used to endorse or promote products derived from this software
     60   1.1  christos  *    without specific prior written permission.
     61   1.1  christos  *
     62   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     63   1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     64   1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     65   1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     66   1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     67   1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     68   1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     69   1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     70   1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     71   1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     72   1.1  christos  * SUCH DAMAGE.
     73   1.1  christos  *
     74   1.1  christos  */
     75   1.1  christos 
     76   1.2  christos #include "includes.h"
     77  1.22       mrg __RCSID("$NetBSD: scp.c,v 1.22 2019/02/04 04:36:41 mrg Exp $");
     78  1.15  christos 
     79  1.11  christos #include <sys/param.h>	/* roundup MAX */
     80   1.1  christos #include <sys/types.h>
     81   1.1  christos #include <sys/poll.h>
     82   1.1  christos #include <sys/wait.h>
     83   1.1  christos #include <sys/stat.h>
     84   1.1  christos #include <sys/time.h>
     85   1.1  christos #include <sys/uio.h>
     86   1.1  christos 
     87   1.1  christos #include <ctype.h>
     88   1.1  christos #include <dirent.h>
     89   1.1  christos #include <errno.h>
     90   1.1  christos #include <fcntl.h>
     91  1.14  christos #include <locale.h>
     92   1.1  christos #include <pwd.h>
     93   1.1  christos #include <signal.h>
     94   1.1  christos #include <stdarg.h>
     95  1.17  christos #include <stdint.h>
     96   1.1  christos #include <stdio.h>
     97   1.1  christos #include <stdlib.h>
     98   1.1  christos #include <string.h>
     99   1.1  christos #include <time.h>
    100   1.1  christos #include <unistd.h>
    101  1.11  christos #include <limits.h>
    102   1.1  christos #include <vis.h>
    103   1.1  christos 
    104   1.1  christos #include "xmalloc.h"
    105  1.18  christos #include "ssh.h"
    106   1.1  christos #include "atomicio.h"
    107   1.1  christos #include "pathnames.h"
    108   1.1  christos #include "log.h"
    109   1.1  christos #include "misc.h"
    110   1.1  christos #include "progressmeter.h"
    111  1.14  christos #include "utf8.h"
    112   1.1  christos 
    113   1.1  christos #define COPY_BUFLEN	16384
    114   1.1  christos 
    115  1.18  christos int do_cmd(char *host, char *remuser, int port, char *cmd, int *fdin, int *fdout);
    116  1.18  christos int do_cmd2(char *host, char *remuser, int port, char *cmd, int fdin, int fdout);
    117   1.1  christos 
    118   1.5  christos static char empty[] = "";
    119   1.1  christos 
    120   1.1  christos /* Struct for addargs */
    121   1.1  christos arglist args;
    122   1.5  christos arglist remote_remote_args;
    123   1.1  christos 
    124   1.1  christos /* Bandwidth limit */
    125   1.5  christos long long limit_kbps = 0;
    126   1.5  christos struct bwlimit bwlimit;
    127   1.1  christos 
    128   1.1  christos /* Name of current file being transferred. */
    129   1.1  christos char *curfile;
    130   1.1  christos 
    131   1.1  christos /* This is set to non-zero to enable verbose mode. */
    132   1.1  christos int verbose_mode = 0;
    133   1.1  christos 
    134   1.1  christos /* This is set to zero if the progressmeter is not desired. */
    135   1.1  christos int showprogress = 1;
    136   1.1  christos 
    137   1.5  christos /*
    138   1.5  christos  * This is set to non-zero if remote-remote copy should be piped
    139   1.5  christos  * through this process.
    140   1.5  christos  */
    141   1.5  christos int throughlocal = 0;
    142   1.5  christos 
    143  1.18  christos /* Non-standard port to use for the ssh connection or -1. */
    144  1.18  christos int sshport = -1;
    145  1.18  christos 
    146   1.1  christos /* This is the program to execute for the secured connection. ("ssh" or -S) */
    147   1.2  christos #ifdef RESCUEDIR
    148   1.5  christos const char *ssh_program = RESCUEDIR "/ssh";
    149   1.2  christos #else
    150   1.5  christos const char *ssh_program = _PATH_SSH_PROGRAM;
    151   1.2  christos #endif
    152   1.1  christos 
    153   1.1  christos /* This is used to store the pid of ssh_program */
    154   1.1  christos pid_t do_cmd_pid = -1;
    155   1.1  christos 
    156   1.6     joerg __dead static void
    157   1.1  christos killchild(int signo)
    158   1.1  christos {
    159   1.1  christos 	if (do_cmd_pid > 1) {
    160   1.1  christos 		kill(do_cmd_pid, signo ? signo : SIGTERM);
    161   1.1  christos 		waitpid(do_cmd_pid, NULL, 0);
    162   1.1  christos 	}
    163   1.1  christos 
    164   1.1  christos 	if (signo)
    165   1.1  christos 		_exit(1);
    166   1.1  christos 	exit(1);
    167   1.1  christos }
    168   1.1  christos 
    169   1.4      adam static void
    170   1.4      adam suspchild(int signo)
    171   1.4      adam {
    172   1.4      adam 	int status;
    173   1.4      adam 
    174   1.4      adam 	if (do_cmd_pid > 1) {
    175   1.4      adam 		kill(do_cmd_pid, signo);
    176   1.4      adam 		while (waitpid(do_cmd_pid, &status, WUNTRACED) == -1 &&
    177   1.4      adam 		    errno == EINTR)
    178   1.4      adam 			;
    179   1.4      adam 		kill(getpid(), SIGSTOP);
    180   1.4      adam 	}
    181   1.4      adam }
    182   1.4      adam 
    183   1.1  christos static int
    184   1.1  christos do_local_cmd(arglist *a)
    185   1.1  christos {
    186   1.1  christos 	u_int i;
    187   1.1  christos 	int status;
    188   1.1  christos 	pid_t pid;
    189   1.1  christos 
    190   1.1  christos 	if (a->num == 0)
    191   1.1  christos 		fatal("do_local_cmd: no arguments");
    192   1.1  christos 
    193   1.1  christos 	if (verbose_mode) {
    194   1.1  christos 		fprintf(stderr, "Executing:");
    195   1.1  christos 		for (i = 0; i < a->num; i++)
    196  1.14  christos 			fmprintf(stderr, " %s", a->list[i]);
    197   1.1  christos 		fprintf(stderr, "\n");
    198   1.1  christos 	}
    199   1.1  christos 	if ((pid = fork()) == -1)
    200   1.1  christos 		fatal("do_local_cmd: fork: %s", strerror(errno));
    201   1.1  christos 
    202   1.1  christos 	if (pid == 0) {
    203   1.1  christos 		execvp(a->list[0], a->list);
    204   1.1  christos 		perror(a->list[0]);
    205   1.1  christos 		exit(1);
    206   1.1  christos 	}
    207   1.1  christos 
    208   1.1  christos 	do_cmd_pid = pid;
    209   1.1  christos 	signal(SIGTERM, killchild);
    210   1.1  christos 	signal(SIGINT, killchild);
    211   1.1  christos 	signal(SIGHUP, killchild);
    212   1.1  christos 
    213   1.1  christos 	while (waitpid(pid, &status, 0) == -1)
    214   1.1  christos 		if (errno != EINTR)
    215   1.1  christos 			fatal("do_local_cmd: waitpid: %s", strerror(errno));
    216   1.1  christos 
    217   1.1  christos 	do_cmd_pid = -1;
    218   1.1  christos 
    219   1.1  christos 	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
    220   1.1  christos 		return (-1);
    221   1.1  christos 
    222   1.1  christos 	return (0);
    223   1.1  christos }
    224   1.1  christos 
    225   1.1  christos /*
    226   1.1  christos  * This function executes the given command as the specified user on the
    227   1.1  christos  * given host.  This returns < 0 if execution fails, and >= 0 otherwise. This
    228   1.1  christos  * assigns the input and output file descriptors on success.
    229   1.1  christos  */
    230   1.1  christos 
    231   1.1  christos int
    232  1.18  christos do_cmd(char *host, char *remuser, int port, char *cmd, int *fdin, int *fdout)
    233   1.1  christos {
    234   1.1  christos 	int pin[2], pout[2], reserved[2];
    235   1.1  christos 
    236   1.1  christos 	if (verbose_mode)
    237  1.14  christos 		fmprintf(stderr,
    238   1.1  christos 		    "Executing: program %s host %s, user %s, command %s\n",
    239   1.1  christos 		    ssh_program, host,
    240   1.1  christos 		    remuser ? remuser : "(unspecified)", cmd);
    241   1.1  christos 
    242  1.18  christos 	if (port == -1)
    243  1.18  christos 		port = sshport;
    244  1.18  christos 
    245   1.1  christos 	/*
    246   1.1  christos 	 * Reserve two descriptors so that the real pipes won't get
    247   1.1  christos 	 * descriptors 0 and 1 because that will screw up dup2 below.
    248   1.1  christos 	 */
    249   1.1  christos 	if (pipe(reserved) < 0)
    250   1.1  christos 		fatal("pipe: %s", strerror(errno));
    251   1.1  christos 
    252   1.1  christos 	/* Create a socket pair for communicating with ssh. */
    253   1.1  christos 	if (pipe(pin) < 0)
    254   1.1  christos 		fatal("pipe: %s", strerror(errno));
    255   1.1  christos 	if (pipe(pout) < 0)
    256   1.1  christos 		fatal("pipe: %s", strerror(errno));
    257   1.1  christos 
    258   1.1  christos 	/* Free the reserved descriptors. */
    259   1.1  christos 	close(reserved[0]);
    260   1.1  christos 	close(reserved[1]);
    261   1.1  christos 
    262   1.4      adam 	signal(SIGTSTP, suspchild);
    263   1.4      adam 	signal(SIGTTIN, suspchild);
    264   1.4      adam 	signal(SIGTTOU, suspchild);
    265   1.4      adam 
    266   1.1  christos 	/* Fork a child to execute the command on the remote host using ssh. */
    267   1.1  christos 	do_cmd_pid = fork();
    268   1.1  christos 	if (do_cmd_pid == 0) {
    269   1.1  christos 		/* Child. */
    270   1.1  christos 		close(pin[1]);
    271   1.1  christos 		close(pout[0]);
    272   1.1  christos 		dup2(pin[0], 0);
    273   1.1  christos 		dup2(pout[1], 1);
    274   1.1  christos 		close(pin[0]);
    275   1.1  christos 		close(pout[1]);
    276   1.1  christos 
    277   1.1  christos 		replacearg(&args, 0, "%s", ssh_program);
    278  1.18  christos 		if (port != -1) {
    279  1.18  christos 			addargs(&args, "-p");
    280  1.18  christos 			addargs(&args, "%d", port);
    281  1.18  christos 		}
    282   1.4      adam 		if (remuser != NULL) {
    283   1.4      adam 			addargs(&args, "-l");
    284   1.4      adam 			addargs(&args, "%s", remuser);
    285   1.4      adam 		}
    286   1.4      adam 		addargs(&args, "--");
    287   1.1  christos 		addargs(&args, "%s", host);
    288   1.1  christos 		addargs(&args, "%s", cmd);
    289   1.1  christos 
    290   1.1  christos 		execvp(ssh_program, args.list);
    291   1.1  christos 		perror(ssh_program);
    292   1.1  christos 		exit(1);
    293   1.1  christos 	} else if (do_cmd_pid == -1) {
    294   1.1  christos 		fatal("fork: %s", strerror(errno));
    295   1.1  christos 	}
    296   1.1  christos 	/* Parent.  Close the other side, and return the local side. */
    297   1.1  christos 	close(pin[0]);
    298   1.1  christos 	*fdout = pin[1];
    299   1.1  christos 	close(pout[1]);
    300   1.1  christos 	*fdin = pout[0];
    301   1.1  christos 	signal(SIGTERM, killchild);
    302   1.1  christos 	signal(SIGINT, killchild);
    303   1.1  christos 	signal(SIGHUP, killchild);
    304   1.1  christos 	return 0;
    305   1.1  christos }
    306   1.1  christos 
    307   1.5  christos /*
    308  1.20  christos  * This function executes a command similar to do_cmd(), but expects the
    309   1.5  christos  * input and output descriptors to be setup by a previous call to do_cmd().
    310   1.5  christos  * This way the input and output of two commands can be connected.
    311   1.5  christos  */
    312   1.5  christos int
    313  1.18  christos do_cmd2(char *host, char *remuser, int port, char *cmd, int fdin, int fdout)
    314   1.5  christos {
    315   1.5  christos 	pid_t pid;
    316   1.5  christos 	int status;
    317   1.5  christos 
    318   1.5  christos 	if (verbose_mode)
    319  1.14  christos 		fmprintf(stderr,
    320   1.5  christos 		    "Executing: 2nd program %s host %s, user %s, command %s\n",
    321   1.5  christos 		    ssh_program, host,
    322   1.5  christos 		    remuser ? remuser : "(unspecified)", cmd);
    323   1.5  christos 
    324  1.18  christos 	if (port == -1)
    325  1.18  christos 		port = sshport;
    326  1.18  christos 
    327   1.5  christos 	/* Fork a child to execute the command on the remote host using ssh. */
    328   1.5  christos 	pid = fork();
    329   1.5  christos 	if (pid == 0) {
    330   1.5  christos 		dup2(fdin, 0);
    331   1.5  christos 		dup2(fdout, 1);
    332   1.5  christos 
    333   1.5  christos 		replacearg(&args, 0, "%s", ssh_program);
    334  1.18  christos 		if (port != -1) {
    335  1.18  christos 			addargs(&args, "-p");
    336  1.18  christos 			addargs(&args, "%d", port);
    337  1.18  christos 		}
    338   1.5  christos 		if (remuser != NULL) {
    339   1.5  christos 			addargs(&args, "-l");
    340   1.5  christos 			addargs(&args, "%s", remuser);
    341   1.5  christos 		}
    342   1.5  christos 		addargs(&args, "--");
    343   1.5  christos 		addargs(&args, "%s", host);
    344   1.5  christos 		addargs(&args, "%s", cmd);
    345   1.5  christos 
    346   1.5  christos 		execvp(ssh_program, args.list);
    347   1.5  christos 		perror(ssh_program);
    348   1.5  christos 		exit(1);
    349   1.5  christos 	} else if (pid == -1) {
    350   1.5  christos 		fatal("fork: %s", strerror(errno));
    351   1.5  christos 	}
    352   1.5  christos 	while (waitpid(pid, &status, 0) == -1)
    353   1.5  christos 		if (errno != EINTR)
    354   1.5  christos 			fatal("do_cmd2: waitpid: %s", strerror(errno));
    355   1.5  christos 	return 0;
    356   1.5  christos }
    357   1.5  christos 
    358   1.1  christos typedef struct {
    359   1.1  christos 	size_t cnt;
    360   1.1  christos 	char *buf;
    361   1.1  christos } BUF;
    362   1.1  christos 
    363   1.1  christos BUF *allocbuf(BUF *, int, int);
    364   1.6     joerg __dead static void lostconn(int);
    365   1.1  christos int okname(char *);
    366   1.8     joerg void run_err(const char *,...) __printflike(1, 2);
    367   1.1  christos void verifydir(char *);
    368   1.1  christos 
    369   1.1  christos struct passwd *pwd;
    370   1.1  christos uid_t userid;
    371   1.1  christos int errs, remin, remout;
    372   1.1  christos int pflag, iamremote, iamrecursive, targetshouldbedirectory;
    373   1.1  christos 
    374   1.1  christos #define	CMDNEEDS	64
    375   1.1  christos char cmd[CMDNEEDS];		/* must hold "rcp -r -p -d\0" */
    376   1.1  christos 
    377   1.1  christos int response(void);
    378   1.1  christos void rsource(char *, struct stat *);
    379   1.1  christos void sink(int, char *[]);
    380   1.1  christos void source(int, char *[]);
    381   1.6     joerg static void tolocal(int, char *[]);
    382  1.18  christos static void toremote(int, char *[]);
    383   1.6     joerg __dead static void usage(void);
    384   1.1  christos 
    385   1.1  christos int
    386   1.1  christos main(int argc, char **argv)
    387   1.1  christos {
    388   1.1  christos 	int ch, fflag, tflag, status, n;
    389  1.18  christos 	char **newargv;
    390   1.5  christos 	const char *errstr;
    391   1.1  christos 	extern char *optarg;
    392   1.1  christos 	extern int optind;
    393   1.1  christos 
    394   1.1  christos 	/* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
    395   1.1  christos 	sanitise_stdfd();
    396   1.1  christos 
    397  1.14  christos 	setlocale(LC_CTYPE, "");
    398  1.14  christos 
    399   1.1  christos 	/* Copy argv, because we modify it */
    400  1.15  christos 	newargv = xcalloc(MAXIMUM(argc + 1, 1), sizeof(*newargv));
    401   1.1  christos 	for (n = 0; n < argc; n++)
    402   1.1  christos 		newargv[n] = xstrdup(argv[n]);
    403   1.1  christos 	argv = newargv;
    404   1.1  christos 
    405   1.1  christos 	memset(&args, '\0', sizeof(args));
    406   1.5  christos 	memset(&remote_remote_args, '\0', sizeof(remote_remote_args));
    407   1.5  christos 	args.list = remote_remote_args.list = NULL;
    408   1.1  christos 	addargs(&args, "%s", ssh_program);
    409   1.1  christos 	addargs(&args, "-x");
    410   1.5  christos 	addargs(&args, "-oForwardAgent=no");
    411   1.5  christos 	addargs(&args, "-oPermitLocalCommand=no");
    412   1.5  christos 	addargs(&args, "-oClearAllForwardings=yes");
    413  1.18  christos 	addargs(&args, "-oRemoteCommand=none");
    414  1.18  christos 	addargs(&args, "-oRequestTTY=no");
    415   1.1  christos 
    416   1.1  christos 	fflag = tflag = 0;
    417   1.5  christos 	while ((ch = getopt(argc, argv, "dfl:prtvBCc:i:P:q12346S:o:F:")) != -1)
    418   1.1  christos 		switch (ch) {
    419   1.1  christos 		/* User-visible flags. */
    420   1.1  christos 		case '1':
    421  1.17  christos 			fatal("SSH protocol v.1 is no longer supported");
    422  1.17  christos 			break;
    423   1.1  christos 		case '2':
    424  1.17  christos 			/* Ignored */
    425  1.17  christos 			break;
    426   1.1  christos 		case '4':
    427   1.1  christos 		case '6':
    428   1.1  christos 		case 'C':
    429   1.1  christos 			addargs(&args, "-%c", ch);
    430   1.5  christos 			addargs(&remote_remote_args, "-%c", ch);
    431   1.5  christos 			break;
    432   1.5  christos 		case '3':
    433   1.5  christos 			throughlocal = 1;
    434   1.1  christos 			break;
    435   1.1  christos 		case 'o':
    436   1.1  christos 		case 'c':
    437   1.1  christos 		case 'i':
    438   1.1  christos 		case 'F':
    439   1.5  christos 			addargs(&remote_remote_args, "-%c", ch);
    440   1.5  christos 			addargs(&remote_remote_args, "%s", optarg);
    441   1.4      adam 			addargs(&args, "-%c", ch);
    442   1.4      adam 			addargs(&args, "%s", optarg);
    443   1.1  christos 			break;
    444   1.1  christos 		case 'P':
    445  1.18  christos 			sshport = a2port(optarg);
    446  1.18  christos 			if (sshport <= 0)
    447  1.18  christos 				fatal("bad port \"%s\"\n", optarg);
    448   1.1  christos 			break;
    449   1.1  christos 		case 'B':
    450   1.5  christos 			addargs(&remote_remote_args, "-oBatchmode=yes");
    451   1.5  christos 			addargs(&args, "-oBatchmode=yes");
    452   1.1  christos 			break;
    453   1.1  christos 		case 'l':
    454   1.5  christos 			limit_kbps = strtonum(optarg, 1, 100 * 1024 * 1024,
    455   1.5  christos 			    &errstr);
    456   1.5  christos 			if (errstr != NULL)
    457   1.1  christos 				usage();
    458   1.5  christos 			limit_kbps *= 1024; /* kbps */
    459   1.5  christos 			bandwidth_limit_init(&bwlimit, limit_kbps, COPY_BUFLEN);
    460   1.1  christos 			break;
    461   1.1  christos 		case 'p':
    462   1.1  christos 			pflag = 1;
    463   1.1  christos 			break;
    464   1.1  christos 		case 'r':
    465   1.1  christos 			iamrecursive = 1;
    466   1.1  christos 			break;
    467   1.1  christos 		case 'S':
    468   1.1  christos 			ssh_program = xstrdup(optarg);
    469   1.1  christos 			break;
    470   1.1  christos 		case 'v':
    471   1.1  christos 			addargs(&args, "-v");
    472   1.5  christos 			addargs(&remote_remote_args, "-v");
    473   1.1  christos 			verbose_mode = 1;
    474   1.1  christos 			break;
    475   1.1  christos 		case 'q':
    476   1.1  christos 			addargs(&args, "-q");
    477   1.5  christos 			addargs(&remote_remote_args, "-q");
    478   1.1  christos 			showprogress = 0;
    479   1.1  christos 			break;
    480   1.1  christos 
    481   1.1  christos 		/* Server options. */
    482   1.1  christos 		case 'd':
    483   1.1  christos 			targetshouldbedirectory = 1;
    484   1.1  christos 			break;
    485   1.1  christos 		case 'f':	/* "from" */
    486   1.1  christos 			iamremote = 1;
    487   1.1  christos 			fflag = 1;
    488   1.1  christos 			break;
    489   1.1  christos 		case 't':	/* "to" */
    490   1.1  christos 			iamremote = 1;
    491   1.1  christos 			tflag = 1;
    492   1.1  christos 			break;
    493   1.1  christos 		default:
    494   1.1  christos 			usage();
    495   1.1  christos 		}
    496   1.1  christos 	argc -= optind;
    497   1.1  christos 	argv += optind;
    498   1.1  christos 
    499   1.1  christos 	if ((pwd = getpwuid(userid = getuid())) == NULL)
    500   1.1  christos 		fatal("unknown user %u", (u_int) userid);
    501   1.1  christos 
    502   1.1  christos 	if (!isatty(STDOUT_FILENO))
    503   1.1  christos 		showprogress = 0;
    504   1.1  christos 
    505  1.13  christos 	if (pflag) {
    506  1.13  christos 		/* Cannot pledge: -p allows setuid/setgid files... */
    507  1.13  christos 	} else {
    508  1.13  christos #ifdef __OpenBSD__
    509  1.13  christos 		if (pledge("stdio rpath wpath cpath fattr tty proc exec",
    510  1.13  christos 		    NULL) == -1) {
    511  1.13  christos 			perror("pledge");
    512  1.13  christos 			exit(1);
    513  1.13  christos 		}
    514  1.13  christos #endif
    515  1.13  christos 	}
    516  1.13  christos 
    517   1.1  christos 	remin = STDIN_FILENO;
    518   1.1  christos 	remout = STDOUT_FILENO;
    519   1.1  christos 
    520   1.1  christos 	if (fflag) {
    521   1.1  christos 		/* Follow "protocol", send data. */
    522   1.1  christos 		(void) response();
    523   1.1  christos 		source(argc, argv);
    524   1.1  christos 		exit(errs != 0);
    525   1.1  christos 	}
    526   1.1  christos 	if (tflag) {
    527   1.1  christos 		/* Receive data. */
    528   1.1  christos 		sink(argc, argv);
    529   1.1  christos 		exit(errs != 0);
    530   1.1  christos 	}
    531   1.1  christos 	if (argc < 2)
    532   1.1  christos 		usage();
    533   1.1  christos 	if (argc > 2)
    534   1.1  christos 		targetshouldbedirectory = 1;
    535   1.1  christos 
    536   1.1  christos 	remin = remout = -1;
    537   1.1  christos 	do_cmd_pid = -1;
    538   1.1  christos 	/* Command to be executed on remote system using "ssh". */
    539   1.1  christos 	(void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
    540   1.1  christos 	    verbose_mode ? " -v" : "",
    541   1.1  christos 	    iamrecursive ? " -r" : "", pflag ? " -p" : "",
    542   1.1  christos 	    targetshouldbedirectory ? " -d" : "");
    543   1.1  christos 
    544   1.1  christos 	(void) signal(SIGPIPE, lostconn);
    545   1.1  christos 
    546  1.18  christos 	if (colon(argv[argc - 1]))	/* Dest is remote host. */
    547  1.18  christos 		toremote(argc, argv);
    548   1.1  christos 	else {
    549   1.1  christos 		if (targetshouldbedirectory)
    550   1.1  christos 			verifydir(argv[argc - 1]);
    551   1.1  christos 		tolocal(argc, argv);	/* Dest is local host. */
    552   1.1  christos 	}
    553   1.1  christos 	/*
    554   1.1  christos 	 * Finally check the exit status of the ssh process, if one was forked
    555   1.1  christos 	 * and no error has occurred yet
    556   1.1  christos 	 */
    557   1.1  christos 	if (do_cmd_pid != -1 && errs == 0) {
    558   1.1  christos 		if (remin != -1)
    559   1.1  christos 		    (void) close(remin);
    560   1.1  christos 		if (remout != -1)
    561   1.1  christos 		    (void) close(remout);
    562   1.1  christos 		if (waitpid(do_cmd_pid, &status, 0) == -1)
    563   1.1  christos 			errs = 1;
    564   1.1  christos 		else {
    565   1.1  christos 			if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
    566   1.1  christos 				errs = 1;
    567   1.1  christos 		}
    568   1.1  christos 	}
    569   1.1  christos 	exit(errs != 0);
    570   1.1  christos }
    571   1.1  christos 
    572   1.5  christos /* Callback from atomicio6 to update progress meter and limit bandwidth */
    573   1.5  christos static int
    574   1.5  christos scpio(void *_cnt, size_t s)
    575   1.1  christos {
    576   1.5  christos 	off_t *cnt = (off_t *)_cnt;
    577   1.5  christos 
    578   1.5  christos 	*cnt += s;
    579   1.5  christos 	if (limit_kbps > 0)
    580   1.5  christos 		bandwidth_limit(&bwlimit, s);
    581   1.5  christos 	return 0;
    582   1.1  christos }
    583   1.1  christos 
    584   1.9  christos static int
    585   1.9  christos do_times(int fd, int verb, const struct stat *sb)
    586   1.9  christos {
    587   1.9  christos 	/* strlen(2^64) == 20; strlen(10^6) == 7 */
    588   1.9  christos 	char buf[(20 + 7 + 2) * 2 + 2];
    589   1.9  christos 
    590   1.9  christos 	(void)snprintf(buf, sizeof(buf), "T%llu 0 %llu 0\n",
    591   1.9  christos 	    (unsigned long long) (sb->st_mtime < 0 ? 0 : sb->st_mtime),
    592   1.9  christos 	    (unsigned long long) (sb->st_atime < 0 ? 0 : sb->st_atime));
    593   1.9  christos 	if (verb) {
    594   1.9  christos 		fprintf(stderr, "File mtime %lld atime %lld\n",
    595   1.9  christos 		    (long long)sb->st_mtime, (long long)sb->st_atime);
    596   1.9  christos 		fprintf(stderr, "Sending file timestamps: %s", buf);
    597   1.9  christos 	}
    598   1.9  christos 	(void) atomicio(vwrite, fd, buf, strlen(buf));
    599   1.9  christos 	return (response());
    600   1.9  christos }
    601   1.9  christos 
    602  1.18  christos static int
    603  1.18  christos parse_scp_uri(const char *uri, char **userp, char **hostp, int *portp,
    604  1.19  christos      const char **pathp)
    605  1.18  christos {
    606  1.18  christos 	int r;
    607  1.18  christos 
    608  1.18  christos 	r = parse_uri("scp", uri, userp, hostp, portp, pathp);
    609  1.18  christos 	if (r == 0 && *pathp == NULL)
    610  1.18  christos 		*pathp = xstrdup(".");
    611  1.18  christos 	return r;
    612  1.18  christos }
    613  1.18  christos 
    614   1.9  christos void
    615  1.18  christos toremote(int argc, char **argv)
    616   1.1  christos {
    617  1.19  christos 	char *suser = NULL, *host = NULL;
    618  1.19  christos 	const char *src = NULL;
    619  1.19  christos 	char *bp, *tuser, *thost;
    620  1.19  christos 	const char *targ;
    621  1.18  christos 	int sport = -1, tport = -1;
    622   1.1  christos 	arglist alist;
    623  1.18  christos 	int i, r;
    624   1.5  christos 	u_int j;
    625   1.1  christos 
    626   1.1  christos 	memset(&alist, '\0', sizeof(alist));
    627   1.1  christos 	alist.list = NULL;
    628   1.1  christos 
    629  1.18  christos 	/* Parse target */
    630  1.18  christos 	r = parse_scp_uri(argv[argc - 1], &tuser, &thost, &tport, &targ);
    631  1.18  christos 	if (r == -1) {
    632  1.18  christos 		fmprintf(stderr, "%s: invalid uri\n", argv[argc - 1]);
    633  1.18  christos 		++errs;
    634  1.18  christos 		goto out;
    635  1.18  christos 	}
    636  1.18  christos 	if (r != 0) {
    637  1.18  christos 		if (parse_user_host_path(argv[argc - 1], &tuser, &thost,
    638  1.18  christos 		    &targ) == -1) {
    639  1.18  christos 			fmprintf(stderr, "%s: invalid target\n", argv[argc - 1]);
    640  1.18  christos 			++errs;
    641  1.18  christos 			goto out;
    642  1.18  christos 		}
    643   1.1  christos 	}
    644   1.1  christos 	if (tuser != NULL && !okname(tuser)) {
    645  1.18  christos 		++errs;
    646  1.18  christos 		goto out;
    647   1.1  christos 	}
    648   1.1  christos 
    649  1.18  christos 	/* Parse source files */
    650   1.1  christos 	for (i = 0; i < argc - 1; i++) {
    651  1.18  christos 		free(suser);
    652  1.18  christos 		free(host);
    653  1.19  christos 		free(__UNCONST(src));
    654  1.18  christos 		r = parse_scp_uri(argv[i], &suser, &host, &sport, &src);
    655  1.18  christos 		if (r == -1) {
    656  1.18  christos 			fmprintf(stderr, "%s: invalid uri\n", argv[i]);
    657  1.18  christos 			++errs;
    658  1.18  christos 			continue;
    659  1.18  christos 		}
    660  1.18  christos 		if (r != 0) {
    661  1.18  christos 			parse_user_host_path(argv[i], &suser, &host, &src);
    662  1.18  christos 		}
    663  1.18  christos 		if (suser != NULL && !okname(suser)) {
    664  1.18  christos 			++errs;
    665  1.18  christos 			continue;
    666  1.18  christos 		}
    667  1.18  christos 		if (host && throughlocal) {	/* extended remote to remote */
    668   1.7  christos 			xasprintf(&bp, "%s -f %s%s", cmd,
    669   1.7  christos 			    *src == '-' ? "-- " : "", src);
    670  1.18  christos 			if (do_cmd(host, suser, sport, bp, &remin, &remout) < 0)
    671   1.5  christos 				exit(1);
    672   1.9  christos 			free(bp);
    673   1.7  christos 			xasprintf(&bp, "%s -t %s%s", cmd,
    674   1.7  christos 			    *targ == '-' ? "-- " : "", targ);
    675  1.18  christos 			if (do_cmd2(thost, tuser, tport, bp, remin, remout) < 0)
    676   1.5  christos 				exit(1);
    677   1.9  christos 			free(bp);
    678   1.5  christos 			(void) close(remin);
    679   1.5  christos 			(void) close(remout);
    680   1.5  christos 			remin = remout = -1;
    681  1.18  christos 		} else if (host) {	/* standard remote to remote */
    682  1.18  christos 			if (tport != -1 && tport != SSH_DEFAULT_PORT) {
    683  1.18  christos 				/* This would require the remote support URIs */
    684  1.18  christos 				fatal("target port not supported with two "
    685  1.18  christos 				    "remote hosts without the -3 option");
    686  1.18  christos 			}
    687  1.18  christos 
    688   1.1  christos 			freeargs(&alist);
    689   1.1  christos 			addargs(&alist, "%s", ssh_program);
    690   1.1  christos 			addargs(&alist, "-x");
    691   1.5  christos 			addargs(&alist, "-oClearAllForwardings=yes");
    692   1.1  christos 			addargs(&alist, "-n");
    693   1.5  christos 			for (j = 0; j < remote_remote_args.num; j++) {
    694   1.5  christos 				addargs(&alist, "%s",
    695   1.5  christos 				    remote_remote_args.list[j]);
    696   1.5  christos 			}
    697  1.18  christos 			if (sport != -1) {
    698  1.18  christos 				addargs(&alist, "-p");
    699  1.18  christos 				addargs(&alist, "%d", sport);
    700  1.18  christos 			}
    701  1.18  christos 			if (suser) {
    702   1.1  christos 				addargs(&alist, "-l");
    703   1.1  christos 				addargs(&alist, "%s", suser);
    704   1.1  christos 			}
    705   1.4      adam 			addargs(&alist, "--");
    706   1.1  christos 			addargs(&alist, "%s", host);
    707   1.1  christos 			addargs(&alist, "%s", cmd);
    708   1.1  christos 			addargs(&alist, "%s", src);
    709   1.1  christos 			addargs(&alist, "%s%s%s:%s",
    710   1.1  christos 			    tuser ? tuser : "", tuser ? "@" : "",
    711   1.1  christos 			    thost, targ);
    712   1.1  christos 			if (do_local_cmd(&alist) != 0)
    713   1.1  christos 				errs = 1;
    714   1.1  christos 		} else {	/* local to remote */
    715   1.1  christos 			if (remin == -1) {
    716   1.7  christos 				xasprintf(&bp, "%s -t %s%s", cmd,
    717   1.7  christos 				    *targ == '-' ? "-- " : "", targ);
    718  1.18  christos 				if (do_cmd(thost, tuser, tport, bp, &remin,
    719   1.1  christos 				    &remout) < 0)
    720   1.1  christos 					exit(1);
    721   1.1  christos 				if (response() < 0)
    722   1.1  christos 					exit(1);
    723   1.9  christos 				free(bp);
    724   1.1  christos 			}
    725   1.1  christos 			source(1, argv + i);
    726   1.1  christos 		}
    727   1.1  christos 	}
    728  1.18  christos out:
    729  1.18  christos 	free(tuser);
    730  1.18  christos 	free(thost);
    731  1.19  christos 	free(__UNCONST(targ));
    732  1.18  christos 	free(suser);
    733  1.18  christos 	free(host);
    734  1.19  christos 	free(__UNCONST(src));
    735   1.1  christos }
    736   1.1  christos 
    737   1.6     joerg static void
    738   1.1  christos tolocal(int argc, char **argv)
    739   1.1  christos {
    740  1.19  christos 	char *bp, *host = NULL, *suser = NULL;
    741  1.19  christos 	const char *src = NULL;
    742   1.1  christos 	arglist alist;
    743  1.18  christos 	int i, r, sport = -1;
    744   1.1  christos 
    745   1.1  christos 	memset(&alist, '\0', sizeof(alist));
    746   1.1  christos 	alist.list = NULL;
    747   1.1  christos 
    748   1.1  christos 	for (i = 0; i < argc - 1; i++) {
    749  1.18  christos 		free(suser);
    750  1.18  christos 		free(host);
    751  1.19  christos 		free(__UNCONST(src));
    752  1.18  christos 		r = parse_scp_uri(argv[i], &suser, &host, &sport, &src);
    753  1.18  christos 		if (r == -1) {
    754  1.18  christos 			fmprintf(stderr, "%s: invalid uri\n", argv[i]);
    755  1.18  christos 			++errs;
    756  1.18  christos 			continue;
    757  1.18  christos 		}
    758  1.18  christos 		if (r != 0)
    759  1.18  christos 			parse_user_host_path(argv[i], &suser, &host, &src);
    760  1.18  christos 		if (suser != NULL && !okname(suser)) {
    761  1.18  christos 			++errs;
    762  1.18  christos 			continue;
    763  1.18  christos 		}
    764  1.18  christos 		if (!host) {	/* Local to local. */
    765   1.1  christos 			freeargs(&alist);
    766   1.1  christos 			addargs(&alist, "%s", _PATH_CP);
    767   1.1  christos 			if (iamrecursive)
    768   1.1  christos 				addargs(&alist, "-r");
    769   1.1  christos 			if (pflag)
    770   1.1  christos 				addargs(&alist, "-p");
    771   1.4      adam 			addargs(&alist, "--");
    772   1.1  christos 			addargs(&alist, "%s", argv[i]);
    773   1.1  christos 			addargs(&alist, "%s", argv[argc-1]);
    774   1.1  christos 			if (do_local_cmd(&alist))
    775   1.1  christos 				++errs;
    776   1.1  christos 			continue;
    777   1.1  christos 		}
    778  1.18  christos 		/* Remote to local. */
    779   1.7  christos 		xasprintf(&bp, "%s -f %s%s",
    780   1.7  christos 		    cmd, *src == '-' ? "-- " : "", src);
    781  1.18  christos 		if (do_cmd(host, suser, sport, bp, &remin, &remout) < 0) {
    782   1.9  christos 			free(bp);
    783   1.1  christos 			++errs;
    784   1.1  christos 			continue;
    785   1.1  christos 		}
    786   1.9  christos 		free(bp);
    787   1.1  christos 		sink(1, argv + argc - 1);
    788   1.1  christos 		(void) close(remin);
    789   1.1  christos 		remin = remout = -1;
    790   1.1  christos 	}
    791  1.18  christos 	free(suser);
    792  1.18  christos 	free(host);
    793  1.19  christos 	free(__UNCONST(src));
    794   1.1  christos }
    795   1.1  christos 
    796   1.1  christos void
    797   1.1  christos source(int argc, char **argv)
    798   1.1  christos {
    799   1.1  christos 	struct stat stb;
    800   1.1  christos 	static BUF buffer;
    801   1.1  christos 	BUF *bp;
    802   1.1  christos 	off_t i, statbytes;
    803  1.10  christos 	size_t amt, nr;
    804   1.1  christos 	int fd = -1, haderr, indx;
    805  1.11  christos 	char *last, *name, buf[16384], encname[PATH_MAX];
    806   1.1  christos 	int len;
    807   1.1  christos 
    808   1.1  christos 	for (indx = 0; indx < argc; ++indx) {
    809   1.2  christos 		fd = -1;
    810   1.1  christos 		name = argv[indx];
    811   1.1  christos 		statbytes = 0;
    812   1.1  christos 		len = strlen(name);
    813   1.1  christos 		while (len > 1 && name[len-1] == '/')
    814   1.1  christos 			name[--len] = '\0';
    815   1.1  christos 		if ((fd = open(name, O_RDONLY|O_NONBLOCK, 0)) < 0)
    816   1.1  christos 			goto syserr;
    817   1.1  christos 		if (strchr(name, '\n') != NULL) {
    818   1.3  stacktic 			strvisx(encname, name, len, VIS_NL);
    819   1.1  christos 			name = encname;
    820   1.1  christos 		}
    821   1.1  christos 		if (fstat(fd, &stb) < 0) {
    822   1.1  christos syserr:			run_err("%s: %s", name, strerror(errno));
    823   1.1  christos 			goto next;
    824   1.1  christos 		}
    825   1.1  christos 		if (stb.st_size < 0) {
    826   1.1  christos 			run_err("%s: %s", name, "Negative file size");
    827   1.1  christos 			goto next;
    828   1.1  christos 		}
    829   1.1  christos 		unset_nonblock(fd);
    830   1.1  christos 		switch (stb.st_mode & S_IFMT) {
    831   1.1  christos 		case S_IFREG:
    832   1.1  christos 			break;
    833   1.1  christos 		case S_IFDIR:
    834   1.1  christos 			if (iamrecursive) {
    835   1.1  christos 				rsource(name, &stb);
    836   1.1  christos 				goto next;
    837   1.1  christos 			}
    838   1.1  christos 			/* FALLTHROUGH */
    839   1.1  christos 		default:
    840   1.1  christos 			run_err("%s: not a regular file", name);
    841   1.1  christos 			goto next;
    842   1.1  christos 		}
    843   1.1  christos 		if ((last = strrchr(name, '/')) == NULL)
    844   1.1  christos 			last = name;
    845   1.1  christos 		else
    846   1.1  christos 			++last;
    847   1.1  christos 		curfile = last;
    848   1.1  christos 		if (pflag) {
    849   1.9  christos 			if (do_times(remout, verbose_mode, &stb) < 0)
    850   1.1  christos 				goto next;
    851   1.1  christos 		}
    852   1.1  christos #define	FILEMODEMASK	(S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
    853   1.1  christos 		snprintf(buf, sizeof buf, "C%04o %lld %s\n",
    854   1.1  christos 		    (u_int) (stb.st_mode & FILEMODEMASK),
    855   1.1  christos 		    (long long)stb.st_size, last);
    856  1.14  christos 		if (verbose_mode)
    857  1.14  christos 			fmprintf(stderr, "Sending file modes: %s", buf);
    858   1.1  christos 		(void) atomicio(vwrite, remout, buf, strlen(buf));
    859   1.1  christos 		if (response() < 0)
    860   1.1  christos 			goto next;
    861   1.1  christos 		if ((bp = allocbuf(&buffer, fd, COPY_BUFLEN)) == NULL) {
    862   1.1  christos next:			if (fd != -1) {
    863   1.1  christos 				(void) close(fd);
    864   1.1  christos 				fd = -1;
    865   1.1  christos 			}
    866   1.1  christos 			continue;
    867   1.1  christos 		}
    868   1.1  christos 		if (showprogress)
    869   1.1  christos 			start_progress_meter(curfile, stb.st_size, &statbytes);
    870   1.1  christos 		set_nonblock(remout);
    871   1.1  christos 		for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
    872   1.1  christos 			amt = bp->cnt;
    873   1.1  christos 			if (i + (off_t)amt > stb.st_size)
    874   1.1  christos 				amt = stb.st_size - i;
    875   1.1  christos 			if (!haderr) {
    876  1.10  christos 				if ((nr = atomicio(read, fd,
    877  1.10  christos 				    bp->buf, amt)) != amt) {
    878   1.1  christos 					haderr = errno;
    879  1.10  christos 					memset(bp->buf + nr, 0, amt - nr);
    880  1.10  christos 				}
    881   1.1  christos 			}
    882   1.1  christos 			/* Keep writing after error to retain sync */
    883   1.1  christos 			if (haderr) {
    884   1.1  christos 				(void)atomicio(vwrite, remout, bp->buf, amt);
    885  1.10  christos 				memset(bp->buf, 0, amt);
    886   1.1  christos 				continue;
    887   1.1  christos 			}
    888   1.5  christos 			if (atomicio6(vwrite, remout, bp->buf, amt, scpio,
    889   1.1  christos 			    &statbytes) != amt)
    890   1.1  christos 				haderr = errno;
    891   1.1  christos 		}
    892   1.1  christos 		unset_nonblock(remout);
    893   1.1  christos 
    894   1.1  christos 		if (fd != -1) {
    895   1.1  christos 			if (close(fd) < 0 && !haderr)
    896   1.1  christos 				haderr = errno;
    897   1.1  christos 			fd = -1;
    898   1.1  christos 		}
    899   1.1  christos 		if (!haderr)
    900   1.5  christos 			(void) atomicio(vwrite, remout, empty, 1);
    901   1.1  christos 		else
    902   1.1  christos 			run_err("%s: %s", name, strerror(haderr));
    903   1.1  christos 		(void) response();
    904  1.14  christos 		if (showprogress)
    905  1.14  christos 			stop_progress_meter();
    906   1.1  christos 	}
    907   1.1  christos }
    908   1.1  christos 
    909   1.1  christos void
    910   1.1  christos rsource(char *name, struct stat *statp)
    911   1.1  christos {
    912   1.1  christos 	DIR *dirp;
    913   1.1  christos 	struct dirent *dp;
    914  1.22       mrg 	char *last, *vect[1], path[PATH_MAX + 20];
    915   1.1  christos 
    916   1.1  christos 	if (!(dirp = opendir(name))) {
    917   1.1  christos 		run_err("%s: %s", name, strerror(errno));
    918   1.1  christos 		return;
    919   1.1  christos 	}
    920   1.1  christos 	last = strrchr(name, '/');
    921  1.13  christos 	if (last == NULL)
    922   1.1  christos 		last = name;
    923   1.1  christos 	else
    924   1.1  christos 		last++;
    925   1.1  christos 	if (pflag) {
    926   1.9  christos 		if (do_times(remout, verbose_mode, statp) < 0) {
    927   1.1  christos 			closedir(dirp);
    928   1.1  christos 			return;
    929   1.1  christos 		}
    930   1.1  christos 	}
    931   1.1  christos 	(void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
    932   1.1  christos 	    (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
    933   1.1  christos 	if (verbose_mode)
    934  1.14  christos 		fmprintf(stderr, "Entering directory: %s", path);
    935   1.1  christos 	(void) atomicio(vwrite, remout, path, strlen(path));
    936   1.1  christos 	if (response() < 0) {
    937   1.1  christos 		closedir(dirp);
    938   1.1  christos 		return;
    939   1.1  christos 	}
    940   1.1  christos 	while ((dp = readdir(dirp)) != NULL) {
    941   1.1  christos 		if (dp->d_ino == 0)
    942   1.1  christos 			continue;
    943   1.1  christos 		if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
    944   1.1  christos 			continue;
    945   1.1  christos 		if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
    946   1.1  christos 			run_err("%s/%s: name too long", name, dp->d_name);
    947   1.1  christos 			continue;
    948   1.1  christos 		}
    949   1.1  christos 		(void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
    950   1.1  christos 		vect[0] = path;
    951   1.1  christos 		source(1, vect);
    952   1.1  christos 	}
    953   1.1  christos 	(void) closedir(dirp);
    954   1.5  christos 	(void) atomicio(vwrite, remout, __UNCONST("E\n"), 2);
    955   1.1  christos 	(void) response();
    956   1.1  christos }
    957   1.1  christos 
    958  1.17  christos #define TYPE_OVERFLOW(type, val) \
    959  1.17  christos 	((sizeof(type) == 4 && (val) > INT32_MAX) || \
    960  1.17  christos 	 (sizeof(type) == 8 && (val) > INT64_MAX) || \
    961  1.17  christos 	 (sizeof(type) != 4 && sizeof(type) != 8))
    962  1.17  christos 
    963   1.1  christos void
    964   1.1  christos sink(int argc, char **argv)
    965   1.1  christos {
    966   1.1  christos 	static BUF buffer;
    967   1.1  christos 	struct stat stb;
    968   1.1  christos 	enum {
    969   1.1  christos 		YES, NO, DISPLAYED
    970   1.1  christos 	} wrerr;
    971   1.1  christos 	BUF *bp;
    972   1.1  christos 	off_t i;
    973   1.1  christos 	size_t j, count;
    974   1.1  christos 	int amt, exists, first, ofd;
    975   1.1  christos 	mode_t mode, omode, mask;
    976   1.1  christos 	off_t size, statbytes;
    977   1.9  christos 	unsigned long long ull;
    978   1.1  christos 	int setimes, targisdir, wrerrno = 0;
    979  1.14  christos 	char ch, *cp, *np, *targ, *vect[1], buf[2048], visbuf[2048];
    980   1.5  christos 	const char *why;
    981   1.1  christos 	struct timeval tv[2];
    982   1.1  christos 
    983   1.1  christos #define	atime	tv[0]
    984   1.1  christos #define	mtime	tv[1]
    985   1.1  christos #define	SCREWUP(str)	{ why = str; goto screwup; }
    986   1.1  christos 
    987  1.17  christos 	if (TYPE_OVERFLOW(time_t, 0) || TYPE_OVERFLOW(off_t, 0))
    988  1.17  christos 		SCREWUP("Unexpected off_t/time_t size");
    989  1.17  christos 
    990   1.1  christos 	setimes = targisdir = 0;
    991   1.1  christos 	mask = umask(0);
    992   1.1  christos 	if (!pflag)
    993   1.1  christos 		(void) umask(mask);
    994   1.1  christos 	if (argc != 1) {
    995   1.1  christos 		run_err("ambiguous target");
    996   1.1  christos 		exit(1);
    997   1.1  christos 	}
    998   1.1  christos 	targ = *argv;
    999   1.1  christos 	if (targetshouldbedirectory)
   1000   1.1  christos 		verifydir(targ);
   1001   1.1  christos 
   1002   1.5  christos 	(void) atomicio(vwrite, remout, empty, 1);
   1003   1.1  christos 	if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
   1004   1.1  christos 		targisdir = 1;
   1005   1.1  christos 	for (first = 1;; first = 0) {
   1006   1.1  christos 		cp = buf;
   1007   1.1  christos 		if (atomicio(read, remin, cp, 1) != 1)
   1008   1.1  christos 			return;
   1009   1.1  christos 		if (*cp++ == '\n')
   1010   1.1  christos 			SCREWUP("unexpected <newline>");
   1011   1.1  christos 		do {
   1012   1.1  christos 			if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
   1013   1.1  christos 				SCREWUP("lost connection");
   1014   1.1  christos 			*cp++ = ch;
   1015   1.1  christos 		} while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
   1016   1.1  christos 		*cp = 0;
   1017   1.1  christos 		if (verbose_mode)
   1018  1.14  christos 			fmprintf(stderr, "Sink: %s", buf);
   1019   1.1  christos 
   1020   1.1  christos 		if (buf[0] == '\01' || buf[0] == '\02') {
   1021  1.14  christos 			if (iamremote == 0) {
   1022  1.14  christos 				(void) snmprintf(visbuf, sizeof(visbuf),
   1023  1.14  christos 				    NULL, "%s", buf + 1);
   1024   1.1  christos 				(void) atomicio(vwrite, STDERR_FILENO,
   1025  1.14  christos 				    visbuf, strlen(visbuf));
   1026  1.14  christos 			}
   1027   1.1  christos 			if (buf[0] == '\02')
   1028   1.1  christos 				exit(1);
   1029   1.1  christos 			++errs;
   1030   1.1  christos 			continue;
   1031   1.1  christos 		}
   1032   1.1  christos 		if (buf[0] == 'E') {
   1033   1.5  christos 			(void) atomicio(vwrite, remout, empty, 1);
   1034   1.1  christos 			return;
   1035   1.1  christos 		}
   1036   1.1  christos 		if (ch == '\n')
   1037   1.1  christos 			*--cp = 0;
   1038   1.1  christos 
   1039   1.1  christos 		cp = buf;
   1040   1.1  christos 		if (*cp == 'T') {
   1041   1.1  christos 			setimes++;
   1042   1.1  christos 			cp++;
   1043   1.9  christos 			if (!isdigit((unsigned char)*cp))
   1044   1.9  christos 				SCREWUP("mtime.sec not present");
   1045   1.9  christos 			ull = strtoull(cp, &cp, 10);
   1046   1.1  christos 			if (!cp || *cp++ != ' ')
   1047   1.1  christos 				SCREWUP("mtime.sec not delimited");
   1048  1.17  christos 			if (TYPE_OVERFLOW(time_t, ull))
   1049   1.9  christos 				setimes = 0;	/* out of range */
   1050   1.9  christos 			mtime.tv_sec = ull;
   1051   1.1  christos 			mtime.tv_usec = strtol(cp, &cp, 10);
   1052   1.9  christos 			if (!cp || *cp++ != ' ' || mtime.tv_usec < 0 ||
   1053   1.9  christos 			    mtime.tv_usec > 999999)
   1054   1.1  christos 				SCREWUP("mtime.usec not delimited");
   1055   1.9  christos 			if (!isdigit((unsigned char)*cp))
   1056   1.9  christos 				SCREWUP("atime.sec not present");
   1057   1.9  christos 			ull = strtoull(cp, &cp, 10);
   1058   1.1  christos 			if (!cp || *cp++ != ' ')
   1059   1.1  christos 				SCREWUP("atime.sec not delimited");
   1060  1.17  christos 			if (TYPE_OVERFLOW(time_t, ull))
   1061   1.9  christos 				setimes = 0;	/* out of range */
   1062   1.9  christos 			atime.tv_sec = ull;
   1063   1.1  christos 			atime.tv_usec = strtol(cp, &cp, 10);
   1064   1.9  christos 			if (!cp || *cp++ != '\0' || atime.tv_usec < 0 ||
   1065   1.9  christos 			    atime.tv_usec > 999999)
   1066   1.1  christos 				SCREWUP("atime.usec not delimited");
   1067   1.5  christos 			(void) atomicio(vwrite, remout, empty, 1);
   1068   1.1  christos 			continue;
   1069   1.1  christos 		}
   1070   1.1  christos 		if (*cp != 'C' && *cp != 'D') {
   1071   1.1  christos 			/*
   1072   1.1  christos 			 * Check for the case "rcp remote:foo\* local:bar".
   1073   1.1  christos 			 * In this case, the line "No match." can be returned
   1074   1.1  christos 			 * by the shell before the rcp command on the remote is
   1075   1.1  christos 			 * executed so the ^Aerror_message convention isn't
   1076   1.1  christos 			 * followed.
   1077   1.1  christos 			 */
   1078   1.1  christos 			if (first) {
   1079   1.1  christos 				run_err("%s", cp);
   1080   1.1  christos 				exit(1);
   1081   1.1  christos 			}
   1082   1.1  christos 			SCREWUP("expected control record");
   1083   1.1  christos 		}
   1084   1.1  christos 		mode = 0;
   1085   1.1  christos 		for (++cp; cp < buf + 5; cp++) {
   1086   1.1  christos 			if (*cp < '0' || *cp > '7')
   1087   1.1  christos 				SCREWUP("bad mode");
   1088   1.1  christos 			mode = (mode << 3) | (*cp - '0');
   1089   1.1  christos 		}
   1090  1.20  christos 		if (!pflag)
   1091  1.20  christos 			mode &= ~mask;
   1092   1.1  christos 		if (*cp++ != ' ')
   1093   1.1  christos 			SCREWUP("mode not delimited");
   1094   1.1  christos 
   1095  1.17  christos 		if (!isdigit((unsigned char)*cp))
   1096  1.17  christos 			SCREWUP("size not present");
   1097  1.17  christos 		ull = strtoull(cp, &cp, 10);
   1098  1.17  christos 		if (!cp || *cp++ != ' ')
   1099   1.1  christos 			SCREWUP("size not delimited");
   1100  1.17  christos 		if (TYPE_OVERFLOW(off_t, ull))
   1101  1.17  christos 			SCREWUP("size out of range");
   1102  1.17  christos 		size = (off_t)ull;
   1103  1.17  christos 
   1104   1.1  christos 		if ((strchr(cp, '/') != NULL) || (strcmp(cp, "..") == 0)) {
   1105   1.1  christos 			run_err("error: unexpected filename: %s", cp);
   1106   1.1  christos 			exit(1);
   1107   1.1  christos 		}
   1108   1.1  christos 		if (targisdir) {
   1109   1.1  christos 			static char *namebuf;
   1110   1.1  christos 			static size_t cursize;
   1111   1.1  christos 			size_t need;
   1112   1.1  christos 
   1113   1.1  christos 			need = strlen(targ) + strlen(cp) + 250;
   1114   1.1  christos 			if (need > cursize) {
   1115   1.9  christos 				free(namebuf);
   1116   1.1  christos 				namebuf = xmalloc(need);
   1117   1.1  christos 				cursize = need;
   1118   1.1  christos 			}
   1119   1.1  christos 			(void) snprintf(namebuf, need, "%s%s%s", targ,
   1120   1.1  christos 			    strcmp(targ, "/") ? "/" : "", cp);
   1121   1.1  christos 			np = namebuf;
   1122   1.1  christos 		} else
   1123   1.1  christos 			np = targ;
   1124   1.1  christos 		curfile = cp;
   1125   1.1  christos 		exists = stat(np, &stb) == 0;
   1126   1.1  christos 		if (buf[0] == 'D') {
   1127   1.1  christos 			int mod_flag = pflag;
   1128   1.1  christos 			if (!iamrecursive)
   1129   1.1  christos 				SCREWUP("received directory without -r");
   1130   1.1  christos 			if (exists) {
   1131   1.1  christos 				if (!S_ISDIR(stb.st_mode)) {
   1132   1.1  christos 					errno = ENOTDIR;
   1133   1.1  christos 					goto bad;
   1134   1.1  christos 				}
   1135   1.1  christos 				if (pflag)
   1136   1.1  christos 					(void) chmod(np, mode);
   1137   1.1  christos 			} else {
   1138   1.1  christos 				/* Handle copying from a read-only
   1139   1.1  christos 				   directory */
   1140   1.1  christos 				mod_flag = 1;
   1141   1.1  christos 				if (mkdir(np, mode | S_IRWXU) < 0)
   1142   1.1  christos 					goto bad;
   1143   1.1  christos 			}
   1144   1.1  christos 			vect[0] = xstrdup(np);
   1145   1.1  christos 			sink(1, vect);
   1146   1.1  christos 			if (setimes) {
   1147   1.1  christos 				setimes = 0;
   1148   1.1  christos 				if (utimes(vect[0], tv) < 0)
   1149   1.1  christos 					run_err("%s: set times: %s",
   1150   1.1  christos 					    vect[0], strerror(errno));
   1151   1.1  christos 			}
   1152   1.1  christos 			if (mod_flag)
   1153   1.1  christos 				(void) chmod(vect[0], mode);
   1154   1.9  christos 			free(vect[0]);
   1155   1.1  christos 			continue;
   1156   1.1  christos 		}
   1157   1.1  christos 		omode = mode;
   1158   1.9  christos 		mode |= S_IWUSR;
   1159   1.1  christos 		if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
   1160   1.1  christos bad:			run_err("%s: %s", np, strerror(errno));
   1161   1.1  christos 			continue;
   1162   1.1  christos 		}
   1163   1.5  christos 		(void) atomicio(vwrite, remout, empty, 1);
   1164   1.1  christos 		if ((bp = allocbuf(&buffer, ofd, COPY_BUFLEN)) == NULL) {
   1165   1.1  christos 			(void) close(ofd);
   1166   1.1  christos 			continue;
   1167   1.1  christos 		}
   1168   1.1  christos 		cp = bp->buf;
   1169   1.1  christos 		wrerr = NO;
   1170   1.1  christos 
   1171   1.1  christos 		statbytes = 0;
   1172   1.1  christos 		if (showprogress)
   1173   1.1  christos 			start_progress_meter(curfile, size, &statbytes);
   1174   1.1  christos 		set_nonblock(remin);
   1175   1.1  christos 		for (count = i = 0; i < size; i += bp->cnt) {
   1176   1.1  christos 			amt = bp->cnt;
   1177   1.1  christos 			if (i + amt > size)
   1178   1.1  christos 				amt = size - i;
   1179   1.1  christos 			count += amt;
   1180   1.1  christos 			do {
   1181   1.5  christos 				j = atomicio6(read, remin, cp, amt,
   1182   1.5  christos 				    scpio, &statbytes);
   1183   1.1  christos 				if (j == 0) {
   1184   1.1  christos 					run_err("%s", j != EPIPE ?
   1185   1.1  christos 					    strerror(errno) :
   1186   1.1  christos 					    "dropped connection");
   1187   1.1  christos 					exit(1);
   1188   1.1  christos 				}
   1189   1.1  christos 				amt -= j;
   1190   1.1  christos 				cp += j;
   1191   1.1  christos 			} while (amt > 0);
   1192   1.1  christos 
   1193   1.1  christos 			if (count == bp->cnt) {
   1194   1.1  christos 				/* Keep reading so we stay sync'd up. */
   1195   1.1  christos 				if (wrerr == NO) {
   1196   1.1  christos 					if (atomicio(vwrite, ofd, bp->buf,
   1197   1.1  christos 					    count) != count) {
   1198   1.1  christos 						wrerr = YES;
   1199   1.1  christos 						wrerrno = errno;
   1200   1.1  christos 					}
   1201   1.1  christos 				}
   1202   1.1  christos 				count = 0;
   1203   1.1  christos 				cp = bp->buf;
   1204   1.1  christos 			}
   1205   1.1  christos 		}
   1206   1.1  christos 		unset_nonblock(remin);
   1207   1.1  christos 		if (count != 0 && wrerr == NO &&
   1208   1.1  christos 		    atomicio(vwrite, ofd, bp->buf, count) != count) {
   1209   1.1  christos 			wrerr = YES;
   1210   1.1  christos 			wrerrno = errno;
   1211   1.1  christos 		}
   1212   1.1  christos 		if (wrerr == NO && (!exists || S_ISREG(stb.st_mode)) &&
   1213   1.1  christos 		    ftruncate(ofd, size) != 0) {
   1214   1.1  christos 			run_err("%s: truncate: %s", np, strerror(errno));
   1215   1.1  christos 			wrerr = DISPLAYED;
   1216   1.1  christos 		}
   1217   1.1  christos 		if (pflag) {
   1218   1.1  christos 			if (exists || omode != mode)
   1219   1.1  christos 				if (fchmod(ofd, omode)) {
   1220   1.1  christos 					run_err("%s: set mode: %s",
   1221   1.1  christos 					    np, strerror(errno));
   1222   1.1  christos 					wrerr = DISPLAYED;
   1223   1.1  christos 				}
   1224   1.1  christos 		} else {
   1225   1.1  christos 			if (!exists && omode != mode)
   1226   1.1  christos 				if (fchmod(ofd, omode & ~mask)) {
   1227   1.1  christos 					run_err("%s: set mode: %s",
   1228   1.1  christos 					    np, strerror(errno));
   1229   1.1  christos 					wrerr = DISPLAYED;
   1230   1.1  christos 				}
   1231   1.1  christos 		}
   1232   1.1  christos 		if (close(ofd) == -1) {
   1233   1.1  christos 			wrerr = YES;
   1234   1.1  christos 			wrerrno = errno;
   1235   1.1  christos 		}
   1236   1.1  christos 		(void) response();
   1237  1.14  christos 		if (showprogress)
   1238  1.14  christos 			stop_progress_meter();
   1239   1.1  christos 		if (setimes && wrerr == NO) {
   1240   1.1  christos 			setimes = 0;
   1241   1.1  christos 			if (utimes(np, tv) < 0) {
   1242   1.1  christos 				run_err("%s: set times: %s",
   1243   1.1  christos 				    np, strerror(errno));
   1244   1.1  christos 				wrerr = DISPLAYED;
   1245   1.1  christos 			}
   1246   1.1  christos 		}
   1247   1.1  christos 		switch (wrerr) {
   1248   1.1  christos 		case YES:
   1249   1.1  christos 			run_err("%s: %s", np, strerror(wrerrno));
   1250   1.1  christos 			break;
   1251   1.1  christos 		case NO:
   1252   1.5  christos 			(void) atomicio(vwrite, remout, empty, 1);
   1253   1.1  christos 			break;
   1254   1.1  christos 		case DISPLAYED:
   1255   1.1  christos 			break;
   1256   1.1  christos 		}
   1257   1.1  christos 	}
   1258   1.1  christos screwup:
   1259   1.1  christos 	run_err("protocol error: %s", why);
   1260   1.1  christos 	exit(1);
   1261   1.1  christos }
   1262   1.1  christos 
   1263   1.1  christos int
   1264   1.1  christos response(void)
   1265   1.1  christos {
   1266  1.14  christos 	char ch, *cp, resp, rbuf[2048], visbuf[2048];
   1267   1.1  christos 
   1268   1.1  christos 	if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
   1269   1.1  christos 		lostconn(0);
   1270   1.1  christos 
   1271   1.1  christos 	cp = rbuf;
   1272   1.1  christos 	switch (resp) {
   1273   1.1  christos 	case 0:		/* ok */
   1274   1.1  christos 		return (0);
   1275   1.1  christos 	default:
   1276   1.1  christos 		*cp++ = resp;
   1277   1.1  christos 		/* FALLTHROUGH */
   1278   1.1  christos 	case 1:		/* error, followed by error msg */
   1279   1.1  christos 	case 2:		/* fatal error, "" */
   1280   1.1  christos 		do {
   1281   1.1  christos 			if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
   1282   1.1  christos 				lostconn(0);
   1283   1.1  christos 			*cp++ = ch;
   1284   1.1  christos 		} while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
   1285   1.1  christos 
   1286  1.14  christos 		if (!iamremote) {
   1287  1.14  christos 			cp[-1] = '\0';
   1288  1.14  christos 			(void) snmprintf(visbuf, sizeof(visbuf),
   1289  1.14  christos 			    NULL, "%s\n", rbuf);
   1290  1.14  christos 			(void) atomicio(vwrite, STDERR_FILENO,
   1291  1.14  christos 			    visbuf, strlen(visbuf));
   1292  1.14  christos 		}
   1293   1.1  christos 		++errs;
   1294   1.1  christos 		if (resp == 1)
   1295   1.1  christos 			return (-1);
   1296   1.1  christos 		exit(1);
   1297   1.1  christos 	}
   1298   1.1  christos 	/* NOTREACHED */
   1299   1.1  christos }
   1300   1.1  christos 
   1301   1.1  christos void
   1302   1.1  christos usage(void)
   1303   1.1  christos {
   1304   1.1  christos 	(void) fprintf(stderr,
   1305  1.17  christos 	    "usage: scp [-346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
   1306  1.18  christos 	    "           [-l limit] [-o ssh_option] [-P port] [-S program] source ... target\n");
   1307   1.1  christos 	exit(1);
   1308   1.1  christos }
   1309   1.1  christos 
   1310   1.1  christos void
   1311   1.1  christos run_err(const char *fmt,...)
   1312   1.1  christos {
   1313   1.1  christos 	static FILE *fp;
   1314   1.1  christos 	va_list ap;
   1315   1.1  christos 
   1316   1.1  christos 	++errs;
   1317   1.1  christos 	if (fp != NULL || (remout != -1 && (fp = fdopen(remout, "w")))) {
   1318   1.1  christos 		(void) fprintf(fp, "%c", 0x01);
   1319   1.1  christos 		(void) fprintf(fp, "scp: ");
   1320   1.1  christos 		va_start(ap, fmt);
   1321   1.1  christos 		(void) vfprintf(fp, fmt, ap);
   1322   1.1  christos 		va_end(ap);
   1323   1.1  christos 		(void) fprintf(fp, "\n");
   1324   1.1  christos 		(void) fflush(fp);
   1325   1.1  christos 	}
   1326   1.1  christos 
   1327   1.1  christos 	if (!iamremote) {
   1328   1.1  christos 		va_start(ap, fmt);
   1329  1.14  christos 		vfmprintf(stderr, fmt, ap);
   1330   1.1  christos 		va_end(ap);
   1331   1.1  christos 		fprintf(stderr, "\n");
   1332   1.1  christos 	}
   1333   1.1  christos }
   1334   1.1  christos 
   1335   1.1  christos void
   1336   1.1  christos verifydir(char *cp)
   1337   1.1  christos {
   1338   1.1  christos 	struct stat stb;
   1339   1.1  christos 
   1340   1.1  christos 	if (!stat(cp, &stb)) {
   1341   1.1  christos 		if (S_ISDIR(stb.st_mode))
   1342   1.1  christos 			return;
   1343   1.1  christos 		errno = ENOTDIR;
   1344   1.1  christos 	}
   1345   1.1  christos 	run_err("%s: %s", cp, strerror(errno));
   1346   1.1  christos 	killchild(0);
   1347   1.1  christos }
   1348   1.1  christos 
   1349   1.1  christos int
   1350   1.1  christos okname(char *cp0)
   1351   1.1  christos {
   1352   1.1  christos 	int c;
   1353   1.1  christos 	char *cp;
   1354   1.1  christos 
   1355   1.1  christos 	cp = cp0;
   1356   1.1  christos 	do {
   1357   1.1  christos 		c = (int)*cp;
   1358   1.1  christos 		if (c & 0200)
   1359   1.1  christos 			goto bad;
   1360  1.10  christos 		if (!isalpha(c) && !isdigit((unsigned char)c)) {
   1361   1.1  christos 			switch (c) {
   1362   1.1  christos 			case '\'':
   1363   1.1  christos 			case '"':
   1364   1.1  christos 			case '`':
   1365   1.1  christos 			case ' ':
   1366   1.1  christos 			case '#':
   1367   1.1  christos 				goto bad;
   1368   1.1  christos 			default:
   1369   1.1  christos 				break;
   1370   1.1  christos 			}
   1371   1.1  christos 		}
   1372   1.1  christos 	} while (*++cp);
   1373   1.1  christos 	return (1);
   1374   1.1  christos 
   1375  1.14  christos bad:	fmprintf(stderr, "%s: invalid user name\n", cp0);
   1376   1.1  christos 	return (0);
   1377   1.1  christos }
   1378   1.1  christos 
   1379   1.1  christos BUF *
   1380   1.1  christos allocbuf(BUF *bp, int fd, int blksize)
   1381   1.1  christos {
   1382   1.1  christos 	size_t size;
   1383   1.1  christos 	struct stat stb;
   1384   1.1  christos 
   1385   1.1  christos 	if (fstat(fd, &stb) < 0) {
   1386   1.1  christos 		run_err("fstat: %s", strerror(errno));
   1387   1.1  christos 		return (0);
   1388   1.1  christos 	}
   1389  1.15  christos 	size = ROUNDUP(stb.st_blksize, blksize);
   1390   1.1  christos 	if (size == 0)
   1391   1.1  christos 		size = blksize;
   1392   1.1  christos 	if (bp->cnt >= size)
   1393   1.1  christos 		return (bp);
   1394  1.17  christos 	bp->buf = xrecallocarray(bp->buf, bp->cnt, size, 1);
   1395   1.1  christos 	bp->cnt = size;
   1396   1.1  christos 	return (bp);
   1397   1.1  christos }
   1398   1.1  christos 
   1399   1.6     joerg static void
   1400   1.1  christos lostconn(int signo)
   1401   1.1  christos {
   1402   1.1  christos 	if (!iamremote)
   1403   1.9  christos 		(void)write(STDERR_FILENO, "lost connection\n", 16);
   1404   1.1  christos 	if (signo)
   1405   1.1  christos 		_exit(1);
   1406   1.1  christos 	else
   1407   1.1  christos 		exit(1);
   1408   1.1  christos }
   1409