1 /* $NetBSD: rcp.c,v 1.49 2012/05/07 15:22:54 chs Exp $ */
4 * Copyright (c) 1983, 1990, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1983, 1990, 1992, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)rcp.c 8.2 (Berkeley) 4/2/94";
42 __RCSID("$NetBSD: rcp.c,v 1.49 2012/05/07 15:22:54 chs Exp $");
46 #include <sys/param.h>
49 #include <sys/socket.h>
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
68 #include "pathnames.h"
71 #define OPTIONS "46dfprt"
78 int pflag
, iamremote
, iamrecursive
, targetshouldbedirectory
;
79 int family
= AF_UNSPEC
;
80 static char dot
[] = ".";
83 char cmd
[CMDNEEDS
]; /* must hold "rcp -r -p -d\0" */
86 void rsource(char *, struct stat
*);
87 void sink(int, char *[]);
88 void source(int, char *[]);
89 void tolocal(int, char *[]);
90 void toremote(char *, int, char *[]);
94 main(int argc
, char *argv
[])
101 setprogname(argv
[0]);
102 (void)setlocale(LC_ALL
, "");
105 while ((ch
= getopt(argc
, argv
, OPTIONS
)) != -1)
106 switch(ch
) { /* User-visible flags. */
121 /* Server options. */
123 targetshouldbedirectory
= 1;
125 case 'f': /* "from" */
140 sp
= getservbyname(shell
= "shell", "tcp");
142 errx(1, "%s/tcp: unknown service", shell
);
145 if ((pwd
= getpwuid(userid
= getuid())) == NULL
)
146 errx(1, "unknown user %d", (int)userid
);
148 if ((pwname
= strdup(pwd
->pw_name
)) == NULL
)
151 rem
= STDIN_FILENO
; /* XXX */
153 if (fflag
) { /* Follow "protocol", send data. */
159 if (tflag
) { /* Receive data. */
167 targetshouldbedirectory
= 1;
170 /* Command to be executed on remote system using "rsh". */
171 (void)snprintf(cmd
, sizeof(cmd
), "rcp%s%s%s",
172 iamrecursive
? " -r" : "", pflag
? " -p" : "",
173 targetshouldbedirectory
? " -d" : "");
175 (void)signal(SIGPIPE
, lostconn
);
177 if ((targ
= colon(argv
[argc
- 1])) != NULL
)/* Dest is remote host. */
178 toremote(targ
, argc
, argv
);
180 tolocal(argc
, argv
); /* Dest is local host. */
181 if (targetshouldbedirectory
)
182 verifydir(argv
[argc
- 1]);
189 toremote(char *targ
, int argc
, char *argv
[])
193 char *bp
, *host
, *src
, *suser
, *thost
, *tuser
;
199 if ((thost
= strchr(argv
[argc
- 1], '@')) != NULL
) {
202 tuser
= argv
[argc
- 1];
205 else if (!okname(tuser
))
208 thost
= argv
[argc
- 1];
211 thost
= unbracket(thost
);
213 for (i
= 0; i
< argc
- 1; i
++) {
214 src
= colon(argv
[i
]);
215 if (src
) { /* remote to remote */
219 host
= strchr(argv
[i
], '@');
220 len
= strlen(_PATH_RSH
) + strlen(argv
[i
]) +
221 strlen(src
) + (tuser
? strlen(tuser
) : 0) +
222 strlen(thost
) + strlen(targ
) + CMDNEEDS
+ 20;
223 if (!(bp
= malloc(len
)))
227 host
= unbracket(host
);
231 else if (!okname(suser
)) {
235 (void)snprintf(bp
, len
,
236 "%s %s -l %s -n %s %s '%s%s%s:%s'",
237 _PATH_RSH
, host
, suser
, cmd
, src
,
238 tuser
? tuser
: "", tuser
? "@" : "",
241 host
= unbracket(argv
[i
]);
242 (void)snprintf(bp
, len
,
243 "exec %s %s -n %s %s '%s%s%s:%s'",
244 _PATH_RSH
, argv
[i
], cmd
, src
,
245 tuser
? tuser
: "", tuser
? "@" : "",
250 } else { /* local to remote */
252 len
= strlen(targ
) + CMDNEEDS
+ 20;
253 if (!(bp
= malloc(len
)))
255 (void)snprintf(bp
, len
, "%s -t %s", cmd
, targ
);
257 rem
= rcmd_af(&host
, port
, pwname
,
258 tuser
? tuser
: pwname
,
272 tolocal(int argc
, char *argv
[])
276 char *bp
, *host
, *src
, *suser
;
278 for (i
= 0; i
< argc
- 1; i
++) {
279 if (!(src
= colon(argv
[i
]))) { /* Local to local. */
280 len
= strlen(_PATH_CP
) + strlen(argv
[i
]) +
281 strlen(argv
[argc
- 1]) + 20;
282 if (!(bp
= malloc(len
)))
284 (void)snprintf(bp
, len
, "exec %s%s%s %s %s", _PATH_CP
,
285 iamrecursive
? " -r" : "", pflag
? " -p" : "",
286 argv
[i
], argv
[argc
- 1]);
295 if ((host
= strchr(argv
[i
], '@')) == NULL
) {
303 else if (!okname(suser
))
306 host
= unbracket(host
);
307 len
= strlen(src
) + CMDNEEDS
+ 20;
308 if ((bp
= malloc(len
)) == NULL
)
310 (void)snprintf(bp
, len
, "%s -f %s", cmd
, src
);
312 rcmd_af(&host
, port
, pwname
, suser
, bp
, NULL
, family
);
318 sink(1, argv
+ argc
- 1);
325 source(int argc
, char *argv
[])
332 int fd
, haderr
, indx
, result
;
333 char *last
, *name
, buf
[BUFSIZ
];
335 for (indx
= 0; indx
< argc
; ++indx
) {
337 if ((fd
= open(name
, O_RDONLY
, 0)) < 0)
339 if (fstat(fd
, &stb
)) {
340 syserr
: run_err("%s: %s", name
, strerror(errno
));
343 switch (stb
.st_mode
& S_IFMT
) {
353 run_err("%s: not a regular file", name
);
356 if ((last
= strrchr(name
, '/')) == NULL
)
362 * Make it compatible with possible future
363 * versions expecting microseconds.
365 (void)snprintf(buf
, sizeof(buf
), "T%lld %ld %lld %ld\n",
366 (long long)stb
.st_mtimespec
.tv_sec
,
367 (long)stb
.st_mtimespec
.tv_nsec
/ 1000,
368 (long long)stb
.st_atimespec
.tv_sec
,
369 (long)stb
.st_atimespec
.tv_nsec
/ 1000);
370 (void)write(rem
, buf
, strlen(buf
));
374 #define RCPMODEMASK (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
375 (void)snprintf(buf
, sizeof(buf
), "C%04o %lld %s\n",
376 stb
.st_mode
& RCPMODEMASK
, (long long)stb
.st_size
, last
);
377 (void)write(rem
, buf
, strlen(buf
));
380 if ((bp
= allocbuf(&buffer
, fd
, BUFSIZ
)) == NULL
) {
381 next
: (void)close(fd
);
385 /* Keep writing after an error so that we stay sync'd up. */
387 for (i
= 0; i
< stb
.st_size
; i
+= bp
->cnt
) {
389 if (i
+ amt
> stb
.st_size
)
390 amt
= stb
.st_size
- i
;
392 result
= read(fd
, bp
->buf
, (size_t)amt
);
394 haderr
= result
>= 0 ? EIO
: errno
;
397 (void)write(rem
, bp
->buf
, (size_t)amt
);
399 result
= write(rem
, bp
->buf
, (size_t)amt
);
401 haderr
= result
>= 0 ? EIO
: errno
;
404 if (close(fd
) && !haderr
)
407 (void)write(rem
, "", 1);
409 run_err("%s: %s", name
, strerror(haderr
));
415 rsource(char *name
, struct stat
*statp
)
419 char *last
, *vect
[1], path
[MAXPATHLEN
];
421 if (!(dirp
= opendir(name
))) {
422 run_err("%s: %s", name
, strerror(errno
));
425 last
= strrchr(name
, '/');
431 (void)snprintf(path
, sizeof(path
), "T%lld %ld %lld %ld\n",
432 (long long)statp
->st_mtimespec
.tv_sec
,
433 (long)statp
->st_mtimespec
.tv_nsec
/ 1000,
434 (long long)statp
->st_atimespec
.tv_sec
,
435 (long)statp
->st_atimespec
.tv_nsec
/ 1000);
436 (void)write(rem
, path
, strlen(path
));
437 if (response() < 0) {
438 (void)closedir(dirp
);
442 (void)snprintf(path
, sizeof(path
),
443 "D%04o %d %s\n", statp
->st_mode
& RCPMODEMASK
, 0, last
);
444 (void)write(rem
, path
, strlen(path
));
445 if (response() < 0) {
446 (void)closedir(dirp
);
449 while ((dp
= readdir(dirp
)) != NULL
) {
452 if (!strcmp(dp
->d_name
, dot
) || !strcmp(dp
->d_name
, ".."))
454 if (strlen(name
) + 1 + strlen(dp
->d_name
) >= MAXPATHLEN
- 1) {
455 run_err("%s/%s: name too long", name
, dp
->d_name
);
458 (void)snprintf(path
, sizeof(path
), "%s/%s", name
, dp
->d_name
);
462 (void)closedir(dirp
);
463 (void)write(rem
, "E\n", 2);
468 sink(int argc
, char *argv
[])
472 struct timeval tv
[2];
473 enum { YES
, NO
, DISPLAYED
} wrerr
;
479 int exists
, first
, ofd
;
483 int setimes
, targisdir
;
484 int wrerrno
= 0; /* pacify gcc */
485 char ch
, *cp
, *np
, *targ
, *vect
[1], buf
[BUFSIZ
];
488 char *namebuf
= NULL
;
493 #define SCREWUP(str) { why = str; goto screwup; }
495 setimes
= targisdir
= 0;
500 run_err("ambiguous target");
504 if (targetshouldbedirectory
)
506 (void)write(rem
, "", 1);
507 if (stat(targ
, &stb
) == 0 && S_ISDIR(stb
.st_mode
))
509 for (first
= 1;; first
= 0) {
511 if (read(rem
, cp
, 1) <= 0)
514 SCREWUP("unexpected <newline>");
516 if (read(rem
, &ch
, sizeof(ch
)) != sizeof(ch
))
517 SCREWUP("lost connection");
519 } while (cp
< &buf
[BUFSIZ
- 1] && ch
!= '\n');
522 if (buf
[0] == '\01' || buf
[0] == '\02') {
524 (void)write(STDERR_FILENO
,
525 buf
+ 1, strlen(buf
+ 1));
532 (void)write(rem
, "", 1);
539 #define getnum(t) (t) = 0; while (isdigit((unsigned char)*cp)) (t) = (t) * 10 + (*cp++ - '0');
544 getnum(mtime
.tv_sec
);
546 SCREWUP("mtime.sec not delimited");
547 getnum(mtime
.tv_usec
);
549 SCREWUP("mtime.usec not delimited");
550 getnum(atime
.tv_sec
);
552 SCREWUP("atime.sec not delimited");
553 getnum(atime
.tv_usec
);
555 SCREWUP("atime.usec not delimited");
556 (void)write(rem
, "", 1);
559 if (*cp
!= 'C' && *cp
!= 'D') {
561 * Check for the case "rcp remote:foo\* local:bar".
562 * In this case, the line "No match." can be returned
563 * by the shell before the rcp command on the remote is
564 * executed so the ^Aerror_message convention isn't
571 SCREWUP("expected control record");
574 for (++cp
; cp
< buf
+ 5; cp
++) {
575 if (*cp
< '0' || *cp
> '7')
577 mode
= (mode
<< 3) | (*cp
- '0');
580 SCREWUP("mode not delimited");
582 for (size
= 0; isdigit((unsigned char)*cp
);)
583 size
= size
* 10 + (*cp
++ - '0');
585 SCREWUP("size not delimited");
590 need
= strlen(targ
) + strlen(cp
) + 2;
591 if (need
> cursize
) {
593 newnamebuf
= realloc(namebuf
, need
);
594 if (newnamebuf
!= NULL
) {
595 namebuf
= newnamebuf
;
598 run_err("%s", strerror(errno
));
602 (void)snprintf(namebuf
, cursize
, "%s%s%s", targ
,
603 *targ
? "/" : "", cp
);
607 exists
= stat(np
, &stb
) == 0;
609 int mod_flag
= pflag
;
611 if (!S_ISDIR(stb
.st_mode
)) {
616 (void)chmod(np
, mode
);
618 /* Handle copying from a read-only directory */
620 if (mkdir(np
, mode
| S_IRWXU
) < 0)
627 if (utimes(np
, tv
) < 0)
628 run_err("%s: set times: %s",
629 np
, strerror(errno
));
632 (void)chmod(np
, mode
);
637 if ((ofd
= open(np
, O_WRONLY
|O_CREAT
, mode
)) < 0) {
638 bad
: run_err("%s: %s", np
, strerror(errno
));
641 (void)write(rem
, "", 1);
642 if ((bp
= allocbuf(&buffer
, ofd
, BUFSIZ
)) == NULL
) {
649 for (i
= 0; i
< size
; i
+= BUFSIZ
) {
655 j
= read(rem
, cp
, (size_t)amt
);
657 run_err("%s", j
? strerror(errno
) :
658 "dropped connection");
664 if (count
== bp
->cnt
) {
665 /* Keep reading so we stay sync'd up. */
667 j
= write(ofd
, bp
->buf
, (size_t)count
);
670 wrerrno
= j
>= 0 ? EIO
: errno
;
677 if (count
!= 0 && wrerr
== NO
&&
678 (j
= write(ofd
, bp
->buf
, (size_t)count
)) != count
) {
680 wrerrno
= j
>= 0 ? EIO
: errno
;
682 if (ftruncate(ofd
, size
)) {
683 run_err("%s: truncate: %s", np
, strerror(errno
));
687 if (exists
|| omode
!= mode
)
688 if (fchmod(ofd
, omode
))
689 run_err("%s: set mode: %s",
690 np
, strerror(errno
));
692 if (!exists
&& omode
!= mode
)
693 if (fchmod(ofd
, omode
& ~mask
))
694 run_err("%s: set mode: %s",
695 np
, strerror(errno
));
698 if (setimes
&& wrerr
== NO
) {
700 if (futimes(ofd
, tv
) < 0) {
701 run_err("%s: set times: %s",
702 np
, strerror(errno
));
709 if (setimes
&& wrerr
== NO
) {
711 if (utimes(np
, tv
) < 0) {
712 run_err("%s: set times: %s",
713 np
, strerror(errno
));
721 run_err("%s: write: %s", np
, strerror(wrerrno
));
724 (void)write(rem
, "", 1);
738 run_err("protocol error: %s", why
);
747 char ch
, *cp
, resp
, rbuf
[BUFSIZ
];
749 if (read(rem
, &resp
, sizeof(resp
)) != sizeof(resp
))
759 case 1: /* error, followed by error msg */
760 case 2: /* fatal error, "" */
762 if (read(rem
, &ch
, sizeof(ch
)) != sizeof(ch
))
765 } while (cp
< &rbuf
[BUFSIZ
] && ch
!= '\n');
768 (void)write(STDERR_FILENO
, rbuf
, (size_t)(cp
- rbuf
));
780 (void)fprintf(stderr
,
781 "usage: rcp [-46p] f1 f2; or: rcp [-46pr] f1 ... fn directory\n");
790 run_err(const char *fmt
, ...)
796 if (fp
== NULL
&& !(fp
= fdopen(rem
, "w")))
801 (void)fprintf(fp
, "%c", 0x01);
802 (void)fprintf(fp
, "rcp: ");
803 (void)vfprintf(fp
, fmt
, ap
);
804 (void)fprintf(fp
, "\n");