xref: /freebsd/usr.sbin/pw/cpdir.c (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
1d6f907dcSJoerg Wunsch /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
4ad7cf975SJoerg Wunsch  * Copyright (C) 1996
5ad7cf975SJoerg Wunsch  *	David L. Nugent.  All rights reserved.
6d6f907dcSJoerg Wunsch  *
7d6f907dcSJoerg Wunsch  * Redistribution and use in source and binary forms, with or without
8d6f907dcSJoerg Wunsch  * modification, are permitted provided that the following conditions
9d6f907dcSJoerg Wunsch  * are met:
10d6f907dcSJoerg Wunsch  * 1. Redistributions of source code must retain the above copyright
11ad7cf975SJoerg Wunsch  *    notice, this list of conditions and the following disclaimer.
12d6f907dcSJoerg Wunsch  * 2. Redistributions in binary form must reproduce the above copyright
13d6f907dcSJoerg Wunsch  *    notice, this list of conditions and the following disclaimer in the
14d6f907dcSJoerg Wunsch  *    documentation and/or other materials provided with the distribution.
15d6f907dcSJoerg Wunsch  *
16ad7cf975SJoerg Wunsch  * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
17d6f907dcSJoerg Wunsch  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18d6f907dcSJoerg Wunsch  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19ad7cf975SJoerg Wunsch  * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
20d6f907dcSJoerg Wunsch  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21d6f907dcSJoerg Wunsch  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22d6f907dcSJoerg Wunsch  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23d6f907dcSJoerg Wunsch  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24d6f907dcSJoerg Wunsch  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25d6f907dcSJoerg Wunsch  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26d6f907dcSJoerg Wunsch  * SUCH DAMAGE.
27d6f907dcSJoerg Wunsch  */
28d6f907dcSJoerg Wunsch 
291dcc6ec7SPhilippe Charnier #ifndef lint
301dcc6ec7SPhilippe Charnier static const char rcsid[] =
3197d92980SPeter Wemm   "$FreeBSD$";
321dcc6ec7SPhilippe Charnier #endif /* not lint */
331dcc6ec7SPhilippe Charnier 
34bcbdb01eSBaptiste Daroussin #include <dirent.h>
351dcc6ec7SPhilippe Charnier #include <err.h>
361dcc6ec7SPhilippe Charnier #include <errno.h>
371dcc6ec7SPhilippe Charnier #include <fcntl.h>
38d6f907dcSJoerg Wunsch #include <string.h>
391dcc6ec7SPhilippe Charnier #include <unistd.h>
40d6f907dcSJoerg Wunsch 
41644af48dSJung-uk Kim #include "pw.h"
42d6f907dcSJoerg Wunsch 
43d6f907dcSJoerg Wunsch void
4465730d93SBaptiste Daroussin copymkdir(int rootfd, char const * dir, int skelfd, mode_t mode, uid_t uid,
4565730d93SBaptiste Daroussin     gid_t gid, int flags)
46d6f907dcSJoerg Wunsch {
4765730d93SBaptiste Daroussin 	char		*p, lnk[MAXPATHLEN], copybuf[4096];
4865730d93SBaptiste Daroussin 	int		len, homefd, srcfd, destfd;
4965730d93SBaptiste Daroussin 	ssize_t		sz;
50d6f907dcSJoerg Wunsch 	struct stat     st;
51d6f907dcSJoerg Wunsch 	struct dirent  *e;
5265730d93SBaptiste Daroussin 	DIR		*d;
5365730d93SBaptiste Daroussin 
5465730d93SBaptiste Daroussin 	if (*dir == '/')
5565730d93SBaptiste Daroussin 		dir++;
5665730d93SBaptiste Daroussin 
5765730d93SBaptiste Daroussin 	if (mkdirat(rootfd, dir, mode) != 0 && errno != EEXIST) {
5865730d93SBaptiste Daroussin 		warn("mkdir(%s)", dir);
5965730d93SBaptiste Daroussin 		return;
6065730d93SBaptiste Daroussin 	}
6165730d93SBaptiste Daroussin 	fchownat(rootfd, dir, uid, gid, AT_SYMLINK_NOFOLLOW);
6265730d93SBaptiste Daroussin 	if (flags > 0)
6365730d93SBaptiste Daroussin 		chflagsat(rootfd, dir, flags, AT_SYMLINK_NOFOLLOW);
6465730d93SBaptiste Daroussin 
6565730d93SBaptiste Daroussin 	if (skelfd == -1)
6665730d93SBaptiste Daroussin 		return;
6765730d93SBaptiste Daroussin 
6865730d93SBaptiste Daroussin 	homefd = openat(rootfd, dir, O_DIRECTORY);
6965730d93SBaptiste Daroussin 	if ((d = fdopendir(skelfd)) == NULL) {
7065730d93SBaptiste Daroussin 		close(skelfd);
7165730d93SBaptiste Daroussin 		close(homefd);
7265730d93SBaptiste Daroussin 		return;
7365730d93SBaptiste Daroussin 	}
74d6f907dcSJoerg Wunsch 
75d6f907dcSJoerg Wunsch 	while ((e = readdir(d)) != NULL) {
7665730d93SBaptiste Daroussin 		if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0)
7765730d93SBaptiste Daroussin 			continue;
78d6f907dcSJoerg Wunsch 
7965730d93SBaptiste Daroussin 		p = e->d_name;
8065730d93SBaptiste Daroussin 		if (fstatat(skelfd, p, &st, AT_SYMLINK_NOFOLLOW) == -1)
8165730d93SBaptiste Daroussin 			continue;
8265730d93SBaptiste Daroussin 
83d6f907dcSJoerg Wunsch 		if (strncmp(p, "dot.", 4) == 0)	/* Conversion */
84d6f907dcSJoerg Wunsch 			p += 3;
85d6f907dcSJoerg Wunsch 
8665730d93SBaptiste Daroussin 		if (S_ISDIR(st.st_mode)) {
8765730d93SBaptiste Daroussin 			copymkdir(homefd, p, openat(skelfd, e->d_name, O_DIRECTORY),
8865730d93SBaptiste Daroussin 			    st.st_mode & _DEF_DIRMODE, uid, gid, st.st_flags);
8965730d93SBaptiste Daroussin 			continue;
9065730d93SBaptiste Daroussin 		}
9165730d93SBaptiste Daroussin 
9265730d93SBaptiste Daroussin 		if (S_ISLNK(st.st_mode) &&
9365730d93SBaptiste Daroussin 		    (len = readlinkat(skelfd, e->d_name, lnk, sizeof(lnk) -1))
9465730d93SBaptiste Daroussin 		    != -1) {
9565730d93SBaptiste Daroussin 			lnk[len] = '\0';
9665730d93SBaptiste Daroussin 			symlinkat(lnk, homefd, p);
9765730d93SBaptiste Daroussin 			fchownat(homefd, p, uid, gid, AT_SYMLINK_NOFOLLOW);
9865730d93SBaptiste Daroussin 			continue;
9965730d93SBaptiste Daroussin 		}
10065730d93SBaptiste Daroussin 
10165730d93SBaptiste Daroussin 		if (!S_ISREG(st.st_mode))
10265730d93SBaptiste Daroussin 			continue;
10365730d93SBaptiste Daroussin 
10465730d93SBaptiste Daroussin 		if ((srcfd = openat(skelfd, e->d_name, O_RDONLY)) == -1)
10565730d93SBaptiste Daroussin 			continue;
10665730d93SBaptiste Daroussin 		destfd = openat(homefd, p, O_RDWR | O_CREAT | O_EXCL,
10765730d93SBaptiste Daroussin 		    st.st_mode);
10865730d93SBaptiste Daroussin 		if (destfd == -1) {
10965730d93SBaptiste Daroussin 			close(srcfd);
11065730d93SBaptiste Daroussin 			continue;
11165730d93SBaptiste Daroussin 		}
11265730d93SBaptiste Daroussin 
11365730d93SBaptiste Daroussin 		while ((sz = read(srcfd, copybuf, sizeof(copybuf))) > 0)
11465730d93SBaptiste Daroussin 			write(destfd, copybuf, sz);
11565730d93SBaptiste Daroussin 
11665730d93SBaptiste Daroussin 		close(srcfd);
1171994f5c7SDavid Nugent 		/*
1183df5ecacSUlrich Spörlein 		 * Propagate special filesystem flags
1191994f5c7SDavid Nugent 		 */
12065730d93SBaptiste Daroussin 		fchown(destfd, uid, gid);
12165730d93SBaptiste Daroussin 		fchflags(destfd, st.st_flags);
12265730d93SBaptiste Daroussin 		close(destfd);
1231994f5c7SDavid Nugent 	}
124d6f907dcSJoerg Wunsch 	closedir(d);
125d6f907dcSJoerg Wunsch }
126