-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdelete_user_config.py
More file actions
53 lines (43 loc) · 1.69 KB
/
Copy pathdelete_user_config.py
File metadata and controls
53 lines (43 loc) · 1.69 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
import os
import subprocess
import fcntl
LOCK_FILE = "/var/lock/easy_rsa.lock"
def delete_user_config(user_id):
"""
Removes an OpenVPN client configuration, keys, and related files.
"""
easy_rsa_dir = "/etc/openvpn/easy-rsa"
ccd_dir = "/etc/openvpn/ccd"
client_config_dir = "/etc/openvpn/client-configs"
files_to_remove = [
(os.path.join(client_config_dir, f"{user_id}.ovpn")),
(os.path.join(easy_rsa_dir, "pki", "issued", f"{user_id}.crt")),
(os.path.join(easy_rsa_dir, "pki", "private", f"{user_id}.key")),
(os.path.join(easy_rsa_dir, "pki", "reqs", f"{user_id}.req")),
(os.path.join(ccd_dir, str(user_id)))
]
for path in files_to_remove:
if os.path.isfile(path):
os.remove(path)
# Generate client certificate and key
env = os.environ.copy()
env["EASYRSA"] = "/etc/openvpn/easy-rsa"
env["EASYRSA_PKI"] = "/etc/openvpn/easy-rsa/pki"
env['EASYRSA_BATCH'] = '1'
with open(LOCK_FILE, 'w') as lock_file:
fcntl.flock(lock_file, fcntl.LOCK_EX)
try:
subprocess.run(["./easyrsa", "--batch", "revoke", str(user_id)],
cwd=easy_rsa_dir, check=True, capture_output=True, env=env)
except Exception:
pass
try:
subprocess.run(["./easyrsa", "gen-crl"], cwd=easy_rsa_dir, check=True, capture_output=True, env=env)
except Exception:
pass
try:
subprocess.run(["cp", os.path.join(easy_rsa_dir, "pki", "crl.pem"), "/etc/openvpn/"],
check=True, capture_output=True)
except Exception:
pass
fcntl.flock(lock_file, fcntl.LOCK_UN)