xref: /freebsd/usr.sbin/pw/cpdir.c (revision 65730d9349cc2fac547af1613bd5a019ff5663c8)
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 
44644af48dSJung-uk Kim #include "pw.h"
45d6f907dcSJoerg Wunsch #include "pwupd.h"
46d6f907dcSJoerg Wunsch 
47d6f907dcSJoerg Wunsch void
48*65730d93SBaptiste Daroussin copymkdir(int rootfd, char const * dir, int skelfd, mode_t mode, uid_t uid,
49*65730d93SBaptiste Daroussin     gid_t gid, int flags)
50d6f907dcSJoerg Wunsch {
51*65730d93SBaptiste Daroussin 	char		*p, lnk[MAXPATHLEN], copybuf[4096];
52*65730d93SBaptiste Daroussin 	int		len, homefd, srcfd, destfd;
53*65730d93SBaptiste Daroussin 	ssize_t		sz;
54d6f907dcSJoerg Wunsch 	struct stat     st;
55d6f907dcSJoerg Wunsch 	struct dirent  *e;
56*65730d93SBaptiste Daroussin 	DIR		*d;
57*65730d93SBaptiste Daroussin 
58*65730d93SBaptiste Daroussin 	if (*dir == '/')
59*65730d93SBaptiste Daroussin 		dir++;
60*65730d93SBaptiste Daroussin 
61*65730d93SBaptiste Daroussin 	if (mkdirat(rootfd, dir, mode) != 0 && errno != EEXIST) {
62*65730d93SBaptiste Daroussin 		warn("mkdir(%s)", dir);
63*65730d93SBaptiste Daroussin 		return;
64*65730d93SBaptiste Daroussin 	}
65*65730d93SBaptiste Daroussin 	fchownat(rootfd, dir, uid, gid, AT_SYMLINK_NOFOLLOW);
66*65730d93SBaptiste Daroussin 	if (flags > 0)
67*65730d93SBaptiste Daroussin 		chflagsat(rootfd, dir, flags, AT_SYMLINK_NOFOLLOW);
68*65730d93SBaptiste Daroussin 
69*65730d93SBaptiste Daroussin 	if (skelfd == -1)
70*65730d93SBaptiste Daroussin 		return;
71*65730d93SBaptiste Daroussin 
72*65730d93SBaptiste Daroussin 	homefd = openat(rootfd, dir, O_DIRECTORY);
73*65730d93SBaptiste Daroussin 	if ((d = fdopendir(skelfd)) == NULL) {
74*65730d93SBaptiste Daroussin 		close(skelfd);
75*65730d93SBaptiste Daroussin 		close(homefd);
76*65730d93SBaptiste Daroussin 		return;
77*65730d93SBaptiste Daroussin 	}
78d6f907dcSJoerg Wunsch 
79d6f907dcSJoerg Wunsch 	while ((e = readdir(d)) != NULL) {
80*65730d93SBaptiste Daroussin 		if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0)
81*65730d93SBaptiste Daroussin 			continue;
82d6f907dcSJoerg Wunsch 
83*65730d93SBaptiste Daroussin 		p = e->d_name;
84*65730d93SBaptiste Daroussin 		if (fstatat(skelfd, p, &st, AT_SYMLINK_NOFOLLOW) == -1)
85*65730d93SBaptiste Daroussin 			continue;
86*65730d93SBaptiste Daroussin 
87d6f907dcSJoerg Wunsch 		if (strncmp(p, "dot.", 4) == 0)	/* Conversion */
88d6f907dcSJoerg Wunsch 			p += 3;
89d6f907dcSJoerg Wunsch 
90*65730d93SBaptiste Daroussin 		if (S_ISDIR(st.st_mode)) {
91*65730d93SBaptiste Daroussin 			copymkdir(homefd, p, openat(skelfd, e->d_name, O_DIRECTORY),
92*65730d93SBaptiste Daroussin 			    st.st_mode & _DEF_DIRMODE, uid, gid, st.st_flags);
93*65730d93SBaptiste Daroussin 			continue;
94*65730d93SBaptiste Daroussin 		}
95*65730d93SBaptiste Daroussin 
96*65730d93SBaptiste Daroussin 		if (S_ISLNK(st.st_mode) &&
97*65730d93SBaptiste Daroussin 		    (len = readlinkat(skelfd, e->d_name, lnk, sizeof(lnk) -1))
98*65730d93SBaptiste Daroussin 		    != -1) {
99*65730d93SBaptiste Daroussin 			lnk[len] = '\0';
100*65730d93SBaptiste Daroussin 			symlinkat(lnk, homefd, p);
101*65730d93SBaptiste Daroussin 			fchownat(homefd, p, uid, gid, AT_SYMLINK_NOFOLLOW);
102*65730d93SBaptiste Daroussin 			continue;
103*65730d93SBaptiste Daroussin 		}
104*65730d93SBaptiste Daroussin 
105*65730d93SBaptiste Daroussin 		if (!S_ISREG(st.st_mode))
106*65730d93SBaptiste Daroussin 			continue;
107*65730d93SBaptiste Daroussin 
108*65730d93SBaptiste Daroussin 		if ((srcfd = openat(skelfd, e->d_name, O_RDONLY)) == -1)
109*65730d93SBaptiste Daroussin 			continue;
110*65730d93SBaptiste Daroussin 		destfd = openat(homefd, p, O_RDWR | O_CREAT | O_EXCL,
111*65730d93SBaptiste Daroussin 		    st.st_mode);
112*65730d93SBaptiste Daroussin 		if (destfd == -1) {
113*65730d93SBaptiste Daroussin 			close(srcfd);
114*65730d93SBaptiste Daroussin 			continue;
115*65730d93SBaptiste Daroussin 		}
116*65730d93SBaptiste Daroussin 
117*65730d93SBaptiste Daroussin 		while ((sz = read(srcfd, copybuf, sizeof(copybuf))) > 0)
118*65730d93SBaptiste Daroussin 			write(destfd, copybuf, sz);
119*65730d93SBaptiste Daroussin 
120*65730d93SBaptiste Daroussin 		close(srcfd);
1211994f5c7SDavid Nugent 		/*
1223df5ecacSUlrich Spörlein 		 * Propagate special filesystem flags
1231994f5c7SDavid Nugent 		 */
124*65730d93SBaptiste Daroussin 		fchown(destfd, uid, gid);
125*65730d93SBaptiste Daroussin 		fchflags(destfd, st.st_flags);
126*65730d93SBaptiste Daroussin 		close(destfd);
1271994f5c7SDavid Nugent 	}
128d6f907dcSJoerg Wunsch 	closedir(d);
129d6f907dcSJoerg Wunsch }
130