-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathovdrive.py
More file actions
executable file
·281 lines (209 loc) · 8.27 KB
/
Copy pathovdrive.py
File metadata and controls
executable file
·281 lines (209 loc) · 8.27 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env python
"""
Driver for OpenVPN client
"""
import time
import base64
import hashlib
import struct
import hmac
import pollengine
# ---
class OVDriveData:
"""OpenVPN authentication data"""
def __init__( self ):
self.user = NO_USER
self.pass_pref = NO_PASS
self.otp_seed = NO_SEED
# ---
class ShellOVDrive:
"""Driver for OpenVPN client"""
def __init__( self ):
# PE.displaymsg( 'dbg:: Shell_OVDrive init' )
self.ovdata = OVDriveData()
return
def set_ovdata( self, ovdata ):
"""Record a data structure"""
self.ovdata = ovdata
def process_request( self, conn ):
"""Handle requests received from the client"""
# PE.displaymsg( 'Received {size} bytes "{req}"'.format(size=len(
# conn.inp_queue), req=conn.inp_queue) )
__continue = True
__lines = conn.inp_queue.split( '\n' )
for __req in __lines:
if __req != '':
PE.displaymsg( __req )
try:
__template = NEED[__req]
PE.displaymsg( 'Response: {out}'.format(out=__template.format(
user=self.ovdata.user, pw=MASKED_PW)) )
conn.queue_response( __template.format(user=self.ovdata.user,
pw=gen_otp(self.ovdata)) )
except KeyError:
pass
conn.inp_queue = ''
conn.inp_lines = ''
return True
def get_name( self ):
"""Return a description of the class"""
return 'Shell_OVDrive'
# ---
class ShellAdmin:
"""Driver for admin shell object"""
def __init__( self ):
# PE.displaymsg( 'dbg:: Shell_Admin init' )
self.ovdata = OVDriveData()
def set_ovdata( self, ovdata ):
"""Record a data structure"""
self.ovdata = ovdata
def process_request( self, conn ):
"""Handle requests received from the client"""
# PE.displaymsg( 'Received {size} bytes "{req}"'.format(
# size=len(conn.inp_queue), req=conn.inp_queue) )
__continue = True
__lines = conn.inp_queue.split( '\n' )
for __req in __lines:
__words = __req.split( ' ' )
# PE.displaymsg( 'dbg:: line "{line}" words {nw}'.format(line=__req,
# nw=len(__words)) )
if len(__words) > 0:
__comm = __words[0]
if __comm == '':
continue
if __comm == ACR_USER:
if len(__words) == 2:
self.ovdata.user = __words[1]
PE.displaymsg( 'Admin: set user to {user}'.
format(user=self.ovdata.user) )
conn.queue_response( 'User set to {user}'.
format(user=self.ovdata.user) )
else:
conn.queue_response(
'The "user" command takes one argument' )
elif __comm == ACR_PASS:
if len(__words) == 2:
self.ovdata.pass_pref = __words[1]
PE.displaymsg( 'Admin: set password for {user}'.
format(user=self.ovdata.user) )
conn.queue_response( 'Password for {user} set'.
format(user=self.ovdata.user) )
else:
conn.queue_response(
'The "pass" command takes one argument' )
elif __comm == ACR_SEED:
if len(__words) == 2:
self.ovdata.otp_seed = __words[1]
PE.displaymsg( 'Admin: set OTP seed for {user}'.
format(user=self.ovdata.user) )
conn.queue_response( 'OTP seed for {user} set'.
format(user=self.ovdata.user) )
else:
conn.queue_response(
'The "seed" command takes one argument' )
elif __comm == ACR_SHOW:
PE.displaymsg( 'Admin: show current settings' )
conn.queue_response( SHOW_TEMPLATE.format(
user=self.ovdata.user, pw=self.ovdata.pass_pref,
seed=self.ovdata.otp_seed) )
elif __comm == ACR_OTP:
__otp = gen_otp(self.ovdata)
PE.displaymsg( 'Admin: Generate OTP {pwmask}{otp}'.format(
otp=__otp[-6:], pwmask=MASKED_PW) )
conn.queue_response( 'OTP for {now} is {otp}'.format(
now=time.strftime('%y/%m/%d-%H:%M:%S'), otp=__otp) )
elif __comm == ACR_DROP:
PE.displaymsg( 'Admin: drop client connection' )
conn.close()
elif __comm == ACR_QUIT:
PE.displaymsg( 'Admin: stop OVDriver' )
__continue = False
else:
PE.displaymsg( 'Admin: unrecognized command "{comm}"'.
format(comm=__comm) )
conn.queue_response( 'Command "{comm}" unrecognized'.
format(comm=__comm) )
# conn.queue_response( 'Got {nl} lines, {size} bytes\n'.format(
# size=len(conn.inp_queue), nl=len(__lines)) )
conn.inp_lines = ''
conn.inp_queue = ''
return __continue
def get_name( self ):
"""Return a description of the class"""
return 'Shell_Admin'
# ---
def gen_otp( ovdata ):
"""Generate the current one-time-password"""
try:
__n_interval = time.time() // OTP_INTERVAL
__key = base64.b32decode( ovdata.otp_seed, casefold=OTP_COLLAPSE )
__msg = struct.pack( '>Q', __n_interval )
__hdig = hmac.new( __key, __msg, OTP_DIGEST_METH ).digest()
__ob_low = ord( __hdig[19] ) & 15
__fulltoken = struct.unpack( '>I', __hdig[__ob_low:__ob_low + 4])
__token = (__fulltoken[0] & OTP_TOKEN_MASK) % (10 ** OTP_TOKEN_LEN)
__result = '{pref}{token:06d}'.format(pref=ovdata.pass_pref,
token=__token)
except TypeError:
__result = '{pref}{failed}'.format(pref=ovdata.pass_pref,
failed=OTP_ERROR)
# PE.displaymsg( 'dbg:: Generate OTP {otp}'.format(otp=__result) )
return( __result )
# ---
def request_status( conn_list ):
"""Callback routine to periodically check server status"""
for __conn in conn_list:
# PE.displaymsg( 'dbg:: port {port} type {ct}'.format(ct=__conn.type,
# port=__conn.server_port) )
if __conn.server_port == OVDRIVE_PORT and __conn.type == IS_SESSION:
# PE.displaymsg( 'dbg:: Found an OVDrive session' )
__conn.queue_response( STATUS_CHECK )
return
# ---
ADMIN_PORT = 7778
OVDRIVE_PORT = 7777
MASKED_PW = '*PW-MASKED*'
IS_LISTEN = 0
IS_SESSION = 1
NO_USER = 'NoUser'
NO_PASS = 'NoPassword'
NO_SEED = 'NoOTPSeed'
OTP_INTERVAL = 30
OTP_TOKEN_LEN = 6
OTP_DIGEST_METH = hashlib.sha1
OTP_COLLAPSE = True
OTP_TOKEN_MASK = 0x7fffffff
OTP_ERROR = 'OTP-Failed'
ACR_USER = 'user'
ACR_PASS = 'pass'
ACR_SEED = 'seed'
ACR_SHOW = 'show'
ACR_QUIT = 'quit'
ACR_OTP = 'otp'
ACR_DROP = 'drop'
STATUS_CHECK = 'load-stats'
REQ_RELEASE = ">HOLD:Waiting for hold release"
REQ_AUTH = ">PASSWORD:Need 'Auth' username/password"
REQ_PASS = "SUCCESS: 'Auth' username entered, but not yet verified"
REQ_SETSTATE = "SUCCESS: 'Auth' password entered, but not yet verified"
NEED = dict()
NEED[REQ_RELEASE] = 'hold release'
NEED[REQ_AUTH] = 'username Auth {user}'
NEED[REQ_PASS] = 'password Auth {pw}'
NEED[REQ_SETSTATE] = 'state on'
SERVER_SPEC_LIST = ( (ADMIN_PORT), (OVDRIVE_PORT) )
SHOW_TEMPLATE = 'User: {user}\nPass: {pw}\nSeed: {seed}'
# ---
PE = pollengine
ENGINE = PE.Engine( server_list = SERVER_SPEC_LIST )
OVDATA = OVDriveData()
OVDATA.user = NO_USER
OVDATA.pass_pref = NO_PASS
OVDATA.otp_seed = NO_SEED
ADMIN_SHELL = ShellAdmin()
ADMIN_SHELL.set_ovdata( OVDATA )
OVDRIVE_SHELL = ShellOVDrive()
OVDRIVE_SHELL.set_ovdata( OVDATA )
ENGINE.config_server( ADMIN_PORT, shell = ADMIN_SHELL, status = request_status )
ENGINE.config_server( OVDRIVE_PORT, shell = OVDRIVE_SHELL )
ENGINE.run()