1*1a0afcdeSderaadt /* $OpenBSD: utils.c,v 1.50 2021/11/28 19:28:41 deraadt Exp $ */
2755ccfbeStholo /* $NetBSD: utils.c,v 1.6 1997/02/26 14:40:51 cgd Exp $ */
3df930be7Sderaadt
4df930be7Sderaadt /*-
5df930be7Sderaadt * Copyright (c) 1991, 1993, 1994
6df930be7Sderaadt * The Regents of the University of California. All rights reserved.
7df930be7Sderaadt *
8df930be7Sderaadt * Redistribution and use in source and binary forms, with or without
9df930be7Sderaadt * modification, are permitted provided that the following conditions
10df930be7Sderaadt * are met:
11df930be7Sderaadt * 1. Redistributions of source code must retain the above copyright
12df930be7Sderaadt * notice, this list of conditions and the following disclaimer.
13df930be7Sderaadt * 2. Redistributions in binary form must reproduce the above copyright
14df930be7Sderaadt * notice, this list of conditions and the following disclaimer in the
15df930be7Sderaadt * documentation and/or other materials provided with the distribution.
1629295d1cSmillert * 3. Neither the name of the University nor the names of its contributors
17df930be7Sderaadt * may be used to endorse or promote products derived from this software
18df930be7Sderaadt * without specific prior written permission.
19df930be7Sderaadt *
20df930be7Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df930be7Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df930be7Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df930be7Sderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df930be7Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df930be7Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df930be7Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df930be7Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df930be7Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df930be7Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df930be7Sderaadt * SUCH DAMAGE.
31df930be7Sderaadt */
32df930be7Sderaadt
33*1a0afcdeSderaadt #include <sys/types.h>
34df930be7Sderaadt #include <sys/stat.h>
35df930be7Sderaadt #include <sys/mman.h>
36df930be7Sderaadt #include <sys/time.h>
37df930be7Sderaadt
38df930be7Sderaadt #include <err.h>
39df930be7Sderaadt #include <errno.h>
40df930be7Sderaadt #include <fcntl.h>
41df930be7Sderaadt #include <fts.h>
42df930be7Sderaadt #include <stdio.h>
43df930be7Sderaadt #include <stdlib.h>
44df930be7Sderaadt #include <string.h>
45df930be7Sderaadt #include <unistd.h>
46b9fc9a72Sderaadt #include <limits.h>
47df930be7Sderaadt
48df930be7Sderaadt #include "extern.h"
49df930be7Sderaadt
50*1a0afcdeSderaadt #define _MAXBSIZE (64 * 1024)
51*1a0afcdeSderaadt
52c7e2baaeSmartijn int copy_overwrite(void);
53c7e2baaeSmartijn
54df930be7Sderaadt int
copy_file(FTSENT * entp,int exists)55c7e2baaeSmartijn copy_file(FTSENT *entp, int exists)
56df930be7Sderaadt {
57091cac40Stedu static char *buf;
58091cac40Stedu static char *zeroes;
59df930be7Sderaadt struct stat to_stat, *fs;
60c7e2baaeSmartijn int from_fd, rcount, rval, to_fd, wcount;
61*1a0afcdeSderaadt const size_t buflen = _MAXBSIZE;
62df930be7Sderaadt #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
63df930be7Sderaadt char *p;
64df930be7Sderaadt #endif
65df930be7Sderaadt
66091cac40Stedu if (!buf) {
67*1a0afcdeSderaadt buf = malloc(buflen);
68091cac40Stedu if (!buf)
69091cac40Stedu err(1, "malloc");
70091cac40Stedu }
71091cac40Stedu if (!zeroes) {
72*1a0afcdeSderaadt zeroes = calloc(1, buflen);
73091cac40Stedu if (!zeroes)
74dd50e736Stedu err(1, "calloc");
75091cac40Stedu }
76091cac40Stedu
77b7041c07Sderaadt if ((from_fd = open(entp->fts_path, O_RDONLY)) == -1) {
78df930be7Sderaadt warn("%s", entp->fts_path);
79df930be7Sderaadt return (1);
80df930be7Sderaadt }
81df930be7Sderaadt
82df930be7Sderaadt fs = entp->fts_statp;
83df930be7Sderaadt
84df930be7Sderaadt /*
85940e0cc1Smillert * In -f (force) mode, we always unlink the destination first
869e6e4517Sderaadt * if it exists. Note that -i and -f are mutually exclusive.
87940e0cc1Smillert */
88c7e2baaeSmartijn if (exists && fflag)
89940e0cc1Smillert (void)unlink(to.p_path);
90940e0cc1Smillert
91940e0cc1Smillert /*
92df930be7Sderaadt * If the file DNE, set the mode to be the from file, minus setuid
93df930be7Sderaadt * bits, modified by the umask; arguably wrong, but it makes copying
94df930be7Sderaadt * executables work right and it's been that way forever. (The
95df930be7Sderaadt * other choice is 666 or'ed with the execute bits on the from file
96df930be7Sderaadt * modified by the umask.)
97df930be7Sderaadt */
98c7e2baaeSmartijn if (exists && !fflag) {
99c7e2baaeSmartijn if (!copy_overwrite()) {
100df930be7Sderaadt (void)close(from_fd);
101c7e2baaeSmartijn return 2;
102df930be7Sderaadt }
103b7041c07Sderaadt to_fd = open(to.p_path, O_WRONLY | O_TRUNC);
104df930be7Sderaadt } else
105df930be7Sderaadt to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
1063f51fc8fSderaadt fs->st_mode & ~(S_ISTXT | S_ISUID | S_ISGID));
107df930be7Sderaadt
108df930be7Sderaadt if (to_fd == -1) {
109df930be7Sderaadt warn("%s", to.p_path);
110df930be7Sderaadt (void)close(from_fd);
1117b425235Smillert return (1);
112df930be7Sderaadt }
113df930be7Sderaadt
114df930be7Sderaadt rval = 0;
115df930be7Sderaadt
116df930be7Sderaadt /*
117df930be7Sderaadt * Mmap and write if less than 8M (the limit is so we don't totally
118df930be7Sderaadt * trash memory on big files. This is really a minor hack, but it
119df930be7Sderaadt * wins some CPU back.
120df930be7Sderaadt */
121df930be7Sderaadt #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
12296e7c894Sespie /* XXX broken for 0-size mmap */
123df930be7Sderaadt if (fs->st_size <= 8 * 1048576) {
124df930be7Sderaadt if ((p = mmap(NULL, (size_t)fs->st_size, PROT_READ,
1257dbd2c17Sart MAP_FILE|MAP_SHARED, from_fd, (off_t)0)) == MAP_FAILED) {
126939d60eeSkstailey warn("mmap: %s", entp->fts_path);
127df930be7Sderaadt rval = 1;
128df930be7Sderaadt } else {
12927aa2447Smickey madvise(p, fs->st_size, MADV_SEQUENTIAL);
130df930be7Sderaadt if (write(to_fd, p, fs->st_size) != fs->st_size) {
131df930be7Sderaadt warn("%s", to.p_path);
132df930be7Sderaadt rval = 1;
133df930be7Sderaadt }
134df930be7Sderaadt /* Some systems don't unmap on close(2). */
1353aaa63ebSderaadt if (munmap(p, fs->st_size) == -1) {
136df930be7Sderaadt warn("%s", entp->fts_path);
137df930be7Sderaadt rval = 1;
138df930be7Sderaadt }
139df930be7Sderaadt }
140df930be7Sderaadt } else
141df930be7Sderaadt #endif
142df930be7Sderaadt {
143091cac40Stedu int skipholes = 0;
144091cac40Stedu struct stat tosb;
145091cac40Stedu if (!fstat(to_fd, &tosb) && S_ISREG(tosb.st_mode))
146091cac40Stedu skipholes = 1;
147*1a0afcdeSderaadt while ((rcount = read(from_fd, buf, buflen)) > 0) {
148091cac40Stedu if (skipholes && memcmp(buf, zeroes, rcount) == 0)
149091cac40Stedu wcount = lseek(to_fd, rcount, SEEK_CUR) == -1 ? -1 : rcount;
150091cac40Stedu else
151df930be7Sderaadt wcount = write(to_fd, buf, rcount);
152df930be7Sderaadt if (rcount != wcount || wcount == -1) {
153df930be7Sderaadt warn("%s", to.p_path);
154df930be7Sderaadt rval = 1;
155df930be7Sderaadt break;
156df930be7Sderaadt }
157df930be7Sderaadt }
1583aaa63ebSderaadt if (skipholes && rcount != -1)
159e04f6429Smatthew rcount = ftruncate(to_fd, lseek(to_fd, 0, SEEK_CUR));
1603aaa63ebSderaadt if (rcount == -1) {
161df930be7Sderaadt warn("%s", entp->fts_path);
162df930be7Sderaadt rval = 1;
163df930be7Sderaadt }
164df930be7Sderaadt }
165df930be7Sderaadt
166df930be7Sderaadt if (rval == 1) {
167df930be7Sderaadt (void)close(from_fd);
168df930be7Sderaadt (void)close(to_fd);
169df930be7Sderaadt return (1);
170df930be7Sderaadt }
171df930be7Sderaadt
172df930be7Sderaadt if (pflag && setfile(fs, to_fd))
173df930be7Sderaadt rval = 1;
174df930be7Sderaadt /*
175df930be7Sderaadt * If the source was setuid or setgid, lose the bits unless the
176df930be7Sderaadt * copy is owned by the same user and group.
177df930be7Sderaadt */
178df930be7Sderaadt #define RETAINBITS \
179df930be7Sderaadt (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
180c7e2baaeSmartijn if (!pflag && !exists &&
1819ef2f1ffSotto fs->st_mode & (S_ISUID | S_ISGID) && fs->st_uid == myuid) {
182df930be7Sderaadt if (fstat(to_fd, &to_stat)) {
183df930be7Sderaadt warn("%s", to.p_path);
184df930be7Sderaadt rval = 1;
185df930be7Sderaadt } else if (fs->st_gid == to_stat.st_gid &&
186df930be7Sderaadt fchmod(to_fd, fs->st_mode & RETAINBITS & ~myumask)) {
187df930be7Sderaadt warn("%s", to.p_path);
188df930be7Sderaadt rval = 1;
189df930be7Sderaadt }
19084f5d3acSart }
191df930be7Sderaadt (void)close(from_fd);
192df930be7Sderaadt if (close(to_fd)) {
193df930be7Sderaadt warn("%s", to.p_path);
194df930be7Sderaadt rval = 1;
195df930be7Sderaadt }
196df930be7Sderaadt return (rval);
197df930be7Sderaadt }
198df930be7Sderaadt
199df930be7Sderaadt int
copy_link(FTSENT * p,int exists)200ab83b6d6Sderaadt copy_link(FTSENT *p, int exists)
201df930be7Sderaadt {
202df930be7Sderaadt int len;
2033e60fd39Stedu char name[PATH_MAX];
204df930be7Sderaadt
205c7e2baaeSmartijn if (exists && !copy_overwrite())
206c7e2baaeSmartijn return (2);
2073e60fd39Stedu if ((len = readlink(p->fts_path, name, sizeof(name)-1)) == -1) {
208df930be7Sderaadt warn("readlink: %s", p->fts_path);
209df930be7Sderaadt return (1);
210df930be7Sderaadt }
2113e60fd39Stedu name[len] = '\0';
212df930be7Sderaadt if (exists && unlink(to.p_path)) {
213df930be7Sderaadt warn("unlink: %s", to.p_path);
214df930be7Sderaadt return (1);
215df930be7Sderaadt }
2163e60fd39Stedu if (symlink(name, to.p_path)) {
2173e60fd39Stedu warn("symlink: %s", name);
218df930be7Sderaadt return (1);
219df930be7Sderaadt }
220595d7346Sguenther return (pflag ? setfile(p->fts_statp, -1) : 0);
221df930be7Sderaadt }
222df930be7Sderaadt
223df930be7Sderaadt int
copy_fifo(struct stat * from_stat,int exists)224ab83b6d6Sderaadt copy_fifo(struct stat *from_stat, int exists)
225df930be7Sderaadt {
226c7e2baaeSmartijn if (exists && !copy_overwrite())
227c7e2baaeSmartijn return (2);
228df930be7Sderaadt if (exists && unlink(to.p_path)) {
229df930be7Sderaadt warn("unlink: %s", to.p_path);
230df930be7Sderaadt return (1);
231df930be7Sderaadt }
232df930be7Sderaadt if (mkfifo(to.p_path, from_stat->st_mode)) {
233df930be7Sderaadt warn("mkfifo: %s", to.p_path);
234df930be7Sderaadt return (1);
235df930be7Sderaadt }
236af3ab428Sguenther return (pflag ? setfile(from_stat, -1) : 0);
237df930be7Sderaadt }
238df930be7Sderaadt
239df930be7Sderaadt int
copy_special(struct stat * from_stat,int exists)240ab83b6d6Sderaadt copy_special(struct stat *from_stat, int exists)
241df930be7Sderaadt {
242c7e2baaeSmartijn if (exists && !copy_overwrite())
243c7e2baaeSmartijn return (2);
244df930be7Sderaadt if (exists && unlink(to.p_path)) {
245df930be7Sderaadt warn("unlink: %s", to.p_path);
246df930be7Sderaadt return (1);
247df930be7Sderaadt }
248df930be7Sderaadt if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
249df930be7Sderaadt warn("mknod: %s", to.p_path);
250df930be7Sderaadt return (1);
251df930be7Sderaadt }
252af3ab428Sguenther return (pflag ? setfile(from_stat, -1) : 0);
253df930be7Sderaadt }
254df930be7Sderaadt
255c7e2baaeSmartijn /*
256c7e2baaeSmartijn * If the file exists and we're interactive, verify with the user.
257c7e2baaeSmartijn */
258c7e2baaeSmartijn int
copy_overwrite(void)259c7e2baaeSmartijn copy_overwrite(void)
260c7e2baaeSmartijn {
261c7e2baaeSmartijn int ch, checkch;
262c7e2baaeSmartijn
263c7e2baaeSmartijn if (iflag) {
264c7e2baaeSmartijn (void)fprintf(stderr, "overwrite %s? ", to.p_path);
265c7e2baaeSmartijn checkch = ch = getchar();
266c7e2baaeSmartijn while (ch != '\n' && ch != EOF)
267c7e2baaeSmartijn ch = getchar();
268c7e2baaeSmartijn if (checkch != 'y' && checkch != 'Y')
269c7e2baaeSmartijn return (0);
270c7e2baaeSmartijn }
271c7e2baaeSmartijn return 1;
272c7e2baaeSmartijn }
273df930be7Sderaadt
274df930be7Sderaadt int
setfile(struct stat * fs,int fd)275ab83b6d6Sderaadt setfile(struct stat *fs, int fd)
276df930be7Sderaadt {
277af3ab428Sguenther struct timespec ts[2];
278755ccfbeStholo int rval;
279df930be7Sderaadt
280755ccfbeStholo rval = 0;
2813f51fc8fSderaadt fs->st_mode &= S_ISTXT | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO;
282df930be7Sderaadt
283af3ab428Sguenther ts[0] = fs->st_atim;
284af3ab428Sguenther ts[1] = fs->st_mtim;
285af3ab428Sguenther if (fd >= 0 ? futimens(fd, ts) :
286af3ab428Sguenther utimensat(AT_FDCWD, to.p_path, ts, AT_SYMLINK_NOFOLLOW)) {
287af3ab428Sguenther warn("update times: %s", to.p_path);
288df930be7Sderaadt rval = 1;
289df930be7Sderaadt }
290df930be7Sderaadt /*
291df930be7Sderaadt * Changing the ownership probably won't succeed, unless we're root
292df930be7Sderaadt * or POSIX_CHOWN_RESTRICTED is not set. Set uid/gid before setting
293df930be7Sderaadt * the mode; current BSD behavior is to remove all setuid bits on
294df930be7Sderaadt * chown. If chown fails, lose setuid/setgid bits.
295df930be7Sderaadt */
296af3ab428Sguenther if (fd >= 0 ? fchown(fd, fs->st_uid, fs->st_gid) :
297af3ab428Sguenther lchown(to.p_path, fs->st_uid, fs->st_gid)) {
298df930be7Sderaadt if (errno != EPERM) {
299a7f40264Smillert warn("chown: %s", to.p_path);
300df930be7Sderaadt rval = 1;
301df930be7Sderaadt }
3023f51fc8fSderaadt fs->st_mode &= ~(S_ISTXT | S_ISUID | S_ISGID);
303df930be7Sderaadt }
304af3ab428Sguenther if (fd >= 0 ? fchmod(fd, fs->st_mode) :
305af3ab428Sguenther fchmodat(AT_FDCWD, to.p_path, fs->st_mode, AT_SYMLINK_NOFOLLOW)) {
3068b7f176eSsobrado warn("chmod: %s", to.p_path);
307df930be7Sderaadt rval = 1;
308df930be7Sderaadt }
309df930be7Sderaadt
310755ccfbeStholo /*
311755ccfbeStholo * XXX
312755ccfbeStholo * NFS doesn't support chflags; ignore errors unless there's reason
313755ccfbeStholo * to believe we're losing bits. (Note, this still won't be right
314755ccfbeStholo * if the server supports flags and we were trying to *remove* flags
315755ccfbeStholo * on a file that we copied, i.e., that we didn't create.)
316755ccfbeStholo */
317755ccfbeStholo errno = 0;
318af3ab428Sguenther if (fd >= 0 ? fchflags(fd, fs->st_flags) :
319af3ab428Sguenther chflagsat(AT_FDCWD, to.p_path, fs->st_flags, AT_SYMLINK_NOFOLLOW))
320755ccfbeStholo if (errno != EOPNOTSUPP || fs->st_flags != 0) {
321df930be7Sderaadt warn("chflags: %s", to.p_path);
322df930be7Sderaadt rval = 1;
323df930be7Sderaadt }
324df930be7Sderaadt return (rval);
325df930be7Sderaadt }
326df930be7Sderaadt
327ff87e383Smillert
328df930be7Sderaadt void
usage(void)329ab83b6d6Sderaadt usage(void)
330df930be7Sderaadt {
3319517852aSmpech (void)fprintf(stderr,
332d8f59f8eSjca "usage: %s [-afipv] [-R [-H | -L | -P]] source target\n", __progname);
3339517852aSmpech (void)fprintf(stderr,
334d8f59f8eSjca " %s [-afipv] [-R [-H | -L | -P]] source ... directory\n",
3359517852aSmpech __progname);
336df930be7Sderaadt exit(1);
337df930be7Sderaadt }
338