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