-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_user
More file actions
executable file
·93 lines (82 loc) · 3.05 KB
/
Copy pathdelete_user
File metadata and controls
executable file
·93 lines (82 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
# Copyright (C) 2021 Corexalys.
#
# This file is part of rs3f.
#
# rs3f is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# rs3f is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with rs3f. If not, see <https://www.gnu.org/licenses/>.
from argparse import ArgumentParser
import os.path
from rs3f_common import get_home_path, get_authorized_keys_path
def main():
parser = ArgumentParser()
parser.add_argument(
"container_name",
help="A comma-separated list of container names",
)
parser.add_argument(
"user_name",
help="A comma-separated list of user names",
)
args = parser.parse_args()
containers_to_edit = set()
users_to_remove = set()
for container in args.container_name.split(","):
container = container.strip()
if not container:
continue
if not os.path.exists(get_home_path(container)):
raise ValueError("Container '{container}' does not exist")
containers_to_edit.add(container)
for user in args.user_name.split(","):
user = user.strip()
if not user:
continue
users_to_remove.add(user)
print(
f"This will {len(users_to_remove)} users from {len(containers_to_edit)} containers:"
)
print("Users:", ", ".join(users_to_remove))
print("Containers:", ", ".join(containers_to_edit))
print(f"Please type a 'C' for each containers, and a 'U' for each user.")
typed_in = input("> ")
if typed_in.count("C") != len(containers_to_edit):
raise ValueError(
"Found {typed_in.count('C')} Cs, expected {len(containers_to_edit)}."
)
if typed_in.count("U") != len(users_to_remove):
raise ValueError(
"Found {typed_in.count('U')} Us, expected {len(users_to_remove)}."
)
for container in containers_to_edit:
path = get_authorized_keys_path(container)
with open(path, "r") as authorized_file:
lines = [line for line in authorized_file]
line_index = 0
found_matching = 0
while line_index < len(lines):
line = lines[line_index]
for user in users_to_remove:
if line.strip().endswith(user):
found_matching += 1
lines.pop(line_index)
break
else:
line_index += 1
if found_matching == 0:
print(f"Could not find user {user} in container {container} !")
with open(path, "w") as authorized_file:
authorized_file.write("\n".join(lines))
print("DONE")
if __name__ == "__main__":
main()