cpdir.c revision 219408
1180867Sdavidxu/*- 2180867Sdavidxu * Copyright (C) 1996 3180867Sdavidxu * David L. Nugent. All rights reserved. 4180867Sdavidxu * 5180867Sdavidxu * Redistribution and use in source and binary forms, with or without 6180867Sdavidxu * modification, are permitted provided that the following conditions 7180867Sdavidxu * are met: 8180867Sdavidxu * 1. Redistributions of source code must retain the above copyright 9180867Sdavidxu * notice, this list of conditions and the following disclaimer. 10180867Sdavidxu * 2. Redistributions in binary form must reproduce the above copyright 11180867Sdavidxu * notice, this list of conditions and the following disclaimer in the 12180867Sdavidxu * documentation and/or other materials provided with the distribution. 13180867Sdavidxu * 14180867Sdavidxu * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND 15180867Sdavidxu * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16180867Sdavidxu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17180867Sdavidxu * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE 18180867Sdavidxu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19180867Sdavidxu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20180867Sdavidxu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21180867Sdavidxu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22180867Sdavidxu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23180867Sdavidxu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24180867Sdavidxu * SUCH DAMAGE. 25180867Sdavidxu */ 26180867Sdavidxu 27180867Sdavidxu#ifndef lint 28180867Sdavidxustatic const char rcsid[] = 29180867Sdavidxu "$FreeBSD: head/usr.sbin/pw/cpdir.c 219408 2011-03-08 20:13:29Z jkim $"; 30180867Sdavidxu#endif /* not lint */ 31180867Sdavidxu 32180867Sdavidxu#include <err.h> 33180867Sdavidxu#include <errno.h> 34180867Sdavidxu#include <fcntl.h> 35180867Sdavidxu#include <stdio.h> 36180867Sdavidxu#include <string.h> 37198788Sbrueffer#include <stdlib.h> 38180867Sdavidxu#include <unistd.h> 39180867Sdavidxu#include <sys/types.h> 40180867Sdavidxu#include <sys/stat.h> 41180867Sdavidxu#include <sys/param.h> 42180867Sdavidxu#include <dirent.h> 43180867Sdavidxu 44180867Sdavidxu#include "pw.h" 45180867Sdavidxu#include "pwupd.h" 46180867Sdavidxu 47180867Sdavidxuvoid 48180867Sdavidxucopymkdir(char const * dir, char const * skel, mode_t mode, uid_t uid, gid_t gid) 49180867Sdavidxu{ 50180867Sdavidxu char src[MAXPATHLEN]; 51180867Sdavidxu char dst[MAXPATHLEN]; 52180867Sdavidxu char lnk[MAXPATHLEN]; 53180867Sdavidxu int len; 54180867Sdavidxu 55180867Sdavidxu if (mkdir(dir, mode) != 0 && errno != EEXIST) { 56180867Sdavidxu warn("mkdir(%s)", dir); 57180867Sdavidxu } else { 58180867Sdavidxu int infd, outfd; 59180867Sdavidxu struct stat st; 60180867Sdavidxu 61180867Sdavidxu static char counter = 0; 62180867Sdavidxu static char *copybuf = NULL; 63180867Sdavidxu 64180867Sdavidxu ++counter; 65180867Sdavidxu chown(dir, uid, gid); 66180867Sdavidxu if (skel != NULL && *skel != '\0') { 67180867Sdavidxu DIR *d = opendir(skel); 68180867Sdavidxu 69180867Sdavidxu if (d != NULL) { 70180867Sdavidxu struct dirent *e; 71180867Sdavidxu 72180867Sdavidxu while ((e = readdir(d)) != NULL) { 73180867Sdavidxu char *p = e->d_name; 74180867Sdavidxu 75180867Sdavidxu if (snprintf(src, sizeof(src), "%s/%s", skel, p) >= (int)sizeof(src)) 76180867Sdavidxu warn("warning: pathname too long '%s/%s' (skel not copied)", skel, p); 77180867Sdavidxu else if (lstat(src, &st) == 0) { 78180867Sdavidxu if (strncmp(p, "dot.", 4) == 0) /* Conversion */ 79180867Sdavidxu p += 3; 80180867Sdavidxu if (snprintf(dst, sizeof(dst), "%s/%s", dir, p) >= (int)sizeof(dst)) 81180867Sdavidxu warn("warning: path too long '%s/%s' (skel file skipped)", dir, p); 82180867Sdavidxu else { 83180867Sdavidxu if (S_ISDIR(st.st_mode)) { /* Recurse for this */ 84180867Sdavidxu if (strcmp(e->d_name, ".") != 0 && strcmp(e->d_name, "..") != 0) 85180867Sdavidxu copymkdir(dst, src, st.st_mode & _DEF_DIRMODE, uid, gid); 86180867Sdavidxu chflags(dst, st.st_flags); /* propogate flags */ 87180867Sdavidxu } else if (S_ISLNK(st.st_mode) && (len = readlink(src, lnk, sizeof(lnk))) != -1) { 88180867Sdavidxu lnk[len] = '\0'; 89180867Sdavidxu symlink(lnk, dst); 90180867Sdavidxu lchown(dst, uid, gid); 91180867Sdavidxu /* 92180867Sdavidxu * Note: don't propogate special attributes 93180867Sdavidxu * but do propogate file flags 94180867Sdavidxu */ 95180867Sdavidxu } else if (S_ISREG(st.st_mode) && (outfd = open(dst, O_RDWR | O_CREAT | O_EXCL, st.st_mode)) != -1) { 96180867Sdavidxu if ((infd = open(src, O_RDONLY)) == -1) { 97180867Sdavidxu close(outfd); 98180867Sdavidxu remove(dst); 99180867Sdavidxu } else { 100180867Sdavidxu int b; 101180867Sdavidxu 102180867Sdavidxu /* 103180867Sdavidxu * Allocate our copy buffer if we need to 104180867Sdavidxu */ 105 if (copybuf == NULL) 106 copybuf = malloc(4096); 107 while ((b = read(infd, copybuf, 4096)) > 0) 108 write(outfd, copybuf, b); 109 close(infd); 110 /* 111 * Propogate special filesystem flags 112 */ 113 fchown(outfd, uid, gid); 114 fchflags(outfd, st.st_flags); 115 close(outfd); 116 chown(dst, uid, gid); 117 } 118 } 119 } 120 } 121 } 122 closedir(d); 123 } 124 } 125 if (--counter == 0 && copybuf != NULL) { 126 free(copybuf); 127 copybuf = NULL; 128 } 129 } 130} 131 132