120253Sjoerg/*-
220302Sjoerg * Copyright (C) 1996
320302Sjoerg *	David L. Nugent.  All rights reserved.
420253Sjoerg *
520253Sjoerg * Redistribution and use in source and binary forms, with or without
620253Sjoerg * modification, are permitted provided that the following conditions
720253Sjoerg * are met:
820253Sjoerg * 1. Redistributions of source code must retain the above copyright
920302Sjoerg *    notice, this list of conditions and the following disclaimer.
1020253Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
1120253Sjoerg *    notice, this list of conditions and the following disclaimer in the
1220253Sjoerg *    documentation and/or other materials provided with the distribution.
1320253Sjoerg *
1420302Sjoerg * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
1520253Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1620253Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1720302Sjoerg * ARE DISCLAIMED.  IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
1820253Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1920253Sjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2020253Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2120253Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2220253Sjoerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2320253Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2420253Sjoerg * SUCH DAMAGE.
2520253Sjoerg */
2620253Sjoerg
2730259Scharnier#ifndef lint
2830259Scharnierstatic const char rcsid[] =
2950479Speter  "$FreeBSD$";
3030259Scharnier#endif /* not lint */
3130259Scharnier
32287084Sbapt#include <sys/stat.h>
33287084Sbapt
34287084Sbapt#include <dirent.h>
35287084Sbapt#include <fcntl.h>
3678720Sdd#include <string.h>
3720253Sjoerg#include <unistd.h>
3820253Sjoerg
3920253Sjoerg#include "pwupd.h"
4020253Sjoerg
4120253Sjoergvoid
42287084Sbaptrm_r(int rootfd, const char *path, uid_t uid)
4320253Sjoerg{
44287084Sbapt	int dirfd;
45287084Sbapt	DIR *d;
46287084Sbapt	struct dirent  *e;
47287084Sbapt	struct stat     st;
4820253Sjoerg
49287084Sbapt	if (*path == '/')
50287084Sbapt		path++;
5120253Sjoerg
52287084Sbapt	dirfd = openat(rootfd, path, O_DIRECTORY);
53303257Sbapt	if (dirfd == -1) {
54303257Sbapt		return;
55303257Sbapt	}
56287084Sbapt
57287084Sbapt	d = fdopendir(dirfd);
58287084Sbapt	while ((e = readdir(d)) != NULL) {
59287084Sbapt		if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0)
60287084Sbapt			continue;
61287084Sbapt
62287084Sbapt		if (fstatat(dirfd, e->d_name, &st, AT_SYMLINK_NOFOLLOW) != 0)
63287084Sbapt			continue;
64287084Sbapt		if (S_ISDIR(st.st_mode))
65287084Sbapt			rm_r(dirfd, e->d_name, uid);
66287084Sbapt		else if (S_ISLNK(st.st_mode) || st.st_uid == uid)
67287084Sbapt			unlinkat(dirfd, e->d_name, 0);
6820253Sjoerg	}
69287084Sbapt	closedir(d);
70287084Sbapt	if (fstatat(rootfd, path, &st, AT_SYMLINK_NOFOLLOW) != 0)
71287084Sbapt		return;
72287084Sbapt	unlinkat(rootfd, path, S_ISDIR(st.st_mode) ? AT_REMOVEDIR : 0);
7320253Sjoerg}
74