120253Sjoerg/*-
2330449Seadler * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3330449Seadler *
420302Sjoerg * Copyright (C) 1996
520302Sjoerg *	David L. Nugent.  All rights reserved.
620253Sjoerg *
720253Sjoerg * Redistribution and use in source and binary forms, with or without
820253Sjoerg * modification, are permitted provided that the following conditions
920253Sjoerg * are met:
1020253Sjoerg * 1. Redistributions of source code must retain the above copyright
1120302Sjoerg *    notice, this list of conditions and the following disclaimer.
1220253Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
1320253Sjoerg *    notice, this list of conditions and the following disclaimer in the
1420253Sjoerg *    documentation and/or other materials provided with the distribution.
1520253Sjoerg *
1620302Sjoerg * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
1720253Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1820253Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1920302Sjoerg * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
2020253Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2120253Sjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2220253Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2320253Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2420253Sjoerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2520253Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2620253Sjoerg * SUCH DAMAGE.
2720253Sjoerg */
2820253Sjoerg
2930259Scharnier#ifndef lint
3030259Scharnierstatic const char rcsid[] =
3150479Speter  "$FreeBSD: stable/11/usr.sbin/pw/cpdir.c 330449 2018-03-05 07:26:05Z eadler $";
3230259Scharnier#endif /* not lint */
3330259Scharnier
34286201Sbapt#include <dirent.h>
3530259Scharnier#include <err.h>
3630259Scharnier#include <errno.h>
3730259Scharnier#include <fcntl.h>
3820253Sjoerg#include <string.h>
3930259Scharnier#include <unistd.h>
4020253Sjoerg
41219408Sjkim#include "pw.h"
4220253Sjoerg
4320253Sjoergvoid
44285430Sbaptcopymkdir(int rootfd, char const * dir, int skelfd, mode_t mode, uid_t uid,
45285430Sbapt    gid_t gid, int flags)
4620253Sjoerg{
47285430Sbapt	char		*p, lnk[MAXPATHLEN], copybuf[4096];
48285430Sbapt	int		len, homefd, srcfd, destfd;
49285430Sbapt	ssize_t		sz;
50285430Sbapt	struct stat     st;
51285430Sbapt	struct dirent  *e;
52285430Sbapt	DIR		*d;
5320253Sjoerg
54285430Sbapt	if (*dir == '/')
55285430Sbapt		dir++;
56285430Sbapt
57285430Sbapt	if (mkdirat(rootfd, dir, mode) != 0 && errno != EEXIST) {
5830259Scharnier		warn("mkdir(%s)", dir);
59285430Sbapt		return;
60285430Sbapt	}
61285430Sbapt	fchownat(rootfd, dir, uid, gid, AT_SYMLINK_NOFOLLOW);
62285430Sbapt	if (flags > 0)
63285430Sbapt		chflagsat(rootfd, dir, flags, AT_SYMLINK_NOFOLLOW);
6420253Sjoerg
65285430Sbapt	if (skelfd == -1)
66285430Sbapt		return;
6720253Sjoerg
68285430Sbapt	homefd = openat(rootfd, dir, O_DIRECTORY);
69285430Sbapt	if ((d = fdopendir(skelfd)) == NULL) {
70285430Sbapt		close(skelfd);
71285430Sbapt		close(homefd);
72285430Sbapt		return;
73285430Sbapt	}
7420253Sjoerg
75285430Sbapt	while ((e = readdir(d)) != NULL) {
76285430Sbapt		if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0)
77285430Sbapt			continue;
7820253Sjoerg
79285430Sbapt		p = e->d_name;
80285430Sbapt		if (fstatat(skelfd, p, &st, AT_SYMLINK_NOFOLLOW) == -1)
81285430Sbapt			continue;
8220253Sjoerg
83285430Sbapt		if (strncmp(p, "dot.", 4) == 0)	/* Conversion */
84285430Sbapt			p += 3;
8520253Sjoerg
86285430Sbapt		if (S_ISDIR(st.st_mode)) {
87285430Sbapt			copymkdir(homefd, p, openat(skelfd, e->d_name, O_DIRECTORY),
88285430Sbapt			    st.st_mode & _DEF_DIRMODE, uid, gid, st.st_flags);
89285430Sbapt			continue;
9020253Sjoerg		}
91285430Sbapt
92285430Sbapt		if (S_ISLNK(st.st_mode) &&
93285430Sbapt		    (len = readlinkat(skelfd, e->d_name, lnk, sizeof(lnk) -1))
94285430Sbapt		    != -1) {
95285430Sbapt			lnk[len] = '\0';
96285430Sbapt			symlinkat(lnk, homefd, p);
97285430Sbapt			fchownat(homefd, p, uid, gid, AT_SYMLINK_NOFOLLOW);
98285430Sbapt			continue;
9920253Sjoerg		}
100285430Sbapt
101285430Sbapt		if (!S_ISREG(st.st_mode))
102285430Sbapt			continue;
103285430Sbapt
104285430Sbapt		if ((srcfd = openat(skelfd, e->d_name, O_RDONLY)) == -1)
105285430Sbapt			continue;
106285430Sbapt		destfd = openat(homefd, p, O_RDWR | O_CREAT | O_EXCL,
107285430Sbapt		    st.st_mode);
108285430Sbapt		if (destfd == -1) {
109285430Sbapt			close(srcfd);
110285430Sbapt			continue;
111285430Sbapt		}
112285430Sbapt
113285430Sbapt		while ((sz = read(srcfd, copybuf, sizeof(copybuf))) > 0)
114285430Sbapt			write(destfd, copybuf, sz);
115285430Sbapt
116285430Sbapt		close(srcfd);
117285430Sbapt		/*
118285430Sbapt		 * Propagate special filesystem flags
119285430Sbapt		 */
120285430Sbapt		fchown(destfd, uid, gid);
121285430Sbapt		fchflags(destfd, st.st_flags);
122285430Sbapt		close(destfd);
12320253Sjoerg	}
124285430Sbapt	closedir(d);
12520253Sjoerg}
126