Deleted Added
full compact
scp.c (114426) scp.c (124211)
1/*
2 * scp - secure remote copy. This is basically patched BSD rcp which
3 * uses ssh to do the data transfer (instead of using rcmd).
4 *
5 * NOTE: This version should NOT be suid root. (This uses ssh to
6 * do the transfer and ssh has the necessary privileges.)
7 *
8 * 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi>

--- 38 unchanged lines hidden (view full) ---

47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
49 * are met:
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
1/*
2 * scp - secure remote copy. This is basically patched BSD rcp which
3 * uses ssh to do the data transfer (instead of using rcmd).
4 *
5 * NOTE: This version should NOT be suid root. (This uses ssh to
6 * do the transfer and ssh has the necessary privileges.)
7 *
8 * 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi>

--- 38 unchanged lines hidden (view full) ---

47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
49 * are met:
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
55 * 3. All advertising materials mentioning features or use of this software
56 * must display the following acknowledgement:
57 * This product includes software developed by the University of
58 * California, Berkeley and its contributors.
59 * 4. Neither the name of the University nor the names of its contributors
55 * 3. Neither the name of the University nor the names of its contributors
60 * may be used to endorse or promote products derived from this software
61 * without specific prior written permission.
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 * SUCH DAMAGE.
74 *
75 */
76
77#include "includes.h"
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 */
72
73#include "includes.h"
78RCSID("$OpenBSD: scp.c,v 1.102 2003/03/05 22:33:43 markus Exp $");
74RCSID("$OpenBSD: scp.c,v 1.108 2003/07/18 01:54:25 deraadt Exp $");
79
80#include "xmalloc.h"
81#include "atomicio.h"
82#include "pathnames.h"
83#include "log.h"
84#include "misc.h"
85#include "progressmeter.h"
86

--- 19 unchanged lines hidden (view full) ---

106
107/* This is set to zero if the progressmeter is not desired. */
108int showprogress = 1;
109
110/* This is the program to execute for the secured connection. ("ssh" or -S) */
111char *ssh_program = _PATH_SSH_PROGRAM;
112
113/* This is used to store the pid of ssh_program */
75
76#include "xmalloc.h"
77#include "atomicio.h"
78#include "pathnames.h"
79#include "log.h"
80#include "misc.h"
81#include "progressmeter.h"
82

--- 19 unchanged lines hidden (view full) ---

102
103/* This is set to zero if the progressmeter is not desired. */
104int showprogress = 1;
105
106/* This is the program to execute for the secured connection. ("ssh" or -S) */
107char *ssh_program = _PATH_SSH_PROGRAM;
108
109/* This is used to store the pid of ssh_program */
114pid_t do_cmd_pid;
110pid_t do_cmd_pid = -1;
115
111
112static void
113killchild(int signo)
114{
115 if (do_cmd_pid > 1)
116 kill(do_cmd_pid, signo);
117
118 _exit(1);
119}
120
116/*
117 * This function executes the given command as the specified user on the
118 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
119 * assigns the input and output file descriptors on success.
120 */
121
122int
123do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)

--- 17 unchanged lines hidden (view full) ---

141 fatal("pipe: %s", strerror(errno));
142 if (pipe(pout) < 0)
143 fatal("pipe: %s", strerror(errno));
144
145 /* Free the reserved descriptors. */
146 close(reserved[0]);
147 close(reserved[1]);
148
121/*
122 * This function executes the given command as the specified user on the
123 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
124 * assigns the input and output file descriptors on success.
125 */
126
127int
128do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc)

--- 17 unchanged lines hidden (view full) ---

