120253Sjoerg/*-
220302Sjoerg * Copyright (C) 1996
320302Sjoerg *	David L. Nugent.  All rights reserved.
420253Sjoerg *
520253Sjoerg * Redistribution and use in source and binary forms, with or without
620253Sjoerg * modification, are permitted provided that the following conditions
720253Sjoerg * are met:
820253Sjoerg * 1. Redistributions of source code must retain the above copyright
920302Sjoerg *    notice, this list of conditions and the following disclaimer.
1020253Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
1120253Sjoerg *    notice, this list of conditions and the following disclaimer in the
1220253Sjoerg *    documentation and/or other materials provided with the distribution.
1320253Sjoerg *
1420302Sjoerg * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
1520253Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1620253Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1720302Sjoerg * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
1820253Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1920253Sjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2020253Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2120253Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2220253Sjoerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2320253Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2420253Sjoerg * SUCH DAMAGE.
2520253Sjoerg */
2620253Sjoerg
2730259Scharnier#ifndef lint
2830259Scharnierstatic const char rcsid[] =
2950479Speter  "$FreeBSD: releng/11.0/usr.sbin/pw/cpdir.c 297290 2016-03-26 11:41:35Z bapt $";
3030259Scharnier#endif /* not lint */
3130259Scharnier
32286201Sbapt#include <dirent.h>
3330259Scharnier#include <err.h>
3430259Scharnier#include <errno.h>
3530259Scharnier#include <fcntl.h>
3620253Sjoerg#include <string.h>
3730259Scharnier#include <unistd.h>
3820253Sjoerg
39219408Sjkim#include "pw.h"
4020253Sjoerg
4120253Sjoergvoid
42285430Sbaptcopymkdir(int rootfd, char const * dir, int skelfd, mode_t mode, uid_t uid,
43285430Sbapt    gid_t gid, int flags)
4420253Sjoerg{
45285430Sbapt	char		*p, lnk[MAXPATHLEN], copybuf[4096];
46285430Sbapt	int		len, homefd, srcfd, destfd;
47285430Sbapt	ssize_t		sz;
48285430Sbapt	struct stat     st;
49285430Sbapt	struct dirent  *e;
50285430Sbapt	DIR		*d;
5120253Sjoerg
52285430Sbapt	if (*dir == '/')
53285430Sbapt		dir++;
54285430Sbapt
55285430Sbapt	if (mkdirat(rootfd, dir, mode) != 0 && errno != EEXIST) {
5630259Scharnier		warn("mkdir(%s)", dir);
57285430Sbapt		return;
58285430Sbapt	}
59285430Sbapt	fchownat(rootfd, dir, uid, gid, AT_SYMLINK_NOFOLLOW);
60285430Sbapt	if (flags > 0)
61285430Sbapt		chflagsat(rootfd, dir, flags, AT_SYMLINK_NOFOLLOW);
6220253Sjoerg
63285430Sbapt	if (skelfd == -1)
64285430Sbapt		return;
6520253Sjoerg
66285430Sbapt	homefd = openat(rootfd, dir, O_DIRECTORY);
67285430Sbapt	if ((d = fdopendir(skelfd)) == NULL) {
68285430Sbapt		close(skelfd);
69285430Sbapt		close(homefd);
70285430Sbapt		return;
71285430Sbapt	}
7220253Sjoerg
73285430Sbapt	while ((e = readdir(d)) != NULL) {
74285430Sbapt		if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0)
75285430Sbapt			continue;
7620253Sjoerg
77285430Sbapt		p = e->d_name;
78285430Sbapt		if (fstatat(skelfd, p, &st, AT_SYMLINK_NOFOLLOW) == -1)
79285430Sbapt			continue;
8020253Sjoerg
81285430Sbapt		if (strncmp(p, "dot.", 4) == 0)	/* Conversion */
82285430Sbapt			p += 3;
8320253Sjoerg
84285430Sbapt		if (S_ISDIR(st.st_mode)) {
85285430Sbapt			copymkdir(homefd, p, openat(skelfd, e->d_name, O_DIRECTORY),
86285430Sbapt			    st.st_mode & _DEF_DIRMODE, uid, gid, st.st_flags);
87285430Sbapt			continue;
8820253Sjoerg		}
89285430Sbapt
90285430Sbapt		if (S_ISLNK(st.st_mode) &&
91285430Sbapt		    (len = readlinkat(skelfd, e->d_name, lnk, sizeof(lnk) -1))
92285430Sbapt		    != -1) {
93285430Sbapt			lnk[len] = '\0';
94285430Sbapt			symlinkat(lnk, homefd, p);
95285430Sbapt			fchownat(homefd, p, uid, gid, AT_SYMLINK_NOFOLLOW);
96285430Sbapt			continue;
9720253Sjoerg		}
98285430Sbapt
99285430Sbapt		if (!S_ISREG(st.st_mode))
100285430Sbapt			continue;
101285430Sbapt
102285430Sbapt		if ((srcfd = openat(skelfd, e->d_name, O_RDONLY)) == -1)
103285430Sbapt			continue;
104285430Sbapt		destfd = openat(homefd, p, O_RDWR | O_CREAT | O_EXCL,
105285430Sbapt		    st.st_mode);
106285430Sbapt		if (destfd == -1) {
107285430Sbapt			close(srcfd);
108285430Sbapt			continue;
109285430Sbapt		}
110285430Sbapt
111285430Sbapt		while ((sz = read(srcfd, copybuf, sizeof(copybuf))) > 0)
112285430Sbapt			write(destfd, copybuf, sz);
113285430Sbapt
114285430Sbapt		close(srcfd);
115285430Sbapt		/*
116285430Sbapt		 * Propagate special filesystem flags
117285430Sbapt		 */
118285430Sbapt		fchown(destfd, uid, gid);
119285430Sbapt		fchflags(destfd, st.st_flags);
120285430Sbapt		close(destfd);
12120253Sjoerg	}
122285430Sbapt	closedir(d);
12320253Sjoerg}
124