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