1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1988, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * David Hitz of Auspex Systems Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 /* 36 * Cp copies source files to target files. 37 * 38 * The global PATH_T structure "to" always contains the path to the 39 * current target file. Since fts(3) does not change directories, 40 * this path can be either absolute or dot-relative. 41 * 42 * The basic algorithm is to initialize "to" and use fts(3) to traverse 43 * the file hierarchy rooted in the argument list. A trivial case is the 44 * case of 'cp file1 file2'. The more interesting case is the case of 45 * 'cp file1 file2 ... fileN dir' where the hierarchy is traversed and the 46 * path (relative to the root of the traversal) is appended to dir (stored 47 * in "to") to form the final target path. 48 */ 49 50 #include <sys/types.h> 51 #include <sys/stat.h> 52 53 #include <assert.h> 54 #include <err.h> 55 #include <errno.h> 56 #include <fcntl.h> 57 #include <fts.h> 58 #include <limits.h> 59 #include <signal.h> 60 #include <stdbool.h> 61 #include <stdio.h> 62 #include <stdlib.h> 63 #include <string.h> 64 #include <unistd.h> 65 66 #include "extern.h" 67 68 static char dot[] = "."; 69 70 #define END(buf) (buf + sizeof(buf)) 71 PATH_T to = { .dir = -1, .end = to.path }; 72 int Nflag, fflag, iflag, lflag, nflag, pflag, sflag, vflag; 73 static int Hflag, Lflag, Pflag, Rflag, rflag; 74 volatile sig_atomic_t info; 75 76 enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE }; 77 78 static int copy(char *[], enum op, int, struct stat *); 79 static void siginfo(int __unused); 80 81 int 82 main(int argc, char *argv[]) 83 { 84 struct stat to_stat, tmp_stat; 85 enum op type; 86 int ch, fts_options, r; 87 char *sep, *target; 88 bool have_trailing_slash = false; 89 90 fts_options = FTS_NOCHDIR | FTS_PHYSICAL; 91 while ((ch = getopt(argc, argv, "HLPRafilNnprsvx")) != -1) 92 switch (ch) { 93 case 'H': 94 Hflag = 1; 95 Lflag = Pflag = 0; 96 break; 97 case 'L': 98 Lflag = 1; 99 Hflag = Pflag = 0; 100 break; 101 case 'P': 102 Pflag = 1; 103 Hflag = Lflag = 0; 104 break; 105 case 'R': 106 Rflag = 1; 107 break; 108 case 'a': 109 pflag = 1; 110 Rflag = 1; 111 Pflag = 1; 112 Hflag = Lflag = 0; 113 break; 114 case 'f': 115 fflag = 1; 116 iflag = nflag = 0; 117 break; 118 case 'i': 119 iflag = 1; 120 fflag = nflag = 0; 121 break; 122 case 'l': 123 lflag = 1; 124 break; 125 case 'N': 126 Nflag = 1; 127 break; 128 case 'n': 129 nflag = 1; 130 fflag = iflag = 0; 131 break; 132 case 'p': 133 pflag = 1; 134 break; 135 case 'r': 136 rflag = Lflag = 1; 137 Hflag = Pflag = 0; 138 break; 139 case 's': 140 sflag = 1; 141 break; 142 case 'v': 143 vflag = 1; 144 break; 145 case 'x': 146 fts_options |= FTS_XDEV; 147 break; 148 default: 149 usage(); 150 } 151 argc -= optind; 152 argv += optind; 153 154 if (argc < 2) 155 usage(); 156 157 if (Rflag && rflag) 158 errx(1, "the -R and -r options may not be specified together"); 159 if (lflag && sflag) 160 errx(1, "the -l and -s options may not be specified together"); 161 if (rflag) 162 Rflag = 1; 163 if (Rflag) { 164 if (Hflag) 165 fts_options |= FTS_COMFOLLOW; 166 if (Lflag) { 167 fts_options &= ~FTS_PHYSICAL; 168 fts_options |= FTS_LOGICAL; 169 } 170 } else if (!Pflag) { 171 fts_options &= ~FTS_PHYSICAL; 172 fts_options |= FTS_LOGICAL | FTS_COMFOLLOW; 173 } 174 (void)signal(SIGINFO, siginfo); 175 176 /* Save the target base in "to". */ 177 target = argv[--argc]; 178 if (*target == '\0') { 179 target = dot; 180 } else if ((sep = strrchr(target, '/')) != NULL && sep[1] == '\0') { 181 have_trailing_slash = true; 182 while (sep > target + 1 && *(sep - 1) == '/') 183 sep--; 184 *sep = '\0'; 185 } 186 /* 187 * Copy target into to.base, leaving room for a possible separator 188 * which will be appended later in the non-FILE_TO_FILE cases. 189 */ 190 if (strlcpy(to.base, target, sizeof(to.base) - 1) >= 191 sizeof(to.base) - 1) 192 errc(1, ENAMETOOLONG, "%s", target); 193 194 /* Set end of argument list for fts(3). */ 195 argv[argc] = NULL; 196 197 /* 198 * Cp has two distinct cases: 199 * 200 * cp [-R] source target 201 * cp [-R] source1 ... sourceN directory 202 * 203 * In both cases, source can be either a file or a directory. 204 * 205 * In (1), the target becomes a copy of the source. That is, if the 206 * source is a file, the target will be a file, and likewise for 207 * directories. 208 * 209 * In (2), the real target is not directory, but "directory/source". 210 */ 211 r = stat(to.base, &to_stat); 212 if (r == -1 && errno != ENOENT) 213 err(1, "%s", target); 214 if (r == -1 || !S_ISDIR(to_stat.st_mode)) { 215 /* 216 * Case (1). Target is not a directory. 217 */ 218 if (argc > 1) 219 errc(1, ENOTDIR, "%s", target); 220 221 /* 222 * Need to detect the case: 223 * cp -R dir foo 224 * Where dir is a directory and foo does not exist, where 225 * we want pathname concatenations turned on but not for 226 * the initial mkdir(). 227 */ 228 if (r == -1) { 229 if (Rflag && (Lflag || Hflag)) 230 stat(*argv, &tmp_stat); 231 else 232 lstat(*argv, &tmp_stat); 233 234 if (S_ISDIR(tmp_stat.st_mode) && Rflag) 235 type = DIR_TO_DNE; 236 else 237 type = FILE_TO_FILE; 238 } else 239 type = FILE_TO_FILE; 240 241 if (have_trailing_slash && type == FILE_TO_FILE) { 242 if (r == -1) 243 errc(1, ENOENT, "%s", target); 244 else 245 errc(1, ENOTDIR, "%s", target); 246 } 247 } else { 248 /* 249 * Case (2). Target is a directory. 250 */ 251 type = FILE_TO_DIR; 252 } 253 254 /* 255 * For DIR_TO_DNE, we could provide copy() with the to_stat we've 256 * already allocated on the stack here that isn't being used for 257 * anything. Not doing so, though, simplifies later logic a little bit 258 * as we need to skip checking root_stat on the first iteration and 259 * ensure that we set it with the first mkdir(). 260 */ 261 exit (copy(argv, type, fts_options, (type == DIR_TO_DNE ? NULL : 262 &to_stat))); 263 } 264 265 static int 266 copy(char *argv[], enum op type, int fts_options, struct stat *root_stat) 267 { 268 char rootname[NAME_MAX]; 269 struct stat created_root_stat, to_stat, *curr_stat; 270 FTS *ftsp; 271 FTSENT *curr; 272 char *recpath = NULL, *sep; 273 int atflags, dne, badcp, len, rval; 274 mode_t mask, mode; 275 bool beneath = Rflag && type != FILE_TO_FILE; 276 bool skipdp = false; 277 278 /* 279 * Keep an inverted copy of the umask, for use in correcting 280 * permissions on created directories when not using -p. 281 */ 282 mask = ~umask(0777); 283 umask(~mask); 284 285 if (type == FILE_TO_FILE) { 286 to.dir = AT_FDCWD; 287 to.end = to.path + strlcpy(to.path, to.base, sizeof(to.path)); 288 to.base[0] = '\0'; 289 } else if (type == FILE_TO_DIR) { 290 to.dir = open(to.base, O_DIRECTORY | O_SEARCH); 291 if (to.dir < 0) 292 err(1, "%s", to.base); 293 /* 294 * We have previously made sure there is room for this. 295 */ 296 sep = strchr(to.base, '\0'); 297 sep[0] = '/'; 298 sep[1] = '\0'; 299 } else { 300 /* 301 * We will create the destination directory imminently. 302 */ 303 to.dir = -1; 304 } 305 306 if ((ftsp = fts_open(argv, fts_options, NULL)) == NULL) 307 err(1, "fts_open"); 308 for (badcp = rval = 0; 309 (curr = fts_read(ftsp)) != NULL; 310 badcp = 0, *to.end = '\0') { 311 curr_stat = curr->fts_statp; 312 switch (curr->fts_info) { 313 case FTS_NS: 314 case FTS_DNR: 315 case FTS_ERR: 316 warnc(curr->fts_errno, "%s", curr->fts_path); 317 badcp = rval = 1; 318 continue; 319 case FTS_DC: /* Warn, continue. */ 320 warnx("%s: directory causes a cycle", curr->fts_path); 321 badcp = rval = 1; 322 continue; 323 case FTS_D: 324 /* 325 * Stash the root basename off for detecting 326 * recursion later. 327 * 328 * This will be essential if the root is a symlink 329 * and we're rolling with -L or -H. The later 330 * bits will need this bit in particular. 331 */ 332 if (curr->fts_level == FTS_ROOTLEVEL) { 333 strlcpy(rootname, curr->fts_name, 334 sizeof(rootname)); 335 } 336 /* 337 * If we FTS_SKIP while handling FTS_D, we will 338 * immediately get FTS_DP for the same directory. 339 * If this happens before we've appended the name 340 * to to.path, we need to remember not to perform 341 * the reverse operation. 342 */ 343 skipdp = true; 344 /* we must have a destination! */ 345 if (type == DIR_TO_DNE && 346 curr->fts_level == FTS_ROOTLEVEL) { 347 assert(to.dir < 0); 348 assert(root_stat == NULL); 349 mode = curr_stat->st_mode | S_IRWXU; 350 /* 351 * Will our umask prevent us from entering 352 * the directory after we create it? 353 */ 354 if (~mask & S_IRWXU) 355 umask(~mask & ~S_IRWXU); 356 if (mkdir(to.base, mode) != 0) { 357 warn("%s", to.base); 358 fts_set(ftsp, curr, FTS_SKIP); 359 badcp = rval = 1; 360 if (~mask & S_IRWXU) 361 umask(~mask); 362 continue; 363 } 364 to.dir = open(to.base, O_DIRECTORY | O_SEARCH); 365 if (to.dir < 0) { 366 warn("%s", to.base); 367 (void)rmdir(to.base); 368 fts_set(ftsp, curr, FTS_SKIP); 369 badcp = rval = 1; 370 if (~mask & S_IRWXU) 371 umask(~mask); 372 continue; 373 } 374 if (fstat(to.dir, &created_root_stat) != 0) { 375 warn("%s", to.base); 376 (void)close(to.dir); 377 (void)rmdir(to.base); 378 fts_set(ftsp, curr, FTS_SKIP); 379 to.dir = -1; 380 badcp = rval = 1; 381 if (~mask & S_IRWXU) 382 umask(~mask); 383 continue; 384 } 385 if (~mask & S_IRWXU) 386 umask(~mask); 387 root_stat = &created_root_stat; 388 curr->fts_number = 1; 389 /* 390 * We have previously made sure there is 391 * room for this. 392 */ 393 sep = strchr(to.base, '\0'); 394 sep[0] = '/'; 395 sep[1] = '\0'; 396 } else { 397 /* entering a directory; append its name to to.path */ 398 len = snprintf(to.end, END(to.path) - to.end, "%s%s", 399 to.end > to.path ? "/" : "", curr->fts_name); 400 if (to.end + len >= END(to.path)) { 401 *to.end = '\0'; 402 warnc(ENAMETOOLONG, "%s%s%s%s", to.base, 403 to.path, to.end > to.path ? "/" : "", 404 curr->fts_name); 405 fts_set(ftsp, curr, FTS_SKIP); 406 badcp = rval = 1; 407 continue; 408 } 409 to.end += len; 410 } 411 skipdp = false; 412 /* 413 * We're on the verge of recursing on ourselves. 414 * Either we need to stop right here (we knowingly 415 * just created it), or we will in an immediate 416 * descendant. Record the path of the immediate 417 * descendant to make our lives a little less 418 * complicated looking. 419 */ 420 if (type != FILE_TO_FILE && 421 root_stat->st_dev == curr_stat->st_dev && 422 root_stat->st_ino == curr_stat->st_ino) { 423 assert(recpath == NULL); 424 if (root_stat == &created_root_stat) { 425 /* 426 * This directory didn't exist 427 * when we started, we created it 428 * as part of traversal. Stop 429 * right here before we do 430 * something silly. 431 */ 432 fts_set(ftsp, curr, FTS_SKIP); 433 continue; 434 } 435 if (asprintf(&recpath, "%s/%s", to.path, 436 rootname) < 0) { 437 warnc(ENOMEM, NULL); 438 fts_set(ftsp, curr, FTS_SKIP); 439 badcp = rval = 1; 440 continue; 441 } 442 } 443 if (recpath != NULL && 444 strcmp(recpath, to.path) == 0) { 445 fts_set(ftsp, curr, FTS_SKIP); 446 continue; 447 } 448 break; 449 case FTS_DP: 450 /* 451 * We are nearly finished with this directory. If we 452 * didn't actually copy it, or otherwise don't need to 453 * change its attributes, then we are done. 454 * 455 * If -p is in effect, set all the attributes. 456 * Otherwise, set the correct permissions, limited 457 * by the umask. Optimise by avoiding a chmod() 458 * if possible (which is usually the case if we 459 * made the directory). Note that mkdir() does not 460 * honour setuid, setgid and sticky bits, but we 461 * normally want to preserve them on directories. 462 */ 463 if (curr->fts_number && pflag) { 464 int fd = *to.path ? -1 : to.dir; 465 if (setfile(curr_stat, fd, true)) 466 rval = 1; 467 if (preserve_dir_acls(curr->fts_accpath, 468 to.path) != 0) 469 rval = 1; 470 } else if (curr->fts_number) { 471 const char *path = *to.path ? to.path : dot; 472 mode = curr_stat->st_mode; 473 if (fchmodat(to.dir, path, mode & mask, 0) != 0) { 474 warn("chmod: %s%s", to.base, to.path); 475 rval = 1; 476 } 477 } 478 /* are we leaving a directory we failed to enter? */ 479 if (skipdp) 480 continue; 481 /* leaving a directory; remove its name from to.path */ 482 if (type == DIR_TO_DNE && 483 curr->fts_level == FTS_ROOTLEVEL) { 484 /* this is actually our created root */ 485 } else { 486 while (to.end > to.path && *to.end != '/') 487 to.end--; 488 assert(strcmp(to.end + (*to.end == '/'), curr->fts_name) == 0); 489 *to.end = '\0'; 490 } 491 continue; 492 default: 493 /* something else: append its name to to.path */ 494 if (type == FILE_TO_FILE) 495 break; 496 len = snprintf(to.end, END(to.path) - to.end, "%s%s", 497 to.end > to.path ? "/" : "", curr->fts_name); 498 if (to.end + len >= END(to.path)) { 499 *to.end = '\0'; 500 warnc(ENAMETOOLONG, "%s%s%s%s", to.base, 501 to.path, to.end > to.path ? "/" : "", 502 curr->fts_name); 503 badcp = rval = 1; 504 continue; 505 } 506 /* intentionally do not update to.end */ 507 break; 508 } 509 510 /* Not an error but need to remember it happened. */ 511 if (to.path[0] == '\0') { 512 /* 513 * This can happen in two cases: 514 * - DIR_TO_DNE; we created the directory and 515 * populated root_stat earlier. 516 * - FILE_TO_DIR if a source has a trailing slash; 517 * the caller populated root_stat. 518 */ 519 dne = false; 520 to_stat = *root_stat; 521 } else { 522 atflags = beneath ? AT_RESOLVE_BENEATH : 0; 523 if (curr->fts_info == FTS_D || curr->fts_info == FTS_SL) 524 atflags |= AT_SYMLINK_NOFOLLOW; 525 dne = fstatat(to.dir, to.path, &to_stat, atflags) != 0; 526 } 527 528 /* Check if source and destination are identical. */ 529 if (!dne && 530 to_stat.st_dev == curr_stat->st_dev && 531 to_stat.st_ino == curr_stat->st_ino) { 532 warnx("%s%s and %s are identical (not copied).", 533 to.base, to.path, curr->fts_path); 534 badcp = rval = 1; 535 if (S_ISDIR(curr_stat->st_mode)) 536 fts_set(ftsp, curr, FTS_SKIP); 537 continue; 538 } 539 540 switch (curr_stat->st_mode & S_IFMT) { 541 case S_IFLNK: 542 if ((fts_options & FTS_LOGICAL) || 543 ((fts_options & FTS_COMFOLLOW) && 544 curr->fts_level == 0)) { 545 /* 546 * We asked FTS to follow links but got 547 * here anyway, which means the target is 548 * nonexistent or inaccessible. Let 549 * copy_file() deal with the error. 550 */ 551 if (copy_file(curr, dne, beneath)) 552 badcp = rval = 1; 553 } else { 554 /* Copy the link. */ 555 if (copy_link(curr, dne, beneath)) 556 badcp = rval = 1; 557 } 558 break; 559 case S_IFDIR: 560 if (!Rflag) { 561 warnx("%s is a directory (not copied).", 562 curr->fts_path); 563 fts_set(ftsp, curr, FTS_SKIP); 564 badcp = rval = 1; 565 break; 566 } 567 /* 568 * If the directory doesn't exist, create the new 569 * one with the from file mode plus owner RWX bits, 570 * modified by the umask. Trade-off between being 571 * able to write the directory (if from directory is 572 * 555) and not causing a permissions race. If the 573 * umask blocks owner writes, we fail. 574 */ 575 if (dne) { 576 mode = curr_stat->st_mode | S_IRWXU; 577 /* 578 * Will our umask prevent us from entering 579 * the directory after we create it? 580 */ 581 if (~mask & S_IRWXU) 582 umask(~mask & ~S_IRWXU); 583 if (mkdirat(to.dir, to.path, mode) != 0) { 584 warn("%s%s", to.base, to.path); 585 fts_set(ftsp, curr, FTS_SKIP); 586 badcp = rval = 1; 587 if (~mask & S_IRWXU) 588 umask(~mask); 589 break; 590 } 591 if (~mask & S_IRWXU) 592 umask(~mask); 593 } else if (!S_ISDIR(to_stat.st_mode)) { 594 warnc(ENOTDIR, "%s%s", to.base, to.path); 595 fts_set(ftsp, curr, FTS_SKIP); 596 badcp = rval = 1; 597 break; 598 } 599 /* 600 * Arrange to correct directory attributes later 601 * (in the post-order phase) if this is a new 602 * directory, or if the -p flag is in effect. 603 * Note that fts_number may already be set if this 604 * is the newly created destination directory. 605 */ 606 curr->fts_number |= pflag || dne; 607 break; 608 case S_IFBLK: 609 case S_IFCHR: 610 if (Rflag && !sflag) { 611 if (copy_special(curr_stat, dne, beneath)) 612 badcp = rval = 1; 613 } else { 614 if (copy_file(curr, dne, beneath)) 615 badcp = rval = 1; 616 } 617 break; 618 case S_IFSOCK: 619 warnx("%s is a socket (not copied).", 620 curr->fts_path); 621 break; 622 case S_IFIFO: 623 if (Rflag && !sflag) { 624 if (copy_fifo(curr_stat, dne, beneath)) 625 badcp = rval = 1; 626 } else { 627 if (copy_file(curr, dne, beneath)) 628 badcp = rval = 1; 629 } 630 break; 631 default: 632 if (copy_file(curr, dne, beneath)) 633 badcp = rval = 1; 634 break; 635 } 636 if (vflag && !badcp) 637 (void)printf("%s -> %s%s\n", curr->fts_path, to.base, to.path); 638 } 639 if (errno) 640 err(1, "fts_read"); 641 (void)fts_close(ftsp); 642 if (to.dir != AT_FDCWD && to.dir >= 0) 643 (void)close(to.dir); 644 free(recpath); 645 return (rval); 646 } 647 648 static void 649 siginfo(int sig __unused) 650 { 651 652 info = 1; 653 } 654