rcp.c revision 34898
11556Srgrimes/* 21556Srgrimes * Copyright (c) 1983, 1990, 1992, 1993 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * Redistribution and use in source and binary forms, with or without 61556Srgrimes * modification, are permitted provided that the following conditions 71556Srgrimes * are met: 81556Srgrimes * 1. Redistributions of source code must retain the above copyright 91556Srgrimes * notice, this list of conditions and the following disclaimer. 101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111556Srgrimes * notice, this list of conditions and the following disclaimer in the 121556Srgrimes * documentation and/or other materials provided with the distribution. 131556Srgrimes * 3. All advertising materials mentioning features or use of this software 141556Srgrimes * must display the following acknowledgement: 151556Srgrimes * This product includes software developed by the University of 161556Srgrimes * California, Berkeley and its contributors. 171556Srgrimes * 4. Neither the name of the University nor the names of its contributors 181556Srgrimes * may be used to endorse or promote products derived from this software 191556Srgrimes * without specific prior written permission. 201556Srgrimes * 211556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311556Srgrimes * SUCH DAMAGE. 323044Sdg * 3334898Smarkm * $Id$ 341556Srgrimes */ 351556Srgrimes 361556Srgrimes#ifndef lint 3720420Sstevestatic char const copyright[] = 381556Srgrimes"@(#) Copyright (c) 1983, 1990, 1992, 1993\n\ 391556Srgrimes The Regents of the University of California. All rights reserved.\n"; 401556Srgrimes#endif /* not lint */ 411556Srgrimes 421556Srgrimes#ifndef lint 4320420Sstevestatic char const sccsid[] = "@(#)rcp.c 8.2 (Berkeley) 4/2/94"; 441556Srgrimes#endif /* not lint */ 451556Srgrimes 461556Srgrimes#include <sys/param.h> 471556Srgrimes#include <sys/stat.h> 481556Srgrimes#include <sys/time.h> 491556Srgrimes#include <sys/socket.h> 501556Srgrimes#include <netinet/in.h> 511556Srgrimes#include <netinet/in_systm.h> 521556Srgrimes#include <netinet/ip.h> 531556Srgrimes 541556Srgrimes#include <ctype.h> 551556Srgrimes#include <dirent.h> 561556Srgrimes#include <err.h> 571556Srgrimes#include <errno.h> 581556Srgrimes#include <fcntl.h> 591556Srgrimes#include <netdb.h> 601556Srgrimes#include <pwd.h> 611556Srgrimes#include <signal.h> 621556Srgrimes#include <stdio.h> 631556Srgrimes#include <stdlib.h> 641556Srgrimes#include <string.h> 651556Srgrimes#include <string.h> 661556Srgrimes#include <unistd.h> 671556Srgrimes 681556Srgrimes#include "pathnames.h" 691556Srgrimes#include "extern.h" 701556Srgrimes 711556Srgrimes#ifdef KERBEROS 7214024Smarkm#include <des.h> 7329914Smarkm#include <krb.h> 741556Srgrimes 751556Srgrimeschar dst_realm_buf[REALM_SZ]; 761556Srgrimeschar *dest_realm = NULL; 771556Srgrimesint use_kerberos = 1; 781556SrgrimesCREDENTIALS cred; 791556SrgrimesKey_schedule schedule; 801556Srgrimesextern char *krb_realmofhost(); 811556Srgrimes#ifdef CRYPT 821556Srgrimesint doencrypt = 0; 831556Srgrimes#define OPTIONS "dfKk:prtx" 841556Srgrimes#else 851556Srgrimes#define OPTIONS "dfKk:prt" 861556Srgrimes#endif 871556Srgrimes#else 881556Srgrimes#define OPTIONS "dfprt" 891556Srgrimes#endif 901556Srgrimes 911556Srgrimesstruct passwd *pwd; 921556Srgrimesu_short port; 931556Srgrimesuid_t userid; 941556Srgrimesint errs, rem; 951556Srgrimesint pflag, iamremote, iamrecursive, targetshouldbedirectory; 961556Srgrimes 9734898Smarkmstatic int argc_copy; 9834898Smarkmstatic char **argv_copy; 9934898Smarkm 1001556Srgrimes#define CMDNEEDS 64 1011556Srgrimeschar cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */ 1021556Srgrimes 1031556Srgrimes#ifdef KERBEROS 1041556Srgrimesint kerberos __P((char **, char *, char *, char *)); 1051556Srgrimesvoid oldw __P((const char *, ...)); 1061556Srgrimes#endif 1071556Srgrimesint response __P((void)); 1081556Srgrimesvoid rsource __P((char *, struct stat *)); 1091556Srgrimesvoid sink __P((int, char *[])); 1101556Srgrimesvoid source __P((int, char *[])); 1111556Srgrimesvoid tolocal __P((int, char *[])); 1121556Srgrimesvoid toremote __P((char *, int, char *[])); 1131556Srgrimesvoid usage __P((void)); 1141556Srgrimes 1151556Srgrimesint 1161556Srgrimesmain(argc, argv) 1171556Srgrimes int argc; 1181556Srgrimes char *argv[]; 1191556Srgrimes{ 1201556Srgrimes struct servent *sp; 1211556Srgrimes int ch, fflag, tflag; 1221556Srgrimes char *targ, *shell; 12334898Smarkm int i; 1241556Srgrimes 12534898Smarkm /* 12634898Smarkm * Prepare for execing ourselves. 12734898Smarkm */ 12834898Smarkm 12934898Smarkm argc_copy = argc + 1; 13034898Smarkm argv_copy = malloc((argc_copy + 1) * sizeof(*argv_copy)); 13134898Smarkm if (argv_copy == NULL) 13234898Smarkm err(1, "malloc"); 13334898Smarkm argv_copy[0] = argv[0]; 13434898Smarkm argv_copy[1] = "-K"; 13534898Smarkm for(i = 1; i < argc; ++i) { 13634898Smarkm argv_copy[i + 1] = strdup(argv[i]); 13734898Smarkm if (argv_copy[i + 1] == NULL) 13834898Smarkm errx(1, "strdup: out of memory"); 13934898Smarkm } 14034898Smarkm argv_copy[argc + 1] = NULL; 14134898Smarkm 1421556Srgrimes fflag = tflag = 0; 14324348Simp while ((ch = getopt(argc, argv, OPTIONS)) != -1) 1441556Srgrimes switch(ch) { /* User-visible flags. */ 1451556Srgrimes case 'K': 1461556Srgrimes#ifdef KERBEROS 1471556Srgrimes use_kerberos = 0; 1481556Srgrimes#endif 1491556Srgrimes break; 1501556Srgrimes#ifdef KERBEROS 1511556Srgrimes case 'k': 1521556Srgrimes dest_realm = dst_realm_buf; 15325612Sjoerg (void)strncpy(dst_realm_buf, optarg, REALM_SZ - 1); 15425612Sjoerg dst_realm_buf[REALM_SZ - 1] = '\0'; 1551556Srgrimes break; 1561556Srgrimes#ifdef CRYPT 1571556Srgrimes case 'x': 1581556Srgrimes doencrypt = 1; 1591556Srgrimes /* des_set_key(cred.session, schedule); */ 1601556Srgrimes break; 1611556Srgrimes#endif 1621556Srgrimes#endif 1631556Srgrimes case 'p': 1641556Srgrimes pflag = 1; 1651556Srgrimes break; 1661556Srgrimes case 'r': 1671556Srgrimes iamrecursive = 1; 1681556Srgrimes break; 1691556Srgrimes /* Server options. */ 1701556Srgrimes case 'd': 1711556Srgrimes targetshouldbedirectory = 1; 1721556Srgrimes break; 1731556Srgrimes case 'f': /* "from" */ 1741556Srgrimes iamremote = 1; 1751556Srgrimes fflag = 1; 1761556Srgrimes break; 1771556Srgrimes case 't': /* "to" */ 1781556Srgrimes iamremote = 1; 1791556Srgrimes tflag = 1; 1801556Srgrimes break; 1811556Srgrimes case '?': 1821556Srgrimes default: 1831556Srgrimes usage(); 1841556Srgrimes } 1851556Srgrimes argc -= optind; 1861556Srgrimes argv += optind; 1871556Srgrimes 1881556Srgrimes#ifdef KERBEROS 1891556Srgrimes if (use_kerberos) { 1901556Srgrimes#ifdef CRYPT 1911556Srgrimes shell = doencrypt ? "ekshell" : "kshell"; 1921556Srgrimes#else 1931556Srgrimes shell = "kshell"; 1941556Srgrimes#endif 1951556Srgrimes if ((sp = getservbyname(shell, "tcp")) == NULL) { 1961556Srgrimes use_kerberos = 0; 1971556Srgrimes oldw("can't get entry for %s/tcp service", shell); 1981556Srgrimes sp = getservbyname(shell = "shell", "tcp"); 1991556Srgrimes } 2001556Srgrimes } else 2011556Srgrimes sp = getservbyname(shell = "shell", "tcp"); 2021556Srgrimes#else 2031556Srgrimes sp = getservbyname(shell = "shell", "tcp"); 2041556Srgrimes#endif 2051556Srgrimes if (sp == NULL) 2061556Srgrimes errx(1, "%s/tcp: unknown service", shell); 2071556Srgrimes port = sp->s_port; 2081556Srgrimes 2091556Srgrimes if ((pwd = getpwuid(userid = getuid())) == NULL) 2101556Srgrimes errx(1, "unknown user %d", (int)userid); 2111556Srgrimes 2121556Srgrimes rem = STDIN_FILENO; /* XXX */ 2131556Srgrimes 2141556Srgrimes if (fflag) { /* Follow "protocol", send data. */ 2151556Srgrimes (void)response(); 2161556Srgrimes (void)setuid(userid); 2171556Srgrimes source(argc, argv); 2181556Srgrimes exit(errs); 2191556Srgrimes } 2201556Srgrimes 2211556Srgrimes if (tflag) { /* Receive data. */ 2221556Srgrimes (void)setuid(userid); 2231556Srgrimes sink(argc, argv); 2241556Srgrimes exit(errs); 2251556Srgrimes } 2261556Srgrimes 2271556Srgrimes if (argc < 2) 2281556Srgrimes usage(); 2291556Srgrimes if (argc > 2) 2301556Srgrimes targetshouldbedirectory = 1; 2311556Srgrimes 2321556Srgrimes rem = -1; 2331556Srgrimes /* Command to be executed on remote system using "rsh". */ 2341556Srgrimes#ifdef KERBEROS 2351556Srgrimes (void)snprintf(cmd, sizeof(cmd), 2361556Srgrimes "rcp%s%s%s%s", iamrecursive ? " -r" : "", 2371556Srgrimes#ifdef CRYPT 2381556Srgrimes (doencrypt && use_kerberos ? " -x" : ""), 2391556Srgrimes#else 2401556Srgrimes "", 2411556Srgrimes#endif 2421556Srgrimes pflag ? " -p" : "", targetshouldbedirectory ? " -d" : ""); 2431556Srgrimes#else 2441556Srgrimes (void)snprintf(cmd, sizeof(cmd), "rcp%s%s%s", 2451556Srgrimes iamrecursive ? " -r" : "", pflag ? " -p" : "", 2461556Srgrimes targetshouldbedirectory ? " -d" : ""); 2471556Srgrimes#endif 2481556Srgrimes 2491556Srgrimes (void)signal(SIGPIPE, lostconn); 2501556Srgrimes 2517165Sjoerg if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */ 2521556Srgrimes toremote(targ, argc, argv); 2531556Srgrimes else { 2541556Srgrimes tolocal(argc, argv); /* Dest is local host. */ 2551556Srgrimes if (targetshouldbedirectory) 2561556Srgrimes verifydir(argv[argc - 1]); 2571556Srgrimes } 2581556Srgrimes exit(errs); 2591556Srgrimes} 2601556Srgrimes 2611556Srgrimesvoid 2621556Srgrimestoremote(targ, argc, argv) 2631556Srgrimes char *targ, *argv[]; 2641556Srgrimes int argc; 2651556Srgrimes{ 2661556Srgrimes int i, len, tos; 2671556Srgrimes char *bp, *host, *src, *suser, *thost, *tuser; 2681556Srgrimes 2691556Srgrimes *targ++ = 0; 2701556Srgrimes if (*targ == 0) 2711556Srgrimes targ = "."; 2721556Srgrimes 2737165Sjoerg if ((thost = strchr(argv[argc - 1], '@'))) { 2741556Srgrimes /* user@host */ 2751556Srgrimes *thost++ = 0; 2761556Srgrimes tuser = argv[argc - 1]; 2771556Srgrimes if (*tuser == '\0') 2781556Srgrimes tuser = NULL; 2791556Srgrimes else if (!okname(tuser)) 2801556Srgrimes exit(1); 2811556Srgrimes } else { 2821556Srgrimes thost = argv[argc - 1]; 2831556Srgrimes tuser = NULL; 2841556Srgrimes } 2851556Srgrimes 2861556Srgrimes for (i = 0; i < argc - 1; i++) { 2871556Srgrimes src = colon(argv[i]); 2881556Srgrimes if (src) { /* remote to remote */ 2891556Srgrimes *src++ = 0; 2901556Srgrimes if (*src == 0) 2911556Srgrimes src = "."; 2921556Srgrimes host = strchr(argv[i], '@'); 2931556Srgrimes len = strlen(_PATH_RSH) + strlen(argv[i]) + 2941556Srgrimes strlen(src) + (tuser ? strlen(tuser) : 0) + 2951556Srgrimes strlen(thost) + strlen(targ) + CMDNEEDS + 20; 2961556Srgrimes if (!(bp = malloc(len))) 2971556Srgrimes err(1, NULL); 2981556Srgrimes if (host) { 2991556Srgrimes *host++ = 0; 3001556Srgrimes suser = argv[i]; 3011556Srgrimes if (*suser == '\0') 3021556Srgrimes suser = pwd->pw_name; 3031556Srgrimes else if (!okname(suser)) 3041556Srgrimes continue; 3051556Srgrimes (void)snprintf(bp, len, 3061556Srgrimes "%s %s -l %s -n %s %s '%s%s%s:%s'", 3071556Srgrimes _PATH_RSH, host, suser, cmd, src, 3081556Srgrimes tuser ? tuser : "", tuser ? "@" : "", 3091556Srgrimes thost, targ); 3101556Srgrimes } else 3111556Srgrimes (void)snprintf(bp, len, 3121556Srgrimes "exec %s %s -n %s %s '%s%s%s:%s'", 3131556Srgrimes _PATH_RSH, argv[i], cmd, src, 3141556Srgrimes tuser ? tuser : "", tuser ? "@" : "", 3151556Srgrimes thost, targ); 3161556Srgrimes (void)susystem(bp, userid); 3171556Srgrimes (void)free(bp); 3181556Srgrimes } else { /* local to remote */ 3191556Srgrimes if (rem == -1) { 3201556Srgrimes len = strlen(targ) + CMDNEEDS + 20; 3211556Srgrimes if (!(bp = malloc(len))) 3221556Srgrimes err(1, NULL); 3231556Srgrimes (void)snprintf(bp, len, "%s -t %s", cmd, targ); 3241556Srgrimes host = thost; 3251556Srgrimes#ifdef KERBEROS 3261556Srgrimes if (use_kerberos) 3271556Srgrimes rem = kerberos(&host, bp, 3281556Srgrimes pwd->pw_name, 3291556Srgrimes tuser ? tuser : pwd->pw_name); 3301556Srgrimes else 3311556Srgrimes#endif 3321556Srgrimes rem = rcmd(&host, port, pwd->pw_name, 3331556Srgrimes tuser ? tuser : pwd->pw_name, 3341556Srgrimes bp, 0); 3351556Srgrimes if (rem < 0) 3361556Srgrimes exit(1); 3371556Srgrimes tos = IPTOS_THROUGHPUT; 3381556Srgrimes if (setsockopt(rem, IPPROTO_IP, IP_TOS, 3391556Srgrimes &tos, sizeof(int)) < 0) 3401556Srgrimes warn("TOS (ignored)"); 3411556Srgrimes if (response() < 0) 3421556Srgrimes exit(1); 3431556Srgrimes (void)free(bp); 3441556Srgrimes (void)setuid(userid); 3451556Srgrimes } 3461556Srgrimes source(1, argv+i); 3471556Srgrimes } 3481556Srgrimes } 3491556Srgrimes} 3501556Srgrimes 3511556Srgrimesvoid 3521556Srgrimestolocal(argc, argv) 3531556Srgrimes int argc; 3541556Srgrimes char *argv[]; 3551556Srgrimes{ 3561556Srgrimes int i, len, tos; 3571556Srgrimes char *bp, *host, *src, *suser; 3581556Srgrimes 3591556Srgrimes for (i = 0; i < argc - 1; i++) { 3601556Srgrimes if (!(src = colon(argv[i]))) { /* Local to local. */ 3611556Srgrimes len = strlen(_PATH_CP) + strlen(argv[i]) + 3621556Srgrimes strlen(argv[argc - 1]) + 20; 3631556Srgrimes if (!(bp = malloc(len))) 3641556Srgrimes err(1, NULL); 3651556Srgrimes (void)snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP, 36631633Swosch iamrecursive ? " -PR" : "", pflag ? " -p" : "", 3671556Srgrimes argv[i], argv[argc - 1]); 3681556Srgrimes if (susystem(bp, userid)) 3691556Srgrimes ++errs; 3701556Srgrimes (void)free(bp); 3711556Srgrimes continue; 3721556Srgrimes } 3731556Srgrimes *src++ = 0; 3741556Srgrimes if (*src == 0) 3751556Srgrimes src = "."; 3761556Srgrimes if ((host = strchr(argv[i], '@')) == NULL) { 3771556Srgrimes host = argv[i]; 3781556Srgrimes suser = pwd->pw_name; 3791556Srgrimes } else { 3801556Srgrimes *host++ = 0; 3811556Srgrimes suser = argv[i]; 3821556Srgrimes if (*suser == '\0') 3831556Srgrimes suser = pwd->pw_name; 3841556Srgrimes else if (!okname(suser)) 3851556Srgrimes continue; 3861556Srgrimes } 3871556Srgrimes len = strlen(src) + CMDNEEDS + 20; 3881556Srgrimes if ((bp = malloc(len)) == NULL) 3891556Srgrimes err(1, NULL); 3901556Srgrimes (void)snprintf(bp, len, "%s -f %s", cmd, src); 3918855Srgrimes rem = 3921556Srgrimes#ifdef KERBEROS 3938855Srgrimes use_kerberos ? 3948855Srgrimes kerberos(&host, bp, pwd->pw_name, suser) : 3951556Srgrimes#endif 3961556Srgrimes rcmd(&host, port, pwd->pw_name, suser, bp, 0); 3971556Srgrimes (void)free(bp); 3981556Srgrimes if (rem < 0) { 3991556Srgrimes ++errs; 4001556Srgrimes continue; 4011556Srgrimes } 4021556Srgrimes (void)seteuid(userid); 4031556Srgrimes tos = IPTOS_THROUGHPUT; 4041556Srgrimes if (setsockopt(rem, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0) 4051556Srgrimes warn("TOS (ignored)"); 4061556Srgrimes sink(1, argv + argc - 1); 4071556Srgrimes (void)seteuid(0); 4081556Srgrimes (void)close(rem); 4091556Srgrimes rem = -1; 4101556Srgrimes } 4111556Srgrimes} 4121556Srgrimes 4131556Srgrimesvoid 4141556Srgrimessource(argc, argv) 4151556Srgrimes int argc; 4161556Srgrimes char *argv[]; 4171556Srgrimes{ 4181556Srgrimes struct stat stb; 4191556Srgrimes static BUF buffer; 4201556Srgrimes BUF *bp; 4211556Srgrimes off_t i; 4221556Srgrimes int amt, fd, haderr, indx, result; 4231556Srgrimes char *last, *name, buf[BUFSIZ]; 4241556Srgrimes 4251556Srgrimes for (indx = 0; indx < argc; ++indx) { 4261556Srgrimes name = argv[indx]; 4271556Srgrimes if ((fd = open(name, O_RDONLY, 0)) < 0) 4281556Srgrimes goto syserr; 4291556Srgrimes if (fstat(fd, &stb)) { 4301556Srgrimessyserr: run_err("%s: %s", name, strerror(errno)); 4311556Srgrimes goto next; 4321556Srgrimes } 4331556Srgrimes switch (stb.st_mode & S_IFMT) { 4341556Srgrimes case S_IFREG: 4351556Srgrimes break; 4361556Srgrimes case S_IFDIR: 4371556Srgrimes if (iamrecursive) { 4381556Srgrimes rsource(name, &stb); 4391556Srgrimes goto next; 4401556Srgrimes } 4411556Srgrimes /* FALLTHROUGH */ 4421556Srgrimes default: 4431556Srgrimes run_err("%s: not a regular file", name); 4441556Srgrimes goto next; 4451556Srgrimes } 4461556Srgrimes if ((last = strrchr(name, '/')) == NULL) 4471556Srgrimes last = name; 4481556Srgrimes else 4491556Srgrimes ++last; 4501556Srgrimes if (pflag) { 4511556Srgrimes /* 4521556Srgrimes * Make it compatible with possible future 4531556Srgrimes * versions expecting microseconds. 4541556Srgrimes */ 4551556Srgrimes (void)snprintf(buf, sizeof(buf), "T%ld 0 %ld 0\n", 45618409Snate stb.st_mtimespec.tv_sec, stb.st_atimespec.tv_sec); 4571556Srgrimes (void)write(rem, buf, strlen(buf)); 4581556Srgrimes if (response() < 0) 4591556Srgrimes goto next; 4601556Srgrimes } 4611556Srgrimes#define MODEMASK (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO) 4621556Srgrimes (void)snprintf(buf, sizeof(buf), "C%04o %qd %s\n", 4631556Srgrimes stb.st_mode & MODEMASK, stb.st_size, last); 4641556Srgrimes (void)write(rem, buf, strlen(buf)); 4651556Srgrimes if (response() < 0) 4661556Srgrimes goto next; 4671556Srgrimes if ((bp = allocbuf(&buffer, fd, BUFSIZ)) == NULL) { 4681556Srgrimesnext: (void)close(fd); 4691556Srgrimes continue; 4701556Srgrimes } 4711556Srgrimes 4721556Srgrimes /* Keep writing after an error so that we stay sync'd up. */ 4731556Srgrimes for (haderr = i = 0; i < stb.st_size; i += bp->cnt) { 4741556Srgrimes amt = bp->cnt; 4751556Srgrimes if (i + amt > stb.st_size) 4761556Srgrimes amt = stb.st_size - i; 4771556Srgrimes if (!haderr) { 4781556Srgrimes result = read(fd, bp->buf, amt); 4791556Srgrimes if (result != amt) 4801556Srgrimes haderr = result >= 0 ? EIO : errno; 4811556Srgrimes } 4821556Srgrimes if (haderr) 4831556Srgrimes (void)write(rem, bp->buf, amt); 4841556Srgrimes else { 4851556Srgrimes result = write(rem, bp->buf, amt); 4861556Srgrimes if (result != amt) 4871556Srgrimes haderr = result >= 0 ? EIO : errno; 4881556Srgrimes } 4891556Srgrimes } 4901556Srgrimes if (close(fd) && !haderr) 4911556Srgrimes haderr = errno; 4921556Srgrimes if (!haderr) 4931556Srgrimes (void)write(rem, "", 1); 4941556Srgrimes else 4951556Srgrimes run_err("%s: %s", name, strerror(haderr)); 4961556Srgrimes (void)response(); 4971556Srgrimes } 4981556Srgrimes} 4991556Srgrimes 5001556Srgrimesvoid 5011556Srgrimesrsource(name, statp) 5021556Srgrimes char *name; 5031556Srgrimes struct stat *statp; 5041556Srgrimes{ 5051556Srgrimes DIR *dirp; 5061556Srgrimes struct dirent *dp; 5071556Srgrimes char *last, *vect[1], path[MAXPATHLEN]; 5081556Srgrimes 5091556Srgrimes if (!(dirp = opendir(name))) { 5101556Srgrimes run_err("%s: %s", name, strerror(errno)); 5111556Srgrimes return; 5121556Srgrimes } 5131556Srgrimes last = strrchr(name, '/'); 5141556Srgrimes if (last == 0) 5151556Srgrimes last = name; 5161556Srgrimes else 5171556Srgrimes last++; 5181556Srgrimes if (pflag) { 5191556Srgrimes (void)snprintf(path, sizeof(path), "T%ld 0 %ld 0\n", 52018409Snate statp->st_mtimespec.tv_sec, statp->st_atimespec.tv_sec); 5211556Srgrimes (void)write(rem, path, strlen(path)); 5221556Srgrimes if (response() < 0) { 5231556Srgrimes closedir(dirp); 5241556Srgrimes return; 5251556Srgrimes } 5261556Srgrimes } 5271556Srgrimes (void)snprintf(path, sizeof(path), 5281556Srgrimes "D%04o %d %s\n", statp->st_mode & MODEMASK, 0, last); 5291556Srgrimes (void)write(rem, path, strlen(path)); 5301556Srgrimes if (response() < 0) { 5311556Srgrimes closedir(dirp); 5321556Srgrimes return; 5331556Srgrimes } 5347165Sjoerg while ((dp = readdir(dirp))) { 5351556Srgrimes if (dp->d_ino == 0) 5361556Srgrimes continue; 5371556Srgrimes if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) 5381556Srgrimes continue; 5391556Srgrimes if (strlen(name) + 1 + strlen(dp->d_name) >= MAXPATHLEN - 1) { 5401556Srgrimes run_err("%s/%s: name too long", name, dp->d_name); 5411556Srgrimes continue; 5421556Srgrimes } 5431556Srgrimes (void)snprintf(path, sizeof(path), "%s/%s", name, dp->d_name); 5441556Srgrimes vect[0] = path; 5451556Srgrimes source(1, vect); 5461556Srgrimes } 5471556Srgrimes (void)closedir(dirp); 5481556Srgrimes (void)write(rem, "E\n", 2); 5491556Srgrimes (void)response(); 5501556Srgrimes} 5511556Srgrimes 5521556Srgrimesvoid 5531556Srgrimessink(argc, argv) 5541556Srgrimes int argc; 5551556Srgrimes char *argv[]; 5561556Srgrimes{ 5571556Srgrimes static BUF buffer; 5581556Srgrimes struct stat stb; 5591556Srgrimes struct timeval tv[2]; 5601556Srgrimes enum { YES, NO, DISPLAYED } wrerr; 5611556Srgrimes BUF *bp; 5621556Srgrimes off_t i, j; 5631556Srgrimes int amt, count, exists, first, mask, mode, ofd, omode; 5647165Sjoerg int setimes, size, targisdir, wrerrno = 0; 5651556Srgrimes char ch, *cp, *np, *targ, *why, *vect[1], buf[BUFSIZ]; 5661556Srgrimes 5671556Srgrimes#define atime tv[0] 5681556Srgrimes#define mtime tv[1] 5691556Srgrimes#define SCREWUP(str) { why = str; goto screwup; } 5701556Srgrimes 5711556Srgrimes setimes = targisdir = 0; 5721556Srgrimes mask = umask(0); 5731556Srgrimes if (!pflag) 5741556Srgrimes (void)umask(mask); 5751556Srgrimes if (argc != 1) { 5761556Srgrimes run_err("ambiguous target"); 5771556Srgrimes exit(1); 5781556Srgrimes } 5791556Srgrimes targ = *argv; 5801556Srgrimes if (targetshouldbedirectory) 5811556Srgrimes verifydir(targ); 5821556Srgrimes (void)write(rem, "", 1); 5831556Srgrimes if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode)) 5841556Srgrimes targisdir = 1; 5851556Srgrimes for (first = 1;; first = 0) { 5861556Srgrimes cp = buf; 5871556Srgrimes if (read(rem, cp, 1) <= 0) 5881556Srgrimes return; 5891556Srgrimes if (*cp++ == '\n') 5901556Srgrimes SCREWUP("unexpected <newline>"); 5911556Srgrimes do { 5921556Srgrimes if (read(rem, &ch, sizeof(ch)) != sizeof(ch)) 5931556Srgrimes SCREWUP("lost connection"); 5941556Srgrimes *cp++ = ch; 5951556Srgrimes } while (cp < &buf[BUFSIZ - 1] && ch != '\n'); 5961556Srgrimes *cp = 0; 5971556Srgrimes 5981556Srgrimes if (buf[0] == '\01' || buf[0] == '\02') { 5991556Srgrimes if (iamremote == 0) 6001556Srgrimes (void)write(STDERR_FILENO, 6011556Srgrimes buf + 1, strlen(buf + 1)); 6021556Srgrimes if (buf[0] == '\02') 6031556Srgrimes exit(1); 6041556Srgrimes ++errs; 6051556Srgrimes continue; 6061556Srgrimes } 6071556Srgrimes if (buf[0] == 'E') { 6081556Srgrimes (void)write(rem, "", 1); 6091556Srgrimes return; 6101556Srgrimes } 6111556Srgrimes 6121556Srgrimes if (ch == '\n') 6131556Srgrimes *--cp = 0; 6141556Srgrimes 6151556Srgrimes cp = buf; 6161556Srgrimes if (*cp == 'T') { 6171556Srgrimes setimes++; 6181556Srgrimes cp++; 61913978Spst mtime.tv_sec = strtol(cp, &cp, 10); 62013978Spst if (!cp || *cp++ != ' ') 6211556Srgrimes SCREWUP("mtime.sec not delimited"); 62213978Spst mtime.tv_usec = strtol(cp, &cp, 10); 62313978Spst if (!cp || *cp++ != ' ') 6241556Srgrimes SCREWUP("mtime.usec not delimited"); 62513978Spst atime.tv_sec = strtol(cp, &cp, 10); 62613978Spst if (!cp || *cp++ != ' ') 6271556Srgrimes SCREWUP("atime.sec not delimited"); 62813978Spst atime.tv_usec = strtol(cp, &cp, 10); 62913978Spst if (!cp || *cp++ != '\0') 6301556Srgrimes SCREWUP("atime.usec not delimited"); 6311556Srgrimes (void)write(rem, "", 1); 6321556Srgrimes continue; 6331556Srgrimes } 6341556Srgrimes if (*cp != 'C' && *cp != 'D') { 6351556Srgrimes /* 6361556Srgrimes * Check for the case "rcp remote:foo\* local:bar". 6371556Srgrimes * In this case, the line "No match." can be returned 6381556Srgrimes * by the shell before the rcp command on the remote is 6391556Srgrimes * executed so the ^Aerror_message convention isn't 6401556Srgrimes * followed. 6411556Srgrimes */ 6421556Srgrimes if (first) { 6431556Srgrimes run_err("%s", cp); 6441556Srgrimes exit(1); 6451556Srgrimes } 6461556Srgrimes SCREWUP("expected control record"); 6471556Srgrimes } 6481556Srgrimes mode = 0; 6491556Srgrimes for (++cp; cp < buf + 5; cp++) { 6501556Srgrimes if (*cp < '0' || *cp > '7') 6511556Srgrimes SCREWUP("bad mode"); 6521556Srgrimes mode = (mode << 3) | (*cp - '0'); 6531556Srgrimes } 6541556Srgrimes if (*cp++ != ' ') 6551556Srgrimes SCREWUP("mode not delimited"); 6561556Srgrimes 6571556Srgrimes for (size = 0; isdigit(*cp);) 6581556Srgrimes size = size * 10 + (*cp++ - '0'); 6591556Srgrimes if (*cp++ != ' ') 6601556Srgrimes SCREWUP("size not delimited"); 6611556Srgrimes if (targisdir) { 6621556Srgrimes static char *namebuf; 6631556Srgrimes static int cursize; 6641556Srgrimes size_t need; 6651556Srgrimes 6661556Srgrimes need = strlen(targ) + strlen(cp) + 250; 6671556Srgrimes if (need > cursize) { 6681556Srgrimes if (!(namebuf = malloc(need))) 6691556Srgrimes run_err("%s", strerror(errno)); 6701556Srgrimes } 6711556Srgrimes (void)snprintf(namebuf, need, "%s%s%s", targ, 6721556Srgrimes *targ ? "/" : "", cp); 6731556Srgrimes np = namebuf; 6741556Srgrimes } else 6751556Srgrimes np = targ; 6761556Srgrimes exists = stat(np, &stb) == 0; 6771556Srgrimes if (buf[0] == 'D') { 6781556Srgrimes int mod_flag = pflag; 6791556Srgrimes if (exists) { 6801556Srgrimes if (!S_ISDIR(stb.st_mode)) { 6811556Srgrimes errno = ENOTDIR; 6821556Srgrimes goto bad; 6831556Srgrimes } 6841556Srgrimes if (pflag) 6851556Srgrimes (void)chmod(np, mode); 6861556Srgrimes } else { 6871556Srgrimes /* Handle copying from a read-only directory */ 6881556Srgrimes mod_flag = 1; 6891556Srgrimes if (mkdir(np, mode | S_IRWXU) < 0) 6901556Srgrimes goto bad; 6911556Srgrimes } 6921556Srgrimes vect[0] = np; 6931556Srgrimes sink(1, vect); 6941556Srgrimes if (setimes) { 6951556Srgrimes setimes = 0; 6961556Srgrimes if (utimes(np, tv) < 0) 6971556Srgrimes run_err("%s: set times: %s", 6981556Srgrimes np, strerror(errno)); 6991556Srgrimes } 7001556Srgrimes if (mod_flag) 7011556Srgrimes (void)chmod(np, mode); 7021556Srgrimes continue; 7031556Srgrimes } 7041556Srgrimes omode = mode; 7051556Srgrimes mode |= S_IWRITE; 7061556Srgrimes if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) { 7071556Srgrimesbad: run_err("%s: %s", np, strerror(errno)); 7081556Srgrimes continue; 7091556Srgrimes } 7101556Srgrimes (void)write(rem, "", 1); 7111556Srgrimes if ((bp = allocbuf(&buffer, ofd, BUFSIZ)) == NULL) { 7121556Srgrimes (void)close(ofd); 7131556Srgrimes continue; 7141556Srgrimes } 7151556Srgrimes cp = bp->buf; 7161556Srgrimes wrerr = NO; 7171556Srgrimes for (count = i = 0; i < size; i += BUFSIZ) { 7181556Srgrimes amt = BUFSIZ; 7191556Srgrimes if (i + amt > size) 7201556Srgrimes amt = size - i; 7211556Srgrimes count += amt; 7221556Srgrimes do { 7231556Srgrimes j = read(rem, cp, amt); 7241556Srgrimes if (j <= 0) { 7251556Srgrimes run_err("%s", j ? strerror(errno) : 7261556Srgrimes "dropped connection"); 7271556Srgrimes exit(1); 7281556Srgrimes } 7291556Srgrimes amt -= j; 7301556Srgrimes cp += j; 7311556Srgrimes } while (amt > 0); 7321556Srgrimes if (count == bp->cnt) { 7331556Srgrimes /* Keep reading so we stay sync'd up. */ 7341556Srgrimes if (wrerr == NO) { 7351556Srgrimes j = write(ofd, bp->buf, count); 7361556Srgrimes if (j != count) { 7371556Srgrimes wrerr = YES; 7388855Srgrimes wrerrno = j >= 0 ? EIO : errno; 7391556Srgrimes } 7401556Srgrimes } 7411556Srgrimes count = 0; 7421556Srgrimes cp = bp->buf; 7431556Srgrimes } 7441556Srgrimes } 7451556Srgrimes if (count != 0 && wrerr == NO && 7461556Srgrimes (j = write(ofd, bp->buf, count)) != count) { 7471556Srgrimes wrerr = YES; 7488855Srgrimes wrerrno = j >= 0 ? EIO : errno; 7491556Srgrimes } 7501556Srgrimes if (ftruncate(ofd, size)) { 7511556Srgrimes run_err("%s: truncate: %s", np, strerror(errno)); 7521556Srgrimes wrerr = DISPLAYED; 7531556Srgrimes } 7541556Srgrimes if (pflag) { 7551556Srgrimes if (exists || omode != mode) 7561556Srgrimes if (fchmod(ofd, omode)) 7571556Srgrimes run_err("%s: set mode: %s", 7581556Srgrimes np, strerror(errno)); 7591556Srgrimes } else { 7601556Srgrimes if (!exists && omode != mode) 7611556Srgrimes if (fchmod(ofd, omode & ~mask)) 7621556Srgrimes run_err("%s: set mode: %s", 7631556Srgrimes np, strerror(errno)); 7641556Srgrimes } 7651556Srgrimes (void)close(ofd); 7661556Srgrimes (void)response(); 7671556Srgrimes if (setimes && wrerr == NO) { 7681556Srgrimes setimes = 0; 7691556Srgrimes if (utimes(np, tv) < 0) { 7701556Srgrimes run_err("%s: set times: %s", 7711556Srgrimes np, strerror(errno)); 7721556Srgrimes wrerr = DISPLAYED; 7731556Srgrimes } 7741556Srgrimes } 7751556Srgrimes switch(wrerr) { 7761556Srgrimes case YES: 7771556Srgrimes run_err("%s: %s", np, strerror(wrerrno)); 7781556Srgrimes break; 7791556Srgrimes case NO: 7801556Srgrimes (void)write(rem, "", 1); 7811556Srgrimes break; 7821556Srgrimes case DISPLAYED: 7831556Srgrimes break; 7841556Srgrimes } 7851556Srgrimes } 7861556Srgrimesscrewup: 7871556Srgrimes run_err("protocol error: %s", why); 7881556Srgrimes exit(1); 7891556Srgrimes} 7901556Srgrimes 7911556Srgrimes#ifdef KERBEROS 7921556Srgrimesint 7931556Srgrimeskerberos(host, bp, locuser, user) 7941556Srgrimes char **host, *bp, *locuser, *user; 7951556Srgrimes{ 7961556Srgrimes if (use_kerberos) { 79734898Smarkm setuid(getuid()); 7981556Srgrimes rem = KSUCCESS; 7991556Srgrimes errno = 0; 8001556Srgrimes if (dest_realm == NULL) 8011556Srgrimes dest_realm = krb_realmofhost(*host); 8028855Srgrimes rem = 8031556Srgrimes#ifdef CRYPT 8048855Srgrimes doencrypt ? 8051556Srgrimes krcmd_mutual(host, 8061556Srgrimes port, user, bp, 0, dest_realm, &cred, schedule) : 8071556Srgrimes#endif 8081556Srgrimes krcmd(host, port, user, bp, 0, dest_realm); 8091556Srgrimes 8101556Srgrimes if (rem < 0) { 8111556Srgrimes if (errno == ECONNREFUSED) 8121556Srgrimes oldw("remote host doesn't support Kerberos"); 8131556Srgrimes else if (errno == ENOENT) 8141556Srgrimes oldw("can't provide Kerberos authentication data"); 81534898Smarkm execv(_PATH_RCP, argv_copy); 8161556Srgrimes } 8171556Srgrimes } else { 8181556Srgrimes#ifdef CRYPT 8191556Srgrimes if (doencrypt) 8201556Srgrimes errx(1, 8211556Srgrimes "the -x option requires Kerberos authentication"); 8221556Srgrimes#endif 8231556Srgrimes rem = rcmd(host, port, locuser, user, bp, 0); 8241556Srgrimes } 8251556Srgrimes return (rem); 8261556Srgrimes} 8271556Srgrimes#endif /* KERBEROS */ 8281556Srgrimes 8291556Srgrimesint 8301556Srgrimesresponse() 8311556Srgrimes{ 8321556Srgrimes char ch, *cp, resp, rbuf[BUFSIZ]; 8331556Srgrimes 8341556Srgrimes if (read(rem, &resp, sizeof(resp)) != sizeof(resp)) 8351556Srgrimes lostconn(0); 8361556Srgrimes 8371556Srgrimes cp = rbuf; 8381556Srgrimes switch(resp) { 8391556Srgrimes case 0: /* ok */ 8401556Srgrimes return (0); 8411556Srgrimes default: 8421556Srgrimes *cp++ = resp; 8431556Srgrimes /* FALLTHROUGH */ 8441556Srgrimes case 1: /* error, followed by error msg */ 8451556Srgrimes case 2: /* fatal error, "" */ 8461556Srgrimes do { 8471556Srgrimes if (read(rem, &ch, sizeof(ch)) != sizeof(ch)) 8481556Srgrimes lostconn(0); 8491556Srgrimes *cp++ = ch; 8501556Srgrimes } while (cp < &rbuf[BUFSIZ] && ch != '\n'); 8511556Srgrimes 8521556Srgrimes if (!iamremote) 8531556Srgrimes (void)write(STDERR_FILENO, rbuf, cp - rbuf); 8541556Srgrimes ++errs; 8551556Srgrimes if (resp == 1) 8561556Srgrimes return (-1); 8571556Srgrimes exit(1); 8581556Srgrimes } 8591556Srgrimes /* NOTREACHED */ 8601556Srgrimes} 8611556Srgrimes 8621556Srgrimesvoid 8631556Srgrimesusage() 8641556Srgrimes{ 8651556Srgrimes#ifdef KERBEROS 8661556Srgrimes#ifdef CRYPT 86726466Scharnier (void)fprintf(stderr, "%s\n%s\n", 8681556Srgrimes "usage: rcp [-Kpx] [-k realm] f1 f2", 86926466Scharnier " rcp [-Kprx] [-k realm] f1 ... fn directory"); 8701556Srgrimes#else 87126466Scharnier (void)fprintf(stderr, "%s\n%s\n", 8721556Srgrimes "usage: rcp [-Kp] [-k realm] f1 f2", 87326466Scharnier " rcp [-Kpr] [-k realm] f1 ... fn directory"); 8741556Srgrimes#endif 8751556Srgrimes#else 87626466Scharnier (void)fprintf(stderr, "%s\n%s\n", 87726466Scharnier "usage: rcp [-p] f1 f2", 87826466Scharnier " rcp [-pr] f1 ... fn directory"); 8791556Srgrimes#endif 8801556Srgrimes exit(1); 8811556Srgrimes} 8821556Srgrimes 8831556Srgrimes#if __STDC__ 8841556Srgrimes#include <stdarg.h> 8851556Srgrimes#else 8861556Srgrimes#include <varargs.h> 8871556Srgrimes#endif 8881556Srgrimes 8891556Srgrimes#ifdef KERBEROS 8901556Srgrimesvoid 8911556Srgrimes#if __STDC__ 8921556Srgrimesoldw(const char *fmt, ...) 8931556Srgrimes#else 8941556Srgrimesoldw(fmt, va_alist) 8951556Srgrimes char *fmt; 8961556Srgrimes va_dcl 8971556Srgrimes#endif 8981556Srgrimes{ 8991556Srgrimes va_list ap; 9001556Srgrimes#if __STDC__ 9011556Srgrimes va_start(ap, fmt); 9021556Srgrimes#else 9031556Srgrimes va_start(ap); 9041556Srgrimes#endif 9051556Srgrimes (void)fprintf(stderr, "rcp: "); 9061556Srgrimes (void)vfprintf(stderr, fmt, ap); 9071556Srgrimes (void)fprintf(stderr, ", using standard rcp\n"); 9081556Srgrimes va_end(ap); 9091556Srgrimes} 9101556Srgrimes#endif 9111556Srgrimes 9121556Srgrimesvoid 9131556Srgrimes#if __STDC__ 9141556Srgrimesrun_err(const char *fmt, ...) 9151556Srgrimes#else 9161556Srgrimesrun_err(fmt, va_alist) 9171556Srgrimes char *fmt; 9181556Srgrimes va_dcl 9191556Srgrimes#endif 9201556Srgrimes{ 9211556Srgrimes static FILE *fp; 9221556Srgrimes va_list ap; 9231556Srgrimes#if __STDC__ 9241556Srgrimes va_start(ap, fmt); 9251556Srgrimes#else 9261556Srgrimes va_start(ap); 9271556Srgrimes#endif 9281556Srgrimes 9291556Srgrimes ++errs; 9301556Srgrimes if (fp == NULL && !(fp = fdopen(rem, "w"))) 9311556Srgrimes return; 9321556Srgrimes (void)fprintf(fp, "%c", 0x01); 9331556Srgrimes (void)fprintf(fp, "rcp: "); 9341556Srgrimes (void)vfprintf(fp, fmt, ap); 9351556Srgrimes (void)fprintf(fp, "\n"); 9361556Srgrimes (void)fflush(fp); 9371556Srgrimes 9381556Srgrimes if (!iamremote) 9391556Srgrimes vwarnx(fmt, ap); 9401556Srgrimes 9411556Srgrimes va_end(ap); 9421556Srgrimes} 943