-
Notifications
You must be signed in to change notification settings - Fork 5
/
rmusers.py
executable file
·32 lines (26 loc) · 1.02 KB
/
rmusers.py
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
#!/usr/bin/env python3
import os
import subprocess
import sys
from pwd import getpwnam
if os.geteuid() != 0:
sys.exit('You must be root')
data_dir = os.getenv("DATA") or sys.argv[1]
authorized_users_file = os.path.join(data_dir, "authorized_users")
existing_users_file = os.path.join(data_dir, "existing_users")
unauthed_file = os.path.join(data_dir, "unauthorized_users")
authorized_users = []
with open(authorized_users_file, "r") as f:
authorized_users = list(filter(None, f.read().split(sep='\n')))
existing_users = []
with open(existing_users_file, "r") as f:
existing_users = list(filter(None, f.read().split(sep='\n')))
with open(unauthed_file, "w") as f:
for user in existing_users:
if user not in authorized_users:
uid = str(getpwnam(user).pw_uid)
subprocess.call(['deluser', "--remove-home", user])
f.write(user + "\n")
print("User " + user + " (" + uid + ") removed")
else:
subprocess.call(['chage', '-M90', '-m1', '-W7', '-I30', user])