xref: /openbsd-src/bin/cp/utils.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: utils.c,v 1.34 2014/04/24 01:34:35 tedu Exp $	*/
2 /*	$NetBSD: utils.c,v 1.6 1997/02/26 14:40:51 cgd Exp $	*/
3 
4 /*-
5  * Copyright (c) 1991, 1993, 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/stat.h>
35 #include <sys/mman.h>
36 #include <sys/time.h>
37 
38 #include <err.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <fts.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 #include "extern.h"
48 
49 int
50 copy_file(FTSENT *entp, int dne)
51 {
52 	static char *buf;
53 	static char *zeroes;
54 	struct stat to_stat, *fs;
55 	int ch, checkch, from_fd, rcount, rval, to_fd, wcount;
56 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
57 	char *p;
58 #endif
59 
60 	if (!buf) {
61 		buf = malloc(MAXBSIZE);
62 		if (!buf)
63 			err(1, "malloc");
64 	}
65 	if (!zeroes) {
66 		zeroes = calloc(1, MAXBSIZE);
67 		if (!zeroes)
68 			err(1, "calloc");
69 	}
70 
71 	if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
72 		warn("%s", entp->fts_path);
73 		return (1);
74 	}
75 
76 	fs = entp->fts_statp;
77 
78 	/*
79 	 * In -f (force) mode, we always unlink the destination first
80 	 * if it exists.  Note that -i and -f are mutually exclusive.
81 	 */
82 	if (!dne && fflag)
83 		(void)unlink(to.p_path);
84 
85 	/*
86 	 * If the file exists and we're interactive, verify with the user.
87 	 * If the file DNE, set the mode to be the from file, minus setuid
88 	 * bits, modified by the umask; arguably wrong, but it makes copying
89 	 * executables work right and it's been that way forever.  (The
90 	 * other choice is 666 or'ed with the execute bits on the from file
91 	 * modified by the umask.)
92 	 */
93 	if (!dne && !fflag) {
94 		if (iflag) {
95 			(void)fprintf(stderr, "overwrite %s? ", to.p_path);
96 			checkch = ch = getchar();
97 			while (ch != '\n' && ch != EOF)
98 				ch = getchar();
99 			if (checkch != 'y' && checkch != 'Y') {
100 				(void)close(from_fd);
101 				return (0);
102 			}
103 		}
104 		to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
105 	} else
106 		to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
107 		    fs->st_mode & ~(S_ISTXT | S_ISUID | S_ISGID));
108 
109 	if (to_fd == -1) {
110 		warn("%s", to.p_path);
111 		(void)close(from_fd);
112 		return (1);
113 	}
114 
115 	rval = 0;
116 
117 	/*
118 	 * Mmap and write if less than 8M (the limit is so we don't totally
119 	 * trash memory on big files.  This is really a minor hack, but it
120 	 * wins some CPU back.
121 	 */
122 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
123 	/* XXX broken for 0-size mmap */
124 	if (fs->st_size <= 8 * 1048576) {
125 		if ((p = mmap(NULL, (size_t)fs->st_size, PROT_READ,
126 		    MAP_FILE|MAP_SHARED, from_fd, (off_t)0)) == MAP_FAILED) {
127 			warn("mmap: %s", entp->fts_path);
128 			rval = 1;
129 		} else {
130 			madvise(p, fs->st_size, MADV_SEQUENTIAL);
131 			if (write(to_fd, p, fs->st_size) != fs->st_size) {
132 				warn("%s", to.p_path);
133 				rval = 1;
134 			}
135 			/* Some systems don't unmap on close(2). */
136 			if (munmap(p, fs->st_size) < 0) {
137 				warn("%s", entp->fts_path);
138 				rval = 1;
139 			}
140 		}
141 	} else
142 #endif
143 	{
144 		int skipholes = 0;
145 		struct stat tosb;
146 		if (!fstat(to_fd, &tosb) && S_ISREG(tosb.st_mode))
147 			skipholes = 1;
148 		while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
149 			if (skipholes && memcmp(buf, zeroes, rcount) == 0)
150 				wcount = lseek(to_fd, rcount, SEEK_CUR) == -1 ? -1 : rcount;
151 			else
152 				wcount = write(to_fd, buf, rcount);
153 			if (rcount != wcount || wcount == -1) {
154 				warn("%s", to.p_path);
155 				rval = 1;
156 				break;
157 			}
158 		}
159 		if (skipholes && rcount >= 0)
160 			rcount = ftruncate(to_fd, lseek(to_fd, 0, SEEK_CUR));
161 		if (rcount < 0) {
162 			warn("%s", entp->fts_path);
163 			rval = 1;
164 		}
165 	}
166 
167 	if (rval == 1) {
168 		(void)close(from_fd);
169 		(void)close(to_fd);
170 		return (1);
171 	}
172 
173 	if (pflag && setfile(fs, to_fd))
174 		rval = 1;
175 	/*
176 	 * If the source was setuid or setgid, lose the bits unless the
177 	 * copy is owned by the same user and group.
178 	 */
179 #define	RETAINBITS \
180 	(S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
181 	if (!pflag && dne &&
182 	    fs->st_mode & (S_ISUID | S_ISGID) && fs->st_uid == myuid) {
183 		if (fstat(to_fd, &to_stat)) {
184 			warn("%s", to.p_path);
185 			rval = 1;
186 		} else if (fs->st_gid == to_stat.st_gid &&
187 		    fchmod(to_fd, fs->st_mode & RETAINBITS & ~myumask)) {
188 			warn("%s", to.p_path);
189 			rval = 1;
190 		}
191 	}
192 	(void)close(from_fd);
193 	if (close(to_fd)) {
194 		warn("%s", to.p_path);
195 		rval = 1;
196 	}
197 	return (rval);
198 }
199 
200 int
201 copy_link(FTSENT *p, int exists)
202 {
203 	int len;
204 	char link[MAXPATHLEN];
205 
206 	if ((len = readlink(p->fts_path, link, sizeof(link)-1)) == -1) {
207 		warn("readlink: %s", p->fts_path);
208 		return (1);
209 	}
210 	link[len] = '\0';
211 	if (exists && unlink(to.p_path)) {
212 		warn("unlink: %s", to.p_path);
213 		return (1);
214 	}
215 	if (symlink(link, to.p_path)) {
216 		warn("symlink: %s", link);
217 		return (1);
218 	}
219 	return (pflag ? setlink(p->fts_statp) : 0);
220 }
221 
222 int
223 copy_fifo(struct stat *from_stat, int exists)
224 {
225 	if (exists && unlink(to.p_path)) {
226 		warn("unlink: %s", to.p_path);
227 		return (1);
228 	}
229 	if (mkfifo(to.p_path, from_stat->st_mode)) {
230 		warn("mkfifo: %s", to.p_path);
231 		return (1);
232 	}
233 	return (pflag ? setfile(from_stat, 0) : 0);
234 }
235 
236 int
237 copy_special(struct stat *from_stat, int exists)
238 {
239 	if (exists && unlink(to.p_path)) {
240 		warn("unlink: %s", to.p_path);
241 		return (1);
242 	}
243 	if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
244 		warn("mknod: %s", to.p_path);
245 		return (1);
246 	}
247 	return (pflag ? setfile(from_stat, 0) : 0);
248 }
249 
250 
251 int
252 setfile(struct stat *fs, int fd)
253 {
254 	static struct timeval tv[2];
255 	int rval;
256 
257 	rval = 0;
258 	fs->st_mode &= S_ISTXT | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
259 
260 	TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
261 	TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
262 	if (utimes(to.p_path, tv)) {
263 		warn("utimes: %s", to.p_path);
264 		rval = 1;
265 	}
266 	/*
267 	 * Changing the ownership probably won't succeed, unless we're root
268 	 * or POSIX_CHOWN_RESTRICTED is not set.  Set uid/gid before setting
269 	 * the mode; current BSD behavior is to remove all setuid bits on
270 	 * chown.  If chown fails, lose setuid/setgid bits.
271 	 */
272 	if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
273 	    chown(to.p_path, fs->st_uid, fs->st_gid)) {
274 		if (errno != EPERM) {
275 			warn("chown: %s", to.p_path);
276 			rval = 1;
277 		}
278 		fs->st_mode &= ~(S_ISTXT | S_ISUID | S_ISGID);
279 	}
280 	if (fd ? fchmod(fd, fs->st_mode) : chmod(to.p_path, fs->st_mode)) {
281 		warn("chmod: %s", to.p_path);
282 		rval = 1;
283 	}
284 
285 	/*
286 	 * XXX
287 	 * NFS doesn't support chflags; ignore errors unless there's reason
288 	 * to believe we're losing bits.  (Note, this still won't be right
289 	 * if the server supports flags and we were trying to *remove* flags
290 	 * on a file that we copied, i.e., that we didn't create.)
291 	 */
292 	errno = 0;
293 	if (fd ? fchflags(fd, fs->st_flags) : chflags(to.p_path, fs->st_flags))
294 		if (errno != EOPNOTSUPP || fs->st_flags != 0) {
295 			warn("chflags: %s", to.p_path);
296 			rval = 1;
297 		}
298 	return (rval);
299 }
300 
301 
302 int
303 setlink(struct stat *fs)
304 {
305 
306 	if (lchown(to.p_path, fs->st_uid, fs->st_gid)) {
307 		if (errno != EPERM) {
308 			warn("lchown: %s", to.p_path);
309 			return (1);
310 		}
311 	}
312 	return (0);
313 }
314 
315 
316 void
317 usage(void)
318 {
319 	(void)fprintf(stderr,
320 	    "usage: %s [-fip] [-R [-H | -L | -P]] source target\n", __progname);
321 	(void)fprintf(stderr,
322 	    "       %s [-fip] [-R [-H | -L | -P]] source ... directory\n",
323 	    __progname);
324 	exit(1);
325 }
326