xref: /freebsd/usr.sbin/pw/rm_r.c (revision 796ba6fcc2bb4305a6faa0029da6f413293b4d9c)
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 
32d6f907dcSJoerg Wunsch #include <sys/stat.h>
33*796ba6fcSBaptiste Daroussin 
34d6f907dcSJoerg Wunsch #include <dirent.h>
35ee8c5d14SBaptiste Daroussin #include <fcntl.h>
36*796ba6fcSBaptiste Daroussin #include <string.h>
37*796ba6fcSBaptiste Daroussin #include <unistd.h>
38d6f907dcSJoerg Wunsch 
39d6f907dcSJoerg Wunsch #include "pwupd.h"
40d6f907dcSJoerg Wunsch 
41d6f907dcSJoerg Wunsch void
42ee8c5d14SBaptiste Daroussin rm_r(int rootfd, const char *path, uid_t uid)
43d6f907dcSJoerg Wunsch {
44ee8c5d14SBaptiste Daroussin 	int dirfd;
45ee8c5d14SBaptiste Daroussin 	DIR *d;
46d6f907dcSJoerg Wunsch 	struct dirent  *e;
47d6f907dcSJoerg Wunsch 	struct stat     st;
48d6f907dcSJoerg Wunsch 
49ee8c5d14SBaptiste Daroussin 	if (*path == '/')
50ee8c5d14SBaptiste Daroussin 		path++;
51ee8c5d14SBaptiste Daroussin 
52ee8c5d14SBaptiste Daroussin 	dirfd = openat(rootfd, path, O_DIRECTORY);
53ee8c5d14SBaptiste Daroussin 
54ee8c5d14SBaptiste Daroussin 	d = fdopendir(dirfd);
55d6f907dcSJoerg Wunsch 	while ((e = readdir(d)) != NULL) {
56ee8c5d14SBaptiste Daroussin 		if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0)
57ee8c5d14SBaptiste Daroussin 			continue;
58ee8c5d14SBaptiste Daroussin 
59ee8c5d14SBaptiste Daroussin 		if (fstatat(dirfd, e->d_name, &st, AT_SYMLINK_NOFOLLOW) != 0)
60ee8c5d14SBaptiste Daroussin 			continue;
61ee8c5d14SBaptiste Daroussin 		if (S_ISDIR(st.st_mode))
62ee8c5d14SBaptiste Daroussin 			rm_r(dirfd, e->d_name, uid);
63ee8c5d14SBaptiste Daroussin 		else if (S_ISLNK(st.st_mode) || st.st_uid == uid)
64ee8c5d14SBaptiste Daroussin 			unlinkat(dirfd, e->d_name, 0);
65d6f907dcSJoerg Wunsch 	}
66d6f907dcSJoerg Wunsch 	closedir(d);
67ee8c5d14SBaptiste Daroussin 	if (fstatat(rootfd, path, &st, AT_SYMLINK_NOFOLLOW) != 0)
68ee8c5d14SBaptiste Daroussin 		return;
69ee8c5d14SBaptiste Daroussin 	unlinkat(rootfd, path, S_ISDIR(st.st_mode) ? AT_REMOVEDIR : 0);
70d6f907dcSJoerg Wunsch }
71