Home | History | Annotate | Line # | Download | only in cp
utils.c revision 1.48
      1 /* $NetBSD: utils.c,v 1.48 2020/05/16 18:31:45 christos Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1991, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "@(#)utils.c	8.3 (Berkeley) 4/1/94";
     36 #else
     37 __RCSID("$NetBSD: utils.c,v 1.48 2020/05/16 18:31:45 christos Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 #define _ACL_PRIVATE
     42 #include <sys/mman.h>
     43 #include <sys/param.h>
     44 #include <sys/stat.h>
     45 #include <sys/time.h>
     46 #include <sys/acl.h>
     47 #include <sys/extattr.h>
     48 
     49 #include <err.h>
     50 #include <errno.h>
     51 #include <fcntl.h>
     52 #include <fts.h>
     53 #include <stdbool.h>
     54 #include <stdio.h>
     55 #include <stdlib.h>
     56 #include <string.h>
     57 #include <unistd.h>
     58 
     59 #include "extern.h"
     60 
     61 #define	MMAP_MAX_SIZE	(8 * 1048576)
     62 #define	MMAP_MAX_WRITE	(64 * 1024)
     63 
     64 int
     65 set_utimes(const char *file, struct stat *fs)
     66 {
     67     struct timespec ts[2];
     68 
     69     ts[0] = fs->st_atimespec;
     70     ts[1] = fs->st_mtimespec;
     71 
     72     if (lutimens(file, ts)) {
     73 	warn("lutimens: %s", file);
     74 	return (1);
     75     }
     76     return (0);
     77 }
     78 
     79 struct finfo {
     80 	const char *from;
     81 	const char *to;
     82 	off_t size;
     83 };
     84 
     85 static void
     86 progress(const struct finfo *fi, off_t written)
     87 {
     88 	int pcent = (int)((100.0 * written) / fi->size);
     89 
     90 	pinfo = 0;
     91 	(void)fprintf(stderr, "%s => %s %llu/%llu bytes %d%% written\n",
     92 	    fi->from, fi->to, (unsigned long long)written,
     93 	    (unsigned long long)fi->size, pcent);
     94 }
     95 
     96 int
     97 copy_file(FTSENT *entp, int dne)
     98 {
     99 	static char buf[MAXBSIZE];
    100 	struct stat to_stat, *fs;
    101 	int ch, checkch, from_fd, rcount, rval, to_fd, tolnk, wcount;
    102 	char *p;
    103 	off_t ptotal = 0;
    104 
    105 	/* if hard linking then simply link and return */
    106 	if (lflag) {
    107 		(void)unlink(to.p_path);
    108 		if (link(entp->fts_path, to.p_path)) {
    109 			warn("%s", to.p_path);
    110 			return (1);
    111 		}
    112 		return (0);
    113 	}
    114 
    115 	if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
    116 		warn("%s", entp->fts_path);
    117 		return (1);
    118 	}
    119 
    120 	to_fd = -1;
    121 	fs = entp->fts_statp;
    122 	tolnk = ((Rflag && !(Lflag || Hflag)) || Pflag);
    123 
    124 	/*
    125 	 * If the file exists and we're interactive, verify with the user.
    126 	 * If the file DNE, set the mode to be the from file, minus setuid
    127 	 * bits, modified by the umask; arguably wrong, but it makes copying
    128 	 * executables work right and it's been that way forever.  (The
    129 	 * other choice is 666 or'ed with the execute bits on the from file
    130 	 * modified by the umask.)
    131 	 */
    132 	if (!dne) {
    133 		struct stat sb;
    134 		int sval;
    135 
    136 		if (iflag) {
    137 			(void)fprintf(stderr, "overwrite %s? ", to.p_path);
    138 			checkch = ch = getchar();
    139 			while (ch != '\n' && ch != EOF)
    140 				ch = getchar();
    141 			if (checkch != 'y' && checkch != 'Y') {
    142 				(void)close(from_fd);
    143 				return (0);
    144 			}
    145 		}
    146 
    147 		sval = tolnk ?
    148 			lstat(to.p_path, &sb) : stat(to.p_path, &sb);
    149 		if (sval == -1) {
    150 			warn("stat: %s", to.p_path);
    151 			(void)close(from_fd);
    152 			return (1);
    153 		}
    154 
    155 		if (!(tolnk && S_ISLNK(sb.st_mode)))
    156 			to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
    157 	} else
    158 		to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
    159 		    fs->st_mode & ~(S_ISUID | S_ISGID));
    160 
    161 	if (to_fd == -1 && (fflag || tolnk)) {
    162 		/*
    163 		 * attempt to remove existing destination file name and
    164 		 * create a new file
    165 		 */
    166 		(void)unlink(to.p_path);
    167 		to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
    168 			     fs->st_mode & ~(S_ISUID | S_ISGID));
    169 	}
    170 
    171 	if (to_fd == -1) {
    172 		warn("%s", to.p_path);
    173 		(void)close(from_fd);
    174 		return (1);
    175 	}
    176 
    177 	rval = 0;
    178 
    179 	/*
    180 	 * There's no reason to do anything other than close the file
    181 	 * now if it's regular and empty, so let's not bother.
    182 	 */
    183 	bool need_copy = !S_ISREG(fs->st_mode) || fs->st_size > 0;
    184 
    185 	struct finfo fi;
    186 
    187 	fi.from = entp->fts_path;
    188 	fi.to = to.p_path;
    189 	fi.size = fs->st_size;
    190 
    191 	/*
    192 	 * Mmap and write if less than 8M (the limit is so
    193 	 * we don't totally trash memory on big files).
    194 	 * This is really a minor hack, but it wins some CPU back.
    195 	 */
    196 	if (S_ISREG(fs->st_mode) && fs->st_size && fs->st_size <= MMAP_MAX_SIZE) {
    197 		size_t fsize = (size_t)fs->st_size;
    198 		p = mmap(NULL, fsize, PROT_READ, MAP_FILE|MAP_SHARED,
    199 		    from_fd, (off_t)0);
    200 		if (p != MAP_FAILED) {
    201 			size_t remainder;
    202 
    203 			need_copy = false;
    204 
    205 			(void) madvise(p, (size_t)fs->st_size, MADV_SEQUENTIAL);
    206 
    207 			/*
    208 			 * Write out the data in small chunks to
    209 			 * avoid locking the output file for a
    210 			 * long time if the reading the data from
    211 			 * the source is slow.
    212 			 */
    213 			remainder = fsize;
    214 			do {
    215 				ssize_t chunk;
    216 
    217 				chunk = (remainder > MMAP_MAX_WRITE) ?
    218 				    MMAP_MAX_WRITE : remainder;
    219 				if (write(to_fd, &p[fsize - remainder],
    220 				    chunk) != chunk) {
    221 					warn("%s", to.p_path);
    222 					rval = 1;
    223 					break;
    224 				}
    225 				remainder -= chunk;
    226 				ptotal += chunk;
    227 				if (pinfo)
    228 					progress(&fi, ptotal);
    229 			} while (remainder > 0);
    230 
    231 			if (munmap(p, fsize) < 0) {
    232 				warn("%s", entp->fts_path);
    233 				rval = 1;
    234 			}
    235 		}
    236 	}
    237 
    238 	if (need_copy) {
    239 		while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
    240 			wcount = write(to_fd, buf, (size_t)rcount);
    241 			if (rcount != wcount || wcount == -1) {
    242 				warn("%s", to.p_path);
    243 				rval = 1;
    244 				break;
    245 			}
    246 			ptotal += wcount;
    247 			if (pinfo)
    248 				progress(&fi, ptotal);
    249 		}
    250 		if (rcount < 0) {
    251 			warn("%s", entp->fts_path);
    252 			rval = 1;
    253 		}
    254 	}
    255 
    256 	if (pflag && (fcpxattr(from_fd, to_fd) != 0))
    257 		warn("%s: error copying extended attributes", to.p_path);
    258 
    259 	if (pflag && preserve_fd_acls(from_fd, to_fd) != 0)
    260 		rval = 1;
    261 
    262 	(void)close(from_fd);
    263 
    264 	if (rval == 1) {
    265 		(void)close(to_fd);
    266 		return (1);
    267 	}
    268 
    269 	if (pflag && setfile(fs, to_fd))
    270 		rval = 1;
    271 	/*
    272 	 * If the source was setuid or setgid, lose the bits unless the
    273 	 * copy is owned by the same user and group.
    274 	 */
    275 #define	RETAINBITS \
    276 	(S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
    277 	if (!pflag && dne
    278 	    && fs->st_mode & (S_ISUID | S_ISGID) && fs->st_uid == myuid) {
    279 		if (fstat(to_fd, &to_stat)) {
    280 			warn("%s", to.p_path);
    281 			rval = 1;
    282 		} else if (fs->st_gid == to_stat.st_gid &&
    283 		    fchmod(to_fd, fs->st_mode & RETAINBITS & ~myumask)) {
    284 			warn("%s", to.p_path);
    285 			rval = 1;
    286 		}
    287 	}
    288 	if (close(to_fd)) {
    289 		warn("%s", to.p_path);
    290 		rval = 1;
    291 	}
    292 	/* set the mod/access times now after close of the fd */
    293 	if (pflag && set_utimes(to.p_path, fs)) {
    294 	    rval = 1;
    295 	}
    296 	return (rval);
    297 }
    298 
    299 int
    300 copy_link(FTSENT *p, int exists)
    301 {
    302 	int len;
    303 	char target[MAXPATHLEN];
    304 
    305 	if ((len = readlink(p->fts_path, target, sizeof(target)-1)) == -1) {
    306 		warn("readlink: %s", p->fts_path);
    307 		return (1);
    308 	}
    309 	target[len] = '\0';
    310 	if (exists && unlink(to.p_path)) {
    311 		warn("unlink: %s", to.p_path);
    312 		return (1);
    313 	}
    314 	if (symlink(target, to.p_path)) {
    315 		warn("symlink: %s", target);
    316 		return (1);
    317 	}
    318 	return (pflag ? setfile(p->fts_statp, 0) : 0);
    319 }
    320 
    321 int
    322 copy_fifo(struct stat *from_stat, int exists)
    323 {
    324 	if (exists && unlink(to.p_path)) {
    325 		warn("unlink: %s", to.p_path);
    326 		return (1);
    327 	}
    328 	if (mkfifo(to.p_path, from_stat->st_mode)) {
    329 		warn("mkfifo: %s", to.p_path);
    330 		return (1);
    331 	}
    332 	return (pflag ? setfile(from_stat, 0) : 0);
    333 }
    334 
    335 int
    336 copy_special(struct stat *from_stat, int exists)
    337 {
    338 	if (exists && unlink(to.p_path)) {
    339 		warn("unlink: %s", to.p_path);
    340 		return (1);
    341 	}
    342 	if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
    343 		warn("mknod: %s", to.p_path);
    344 		return (1);
    345 	}
    346 	return (pflag ? setfile(from_stat, 0) : 0);
    347 }
    348 
    349 
    350 /*
    351  * Function: setfile
    352  *
    353  * Purpose:
    354  *   Set the owner/group/permissions for the "to" file to the information
    355  *   in the stat structure.  If fd is zero, also call set_utimes() to set
    356  *   the mod/access times.  If fd is non-zero, the caller must do a utimes
    357  *   itself after close(fd).
    358  */
    359 int
    360 setfile(struct stat *fs, int fd)
    361 {
    362 	int rval, islink;
    363 
    364 	rval = 0;
    365 	islink = S_ISLNK(fs->st_mode);
    366 	fs->st_mode &= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
    367 
    368 	/*
    369 	 * Changing the ownership probably won't succeed, unless we're root
    370 	 * or POSIX_CHOWN_RESTRICTED is not set.  Set uid/gid before setting
    371 	 * the mode; current BSD behavior is to remove all setuid bits on
    372 	 * chown.  If chown fails, lose setuid/setgid bits.
    373 	 */
    374 	if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
    375 	    lchown(to.p_path, fs->st_uid, fs->st_gid)) {
    376 		if (errno != EPERM) {
    377 			warn("chown: %s", to.p_path);
    378 			rval = 1;
    379 		}
    380 		fs->st_mode &= ~(S_ISUID | S_ISGID);
    381 	}
    382 	if (fd ? fchmod(fd, fs->st_mode) : lchmod(to.p_path, fs->st_mode)) {
    383 		warn("chmod: %s", to.p_path);
    384 		rval = 1;
    385 	}
    386 
    387 	if (!islink && !Nflag) {
    388 		unsigned long fflags = fs->st_flags;
    389 		/*
    390 		 * XXX
    391 		 * NFS doesn't support chflags; ignore errors unless
    392 		 * there's reason to believe we're losing bits.
    393 		 * (Note, this still won't be right if the server
    394 		 * supports flags and we were trying to *remove* flags
    395 		 * on a file that we copied, i.e., that we didn't create.)
    396 		 */
    397 		errno = 0;
    398 		if ((fd ? fchflags(fd, fflags) :
    399 		    chflags(to.p_path, fflags)) == -1)
    400 			if (errno != EOPNOTSUPP || fs->st_flags != 0) {
    401 				warn("chflags: %s", to.p_path);
    402 				rval = 1;
    403 			}
    404 	}
    405 	/* if fd is non-zero, caller must call set_utimes() after close() */
    406 	if (fd == 0 && set_utimes(to.p_path, fs))
    407 	    rval = 1;
    408 	return (rval);
    409 }
    410 
    411 int
    412 preserve_fd_acls(int source_fd, int dest_fd)
    413 {
    414 	acl_t acl;
    415 	acl_type_t acl_type;
    416 	int acl_supported = 0, ret, trivial;
    417 
    418 	ret = fpathconf(source_fd, _PC_ACL_NFS4);
    419 	if (ret > 0 ) {
    420 		acl_supported = 1;
    421 		acl_type = ACL_TYPE_NFS4;
    422 	} else if (ret < 0 && errno != EINVAL) {
    423 		warn("fpathconf(..., _PC_ACL_NFS4) failed for %s", to.p_path);
    424 		return (1);
    425 	}
    426 	if (acl_supported == 0) {
    427 		ret = fpathconf(source_fd, _PC_ACL_EXTENDED);
    428 		if (ret > 0 ) {
    429 			acl_supported = 1;
    430 			acl_type = ACL_TYPE_ACCESS;
    431 		} else if (ret < 0 && errno != EINVAL) {
    432 			warn("fpathconf(..., _PC_ACL_EXTENDED) failed for %s",
    433 			    to.p_path);
    434 			return (1);
    435 		}
    436 	}
    437 	if (acl_supported == 0)
    438 		return (0);
    439 
    440 	acl = acl_get_fd_np(source_fd, acl_type);
    441 	if (acl == NULL) {
    442 		warn("failed to get acl entries while setting %s", to.p_path);
    443 		return (1);
    444 	}
    445 	if (acl_is_trivial_np(acl, &trivial)) {
    446 		warn("acl_is_trivial() failed for %s", to.p_path);
    447 		acl_free(acl);
    448 		return (1);
    449 	}
    450 	if (trivial) {
    451 		acl_free(acl);
    452 		return (0);
    453 	}
    454 	if (acl_set_fd_np(dest_fd, acl, acl_type) < 0) {
    455 		warn("failed to set acl entries for %s", to.p_path);
    456 		acl_free(acl);
    457 		return (1);
    458 	}
    459 	acl_free(acl);
    460 	return (0);
    461 }
    462 
    463 int
    464 preserve_dir_acls(struct stat *fs, char *source_dir, char *dest_dir)
    465 {
    466 	acl_t (*aclgetf)(const char *, acl_type_t);
    467 	int (*aclsetf)(const char *, acl_type_t, acl_t);
    468 	struct acl *aclp;
    469 	acl_t acl;
    470 	acl_type_t acl_type;
    471 	int acl_supported = 0, ret, trivial;
    472 
    473 	ret = pathconf(source_dir, _PC_ACL_NFS4);
    474 	if (ret > 0) {
    475 		acl_supported = 1;
    476 		acl_type = ACL_TYPE_NFS4;
    477 	} else if (ret < 0 && errno != EINVAL) {
    478 		warn("fpathconf(..., _PC_ACL_NFS4) failed for %s", source_dir);
    479 		return (1);
    480 	}
    481 	if (acl_supported == 0) {
    482 		ret = pathconf(source_dir, _PC_ACL_EXTENDED);
    483 		if (ret > 0) {
    484 			acl_supported = 1;
    485 			acl_type = ACL_TYPE_ACCESS;
    486 		} else if (ret < 0 && errno != EINVAL) {
    487 			warn("fpathconf(..., _PC_ACL_EXTENDED) failed for %s",
    488 			    source_dir);
    489 			return (1);
    490 		}
    491 	}
    492 	if (acl_supported == 0)
    493 		return (0);
    494 
    495 	/*
    496 	 * If the file is a link we will not follow it.
    497 	 */
    498 	if (S_ISLNK(fs->st_mode)) {
    499 		aclgetf = acl_get_link_np;
    500 		aclsetf = acl_set_link_np;
    501 	} else {
    502 		aclgetf = acl_get_file;
    503 		aclsetf = acl_set_file;
    504 	}
    505 	if (acl_type == ACL_TYPE_ACCESS) {
    506 		/*
    507 		 * Even if there is no ACL_TYPE_DEFAULT entry here, a zero
    508 		 * size ACL will be returned. So it is not safe to simply
    509 		 * check the pointer to see if the default ACL is present.
    510 		 */
    511 		acl = aclgetf(source_dir, ACL_TYPE_DEFAULT);
    512 		if (acl == NULL) {
    513 			warn("failed to get default acl entries on %s",
    514 			    source_dir);
    515 			return (1);
    516 		}
    517 		aclp = &acl->ats_acl;
    518 		if (aclp->acl_cnt != 0 && aclsetf(dest_dir,
    519 		    ACL_TYPE_DEFAULT, acl) < 0) {
    520 			warn("failed to set default acl entries on %s",
    521 			    dest_dir);
    522 			acl_free(acl);
    523 			return (1);
    524 		}
    525 		acl_free(acl);
    526 	}
    527 	acl = aclgetf(source_dir, acl_type);
    528 	if (acl == NULL) {
    529 		warn("failed to get acl entries on %s", source_dir);
    530 		return (1);
    531 	}
    532 	if (acl_is_trivial_np(acl, &trivial)) {
    533 		warn("acl_is_trivial() failed on %s", source_dir);
    534 		acl_free(acl);
    535 		return (1);
    536 	}
    537 	if (trivial) {
    538 		acl_free(acl);
    539 		return (0);
    540 	}
    541 	if (aclsetf(dest_dir, acl_type, acl) < 0) {
    542 		warn("failed to set acl entries on %s", dest_dir);
    543 		acl_free(acl);
    544 		return (1);
    545 	}
    546 	acl_free(acl);
    547 	return (0);
    548 }
    549 
    550 void
    551 usage(void)
    552 {
    553 	(void)fprintf(stderr,
    554 	    "usage: %s [-R [-H | -L | -P]] [-f | -i] [-alNpv] src target\n"
    555 	    "       %s [-R [-H | -L | -P]] [-f | -i] [-alNpv] src1 ... srcN directory\n",
    556 	    getprogname(), getprogname());
    557 	exit(1);
    558 	/* NOTREACHED */
    559 }
    560