xref: /freebsd/usr.sbin/pw/cpdir.c (revision 1994f5c7aadb7dc3c2b9f2c06c387cb0f838d2b3)
1d6f907dcSJoerg Wunsch /*-
2ad7cf975SJoerg Wunsch  * Copyright (C) 1996
3ad7cf975SJoerg Wunsch  *	David L. Nugent.  All rights reserved.
4d6f907dcSJoerg Wunsch  *
5d6f907dcSJoerg Wunsch  * Redistribution and use in source and binary forms, with or without
6d6f907dcSJoerg Wunsch  * modification, are permitted provided that the following conditions
7d6f907dcSJoerg Wunsch  * are met:
8d6f907dcSJoerg Wunsch  * 1. Redistributions of source code must retain the above copyright
9ad7cf975SJoerg Wunsch  *    notice, this list of conditions and the following disclaimer.
10d6f907dcSJoerg Wunsch  * 2. Redistributions in binary form must reproduce the above copyright
11d6f907dcSJoerg Wunsch  *    notice, this list of conditions and the following disclaimer in the
12d6f907dcSJoerg Wunsch  *    documentation and/or other materials provided with the distribution.
13d6f907dcSJoerg Wunsch  *
14ad7cf975SJoerg Wunsch  * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
15d6f907dcSJoerg Wunsch  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16d6f907dcSJoerg Wunsch  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17ad7cf975SJoerg Wunsch  * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
18d6f907dcSJoerg Wunsch  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19d6f907dcSJoerg Wunsch  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20d6f907dcSJoerg Wunsch  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21d6f907dcSJoerg Wunsch  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22d6f907dcSJoerg Wunsch  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23d6f907dcSJoerg Wunsch  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24d6f907dcSJoerg Wunsch  * SUCH DAMAGE.
25d6f907dcSJoerg Wunsch  */
26d6f907dcSJoerg Wunsch 
271dcc6ec7SPhilippe Charnier #ifndef lint
281dcc6ec7SPhilippe Charnier static const char rcsid[] =
2997d92980SPeter Wemm   "$FreeBSD$";
301dcc6ec7SPhilippe Charnier #endif /* not lint */
311dcc6ec7SPhilippe Charnier 
321dcc6ec7SPhilippe Charnier #include <err.h>
331dcc6ec7SPhilippe Charnier #include <errno.h>
341dcc6ec7SPhilippe Charnier #include <fcntl.h>
35d6f907dcSJoerg Wunsch #include <stdio.h>
36d6f907dcSJoerg Wunsch #include <string.h>
37d6f907dcSJoerg Wunsch #include <stdlib.h>
381dcc6ec7SPhilippe Charnier #include <unistd.h>
39d6f907dcSJoerg Wunsch #include <sys/types.h>
40d6f907dcSJoerg Wunsch #include <sys/stat.h>
41d6f907dcSJoerg Wunsch #include <sys/param.h>
421dcc6ec7SPhilippe Charnier #include <dirent.h>
43d6f907dcSJoerg Wunsch 
44d6f907dcSJoerg Wunsch #include "pwupd.h"
45d6f907dcSJoerg Wunsch 
46d6f907dcSJoerg Wunsch void
47d6f907dcSJoerg Wunsch copymkdir(char const * dir, char const * skel, mode_t mode, uid_t uid, gid_t gid)
48d6f907dcSJoerg Wunsch {
49d6f907dcSJoerg Wunsch 	int             rc = 0;
50d6f907dcSJoerg Wunsch 	char            src[MAXPATHLEN];
51d6f907dcSJoerg Wunsch 	char            dst[MAXPATHLEN];
52d6f907dcSJoerg Wunsch 
53d6f907dcSJoerg Wunsch 	if (mkdir(dir, mode) != 0 && errno != EEXIST) {
541dcc6ec7SPhilippe Charnier 		warn("mkdir(%s)", dir);
55d6f907dcSJoerg Wunsch 	} else {
56d6f907dcSJoerg Wunsch 		int             infd, outfd;
57d6f907dcSJoerg Wunsch 		struct stat     st;
58d6f907dcSJoerg Wunsch 
59d6f907dcSJoerg Wunsch 		static char     counter = 0;
60d6f907dcSJoerg Wunsch 		static char    *copybuf = NULL;
61d6f907dcSJoerg Wunsch 
62d6f907dcSJoerg Wunsch 		++counter;
63d6f907dcSJoerg Wunsch 		chown(dir, uid, gid);
64d6f907dcSJoerg Wunsch 		if (skel == NULL || *skel == '\0')
65d6f907dcSJoerg Wunsch 			rc = 1;
66d6f907dcSJoerg Wunsch 		else {
67d6f907dcSJoerg Wunsch 			DIR            *d = opendir(skel);
68d6f907dcSJoerg Wunsch 
69d6f907dcSJoerg Wunsch 			if (d != NULL) {
70d6f907dcSJoerg Wunsch 				struct dirent  *e;
71d6f907dcSJoerg Wunsch 
72d6f907dcSJoerg Wunsch 				while ((e = readdir(d)) != NULL) {
73d6f907dcSJoerg Wunsch 					char           *p = e->d_name;
74d6f907dcSJoerg Wunsch 
751994f5c7SDavid Nugent 					if (snprintf(src, sizeof(src), "%s/%s", skel, p) >= (int)sizeof(src))
761994f5c7SDavid Nugent 						warn("warning: pathname too long '%s/%s' (skel not copied)", skel, p);
771994f5c7SDavid Nugent 					else if (stat(src, &st) == 0) {
78d6f907dcSJoerg Wunsch 						if (strncmp(p, "dot.", 4) == 0)	/* Conversion */
79d6f907dcSJoerg Wunsch 							p += 3;
801994f5c7SDavid Nugent 						if (snprintf(dst, sizeof(dst), "%s/%s", dir, p) >= (int)sizeof(dst))
811994f5c7SDavid Nugent 							warn("warning: path too long '%s/%s' (skel file skipped)", dir, p);
821994f5c7SDavid Nugent 						else {
83d6f907dcSJoerg Wunsch 						    if (S_ISDIR(st.st_mode)) {	/* Recurse for this */
84d6f907dcSJoerg Wunsch 							if (strcmp(e->d_name, ".") != 0 && strcmp(e->d_name, "..") != 0)
85d6f907dcSJoerg Wunsch 								copymkdir(dst, src, (st.st_mode & 0777), uid, gid);
861994f5c7SDavid Nugent 								chflags(dst, st.st_flags);	/* propogate flags */
87d6f907dcSJoerg Wunsch 							/*
881994f5c7SDavid Nugent 							 * Note: don't propogate special attributes
891994f5c7SDavid Nugent 							 * but do propogate file flags
90d6f907dcSJoerg Wunsch 							 */
91d6f907dcSJoerg Wunsch 						    } else if (S_ISREG(st.st_mode) && (outfd = open(dst, O_RDWR | O_CREAT | O_EXCL, st.st_mode)) != -1) {
92d6f907dcSJoerg Wunsch 							if ((infd = open(src, O_RDONLY)) == -1) {
93d6f907dcSJoerg Wunsch 								close(outfd);
94d6f907dcSJoerg Wunsch 								remove(dst);
95d6f907dcSJoerg Wunsch 							} else {
96d6f907dcSJoerg Wunsch 								int             b;
97d6f907dcSJoerg Wunsch 
98d6f907dcSJoerg Wunsch 								/*
99d6f907dcSJoerg Wunsch 								 * Allocate our copy buffer if we need to
100d6f907dcSJoerg Wunsch 								 */
101d6f907dcSJoerg Wunsch 								if (copybuf == NULL)
102d6f907dcSJoerg Wunsch 									copybuf = malloc(4096);
103d6f907dcSJoerg Wunsch 								while ((b = read(infd, copybuf, 4096)) > 0)
104d6f907dcSJoerg Wunsch 									write(outfd, copybuf, b);
105d6f907dcSJoerg Wunsch 								close(infd);
1061994f5c7SDavid Nugent 								/*
1071994f5c7SDavid Nugent 								 * Propogate special filesystem flags
1081994f5c7SDavid Nugent 								 */
1091994f5c7SDavid Nugent 								fchown(outfd, uid, gid);
1101994f5c7SDavid Nugent 								fchflags(outfd, st.st_flags);
111d6f907dcSJoerg Wunsch 								close(outfd);
112d6f907dcSJoerg Wunsch 								chown(dst, uid, gid);
113d6f907dcSJoerg Wunsch 							}
114d6f907dcSJoerg Wunsch 						    }
115d6f907dcSJoerg Wunsch 						}
116d6f907dcSJoerg Wunsch 					}
1171994f5c7SDavid Nugent 				}
118d6f907dcSJoerg Wunsch 				closedir(d);
119d6f907dcSJoerg Wunsch 			}
120d6f907dcSJoerg Wunsch 		}
121d6f907dcSJoerg Wunsch 		if (--counter == 0 && copybuf != NULL) {
122d6f907dcSJoerg Wunsch 			free(copybuf);
123d6f907dcSJoerg Wunsch 			copybuf = NULL;
124d6f907dcSJoerg Wunsch 		}
125d6f907dcSJoerg Wunsch 	}
126d6f907dcSJoerg Wunsch }
12748aee7f3SJoerg Wunsch 
128