]>
git.saurik.com Git - apple/network_cmds.git/blob - rcp.tproj/rcp.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1983, 1990, 1992, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 #include <sys/param.h>
61 #include <sys/socket.h>
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
80 #include "pathnames.h"
84 #include <kerberosIV/des.h>
85 #include <kerberosIV/krb.h>
87 char dst_realm_buf
[REALM_SZ
];
88 char *dest_realm
= NULL
;
91 Key_schedule schedule
;
92 extern char *krb_realmofhost();
95 #define OPTIONS "dfKk:prtx"
97 #define OPTIONS "dfKk:prt"
100 #define OPTIONS "dfprt"
107 int pflag
, iamremote
, iamrecursive
, targetshouldbedirectory
;
110 char cmd
[CMDNEEDS
]; /* must hold "rcp -r -p -d\0" */
113 int kerberos
__P((char **, char *, char *, char *));
114 void oldw
__P((const char *, ...));
116 int response
__P((void));
117 void rsource
__P((char *, struct stat
*));
118 void sink
__P((int, char *[]));
119 void source
__P((int, char *[]));
120 void tolocal
__P((int, char *[]));
121 void toremote
__P((char *, int, char *[]));
122 void usage
__P((void));
130 int ch
, fflag
, tflag
;
134 while ((ch
= getopt(argc
, argv
, OPTIONS
)) != EOF
)
135 switch(ch
) { /* User-visible flags. */
143 dest_realm
= dst_realm_buf
;
144 (void)strncpy(dst_realm_buf
, optarg
, REALM_SZ
);
149 /* des_set_key(cred.session, schedule); */
159 /* Server options. */
161 targetshouldbedirectory
= 1;
163 case 'f': /* "from" */
181 shell
= doencrypt
? "ekshell" : "kshell";
185 if ((sp
= getservbyname(shell
, "tcp")) == NULL
) {
187 oldw("can't get entry for %s/tcp service", shell
);
188 sp
= getservbyname(shell
= "shell", "tcp");
191 sp
= getservbyname(shell
= "shell", "tcp");
193 sp
= getservbyname(shell
= "shell", "tcp");
196 errx(1, "%s/tcp: unknown service", shell
);
199 if ((pwd
= getpwuid(userid
= getuid())) == NULL
)
200 errx(1, "unknown user %d", (int)userid
);
202 rem
= STDIN_FILENO
; /* XXX */
204 if (fflag
) { /* Follow "protocol", send data. */
206 (void)setuid(userid
);
211 if (tflag
) { /* Receive data. */
212 (void)setuid(userid
);
220 targetshouldbedirectory
= 1;
223 /* Command to be executed on remote system using "rsh". */
225 (void)snprintf(cmd
, sizeof(cmd
),
226 "rcp%s%s%s%s", iamrecursive
? " -r" : "",
228 (doencrypt
&& use_kerberos
? " -x" : ""),
232 pflag
? " -p" : "", targetshouldbedirectory
? " -d" : "");
234 (void)snprintf(cmd
, sizeof(cmd
), "rcp%s%s%s",
235 iamrecursive
? " -r" : "", pflag
? " -p" : "",
236 targetshouldbedirectory
? " -d" : "");
239 (void)signal(SIGPIPE
, lostconn
);
241 if (targ
= colon(argv
[argc
- 1])) /* Dest is remote host. */
242 toremote(targ
, argc
, argv
);
244 tolocal(argc
, argv
); /* Dest is local host. */
245 if (targetshouldbedirectory
)
246 verifydir(argv
[argc
- 1]);
252 toremote(targ
, argc
, argv
)
257 char *bp
, *host
, *src
, *suser
, *thost
, *tuser
;
263 if (thost
= strchr(argv
[argc
- 1], '@')) {
266 tuser
= argv
[argc
- 1];
269 else if (!okname(tuser
))
272 thost
= argv
[argc
- 1];
276 for (i
= 0; i
< argc
- 1; i
++) {
277 src
= colon(argv
[i
]);
278 if (src
) { /* remote to remote */
282 host
= strchr(argv
[i
], '@');
283 len
= strlen(_PATH_RSH
) + strlen(argv
[i
]) +
284 strlen(src
) + (tuser
? strlen(tuser
) : 0) +
285 strlen(thost
) + strlen(targ
) + CMDNEEDS
+ 20;
286 if (!(bp
= malloc(len
)))
292 suser
= pwd
->pw_name
;
293 else if (!okname(suser
))
295 (void)snprintf(bp
, len
,
296 "%s %s -l %s -n %s %s '%s%s%s:%s'",
297 _PATH_RSH
, host
, suser
, cmd
, src
,
298 tuser
? tuser
: "", tuser
? "@" : "",
301 (void)snprintf(bp
, len
,
302 "exec %s %s -n %s %s '%s%s%s:%s'",
303 _PATH_RSH
, argv
[i
], cmd
, src
,
304 tuser
? tuser
: "", tuser
? "@" : "",
306 (void)susystem(bp
, userid
);
308 } else { /* local to remote */
310 len
= strlen(targ
) + CMDNEEDS
+ 20;
311 if (!(bp
= malloc(len
)))
313 (void)snprintf(bp
, len
, "%s -t %s", cmd
, targ
);
317 rem
= kerberos(&host
, bp
,
319 tuser
? tuser
: pwd
->pw_name
);
322 rem
= rcmd(&host
, port
, pwd
->pw_name
,
323 tuser
? tuser
: pwd
->pw_name
,
327 tos
= IPTOS_THROUGHPUT
;
328 if (setsockopt(rem
, IPPROTO_IP
, IP_TOS
,
329 &tos
, sizeof(int)) < 0)
330 warn("TOS (ignored)");
334 (void)setuid(userid
);
347 char *bp
, *host
, *src
, *suser
;
349 for (i
= 0; i
< argc
- 1; i
++) {
350 if (!(src
= colon(argv
[i
]))) { /* Local to local. */
351 len
= strlen(_PATH_CP
) + strlen(argv
[i
]) +
352 strlen(argv
[argc
- 1]) + 20;
353 if (!(bp
= malloc(len
)))
355 (void)snprintf(bp
, len
, "exec %s%s%s %s %s", _PATH_CP
,
356 iamrecursive
? " -r" : "", pflag
? " -p" : "",
357 argv
[i
], argv
[argc
- 1]);
358 if (susystem(bp
, userid
))
366 if ((host
= strchr(argv
[i
], '@')) == NULL
) {
368 suser
= pwd
->pw_name
;
373 suser
= pwd
->pw_name
;
374 else if (!okname(suser
))
377 len
= strlen(src
) + CMDNEEDS
+ 20;
378 if ((bp
= malloc(len
)) == NULL
)
380 (void)snprintf(bp
, len
, "%s -f %s", cmd
, src
);
384 kerberos(&host
, bp
, pwd
->pw_name
, suser
) :
386 rcmd(&host
, port
, pwd
->pw_name
, suser
, bp
, 0);
392 (void)seteuid(userid
);
393 tos
= IPTOS_THROUGHPUT
;
394 if (setsockopt(rem
, IPPROTO_IP
, IP_TOS
, &tos
, sizeof(int)) < 0)
395 warn("TOS (ignored)");
396 sink(1, argv
+ argc
- 1);
412 int amt
, fd
, haderr
, indx
, result
;
413 char *last
, *name
, buf
[BUFSIZ
];
415 for (indx
= 0; indx
< argc
; ++indx
) {
417 if ((fd
= open(name
, O_RDONLY
, 0)) < 0)
419 if (fstat(fd
, &stb
)) {
420 syserr
: run_err("%s: %s", name
, strerror(errno
));
423 switch (stb
.st_mode
& S_IFMT
) {
433 run_err("%s: not a regular file", name
);
436 if ((last
= strrchr(name
, '/')) == NULL
)
442 * Make it compatible with possible future
443 * versions expecting microseconds.
445 (void)snprintf(buf
, sizeof(buf
), "T%ld 0 %ld 0\n",
446 stb
.st_mtimespec
.tv_sec
, stb
.st_atimespec
.tv_sec
);
447 (void)write(rem
, buf
, strlen(buf
));
451 #define MODEMASK (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
452 (void)snprintf(buf
, sizeof(buf
), "C%04o %qd %s\n",
453 stb
.st_mode
& MODEMASK
, stb
.st_size
, last
);
454 (void)write(rem
, buf
, strlen(buf
));
457 if ((bp
= allocbuf(&buffer
, fd
, BUFSIZ
)) == NULL
) {
458 next
: (void)close(fd
);
462 /* Keep writing after an error so that we stay sync'd up. */
463 for (haderr
= i
= 0; i
< stb
.st_size
; i
+= bp
->cnt
) {
465 if (i
+ amt
> stb
.st_size
)
466 amt
= stb
.st_size
- i
;
468 result
= read(fd
, bp
->buf
, amt
);
470 haderr
= result
>= 0 ? EIO
: errno
;
473 (void)write(rem
, bp
->buf
, amt
);
475 result
= write(rem
, bp
->buf
, amt
);
477 haderr
= result
>= 0 ? EIO
: errno
;
480 if (close(fd
) && !haderr
)
483 (void)write(rem
, "", 1);
485 run_err("%s: %s", name
, strerror(haderr
));
497 char *last
, *vect
[1], path
[MAXPATHLEN
];
499 if (!(dirp
= opendir(name
))) {
500 run_err("%s: %s", name
, strerror(errno
));
503 last
= strrchr(name
, '/');
509 (void)snprintf(path
, sizeof(path
), "T%ld 0 %ld 0\n",
510 statp
->st_mtimespec
.tv_sec
, statp
->st_atimespec
.tv_sec
);
511 (void)write(rem
, path
, strlen(path
));
512 if (response() < 0) {
517 (void)snprintf(path
, sizeof(path
),
518 "D%04o %d %s\n", statp
->st_mode
& MODEMASK
, 0, last
);
519 (void)write(rem
, path
, strlen(path
));
520 if (response() < 0) {
524 while (dp
= readdir(dirp
)) {
527 if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, ".."))
529 if (strlen(name
) + 1 + strlen(dp
->d_name
) >= MAXPATHLEN
- 1) {
530 run_err("%s/%s: name too long", name
, dp
->d_name
);
533 (void)snprintf(path
, sizeof(path
), "%s/%s", name
, dp
->d_name
);
537 (void)closedir(dirp
);
538 (void)write(rem
, "E\n", 2);
549 struct timeval tv
[2];
550 enum { YES
, NO
, DISPLAYED
} wrerr
;
553 int amt
, count
, exists
, first
, mask
, mode
, ofd
, omode
;
554 int setimes
, size
, targisdir
, wrerrno
;
555 char ch
, *cp
, *np
, *targ
, *why
, *vect
[1], buf
[BUFSIZ
];
559 #define SCREWUP(str) { why = str; goto screwup; }
561 setimes
= targisdir
= 0;
566 run_err("ambiguous target");
570 if (targetshouldbedirectory
)
572 (void)write(rem
, "", 1);
573 if (stat(targ
, &stb
) == 0 && S_ISDIR(stb
.st_mode
))
575 for (first
= 1;; first
= 0) {
577 if (read(rem
, cp
, 1) <= 0)
580 SCREWUP("unexpected <newline>");
582 if (read(rem
, &ch
, sizeof(ch
)) != sizeof(ch
))
583 SCREWUP("lost connection");
585 } while (cp
< &buf
[BUFSIZ
- 1] && ch
!= '\n');
588 if (buf
[0] == '\01' || buf
[0] == '\02') {
590 (void)write(STDERR_FILENO
,
591 buf
+ 1, strlen(buf
+ 1));
598 (void)write(rem
, "", 1);
605 #define getnum(t) (t) = 0; while (isdigit(*cp)) (t) = (t) * 10 + (*cp++ - '0');
610 getnum(mtime
.tv_sec
);
612 SCREWUP("mtime.sec not delimited");
613 getnum(mtime
.tv_usec
);
615 SCREWUP("mtime.usec not delimited");
616 getnum(atime
.tv_sec
);
618 SCREWUP("atime.sec not delimited");
619 getnum(atime
.tv_usec
);
621 SCREWUP("atime.usec not delimited");
622 (void)write(rem
, "", 1);
625 if (*cp
!= 'C' && *cp
!= 'D') {
627 * Check for the case "rcp remote:foo\* local:bar".
628 * In this case, the line "No match." can be returned
629 * by the shell before the rcp command on the remote is
630 * executed so the ^Aerror_message convention isn't
637 SCREWUP("expected control record");
640 for (++cp
; cp
< buf
+ 5; cp
++) {
641 if (*cp
< '0' || *cp
> '7')
643 mode
= (mode
<< 3) | (*cp
- '0');
646 SCREWUP("mode not delimited");
648 for (size
= 0; isdigit(*cp
);)
649 size
= size
* 10 + (*cp
++ - '0');
651 SCREWUP("size not delimited");
653 static char *namebuf
;
657 need
= strlen(targ
) + strlen(cp
) + 250;
658 if (need
> cursize
) {
659 if (!(namebuf
= malloc(need
)))
660 run_err("%s", strerror(errno
));
662 (void)snprintf(namebuf
, need
, "%s%s%s", targ
,
663 *targ
? "/" : "", cp
);
667 exists
= stat(np
, &stb
) == 0;
669 int mod_flag
= pflag
;
671 if (!S_ISDIR(stb
.st_mode
)) {
676 (void)chmod(np
, mode
);
678 /* Handle copying from a read-only directory */
680 if (mkdir(np
, mode
| S_IRWXU
) < 0)
687 if (utimes(np
, tv
) < 0)
688 run_err("%s: set times: %s",
689 np
, strerror(errno
));
692 (void)chmod(np
, mode
);
697 if ((ofd
= open(np
, O_WRONLY
|O_CREAT
, mode
)) < 0) {
698 bad
: run_err("%s: %s", np
, strerror(errno
));
701 (void)write(rem
, "", 1);
702 if ((bp
= allocbuf(&buffer
, ofd
, BUFSIZ
)) == NULL
) {
708 for (count
= i
= 0; i
< size
; i
+= BUFSIZ
) {
714 j
= read(rem
, cp
, amt
);
716 run_err("%s", j
? strerror(errno
) :
717 "dropped connection");
723 if (count
== bp
->cnt
) {
724 /* Keep reading so we stay sync'd up. */
726 j
= write(ofd
, bp
->buf
, count
);
729 wrerrno
= j
>= 0 ? EIO
: errno
;
736 if (count
!= 0 && wrerr
== NO
&&
737 (j
= write(ofd
, bp
->buf
, count
)) != count
) {
739 wrerrno
= j
>= 0 ? EIO
: errno
;
741 if (ftruncate(ofd
, size
)) {
742 run_err("%s: truncate: %s", np
, strerror(errno
));
746 if (exists
|| omode
!= mode
)
747 if (fchmod(ofd
, omode
))
748 run_err("%s: set mode: %s",
749 np
, strerror(errno
));
751 if (!exists
&& omode
!= mode
)
752 if (fchmod(ofd
, omode
& ~mask
))
753 run_err("%s: set mode: %s",
754 np
, strerror(errno
));
758 if (setimes
&& wrerr
== NO
) {
760 if (utimes(np
, tv
) < 0) {
761 run_err("%s: set times: %s",
762 np
, strerror(errno
));
768 run_err("%s: %s", np
, strerror(wrerrno
));
771 (void)write(rem
, "", 1);
778 run_err("protocol error: %s", why
);
784 kerberos(host
, bp
, locuser
, user
)
785 char **host
, *bp
, *locuser
, *user
;
793 if (dest_realm
== NULL
)
794 dest_realm
= krb_realmofhost(*host
);
799 port
, user
, bp
, 0, dest_realm
, &cred
, schedule
) :
801 krcmd(host
, port
, user
, bp
, 0, dest_realm
);
805 if ((sp
= getservbyname("shell", "tcp")) == NULL
)
806 errx(1, "unknown service shell/tcp");
807 if (errno
== ECONNREFUSED
)
808 oldw("remote host doesn't support Kerberos");
809 else if (errno
== ENOENT
)
810 oldw("can't provide Kerberos authentication data");
818 "the -x option requires Kerberos authentication");
820 rem
= rcmd(host
, port
, locuser
, user
, bp
, 0);
824 #endif /* KERBEROS */
829 char ch
, *cp
, resp
, rbuf
[BUFSIZ
];
831 if (read(rem
, &resp
, sizeof(resp
)) != sizeof(resp
))
841 case 1: /* error, followed by error msg */
842 case 2: /* fatal error, "" */
844 if (read(rem
, &ch
, sizeof(ch
)) != sizeof(ch
))
847 } while (cp
< &rbuf
[BUFSIZ
] && ch
!= '\n');
850 (void)write(STDERR_FILENO
, rbuf
, cp
- rbuf
);
864 (void)fprintf(stderr
, "%s\n\t%s\n",
865 "usage: rcp [-Kpx] [-k realm] f1 f2",
866 "or: rcp [-Kprx] [-k realm] f1 ... fn directory");
868 (void)fprintf(stderr
, "%s\n\t%s\n",
869 "usage: rcp [-Kp] [-k realm] f1 f2",
870 "or: rcp [-Kpr] [-k realm] f1 ... fn directory");
873 (void)fprintf(stderr
,
874 "usage: rcp [-p] f1 f2; or: rcp [-pr] f1 ... fn directory\n");
888 oldw(const char *fmt
, ...)
901 (void)fprintf(stderr
, "rcp: ");
902 (void)vfprintf(stderr
, fmt
, ap
);
903 (void)fprintf(stderr
, ", using standard rcp\n");
910 run_err(const char *fmt
, ...)
912 run_err(fmt
, va_alist
)
926 if (fp
== NULL
&& !(fp
= fdopen(rem
, "w")))
928 (void)fprintf(fp
, "%c", 0x01);
929 (void)fprintf(fp
, "rcp: ");
930 (void)vfprintf(fp
, fmt
, ap
);
931 (void)fprintf(fp
, "\n");