diff options
author | bapt <bapt@FreeBSD.org> | 2015-07-12 21:43:57 +0000 |
---|---|---|
committer | bapt <bapt@FreeBSD.org> | 2015-07-12 21:43:57 +0000 |
commit | 16b27ac1cfeeb4676dfc24ad1be02d253f418ac3 (patch) | |
tree | 838245f376118a5ed4dceb73d9e87668871142ee /usr.sbin/pw/rm_r.c | |
parent | 8621997ee99ee7487f32e3f5c0c45b8dc9c21d5a (diff) | |
download | FreeBSD-src-16b27ac1cfeeb4676dfc24ad1be02d253f418ac3.zip FreeBSD-src-16b27ac1cfeeb4676dfc24ad1be02d253f418ac3.tar.gz |
pw -R <rootdir> userdel can now cleanup installation
Rewrite rm_r to use *at function, allowing to remove home directories along with
users. only crontabs and at(1) installation are not removed
Relnotes: yes
Diffstat (limited to 'usr.sbin/pw/rm_r.c')
-rw-r--r-- | usr.sbin/pw/rm_r.c | 52 |
1 files changed, 25 insertions, 27 deletions
diff --git a/usr.sbin/pw/rm_r.c b/usr.sbin/pw/rm_r.c index 797ca9d..65a63e6 100644 --- a/usr.sbin/pw/rm_r.c +++ b/usr.sbin/pw/rm_r.c @@ -37,39 +37,37 @@ static const char rcsid[] = #include <sys/param.h> #include <unistd.h> #include <dirent.h> +#include <fcntl.h> #include "pwupd.h" void -rm_r(char const * dir, uid_t uid) +rm_r(int rootfd, const char *path, uid_t uid) { - DIR *d = opendir(dir); + int dirfd; + DIR *d; + struct dirent *e; + struct stat st; - if (d != NULL) { - struct dirent *e; - struct stat st; - char file[MAXPATHLEN]; + if (*path == '/') + path++; - while ((e = readdir(d)) != NULL) { - if (strcmp(e->d_name, ".") != 0 && strcmp(e->d_name, "..") != 0) { - snprintf(file, sizeof(file), "%s/%s", dir, e->d_name); - if (lstat(file, &st) == 0) { /* Need symlinks, not - * linked file */ - if (S_ISDIR(st.st_mode)) /* Directory - recurse */ - rm_r(file, uid); - else { - if (S_ISLNK(st.st_mode) || st.st_uid == uid) - remove(file); - } - } - } - } - closedir(d); - if (lstat(dir, &st) == 0) { - if (S_ISLNK(st.st_mode)) - remove(dir); - else if (st.st_uid == uid) - rmdir(dir); - } + dirfd = openat(rootfd, path, O_DIRECTORY); + + d = fdopendir(dirfd); + while ((e = readdir(d)) != NULL) { + if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0) + continue; + + if (fstatat(dirfd, e->d_name, &st, AT_SYMLINK_NOFOLLOW) != 0) + continue; + if (S_ISDIR(st.st_mode)) + rm_r(dirfd, e->d_name, uid); + else if (S_ISLNK(st.st_mode) || st.st_uid == uid) + unlinkat(dirfd, e->d_name, 0); } + closedir(d); + if (fstatat(rootfd, path, &st, AT_SYMLINK_NOFOLLOW) != 0) + return; + unlinkat(rootfd, path, S_ISDIR(st.st_mode) ? AT_REMOVEDIR : 0); } |