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