146 fatal("pipe: %s", strerror(errno));
147 if (pipe(pout) < 0)
148 fatal("pipe: %s", strerror(errno));
149
150 /* Free the reserved descriptors. */
151 close(reserved[0]);
152 close(reserved[1]);
153
149 /* For a child to execute the command on the remote host using ssh. */
154 /* Fork a child to execute the command on the remote host using ssh. */
150 do_cmd_pid = fork();
151 if (do_cmd_pid == 0) {
152 /* Child. */
153 close(pin[1]);
154 close(pout[0]);
155 dup2(pin[0], 0);
156 dup2(pout[1], 1);
157 close(pin[0]);

--- 11 unchanged lines hidden (view full) ---

169 } else if (do_cmd_pid == -1) {
170 fatal("fork: %s", strerror(errno));
171 }
172 /* Parent. Close the other side, and return the local side. */
173 close(pin[0]);
174 *fdout = pin[1];
175 close(pout[1]);
176 *fdin = pout[0];
155 do_cmd_pid = fork();
156 if (do_cmd_pid == 0) {
157 /* Child. */
158 close(pin[1]);
159 close(pout[0]);
160 dup2(pin[0], 0);
161 dup2(pout[1], 1);
162 close(pin[0]);

--- 11 unchanged lines hidden (view full) ---

174 } else if (do_cmd_pid == -1) {
175 fatal("fork: %s", strerror(errno));
176 }
177 /* Parent. Close the other side, and return the local side. */
178 close(pin[0]);
179 *fdout = pin[1];
180 close(pout[1]);
181 *fdin = pout[0];
182 signal(SIGTERM, killchild);
183 signal(SIGINT, killchild);
184 signal(SIGHUP, killchild);
177 return 0;
178}
179
180typedef struct {
181 int cnt;
182 char *buf;
183} BUF;
184

--- 16 unchanged lines hidden (view full) ---

201void rsource(char *, struct stat *);
202void sink(int, char *[]);
203void source(int, char *[]);
204void tolocal(int, char *[]);
205void toremote(char *, int, char *[]);
206void usage(void);
207
208int
185 return 0;
186}
187
188typedef struct {
189 int cnt;
190 char *buf;
191} BUF;
192

--- 16 unchanged lines hidden (view full) ---

