xref: /freebsd/bin/cp/utils.c (revision 180f822596ecc49d3074dcc9dfea9628aae1d48d)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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 #ifndef lint
33 #if 0
34 static char sccsid[] = "@(#)utils.c	8.3 (Berkeley) 4/1/94";
35 #endif
36 #endif /* not lint */
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include <sys/types.h>
41 #include <sys/acl.h>
42 #include <sys/param.h>
43 #include <sys/stat.h>
44 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
45 #include <sys/mman.h>
46 #endif
47 
48 #include <err.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <fts.h>
52 #include <limits.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <sysexits.h>
56 #include <unistd.h>
57 
58 #include "extern.h"
59 
60 #define	cp_pct(x, y)	((y == 0) ? 0 : (int)(100.0 * (x) / (y)))
61 
62 /*
63  * Memory strategy threshold, in pages: if physmem is larger then this, use a
64  * large buffer.
65  */
66 #define PHYSPAGES_THRESHOLD (32*1024)
67 
68 /* Maximum buffer size in bytes - do not allow it to grow larger than this. */
69 #define BUFSIZE_MAX (2*1024*1024)
70 
71 /*
72  * Small (default) buffer size in bytes. It's inefficient for this to be
73  * smaller than MAXPHYS.
74  */
75 #define BUFSIZE_SMALL (MAXPHYS)
76 
77 static int
78 copy_fallback(int from_fd, int to_fd, char *buf, size_t bufsize)
79 {
80 	int rcount;
81 	ssize_t wresid, wcount = 0;
82 	char *bufp;
83 
84 	rcount = read(from_fd, buf, bufsize);
85 	if (rcount <= 0)
86 		return (rcount);
87 	for (bufp = buf, wresid = rcount; ; bufp += wcount, wresid -= wcount) {
88 		wcount = write(to_fd, bufp, wresid);
89 		if (wcount <= 0)
90 			break;
91 		if (wcount >= (ssize_t)wresid)
92 			break;
93 	}
94 	return (wcount < 0 ? wcount : rcount);
95 }
96 
97 int
98 copy_file(const FTSENT *entp, int dne)
99 {
100 	static char *buf = NULL;
101 	static size_t bufsize;
102 	struct stat *fs;
103 	ssize_t wcount;
104 	size_t wresid;
105 	off_t wtotal;
106 	int ch, checkch, from_fd, rcount, rval, to_fd;
107 	char *bufp;
108 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
109 	char *p;
110 #endif
111 	int use_copy_file_range = 1;
112 
113 	from_fd = to_fd = -1;
114 	if (!lflag && !sflag &&
115 	    (from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
116 		warn("%s", entp->fts_path);
117 		return (1);
118 	}
119 
120 	fs = entp->fts_statp;
121 
122 	/*
123 	 * If the file exists and we're interactive, verify with the user.
124 	 * If the file DNE, set the mode to be the from file, minus setuid
125 	 * bits, modified by the umask; arguably wrong, but it makes copying
126 	 * executables work right and it's been that way forever.  (The
127 	 * other choice is 666 or'ed with the execute bits on the from file
128 	 * modified by the umask.)
129 	 */
130 	if (!dne) {
131 #define YESNO "(y/n [n]) "
132 		if (nflag) {
133 			if (vflag)
134 				printf("%s not overwritten\n", to.p_path);
135 			rval = 1;
136 			goto done;
137 		} else if (iflag) {
138 			(void)fprintf(stderr, "overwrite %s? %s",
139 			    to.p_path, YESNO);
140 			checkch = ch = getchar();
141 			while (ch != '\n' && ch != EOF)
142 				ch = getchar();
143 			if (checkch != 'y' && checkch != 'Y') {
144 				(void)fprintf(stderr, "not overwritten\n");
145 				rval = 1;
146 				goto done;
147 			}
148 		}
149 
150 		if (fflag) {
151 			/*
152 			 * Remove existing destination file name create a new
153 			 * file.
154 			 */
155 			(void)unlink(to.p_path);
156 			if (!lflag && !sflag) {
157 				to_fd = open(to.p_path,
158 				    O_WRONLY | O_TRUNC | O_CREAT,
159 				    fs->st_mode & ~(S_ISUID | S_ISGID));
160 			}
161 		} else if (!lflag && !sflag) {
162 			/* Overwrite existing destination file name. */
163 			to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
164 		}
165 	} else if (!lflag && !sflag) {
166 		to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
167 		    fs->st_mode & ~(S_ISUID | S_ISGID));
168 	}
169 
170 	if (!lflag && !sflag && to_fd == -1) {
171 		warn("%s", to.p_path);
172 		rval = 1;
173 		goto done;
174 	}
175 
176 	rval = 0;
177 
178 	if (!lflag && !sflag) {
179 		/*
180 		 * Mmap and write if less than 8M (the limit is so we don't
181 		 * totally trash memory on big files.  This is really a minor
182 		 * hack, but it wins some CPU back.
183 		 * Some filesystems, such as smbnetfs, don't support mmap,
184 		 * so this is a best-effort attempt.
185 		 */
186 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
187 		if (S_ISREG(fs->st_mode) && fs->st_size > 0 &&
188 		    fs->st_size <= 8 * 1024 * 1024 &&
189 		    (p = mmap(NULL, (size_t)fs->st_size, PROT_READ,
190 		    MAP_SHARED, from_fd, (off_t)0)) != MAP_FAILED) {
191 			wtotal = 0;
192 			for (bufp = p, wresid = fs->st_size; ;
193 			    bufp += wcount, wresid -= (size_t)wcount) {
194 				wcount = write(to_fd, bufp, wresid);
195 				if (wcount <= 0)
196 					break;
197 				wtotal += wcount;
198 				if (info) {
199 					info = 0;
200 					(void)fprintf(stderr,
201 					    "%s -> %s %3d%%\n",
202 					    entp->fts_path, to.p_path,
203 					    cp_pct(wtotal, fs->st_size));
204 				}
205 				if (wcount >= (ssize_t)wresid)
206 					break;
207 			}
208 			if (wcount != (ssize_t)wresid) {
209 				warn("%s", to.p_path);
210 				rval = 1;
211 			}
212 			/* Some systems don't unmap on close(2). */
213 			if (munmap(p, fs->st_size) < 0) {
214 				warn("%s", entp->fts_path);
215 				rval = 1;
216 			}
217 		} else
218 #endif
219 		{
220 			if (buf == NULL) {
221 				/*
222 				 * Note that buf and bufsize are static. If
223 				 * malloc() fails, it will fail at the start
224 				 * and not copy only some files.
225 				 */
226 				if (sysconf(_SC_PHYS_PAGES) >
227 				    PHYSPAGES_THRESHOLD)
228 					bufsize = MIN(BUFSIZE_MAX, MAXPHYS * 8);
229 				else
230 					bufsize = BUFSIZE_SMALL;
231 				buf = malloc(bufsize);
232 				if (buf == NULL)
233 					err(1, "Not enough memory");
234 			}
235 			wtotal = 0;
236 			do {
237 				if (use_copy_file_range) {
238 					rcount = copy_file_range(from_fd, NULL,
239 			    		    to_fd, NULL, bufsize, 0);
240 					if (rcount < 0 && errno == EINVAL) {
241 						/* Prob a non-seekable FD */
242 						use_copy_file_range = 0;
243 					}
244 				}
245 				if (!use_copy_file_range) {
246 					rcount = copy_fallback(from_fd, to_fd,
247 					    buf, bufsize);
248 				}
249 				wtotal += rcount;
250 				if (info) {
251 					info = 0;
252 					(void)fprintf(stderr,
253 					    "%s -> %s %3d%%\n",
254 					    entp->fts_path, to.p_path,
255 					    cp_pct(wtotal, fs->st_size));
256 				}
257 			} while (rcount > 0);
258 			if (rcount < 0) {
259 				warn("%s", entp->fts_path);
260 				rval = 1;
261 			}
262 		}
263 	} else if (lflag) {
264 		if (link(entp->fts_path, to.p_path)) {
265 			warn("%s", to.p_path);
266 			rval = 1;
267 		}
268 	} else if (sflag) {
269 		if (symlink(entp->fts_path, to.p_path)) {
270 			warn("%s", to.p_path);
271 			rval = 1;
272 		}
273 	}
274 
275 	/*
276 	 * Don't remove the target even after an error.  The target might
277 	 * not be a regular file, or its attributes might be important,
278 	 * or its contents might be irreplaceable.  It would only be safe
279 	 * to remove it if we created it and its length is 0.
280 	 */
281 
282 	if (!lflag && !sflag) {
283 		if (pflag && setfile(fs, to_fd))
284 			rval = 1;
285 		if (pflag && preserve_fd_acls(from_fd, to_fd) != 0)
286 			rval = 1;
287 		if (close(to_fd)) {
288 			warn("%s", to.p_path);
289 			rval = 1;
290 		}
291 	}
292 
293 done:
294 	if (from_fd != -1)
295 		(void)close(from_fd);
296 	return (rval);
297 }
298 
299 int
300 copy_link(const FTSENT *p, int exists)
301 {
302 	int len;
303 	char llink[PATH_MAX];
304 
305 	if (exists && nflag) {
306 		if (vflag)
307 			printf("%s not overwritten\n", to.p_path);
308 		return (1);
309 	}
310 	if ((len = readlink(p->fts_path, llink, sizeof(llink) - 1)) == -1) {
311 		warn("readlink: %s", p->fts_path);
312 		return (1);
313 	}
314 	llink[len] = '\0';
315 	if (exists && unlink(to.p_path)) {
316 		warn("unlink: %s", to.p_path);
317 		return (1);
318 	}
319 	if (symlink(llink, to.p_path)) {
320 		warn("symlink: %s", llink);
321 		return (1);
322 	}
323 	return (pflag ? setfile(p->fts_statp, -1) : 0);
324 }
325 
326 int
327 copy_fifo(struct stat *from_stat, int exists)
328 {
329 
330 	if (exists && nflag) {
331 		if (vflag)
332 			printf("%s not overwritten\n", to.p_path);
333 		return (1);
334 	}
335 	if (exists && unlink(to.p_path)) {
336 		warn("unlink: %s", to.p_path);
337 		return (1);
338 	}
339 	if (mkfifo(to.p_path, from_stat->st_mode)) {
340 		warn("mkfifo: %s", to.p_path);
341 		return (1);
342 	}
343 	return (pflag ? setfile(from_stat, -1) : 0);
344 }
345 
346 int
347 copy_special(struct stat *from_stat, int exists)
348 {
349 
350 	if (exists && nflag) {
351 		if (vflag)
352 			printf("%s not overwritten\n", to.p_path);
353 		return (1);
354 	}
355 	if (exists && unlink(to.p_path)) {
356 		warn("unlink: %s", to.p_path);
357 		return (1);
358 	}
359 	if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
360 		warn("mknod: %s", to.p_path);
361 		return (1);
362 	}
363 	return (pflag ? setfile(from_stat, -1) : 0);
364 }
365 
366 int
367 setfile(struct stat *fs, int fd)
368 {
369 	static struct timespec tspec[2];
370 	struct stat ts;
371 	int rval, gotstat, islink, fdval;
372 
373 	rval = 0;
374 	fdval = fd != -1;
375 	islink = !fdval && S_ISLNK(fs->st_mode);
376 	fs->st_mode &= S_ISUID | S_ISGID | S_ISVTX |
377 	    S_IRWXU | S_IRWXG | S_IRWXO;
378 
379 	tspec[0] = fs->st_atim;
380 	tspec[1] = fs->st_mtim;
381 	if (fdval ? futimens(fd, tspec) : utimensat(AT_FDCWD, to.p_path, tspec,
382 	    islink ? AT_SYMLINK_NOFOLLOW : 0)) {
383 		warn("utimensat: %s", to.p_path);
384 		rval = 1;
385 	}
386 	if (fdval ? fstat(fd, &ts) :
387 	    (islink ? lstat(to.p_path, &ts) : stat(to.p_path, &ts)))
388 		gotstat = 0;
389 	else {
390 		gotstat = 1;
391 		ts.st_mode &= S_ISUID | S_ISGID | S_ISVTX |
392 		    S_IRWXU | S_IRWXG | S_IRWXO;
393 	}
394 	/*
395 	 * Changing the ownership probably won't succeed, unless we're root
396 	 * or POSIX_CHOWN_RESTRICTED is not set.  Set uid/gid before setting
397 	 * the mode; current BSD behavior is to remove all setuid bits on
398 	 * chown.  If chown fails, lose setuid/setgid bits.
399 	 */
400 	if (!gotstat || fs->st_uid != ts.st_uid || fs->st_gid != ts.st_gid)
401 		if (fdval ? fchown(fd, fs->st_uid, fs->st_gid) :
402 		    (islink ? lchown(to.p_path, fs->st_uid, fs->st_gid) :
403 		    chown(to.p_path, fs->st_uid, fs->st_gid))) {
404 			if (errno != EPERM) {
405 				warn("chown: %s", to.p_path);
406 				rval = 1;
407 			}
408 			fs->st_mode &= ~(S_ISUID | S_ISGID);
409 		}
410 
411 	if (!gotstat || fs->st_mode != ts.st_mode)
412 		if (fdval ? fchmod(fd, fs->st_mode) :
413 		    (islink ? lchmod(to.p_path, fs->st_mode) :
414 		    chmod(to.p_path, fs->st_mode))) {
415 			warn("chmod: %s", to.p_path);
416 			rval = 1;
417 		}
418 
419 	if (!gotstat || fs->st_flags != ts.st_flags)
420 		if (fdval ?
421 		    fchflags(fd, fs->st_flags) :
422 		    (islink ? lchflags(to.p_path, fs->st_flags) :
423 		    chflags(to.p_path, fs->st_flags))) {
424 			warn("chflags: %s", to.p_path);
425 			rval = 1;
426 		}
427 
428 	return (rval);
429 }
430 
431 int
432 preserve_fd_acls(int source_fd, int dest_fd)
433 {
434 	acl_t acl;
435 	acl_type_t acl_type;
436 	int acl_supported = 0, ret, trivial;
437 
438 	ret = fpathconf(source_fd, _PC_ACL_NFS4);
439 	if (ret > 0 ) {
440 		acl_supported = 1;
441 		acl_type = ACL_TYPE_NFS4;
442 	} else if (ret < 0 && errno != EINVAL) {
443 		warn("fpathconf(..., _PC_ACL_NFS4) failed for %s", to.p_path);
444 		return (1);
445 	}
446 	if (acl_supported == 0) {
447 		ret = fpathconf(source_fd, _PC_ACL_EXTENDED);
448 		if (ret > 0 ) {
449 			acl_supported = 1;
450 			acl_type = ACL_TYPE_ACCESS;
451 		} else if (ret < 0 && errno != EINVAL) {
452 			warn("fpathconf(..., _PC_ACL_EXTENDED) failed for %s",
453 			    to.p_path);
454 			return (1);
455 		}
456 	}
457 	if (acl_supported == 0)
458 		return (0);
459 
460 	acl = acl_get_fd_np(source_fd, acl_type);
461 	if (acl == NULL) {
462 		warn("failed to get acl entries while setting %s", to.p_path);
463 		return (1);
464 	}
465 	if (acl_is_trivial_np(acl, &trivial)) {
466 		warn("acl_is_trivial() failed for %s", to.p_path);
467 		acl_free(acl);
468 		return (1);
469 	}
470 	if (trivial) {
471 		acl_free(acl);
472 		return (0);
473 	}
474 	if (acl_set_fd_np(dest_fd, acl, acl_type) < 0) {
475 		warn("failed to set acl entries for %s", to.p_path);
476 		acl_free(acl);
477 		return (1);
478 	}
479 	acl_free(acl);
480 	return (0);
481 }
482 
483 int
484 preserve_dir_acls(struct stat *fs, char *source_dir, char *dest_dir)
485 {
486 	acl_t (*aclgetf)(const char *, acl_type_t);
487 	int (*aclsetf)(const char *, acl_type_t, acl_t);
488 	struct acl *aclp;
489 	acl_t acl;
490 	acl_type_t acl_type;
491 	int acl_supported = 0, ret, trivial;
492 
493 	ret = pathconf(source_dir, _PC_ACL_NFS4);
494 	if (ret > 0) {
495 		acl_supported = 1;
496 		acl_type = ACL_TYPE_NFS4;
497 	} else if (ret < 0 && errno != EINVAL) {
498 		warn("fpathconf(..., _PC_ACL_NFS4) failed for %s", source_dir);
499 		return (1);
500 	}
501 	if (acl_supported == 0) {
502 		ret = pathconf(source_dir, _PC_ACL_EXTENDED);
503 		if (ret > 0) {
504 			acl_supported = 1;
505 			acl_type = ACL_TYPE_ACCESS;
506 		} else if (ret < 0 && errno != EINVAL) {
507 			warn("fpathconf(..., _PC_ACL_EXTENDED) failed for %s",
508 			    source_dir);
509 			return (1);
510 		}
511 	}
512 	if (acl_supported == 0)
513 		return (0);
514 
515 	/*
516 	 * If the file is a link we will not follow it.
517 	 */
518 	if (S_ISLNK(fs->st_mode)) {
519 		aclgetf = acl_get_link_np;
520 		aclsetf = acl_set_link_np;
521 	} else {
522 		aclgetf = acl_get_file;
523 		aclsetf = acl_set_file;
524 	}
525 	if (acl_type == ACL_TYPE_ACCESS) {
526 		/*
527 		 * Even if there is no ACL_TYPE_DEFAULT entry here, a zero
528 		 * size ACL will be returned. So it is not safe to simply
529 		 * check the pointer to see if the default ACL is present.
530 		 */
531 		acl = aclgetf(source_dir, ACL_TYPE_DEFAULT);
532 		if (acl == NULL) {
533 			warn("failed to get default acl entries on %s",
534 			    source_dir);
535 			return (1);
536 		}
537 		aclp = &acl->ats_acl;
538 		if (aclp->acl_cnt != 0 && aclsetf(dest_dir,
539 		    ACL_TYPE_DEFAULT, acl) < 0) {
540 			warn("failed to set default acl entries on %s",
541 			    dest_dir);
542 			acl_free(acl);
543 			return (1);
544 		}
545 		acl_free(acl);
546 	}
547 	acl = aclgetf(source_dir, acl_type);
548 	if (acl == NULL) {
549 		warn("failed to get acl entries on %s", source_dir);
550 		return (1);
551 	}
552 	if (acl_is_trivial_np(acl, &trivial)) {
553 		warn("acl_is_trivial() failed on %s", source_dir);
554 		acl_free(acl);
555 		return (1);
556 	}
557 	if (trivial) {
558 		acl_free(acl);
559 		return (0);
560 	}
561 	if (aclsetf(dest_dir, acl_type, acl) < 0) {
562 		warn("failed to set acl entries on %s", dest_dir);
563 		acl_free(acl);
564 		return (1);
565 	}
566 	acl_free(acl);
567 	return (0);
568 }
569 
570 void
571 usage(void)
572 {
573 
574 	(void)fprintf(stderr, "%s\n%s\n",
575 	    "usage: cp [-R [-H | -L | -P]] [-f | -i | -n] [-alpsvx] "
576 	    "source_file target_file",
577 	    "       cp [-R [-H | -L | -P]] [-f | -i | -n] [-alpsvx] "
578 	    "source_file ... "
579 	    "target_directory");
580 	exit(EX_USAGE);
581 }
582