-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSshMuxClient.py
338 lines (264 loc) · 10.2 KB
/
SshMuxClient.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import os
import socket
import struct
from Buffer import Buffer
MUX_MSG_HELLO = 0x00000001
MUX_C_NEW_SESSION = 0x10000002
MUX_C_ALIVE_CHECK = 0x10000004
MUX_C_TERMINATE = 0x10000005
MUX_C_OPEN_FWD = 0x10000006
MUX_C_CLOSE_FWD = 0x10000007
MUX_C_NEW_STDIO_FWD = 0x10000008
MUX_C_STOP_LISTENING = 0x10000009
MUX_C_LIST_FWDS = 0x1000000a
MUX_C_LIST_SESSIONS = 0x1000000b
MUX_C_INFO = 0x1000000c
MUX_S_OK = 0x80000001
MUX_S_PERMISSION_DENIED = 0x80000002
MUX_S_FAILURE = 0x80000003
MUX_S_EXIT_MESSAGE = 0x80000004
MUX_S_ALIVE = 0x80000005
MUX_S_SESSION_OPENED = 0x80000006
MUX_S_REMOTE_PORT = 0x80000007
MUX_S_TTY_ALLOC_FAIL = 0x80000008
MUX_S_RESULT = 0x80000009
MUX_FWD_LOCAL = 1
MUX_FWD_REMOTE = 2
MUX_FWD_DYNAMIC = 3
MUX_FWD_PORT_STREAMLOCAL = -2
SSHMUX_VER = 4
_fwd_types = {
MUX_FWD_LOCAL: 'local',
MUX_FWD_REMOTE: 'remote',
MUX_FWD_DYNAMIC: 'dynamic'
}
_fwd_names = {
'local': MUX_FWD_LOCAL,
'remote': MUX_FWD_REMOTE,
'dynamic': MUX_FWD_DYNAMIC
}
class SshMuxClient(object):
def __init__(self, path):
self.rid = 0
self.path = path
self.fd = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
def _write_packet(self, buf):
pkt = Buffer()
pkt.put_str(buf)
return self.fd.sendall(pkt)
def _read_packet(self):
m = Buffer(self.fd.recv(4))
pktlen = m.get_int()
return Buffer(self.fd.recv(pktlen))
def connect(self, blocking=1):
self.fd.connect(self.path)
self.fd.setblocking(blocking)
m = Buffer()
m.put_int(MUX_MSG_HELLO)
m.put_int(SSHMUX_VER)
if self._write_packet(m) is not None:
return (False, 'Can\'t send HELLO message')
m = self._read_packet()
mux_msg = m.get_int()
if mux_msg != MUX_MSG_HELLO:
return (False, 'Expected HELLO reply, got %u' % (mux_msg,))
mux_ver = m.get_int()
if mux_ver != SSHMUX_VER:
return (False, 'Unsupported multiplexing protocol version %d (expected %d)' % (mux_ver, SSHMUX_VER))
extensions = {}
while len(m):
name = m.get_str()
value = m.get_str()
extensions[name] = value
return (True, extensions)
def close(self):
self.fd.close()
def check(self):
m = Buffer()
m.put_int(MUX_C_ALIVE_CHECK)
rid = self.rid
self.rid += 1
m.put_int(rid)
if self._write_packet(m) is not None:
return (False, 'Can\'t send ALIVE-CHECK request')
m = self._read_packet()
rep_msg = m.get_int()
if rep_msg != MUX_S_ALIVE:
return (False, 'Expected ALIVE reply, got %u' % (rep_msg,))
rep_rid = m.get_int()
if rep_rid != rid:
return (False, 'Got unexpected request id %u, expected %u' % (rep_rid, rid))
rep_pid = m.get_int()
return (True, rep_pid)
def exit(self):
m = Buffer()
m.put_int(MUX_C_TERMINATE)
rid = self.rid
self.rid += 1
m.put_int(rid)
if self._write_packet(m) is not None:
return (False, 'Can\'t send TERMINATE request')
# ignore reply
# m = self._read_packet()
#
# rep_msg = m.get_int()
#
# rep_rid = m.get_int()
# if rep_rid != rid:
# return (False, 'Got unexpected request id %u, expected %u' % (rep_rid, rid))
#
# if rep_msg != MUX_S_OK:
# rep_reason = m.get_str()
# if rep_msg == MUX_S_FAILURE:
# return (False, 'Failure in EXIT message: %s' % (rep_reason,))
# elif rep_msg == MUX_S_PERMISSION_DENIED:
# return (False, 'Permission denied for EXIT message: %s' % (rep_reason,))
# return (False, 'Unexpected server reply, got %u' % (rep_msg,))
return (True, None)
def stop(self):
m = Buffer()
m.put_int(MUX_C_STOP_LISTENING)
rid = self.rid
self.rid += 1
m.put_int(rid)
if self._write_packet(m) is not None:
return (False, 'Can\'t send STOP-LISTENING request')
m = self._read_packet()
rep_msg = m.get_int()
rep_rid = m.get_int()
if rep_rid != rid:
return (False, 'Got unexpected request id %u, expected %u' % (rep_rid, rid))
if rep_msg != MUX_S_OK:
rep_reason = m.get_str()
if rep_msg == MUX_S_FAILURE:
return (False, 'Failure in STOP message: %s' % (rep_reason,))
elif rep_msg == MUX_S_PERMISSION_DENIED:
return (False, 'Permission denied for STOP message: %s' % (rep_reason,))
return (False, 'Unexpected server reply, got %u' % (rep_msg,))
return (True, None)
def forward(self, mode, ftype, listen_host, listen_port, connect_host, connect_port):
m = Buffer()
if mode:
n = 'OPEN'
m.put_int(MUX_C_OPEN_FWD)
else:
n = 'CLOSE'
m.put_int(MUX_C_CLOSE_FWD)
rid = self.rid
self.rid += 1
m.put_int(rid)
if isinstance(ftype, basestring):
assert ftype in _fwd_names, 'Invalid forward type %s' % (ftype,)
ftype = _fwd_names[ftype]
m.put_int(ftype)
m.put_str(listen_host)
m.put_int(listen_port)
m.put_str(connect_host)
m.put_int(connect_port)
if self._write_packet(m) is not None:
return (False, 'Can\'t send %s-FORWARD request' % (n,))
m = self._read_packet()
rep_msg = m.get_int()
rep_rid = m.get_int()
if rep_rid != rid:
return (False, 'Got unexpected request id %u, expected %u' % (rep_rid, rid))
if rep_msg != MUX_S_OK and rep_msg != MUX_S_REMOTE_PORT:
rep_reason = m.get_str()
if rep_msg == MUX_S_FAILURE:
return (False, 'Failure in %s-FORWARD request: %s' % (n, rep_reason,))
elif rep_msg == MUX_S_PERMISSION_DENIED:
return (False, 'Permission denied for %s-FORWARD request: %s' % (n, rep_reason,))
return (False, 'Unexpected server reply, got %u' % (rep_msg,))
if ftype == MUX_FWD_REMOTE and listen_port == 0:
if rep_msg != MUX_S_REMOTE_PORT:
return (False, 'Expected remote port reply, got %u' % (rep_msg,))
rep_port = m.get_int()
return (True, rep_port)
return (True, None)
def open_forward(self, ftype, listen_host, listen_port, connect_host, connect_port):
return self.forward(True, ftype, listen_host, listen_port, connect_host, connect_port)
def close_forward(self, ftype, listen_host, listen_port, connect_host, connect_port):
return self.forward(False, ftype, listen_host, listen_port, connect_host, connect_port)
def forwards(self):
m = Buffer()
m.put_int(MUX_C_LIST_FWDS)
rid = self.rid
self.rid += 1
m.put_int(rid)
if self._write_packet(m) is not None:
return (False, 'Can\'t send LIST-FORWARDS request')
m = self._read_packet()
rep_msg = m.get_int()
rep_rid = m.get_int()
if rep_rid != rid:
return (False, 'Got unexpected request id %u, expected %u' % (rep_rid, rid))
if rep_msg != MUX_S_RESULT:
rep_reason = m.get_str()
if rep_msg == MUX_S_FAILURE:
return (False, 'Failure in LIST-FORWARDS request: %s' % (rep_reason,))
elif rep_msg == MUX_S_PERMISSION_DENIED:
return (False, 'Permission denied for LIST-FORWARDS request: %s' % (rep_reason,))
return (False, 'Unexpected server reply, got %u' % (rep_msg,))
fwds = []
while len(m):
fid = m.get_int()
ftype = m.get_int()
listen_host = m.get_str()
listen_port = m.get_int()
connect_host = m.get_str()
connect_port = m.get_int()
if ftype == MUX_FWD_REMOTE and listen_port == 0:
listen_port = m.get_int()
fwds.append((fid, _fwd_types[ftype], listen_host, listen_port, connect_host, connect_port))
return (True, fwds)
def sessions(self):
m = Buffer()
m.put_int(MUX_C_LIST_SESSIONS)
rid = self.rid
self.rid += 1
m.put_int(rid)
if self._write_packet(m) is not None:
return (False, 'Can\'t send LIST-SESSIONS request')
m = self._read_packet()
rep_msg = m.get_int()
rep_rid = m.get_int()
if rep_rid != rid:
return (False, 'Got unexpected request id %u, expected %u' % (rep_rid, rid))
if rep_msg != MUX_S_RESULT:
rep_reason = m.get_str()
if rep_msg == MUX_S_FAILURE:
return (False, 'Failure in LIST-SESSIONS request: %s' % (rep_reason,))
elif rep_msg == MUX_S_PERMISSION_DENIED:
return (False, 'Permission denied for LIST-SESSIONS request: %s' % (rep_reason,))
return (False, 'Unexpected server reply, got %u' % (rep_msg,))
sessions = []
while len(m):
sid = m.get_int();
stype = m.get_int();
rid = m.get_int();
cid = m.get_int();
tname = m.get_str();
rname = m.get_str();
sessions.append((sid, stype, rid, cid, tname, rname))
return (True, sessions)
def info(self, fmt):
m = Buffer()
m.put_int(MUX_C_INFO)
rid = self.rid
self.rid += 1
m.put_int(rid)
m.put_str(fmt)
if self._write_packet(m) is not None:
return (False, 'Can\'t send INFO request')
m = self._read_packet()
rep_msg = m.get_int()
rep_rid = m.get_int()
if rep_rid != rid:
return (False, 'Got unexpected request id %u, expected %u' % (rep_rid, rid))
if rep_msg != MUX_S_RESULT:
rep_reason = m.get_str()
if rep_msg == MUX_S_FAILURE:
return (False, rep_reason)
return (False, 'Expected INFO reply, got %x' % (rep_msg,))
rep_str = m.get_str()
return (True, rep_str)