cpdir.c revision 63094
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 63094 2000-07-13 23:52:49Z davidn $";
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	int             rc = 0;
5020253Sjoerg	char            src[MAXPATHLEN];
5120253Sjoerg	char            dst[MAXPATHLEN];
5220253Sjoerg
5320253Sjoerg	if (mkdir(dir, mode) != 0 && errno != EEXIST) {
5430259Scharnier		warn("mkdir(%s)", dir);
5520253Sjoerg	} else {
5620253Sjoerg		int             infd, outfd;
5720253Sjoerg		struct stat     st;
5820253Sjoerg
5920253Sjoerg		static char     counter = 0;
6020253Sjoerg		static char    *copybuf = NULL;
6120253Sjoerg
6220253Sjoerg		++counter;
6320253Sjoerg		chown(dir, uid, gid);
6420253Sjoerg		if (skel == NULL || *skel == '\0')
6520253Sjoerg			rc = 1;
6620253Sjoerg		else {
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);
7763094Sdavidn					else if (stat(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)
8520253Sjoerg								copymkdir(dst, src, (st.st_mode & 0777), uid, gid);
8663094Sdavidn								chflags(dst, st.st_flags);	/* propogate flags */
8720253Sjoerg							/*
8863094Sdavidn							 * Note: don't propogate special attributes
8963094Sdavidn							 * but do propogate file flags
9020253Sjoerg							 */
9163094Sdavidn						    } else if (S_ISREG(st.st_mode) && (outfd = open(dst, O_RDWR | O_CREAT | O_EXCL, st.st_mode)) != -1) {
9220253Sjoerg							if ((infd = open(src, O_RDONLY)) == -1) {
9320253Sjoerg								close(outfd);
9420253Sjoerg								remove(dst);
9520253Sjoerg							} else {
9620253Sjoerg								int             b;
9720253Sjoerg
9820253Sjoerg								/*
9920253Sjoerg								 * Allocate our copy buffer if we need to
10020253Sjoerg								 */
10120253Sjoerg								if (copybuf == NULL)
10220253Sjoerg									copybuf = malloc(4096);
10320253Sjoerg								while ((b = read(infd, copybuf, 4096)) > 0)
10420253Sjoerg									write(outfd, copybuf, b);
10520253Sjoerg								close(infd);
10663094Sdavidn								/*
10763094Sdavidn								 * Propogate special filesystem flags
10863094Sdavidn								 */
10963094Sdavidn								fchown(outfd, uid, gid);
11063094Sdavidn								fchflags(outfd, st.st_flags);
11120253Sjoerg								close(outfd);
11220253Sjoerg								chown(dst, uid, gid);
11320253Sjoerg							}
11463094Sdavidn						    }
11520253Sjoerg						}
11620253Sjoerg					}
11720253Sjoerg				}
11820253Sjoerg				closedir(d);
11920253Sjoerg			}
12020253Sjoerg		}
12120253Sjoerg		if (--counter == 0 && copybuf != NULL) {
12220253Sjoerg			free(copybuf);
12320253Sjoerg			copybuf = NULL;
12420253Sjoerg		}
12520253Sjoerg	}
12620253Sjoerg}
12720267Sjoerg
128