]>
git.saurik.com Git - apple/network_cmds.git/blob - rcp.tproj/rcp.c
e78621f9819640386171e1dd969e08d85bb1553a
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1983, 1990, 1992, 1993
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 #include <sys/param.h>
60 #include <sys/socket.h>
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
79 #include "pathnames.h"
83 #include <kerberosIV/des.h>
84 #include <kerberosIV/krb.h>
86 char dst_realm_buf
[REALM_SZ
];
87 char *dest_realm
= NULL
;
90 Key_schedule schedule
;
91 extern char *krb_realmofhost();
94 #define OPTIONS "dfKk:prtx"
96 #define OPTIONS "dfKk:prt"
99 #define OPTIONS "dfprt"
106 int pflag
, iamremote
, iamrecursive
, targetshouldbedirectory
;
109 char cmd
[CMDNEEDS
]; /* must hold "rcp -r -p -d\0" */
112 int kerberos
__P((char **, char *, char *, char *));
113 void oldw
__P((const char *, ...));
115 int response
__P((void));
116 void rsource
__P((char *, struct stat
*));
117 void sink
__P((int, char *[]));
118 void source
__P((int, char *[]));
119 void tolocal
__P((int, char *[]));
120 void toremote
__P((char *, int, char *[]));
121 void usage
__P((void));
129 int ch
, fflag
, tflag
;
133 while ((ch
= getopt(argc
, argv
, OPTIONS
)) != EOF
)
134 switch(ch
) { /* User-visible flags. */
142 dest_realm
= dst_realm_buf
;
143 (void)strncpy(dst_realm_buf
, optarg
, REALM_SZ
);
148 /* des_set_key(cred.session, schedule); */
158 /* Server options. */
160 targetshouldbedirectory
= 1;
162 case 'f': /* "from" */
180 shell
= doencrypt
? "ekshell" : "kshell";
184 if ((sp
= getservbyname(shell
, "tcp")) == NULL
) {
186 oldw("can't get entry for %s/tcp service", shell
);
187 sp
= getservbyname(shell
= "shell", "tcp");
190 sp
= getservbyname(shell
= "shell", "tcp");
192 sp
= getservbyname(shell
= "shell", "tcp");
195 errx(1, "%s/tcp: unknown service", shell
);
198 if ((pwd
= getpwuid(userid
= getuid())) == NULL
)
199 errx(1, "unknown user %d", (int)userid
);
201 rem
= STDIN_FILENO
; /* XXX */
203 if (fflag
) { /* Follow "protocol", send data. */
205 (void)setuid(userid
);
210 if (tflag
) { /* Receive data. */
211 (void)setuid(userid
);
219 targetshouldbedirectory
= 1;
222 /* Command to be executed on remote system using "rsh". */
224 (void)snprintf(cmd
, sizeof(cmd
),
225 "rcp%s%s%s%s", iamrecursive
? " -r" : "",
227 (doencrypt
&& use_kerberos
? " -x" : ""),
231 pflag
? " -p" : "", targetshouldbedirectory
? " -d" : "");
233 (void)snprintf(cmd
, sizeof(cmd
), "rcp%s%s%s",
234 iamrecursive
? " -r" : "", pflag
? " -p" : "",
235 targetshouldbedirectory
? " -d" : "");
238 (void)signal(SIGPIPE
, lostconn
);
240 if (targ
= colon(argv
[argc
- 1])) /* Dest is remote host. */
241 toremote(targ
, argc
, argv
);
243 tolocal(argc
, argv
); /* Dest is local host. */
244 if (targetshouldbedirectory
)
245 verifydir(argv
[argc
- 1]);
251 toremote(targ
, argc
, argv
)
256 char *bp
, *host
, *src
, *suser
, *thost
, *tuser
;
262 if (thost
= strchr(argv
[argc
- 1], '@')) {
265 tuser
= argv
[argc
- 1];
268 else if (!okname(tuser
))
271 thost
= argv
[argc
- 1];
275 for (i
= 0; i
< argc
- 1; i
++) {
276 src
= colon(argv
[i
]);
277 if (src
) { /* remote to remote */
281 host
= strchr(argv
[i
], '@');
282 len
= strlen(_PATH_RSH
) + strlen(argv
[i
]) +
283 strlen(src
) + (tuser
? strlen(tuser
) : 0) +
284 strlen(thost
) + strlen(targ
) + CMDNEEDS
+ 20;
285 if (!(bp
= malloc(len
)))
291 suser
= pwd
->pw_name
;
292 else if (!okname(suser
))
294 (void)snprintf(bp
, len
,
295 "%s %s -l %s -n %s %s '%s%s%s:%s'",
296 _PATH_RSH
, host
, suser
, cmd
, src
,
297 tuser
? tuser
: "", tuser
? "@" : "",
300 (void)snprintf(bp
, len
,
301 "exec %s %s -n %s %s '%s%s%s:%s'",
302 _PATH_RSH
, argv
[i
], cmd
, src
,
303 tuser
? tuser
: "", tuser
? "@" : "",
305 (void)susystem(bp
, userid
);
307 } else { /* local to remote */
309 len
= strlen(targ
) + CMDNEEDS
+ 20;
310 if (!(bp
= malloc(len
)))
312 (void)snprintf(bp
, len
, "%s -t %s", cmd
, targ
);
316 rem
= kerberos(&host
, bp
,
318 tuser
? tuser
: pwd
->pw_name
);
321 rem
= rcmd(&host
, port
, pwd
->pw_name
,
322 tuser
? tuser
: pwd
->pw_name
,
326 tos
= IPTOS_THROUGHPUT
;
327 if (setsockopt(rem
, IPPROTO_IP
, IP_TOS
,
328 &tos
, sizeof(int)) < 0)
329 warn("TOS (ignored)");
333 (void)setuid(userid
);
346 char *bp
, *host
, *src
, *suser
;
348 for (i
= 0; i
< argc
- 1; i
++) {
349 if (!(src
= colon(argv
[i
]))) { /* Local to local. */
350 len
= strlen(_PATH_CP
) + strlen(argv
[i
]) +
351 strlen(argv
[argc
- 1]) + 20;
352 if (!(bp
= malloc(len
)))
354 (void)snprintf(bp
, len
, "exec %s%s%s %s %s", _PATH_CP
,
355 iamrecursive
? " -r" : "", pflag
? " -p" : "",
356 argv
[i
], argv
[argc
- 1]);
357 if (susystem(bp
, userid
))
365 if ((host
= strchr(argv
[i
], '@')) == NULL
) {
367 suser
= pwd
->pw_name
;
372 suser
= pwd
->pw_name
;
373 else if (!okname(suser
))
376 len
= strlen(src
) + CMDNEEDS
+ 20;
377 if ((bp
= malloc(len
)) == NULL
)
379 (void)snprintf(bp
, len
, "%s -f %s", cmd
, src
);
383 kerberos(&host
, bp
, pwd
->pw_name
, suser
) :
385 rcmd(&host
, port
, pwd
->pw_name
, suser
, bp
, 0);
391 (void)seteuid(userid
);
392 tos
= IPTOS_THROUGHPUT
;
393 if (setsockopt(rem
, IPPROTO_IP
, IP_TOS
, &tos
, sizeof(int)) < 0)
394 warn("TOS (ignored)");
395 sink(1, argv
+ argc
- 1);
411 int amt
, fd
, haderr
, indx
, result
;
412 char *last
, *name
, buf
[BUFSIZ
];
414 for (indx
= 0; indx
< argc
; ++indx
) {
416 if ((fd
= open(name
, O_RDONLY
, 0)) < 0)
418 if (fstat(fd
, &stb
)) {
419 syserr
: run_err("%s: %s", name
, strerror(errno
));
422 switch (stb
.st_mode
& S_IFMT
) {
432 run_err("%s: not a regular file", name
);
435 if ((last
= strrchr(name
, '/')) == NULL
)
441 * Make it compatible with possible future
442 * versions expecting microseconds.
444 (void)snprintf(buf
, sizeof(buf
), "T%ld 0 %ld 0\n",
445 stb
.st_mtimespec
.tv_sec
, stb
.st_atimespec
.tv_sec
);
446 (void)write(rem
, buf
, strlen(buf
));
450 #define MODEMASK (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
451 (void)snprintf(buf
, sizeof(buf
), "C%04o %qd %s\n",
452 stb
.st_mode
& MODEMASK
, stb
.st_size
, last
);
453 (void)write(rem
, buf
, strlen(buf
));
456 if ((bp
= allocbuf(&buffer
, fd
, BUFSIZ
)) == NULL
) {
457 next
: (void)close(fd
);
461 /* Keep writing after an error so that we stay sync'd up. */
462 for (haderr
= i
= 0; i
< stb
.st_size
; i
+= bp
->cnt
) {
464 if (i
+ amt
> stb
.st_size
)
465 amt
= stb
.st_size
- i
;
467 result
= read(fd
, bp
->buf
, amt
);
469 haderr
= result
>= 0 ? EIO
: errno
;
472 (void)write(rem
, bp
->buf
, amt
);
474 result
= write(rem
, bp
->buf
, amt
);
476 haderr
= result
>= 0 ? EIO
: errno
;
479 if (close(fd
) && !haderr
)
482 (void)write(rem
, "", 1);
484 run_err("%s: %s", name
, strerror(haderr
));
496 char *last
, *vect
[1], path
[MAXPATHLEN
];
498 if (!(dirp
= opendir(name
))) {
499 run_err("%s: %s", name
, strerror(errno
));
502 last
= strrchr(name
, '/');
508 (void)snprintf(path
, sizeof(path
), "T%ld 0 %ld 0\n",
509 statp
->st_mtimespec
.tv_sec
, statp
->st_atimespec
.tv_sec
);
510 (void)write(rem
, path
, strlen(path
));
511 if (response() < 0) {
516 (void)snprintf(path
, sizeof(path
),
517 "D%04o %d %s\n", statp
->st_mode
& MODEMASK
, 0, last
);
518 (void)write(rem
, path
, strlen(path
));
519 if (response() < 0) {
523 while (dp
= readdir(dirp
)) {
526 if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, ".."))
528 if (strlen(name
) + 1 + strlen(dp
->d_name
) >= MAXPATHLEN
- 1) {
529 run_err("%s/%s: name too long", name
, dp
->d_name
);
532 (void)snprintf(path
, sizeof(path
), "%s/%s", name
, dp
->d_name
);
536 (void)closedir(dirp
);
537 (void)write(rem
, "E\n", 2);
548 struct timeval tv
[2];
549 enum { YES
, NO
, DISPLAYED
} wrerr
;
552 int amt
, count
, exists
, first
, mask
, mode
, ofd
, omode
;
553 int setimes
, size
, targisdir
, wrerrno
;
554 char ch
, *cp
, *np
, *targ
, *why
, *vect
[1], buf
[BUFSIZ
];
558 #define SCREWUP(str) { why = str; goto screwup; }
560 setimes
= targisdir
= 0;
565 run_err("ambiguous target");
569 if (targetshouldbedirectory
)
571 (void)write(rem
, "", 1);
572 if (stat(targ
, &stb
) == 0 && S_ISDIR(stb
.st_mode
))
574 for (first
= 1;; first
= 0) {
576 if (read(rem
, cp
, 1) <= 0)
579 SCREWUP("unexpected <newline>");
581 if (read(rem
, &ch
, sizeof(ch
)) != sizeof(ch
))
582 SCREWUP("lost connection");
584 } while (cp
< &buf
[BUFSIZ
- 1] && ch
!= '\n');
587 if (buf
[0] == '\01' || buf
[0] == '\02') {
589 (void)write(STDERR_FILENO
,
590 buf
+ 1, strlen(buf
+ 1));
597 (void)write(rem
, "", 1);
604 #define getnum(t) (t) = 0; while (isdigit(*cp)) (t) = (t) * 10 + (*cp++ - '0');
609 getnum(mtime
.tv_sec
);
611 SCREWUP("mtime.sec not delimited");
612 getnum(mtime
.tv_usec
);
614 SCREWUP("mtime.usec not delimited");
615 getnum(atime
.tv_sec
);
617 SCREWUP("atime.sec not delimited");
618 getnum(atime
.tv_usec
);
620 SCREWUP("atime.usec not delimited");
621 (void)write(rem
, "", 1);
624 if (*cp
!= 'C' && *cp
!= 'D') {
626 * Check for the case "rcp remote:foo\* local:bar".
627 * In this case, the line "No match." can be returned
628 * by the shell before the rcp command on the remote is
629 * executed so the ^Aerror_message convention isn't
636 SCREWUP("expected control record");
639 for (++cp
; cp
< buf
+ 5; cp
++) {
640 if (*cp
< '0' || *cp
> '7')
642 mode
= (mode
<< 3) | (*cp
- '0');
645 SCREWUP("mode not delimited");
647 for (size
= 0; isdigit(*cp
);)
648 size
= size
* 10 + (*cp
++ - '0');
650 SCREWUP("size not delimited");
652 static char *namebuf
;
656 need
= strlen(targ
) + strlen(cp
) + 250;
657 if (need
> cursize
) {
658 if (!(namebuf
= malloc(need
)))
659 run_err("%s", strerror(errno
));
661 (void)snprintf(namebuf
, need
, "%s%s%s", targ
,
662 *targ
? "/" : "", cp
);
666 exists
= stat(np
, &stb
) == 0;
668 int mod_flag
= pflag
;
670 if (!S_ISDIR(stb
.st_mode
)) {
675 (void)chmod(np
, mode
);
677 /* Handle copying from a read-only directory */
679 if (mkdir(np
, mode
| S_IRWXU
) < 0)
686 if (utimes(np
, tv
) < 0)
687 run_err("%s: set times: %s",
688 np
, strerror(errno
));
691 (void)chmod(np
, mode
);
696 if ((ofd
= open(np
, O_WRONLY
|O_CREAT
, mode
)) < 0) {
697 bad
: run_err("%s: %s", np
, strerror(errno
));
700 (void)write(rem
, "", 1);
701 if ((bp
= allocbuf(&buffer
, ofd
, BUFSIZ
)) == NULL
) {
707 for (count
= i
= 0; i
< size
; i
+= BUFSIZ
) {
713 j
= read(rem
, cp
, amt
);
715 run_err("%s", j
? strerror(errno
) :
716 "dropped connection");
722 if (count
== bp
->cnt
) {
723 /* Keep reading so we stay sync'd up. */
725 j
= write(ofd
, bp
->buf
, count
);
728 wrerrno
= j
>= 0 ? EIO
: errno
;
735 if (count
!= 0 && wrerr
== NO
&&
736 (j
= write(ofd
, bp
->buf
, count
)) != count
) {
738 wrerrno
= j
>= 0 ? EIO
: errno
;
740 if (ftruncate(ofd
, size
)) {
741 run_err("%s: truncate: %s", np
, strerror(errno
));
745 if (exists
|| omode
!= mode
)
746 if (fchmod(ofd
, omode
))
747 run_err("%s: set mode: %s",
748 np
, strerror(errno
));
750 if (!exists
&& omode
!= mode
)
751 if (fchmod(ofd
, omode
& ~mask
))
752 run_err("%s: set mode: %s",
753 np
, strerror(errno
));
757 if (setimes
&& wrerr
== NO
) {
759 if (utimes(np
, tv
) < 0) {
760 run_err("%s: set times: %s",
761 np
, strerror(errno
));
767 run_err("%s: %s", np
, strerror(wrerrno
));
770 (void)write(rem
, "", 1);
777 run_err("protocol error: %s", why
);
783 kerberos(host
, bp
, locuser
, user
)
784 char **host
, *bp
, *locuser
, *user
;
792 if (dest_realm
== NULL
)
793 dest_realm
= krb_realmofhost(*host
);
798 port
, user
, bp
, 0, dest_realm
, &cred
, schedule
) :
800 krcmd(host
, port
, user
, bp
, 0, dest_realm
);
804 if ((sp
= getservbyname("shell", "tcp")) == NULL
)
805 errx(1, "unknown service shell/tcp");
806 if (errno
== ECONNREFUSED
)
807 oldw("remote host doesn't support Kerberos");
808 else if (errno
== ENOENT
)
809 oldw("can't provide Kerberos authentication data");
817 "the -x option requires Kerberos authentication");
819 rem
= rcmd(host
, port
, locuser
, user
, bp
, 0);
823 #endif /* KERBEROS */
828 char ch
, *cp
, resp
, rbuf
[BUFSIZ
];
830 if (read(rem
, &resp
, sizeof(resp
)) != sizeof(resp
))
840 case 1: /* error, followed by error msg */
841 case 2: /* fatal error, "" */
843 if (read(rem
, &ch
, sizeof(ch
)) != sizeof(ch
))
846 } while (cp
< &rbuf
[BUFSIZ
] && ch
!= '\n');
849 (void)write(STDERR_FILENO
, rbuf
, cp
- rbuf
);
863 (void)fprintf(stderr
, "%s\n\t%s\n",
864 "usage: rcp [-Kpx] [-k realm] f1 f2",
865 "or: rcp [-Kprx] [-k realm] f1 ... fn directory");
867 (void)fprintf(stderr
, "%s\n\t%s\n",
868 "usage: rcp [-Kp] [-k realm] f1 f2",
869 "or: rcp [-Kpr] [-k realm] f1 ... fn directory");
872 (void)fprintf(stderr
,
873 "usage: rcp [-p] f1 f2; or: rcp [-pr] f1 ... fn directory\n");
887 oldw(const char *fmt
, ...)
900 (void)fprintf(stderr
, "rcp: ");
901 (void)vfprintf(stderr
, fmt
, ap
);
902 (void)fprintf(stderr
, ", using standard rcp\n");
909 run_err(const char *fmt
, ...)
911 run_err(fmt
, va_alist
)
925 if (fp
== NULL
&& !(fp
= fdopen(rem
, "w")))
927 (void)fprintf(fp
, "%c", 0x01);
928 (void)fprintf(fp
, "rcp: ");
929 (void)vfprintf(fp
, fmt
, ap
);
930 (void)fprintf(fp
, "\n");