-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctrl
executable file
·131 lines (113 loc) · 3.73 KB
/
ctrl
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3
from config import defaults
from config.logs import logger
import argparse
import asyncio
import json
import ssl
import sys
logger.remove()
logger.configure(
**{
"handlers": [
{
"sink": sys.stdout,
"format": "<lvl>{message}</lvl>",
"level": "INFO",
"colorize": False,
},
],
}
)
parser = argparse.ArgumentParser(
description="EHLO CTRL", formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
"-H",
"--host",
dest="hostname",
type=str,
default="localhost",
help="Override localhost",
)
parser.add_argument(
"-t",
"--confirm_token",
action="store_true",
help="Generate a token when prompted by the application",
)
parser.add_argument(
"-p",
"--promote-user",
dest="promote_user",
type=str,
help="Promote a user to system administrator",
)
ctrl_parameters = vars(parser.parse_args())
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(1)
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
context.load_cert_chain(certfile=defaults.TLS_CERTFILE, keyfile=defaults.TLS_KEYFILE)
context.load_verify_locations(cafile=defaults.TLS_CA)
context.check_hostname = False
context.verify_mode = ssl.VerifyMode.CERT_REQUIRED
context.minimum_version = ssl.TLSVersion.TLSv1_3
async def main():
match ctrl_parameters:
case {
"promote_user": promote_user,
} if isinstance(promote_user, str):
selection = 0
r, w = await asyncio.open_connection(
ctrl_parameters["hostname"], 2102, ssl=context
)
w.write(b"\x97")
w.write(f"{promote_user}\n".encode("utf-8"))
await w.drain()
data = await r.readexactly(1)
if data == b"\x01":
logger.info("User was promoted")
elif data == b"\x02":
logger.warning("User is already administrator")
elif data == b"\x03":
logger.error("User does not exist")
case {
"confirm_token": True,
}:
try:
selection = 0
r, w = await asyncio.open_connection(
ctrl_parameters["hostname"], 2102, ssl=context
)
w.write(b"\x98")
await w.drain()
data = await r.readuntil(b"\n")
requests = json.loads(data.strip().decode("ascii"))
if not requests:
logger.warning("No request is awaiting confirmation.")
else:
logger.info("\x1b[1mPlease select a token to confirm:\x1b[0m\n")
for idx, (code, intention) in requests.items():
logger.info(f"\x1b[1m#{idx}\x1b[0m - {code}: {intention}")
while selection not in requests.keys():
try:
selection = str(input("Enter a token #: "))
except ValueError:
continue
w.write(b"\x99")
w.write(f"{requests[selection][0]}".encode("ascii"))
await w.drain()
data = await r.readexactly(6)
logger.info(
"\nConfirmation code: \x1b[1;32m"
+ data.strip().decode("ascii")
+ "\x1b[0m\n"
)
except asyncio.exceptions.IncompleteReadError as e:
logger.error(f"Server error: {e}")
finally:
w.close()
await w.wait_closed()
if __name__ == "__main__":
asyncio.run(main())