1 /* $NetBSD: rcp.c,v 1.46 2008/05/26 14:21:08 christos 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.46 2008/05/26 14:21:08 christos 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
];
491 #define SCREWUP(str) { why = str; goto screwup; }
493 setimes
= targisdir
= 0;
498 run_err("ambiguous target");
502 if (targetshouldbedirectory
)
504 (void)write(rem
, "", 1);
505 if (stat(targ
, &stb
) == 0 && S_ISDIR(stb
.st_mode
))
507 for (first
= 1;; first
= 0) {
509 if (read(rem
, cp
, 1) <= 0)
512 SCREWUP("unexpected <newline>");
514 if (read(rem
, &ch
, sizeof(ch
)) != sizeof(ch
))
515 SCREWUP("lost connection");
517 } while (cp
< &buf
[BUFSIZ
- 1] && ch
!= '\n');
520 if (buf
[0] == '\01' || buf
[0] == '\02') {
522 (void)write(STDERR_FILENO
,
523 buf
+ 1, strlen(buf
+ 1));
530 (void)write(rem
, "", 1);
537 #define getnum(t) (t) = 0; while (isdigit((unsigned char)*cp)) (t) = (t) * 10 + (*cp++ - '0');
542 getnum(mtime
.tv_sec
);
544 SCREWUP("mtime.sec not delimited");
545 getnum(mtime
.tv_usec
);
547 SCREWUP("mtime.usec not delimited");
548 getnum(atime
.tv_sec
);
550 SCREWUP("atime.sec not delimited");
551 getnum(atime
.tv_usec
);
553 SCREWUP("atime.usec not delimited");
554 (void)write(rem
, "", 1);
557 if (*cp
!= 'C' && *cp
!= 'D') {
559 * Check for the case "rcp remote:foo\* local:bar".
560 * In this case, the line "No match." can be returned
561 * by the shell before the rcp command on the remote is
562 * executed so the ^Aerror_message convention isn't
569 SCREWUP("expected control record");
572 for (++cp
; cp
< buf
+ 5; cp
++) {
573 if (*cp
< '0' || *cp
> '7')
575 mode
= (mode
<< 3) | (*cp
- '0');
578 SCREWUP("mode not delimited");
580 for (size
= 0; isdigit((unsigned char)*cp
);)
581 size
= size
* 10 + (*cp
++ - '0');
583 SCREWUP("size not delimited");
585 static char *namebuf
;
589 need
= strlen(targ
) + strlen(cp
) + 250;
590 if (need
> cursize
) {
591 if (!(namebuf
= malloc(need
)))
592 run_err("%s", strerror(errno
));
594 (void)snprintf(namebuf
, need
, "%s%s%s", targ
,
595 *targ
? "/" : "", cp
);
599 exists
= stat(np
, &stb
) == 0;
601 int mod_flag
= pflag
;
603 if (!S_ISDIR(stb
.st_mode
)) {
608 (void)chmod(np
, mode
);
610 /* Handle copying from a read-only directory */
612 if (mkdir(np
, mode
| S_IRWXU
) < 0)
619 if (utimes(np
, tv
) < 0)
620 run_err("%s: set times: %s",
621 np
, strerror(errno
));
624 (void)chmod(np
, mode
);
629 if ((ofd
= open(np
, O_WRONLY
|O_CREAT
, mode
)) < 0) {
630 bad
: run_err("%s: %s", np
, strerror(errno
));
633 (void)write(rem
, "", 1);
634 if ((bp
= allocbuf(&buffer
, ofd
, BUFSIZ
)) == NULL
) {
641 for (i
= 0; i
< size
; i
+= BUFSIZ
) {
647 j
= read(rem
, cp
, (size_t)amt
);
649 run_err("%s", j
? strerror(errno
) :
650 "dropped connection");
656 if (count
== bp
->cnt
) {
657 /* Keep reading so we stay sync'd up. */
659 j
= write(ofd
, bp
->buf
, (size_t)count
);
662 wrerrno
= j
>= 0 ? EIO
: errno
;
669 if (count
!= 0 && wrerr
== NO
&&
670 (j
= write(ofd
, bp
->buf
, (size_t)count
)) != count
) {
672 wrerrno
= j
>= 0 ? EIO
: errno
;
674 if (ftruncate(ofd
, size
)) {
675 run_err("%s: truncate: %s", np
, strerror(errno
));
679 if (exists
|| omode
!= mode
)
680 if (fchmod(ofd
, omode
))
681 run_err("%s: set mode: %s",
682 np
, strerror(errno
));
684 if (!exists
&& omode
!= mode
)
685 if (fchmod(ofd
, omode
& ~mask
))
686 run_err("%s: set mode: %s",
687 np
, strerror(errno
));
690 if (setimes
&& wrerr
== NO
) {
692 if (futimes(ofd
, tv
) < 0) {
693 run_err("%s: set times: %s",
694 np
, strerror(errno
));
701 if (setimes
&& wrerr
== NO
) {
703 if (utimes(np
, tv
) < 0) {
704 run_err("%s: set times: %s",
705 np
, strerror(errno
));
713 run_err("%s: write: %s", np
, strerror(wrerrno
));
716 (void)write(rem
, "", 1);
723 run_err("protocol error: %s", why
);
732 char ch
, *cp
, resp
, rbuf
[BUFSIZ
];
734 if (read(rem
, &resp
, sizeof(resp
)) != sizeof(resp
))
744 case 1: /* error, followed by error msg */
745 case 2: /* fatal error, "" */
747 if (read(rem
, &ch
, sizeof(ch
)) != sizeof(ch
))
750 } while (cp
< &rbuf
[BUFSIZ
] && ch
!= '\n');
753 (void)write(STDERR_FILENO
, rbuf
, (size_t)(cp
- rbuf
));
765 (void)fprintf(stderr
,
766 "usage: rcp [-46p] f1 f2; or: rcp [-46pr] f1 ... fn directory\n");
775 run_err(const char *fmt
, ...)
781 if (fp
== NULL
&& !(fp
= fdopen(rem
, "w")))
786 (void)fprintf(fp
, "%c", 0x01);
787 (void)fprintf(fp
, "rcp: ");
788 (void)vfprintf(fp
, fmt
, ap
);
789 (void)fprintf(fp
, "\n");