| Commit | Line | Data |
|---|---|---|
| 984263bc MD |
1 | /* |
| 2 | * Copyright (c) 1983, 1990, 1992, 1993 | |
| 3 | * The Regents of the University of California. All rights reserved. | |
| 4 | * Copyright (c) 2002 Networks Associates Technology, Inc. | |
| 5 | * All rights reserved. | |
| 6 | * | |
| 7 | * Portions of this software were developed for the FreeBSD Project by | |
| 8 | * ThinkSec AS and NAI Labs, the Security Research Division of Network | |
| 9 | * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 | |
| 10 | * ("CBOSS"), as part of the DARPA CHATS research program. | |
| 11 | * | |
| 12 | * Redistribution and use in source and binary forms, with or without | |
| 13 | * modification, are permitted provided that the following conditions | |
| 14 | * are met: | |
| 15 | * 1. Redistributions of source code must retain the above copyright | |
| 16 | * notice, this list of conditions and the following disclaimer. | |
| 17 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 18 | * notice, this list of conditions and the following disclaimer in the | |
| 19 | * documentation and/or other materials provided with the distribution. | |
| 20 | * 3. All advertising materials mentioning features or use of this software | |
| 21 | * must display the following acknowledgement: | |
| 22 | * This product includes software developed by the University of | |
| 23 | * California, Berkeley and its contributors. | |
| 24 | * 4. Neither the name of the University nor the names of its contributors | |
| 25 | * may be used to endorse or promote products derived from this software | |
| 26 | * without specific prior written permission. | |
| 27 | * | |
| 28 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
| 29 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 31 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
| 32 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 34 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 37 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 38 | * SUCH DAMAGE. | |
| 1de703da MD |
39 | * |
| 40 | * @(#) Copyright (c) 1983, 1990, 1992, 1993 The Regents of the University of California. All rights reserved. | |
| 41 | * @(#)rcp.c 8.2 (Berkeley) 4/2/94 | |
| a17407d2 | 42 | * $FreeBSD: src/bin/rcp/rcp.c,v 1.26.2.6 2004/09/16 12:16:10 delphij Exp $ |
| 984263bc MD |
43 | */ |
| 44 | ||
| 984263bc MD |
45 | #include <sys/param.h> |
| 46 | #include <sys/stat.h> | |
| 47 | #include <sys/time.h> | |
| 48 | #include <sys/socket.h> | |
| 49 | #include <netinet/in.h> | |
| 50 | #include <netinet/in_systm.h> | |
| 51 | #include <netinet/ip.h> | |
| 52 | ||
| 53 | #include <ctype.h> | |
| 54 | #include <dirent.h> | |
| 55 | #include <err.h> | |
| 56 | #include <errno.h> | |
| 57 | #include <fcntl.h> | |
| 58 | #include <libutil.h> | |
| 59 | #include <limits.h> | |
| 60 | #include <netdb.h> | |
| 61 | #include <paths.h> | |
| 62 | #include <pwd.h> | |
| 63 | #include <signal.h> | |
| 64 | #include <stdio.h> | |
| 65 | #include <stdlib.h> | |
| 66 | #include <string.h> | |
| 984263bc MD |
67 | #include <unistd.h> |
| 68 | ||
| 69 | #include "extern.h" | |
| 70 | ||
| 71 | #ifdef KERBEROS | |
| 72 | #include <openssl/des.h> | |
| 73 | #include <krb.h> | |
| 74 | #include "bsd_locl.h" | |
| 75 | ||
| 76 | char dst_realm_buf[REALM_SZ]; | |
| 77 | char *dest_realm = NULL; | |
| 78 | int use_kerberos = 1; | |
| 79 | CREDENTIALS cred; | |
| 80 | Key_schedule schedule; | |
| 81 | extern char *krb_realmofhost(); | |
| 82 | #ifdef CRYPT | |
| 83 | int doencrypt = 0; | |
| 84 | #define OPTIONS "46dfKk:prtx" | |
| 85 | #else | |
| 86 | #define OPTIONS "46dfKk:prt" | |
| 87 | #endif | |
| 88 | #else | |
| 89 | #define OPTIONS "46dfprt" | |
| 90 | #endif | |
| 91 | ||
| 92 | struct passwd *pwd; | |
| 93 | u_short port; | |
| 94 | uid_t userid; | |
| 95 | int errs, rem; | |
| 96 | int pflag, iamremote, iamrecursive, targetshouldbedirectory; | |
| 97 | int family = PF_UNSPEC; | |
| 98 | ||
| 99 | static int argc_copy; | |
| 100 | static char **argv_copy; | |
| 101 | ||
| 102 | #define CMDNEEDS 64 | |
| 103 | char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */ | |
| 104 | ||
| 105 | #ifdef KERBEROS | |
| 106 | int kerberos(char **, char *, char *, char *); | |
| 107 | void oldw(const char *, ...) __printflike(1, 2); | |
| 108 | #endif | |
| 109 | int response(void); | |
| 110 | void rsource(char *, struct stat *); | |
| 984263bc MD |
111 | void sink(int, char *[]); |
| 112 | void source(int, char *[]); | |
| 113 | void tolocal(int, char *[]); | |
| 114 | void toremote(char *, int, char *[]); | |
| 115 | void usage(void); | |
| 116 | ||
| 117 | int | |
| 118 | main(int argc, char *argv[]) | |
| 119 | { | |
| 120 | struct servent *sp; | |
| 121 | int ch, fflag, i, tflag; | |
| 122 | char *targ, *shell; | |
| 123 | #ifdef KERBEROS | |
| 124 | char *k; | |
| 125 | #endif | |
| 126 | ||
| 127 | /* | |
| 128 | * Prepare for execing ourselves. | |
| 129 | */ | |
| 130 | argc_copy = argc + 1; | |
| 131 | argv_copy = malloc((argc_copy + 1) * sizeof(*argv_copy)); | |
| 132 | if (argv_copy == NULL) | |
| 133 | err(1, "malloc"); | |
| 134 | argv_copy[0] = argv[0]; | |
| c5fe5fd1 | 135 | argv_copy[1] = __DECONST(char *, "-K"); |
| 984263bc MD |
136 | for (i = 1; i < argc; ++i) { |
| 137 | argv_copy[i + 1] = strdup(argv[i]); | |
| 138 | if (argv_copy[i + 1] == NULL) | |
| 139 | errx(1, "strdup: out of memory"); | |
| 140 | } | |
| 141 | argv_copy[argc + 1] = NULL; | |
| 142 | ||
| 143 | fflag = tflag = 0; | |
| 144 | while ((ch = getopt(argc, argv, OPTIONS)) != -1) | |
| 145 | switch(ch) { /* User-visible flags. */ | |
| 146 | case '4': | |
| 147 | family = PF_INET; | |
| 148 | break; | |
| 149 | ||
| 150 | case '6': | |
| 151 | family = PF_INET6; | |
| 152 | break; | |
| 153 | ||
| 154 | case 'K': | |
| 155 | #ifdef KERBEROS | |
| 156 | use_kerberos = 0; | |
| 157 | #endif | |
| 158 | break; | |
| 159 | #ifdef KERBEROS | |
| 160 | case 'k': | |
| 161 | dest_realm = dst_realm_buf; | |
| 57fed2af | 162 | strncpy(dst_realm_buf, optarg, REALM_SZ - 1); |
| 984263bc MD |
163 | dst_realm_buf[REALM_SZ - 1] = '\0'; |
| 164 | break; | |
| 165 | #ifdef CRYPT | |
| 166 | case 'x': | |
| 167 | doencrypt = 1; | |
| 168 | /* des_set_key(cred.session, schedule); */ | |
| 169 | break; | |
| 170 | #endif | |
| 171 | #endif | |
| 172 | case 'p': | |
| 173 | pflag = 1; | |
| 174 | break; | |
| 175 | case 'r': | |
| 176 | iamrecursive = 1; | |
| 177 | break; | |
| 178 | /* Server options. */ | |
| 179 | case 'd': | |
| 180 | targetshouldbedirectory = 1; | |
| 181 | break; | |
| 182 | case 'f': /* "from" */ | |
| 183 | iamremote = 1; | |
| 184 | fflag = 1; | |
| 185 | break; | |
| 186 | case 't': /* "to" */ | |
| 187 | iamremote = 1; | |
| 188 | tflag = 1; | |
| 189 | break; | |
| 190 | case '?': | |
| 191 | default: | |
| 192 | usage(); | |
| 193 | } | |
| 194 | argc -= optind; | |
| 195 | argv += optind; | |
| 196 | ||
| 197 | #ifdef KERBEROS | |
| 198 | k = auth_getval("auth_list"); | |
| 199 | if (k && !strstr(k, "kerberos")) | |
| 200 | use_kerberos = 0; | |
| 201 | if (use_kerberos) { | |
| 202 | #ifdef CRYPT | |
| c5fe5fd1 EN |
203 | shell = doencrypt ? __DECONST(char *, "ekshell") : |
| 204 | __DECONST(char *, "kshell"); | |
| 984263bc | 205 | #else |
| c5fe5fd1 | 206 | shell = __DECONST(char *, "kshell"); |
| 984263bc MD |
207 | #endif |
| 208 | if ((sp = getservbyname(shell, "tcp")) == NULL) { | |
| 209 | use_kerberos = 0; | |
| 210 | oldw("can't get entry for %s/tcp service", shell); | |
| 211 | sp = getservbyname(shell = "shell", "tcp"); | |
| 212 | } | |
| 213 | } else | |
| c5fe5fd1 EN |
214 | shell = __DECONST(char *, "shell"); |
| 215 | sp = getservbyname(shell, "tcp"); | |
| 984263bc | 216 | #else |
| c5fe5fd1 EN |
217 | shell = __DECONST(char *, "shell"); |
| 218 | sp = getservbyname(shell, "tcp"); | |
| 984263bc MD |
219 | #endif |
| 220 | if (sp == NULL) | |
| 221 | errx(1, "%s/tcp: unknown service", shell); | |
| 222 | port = sp->s_port; | |
| 223 | ||
| 224 | if ((pwd = getpwuid(userid = getuid())) == NULL) | |
| 225 | errx(1, "unknown user %d", (int)userid); | |
| 226 | ||
| 227 | rem = STDIN_FILENO; /* XXX */ | |
| 228 | ||
| 229 | if (fflag) { /* Follow "protocol", send data. */ | |
| 57fed2af EN |
230 | response(); |
| 231 | setuid(userid); | |
| 984263bc MD |
232 | source(argc, argv); |
| 233 | exit(errs); | |
| 234 | } | |
| 235 | ||
| 236 | if (tflag) { /* Receive data. */ | |
| 57fed2af | 237 | setuid(userid); |
| 984263bc MD |
238 | sink(argc, argv); |
| 239 | exit(errs); | |
| 240 | } | |
| 241 | ||
| 242 | if (argc < 2) | |
| 243 | usage(); | |
| 244 | if (argc > 2) | |
| 245 | targetshouldbedirectory = 1; | |
| 246 | ||
| 247 | rem = -1; | |
| 248 | /* Command to be executed on remote system using "rsh". */ | |
| 249 | #ifdef KERBEROS | |
| 57fed2af | 250 | snprintf(cmd, sizeof(cmd), |
| 984263bc MD |
251 | "rcp%s%s%s%s", iamrecursive ? " -r" : "", |
| 252 | #ifdef CRYPT | |
| 253 | (doencrypt && use_kerberos ? " -x" : ""), | |
| 254 | #else | |
| 255 | "", | |
| 256 | #endif | |
| 257 | pflag ? " -p" : "", targetshouldbedirectory ? " -d" : ""); | |
| 258 | #else | |
| 57fed2af | 259 | snprintf(cmd, sizeof(cmd), "rcp%s%s%s", |
| 984263bc MD |
260 | iamrecursive ? " -r" : "", pflag ? " -p" : "", |
| 261 | targetshouldbedirectory ? " -d" : ""); | |
| 262 | #endif | |
| 263 | ||
| 57fed2af | 264 | signal(SIGPIPE, lostconn); |
| 984263bc MD |
265 | |
| 266 | if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */ | |
| 267 | toremote(targ, argc, argv); | |
| 268 | else { | |
| 269 | tolocal(argc, argv); /* Dest is local host. */ | |
| 270 | if (targetshouldbedirectory) | |
| 271 | verifydir(argv[argc - 1]); | |
| 272 | } | |
| 273 | exit(errs); | |
| 274 | } | |
| 275 | ||
| 276 | void | |
| 277 | toremote(char *targ, int argc, char *argv[]) | |
| 278 | { | |
| 279 | int i, len, tos; | |
| 280 | char *bp, *host, *src, *suser, *thost, *tuser; | |
| 281 | ||
| 282 | *targ++ = 0; | |
| 283 | if (*targ == 0) | |
| c5fe5fd1 | 284 | targ = __DECONST(char *, "."); |
| 984263bc MD |
285 | |
| 286 | if ((thost = strchr(argv[argc - 1], '@'))) { | |
| 287 | /* user@host */ | |
| 288 | *thost++ = 0; | |
| 289 | tuser = argv[argc - 1]; | |
| 290 | if (*tuser == '\0') | |
| 291 | tuser = NULL; | |
| 292 | else if (!okname(tuser)) | |
| 293 | exit(1); | |
| 294 | } else { | |
| 295 | thost = argv[argc - 1]; | |
| 296 | tuser = NULL; | |
| 297 | } | |
| 298 | ||
| 299 | for (i = 0; i < argc - 1; i++) { | |
| 300 | src = colon(argv[i]); | |
| 301 | if (src) { /* remote to remote */ | |
| 302 | *src++ = 0; | |
| 303 | if (*src == 0) | |
| c5fe5fd1 | 304 | src = __DECONST(char *, "."); |
| 984263bc MD |
305 | host = strchr(argv[i], '@'); |
| 306 | len = strlen(_PATH_RSH) + strlen(argv[i]) + | |
| 307 | strlen(src) + (tuser ? strlen(tuser) : 0) + | |
| 308 | strlen(thost) + strlen(targ) + CMDNEEDS + 20; | |
| 309 | if (!(bp = malloc(len))) | |
| 310 | err(1, "malloc"); | |
| 311 | if (host) { | |
| 312 | *host++ = 0; | |
| 313 | suser = argv[i]; | |
| 314 | if (*suser == '\0') | |
| 315 | suser = pwd->pw_name; | |
| 316 | else if (!okname(suser)) { | |
| 317 | ++errs; | |
| 318 | continue; | |
| 319 | } | |
| 57fed2af | 320 | snprintf(bp, len, |
| 984263bc MD |
321 | "%s %s -l %s -n %s %s '%s%s%s:%s'", |
| 322 | _PATH_RSH, host, suser, cmd, src, | |
| 323 | tuser ? tuser : "", tuser ? "@" : "", | |
| 324 | thost, targ); | |
| 325 | } else | |
| 57fed2af | 326 | snprintf(bp, len, |
| 984263bc MD |
327 | "exec %s %s -n %s %s '%s%s%s:%s'", |
| 328 | _PATH_RSH, argv[i], cmd, src, | |
| 329 | tuser ? tuser : "", tuser ? "@" : "", | |
| 330 | thost, targ); | |
| 57fed2af EN |
331 | susystem(bp, userid); |
| 332 | free(bp); | |
| 984263bc MD |
333 | } else { /* local to remote */ |
| 334 | if (rem == -1) { | |
| 335 | len = strlen(targ) + CMDNEEDS + 20; | |
| 336 | if (!(bp = malloc(len))) | |
| 337 | err(1, "malloc"); | |
| 57fed2af | 338 | snprintf(bp, len, "%s -t %s", cmd, targ); |
| 984263bc MD |
339 | host = thost; |
| 340 | #ifdef KERBEROS | |
| 341 | if (use_kerberos) | |
| 342 | rem = kerberos(&host, bp, | |
| 343 | pwd->pw_name, | |
| 344 | tuser ? tuser : pwd->pw_name); | |
| 345 | else | |
| 346 | #endif | |
| 347 | rem = rcmd_af(&host, port, | |
| 348 | pwd->pw_name, | |
| 349 | tuser ? tuser : pwd->pw_name, | |
| 350 | bp, 0, family); | |
| 351 | if (rem < 0) | |
| 352 | exit(1); | |
| 353 | if (family == PF_INET) { | |
| 354 | tos = IPTOS_THROUGHPUT; | |
| 355 | if (setsockopt(rem, IPPROTO_IP, IP_TOS, | |
| 356 | &tos, sizeof(int)) < 0) | |
| 357 | warn("TOS (ignored)"); | |
| 358 | } | |
| 359 | if (response() < 0) | |
| 360 | exit(1); | |
| 57fed2af EN |
361 | free(bp); |
| 362 | setuid(userid); | |
| 984263bc MD |
363 | } |
| 364 | source(1, argv+i); | |
| 365 | } | |
| 366 | } | |
| 367 | } | |
| 368 | ||
| 369 | void | |
| 370 | tolocal(int argc, char *argv[]) | |
| 371 | { | |
| 372 | int i, len, tos; | |
| 373 | char *bp, *host, *src, *suser; | |
| 374 | ||
| 375 | for (i = 0; i < argc - 1; i++) { | |
| 376 | if (!(src = colon(argv[i]))) { /* Local to local. */ | |
| 377 | len = strlen(_PATH_CP) + strlen(argv[i]) + | |
| 378 | strlen(argv[argc - 1]) + 20; | |
| 379 | if (!(bp = malloc(len))) | |
| 380 | err(1, "malloc"); | |
| 57fed2af | 381 | snprintf(bp, len, "exec %s%s%s %s %s", _PATH_CP, |
| 984263bc MD |
382 | iamrecursive ? " -PR" : "", pflag ? " -p" : "", |
| 383 | argv[i], argv[argc - 1]); | |
| 384 | if (susystem(bp, userid)) | |
| 385 | ++errs; | |
| 57fed2af | 386 | free(bp); |
| 984263bc MD |
387 | continue; |
| 388 | } | |
| 389 | *src++ = 0; | |
| 390 | if (*src == 0) | |
| c5fe5fd1 | 391 | src = __DECONST(char *, "."); |
| 984263bc MD |
392 | if ((host = strchr(argv[i], '@')) == NULL) { |
| 393 | host = argv[i]; | |
| 394 | suser = pwd->pw_name; | |
| 395 | } else { | |
| 396 | *host++ = 0; | |
| 397 | suser = argv[i]; | |
| 398 | if (*suser == '\0') | |
| 399 | suser = pwd->pw_name; | |
| 400 | else if (!okname(suser)) { | |
| 401 | ++errs; | |
| 402 | continue; | |
| 403 | } | |
| 404 | } | |
| 405 | len = strlen(src) + CMDNEEDS + 20; | |
| 406 | if ((bp = malloc(len)) == NULL) | |
| 407 | err(1, "malloc"); | |
| 57fed2af | 408 | snprintf(bp, len, "%s -f %s", cmd, src); |
| 984263bc MD |
409 | rem = |
| 410 | #ifdef KERBEROS | |
| 411 | use_kerberos ? | |
| 412 | kerberos(&host, bp, pwd->pw_name, suser) : | |
| 413 | #endif | |
| 414 | rcmd_af(&host, port, pwd->pw_name, suser, bp, 0, | |
| 415 | family); | |
| 57fed2af | 416 | free(bp); |
| 984263bc MD |
417 | if (rem < 0) { |
| 418 | ++errs; | |
| 419 | continue; | |
| 420 | } | |
| 57fed2af | 421 | seteuid(userid); |
| 984263bc MD |
422 | if (family == PF_INET) { |
| 423 | tos = IPTOS_THROUGHPUT; | |
| 424 | if (setsockopt(rem, IPPROTO_IP, IP_TOS, &tos, | |
| 425 | sizeof(int)) < 0) | |
| 426 | warn("TOS (ignored)"); | |
| 427 | } | |
| 428 | sink(1, argv + argc - 1); | |
| 57fed2af EN |
429 | seteuid(0); |
| 430 | close(rem); | |
| 984263bc MD |
431 | rem = -1; |
| 432 | } | |
| 433 | } | |
| 434 | ||
| 435 | void | |
| 436 | source(int argc, char *argv[]) | |
| 437 | { | |
| 438 | struct stat stb; | |
| 439 | static BUF buffer; | |
| 440 | BUF *bp; | |
| 441 | off_t i; | |
| 442 | int amt, fd, haderr, indx, result; | |
| 443 | char *last, *name, buf[BUFSIZ]; | |
| 444 | ||
| 445 | for (indx = 0; indx < argc; ++indx) { | |
| 446 | name = argv[indx]; | |
| 447 | if ((fd = open(name, O_RDONLY, 0)) < 0) | |
| 448 | goto syserr; | |
| 449 | if (fstat(fd, &stb)) { | |
| 450 | syserr: run_err("%s: %s", name, strerror(errno)); | |
| 451 | goto next; | |
| 452 | } | |
| 453 | switch (stb.st_mode & S_IFMT) { | |
| 454 | case S_IFREG: | |
| 455 | break; | |
| 456 | case S_IFDIR: | |
| 457 | if (iamrecursive) { | |
| 458 | rsource(name, &stb); | |
| 459 | goto next; | |
| 460 | } | |
| 461 | /* FALLTHROUGH */ | |
| 462 | default: | |
| 463 | run_err("%s: not a regular file", name); | |
| 464 | goto next; | |
| 465 | } | |
| 466 | if ((last = strrchr(name, '/')) == NULL) | |
| 467 | last = name; | |
| 468 | else | |
| 469 | ++last; | |
| 470 | if (pflag) { | |
| 471 | /* | |
| 472 | * Make it compatible with possible future | |
| 473 | * versions expecting microseconds. | |
| 474 | */ | |
| 57fed2af | 475 | snprintf(buf, sizeof(buf), "T%ld 0 %ld 0\n", |
| 984263bc MD |
476 | (long)stb.st_mtimespec.tv_sec, |
| 477 | (long)stb.st_atimespec.tv_sec); | |
| 57fed2af | 478 | write(rem, buf, strlen(buf)); |
| 984263bc MD |
479 | if (response() < 0) |
| 480 | goto next; | |
| 481 | } | |
| 482 | #define MODEMASK (S_ISUID|S_ISGID|S_ISTXT|S_IRWXU|S_IRWXG|S_IRWXO) | |
| 782054ba SW |
483 | snprintf(buf, sizeof(buf), "C%04o %jd %s\n", |
| 484 | stb.st_mode & MODEMASK, (intmax_t)stb.st_size, last); | |
| 57fed2af | 485 | write(rem, buf, strlen(buf)); |
| 984263bc MD |
486 | if (response() < 0) |
| 487 | goto next; | |
| 488 | if ((bp = allocbuf(&buffer, fd, BUFSIZ)) == NULL) { | |
| 489 | next: (void)close(fd); | |
| 490 | continue; | |
| 491 | } | |
| 492 | ||
| 493 | /* Keep writing after an error so that we stay sync'd up. */ | |
| 494 | for (haderr = i = 0; i < stb.st_size; i += bp->cnt) { | |
| 495 | amt = bp->cnt; | |
| 496 | if (i + amt > stb.st_size) | |
| 497 | amt = stb.st_size - i; | |
| 498 | if (!haderr) { | |
| 499 | result = read(fd, bp->buf, amt); | |
| 500 | if (result != amt) | |
| 501 | haderr = result >= 0 ? EIO : errno; | |
| 502 | } | |
| 503 | if (haderr) | |
| 57fed2af | 504 | write(rem, bp->buf, amt); |
| 984263bc MD |
505 | else { |
| 506 | result = write(rem, bp->buf, amt); | |
| 507 | if (result != amt) | |
| 508 | haderr = result >= 0 ? EIO : errno; | |
| 509 | } | |
| 510 | } | |
| 511 | if (close(fd) && !haderr) | |
| 512 | haderr = errno; | |
| 513 | if (!haderr) | |
| 57fed2af | 514 | write(rem, "", 1); |
| 984263bc MD |
515 | else |
| 516 | run_err("%s: %s", name, strerror(haderr)); | |
| 57fed2af | 517 | response(); |
| 984263bc MD |
518 | } |
| 519 | } | |
| 520 | ||
| 521 | void | |
| 522 | rsource(char *name, struct stat *statp) | |
| 523 | { | |
| 524 | DIR *dirp; | |
| 525 | struct dirent *dp; | |
| 526 | char *last, *vect[1], path[PATH_MAX]; | |
| 527 | ||
| 528 | if (!(dirp = opendir(name))) { | |
| 529 | run_err("%s: %s", name, strerror(errno)); | |
| 530 | return; | |
| 531 | } | |
| 532 | last = strrchr(name, '/'); | |
| 678e8cc6 | 533 | if (last == NULL) |
| 984263bc MD |
534 | last = name; |
| 535 | else | |
| 536 | last++; | |
| 537 | if (pflag) { | |
| 57fed2af | 538 | snprintf(path, sizeof(path), "T%ld 0 %ld 0\n", |
| 984263bc MD |
539 | (long)statp->st_mtimespec.tv_sec, |
| 540 | (long)statp->st_atimespec.tv_sec); | |
| 57fed2af | 541 | write(rem, path, strlen(path)); |
| 984263bc MD |
542 | if (response() < 0) { |
| 543 | closedir(dirp); | |
| 544 | return; | |
| 545 | } | |
| 546 | } | |
| 57fed2af | 547 | snprintf(path, sizeof(path), |
| 984263bc | 548 | "D%04o %d %s\n", statp->st_mode & MODEMASK, 0, last); |
| 57fed2af | 549 | write(rem, path, strlen(path)); |
| 984263bc MD |
550 | if (response() < 0) { |
| 551 | closedir(dirp); | |
| 552 | return; | |
| 553 | } | |
| 554 | while ((dp = readdir(dirp))) { | |
| 555 | if (dp->d_ino == 0) | |
| 556 | continue; | |
| 557 | if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) | |
| 558 | continue; | |
| 559 | if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path)) { | |
| 560 | run_err("%s/%s: name too long", name, dp->d_name); | |
| 561 | continue; | |
| 562 | } | |
| 57fed2af | 563 | snprintf(path, sizeof(path), "%s/%s", name, dp->d_name); |
| 984263bc MD |
564 | vect[0] = path; |
| 565 | source(1, vect); | |
| 566 | } | |
| 57fed2af EN |
567 | closedir(dirp); |
| 568 | write(rem, "E\n", 2); | |
| 569 | response(); | |
| 984263bc MD |
570 | } |
| 571 | ||
| 572 | void | |
| 573 | sink(int argc, char *argv[]) | |
| 574 | { | |
| 575 | static BUF buffer; | |
| 576 | struct stat stb; | |
| 577 | struct timeval tv[2]; | |
| 578 | enum { YES, NO, DISPLAYED } wrerr; | |
| 579 | BUF *bp; | |
| 580 | off_t i, j, size; | |
| 782054ba SW |
581 | size_t count; |
| 582 | int amt, exists, first, mask, mode, ofd, omode; | |
| 984263bc | 583 | int setimes, targisdir, wrerrno = 0; |
| a17407d2 | 584 | char ch, *cp, *np, *targ, *why, *vect[1], buf[BUFSIZ], path[PATH_MAX]; |
| 984263bc MD |
585 | |
| 586 | #define atime tv[0] | |
| 587 | #define mtime tv[1] | |
| c5fe5fd1 | 588 | #define SCREWUP(str) { why = __DECONST(char *, str); goto screwup; } |
| 984263bc MD |
589 | |
| 590 | setimes = targisdir = 0; | |
| 591 | mask = umask(0); | |
| 592 | if (!pflag) | |
| 57fed2af | 593 | umask(mask); |
| 984263bc MD |
594 | if (argc != 1) { |
| 595 | run_err("ambiguous target"); | |
| 596 | exit(1); | |
| 597 | } | |
| 598 | targ = *argv; | |
| 599 | if (targetshouldbedirectory) | |
| 600 | verifydir(targ); | |
| 57fed2af | 601 | write(rem, "", 1); |
| 984263bc MD |
602 | if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode)) |
| 603 | targisdir = 1; | |
| 604 | for (first = 1;; first = 0) { | |
| 605 | cp = buf; | |
| 606 | if (read(rem, cp, 1) <= 0) | |
| 607 | return; | |
| 608 | if (*cp++ == '\n') | |
| 609 | SCREWUP("unexpected <newline>"); | |
| 610 | do { | |
| 611 | if (read(rem, &ch, sizeof(ch)) != sizeof(ch)) | |
| 612 | SCREWUP("lost connection"); | |
| 613 | *cp++ = ch; | |
| 614 | } while (cp < &buf[BUFSIZ - 1] && ch != '\n'); | |
| 615 | *cp = 0; | |
| 616 | ||
| 617 | if (buf[0] == '\01' || buf[0] == '\02') { | |
| 618 | if (iamremote == 0) | |
| 57fed2af | 619 | write(STDERR_FILENO, |
| 984263bc MD |
620 | buf + 1, strlen(buf + 1)); |
| 621 | if (buf[0] == '\02') | |
| 622 | exit(1); | |
| 623 | ++errs; | |
| 624 | continue; | |
| 625 | } | |
| 626 | if (buf[0] == 'E') { | |
| 57fed2af | 627 | write(rem, "", 1); |
| 984263bc MD |
628 | return; |
| 629 | } | |
| 630 | ||
| 631 | if (ch == '\n') | |
| 632 | *--cp = 0; | |
| 633 | ||
| 634 | cp = buf; | |
| 635 | if (*cp == 'T') { | |
| 636 | setimes++; | |
| 637 | cp++; | |
| 638 | mtime.tv_sec = strtol(cp, &cp, 10); | |
| 639 | if (!cp || *cp++ != ' ') | |
| 640 | SCREWUP("mtime.sec not delimited"); | |
| 641 | mtime.tv_usec = strtol(cp, &cp, 10); | |
| 642 | if (!cp || *cp++ != ' ') | |
| 643 | SCREWUP("mtime.usec not delimited"); | |
| 644 | atime.tv_sec = strtol(cp, &cp, 10); | |
| 645 | if (!cp || *cp++ != ' ') | |
| 646 | SCREWUP("atime.sec not delimited"); | |
| 647 | atime.tv_usec = strtol(cp, &cp, 10); | |
| 648 | if (!cp || *cp++ != '\0') | |
| 649 | SCREWUP("atime.usec not delimited"); | |
| 57fed2af | 650 | write(rem, "", 1); |
| 984263bc MD |
651 | continue; |
| 652 | } | |
| 653 | if (*cp != 'C' && *cp != 'D') { | |
| 654 | /* | |
| 655 | * Check for the case "rcp remote:foo\* local:bar". | |
| 656 | * In this case, the line "No match." can be returned | |
| 657 | * by the shell before the rcp command on the remote is | |
| 658 | * executed so the ^Aerror_message convention isn't | |
| 659 | * followed. | |
| 660 | */ | |
| 661 | if (first) { | |
| 662 | run_err("%s", cp); | |
| 663 | exit(1); | |
| 664 | } | |
| 665 | SCREWUP("expected control record"); | |
| 666 | } | |
| 667 | mode = 0; | |
| 668 | for (++cp; cp < buf + 5; cp++) { | |
| 669 | if (*cp < '0' || *cp > '7') | |
| 670 | SCREWUP("bad mode"); | |
| 671 | mode = (mode << 3) | (*cp - '0'); | |
| 672 | } | |
| 673 | if (*cp++ != ' ') | |
| 674 | SCREWUP("mode not delimited"); | |
| 675 | ||
| 676 | for (size = 0; isdigit(*cp);) | |
| 677 | size = size * 10 + (*cp++ - '0'); | |
| 678 | if (*cp++ != ' ') | |
| 679 | SCREWUP("size not delimited"); | |
| 680 | if (targisdir) { | |
| a17407d2 MD |
681 | if (strlen(targ) + (*targ ? 1 : 0) + strlen(cp) |
| 682 | >= sizeof(path)) { | |
| 683 | run_err("%s%s%s: name too long", targ, | |
| 684 | *targ ? "/" : "", cp); | |
| 685 | exit(1); | |
| 984263bc | 686 | } |
| a17407d2 MD |
687 | snprintf(path, sizeof(path), "%s%s%s", targ, |
| 688 | *targ ? "/" : "", cp); | |
| 689 | np = path; | |
| 984263bc MD |
690 | } else |
| 691 | np = targ; | |
| 692 | exists = stat(np, &stb) == 0; | |
| 693 | if (buf[0] == 'D') { | |
| 694 | int mod_flag = pflag; | |
| 695 | if (exists) { | |
| 696 | if (!S_ISDIR(stb.st_mode)) { | |
| 697 | errno = ENOTDIR; | |
| 698 | goto bad; | |
| 699 | } | |
| 700 | if (pflag) | |
| 57fed2af | 701 | chmod(np, mode); |
| 984263bc MD |
702 | } else { |
| 703 | /* Handle copying from a read-only directory */ | |
| 704 | mod_flag = 1; | |
| 705 | if (mkdir(np, mode | S_IRWXU) < 0) | |
| 706 | goto bad; | |
| 707 | } | |
| 708 | vect[0] = np; | |
| 709 | sink(1, vect); | |
| 710 | if (setimes) { | |
| 711 | setimes = 0; | |
| 712 | if (utimes(np, tv) < 0) | |
| 713 | run_err("%s: set times: %s", | |
| 714 | np, strerror(errno)); | |
| 715 | } | |
| 716 | if (mod_flag) | |
| 57fed2af | 717 | chmod(np, mode); |
| 984263bc MD |
718 | continue; |
| 719 | } | |
| 720 | omode = mode; | |
| 721 | mode |= S_IWRITE; | |
| 722 | if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) { | |
| 723 | bad: run_err("%s: %s", np, strerror(errno)); | |
| 724 | continue; | |
| 725 | } | |
| 57fed2af | 726 | write(rem, "", 1); |
| 984263bc | 727 | if ((bp = allocbuf(&buffer, ofd, BUFSIZ)) == NULL) { |
| 57fed2af | 728 | close(ofd); |
| 984263bc MD |
729 | continue; |
| 730 | } | |
| 731 | cp = bp->buf; | |
| 732 | wrerr = NO; | |
| 733 | for (count = i = 0; i < size; i += BUFSIZ) { | |
| 734 | amt = BUFSIZ; | |
| 735 | if (i + amt > size) | |
| 736 | amt = size - i; | |
| 737 | count += amt; | |
| 738 | do { | |
| 739 | j = read(rem, cp, amt); | |
| 740 | if (j <= 0) { | |
| 741 | run_err("%s", j ? strerror(errno) : | |
| 742 | "dropped connection"); | |
| 743 | exit(1); | |
| 744 | } | |
| 745 | amt -= j; | |
| 746 | cp += j; | |
| 747 | } while (amt > 0); | |
| 748 | if (count == bp->cnt) { | |
| 749 | /* Keep reading so we stay sync'd up. */ | |
| 750 | if (wrerr == NO) { | |
| 751 | j = write(ofd, bp->buf, count); | |
| 782054ba | 752 | if (j != (off_t)count) { |
| 984263bc MD |
753 | wrerr = YES; |
| 754 | wrerrno = j >= 0 ? EIO : errno; | |
| 755 | } | |
| 756 | } | |
| 757 | count = 0; | |
| 758 | cp = bp->buf; | |
| 759 | } | |
| 760 | } | |
| 761 | if (count != 0 && wrerr == NO && | |
| 782054ba | 762 | (j = write(ofd, bp->buf, count)) != (off_t)count) { |
| 984263bc MD |
763 | wrerr = YES; |
| 764 | wrerrno = j >= 0 ? EIO : errno; | |
| 765 | } | |
| 766 | if (ftruncate(ofd, size)) { | |
| 767 | run_err("%s: truncate: %s", np, strerror(errno)); | |
| 768 | wrerr = DISPLAYED; | |
| 769 | } | |
| 770 | if (pflag) { | |
| 771 | if (exists || omode != mode) | |
| 772 | if (fchmod(ofd, omode)) | |
| 773 | run_err("%s: set mode: %s", | |
| 774 | np, strerror(errno)); | |
| 775 | } else { | |
| 776 | if (!exists && omode != mode) | |
| 777 | if (fchmod(ofd, omode & ~mask)) | |
| 778 | run_err("%s: set mode: %s", | |
| 779 | np, strerror(errno)); | |
| 780 | } | |
| 57fed2af EN |
781 | close(ofd); |
| 782 | response(); | |
| 984263bc MD |
783 | if (setimes && wrerr == NO) { |
| 784 | setimes = 0; | |
| 785 | if (utimes(np, tv) < 0) { | |
| 786 | run_err("%s: set times: %s", | |
| 787 | np, strerror(errno)); | |
| 788 | wrerr = DISPLAYED; | |
| 789 | } | |
| 790 | } | |
| 791 | switch(wrerr) { | |
| 792 | case YES: | |
| 793 | run_err("%s: %s", np, strerror(wrerrno)); | |
| 794 | break; | |
| 795 | case NO: | |
| 57fed2af | 796 | write(rem, "", 1); |
| 984263bc MD |
797 | break; |
| 798 | case DISPLAYED: | |
| 799 | break; | |
| 800 | } | |
| 801 | } | |
| 802 | screwup: | |
| 803 | run_err("protocol error: %s", why); | |
| 804 | exit(1); | |
| 805 | } | |
| 806 | ||
| 807 | #ifdef KERBEROS | |
| 808 | int | |
| 809 | kerberos(char **host, char *bp, char *locuser, char *user) | |
| 810 | { | |
| 811 | if (use_kerberos) { | |
| 812 | setuid(getuid()); | |
| 813 | rem = KSUCCESS; | |
| 814 | errno = 0; | |
| 815 | if (dest_realm == NULL) | |
| 816 | dest_realm = krb_realmofhost(*host); | |
| 817 | rem = | |
| 818 | #ifdef CRYPT | |
| 819 | doencrypt ? | |
| 820 | krcmd_mutual(host, | |
| 821 | port, user, bp, 0, dest_realm, &cred, schedule) : | |
| 822 | #endif | |
| 823 | krcmd(host, port, user, bp, 0, dest_realm); | |
| 824 | ||
| 825 | if (rem < 0) { | |
| 826 | if (errno == ECONNREFUSED) | |
| 827 | oldw("remote host doesn't support Kerberos"); | |
| 828 | else if (errno == ENOENT) | |
| 829 | oldw("can't provide Kerberos authentication data"); | |
| 830 | execv(_PATH_RCP, argv_copy); | |
| 831 | err(1, "execv: %s", _PATH_RCP); | |
| 832 | } | |
| 833 | } else { | |
| 834 | #ifdef CRYPT | |
| 835 | if (doencrypt) | |
| 836 | errx(1, | |
| 837 | "the -x option requires Kerberos authentication"); | |
| 838 | #endif | |
| 839 | rem = rcmd_af(host, port, locuser, user, bp, 0, family); | |
| 840 | } | |
| 841 | return (rem); | |
| 842 | } | |
| 843 | #endif /* KERBEROS */ | |
| 844 | ||
| 845 | int | |
| 846 | response(void) | |
| 847 | { | |
| 848 | char ch, *cp, resp, rbuf[BUFSIZ]; | |
| 849 | ||
| 850 | if (read(rem, &resp, sizeof(resp)) != sizeof(resp)) | |
| 851 | lostconn(0); | |
| 852 | ||
| 853 | cp = rbuf; | |
| 854 | switch(resp) { | |
| 855 | case 0: /* ok */ | |
| 856 | return (0); | |
| 857 | default: | |
| 858 | *cp++ = resp; | |
| 859 | /* FALLTHROUGH */ | |
| 860 | case 1: /* error, followed by error msg */ | |
| 861 | case 2: /* fatal error, "" */ | |
| 862 | do { | |
| 863 | if (read(rem, &ch, sizeof(ch)) != sizeof(ch)) | |
| 864 | lostconn(0); | |
| 865 | *cp++ = ch; | |
| 866 | } while (cp < &rbuf[BUFSIZ] && ch != '\n'); | |
| 867 | ||
| 868 | if (!iamremote) | |
| 57fed2af | 869 | write(STDERR_FILENO, rbuf, cp - rbuf); |
| 984263bc MD |
870 | ++errs; |
| 871 | if (resp == 1) | |
| 872 | return (-1); | |
| 873 | exit(1); | |
| 874 | } | |
| 875 | /* NOTREACHED */ | |
| 876 | } | |
| 877 | ||
| 878 | void | |
| 879 | usage(void) | |
| 880 | { | |
| 881 | #ifdef KERBEROS | |
| 882 | #ifdef CRYPT | |
| 57fed2af | 883 | fprintf(stderr, "%s\n%s\n", |
| 984263bc MD |
884 | "usage: rcp [-46Kpx] [-k realm] f1 f2", |
| 885 | " rcp [-46Kprx] [-k realm] f1 ... fn directory"); | |
| 886 | #else | |
| 57fed2af | 887 | fprintf(stderr, "%s\n%s\n", |
| 984263bc MD |
888 | "usage: rcp [-46Kp] [-k realm] f1 f2", |
| 889 | " rcp [-46Kpr] [-k realm] f1 ... fn directory"); | |
| 890 | #endif | |
| 891 | #else | |
| 57fed2af | 892 | fprintf(stderr, "%s\n%s\n", |
| 984263bc MD |
893 | "usage: rcp [-46p] f1 f2", |
| 894 | " rcp [-46pr] f1 ... fn directory"); | |
| 895 | #endif | |
| 896 | exit(1); | |
| 897 | } | |
| 898 | ||
| 899 | #include <stdarg.h> | |
| 900 | ||
| 901 | #ifdef KERBEROS | |
| 902 | void | |
| 903 | oldw(const char *fmt, ...) | |
| 904 | { | |
| 905 | va_list ap; | |
| 906 | va_start(ap, fmt); | |
| 57fed2af EN |
907 | fprintf(stderr, "rcp: "); |
| 908 | vfprintf(stderr, fmt, ap); | |
| 909 | fprintf(stderr, ", using standard rcp\n"); | |
| 984263bc MD |
910 | va_end(ap); |
| 911 | } | |
| 912 | #endif | |
| 913 | ||
| 914 | void | |
| 915 | run_err(const char *fmt, ...) | |
| 916 | { | |
| 917 | static FILE *fp; | |
| 918 | va_list ap; | |
| 919 | va_start(ap, fmt); | |
| 920 | ||
| 921 | ++errs; | |
| 922 | if (fp == NULL && !(fp = fdopen(rem, "w"))) | |
| 923 | return; | |
| 57fed2af EN |
924 | fprintf(fp, "%c", 0x01); |
| 925 | fprintf(fp, "rcp: "); | |
| 926 | vfprintf(fp, fmt, ap); | |
| 927 | fprintf(fp, "\n"); | |
| 928 | fflush(fp); | |
| 984263bc MD |
929 | |
| 930 | if (!iamremote) | |
| 931 | vwarnx(fmt, ap); | |
| 932 | ||
| 933 | va_end(ap); | |
| 934 | } |