209void rsource(char *, struct stat *);
210void sink(int, char *[]);
211void source(int, char *[]);
212void tolocal(int, char *[]);
213void toremote(char *, int, char *[]);
214void usage(void);
215
216int
209main(argc, argv)
210 int argc;
211 char *argv[];
217main(int argc, char **argv)
212{
213 int ch, fflag, tflag, status;
214 double speed;
215 char *targ, *endp;
216 extern char *optarg;
217 extern int optind;
218
218{
219 int ch, fflag, tflag, status;
220 double speed;
221 char *targ, *endp;
222 extern char *optarg;
223 extern int optind;
224
219 __progname = get_progname(argv[0]);
225 __progname = ssh_get_progname(argv[0]);
220
221 args.list = NULL;
222 addargs(&args, "ssh"); /* overwritten with ssh_program */
223 addargs(&args, "-x");
224 addargs(&args, "-oForwardAgent no");
225 addargs(&args, "-oClearAllForwardings yes");
226
227 fflag = tflag = 0;

--- 59 unchanged lines hidden (view full) ---

287 break;
288 default:
289 usage();
290 }
291 argc -= optind;
292 argv += optind;
293
294 if ((pwd = getpwuid(userid = getuid())) == NULL)
226
227 args.list = NULL;
228 addargs(&args, "ssh"); /* overwritten with ssh_program */
229 addargs(&args, "-x");
230 addargs(&args, "-oForwardAgent no");
231 addargs(&args, "-oClearAllForwardings yes");
232
233 fflag = tflag = 0;

--- 59 unchanged lines hidden (view full) ---

293 break;
294 default:
295 usage();
296 }
297 argc -= optind;
298 argv += optind;
299
300 if ((pwd = getpwuid(userid = getuid())) == NULL)
295 fatal("unknown user %d", (int) userid);
301 fatal("unknown user %u", (u_int) userid);
296
297 if (!isatty(STDERR_FILENO))
298 showprogress = 0;
299
300 remin = STDIN_FILENO;
301 remout = STDOUT_FILENO;
302
303 if (fflag) {

--- 44 unchanged lines hidden (view full) ---

348 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
349 errs = 1;
350 }
351 }
352 exit(errs != 0);
353}
354
355void
302
303 if (!isatty(STDERR_FILENO))
304 showprogress = 0;
305
306 remin = STDIN_FILENO;
307 remout = STDOUT_FILENO;
308
309 if (fflag) {

--- 44 unchanged lines hidden (view full) ---

354 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
355 errs = 1;
356 }
357 }
358 exit(errs != 0);
359}
360
361void
356toremote(targ, argc, argv)
357 char *targ, *argv[];
358 int argc;
362toremote(char *targ, int argc, char **argv)
359{
360 int i, len;
361 char *bp, *host, *src, *suser, *thost, *tuser;
362
363 *targ++ = 0;
364 if (*targ == 0)
365 targ = ".";
366

--- 71 unchanged lines hidden (view full) ---

438 (void) xfree(bp);
439 }
440 source(1, argv + i);
441 }
442 }
443}
444
445void
363{
364 int i, len;
365 char *bp, *host, *src, *suser, *thost, *tuser;
366
367 *targ++ = 0;
368 if (*targ == 0)
369 targ = ".";
370

--- 71 unchanged lines hidden (view full) ---

442 (void) xfree(bp);
443 }
444 source(1, argv + i);
445 }
446 }
447}
448
449void
446tolocal(argc, argv)
447 int argc;
448 char *argv[];
450tolocal(int argc, char **argv)
449{
450 int i, len;
451 char *bp, *host, *src, *suser;
452
453 for (i = 0; i < argc - 1; i++) {
454 if (!(src = colon(argv[i]))) { /* Local to local. */
455 len = strlen(_PATH_CP) + strlen(argv[i]) +
456 strlen(argv[argc - 1]) + 20;

--- 32 unchanged lines hidden (view full) ---

489 xfree(bp);
490 sink(1, argv + argc - 1);
491 (void) close(remin);
492 remin = remout = -1;
493 }
494}
495
496void
451{
452 int i, len;
453 char *bp, *host, *src, *suser;
454
455 for (i = 0; i < argc - 1; i++) {
456 if (!(src = colon(argv[i]))) { /* Local to local. */
457 len = strlen(_PATH_CP) + strlen(argv[i]) +
458 strlen(argv[argc - 1]) + 20;

--- 32 unchanged lines hidden (view full) ---

491 xfree(bp);
492 sink(1, argv + argc - 1);
493 (void) close(remin);
494 remin = remout = -1;
495 }
496}
497
498void
497source(argc, argv)
498 int argc;
499 char *argv[];
499source(int argc, char **argv)
500{
501 struct stat stb;
502 static BUF buffer;
503 BUF *bp;
504 off_t i, amt, result, statbytes;
505 int fd, haderr, indx;
506 char *last, *name, buf[2048];
507 int len;

--- 36 unchanged lines hidden (view full) ---

544 if (pflag) {
545 /*
546 * Make it compatible with possible future
547 * versions expecting microseconds.
548 */
549 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
550 (u_long) stb.st_mtime,
551 (u_long) stb.st_atime);
500{
501 struct stat stb;
502 static BUF buffer;
503 BUF *bp;
504 off_t i, amt, result, statbytes;
505 int fd, haderr, indx;
506 char *last, *name, buf[2048];
507 int len;

--- 36 unchanged lines hidden (view full) ---

544 if (pflag) {
545 /*
546 * Make it compatible with possible future
547 * versions expecting microseconds.
548 */
549 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
550 (u_long) stb.st_mtime,
551 (u_long) stb.st_atime);
552 (void) atomicio(write, remout, buf, strlen(buf));
552 (void) atomicio(vwrite, remout, buf, strlen(buf));
553 if (response() < 0)
554 goto next;
555 }
556#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
553 if (response() < 0)
554 goto next;
555 }
556#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
557#ifdef HAVE_LONG_LONG_INT
558 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
559 (u_int) (stb.st_mode & FILEMODEMASK),
557 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
558 (u_int) (stb.st_mode & FILEMODEMASK),
560 (long long)stb.st_size, last);
561#else
562 /* XXX: Handle integer overflow? */
563 snprintf(buf, sizeof buf, "C%04o %lu %s\n",
564 (u_int) (stb.st_mode & FILEMODEMASK),
565 (u_long) stb.st_size, last);
566#endif
559 (int64_t)stb.st_size, last);
567 if (verbose_mode) {
568 fprintf(stderr, "Sending file modes: %s", buf);
569 }
560 if (verbose_mode) {
561 fprintf(stderr, "Sending file modes: %s", buf);
562 }
570 (void) atomicio(write, remout, buf, strlen(buf));
563 (void) atomicio(vwrite, remout, buf, strlen(buf));
571 if (response() < 0)
572 goto next;
573 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
574next: (void) close(fd);
575 continue;
576 }
577 if (showprogress)
578 start_progress_meter(curfile, stb.st_size, &statbytes);
579 /* Keep writing after an error so that we stay sync'd up. */
580 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
581 amt = bp->cnt;
582 if (i + amt > stb.st_size)
583 amt = stb.st_size - i;
584 if (!haderr) {
585 result = atomicio(read, fd, bp->buf, amt);
586 if (result != amt)
587 haderr = result >= 0 ? EIO : errno;
588 }
589 if (haderr)
564 if (response() < 0)
565 goto next;
566 if ((bp = allocbuf(&buffer, fd, 2048)) == NULL) {
567next: (void) close(fd);
568 continue;
569 }
570 if (showprogress)
571 start_progress_meter(curfile, stb.st_size, &statbytes);
572 /* Keep writing after an error so that we stay sync'd up. */
573 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
574 amt = bp->cnt;
575 if (i + amt > stb.st_size)
576 amt = stb.st_size - i;
577 if (!haderr) {
578 result = atomicio(read, fd, bp->buf, amt);
579 if (result != amt)
580 haderr = result >= 0 ? EIO : errno;
581 }
582 if (haderr)
590 (void) atomicio(write, remout, bp->buf, amt);
583 (void) atomicio(vwrite, remout, bp->buf, amt);
591 else {
584 else {
592 result = atomicio(write, remout, bp->buf, amt);
585 result = atomicio(vwrite, remout, bp->buf, amt);
593 if (result != amt)
594 haderr = result >= 0 ? EIO : errno;
595 statbytes += result;
596 }
597 if (limitbw)
598 bwlimit(amt);
599 }
600 if (showprogress)
601 stop_progress_meter();
602
603 if (close(fd) < 0 && !haderr)
604 haderr = errno;
605 if (!haderr)
586 if (result != amt)
587 haderr = result >= 0 ? EIO : errno;
588 statbytes += result;
589 }
590 if (limitbw)
591 bwlimit(amt);
592 }
593 if (showprogress)
594 stop_progress_meter();
595
596 if (close(fd) < 0 && !haderr)
597 haderr = errno;
598 if (!haderr)
606 (void) atomicio(write, remout, "", 1);
599 (void) atomicio(vwrite, remout, "", 1);
607 else
608 run_err("%s: %s", name, strerror(haderr));
609 (void) response();
610 }
611}
612
613void
600 else
601 run_err("%s: %s", name, strerror(haderr));
602 (void) response();
603 }
604}
605
606void
614rsource(name, statp)
615 char *name;
616 struct stat *statp;
607rsource(char *name, struct stat *statp)
617{
618 DIR *dirp;
619 struct dirent *dp;
620 char *last, *vect[1], path[1100];
621
622 if (!(dirp = opendir(name))) {
623 run_err("%s: %s", name, strerror(errno));
624 return;
625 }
626 last = strrchr(name, '/');
627 if (last == 0)
628 last = name;
629 else
630 last++;
631 if (pflag) {
632 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
633 (u_long) statp->st_mtime,
634 (u_long) statp->st_atime);
608{
609 DIR *dirp;
610 struct dirent *dp;
611 char *last, *vect[1], path[1100];
612
613 if (!(dirp = opendir(name))) {
614 run_err("%s: %s", name, strerror(errno));
615 return;
616 }
617 last = strrchr(name, '/');
618 if (last == 0)
619 last = name;
620 else
621 last++;
622 if (pflag) {
623 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
624 (u_long) statp->st_mtime,
625 (u_long) statp->st_atime);
635 (void) atomicio(write, remout, path, strlen(path));
626 (void) atomicio(vwrite, remout, path, strlen(path));
636 if (response() < 0) {
637 closedir(dirp);
638 return;
639 }
640 }
641 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
642 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
643 if (verbose_mode)
644 fprintf(stderr, "Entering directory: %s", path);
627 if (response() < 0) {
628 closedir(dirp);
629 return;
630 }
631 }
632 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
633 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
634 if (verbose_mode)
635 fprintf(stderr, "Entering directory: %s", path);
645 (void) atomicio(write, remout, path, strlen(path));
636 (void) atomicio(vwrite, remout, path, strlen(path));
646 if (response() < 0) {
647 closedir(dirp);
648 return;
649 }
650 while ((dp = readdir(dirp)) != NULL) {
651 if (dp->d_ino == 0)
652 continue;
653 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
654 continue;
655 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
656 run_err("%s/%s: name too long", name, dp->d_name);
657 continue;
658 }
659 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
660 vect[0] = path;
661 source(1, vect);
662 }
663 (void) closedir(dirp);
637 if (response() < 0) {
638 closedir(dirp);
639 return;
640 }
641 while ((dp = readdir(dirp)) != NULL) {
642 if (dp->d_ino == 0)
643 continue;
644 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
645 continue;
646 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
647 run_err("%s/%s: name too long", name, dp->d_name);
648 continue;
649 }
650 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
651 vect[0] = path;
652 source(1, vect);
653 }
654 (void) closedir(dirp);
664 (void) atomicio(write, remout, "E\n", 2);
655 (void) atomicio(vwrite, remout, "E\n", 2);
665 (void) response();
666}
667
668void
669bwlimit(int amount)
670{
671 static struct timeval bwstart, bwend;
672 static int lamt, thresh = 16384;

--- 42 unchanged lines hidden (view full) ---

715 }
716 }
717
718 lamt = 0;
719 gettimeofday(&bwstart, NULL);
720}
721
722void
656 (void) response();
657}
658
659void
660bwlimit(int amount)
661{
662 static struct timeval bwstart, bwend;
663 static int lamt, thresh = 16384;

--- 42 unchanged lines hidden (view full) ---

706 }
707 }
708
709 lamt = 0;
710 gettimeofday(&bwstart, NULL);
711}
712
713void
723sink(argc, argv)
724 int argc;
725 char *argv[];
714sink(int argc, char **argv)
726{
727 static BUF buffer;
728 struct stat stb;
729 enum {
730 YES, NO, DISPLAYED
731 } wrerr;
732 BUF *bp;
733 off_t i, j;

--- 14 unchanged lines hidden (view full) ---

748 if (argc != 1) {
749 run_err("ambiguous target");
750 exit(1);
751 }
752 targ = *argv;
753 if (targetshouldbedirectory)
754 verifydir(targ);
755
715{
716 static BUF buffer;
717 struct stat stb;
718 enum {
719 YES, NO, DISPLAYED
720 } wrerr;
721 BUF *bp;
722 off_t i, j;

--- 14 unchanged lines hidden (view full) ---

737 if (argc != 1) {
738 run_err("ambiguous target");
739 exit(1);
740 }
741 targ = *argv;
742 if (targetshouldbedirectory)
743 verifydir(targ);
744
756 (void) atomicio(write, remout, "", 1);
745 (void) atomicio(vwrite, remout, "", 1);
757 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
758 targisdir = 1;
759 for (first = 1;; first = 0) {
760 cp = buf;
761 if (atomicio(read, remin, cp, 1) <= 0)
762 return;
763 if (*cp++ == '\n')
764 SCREWUP("unexpected <newline>");
765 do {
766 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
767 SCREWUP("lost connection");
768 *cp++ = ch;
769 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
770 *cp = 0;
771
772 if (buf[0] == '\01' || buf[0] == '\02') {
773 if (iamremote == 0)
746 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
747 targisdir = 1;
748 for (first = 1;; first = 0) {
749 cp = buf;
750 if (atomicio(read, remin, cp, 1) <= 0)
751 return;
752 if (*cp++ == '\n')
753 SCREWUP("unexpected <newline>");
754 do {
755 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
756 SCREWUP("lost connection");
757 *cp++ = ch;
758 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
759 *cp = 0;
760
761 if (buf[0] == '\01' || buf[0] == '\02') {
762 if (iamremote == 0)
774 (void) atomicio(write, STDERR_FILENO,
763 (void) atomicio(vwrite, STDERR_FILENO,
775 buf + 1, strlen(buf + 1));
776 if (buf[0] == '\02')
777 exit(1);
778 ++errs;
779 continue;
780 }
781 if (buf[0] == 'E') {
764 buf + 1, strlen(buf + 1));
765 if (buf[0] == '\02')
766 exit(1);
767 ++errs;
768 continue;
769 }
770 if (buf[0] == 'E') {
782 (void) atomicio(write, remout, "", 1);
771 (void) atomicio(vwrite, remout, "", 1);
783 return;
784 }
785 if (ch == '\n')
786 *--cp = 0;
787
788 cp = buf;
789 if (*cp == 'T') {
790 setimes++;

--- 5 unchanged lines hidden (view full) ---

796 if (!cp || *cp++ != ' ')
797 SCREWUP("mtime.usec not delimited");
798 atime.tv_sec = strtol(cp, &cp, 10);
799 if (!cp || *cp++ != ' ')
800 SCREWUP("atime.sec not delimited");
801 atime.tv_usec = strtol(cp, &cp, 10);
802 if (!cp || *cp++ != '\0')
803 SCREWUP("atime.usec not delimited");
772 return;
773 }
774 if (ch == '\n')
775 *--cp = 0;
776
777 cp = buf;
778 if (*cp == 'T') {
779 setimes++;

--- 5 unchanged lines hidden (view full) ---

785 if (!cp || *cp++ != ' ')
786 SCREWUP("mtime.usec not delimited");
787 atime.tv_sec = strtol(cp, &cp, 10);
788 if (!cp || *cp++ != ' ')
789 SCREWUP("atime.sec not delimited");
790 atime.tv_usec = strtol(cp, &cp, 10);
791 if (!cp || *cp++ != '\0')
792 SCREWUP("atime.usec not delimited");
804 (void) atomicio(write, remout, "", 1);
793 (void) atomicio(vwrite, remout, "", 1);
805 continue;
806 }
807 if (*cp != 'C' && *cp != 'D') {
808 /*
809 * Check for the case "rcp remote:foo\* local:bar".
810 * In this case, the line "No match." can be returned
811 * by the shell before the rcp command on the remote is
812 * executed so the ^Aerror_message convention isn't

--- 68 unchanged lines hidden (view full) ---

881 continue;
882 }
883 omode = mode;
884 mode |= S_IWRITE;
885 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
886bad: run_err("%s: %s", np, strerror(errno));
887 continue;
888 }
794 continue;
795 }
796 if (*cp != 'C' && *cp != 'D') {
797 /*
798 * Check for the case "rcp remote:foo\* local:bar".
799 * In this case, the line "No match." can be returned
800 * by the shell before the rcp command on the remote is
801 * executed so the ^Aerror_message convention isn't

--- 68 unchanged lines hidden (view full) ---

870 continue;
871 }
872 omode = mode;
873 mode |= S_IWRITE;
874 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
875bad: run_err("%s: %s", np, strerror(errno));
876 continue;
877 }
889 (void) atomicio(write, remout, "", 1);
878 (void) atomicio(vwrite, remout, "", 1);
890 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
891 (void) close(ofd);
892 continue;
893 }
894 cp = bp->buf;
895 wrerr = NO;
896
897 statbytes = 0;

--- 20 unchanged lines hidden (view full) ---

918 } while (amt > 0);
919
920 if (limitbw)
921 bwlimit(4096);
922
923 if (count == bp->cnt) {
924 /* Keep reading so we stay sync'd up. */
925 if (wrerr == NO) {
879 if ((bp = allocbuf(&buffer, ofd, 4096)) == NULL) {
880 (void) close(ofd);
881 continue;
882 }
883 cp = bp->buf;
884 wrerr = NO;
885
886 statbytes = 0;

--- 20 unchanged lines hidden (view full) ---

907 } while (amt > 0);
908
909 if (limitbw)
910 bwlimit(4096);
911
912 if (count == bp->cnt) {
913 /* Keep reading so we stay sync'd up. */
914 if (wrerr == NO) {
926 j = atomicio(write, ofd, bp->buf, count);
915 j = atomicio(vwrite, ofd, bp->buf, count);
927 if (j != count) {
928 wrerr = YES;
929 wrerrno = j >= 0 ? EIO : errno;
930 }
931 }
932 count = 0;
933 cp = bp->buf;
934 }
935 }
936 if (showprogress)
937 stop_progress_meter();
938 if (count != 0 && wrerr == NO &&
916 if (j != count) {
917 wrerr = YES;
918 wrerrno = j >= 0 ? EIO : errno;
919 }
920 }
921 count = 0;
922 cp = bp->buf;
923 }
924 }
925 if (showprogress)
926 stop_progress_meter();
927 if (count != 0 && wrerr == NO &&
939 (j = atomicio(write, ofd, bp->buf, count)) != count) {
928 (j = atomicio(vwrite, ofd, bp->buf, count)) != count) {
940 wrerr = YES;
941 wrerrno = j >= 0 ? EIO : errno;
942 }
943 if (wrerr == NO && ftruncate(ofd, size) != 0) {
944 run_err("%s: truncate: %s", np, strerror(errno));
945 wrerr = DISPLAYED;
946 }
947 if (pflag) {

--- 28 unchanged lines hidden (view full) ---

976 wrerr = DISPLAYED;
977 }
978 }
979 switch (wrerr) {
980 case YES:
981 run_err("%s: %s", np, strerror(wrerrno));
982 break;
983 case NO:
929 wrerr = YES;
930 wrerrno = j >= 0 ? EIO : errno;
931 }
932 if (wrerr == NO && ftruncate(ofd, size) != 0) {
933 run_err("%s: truncate: %s", np, strerror(errno));
934 wrerr = DISPLAYED;
935 }
936 if (pflag) {

--- 28 unchanged lines hidden (view full) ---

965 wrerr = DISPLAYED;
966 }
967 }
968 switch (wrerr) {
969 case YES:
970 run_err("%s: %s", np, strerror(wrerrno));
971 break;
972 case NO:
984 (void) atomicio(write, remout, "", 1);
973 (void) atomicio(vwrite, remout, "", 1);
985 break;
986 case DISPLAYED:
987 break;
988 }
989 }
990screwup:
991 run_err("protocol error: %s", why);
992 exit(1);

--- 18 unchanged lines hidden (view full) ---

1011 case 2: /* fatal error, "" */
1012 do {
1013 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
1014 lostconn(0);
1015 *cp++ = ch;
1016 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
1017
1018 if (!iamremote)
974 break;
975 case DISPLAYED:
976 break;
977 }
978 }
979screwup:
980 run_err("protocol error: %s", why);
981 exit(1);

--- 18 unchanged lines hidden (view full) ---

1000 case 2: /* fatal error, "" */
1001 do {
1002 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
1003 lostconn(0);
1004 *cp++ = ch;
1005 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
1006
1007 if (!iamremote)
1019 (void) atomicio(write, STDERR_FILENO, rbuf, cp - rbuf);
1008 (void) atomicio(vwrite, STDERR_FILENO, rbuf, cp - rbuf);
1020 ++errs;
1021 if (resp == 1)
1022 return (-1);
1023 exit(1);
1024 }
1025 /* NOTREACHED */
1026}
1027

