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
29bcbdb01eSBaptiste Daroussin #include <dirent.h>
301dcc6ec7SPhilippe Charnier #include <err.h>
311dcc6ec7SPhilippe Charnier #include <errno.h>
321dcc6ec7SPhilippe Charnier #include <fcntl.h>
33d6f907dcSJoerg Wunsch #include <string.h>
341dcc6ec7SPhilippe Charnier #include <unistd.h>
35d6f907dcSJoerg Wunsch
36644af48dSJung-uk Kim #include "pw.h"
37d6f907dcSJoerg Wunsch
38d6f907dcSJoerg Wunsch void
copymkdir(int rootfd,char const * dir,int skelfd,mode_t mode,uid_t uid,gid_t gid,int flags)3965730d93SBaptiste Daroussin copymkdir(int rootfd, char const * dir, int skelfd, mode_t mode, uid_t uid,
4065730d93SBaptiste Daroussin gid_t gid, int flags)
41d6f907dcSJoerg Wunsch {
4265730d93SBaptiste Daroussin char *p, lnk[MAXPATHLEN], copybuf[4096];
4365730d93SBaptiste Daroussin int len, homefd, srcfd, destfd;
4465730d93SBaptiste Daroussin ssize_t sz;
45d6f907dcSJoerg Wunsch struct stat st;
46d6f907dcSJoerg Wunsch struct dirent *e;
4765730d93SBaptiste Daroussin DIR *d;
4865730d93SBaptiste Daroussin
4965730d93SBaptiste Daroussin if (*dir == '/')
5065730d93SBaptiste Daroussin dir++;
5165730d93SBaptiste Daroussin
5265730d93SBaptiste Daroussin if (mkdirat(rootfd, dir, mode) != 0 && errno != EEXIST) {
5365730d93SBaptiste Daroussin warn("mkdir(%s)", dir);
5465730d93SBaptiste Daroussin return;
5565730d93SBaptiste Daroussin }
5665730d93SBaptiste Daroussin fchownat(rootfd, dir, uid, gid, AT_SYMLINK_NOFOLLOW);
5765730d93SBaptiste Daroussin if (flags > 0)
5865730d93SBaptiste Daroussin chflagsat(rootfd, dir, flags, AT_SYMLINK_NOFOLLOW);
5965730d93SBaptiste Daroussin
6065730d93SBaptiste Daroussin if (skelfd == -1)
6165730d93SBaptiste Daroussin return;
6265730d93SBaptiste Daroussin
6365730d93SBaptiste Daroussin homefd = openat(rootfd, dir, O_DIRECTORY);
6465730d93SBaptiste Daroussin if ((d = fdopendir(skelfd)) == NULL) {
6565730d93SBaptiste Daroussin close(skelfd);
6665730d93SBaptiste Daroussin close(homefd);
6765730d93SBaptiste Daroussin return;
6865730d93SBaptiste Daroussin }
69d6f907dcSJoerg Wunsch
70d6f907dcSJoerg Wunsch while ((e = readdir(d)) != NULL) {
7165730d93SBaptiste Daroussin if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0)
7265730d93SBaptiste Daroussin continue;
73d6f907dcSJoerg Wunsch
7465730d93SBaptiste Daroussin p = e->d_name;
7565730d93SBaptiste Daroussin if (fstatat(skelfd, p, &st, AT_SYMLINK_NOFOLLOW) == -1)
7665730d93SBaptiste Daroussin continue;
7765730d93SBaptiste Daroussin
78d6f907dcSJoerg Wunsch if (strncmp(p, "dot.", 4) == 0) /* Conversion */
79d6f907dcSJoerg Wunsch p += 3;
80d6f907dcSJoerg Wunsch
8165730d93SBaptiste Daroussin if (S_ISDIR(st.st_mode)) {
8265730d93SBaptiste Daroussin copymkdir(homefd, p, openat(skelfd, e->d_name, O_DIRECTORY),
8365730d93SBaptiste Daroussin st.st_mode & _DEF_DIRMODE, uid, gid, st.st_flags);
8465730d93SBaptiste Daroussin continue;
8565730d93SBaptiste Daroussin }
8665730d93SBaptiste Daroussin
8765730d93SBaptiste Daroussin if (S_ISLNK(st.st_mode) &&
8865730d93SBaptiste Daroussin (len = readlinkat(skelfd, e->d_name, lnk, sizeof(lnk) -1))
8965730d93SBaptiste Daroussin != -1) {
9065730d93SBaptiste Daroussin lnk[len] = '\0';
9165730d93SBaptiste Daroussin symlinkat(lnk, homefd, p);
9265730d93SBaptiste Daroussin fchownat(homefd, p, uid, gid, AT_SYMLINK_NOFOLLOW);
9365730d93SBaptiste Daroussin continue;
9465730d93SBaptiste Daroussin }
9565730d93SBaptiste Daroussin
9665730d93SBaptiste Daroussin if (!S_ISREG(st.st_mode))
9765730d93SBaptiste Daroussin continue;
9865730d93SBaptiste Daroussin
9965730d93SBaptiste Daroussin if ((srcfd = openat(skelfd, e->d_name, O_RDONLY)) == -1)
10065730d93SBaptiste Daroussin continue;
10165730d93SBaptiste Daroussin destfd = openat(homefd, p, O_RDWR | O_CREAT | O_EXCL,
10265730d93SBaptiste Daroussin st.st_mode);
10365730d93SBaptiste Daroussin if (destfd == -1) {
10465730d93SBaptiste Daroussin close(srcfd);
10565730d93SBaptiste Daroussin continue;
10665730d93SBaptiste Daroussin }
10765730d93SBaptiste Daroussin
10865730d93SBaptiste Daroussin while ((sz = read(srcfd, copybuf, sizeof(copybuf))) > 0)
10965730d93SBaptiste Daroussin write(destfd, copybuf, sz);
11065730d93SBaptiste Daroussin
11165730d93SBaptiste Daroussin close(srcfd);
1121994f5c7SDavid Nugent /*
1133df5ecacSUlrich Spörlein * Propagate special filesystem flags
1141994f5c7SDavid Nugent */
11565730d93SBaptiste Daroussin fchown(destfd, uid, gid);
11665730d93SBaptiste Daroussin fchflags(destfd, st.st_flags);
11765730d93SBaptiste Daroussin close(destfd);
1181994f5c7SDavid Nugent }
119d6f907dcSJoerg Wunsch closedir(d);
120d6f907dcSJoerg Wunsch }
121