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$"; 3030259Scharnier#endif /* not lint */ 3130259Scharnier 3230259Scharnier#include <err.h> 3330259Scharnier#include <errno.h> 3430259Scharnier#include <fcntl.h> 3520253Sjoerg#include <stdio.h> 3620253Sjoerg#include <string.h> 3720253Sjoerg#include <stdlib.h> 3830259Scharnier#include <unistd.h> 3920253Sjoerg#include <sys/types.h> 4020253Sjoerg#include <sys/stat.h> 4130259Scharnier#include <sys/param.h> 4220253Sjoerg#include <dirent.h> 4320253Sjoerg 44219408Sjkim#include "pw.h" 4520253Sjoerg#include "pwupd.h" 4620253Sjoerg 4720253Sjoergvoid 4820253Sjoergcopymkdir(char const * dir, char const * skel, mode_t mode, uid_t uid, gid_t gid) 4920253Sjoerg{ 5020253Sjoerg char src[MAXPATHLEN]; 5120253Sjoerg char dst[MAXPATHLEN]; 52168046Sle char lnk[MAXPATHLEN]; 53168046Sle int len; 5420253Sjoerg 5520253Sjoerg if (mkdir(dir, mode) != 0 && errno != EEXIST) { 5630259Scharnier warn("mkdir(%s)", dir); 5720253Sjoerg } else { 5820253Sjoerg int infd, outfd; 5920253Sjoerg struct stat st; 6020253Sjoerg 6120253Sjoerg static char counter = 0; 6220253Sjoerg static char *copybuf = NULL; 6320253Sjoerg 6420253Sjoerg ++counter; 6520253Sjoerg chown(dir, uid, gid); 66144841Sstefanf if (skel != NULL && *skel != '\0') { 6720253Sjoerg DIR *d = opendir(skel); 6820253Sjoerg 6920253Sjoerg if (d != NULL) { 7020253Sjoerg struct dirent *e; 7120253Sjoerg 7220253Sjoerg while ((e = readdir(d)) != NULL) { 7320253Sjoerg char *p = e->d_name; 7420253Sjoerg 7563094Sdavidn if (snprintf(src, sizeof(src), "%s/%s", skel, p) >= (int)sizeof(src)) 7663094Sdavidn warn("warning: pathname too long '%s/%s' (skel not copied)", skel, p); 77168046Sle else if (lstat(src, &st) == 0) { 7820253Sjoerg if (strncmp(p, "dot.", 4) == 0) /* Conversion */ 7920253Sjoerg p += 3; 8063094Sdavidn if (snprintf(dst, sizeof(dst), "%s/%s", dir, p) >= (int)sizeof(dst)) 8163094Sdavidn warn("warning: path too long '%s/%s' (skel file skipped)", dir, p); 8263094Sdavidn else { 8363094Sdavidn if (S_ISDIR(st.st_mode)) { /* Recurse for this */ 8420253Sjoerg if (strcmp(e->d_name, ".") != 0 && strcmp(e->d_name, "..") != 0) 85219408Sjkim copymkdir(dst, src, st.st_mode & _DEF_DIRMODE, uid, gid); 86228990Suqs chflags(dst, st.st_flags); /* propagate flags */ 87231654Skevlo } else if (S_ISLNK(st.st_mode) && (len = readlink(src, lnk, sizeof(lnk) - 1)) != -1) { 88168046Sle lnk[len] = '\0'; 89168046Sle symlink(lnk, dst); 90168046Sle lchown(dst, uid, gid); 9120253Sjoerg /* 92228990Suqs * Note: don't propagate special attributes 93228990Suqs * but do propagate file flags 9420253Sjoerg */ 9563094Sdavidn } else if (S_ISREG(st.st_mode) && (outfd = open(dst, O_RDWR | O_CREAT | O_EXCL, st.st_mode)) != -1) { 9620253Sjoerg if ((infd = open(src, O_RDONLY)) == -1) { 9720253Sjoerg close(outfd); 9820253Sjoerg remove(dst); 9920253Sjoerg } else { 10020253Sjoerg int b; 10120253Sjoerg 10220253Sjoerg /* 10320253Sjoerg * Allocate our copy buffer if we need to 10420253Sjoerg */ 10520253Sjoerg if (copybuf == NULL) 10620253Sjoerg copybuf = malloc(4096); 10720253Sjoerg while ((b = read(infd, copybuf, 4096)) > 0) 10820253Sjoerg write(outfd, copybuf, b); 10920253Sjoerg close(infd); 11063094Sdavidn /* 111228990Suqs * Propagate special filesystem flags 11263094Sdavidn */ 11363094Sdavidn fchown(outfd, uid, gid); 11463094Sdavidn fchflags(outfd, st.st_flags); 11520253Sjoerg close(outfd); 11620253Sjoerg chown(dst, uid, gid); 11720253Sjoerg } 11863094Sdavidn } 11920253Sjoerg } 12020253Sjoerg } 12120253Sjoerg } 12220253Sjoerg closedir(d); 12320253Sjoerg } 12420253Sjoerg } 12520253Sjoerg if (--counter == 0 && copybuf != NULL) { 12620253Sjoerg free(copybuf); 12720253Sjoerg copybuf = NULL; 12820253Sjoerg } 12920253Sjoerg } 13020253Sjoerg} 13120267Sjoerg 132