--- 28 unchanged lines hidden (view full) ---

1056 va_start(ap, fmt);
1057 vfprintf(stderr, fmt, ap);
1058 va_end(ap);
1059 fprintf(stderr, "\n");
1060 }
1061}
1062
1063void
1009 ++errs;
1010 if (resp == 1)
1011 return (-1);
1012 exit(1);
1013 }
1014 /* NOTREACHED */
1015}
1016

--- 28 unchanged lines hidden (view full) ---

1045 va_start(ap, fmt);
1046 vfprintf(stderr, fmt, ap);
1047 va_end(ap);
1048 fprintf(stderr, "\n");
1049 }
1050}
1051
1052void
1064verifydir(cp)
1065 char *cp;
1053verifydir(char *cp)
1066{
1067 struct stat stb;
1068
1069 if (!stat(cp, &stb)) {
1070 if (S_ISDIR(stb.st_mode))
1071 return;
1072 errno = ENOTDIR;
1073 }
1074 run_err("%s: %s", cp, strerror(errno));
1075 exit(1);
1076}
1077
1078int
1054{
1055 struct stat stb;
1056
1057 if (!stat(cp, &stb)) {
1058 if (S_ISDIR(stb.st_mode))
1059 return;
1060 errno = ENOTDIR;
1061 }
1062 run_err("%s: %s", cp, strerror(errno));
1063 exit(1);
1064}
1065
1066int
1079okname(cp0)
1080 char *cp0;
1067okname(char *cp0)
1081{
1082 int c;
1083 char *cp;
1084
1085 cp = cp0;
1086 do {
1087 c = (int)*cp;
1088 if (c & 0200)

--- 13 unchanged lines hidden (view full) ---

1102 } while (*++cp);
1103 return (1);
1104
1105bad: fprintf(stderr, "%s: invalid user name\n", cp0);
1106 return (0);
1107}
1108
1109BUF *
1068{
1069 int c;
1070 char *cp;
1071
1072 cp = cp0;
1073 do {
1074 c = (int)*cp;
1075 if (c & 0200)

--- 13 unchanged lines hidden (view full) ---

1089 } while (*++cp);
1090 return (1);
1091
1092bad: fprintf(stderr, "%s: invalid user name\n", cp0);
1093 return (0);
1094}
1095
1096BUF *
1110allocbuf(bp, fd, blksize)
1111 BUF *bp;
1112 int fd, blksize;
1097allocbuf(BUF *bp, int fd, int blksize)
1113{
1114 size_t size;
1115#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1116 struct stat stb;
1117
1118 if (fstat(fd, &stb) < 0) {
1119 run_err("fstat: %s", strerror(errno));
1120 return (0);

--- 11 unchanged lines hidden (view full) ---

1132 else
1133 bp->buf = xrealloc(bp->buf, size);
1134 memset(bp->buf, 0, size);
1135 bp->cnt = size;
1136 return (bp);
1137}
1138
1139void
1098{
1099 size_t size;
1100#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1101 struct stat stb;
1102
1103 if (fstat(fd, &stb) < 0) {
1104 run_err("fstat: %s", strerror(errno));
1105 return (0);

--- 11 unchanged lines hidden (view full) ---

1117 else
1118 bp->buf = xrealloc(bp->buf, size);
1119 memset(bp->buf, 0, size);
1120 bp->cnt = size;
1121 return (bp);
1122}
1123
1124void
1140lostconn(signo)
1141 int signo;
1125lostconn(int signo)
1142{
1143 if (!iamremote)
1144 write(STDERR_FILENO, "lost connection\n", 16);
1145 if (signo)
1146 _exit(1);
1147 else
1148 exit(1);
1149}
1126{
1127 if (!iamremote)
1128 write(STDERR_FILENO, "lost connection\n", 16);
1129 if (signo)
1130 _exit(1);
1131 else
1132 exit(1);
1133}