1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (C) 1996 5 * David L. Nugent. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #ifndef lint 30 static const char rcsid[] = 31 "$FreeBSD$"; 32 #endif /* not lint */ 33 34 #include <dirent.h> 35 #include <err.h> 36 #include <errno.h> 37 #include <fcntl.h> 38 #include <string.h> 39 #include <unistd.h> 40 41 #include "pw.h" 42 43 void 44 copymkdir(int rootfd, char const * dir, int skelfd, mode_t mode, uid_t uid, 45 gid_t gid, int flags) 46 { 47 char *p, lnk[MAXPATHLEN], copybuf[4096]; 48 int len, homefd, srcfd, destfd; 49 ssize_t sz; 50 struct stat st; 51 struct dirent *e; 52 DIR *d; 53 54 if (*dir == '/') 55 dir++; 56 57 if (mkdirat(rootfd, dir, mode) != 0 && errno != EEXIST) { 58 warn("mkdir(%s)", dir); 59 return; 60 } 61 fchownat(rootfd, dir, uid, gid, AT_SYMLINK_NOFOLLOW); 62 if (flags > 0) 63 chflagsat(rootfd, dir, flags, AT_SYMLINK_NOFOLLOW); 64 65 if (skelfd == -1) 66 return; 67 68 homefd = openat(rootfd, dir, O_DIRECTORY); 69 if ((d = fdopendir(skelfd)) == NULL) { 70 close(skelfd); 71 close(homefd); 72 return; 73 } 74 75 while ((e = readdir(d)) != NULL) { 76 if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0) 77 continue; 78 79 p = e->d_name; 80 if (fstatat(skelfd, p, &st, AT_SYMLINK_NOFOLLOW) == -1) 81 continue; 82 83 if (strncmp(p, "dot.", 4) == 0) /* Conversion */ 84 p += 3; 85 86 if (S_ISDIR(st.st_mode)) { 87 copymkdir(homefd, p, openat(skelfd, e->d_name, O_DIRECTORY), 88 st.st_mode & _DEF_DIRMODE, uid, gid, st.st_flags); 89 continue; 90 } 91 92 if (S_ISLNK(st.st_mode) && 93 (len = readlinkat(skelfd, e->d_name, lnk, sizeof(lnk) -1)) 94 != -1) { 95 lnk[len] = '\0'; 96 symlinkat(lnk, homefd, p); 97 fchownat(homefd, p, uid, gid, AT_SYMLINK_NOFOLLOW); 98 continue; 99 } 100 101 if (!S_ISREG(st.st_mode)) 102 continue; 103 104 if ((srcfd = openat(skelfd, e->d_name, O_RDONLY)) == -1) 105 continue; 106 destfd = openat(homefd, p, O_RDWR | O_CREAT | O_EXCL, 107 st.st_mode); 108 if (destfd == -1) { 109 close(srcfd); 110 continue; 111 } 112 113 while ((sz = read(srcfd, copybuf, sizeof(copybuf))) > 0) 114 write(destfd, copybuf, sz); 115 116 close(srcfd); 117 /* 118 * Propagate special filesystem flags 119 */ 120 fchown(destfd, uid, gid); 121 fchflags(destfd, st.st_flags); 122 close(destfd); 123 } 124 closedir(d); 125 } 126