xref: /freebsd/usr.sbin/pw/cpdir.c (revision 18b51f792fb28f3909a1e3a6b44898572b9afdcf)
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 	char            src[MAXPATHLEN];
50d6f907dcSJoerg Wunsch 	char            dst[MAXPATHLEN];
51d6f907dcSJoerg Wunsch 
52d6f907dcSJoerg Wunsch 	if (mkdir(dir, mode) != 0 && errno != EEXIST) {
531dcc6ec7SPhilippe Charnier 		warn("mkdir(%s)", dir);
54d6f907dcSJoerg Wunsch 	} else {
55d6f907dcSJoerg Wunsch 		int             infd, outfd;
56d6f907dcSJoerg Wunsch 		struct stat     st;
57d6f907dcSJoerg Wunsch 
58d6f907dcSJoerg Wunsch 		static char     counter = 0;
59d6f907dcSJoerg Wunsch 		static char    *copybuf = NULL;
60d6f907dcSJoerg Wunsch 
61d6f907dcSJoerg Wunsch 		++counter;
62d6f907dcSJoerg Wunsch 		chown(dir, uid, gid);
6318b51f79SStefan Farfeleder 		if (skel != NULL && *skel != '\0') {
64d6f907dcSJoerg Wunsch 			DIR            *d = opendir(skel);
65d6f907dcSJoerg Wunsch 
66d6f907dcSJoerg Wunsch 			if (d != NULL) {
67d6f907dcSJoerg Wunsch 				struct dirent  *e;
68d6f907dcSJoerg Wunsch 
69d6f907dcSJoerg Wunsch 				while ((e = readdir(d)) != NULL) {
70d6f907dcSJoerg Wunsch 					char           *p = e->d_name;
71d6f907dcSJoerg Wunsch 
721994f5c7SDavid Nugent 					if (snprintf(src, sizeof(src), "%s/%s", skel, p) >= (int)sizeof(src))
731994f5c7SDavid Nugent 						warn("warning: pathname too long '%s/%s' (skel not copied)", skel, p);
741994f5c7SDavid Nugent 					else if (stat(src, &st) == 0) {
75d6f907dcSJoerg Wunsch 						if (strncmp(p, "dot.", 4) == 0)	/* Conversion */
76d6f907dcSJoerg Wunsch 							p += 3;
771994f5c7SDavid Nugent 						if (snprintf(dst, sizeof(dst), "%s/%s", dir, p) >= (int)sizeof(dst))
781994f5c7SDavid Nugent 							warn("warning: path too long '%s/%s' (skel file skipped)", dir, p);
791994f5c7SDavid Nugent 						else {
80d6f907dcSJoerg Wunsch 						    if (S_ISDIR(st.st_mode)) {	/* Recurse for this */
81d6f907dcSJoerg Wunsch 							if (strcmp(e->d_name, ".") != 0 && strcmp(e->d_name, "..") != 0)
82d6f907dcSJoerg Wunsch 								copymkdir(dst, src, (st.st_mode & 0777), uid, gid);
831994f5c7SDavid Nugent 								chflags(dst, st.st_flags);	/* propogate flags */
84d6f907dcSJoerg Wunsch 							/*
851994f5c7SDavid Nugent 							 * Note: don't propogate special attributes
861994f5c7SDavid Nugent 							 * but do propogate file flags
87d6f907dcSJoerg Wunsch 							 */
88d6f907dcSJoerg Wunsch 						    } else if (S_ISREG(st.st_mode) && (outfd = open(dst, O_RDWR | O_CREAT | O_EXCL, st.st_mode)) != -1) {
89d6f907dcSJoerg Wunsch 							if ((infd = open(src, O_RDONLY)) == -1) {
90d6f907dcSJoerg Wunsch 								close(outfd);
91d6f907dcSJoerg Wunsch 								remove(dst);
92d6f907dcSJoerg Wunsch 							} else {
93d6f907dcSJoerg Wunsch 								int             b;
94d6f907dcSJoerg Wunsch 
95d6f907dcSJoerg Wunsch 								/*
96d6f907dcSJoerg Wunsch 								 * Allocate our copy buffer if we need to
97d6f907dcSJoerg Wunsch 								 */
98d6f907dcSJoerg Wunsch 								if (copybuf == NULL)
99d6f907dcSJoerg Wunsch 									copybuf = malloc(4096);
100d6f907dcSJoerg Wunsch 								while ((b = read(infd, copybuf, 4096)) > 0)
101d6f907dcSJoerg Wunsch 									write(outfd, copybuf, b);
102d6f907dcSJoerg Wunsch 								close(infd);
1031994f5c7SDavid Nugent 								/*
1041994f5c7SDavid Nugent 								 * Propogate special filesystem flags
1051994f5c7SDavid Nugent 								 */
1061994f5c7SDavid Nugent 								fchown(outfd, uid, gid);
1071994f5c7SDavid Nugent 								fchflags(outfd, st.st_flags);
108d6f907dcSJoerg Wunsch 								close(outfd);
109d6f907dcSJoerg Wunsch 								chown(dst, uid, gid);
110d6f907dcSJoerg Wunsch 							}
111d6f907dcSJoerg Wunsch 						    }
112d6f907dcSJoerg Wunsch 						}
113d6f907dcSJoerg Wunsch 					}
1141994f5c7SDavid Nugent 				}
115d6f907dcSJoerg Wunsch 				closedir(d);
116d6f907dcSJoerg Wunsch 			}
117d6f907dcSJoerg Wunsch 		}
118d6f907dcSJoerg Wunsch 		if (--counter == 0 && copybuf != NULL) {
119d6f907dcSJoerg Wunsch 			free(copybuf);
120d6f907dcSJoerg Wunsch 			copybuf = NULL;
121d6f907dcSJoerg Wunsch 		}
122d6f907dcSJoerg Wunsch 	}
123d6f907dcSJoerg Wunsch }
12448aee7f3SJoerg Wunsch 
125