]>
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 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1983, 1990, 1992, 1993
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include <sys/param.h>
62 #include <sys/socket.h>
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/ip.h>
81 #include "pathnames.h"
85 #include <kerberosIV/des.h>
86 #include <kerberosIV/krb.h>
88 char dst_realm_buf
[REALM_SZ
];
89 char *dest_realm
= NULL
;
92 Key_schedule schedule
;
93 extern char *krb_realmofhost();
96 #define OPTIONS "dfKk:prtx"
98 #define OPTIONS "dfKk:prt"
101 #define OPTIONS "dfprt"
108 int pflag
, iamremote
, iamrecursive
, targetshouldbedirectory
;
111 char cmd
[CMDNEEDS
]; /* must hold "rcp -r -p -d\0" */
114 int kerberos
__P((char **, char *, char *, char *));
115 void oldw
__P((const char *, ...));
117 int response
__P((void));
118 void rsource
__P((char *, struct stat
*));
119 void sink
__P((int, char *[]));
120 void source
__P((int, char *[]));
121 void tolocal
__P((int, char *[]));
122 void toremote
__P((char *, int, char *[]));
123 void usage
__P((void));
131 int ch
, fflag
, tflag
;
135 while ((ch
= getopt(argc
, argv
, OPTIONS
)) != EOF
)
136 switch(ch
) { /* User-visible flags. */
144 dest_realm
= dst_realm_buf
;
145 (void)strncpy(dst_realm_buf
, optarg
, REALM_SZ
);
150 /* des_set_key(cred.session, schedule); */
160 /* Server options. */
162 targetshouldbedirectory
= 1;
164 case 'f': /* "from" */
182 shell
= doencrypt
? "ekshell" : "kshell";
186 if ((sp
= getservbyname(shell
, "tcp")) == NULL
) {
188 oldw("can't get entry for %s/tcp service", shell
);
189 sp
= getservbyname(shell
= "shell", "tcp");
192 sp
= getservbyname(shell
= "shell", "tcp");
194 sp
= getservbyname(shell
= "shell", "tcp");
197 errx(1, "%s/tcp: unknown service", shell
);
200 if ((pwd
= getpwuid(userid
= getuid())) == NULL
)
201 errx(1, "unknown user %d", (int)userid
);
203 rem
= STDIN_FILENO
; /* XXX */
205 if (fflag
) { /* Follow "protocol", send data. */
207 (void)setuid(userid
);
212 if (tflag
) { /* Receive data. */
213 (void)setuid(userid
);
221 targetshouldbedirectory
= 1;
224 /* Command to be executed on remote system using "rsh". */
226 (void)snprintf(cmd
, sizeof(cmd
),
227 "rcp%s%s%s%s", iamrecursive
? " -r" : "",
229 (doencrypt
&& use_kerberos
? " -x" : ""),
233 pflag
? " -p" : "", targetshouldbedirectory
? " -d" : "");
235 (void)snprintf(cmd
, sizeof(cmd
), "rcp%s%s%s",
236 iamrecursive
? " -r" : "", pflag
? " -p" : "",
237 targetshouldbedirectory
? " -d" : "");
240 (void)signal(SIGPIPE
, lostconn
);
242 if (targ
= colon(argv
[argc
- 1])) /* Dest is remote host. */
243 toremote(targ
, argc
, argv
);
245 tolocal(argc
, argv
); /* Dest is local host. */
246 if (targetshouldbedirectory
)
247 verifydir(argv
[argc
- 1]);
253 toremote(targ
, argc
, argv
)
258 char *bp
, *host
, *src
, *suser
, *thost
, *tuser
;
264 if (thost
= strchr(argv
[argc
- 1], '@')) {
267 tuser
= argv
[argc
- 1];
270 else if (!okname(tuser
))
273 thost
= argv
[argc
- 1];
277 for (i
= 0; i
< argc
- 1; i
++) {
278 src
= colon(argv
[i
]);
279 if (src
) { /* remote to remote */
283 host
= strchr(argv
[i
], '@');
284 len
= strlen(_PATH_RSH
) + strlen(argv
[i
]) +
285 strlen(src
) + (tuser
? strlen(tuser
) : 0) +
286 strlen(thost
) + strlen(targ
) + CMDNEEDS
+ 20;
287 if (!(bp
= malloc(len
)))
293 suser
= pwd
->pw_name
;
294 else if (!okname(suser
))
296 (void)snprintf(bp
, len
,
297 "%s %s -l %s -n %s %s '%s%s%s:%s'",
298 _PATH_RSH
, host
, suser
, cmd
, src
,
299 tuser
? tuser
: "", tuser
? "@" : "",
302 (void)snprintf(bp
, len
,
303 "exec %s %s -n %s %s '%s%s%s:%s'",
304 _PATH_RSH
, argv
[i
], cmd
, src
,
305 tuser
? tuser
: "", tuser
? "@" : "",
307 (void)susystem(bp
, userid
);
309 } else { /* local to remote */
311 len
= strlen(targ
) + CMDNEEDS
+ 20;
312 if (!(bp
= malloc(len
)))
314 (void)snprintf(bp
, len
, "%s -t %s", cmd
, targ
);
318 rem
= kerberos(&host
, bp
,
320 tuser
? tuser
: pwd
->pw_name
);
323 rem
= rcmd(&host
, port
, pwd
->pw_name
,
324 tuser
? tuser
: pwd
->pw_name
,
328 tos
= IPTOS_THROUGHPUT
;
329 if (setsockopt(rem
, IPPROTO_IP
, IP_TOS
,
330 &tos
, sizeof(int)) < 0)
331 warn("TOS (ignored)");
335 (void)setuid(userid
);
348 char *bp
, *host
, *src
, *suser
;
350 for (i
= 0; i
< argc
- 1; i
++) {
351 if (!(src
= colon(argv
[i
]))) { /* Local to local. */
352 len
= strlen(_PATH_CP
) + strlen(argv
[i
]) +
353 strlen(argv
[argc
- 1]) + 20;
354 if (!(bp
= malloc(len
)))
356 (void)snprintf(bp
, len
, "exec %s%s%s %s %s", _PATH_CP
,
357 iamrecursive
? " -r" : "", pflag
? " -p" : "",
358 argv
[i
], argv
[argc
- 1]);
359 if (susystem(bp
, userid
))
367 if ((host
= strchr(argv
[i
], '@')) == NULL
) {
369 suser
= pwd
->pw_name
;
374 suser
= pwd
->pw_name
;
375 else if (!okname(suser
))
378 len
= strlen(src
) + CMDNEEDS
+ 20;
379 if ((bp
= malloc(len
)) == NULL
)
381 (void)snprintf(bp
, len
, "%s -f %s", cmd
, src
);
385 kerberos(&host
, bp
, pwd
->pw_name
, suser
) :
387 rcmd(&host
, port
, pwd
->pw_name
, suser
, bp
, 0);
393 (void)seteuid(userid
);
394 tos
= IPTOS_THROUGHPUT
;
395 if (setsockopt(rem
, IPPROTO_IP
, IP_TOS
, &tos
, sizeof(int)) < 0)
396 warn("TOS (ignored)");
397 sink(1, argv
+ argc
- 1);
413 int amt
, fd
, haderr
, indx
, result
;
414 char *last
, *name
, buf
[BUFSIZ
];
416 for (indx
= 0; indx
< argc
; ++indx
) {
418 if ((fd
= open(name
, O_RDONLY
, 0)) < 0)
420 if (fstat(fd
, &stb
)) {
421 syserr
: run_err("%s: %s", name
, strerror(errno
));
424 switch (stb
.st_mode
& S_IFMT
) {
434 run_err("%s: not a regular file", name
);
437 if ((last
= strrchr(name
, '/')) == NULL
)
443 * Make it compatible with possible future
444 * versions expecting microseconds.
446 (void)snprintf(buf
, sizeof(buf
), "T%ld 0 %ld 0\n",
447 stb
.st_mtimespec
.tv_sec
, stb
.st_atimespec
.tv_sec
);
448 (void)write(rem
, buf
, strlen(buf
));
452 #define MODEMASK (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO)
453 (void)snprintf(buf
, sizeof(buf
), "C%04o %qd %s\n",
454 stb
.st_mode
& MODEMASK
, stb
.st_size
, last
);
455 (void)write(rem
, buf
, strlen(buf
));
458 if ((bp
= allocbuf(&buffer
, fd
, BUFSIZ
)) == NULL
) {
459 next
: (void)close(fd
);
463 /* Keep writing after an error so that we stay sync'd up. */
464 for (haderr
= i
= 0; i
< stb
.st_size
; i
+= bp
->cnt
) {
466 if (i
+ amt
> stb
.st_size
)
467 amt
= stb
.st_size
- i
;
469 result
= read(fd
, bp
->buf
, amt
);
471 haderr
= result
>= 0 ? EIO
: errno
;
474 (void)write(rem
, bp
->buf
, amt
);
476 result
= write(rem
, bp
->buf
, amt
);
478 haderr
= result
>= 0 ? EIO
: errno
;
481 if (close(fd
) && !haderr
)
484 (void)write(rem
, "", 1);
486 run_err("%s: %s", name
, strerror(haderr
));
498 char *last
, *vect
[1], path
[MAXPATHLEN
];
500 if (!(dirp
= opendir(name
))) {
501 run_err("%s: %s", name
, strerror(errno
));
504 last
= strrchr(name
, '/');
510 (void)snprintf(path
, sizeof(path
), "T%ld 0 %ld 0\n",
511 statp
->st_mtimespec
.tv_sec
, statp
->st_atimespec
.tv_sec
);
512 (void)write(rem
, path
, strlen(path
));
513 if (response() < 0) {
518 (void)snprintf(path
, sizeof(path
),
519 "D%04o %d %s\n", statp
->st_mode
& MODEMASK
, 0, last
);
520 (void)write(rem
, path
, strlen(path
));
521 if (response() < 0) {
525 while (dp
= readdir(dirp
)) {
528 if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, ".."))
530 if (strlen(name
) + 1 + strlen(dp
->d_name
) >= MAXPATHLEN
- 1) {
531 run_err("%s/%s: name too long", name
, dp
->d_name
);
534 (void)snprintf(path
, sizeof(path
), "%s/%s", name
, dp
->d_name
);
538 (void)closedir(dirp
);
539 (void)write(rem
, "E\n", 2);
550 struct timeval tv
[2];
551 enum { YES
, NO
, DISPLAYED
} wrerr
;
554 int amt
, count
, exists
, first
, mask
, mode
, ofd
, omode
;
555 int setimes
, size
, targisdir
, wrerrno
;
556 char ch
, *cp
, *np
, *targ
, *why
, *vect
[1], buf
[BUFSIZ
];
560 #define SCREWUP(str) { why = str; goto screwup; }
562 setimes
= targisdir
= 0;
567 run_err("ambiguous target");
571 if (targetshouldbedirectory
)
573 (void)write(rem
, "", 1);
574 if (stat(targ
, &stb
) == 0 && S_ISDIR(stb
.st_mode
))
576 for (first
= 1;; first
= 0) {
578 if (read(rem
, cp
, 1) <= 0)
581 SCREWUP("unexpected <newline>");
583 if (read(rem
, &ch
, sizeof(ch
)) != sizeof(ch
))
584 SCREWUP("lost connection");
586 } while (cp
< &buf
[BUFSIZ
- 1] && ch
!= '\n');
589 if (buf
[0] == '\01' || buf
[0] == '\02') {
591 (void)write(STDERR_FILENO
,
592 buf
+ 1, strlen(buf
+ 1));
599 (void)write(rem
, "", 1);
606 #define getnum(t) (t) = 0; while (isdigit(*cp)) (t) = (t) * 10 + (*cp++ - '0');
611 getnum(mtime
.tv_sec
);
613 SCREWUP("mtime.sec not delimited");
614 getnum(mtime
.tv_usec
);
616 SCREWUP("mtime.usec not delimited");
617 getnum(atime
.tv_sec
);
619 SCREWUP("atime.sec not delimited");
620 getnum(atime
.tv_usec
);
622 SCREWUP("atime.usec not delimited");
623 (void)write(rem
, "", 1);
626 if (*cp
!= 'C' && *cp
!= 'D') {
628 * Check for the case "rcp remote:foo\* local:bar".
629 * In this case, the line "No match." can be returned
630 * by the shell before the rcp command on the remote is
631 * executed so the ^Aerror_message convention isn't
638 SCREWUP("expected control record");
641 for (++cp
; cp
< buf
+ 5; cp
++) {
642 if (*cp
< '0' || *cp
> '7')
644 mode
= (mode
<< 3) | (*cp
- '0');
647 SCREWUP("mode not delimited");
649 for (size
= 0; isdigit(*cp
);)
650 size
= size
* 10 + (*cp
++ - '0');
652 SCREWUP("size not delimited");
654 static char *namebuf
;
658 need
= strlen(targ
) + strlen(cp
) + 250;
659 if (need
> cursize
) {
660 if (!(namebuf
= malloc(need
)))
661 run_err("%s", strerror(errno
));
663 (void)snprintf(namebuf
, need
, "%s%s%s", targ
,
664 *targ
? "/" : "", cp
);
668 exists
= stat(np
, &stb
) == 0;
670 int mod_flag
= pflag
;
672 if (!S_ISDIR(stb
.st_mode
)) {
677 (void)chmod(np
, mode
);
679 /* Handle copying from a read-only directory */
681 if (mkdir(np
, mode
| S_IRWXU
) < 0)
688 if (utimes(np
, tv
) < 0)
689 run_err("%s: set times: %s",
690 np
, strerror(errno
));
693 (void)chmod(np
, mode
);
698 if ((ofd
= open(np
, O_WRONLY
|O_CREAT
, mode
)) < 0) {
699 bad
: run_err("%s: %s", np
, strerror(errno
));
702 (void)write(rem
, "", 1);
703 if ((bp
= allocbuf(&buffer
, ofd
, BUFSIZ
)) == NULL
) {
709 for (count
= i
= 0; i
< size
; i
+= BUFSIZ
) {
715 j
= read(rem
, cp
, amt
);
717 run_err("%s", j
? strerror(errno
) :
718 "dropped connection");
724 if (count
== bp
->cnt
) {
725 /* Keep reading so we stay sync'd up. */
727 j
= write(ofd
, bp
->buf
, count
);
730 wrerrno
= j
>= 0 ? EIO
: errno
;
737 if (count
!= 0 && wrerr
== NO
&&
738 (j
= write(ofd
, bp
->buf
, count
)) != count
) {
740 wrerrno
= j
>= 0 ? EIO
: errno
;
742 if (ftruncate(ofd
, size
)) {
743 run_err("%s: truncate: %s", np
, strerror(errno
));
747 if (exists
|| omode
!= mode
)
748 if (fchmod(ofd
, omode
))
749 run_err("%s: set mode: %s",
750 np
, strerror(errno
));
752 if (!exists
&& omode
!= mode
)
753 if (fchmod(ofd
, omode
& ~mask
))
754 run_err("%s: set mode: %s",
755 np
, strerror(errno
));
759 if (setimes
&& wrerr
== NO
) {
761 if (utimes(np
, tv
) < 0) {
762 run_err("%s: set times: %s",
763 np
, strerror(errno
));
769 run_err("%s: %s", np
, strerror(wrerrno
));
772 (void)write(rem
, "", 1);
779 run_err("protocol error: %s", why
);
785 kerberos(host
, bp
, locuser
, user
)
786 char **host
, *bp
, *locuser
, *user
;
794 if (dest_realm
== NULL
)
795 dest_realm
= krb_realmofhost(*host
);
800 port
, user
, bp
, 0, dest_realm
, &cred
, schedule
) :
802 krcmd(host
, port
, user
, bp
, 0, dest_realm
);
806 if ((sp
= getservbyname("shell", "tcp")) == NULL
)
807 errx(1, "unknown service shell/tcp");
808 if (errno
== ECONNREFUSED
)
809 oldw("remote host doesn't support Kerberos");
810 else if (errno
== ENOENT
)
811 oldw("can't provide Kerberos authentication data");
819 "the -x option requires Kerberos authentication");
821 rem
= rcmd(host
, port
, locuser
, user
, bp
, 0);
825 #endif /* KERBEROS */
830 char ch
, *cp
, resp
, rbuf
[BUFSIZ
];
832 if (read(rem
, &resp
, sizeof(resp
)) != sizeof(resp
))
842 case 1: /* error, followed by error msg */
843 case 2: /* fatal error, "" */
845 if (read(rem
, &ch
, sizeof(ch
)) != sizeof(ch
))
848 } while (cp
< &rbuf
[BUFSIZ
] && ch
!= '\n');
851 (void)write(STDERR_FILENO
, rbuf
, cp
- rbuf
);
865 (void)fprintf(stderr
, "%s\n\t%s\n",
866 "usage: rcp [-Kpx] [-k realm] f1 f2",
867 "or: rcp [-Kprx] [-k realm] f1 ... fn directory");
869 (void)fprintf(stderr
, "%s\n\t%s\n",
870 "usage: rcp [-Kp] [-k realm] f1 f2",
871 "or: rcp [-Kpr] [-k realm] f1 ... fn directory");
874 (void)fprintf(stderr
,
875 "usage: rcp [-p] f1 f2; or: rcp [-pr] f1 ... fn directory\n");
889 oldw(const char *fmt
, ...)
902 (void)fprintf(stderr
, "rcp: ");
903 (void)vfprintf(stderr
, fmt
, ap
);
904 (void)fprintf(stderr
, ", using standard rcp\n");
911 run_err(const char *fmt
, ...)
913 run_err(fmt
, va_alist
)
927 if (fp
== NULL
&& !(fp
= fdopen(rem
, "w")))
929 (void)fprintf(fp
, "%c", 0x01);
930 (void)fprintf(fp
, "rcp: ");
931 (void)vfprintf(fp
, fmt
, ap
);
932 (void)fprintf(fp
, "\n");