1 /* $OpenBSD: cp.c,v 1.8 2019/06/28 13:34:59 deraadt Exp $ */ 2 /* $NetBSD: cp.c,v 1.14 1995/09/07 06:14:51 jtc Exp $ */ 3 4 /* 5 * Copyright (c) 1988, 1993, 1994 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * David Hitz of Auspex Systems Inc. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 /* 37 * Cp copies source files to target files. 38 * 39 * The global PATH_T structure "to" always contains the path to the 40 * current target file. Since fts(3) does not change directories, 41 * this path can be either absolute or dot-relative. 42 * 43 * The basic algorithm is to initialize "to" and use fts(3) to traverse 44 * the file hierarchy rooted in the argument list. A trivial case is the 45 * case of 'cp file1 file2'. The more interesting case is the case of 46 * 'cp file1 file2 ... fileN dir' where the hierarchy is traversed and the 47 * path (relative to the root of the traversal) is appended to dir (stored 48 * in "to") to form the final target path. 49 */ 50 51 #include <sys/types.h> 52 #include <sys/stat.h> 53 #include <sys/mman.h> 54 #include <sys/time.h> 55 56 #include <dirent.h> 57 #include <err.h> 58 #include <errno.h> 59 #include <fcntl.h> 60 #include <fts.h> 61 #include <stdio.h> 62 #include <stdlib.h> 63 #include <string.h> 64 #include <unistd.h> 65 #include <limits.h> 66 67 #define fts_dne(_x) (_x->fts_pointer != NULL) 68 69 typedef struct { 70 char *p_end; /* pointer to NULL at end of path */ 71 char *target_end; /* pointer to end of target base */ 72 char p_path[PATH_MAX]; /* pointer to the start of a path */ 73 } PATH_T; 74 75 static PATH_T to = { to.p_path, "" }; 76 77 static int copy_fifo(struct stat *, int); 78 static int copy_file(FTSENT *, int); 79 static int copy_link(FTSENT *, int); 80 static int copy_special(struct stat *, int); 81 static int setfile(struct stat *, int); 82 83 84 extern char *__progname; 85 86 static uid_t myuid; 87 static int fflag, iflag; 88 static mode_t myumask; 89 90 enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE }; 91 92 static int copy(char *[], enum op, int); 93 static char *find_last_component(char *); 94 95 static void __dead 96 usage(void) 97 { 98 (void)fprintf(stderr, 99 "usage: %s [-fip] [-R [-H | -L | -P]] source target\n", __progname); 100 (void)fprintf(stderr, 101 " %s [-fip] [-R [-H | -L | -P]] source ... directory\n", 102 __progname); 103 exit(1); 104 } 105 106 int 107 cpmain(int argc, char *argv[]) 108 { 109 struct stat to_stat, tmp_stat; 110 enum op type; 111 int fts_options, r; 112 char *target; 113 114 fts_options = FTS_NOCHDIR | FTS_PHYSICAL; 115 116 myuid = getuid(); 117 118 /* Copy the umask for explicit mode setting. */ 119 myumask = umask(0); 120 (void)umask(myumask); 121 122 /* Save the target base in "to". */ 123 target = argv[--argc]; 124 if (strlcpy(to.p_path, target, sizeof to.p_path) >= sizeof(to.p_path)) 125 errx(1, "%s: name too long", target); 126 to.p_end = to.p_path + strlen(to.p_path); 127 if (to.p_path == to.p_end) { 128 *to.p_end++ = '.'; 129 *to.p_end = '\0'; 130 } 131 to.target_end = to.p_end; 132 133 /* Set end of argument list for fts(3). */ 134 argv[argc] = NULL; 135 136 /* 137 * Cp has two distinct cases: 138 * 139 * cp [-R] source target 140 * cp [-R] source1 ... sourceN directory 141 * 142 * In both cases, source can be either a file or a directory. 143 * 144 * In (1), the target becomes a copy of the source. That is, if the 145 * source is a file, the target will be a file, and likewise for 146 * directories. 147 * 148 * In (2), the real target is not directory, but "directory/source". 149 */ 150 r = stat(to.p_path, &to_stat); 151 if (r == -1 && errno != ENOENT) 152 err(1, "%s", to.p_path); 153 if (r == -1 || !S_ISDIR(to_stat.st_mode)) { 154 /* 155 * Case (1). Target is not a directory. 156 */ 157 if (argc > 1) 158 usage(); 159 /* 160 * Need to detect the case: 161 * cp -R dir foo 162 * Where dir is a directory and foo does not exist, where 163 * we want pathname concatenations turned on but not for 164 * the initial mkdir(). 165 */ 166 if (r == -1) { 167 lstat(*argv, &tmp_stat); 168 169 if (S_ISDIR(tmp_stat.st_mode)) 170 type = DIR_TO_DNE; 171 else 172 type = FILE_TO_FILE; 173 } else 174 type = FILE_TO_FILE; 175 } else { 176 /* 177 * Case (2). Target is a directory. 178 */ 179 type = FILE_TO_DIR; 180 } 181 182 return (copy(argv, type, fts_options)); 183 } 184 185 static char * 186 find_last_component(char *path) 187 { 188 char *p; 189 190 if ((p = strrchr(path, '/')) == NULL) 191 p = path; 192 else { 193 /* Special case foo/ */ 194 if (!*(p+1)) { 195 while ((p >= path) && *p == '/') 196 p--; 197 198 while ((p >= path) && *p != '/') 199 p--; 200 } 201 202 p++; 203 } 204 205 return (p); 206 } 207 208 static int 209 copy(char *argv[], enum op type, int fts_options) 210 { 211 struct stat to_stat; 212 FTS *ftsp; 213 FTSENT *curr; 214 int base, nlen, rval; 215 char *p, *target_mid; 216 base = 0; 217 218 if ((ftsp = fts_open(argv, fts_options, NULL)) == NULL) 219 err(1, NULL); 220 for (rval = 0; (curr = fts_read(ftsp)) != NULL;) { 221 switch (curr->fts_info) { 222 case FTS_NS: 223 case FTS_DNR: 224 case FTS_ERR: 225 warnx("%s: %s", 226 curr->fts_path, strerror(curr->fts_errno)); 227 rval = 1; 228 continue; 229 case FTS_DC: 230 warnx("%s: directory causes a cycle", curr->fts_path); 231 rval = 1; 232 continue; 233 } 234 235 /* 236 * If we are in case (2) or (3) above, we need to append the 237 * source name to the target name. 238 */ 239 if (type != FILE_TO_FILE) { 240 /* 241 * Need to remember the roots of traversals to create 242 * correct pathnames. If there's a directory being 243 * copied to a non-existent directory, e.g. 244 * cp -R a/dir noexist 245 * the resulting path name should be noexist/foo, not 246 * noexist/dir/foo (where foo is a file in dir), which 247 * is the case where the target exists. 248 * 249 * Also, check for "..". This is for correct path 250 * concatenation for paths ending in "..", e.g. 251 * cp -R .. /tmp 252 * Paths ending in ".." are changed to ".". This is 253 * tricky, but seems the easiest way to fix the problem. 254 * 255 * XXX 256 * Since the first level MUST be FTS_ROOTLEVEL, base 257 * is always initialized. 258 */ 259 if (curr->fts_level == FTS_ROOTLEVEL) { 260 if (type != DIR_TO_DNE) { 261 p = find_last_component(curr->fts_path); 262 base = p - curr->fts_path; 263 264 if (!strcmp(&curr->fts_path[base], 265 "..")) 266 base += 1; 267 } else 268 base = curr->fts_pathlen; 269 } 270 271 p = &curr->fts_path[base]; 272 nlen = curr->fts_pathlen - base; 273 target_mid = to.target_end; 274 if (*p != '/' && target_mid[-1] != '/') 275 *target_mid++ = '/'; 276 *target_mid = '\0'; 277 if (target_mid - to.p_path + nlen >= PATH_MAX) { 278 warnx("%s%s: name too long (not copied)", 279 to.p_path, p); 280 rval = 1; 281 continue; 282 } 283 (void)strncat(target_mid, p, nlen); 284 to.p_end = target_mid + nlen; 285 *to.p_end = '\0'; 286 } 287 288 /* Not an error but need to remember it happened */ 289 if (stat(to.p_path, &to_stat) == -1) { 290 if (curr->fts_info == FTS_DP) 291 continue; 292 /* 293 * We use fts_pointer as a boolean to indicate that 294 * we created this directory ourselves. We'll use 295 * this later on via the fts_dne macro to decide 296 * whether or not to set the directory mode during 297 * the post-order pass. 298 */ 299 curr->fts_pointer = (void *)1; 300 } else { 301 /* 302 * Set directory mode/user/times on the post-order 303 * pass. We can't do this earlier because the mode 304 * may not allow us write permission. Furthermore, 305 * if we set the times during the pre-order pass, 306 * they will get changed later when the directory 307 * is populated. 308 */ 309 if (curr->fts_info == FTS_DP) { 310 if (!S_ISDIR(to_stat.st_mode)) 311 continue; 312 /* 313 * If not -p and directory didn't exist, set 314 * it to be the same as the from directory, 315 * unmodified by the umask; arguably wrong, 316 * but it's been that way forever. 317 */ 318 if (setfile(curr->fts_statp, -1)) 319 rval = 1; 320 else if (fts_dne(curr)) 321 (void)chmod(to.p_path, 322 curr->fts_statp->st_mode); 323 continue; 324 } 325 if (to_stat.st_dev == curr->fts_statp->st_dev && 326 to_stat.st_ino == curr->fts_statp->st_ino) { 327 warnx("%s and %s are identical (not copied).", 328 to.p_path, curr->fts_path); 329 rval = 1; 330 if (S_ISDIR(curr->fts_statp->st_mode)) 331 (void)fts_set(ftsp, curr, FTS_SKIP); 332 continue; 333 } 334 if (!S_ISDIR(curr->fts_statp->st_mode) && 335 S_ISDIR(to_stat.st_mode)) { 336 warnx("cannot overwrite directory %s with non-directory %s", 337 to.p_path, curr->fts_path); 338 rval = 1; 339 continue; 340 } 341 } 342 343 switch (curr->fts_statp->st_mode & S_IFMT) { 344 case S_IFLNK: 345 if (copy_link(curr, !fts_dne(curr))) 346 rval = 1; 347 break; 348 case S_IFDIR: 349 /* 350 * If the directory doesn't exist, create the new 351 * one with the from file mode plus owner RWX bits, 352 * modified by the umask. Trade-off between being 353 * able to write the directory (if from directory is 354 * 555) and not causing a permissions race. If the 355 * umask blocks owner writes, we fail.. 356 */ 357 if (fts_dne(curr)) { 358 if (mkdir(to.p_path, 359 curr->fts_statp->st_mode | S_IRWXU) == -1) 360 err(1, "%s", to.p_path); 361 } else if (!S_ISDIR(to_stat.st_mode)) 362 errc(1, ENOTDIR, "%s", to.p_path); 363 break; 364 case S_IFBLK: 365 case S_IFCHR: 366 if (copy_special(curr->fts_statp, !fts_dne(curr))) 367 rval = 1; 368 break; 369 case S_IFIFO: 370 if (copy_fifo(curr->fts_statp, !fts_dne(curr))) 371 rval = 1; 372 break; 373 case S_IFSOCK: 374 warnc(EOPNOTSUPP, "%s", curr->fts_path); 375 break; 376 default: 377 if (copy_file(curr, fts_dne(curr))) 378 rval = 1; 379 break; 380 } 381 } 382 if (errno) 383 err(1, "fts_read"); 384 (void)fts_close(ftsp); 385 return (rval); 386 } 387 388 389 /* $OpenBSD: cp.c,v 1.8 2019/06/28 13:34:59 deraadt Exp $ */ 390 /* $NetBSD: utils.c,v 1.6 1997/02/26 14:40:51 cgd Exp $ */ 391 392 /*- 393 * Copyright (c) 1991, 1993, 1994 394 * The Regents of the University of California. All rights reserved. 395 * 396 * Redistribution and use in source and binary forms, with or without 397 * modification, are permitted provided that the following conditions 398 * are met: 399 * 1. Redistributions of source code must retain the above copyright 400 * notice, this list of conditions and the following disclaimer. 401 * 2. Redistributions in binary form must reproduce the above copyright 402 * notice, this list of conditions and the following disclaimer in the 403 * documentation and/or other materials provided with the distribution. 404 * 3. Neither the name of the University nor the names of its contributors 405 * may be used to endorse or promote products derived from this software 406 * without specific prior written permission. 407 * 408 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 409 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 410 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 411 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 412 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 413 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 414 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 415 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 416 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 417 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 418 * SUCH DAMAGE. 419 */ 420 421 #include <sys/param.h> /* MAXBSIZE */ 422 #include <sys/stat.h> 423 #include <sys/mman.h> 424 #include <sys/time.h> 425 426 #include <err.h> 427 #include <errno.h> 428 #include <fcntl.h> 429 #include <fts.h> 430 #include <stdio.h> 431 #include <stdlib.h> 432 #include <string.h> 433 #include <unistd.h> 434 #include <limits.h> 435 436 static int 437 copy_file(FTSENT *entp, int dne) 438 { 439 static char *buf; 440 static char *zeroes; 441 struct stat *fs; 442 int ch, checkch, from_fd, rcount, rval, to_fd, wcount; 443 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED 444 char *p; 445 #endif 446 447 if (!buf) { 448 buf = malloc(MAXBSIZE); 449 if (!buf) 450 err(1, "malloc"); 451 } 452 if (!zeroes) { 453 zeroes = calloc(1, MAXBSIZE); 454 if (!zeroes) 455 err(1, "calloc"); 456 } 457 458 if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) { 459 warn("%s", entp->fts_path); 460 return (1); 461 } 462 463 fs = entp->fts_statp; 464 465 /* 466 * In -f (force) mode, we always unlink the destination first 467 * if it exists. Note that -i and -f are mutually exclusive. 468 */ 469 if (!dne && fflag) 470 (void)unlink(to.p_path); 471 472 /* 473 * If the file exists and we're interactive, verify with the user. 474 * If the file DNE, set the mode to be the from file, minus setuid 475 * bits, modified by the umask; arguably wrong, but it makes copying 476 * executables work right and it's been that way forever. (The 477 * other choice is 666 or'ed with the execute bits on the from file 478 * modified by the umask.) 479 */ 480 if (!dne && !fflag) { 481 if (iflag) { 482 (void)fprintf(stderr, "overwrite %s? ", to.p_path); 483 checkch = ch = getchar(); 484 while (ch != '\n' && ch != EOF) 485 ch = getchar(); 486 if (checkch != 'y' && checkch != 'Y') { 487 (void)close(from_fd); 488 return (0); 489 } 490 } 491 to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0); 492 } else 493 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT, 494 fs->st_mode & ~(S_ISTXT | S_ISUID | S_ISGID)); 495 496 if (to_fd == -1) { 497 warn("%s", to.p_path); 498 (void)close(from_fd); 499 return (1); 500 } 501 502 rval = 0; 503 504 /* 505 * Mmap and write if less than 8M (the limit is so we don't totally 506 * trash memory on big files. This is really a minor hack, but it 507 * wins some CPU back. 508 */ 509 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED 510 /* XXX broken for 0-size mmap */ 511 if (fs->st_size <= 8 * 1048576) { 512 if ((p = mmap(NULL, (size_t)fs->st_size, PROT_READ, 513 MAP_FILE|MAP_SHARED, from_fd, (off_t)0)) == MAP_FAILED) { 514 warn("mmap: %s", entp->fts_path); 515 rval = 1; 516 } else { 517 madvise(p, fs->st_size, MADV_SEQUENTIAL); 518 if (write(to_fd, p, fs->st_size) != fs->st_size) { 519 warn("%s", to.p_path); 520 rval = 1; 521 } 522 /* Some systems don't unmap on close(2). */ 523 if (munmap(p, fs->st_size) == -1) { 524 warn("%s", entp->fts_path); 525 rval = 1; 526 } 527 } 528 } else 529 #endif 530 { 531 int skipholes = 0; 532 struct stat tosb; 533 if (!fstat(to_fd, &tosb) && S_ISREG(tosb.st_mode)) 534 skipholes = 1; 535 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) { 536 if (skipholes && memcmp(buf, zeroes, rcount) == 0) 537 wcount = lseek(to_fd, rcount, SEEK_CUR) == -1 ? -1 : rcount; 538 else 539 wcount = write(to_fd, buf, rcount); 540 if (rcount != wcount || wcount == -1) { 541 warn("%s", to.p_path); 542 rval = 1; 543 break; 544 } 545 } 546 if (skipholes && rcount >= 0) 547 rcount = ftruncate(to_fd, lseek(to_fd, 0, SEEK_CUR)); 548 if (rcount == -1) { 549 warn("%s", entp->fts_path); 550 rval = 1; 551 } 552 } 553 554 if (rval == 1) { 555 (void)close(from_fd); 556 (void)close(to_fd); 557 return (1); 558 } 559 560 if (setfile(fs, to_fd)) 561 rval = 1; 562 (void)close(from_fd); 563 if (close(to_fd)) { 564 warn("%s", to.p_path); 565 rval = 1; 566 } 567 return (rval); 568 } 569 570 static int 571 copy_link(FTSENT *p, int exists) 572 { 573 int len; 574 char linkname[PATH_MAX]; 575 576 if ((len = readlink(p->fts_path, linkname, sizeof(linkname)-1)) == -1) { 577 warn("readlink: %s", p->fts_path); 578 return (1); 579 } 580 linkname[len] = '\0'; 581 if (exists && unlink(to.p_path)) { 582 warn("unlink: %s", to.p_path); 583 return (1); 584 } 585 if (symlink(linkname, to.p_path)) { 586 warn("symlink: %s", linkname); 587 return (1); 588 } 589 return (setfile(p->fts_statp, -1)); 590 } 591 592 static int 593 copy_fifo(struct stat *from_stat, int exists) 594 { 595 if (exists && unlink(to.p_path)) { 596 warn("unlink: %s", to.p_path); 597 return (1); 598 } 599 if (mkfifo(to.p_path, from_stat->st_mode)) { 600 warn("mkfifo: %s", to.p_path); 601 return (1); 602 } 603 return (setfile(from_stat, -1)); 604 } 605 606 static int 607 copy_special(struct stat *from_stat, int exists) 608 { 609 if (exists && unlink(to.p_path)) { 610 warn("unlink: %s", to.p_path); 611 return (1); 612 } 613 if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) { 614 warn("mknod: %s", to.p_path); 615 return (1); 616 } 617 return (setfile(from_stat, -1)); 618 } 619 620 621 static int 622 setfile(struct stat *fs, int fd) 623 { 624 struct timespec ts[2]; 625 int rval; 626 627 rval = 0; 628 fs->st_mode &= S_ISTXT | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO; 629 630 ts[0] = fs->st_atim; 631 ts[1] = fs->st_mtim; 632 if (fd >= 0 ? futimens(fd, ts) : 633 utimensat(AT_FDCWD, to.p_path, ts, AT_SYMLINK_NOFOLLOW)) { 634 warn("update times: %s", to.p_path); 635 rval = 1; 636 } 637 /* 638 * Changing the ownership probably won't succeed, unless we're root 639 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting 640 * the mode; current BSD behavior is to remove all setuid bits on 641 * chown. If chown fails, lose setuid/setgid bits. 642 */ 643 if (fd >= 0 ? fchown(fd, fs->st_uid, fs->st_gid) : 644 lchown(to.p_path, fs->st_uid, fs->st_gid)) { 645 if (errno != EPERM) { 646 warn("chown: %s", to.p_path); 647 rval = 1; 648 } 649 fs->st_mode &= ~(S_ISTXT | S_ISUID | S_ISGID); 650 } 651 if (fd >= 0 ? fchmod(fd, fs->st_mode) : 652 fchmodat(AT_FDCWD, to.p_path, fs->st_mode, AT_SYMLINK_NOFOLLOW)) { 653 warn("chmod: %s", to.p_path); 654 rval = 1; 655 } 656 657 /* 658 * XXX 659 * NFS doesn't support chflags; ignore errors unless there's reason 660 * to believe we're losing bits. (Note, this still won't be right 661 * if the server supports flags and we were trying to *remove* flags 662 * on a file that we copied, i.e., that we didn't create.) 663 */ 664 errno = 0; 665 if (fd >= 0 ? fchflags(fd, fs->st_flags) : 666 chflagsat(AT_FDCWD, to.p_path, fs->st_flags, AT_SYMLINK_NOFOLLOW)) 667 if (errno != EOPNOTSUPP || fs->st_flags != 0) { 668 warn("chflags: %s", to.p_path); 669 rval = 1; 670 } 671 return (rval); 672 } 673