xref: /openbsd-src/bin/cp/utils.c (revision 8500990981f885cbe5e6a4958549cacc238b5ae6)
1 /*	$OpenBSD: utils.c,v 1.25 2003/11/24 04:22:44 mickey 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 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)utils.c	8.3 (Berkeley) 4/1/94";
36 #else
37 static char rcsid[] = "$OpenBSD: utils.c,v 1.25 2003/11/24 04:22:44 mickey Exp $";
38 #endif
39 #endif /* not lint */
40 
41 #include <sys/param.h>
42 #include <sys/stat.h>
43 #include <sys/mman.h>
44 #include <sys/time.h>
45 
46 #include <err.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <fts.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 
55 #include "extern.h"
56 
57 int
58 copy_file(FTSENT *entp, int dne)
59 {
60 	static char buf[MAXBSIZE];
61 	struct stat to_stat, *fs;
62 	int ch, checkch, from_fd, rcount, rval, to_fd, wcount;
63 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
64 	char *p;
65 #endif
66 
67 	if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
68 		warn("%s", entp->fts_path);
69 		return (1);
70 	}
71 
72 	fs = entp->fts_statp;
73 
74 	/*
75 	 * In -f (force) mode, we always unlink the destination first
76 	 * if it exists.  Note that -i and -f are mutually exclusive.
77 	 */
78 	if (!dne && fflag)
79 		(void)unlink(to.p_path);
80 
81 	/*
82 	 * If the file exists and we're interactive, verify with the user.
83 	 * If the file DNE, set the mode to be the from file, minus setuid
84 	 * bits, modified by the umask; arguably wrong, but it makes copying
85 	 * executables work right and it's been that way forever.  (The
86 	 * other choice is 666 or'ed with the execute bits on the from file
87 	 * modified by the umask.)
88 	 */
89 	if (!dne && !fflag) {
90 		if (iflag) {
91 			(void)fprintf(stderr, "overwrite %s? ", to.p_path);
92 			checkch = ch = getchar();
93 			while (ch != '\n' && ch != EOF)
94 				ch = getchar();
95 			if (checkch != 'y' && checkch != 'Y') {
96 				(void)close(from_fd);
97 				return (0);
98 			}
99 		}
100 		to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
101 	} else
102 		to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
103 		    fs->st_mode & ~(S_ISTXT | S_ISUID | S_ISGID));
104 
105 	if (to_fd == -1) {
106 		warn("%s", to.p_path);
107 		(void)close(from_fd);
108 		return (1);
109 	}
110 
111 	rval = 0;
112 
113 	/*
114 	 * Mmap and write if less than 8M (the limit is so we don't totally
115 	 * trash memory on big files.  This is really a minor hack, but it
116 	 * wins some CPU back.
117 	 */
118 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
119 	if (fs->st_size <= 8 * 1048576) {
120 		if ((p = mmap(NULL, (size_t)fs->st_size, PROT_READ,
121 		    MAP_FILE|MAP_SHARED, from_fd, (off_t)0)) == MAP_FAILED) {
122 			warn("mmap: %s", entp->fts_path);
123 			rval = 1;
124 		} else {
125 			madvise(p, fs->st_size, MADV_SEQUENTIAL);
126 			if (write(to_fd, p, fs->st_size) != fs->st_size) {
127 				warn("%s", to.p_path);
128 				rval = 1;
129 			}
130 			/* Some systems don't unmap on close(2). */
131 			if (munmap(p, fs->st_size) < 0) {
132 				warn("%s", entp->fts_path);
133 				rval = 1;
134 			}
135 		}
136 	} else
137 #endif
138 	{
139 		while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
140 			wcount = write(to_fd, buf, rcount);
141 			if (rcount != wcount || wcount == -1) {
142 				warn("%s", to.p_path);
143 				rval = 1;
144 				break;
145 			}
146 		}
147 		if (rcount < 0) {
148 			warn("%s", entp->fts_path);
149 			rval = 1;
150 		}
151 	}
152 
153 	if (rval == 1) {
154 		(void)close(from_fd);
155 		(void)close(to_fd);
156 		return (1);
157 	}
158 
159 	if (pflag && setfile(fs, to_fd))
160 		rval = 1;
161 	/*
162 	 * If the source was setuid or setgid, lose the bits unless the
163 	 * copy is owned by the same user and group.
164 	 */
165 #define	RETAINBITS \
166 	(S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
167 	else if (fs->st_mode & (S_ISUID | S_ISGID) && fs->st_uid == myuid) {
168 		if (fstat(to_fd, &to_stat)) {
169 			warn("%s", to.p_path);
170 			rval = 1;
171 		} else if (fs->st_gid == to_stat.st_gid &&
172 		    fchmod(to_fd, fs->st_mode & RETAINBITS & ~myumask)) {
173 			warn("%s", to.p_path);
174 			rval = 1;
175 		}
176 	}
177 	(void)close(from_fd);
178 	if (close(to_fd)) {
179 		warn("%s", to.p_path);
180 		rval = 1;
181 	}
182 	return (rval);
183 }
184 
185 int
186 copy_link(FTSENT *p, int exists)
187 {
188 	int len;
189 	char link[MAXPATHLEN];
190 
191 	if ((len = readlink(p->fts_path, link, sizeof(link)-1)) == -1) {
192 		warn("readlink: %s", p->fts_path);
193 		return (1);
194 	}
195 	link[len] = '\0';
196 	if (exists && unlink(to.p_path)) {
197 		warn("unlink: %s", to.p_path);
198 		return (1);
199 	}
200 	if (symlink(link, to.p_path)) {
201 		warn("symlink: %s", link);
202 		return (1);
203 	}
204 	return (pflag ? setlink(p->fts_statp) : 0);
205 }
206 
207 int
208 copy_fifo(struct stat *from_stat, int exists)
209 {
210 	if (exists && unlink(to.p_path)) {
211 		warn("unlink: %s", to.p_path);
212 		return (1);
213 	}
214 	if (mkfifo(to.p_path, from_stat->st_mode)) {
215 		warn("mkfifo: %s", to.p_path);
216 		return (1);
217 	}
218 	return (pflag ? setfile(from_stat, 0) : 0);
219 }
220 
221 int
222 copy_special(struct stat *from_stat, 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(struct stat *fs, int fd)
238 {
239 	static struct timeval tv[2];
240 	int rval;
241 
242 	rval = 0;
243 	fs->st_mode &= S_ISTXT | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
244 
245 	TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
246 	TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
247 	if (utimes(to.p_path, tv)) {
248 		warn("utimes: %s", to.p_path);
249 		rval = 1;
250 	}
251 	/*
252 	 * Changing the ownership probably won't succeed, unless we're root
253 	 * or POSIX_CHOWN_RESTRICTED is not set.  Set uid/gid before setting
254 	 * the mode; current BSD behavior is to remove all setuid bits on
255 	 * chown.  If chown fails, lose setuid/setgid bits.
256 	 */
257 	if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
258 	    chown(to.p_path, fs->st_uid, fs->st_gid)) {
259 		if (errno != EPERM) {
260 			warn("chown: %s", to.p_path);
261 			rval = 1;
262 		}
263 		fs->st_mode &= ~(S_ISTXT | S_ISUID | S_ISGID);
264 	}
265 	if (fd ? fchmod(fd, fs->st_mode) : chmod(to.p_path, fs->st_mode)) {
266 		warn("chown: %s", to.p_path);
267 		rval = 1;
268 	}
269 
270 	/*
271 	 * XXX
272 	 * NFS doesn't support chflags; ignore errors unless there's reason
273 	 * to believe we're losing bits.  (Note, this still won't be right
274 	 * if the server supports flags and we were trying to *remove* flags
275 	 * on a file that we copied, i.e., that we didn't create.)
276 	 */
277 	errno = 0;
278 	if (fd ? fchflags(fd, fs->st_flags) : chflags(to.p_path, fs->st_flags))
279 		if (errno != EOPNOTSUPP || fs->st_flags != 0) {
280 			warn("chflags: %s", to.p_path);
281 			rval = 1;
282 		}
283 	return (rval);
284 }
285 
286 
287 int
288 setlink(struct stat *fs)
289 {
290 
291 	if (lchown(to.p_path, fs->st_uid, fs->st_gid)) {
292 		if (errno != EPERM) {
293 			warn("lchown: %s", to.p_path);
294 			return (1);
295 		}
296 	}
297 	return (0);
298 }
299 
300 
301 void
302 usage(void)
303 {
304 	(void)fprintf(stderr,
305 	    "usage: %s [-R [-H | -L | -P]] [-fip] src target\n", __progname);
306 	(void)fprintf(stderr,
307 	    "       %s [-R [-H | -L | -P]] [-fip] src1 ... srcN directory\n",
308 	    __progname);
309 	exit(1);
310 }
311