xref: /freebsd/bin/cp/utils.c (revision 417ed37975261df51f61d13e179ad04d8f4839c7)
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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	$Id$
34  */
35 
36 #ifndef lint
37 static char sccsid[] = "@(#)utils.c	8.3 (Berkeley) 4/1/94";
38 #endif /* not lint */
39 
40 #include <sys/param.h>
41 #include <sys/stat.h>
42 #include <sys/mman.h>
43 #include <sys/time.h>
44 
45 #include <err.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <fts.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53 
54 #include "extern.h"
55 
56 int
57 copy_file(entp, dne)
58 	FTSENT *entp;
59 	int dne;
60 {
61 	static char buf[MAXBSIZE];
62 	struct stat to_stat, *fs;
63 	int ch, checkch, from_fd, rcount, rval, to_fd, wcount;
64 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
65 	char *p;
66 #endif
67 
68 	if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
69 		warn("%s", entp->fts_path);
70 		return (1);
71 	}
72 
73 	fs = entp->fts_statp;
74 
75 	/*
76 	 * If the file exists and we're interactive, verify with the user.
77 	 * If the file DNE, set the mode to be the from file, minus setuid
78 	 * bits, modified by the umask; arguably wrong, but it makes copying
79 	 * executables work right and it's been that way forever.  (The
80 	 * other choice is 666 or'ed with the execute bits on the from file
81 	 * modified by the umask.)
82 	 */
83 	if (!dne) {
84 		if (iflag) {
85 			(void)fprintf(stderr, "overwrite %s? ", to.p_path);
86 			checkch = ch = getchar();
87 			while (ch != '\n' && ch != EOF)
88 				ch = getchar();
89 			if (checkch != 'y') {
90 				(void)close(from_fd);
91 				return (0);
92 			}
93 		}
94 		to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
95 	} else
96 		to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
97 		    fs->st_mode & ~(S_ISUID | S_ISGID));
98 
99 	if (to_fd == -1) {
100 		warn("%s", to.p_path);
101 		(void)close(from_fd);
102 		return (1);;
103 	}
104 
105 	rval = 0;
106 
107 	/*
108 	 * Mmap and write if less than 8M (the limit is so we don't totally
109 	 * trash memory on big files.  This is really a minor hack, but it
110 	 * wins some CPU back.
111 	 */
112 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
113 	if (fs->st_size <= 8 * 1048576) {
114 		if ((p = mmap(NULL, (size_t)fs->st_size, PROT_READ,
115 		    0, from_fd, (off_t)0)) == (char *)-1) {
116 			warn("%s", entp->fts_path);
117 			rval = 1;
118 		} else {
119 			if (write(to_fd, p, fs->st_size) != fs->st_size) {
120 				warn("%s", to.p_path);
121 				rval = 1;
122 			}
123 			/* Some systems don't unmap on close(2). */
124 			if (munmap(p, fs->st_size) < 0) {
125 				warn("%s", entp->fts_path);
126 				rval = 1;
127 			}
128 		}
129 	} else
130 #endif
131 	{
132 		while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
133 			wcount = write(to_fd, buf, rcount);
134 			if (rcount != wcount || wcount == -1) {
135 				warn("%s", to.p_path);
136 				rval = 1;
137 				break;
138 			}
139 		}
140 		if (rcount < 0) {
141 			warn("%s", entp->fts_path);
142 			rval = 1;
143 		}
144 	}
145 
146 	/* If the copy went bad, lose the file. */
147 	if (rval == 1) {
148 		(void)unlink(to.p_path);
149 		(void)close(from_fd);
150 		(void)close(to_fd);
151 		return (1);
152 	}
153 
154 	if (pflag && setfile(fs, to_fd))
155 		rval = 1;
156 	/*
157 	 * If the source was setuid or setgid, lose the bits unless the
158 	 * copy is owned by the same user and group.
159 	 */
160 #define	RETAINBITS \
161 	(S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
162 	else if (fs->st_mode & (S_ISUID | S_ISGID) && fs->st_uid == myuid)
163 		if (fstat(to_fd, &to_stat)) {
164 			warn("%s", to.p_path);
165 			rval = 1;
166 		} else if (fs->st_gid == to_stat.st_gid &&
167 		    fchmod(to_fd, fs->st_mode & RETAINBITS & ~myumask)) {
168 			warn("%s", to.p_path);
169 			rval = 1;
170 		}
171 	(void)close(from_fd);
172 	if (close(to_fd)) {
173 		warn("%s", to.p_path);
174 		rval = 1;
175 	}
176 	return (rval);
177 }
178 
179 int
180 copy_link(p, exists)
181 	FTSENT *p;
182 	int exists;
183 {
184 	int len;
185 	char link[MAXPATHLEN];
186 
187 	if ((len = readlink(p->fts_path, link, sizeof(link))) == -1) {
188 		warn("readlink: %s", p->fts_path);
189 		return (1);
190 	}
191 	link[len] = '\0';
192 	if (exists && unlink(to.p_path)) {
193 		warn("unlink: %s", to.p_path);
194 		return (1);
195 	}
196 	if (symlink(link, to.p_path)) {
197 		warn("symlink: %s", link);
198 		return (1);
199 	}
200 	return (0);
201 }
202 
203 int
204 copy_fifo(from_stat, exists)
205 	struct stat *from_stat;
206 	int exists;
207 {
208 	if (exists && unlink(to.p_path)) {
209 		warn("unlink: %s", to.p_path);
210 		return (1);
211 	}
212 	if (mkfifo(to.p_path, from_stat->st_mode)) {
213 		warn("mkfifo: %s", to.p_path);
214 		return (1);
215 	}
216 	return (pflag ? setfile(from_stat, 0) : 0);
217 }
218 
219 int
220 copy_special(from_stat, exists)
221 	struct stat *from_stat;
222 	int exists;
223 {
224 	if (exists && unlink(to.p_path)) {
225 		warn("unlink: %s", to.p_path);
226 		return (1);
227 	}
228 	if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
229 		warn("mknod: %s", to.p_path);
230 		return (1);
231 	}
232 	return (pflag ? setfile(from_stat, 0) : 0);
233 }
234 
235 
236 int
237 setfile(fs, fd)
238 	register struct stat *fs;
239 	int fd;
240 {
241 	static struct timeval tv[2];
242 	int rval;
243 
244 	rval = 0;
245 	fs->st_mode &= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
246 
247 	TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
248 	TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
249 	if (utimes(to.p_path, tv)) {
250 		warn("utimes: %s", to.p_path);
251 		rval = 1;
252 	}
253 	/*
254 	 * Changing the ownership probably won't succeed, unless we're root
255 	 * or POSIX_CHOWN_RESTRICTED is not set.  Set uid/gid before setting
256 	 * the mode; current BSD behavior is to remove all setuid bits on
257 	 * chown.  If chown fails, lose setuid/setgid bits.
258 	 */
259 	if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
260 	    chown(to.p_path, fs->st_uid, fs->st_gid)) {
261 		if (errno != EPERM) {
262 			warn("chown: %s", to.p_path);
263 			rval = 1;
264 		}
265 		fs->st_mode &= ~(S_ISUID | S_ISGID);
266 	}
267 	if (fd ? fchmod(fd, fs->st_mode) : chmod(to.p_path, fs->st_mode)) {
268 		warn("chown: %s", to.p_path);
269 		rval = 1;
270 	}
271 
272 	if (fd ?
273 	    fchflags(fd, fs->st_flags) : chflags(to.p_path, fs->st_flags)) {
274 		warn("chflags: %s", to.p_path);
275 		rval = 1;
276 	}
277 	return (rval);
278 }
279 
280 void
281 usage()
282 {
283 	(void)fprintf(stderr, "%s\n%s\n",
284 "usage: cp [-R [-H | -L | -P] [-fip] src target",
285 "       cp [-R [-H | -L | -P] [-fip] src1 ... srcN directory");
286 	exit(1);
287 }
288