xref: /netbsd-src/bin/cp/utils.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*	$NetBSD: utils.c,v 1.4 1995/08/02 07:17:02 jtc 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.4 1995/08/02 07:17:02 jtc 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 (rval == 1) {
151 		(void)close(from_fd);
152 		(void)close(to_fd);
153 		return (1);
154 	}
155 
156 	if (pflag && setfile(fs, to_fd))
157 		rval = 1;
158 	/*
159 	 * If the source was setuid or setgid, lose the bits unless the
160 	 * copy is owned by the same user and group.
161 	 */
162 #define	RETAINBITS \
163 	(S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
164 	else if (fs->st_mode & (S_ISUID | S_ISGID) && fs->st_uid == myuid)
165 		if (fstat(to_fd, &to_stat)) {
166 			warn("%s", to.p_path);
167 			rval = 1;
168 		} else if (fs->st_gid == to_stat.st_gid &&
169 		    fchmod(to_fd, fs->st_mode & RETAINBITS & ~myumask)) {
170 			warn("%s", to.p_path);
171 			rval = 1;
172 		}
173 	(void)close(from_fd);
174 	if (close(to_fd)) {
175 		warn("%s", to.p_path);
176 		rval = 1;
177 	}
178 	return (rval);
179 }
180 
181 int
182 copy_link(p, exists)
183 	FTSENT *p;
184 	int exists;
185 {
186 	int len;
187 	char link[MAXPATHLEN];
188 
189 	if ((len = readlink(p->fts_path, link, sizeof(link))) == -1) {
190 		warn("readlink: %s", p->fts_path);
191 		return (1);
192 	}
193 	link[len] = '\0';
194 	if (exists && unlink(to.p_path)) {
195 		warn("unlink: %s", to.p_path);
196 		return (1);
197 	}
198 	if (symlink(link, to.p_path)) {
199 		warn("symlink: %s", link);
200 		return (1);
201 	}
202 	return (0);
203 }
204 
205 int
206 copy_fifo(from_stat, exists)
207 	struct stat *from_stat;
208 	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(from_stat, exists)
223 	struct stat *from_stat;
224 	int exists;
225 {
226 	if (exists && unlink(to.p_path)) {
227 		warn("unlink: %s", to.p_path);
228 		return (1);
229 	}
230 	if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
231 		warn("mknod: %s", to.p_path);
232 		return (1);
233 	}
234 	return (pflag ? setfile(from_stat, 0) : 0);
235 }
236 
237 
238 int
239 setfile(fs, fd)
240 	register struct stat *fs;
241 	int fd;
242 {
243 	static struct timeval tv[2];
244 	int rval;
245 
246 	rval = 0;
247 	fs->st_mode &= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
248 
249 	TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
250 	TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
251 	if (utimes(to.p_path, tv)) {
252 		warn("utimes: %s", to.p_path);
253 		rval = 1;
254 	}
255 	/*
256 	 * Changing the ownership probably won't succeed, unless we're root
257 	 * or POSIX_CHOWN_RESTRICTED is not set.  Set uid/gid before setting
258 	 * the mode; current BSD behavior is to remove all setuid bits on
259 	 * chown.  If chown fails, lose setuid/setgid bits.
260 	 */
261 	if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
262 	    chown(to.p_path, fs->st_uid, fs->st_gid)) {
263 		if (errno != EPERM) {
264 			warn("chown: %s", to.p_path);
265 			rval = 1;
266 		}
267 		fs->st_mode &= ~(S_ISUID | S_ISGID);
268 	}
269 	if (fd ? fchmod(fd, fs->st_mode) : chmod(to.p_path, fs->st_mode)) {
270 		warn("chown: %s", to.p_path);
271 		rval = 1;
272 	}
273 
274 	if (fd ?
275 	    fchflags(fd, fs->st_flags) : chflags(to.p_path, fs->st_flags)) {
276 		warn("chflags: %s", to.p_path);
277 		rval = 1;
278 	}
279 	return (rval);
280 }
281 
282 void
283 usage()
284 {
285 	(void)fprintf(stderr, "%s\n%s\n",
286 "usage: cp [-R [-H | -L | -P] [-fip] src target",
287 "       cp [-R [-H | -L | -P] [-fip] src1 ... srcN directory");
288 	exit(1);
289 }
290