2020-02-19 14:59:09 +03:00
|
|
|
/*
|
2020-03-02 16:23:54 +03:00
|
|
|
* Copyright (c) 2020, Fei Wu <f.eiwu@yahoo.com>
|
2021-05-29 15:36:08 +03:00
|
|
|
* Copyright (c) 2021, Brandon Pruitt <brapru@pm.me>
|
2020-02-19 14:59:09 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-19 14:59:09 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/String.h>
|
|
|
|
#include <AK/StringBuilder.h>
|
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-12-21 13:41:08 +03:00
|
|
|
#include <LibCore/File.h>
|
2020-02-19 14:59:09 +03:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <pwd.h>
|
2021-05-29 15:36:08 +03:00
|
|
|
#include <shadow.h>
|
2020-06-28 20:40:10 +03:00
|
|
|
#include <spawn.h>
|
2020-02-19 14:59:09 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2021-01-12 09:39:08 +03:00
|
|
|
if (pledge("stdio wpath rpath cpath fattr proc exec", nullptr) < 0) {
|
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unveil("/etc/", "rwc") < 0) {
|
|
|
|
perror("unveil");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unveil("/bin/rm", "x") < 0) {
|
|
|
|
perror("unveil");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
unveil(nullptr, nullptr);
|
|
|
|
|
2020-02-19 14:59:09 +03:00
|
|
|
const char* username = nullptr;
|
|
|
|
bool remove_home = false;
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.add_option(remove_home, "Remove home directory", "remove", 'r');
|
|
|
|
args_parser.add_positional_argument(username, "Login user identity (username)", "login");
|
|
|
|
args_parser.parse(argc, argv);
|
|
|
|
|
2021-01-12 09:39:08 +03:00
|
|
|
if (!remove_home) {
|
|
|
|
if (pledge("stdio wpath rpath cpath fattr", nullptr) < 0) {
|
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-29 15:36:08 +03:00
|
|
|
char temp_passwd[] = "/etc/passwd.XXXXXX";
|
|
|
|
char temp_shadow[] = "/etc/shadow.XXXXXX";
|
|
|
|
|
|
|
|
auto temp_passwd_fd = mkstemp(temp_passwd);
|
|
|
|
if (temp_passwd_fd == -1) {
|
|
|
|
perror("failed to create temporary passwd file");
|
2020-02-19 14:59:09 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-05-29 15:36:08 +03:00
|
|
|
auto temp_shadow_fd = mkstemp(temp_shadow);
|
|
|
|
if (temp_shadow_fd == -1) {
|
|
|
|
perror("failed to create temporary shadow file");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE* temp_passwd_file = fdopen(temp_passwd_fd, "w");
|
|
|
|
if (!temp_passwd_file) {
|
2020-02-19 14:59:09 +03:00
|
|
|
perror("fdopen");
|
2021-05-29 15:36:08 +03:00
|
|
|
if (unlink(temp_passwd) < 0) {
|
|
|
|
perror("unlink");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE* temp_shadow_file = fdopen(temp_shadow_fd, "w");
|
|
|
|
if (!temp_shadow_file) {
|
|
|
|
perror("fdopen");
|
|
|
|
if (unlink(temp_shadow) < 0) {
|
2020-02-19 14:59:09 +03:00
|
|
|
perror("unlink");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool user_exists = false;
|
|
|
|
String home_directory;
|
|
|
|
|
|
|
|
int rc = 0;
|
|
|
|
setpwent();
|
|
|
|
for (auto* pw = getpwent(); pw; pw = getpwent()) {
|
|
|
|
if (strcmp(pw->pw_name, username)) {
|
2021-05-29 15:36:08 +03:00
|
|
|
if (putpwent(pw, temp_passwd_file) != 0) {
|
2020-02-19 14:59:09 +03:00
|
|
|
perror("failed to put an entry in the temporary passwd file");
|
|
|
|
rc = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
user_exists = true;
|
|
|
|
if (remove_home)
|
|
|
|
home_directory = pw->pw_dir;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
endpwent();
|
|
|
|
|
2021-05-29 15:36:08 +03:00
|
|
|
setspent();
|
|
|
|
for (auto* spwd = getspent(); spwd; spwd = getspent()) {
|
|
|
|
if (strcmp(spwd->sp_namp, username)) {
|
|
|
|
if (putspent(spwd, temp_shadow_file) != 0) {
|
|
|
|
perror("failed to put an entry in the temporary shadow file");
|
|
|
|
rc = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
endspent();
|
|
|
|
|
|
|
|
if (fclose(temp_passwd_file)) {
|
|
|
|
perror("fclose");
|
|
|
|
if (!rc)
|
|
|
|
rc = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fclose(temp_shadow_file)) {
|
2020-02-19 14:59:09 +03:00
|
|
|
perror("fclose");
|
|
|
|
if (!rc)
|
|
|
|
rc = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc == 0 && !user_exists) {
|
2021-05-30 16:05:34 +03:00
|
|
|
warnln("specified user doesn't exist");
|
2020-02-19 14:59:09 +03:00
|
|
|
rc = 6;
|
|
|
|
}
|
|
|
|
|
2021-05-29 15:36:08 +03:00
|
|
|
if (rc == 0 && chmod(temp_passwd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) {
|
|
|
|
perror("chmod");
|
|
|
|
rc = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc == 0 && chmod(temp_shadow, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) {
|
2020-02-19 14:59:09 +03:00
|
|
|
perror("chmod");
|
|
|
|
rc = 1;
|
|
|
|
}
|
|
|
|
|
2021-05-29 15:36:08 +03:00
|
|
|
if (rc == 0 && rename(temp_passwd, "/etc/passwd") < 0) {
|
2020-02-19 14:59:09 +03:00
|
|
|
perror("failed to rename the temporary passwd file");
|
|
|
|
rc = 1;
|
|
|
|
}
|
|
|
|
|
2021-05-29 15:36:08 +03:00
|
|
|
if (rc == 0 && rename(temp_shadow, "/etc/shadow") < 0) {
|
|
|
|
perror("failed to rename the temporary shadow file");
|
|
|
|
rc = 1;
|
|
|
|
}
|
|
|
|
|
2020-02-19 14:59:09 +03:00
|
|
|
if (rc) {
|
2021-05-29 15:36:08 +03:00
|
|
|
if (unlink(temp_passwd) < 0) {
|
2020-02-19 14:59:09 +03:00
|
|
|
perror("unlink");
|
|
|
|
}
|
2021-05-29 15:36:08 +03:00
|
|
|
|
|
|
|
if (unlink(temp_shadow) < 0) {
|
|
|
|
perror("unlink");
|
|
|
|
}
|
|
|
|
|
2020-02-19 14:59:09 +03:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (remove_home) {
|
2020-12-21 13:41:08 +03:00
|
|
|
if (access(home_directory.characters(), F_OK) == -1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
String real_path = Core::File::real_path_for(home_directory);
|
|
|
|
|
|
|
|
if (real_path == "/") {
|
2021-05-30 16:05:34 +03:00
|
|
|
warnln("home directory is /, not deleted!");
|
2020-02-19 14:59:09 +03:00
|
|
|
return 12;
|
|
|
|
}
|
|
|
|
|
2020-12-21 13:41:08 +03:00
|
|
|
pid_t child;
|
|
|
|
const char* argv[] = { "rm", "-r", home_directory.characters(), nullptr };
|
|
|
|
if ((errno = posix_spawn(&child, "/bin/rm", nullptr, nullptr, const_cast<char**>(argv), environ))) {
|
|
|
|
perror("posix_spawn");
|
|
|
|
return 12;
|
|
|
|
}
|
|
|
|
int wstatus;
|
|
|
|
if (waitpid(child, &wstatus, 0) < 0) {
|
|
|
|
perror("waitpid");
|
|
|
|
return 12;
|
|
|
|
}
|
|
|
|
if (WEXITSTATUS(wstatus)) {
|
2021-05-30 16:05:34 +03:00
|
|
|
warnln("failed to remove the home directory");
|
2020-12-21 13:41:08 +03:00
|
|
|
return 12;
|
2020-02-19 14:59:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|