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