cpdir.c revision 168046
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: head/usr.sbin/pw/cpdir.c 168046 2007-03-30 13:18:52Z le $";
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
4420253Sjoerg#include "pwupd.h"
4520253Sjoerg
4620253Sjoergvoid
4720253Sjoergcopymkdir(char const * dir, char const * skel, mode_t mode, uid_t uid, gid_t gid)
4820253Sjoerg{
4920253Sjoerg	char            src[MAXPATHLEN];
5020253Sjoerg	char            dst[MAXPATHLEN];
51168046Sle	char            lnk[MAXPATHLEN];
52168046Sle	int             len;
5320253Sjoerg
5420253Sjoerg	if (mkdir(dir, mode) != 0 && errno != EEXIST) {
5530259Scharnier		warn("mkdir(%s)", dir);
5620253Sjoerg	} else {
5720253Sjoerg		int             infd, outfd;
5820253Sjoerg		struct stat     st;
5920253Sjoerg
6020253Sjoerg		static char     counter = 0;
6120253Sjoerg		static char    *copybuf = NULL;
6220253Sjoerg
6320253Sjoerg		++counter;
6420253Sjoerg		chown(dir, uid, gid);
65144841Sstefanf		if (skel != NULL && *skel != '\0') {
6620253Sjoerg			DIR            *d = opendir(skel);
6720253Sjoerg
6820253Sjoerg			if (d != NULL) {
6920253Sjoerg				struct dirent  *e;
7020253Sjoerg
7120253Sjoerg				while ((e = readdir(d)) != NULL) {
7220253Sjoerg					char           *p = e->d_name;
7320253Sjoerg
7463094Sdavidn					if (snprintf(src, sizeof(src), "%s/%s", skel, p) >= (int)sizeof(src))
7563094Sdavidn						warn("warning: pathname too long '%s/%s' (skel not copied)", skel, p);
76168046Sle					else if (lstat(src, &st) == 0) {
7720253Sjoerg						if (strncmp(p, "dot.", 4) == 0)	/* Conversion */
7820253Sjoerg							p += 3;
7963094Sdavidn						if (snprintf(dst, sizeof(dst), "%s/%s", dir, p) >= (int)sizeof(dst))
8063094Sdavidn							warn("warning: path too long '%s/%s' (skel file skipped)", dir, p);
8163094Sdavidn						else {
8263094Sdavidn						    if (S_ISDIR(st.st_mode)) {	/* Recurse for this */
8320253Sjoerg							if (strcmp(e->d_name, ".") != 0 && strcmp(e->d_name, "..") != 0)
8420253Sjoerg								copymkdir(dst, src, (st.st_mode & 0777), uid, gid);
8563094Sdavidn								chflags(dst, st.st_flags);	/* propogate flags */
86168046Sle						    } else if (S_ISLNK(st.st_mode) && (len = readlink(src, lnk, sizeof(lnk))) != -1) {
87168046Sle							lnk[len] = '\0';
88168046Sle							symlink(lnk, dst);
89168046Sle							lchown(dst, uid, gid);
9020253Sjoerg							/*
9163094Sdavidn							 * Note: don't propogate special attributes
9263094Sdavidn							 * but do propogate file flags
9320253Sjoerg							 */
9463094Sdavidn						    } else if (S_ISREG(st.st_mode) && (outfd = open(dst, O_RDWR | O_CREAT | O_EXCL, st.st_mode)) != -1) {
9520253Sjoerg							if ((infd = open(src, O_RDONLY)) == -1) {
9620253Sjoerg								close(outfd);
9720253Sjoerg								remove(dst);
9820253Sjoerg							} else {
9920253Sjoerg								int             b;
10020253Sjoerg
10120253Sjoerg								/*
10220253Sjoerg								 * Allocate our copy buffer if we need to
10320253Sjoerg								 */
10420253Sjoerg								if (copybuf == NULL)
10520253Sjoerg									copybuf = malloc(4096);
10620253Sjoerg								while ((b = read(infd, copybuf, 4096)) > 0)
10720253Sjoerg									write(outfd, copybuf, b);
10820253Sjoerg								close(infd);
10963094Sdavidn								/*
11063094Sdavidn								 * Propogate special filesystem flags
11163094Sdavidn								 */
11263094Sdavidn								fchown(outfd, uid, gid);
11363094Sdavidn								fchflags(outfd, st.st_flags);
11420253Sjoerg								close(outfd);
11520253Sjoerg								chown(dst, uid, gid);
11620253Sjoerg							}
11763094Sdavidn						    }
11820253Sjoerg						}
11920253Sjoerg					}
12020253Sjoerg				}
12120253Sjoerg				closedir(d);
12220253Sjoerg			}
12320253Sjoerg		}
12420253Sjoerg		if (--counter == 0 && copybuf != NULL) {
12520253Sjoerg			free(copybuf);
12620253Sjoerg			copybuf = NULL;
12720253Sjoerg		}
12820253Sjoerg	}
12920253Sjoerg}
13020267Sjoerg
131