utils.c revision 1.24 1 /* $NetBSD: utils.c,v 1.24 2003/08/04 22:31:22 jschauma 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 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)utils.c 8.3 (Berkeley) 4/1/94";
40 #else
41 __RCSID("$NetBSD: utils.c,v 1.24 2003/08/04 22:31:22 jschauma Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/mman.h>
46 #include <sys/param.h>
47 #include <sys/stat.h>
48 #include <sys/time.h>
49
50 #include <err.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <fts.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 #include <vis.h>
59
60 #include "extern.h"
61
62 int
63 set_utimes(const char *file, struct stat *fs)
64 {
65 static struct timeval tv[2];
66
67 TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atimespec);
68 TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtimespec);
69
70 if (lutimes(file, tv)) {
71 warn("lutimes: %s", file);
72 return (1);
73 }
74 return (0);
75 }
76
77 int
78 copy_file(FTSENT *entp, int dne)
79 {
80 static char buf[MAXBSIZE];
81 struct stat to_stat, *fs;
82 int ch, checkch, from_fd, rcount, rval, to_fd, wcount;
83 char *p;
84
85 if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
86 warn("%s", entp->fts_path);
87 return (1);
88 }
89
90 fs = entp->fts_statp;
91
92 /*
93 * If the file exists and we're interactive, verify with the user.
94 * If the file DNE, set the mode to be the from file, minus setuid
95 * bits, modified by the umask; arguably wrong, but it makes copying
96 * executables work right and it's been that way forever. (The
97 * other choice is 666 or'ed with the execute bits on the from file
98 * modified by the umask.)
99 */
100 if (!dne) {
101 if (iflag) {
102 (void)fprintf(stderr, "overwrite %s? ", to.p_path);
103 checkch = ch = getchar();
104 while (ch != '\n' && ch != EOF)
105 ch = getchar();
106 if (checkch != 'y' && checkch != 'Y') {
107 (void)close(from_fd);
108 return (0);
109 }
110 }
111 /* overwrite existing destination file name */
112 to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
113 } else
114 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
115 fs->st_mode & ~(S_ISUID | S_ISGID));
116
117 if (to_fd == -1 && fflag) {
118 /*
119 * attempt to remove existing destination file name and
120 * create a new file
121 */
122 (void)unlink(to.p_path);
123 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
124 fs->st_mode & ~(S_ISUID | S_ISGID));
125 }
126
127 if (to_fd == -1) {
128 warn("%s", to.p_path);
129 (void)close(from_fd);
130 return (1);
131 }
132
133 rval = 0;
134
135 /*
136 * There's no reason to do anything other than close the file
137 * now if it's empty, so let's not bother.
138 */
139
140 if (fs->st_size > 0) {
141
142 /*
143 * Mmap and write if less than 8M (the limit is so
144 * we don't totally trash memory on big files).
145 * This is really a minor hack, but it wins some CPU back.
146 */
147
148 if (fs->st_size <= 8 * 1048576) {
149 p = mmap(NULL, (size_t)fs->st_size, PROT_READ,
150 MAP_FILE|MAP_SHARED, from_fd, (off_t)0);
151 if (p == MAP_FAILED) {
152 goto mmap_failed;
153 } else {
154 (void) madvise(p, (size_t)fs->st_size,
155 MADV_SEQUENTIAL);
156 if (write(to_fd, p, fs->st_size) !=
157 fs->st_size) {
158 warn("%s", to.p_path);
159 rval = 1;
160 }
161 if (munmap(p, fs->st_size) < 0) {
162 warn("%s", entp->fts_path);
163 rval = 1;
164 }
165 }
166 } else {
167 mmap_failed:
168 while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
169 wcount = write(to_fd, buf, rcount);
170 if (rcount != wcount || wcount == -1) {
171 warn("%s", to.p_path);
172 rval = 1;
173 break;
174 }
175 }
176 if (rcount < 0) {
177 warn("%s", entp->fts_path);
178 rval = 1;
179 }
180 }
181 }
182
183 if (rval == 1) {
184 (void)close(from_fd);
185 (void)close(to_fd);
186 return (1);
187 }
188
189 if (pflag && setfile(fs, to_fd))
190 rval = 1;
191 /*
192 * If the source was setuid or setgid, lose the bits unless the
193 * copy is owned by the same user and group.
194 */
195 #define RETAINBITS \
196 (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
197 else if (fs->st_mode & (S_ISUID | S_ISGID) && fs->st_uid == myuid) {
198 if (fstat(to_fd, &to_stat)) {
199 warn("%s", to.p_path);
200 rval = 1;
201 } else if (fs->st_gid == to_stat.st_gid &&
202 fchmod(to_fd, fs->st_mode & RETAINBITS & ~myumask)) {
203 warn("%s", to.p_path);
204 rval = 1;
205 }
206 }
207 (void)close(from_fd);
208 if (close(to_fd)) {
209 warn("%s", to.p_path);
210 rval = 1;
211 }
212 /* set the mod/access times now after close of the fd */
213 if (pflag && set_utimes(to.p_path, fs)) {
214 rval = 1;
215 }
216 return (rval);
217 }
218
219 int
220 copy_link(FTSENT *p, int exists)
221 {
222 int len;
223 char target[MAXPATHLEN];
224
225 if ((len = readlink(p->fts_path, target, sizeof(target)-1)) == -1) {
226 warn("readlink: %s", p->fts_path);
227 return (1);
228 }
229 target[len] = '\0';
230 if (exists && unlink(to.p_path)) {
231 warn("unlink: %s", to.p_path);
232 return (1);
233 }
234 if (symlink(target, to.p_path)) {
235 warn("symlink: %s", target);
236 return (1);
237 }
238 return (pflag ? setfile(p->fts_statp, 0) : 0);
239 }
240
241 int
242 copy_fifo(struct stat *from_stat, int exists)
243 {
244 if (exists && unlink(to.p_path)) {
245 warn("unlink: %s", to.p_path);
246 return (1);
247 }
248 if (mkfifo(to.p_path, from_stat->st_mode)) {
249 warn("mkfifo: %s", to.p_path);
250 return (1);
251 }
252 return (pflag ? setfile(from_stat, 0) : 0);
253 }
254
255 int
256 copy_special(struct stat *from_stat, int exists)
257 {
258 if (exists && unlink(to.p_path)) {
259 warn("unlink: %s", to.p_path);
260 return (1);
261 }
262 if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
263 warn("mknod: %s", to.p_path);
264 return (1);
265 }
266 return (pflag ? setfile(from_stat, 0) : 0);
267 }
268
269
270 /*
271 * Function: setfile
272 *
273 * Purpose:
274 * Set the owner/group/permissions for the "to" file to the information
275 * in the stat structure. If fd is zero, also call set_utimes() to set
276 * the mod/access times. If fd is non-zero, the caller must do a utimes
277 * itself after close(fd).
278 */
279 int
280 setfile(struct stat *fs, int fd)
281 {
282 int rval, islink;
283
284 rval = 0;
285 islink = S_ISLNK(fs->st_mode);
286 fs->st_mode &= S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
287
288 /*
289 * Changing the ownership probably won't succeed, unless we're root
290 * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
291 * the mode; current BSD behavior is to remove all setuid bits on
292 * chown. If chown fails, lose setuid/setgid bits.
293 */
294 if (fd ? fchown(fd, fs->st_uid, fs->st_gid) :
295 lchown(to.p_path, fs->st_uid, fs->st_gid)) {
296 if (errno != EPERM) {
297 warn("chown: %s", to.p_path);
298 rval = 1;
299 }
300 fs->st_mode &= ~(S_ISUID | S_ISGID);
301 }
302 if (fd ? fchmod(fd, fs->st_mode) : lchmod(to.p_path, fs->st_mode)) {
303 warn("chmod: %s", to.p_path);
304 rval = 1;
305 }
306
307 if (!islink) {
308 /*
309 * XXX
310 * NFS doesn't support chflags; ignore errors unless
311 * there's reason to believe we're losing bits.
312 * (Note, this still won't be right if the server
313 * supports flags and we were trying to *remove* flags
314 * on a file that we copied, i.e., that we didn't create.)
315 */
316 errno = 0;
317 if (fd ? fchflags(fd, fs->st_flags) :
318 chflags(to.p_path, fs->st_flags))
319 if (errno != EOPNOTSUPP || fs->st_flags != 0) {
320 warn("chflags: %s", to.p_path);
321 rval = 1;
322 }
323 }
324 /* if fd is non-zero, caller must call set_utimes() after close() */
325 if (fd == 0 && set_utimes(to.p_path, fs))
326 rval = 1;
327 return (rval);
328 }
329
330 void
331 usage(void)
332 {
333 (void)fprintf(stderr,
334 "usage: %s [-R [-H | -L | -P]] [-f | -i] [-pv] src target\n"
335 " %s [-R [-H | -L | -P]] [-f | -i] [-pv] src1 ... srcN directory\n",
336 getprogname(), getprogname());
337 exit(1);
338 /* NOTREACHED */
339 }
340
341 char *
342 printescaped(const char *src)
343 {
344 size_t len;
345 char *retval;
346
347 len = strlen(src);
348 if (len != 0 && SIZE_T_MAX/len <= 4) {
349 errx(EXIT_FAILURE, "%s: name too long", src);
350 /* NOTREACHED */
351 }
352
353 retval = (char *)malloc(4*len+1);
354 if (retval != NULL) {
355 if (stdout_ok)
356 (void)strvis(retval, src, VIS_NL | VIS_CSTYLE);
357 else
358 (void)strcpy(retval, src);
359 return retval;
360 } else
361 errx(EXIT_FAILURE, "out of memory!");
362 /* NOTREACHED */
363 }
364