xref: /freebsd/usr.sbin/pw/cpdir.c (revision 3df5ecac8c5e0a9105ba0e783bd9e7077df944e7)
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
48d6f907dcSJoerg Wunsch copymkdir(char const * dir, char const * skel, mode_t mode, uid_t uid, gid_t gid)
49d6f907dcSJoerg Wunsch {
50d6f907dcSJoerg Wunsch 	char            src[MAXPATHLEN];
51d6f907dcSJoerg Wunsch 	char            dst[MAXPATHLEN];
521b692a8cSLukas Ertl 	char            lnk[MAXPATHLEN];
531b692a8cSLukas Ertl 	int             len;
54d6f907dcSJoerg Wunsch 
55d6f907dcSJoerg Wunsch 	if (mkdir(dir, mode) != 0 && errno != EEXIST) {
561dcc6ec7SPhilippe Charnier 		warn("mkdir(%s)", dir);
57d6f907dcSJoerg Wunsch 	} else {
58d6f907dcSJoerg Wunsch 		int             infd, outfd;
59d6f907dcSJoerg Wunsch 		struct stat     st;
60d6f907dcSJoerg Wunsch 
61d6f907dcSJoerg Wunsch 		static char     counter = 0;
62d6f907dcSJoerg Wunsch 		static char    *copybuf = NULL;
63d6f907dcSJoerg Wunsch 
64d6f907dcSJoerg Wunsch 		++counter;
65d6f907dcSJoerg Wunsch 		chown(dir, uid, gid);
6618b51f79SStefan Farfeleder 		if (skel != NULL && *skel != '\0') {
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);
771b692a8cSLukas Ertl 					else if (lstat(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)
85644af48dSJung-uk Kim 								copymkdir(dst, src, st.st_mode & _DEF_DIRMODE, uid, gid);
86*3df5ecacSUlrich Spörlein 								chflags(dst, st.st_flags);	/* propagate flags */
871b692a8cSLukas Ertl 						    } else if (S_ISLNK(st.st_mode) && (len = readlink(src, lnk, sizeof(lnk))) != -1) {
881b692a8cSLukas Ertl 							lnk[len] = '\0';
891b692a8cSLukas Ertl 							symlink(lnk, dst);
901b692a8cSLukas Ertl 							lchown(dst, uid, gid);
91d6f907dcSJoerg Wunsch 							/*
92*3df5ecacSUlrich Spörlein 							 * Note: don't propagate special attributes
93*3df5ecacSUlrich Spörlein 							 * but do propagate file flags
94d6f907dcSJoerg Wunsch 							 */
95d6f907dcSJoerg Wunsch 						    } else if (S_ISREG(st.st_mode) && (outfd = open(dst, O_RDWR | O_CREAT | O_EXCL, st.st_mode)) != -1) {
96d6f907dcSJoerg Wunsch 							if ((infd = open(src, O_RDONLY)) == -1) {
97d6f907dcSJoerg Wunsch 								close(outfd);
98d6f907dcSJoerg Wunsch 								remove(dst);
99d6f907dcSJoerg Wunsch 							} else {
100d6f907dcSJoerg Wunsch 								int             b;
101d6f907dcSJoerg Wunsch 
102d6f907dcSJoerg Wunsch 								/*
103d6f907dcSJoerg Wunsch 								 * Allocate our copy buffer if we need to
104d6f907dcSJoerg Wunsch 								 */
105d6f907dcSJoerg Wunsch 								if (copybuf == NULL)
106d6f907dcSJoerg Wunsch 									copybuf = malloc(4096);
107d6f907dcSJoerg Wunsch 								while ((b = read(infd, copybuf, 4096)) > 0)
108d6f907dcSJoerg Wunsch 									write(outfd, copybuf, b);
109d6f907dcSJoerg Wunsch 								close(infd);
1101994f5c7SDavid Nugent 								/*
111*3df5ecacSUlrich Spörlein 								 * Propagate special filesystem flags
1121994f5c7SDavid Nugent 								 */
1131994f5c7SDavid Nugent 								fchown(outfd, uid, gid);
1141994f5c7SDavid Nugent 								fchflags(outfd, st.st_flags);
115d6f907dcSJoerg Wunsch 								close(outfd);
116d6f907dcSJoerg Wunsch 								chown(dst, uid, gid);
117d6f907dcSJoerg Wunsch 							}
118d6f907dcSJoerg Wunsch 						    }
119d6f907dcSJoerg Wunsch 						}
120d6f907dcSJoerg Wunsch 					}
1211994f5c7SDavid Nugent 				}
122d6f907dcSJoerg Wunsch 				closedir(d);
123d6f907dcSJoerg Wunsch 			}
124d6f907dcSJoerg Wunsch 		}
125d6f907dcSJoerg Wunsch 		if (--counter == 0 && copybuf != NULL) {
126d6f907dcSJoerg Wunsch 			free(copybuf);
127d6f907dcSJoerg Wunsch 			copybuf = NULL;
128d6f907dcSJoerg Wunsch 		}
129d6f907dcSJoerg Wunsch 	}
130d6f907dcSJoerg Wunsch }
13148aee7f3SJoerg Wunsch 
132