-
Notifications
You must be signed in to change notification settings - Fork 13
/
ssh.lua
487 lines (368 loc) · 11.5 KB
/
ssh.lua
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
-- SPDX-License-Identifier: MIT
-- Author: Jianhui Zhao <zhaojh329@gmail.com>
local socket = require 'eco.socket'
local ssh = require 'eco.core.ssh'
local file = require 'eco.file'
local time = require 'eco.time'
local sys = require 'eco.sys'
local M = {}
local function waitsocket(session, timeout)
local dir = session.session:block_directions()
local ev = 0
if dir & ssh.SESSION_BLOCK_INBOUND then
ev = ev | eco.READ
end
if dir & ssh.SESSION_BLOCK_OUTBOUND then
ev = ev | eco.WRITE
end
session.iow:modify(ev)
return session.iow:wait(timeout)
end
local function open_channel(session)
local channel, err
while true do
channel, err = session.session:open_channel()
if channel then break end
if session.session:last_errno() ~= ssh.ERROR_EAGAIN then
return nil, err
end
if not waitsocket(session, 5.0) then
return nil, 'timeout'
end
end
return channel
end
local function channel_scp_recv(session, source)
local channel, size
while true do
channel, size = session.session:scp_recv(source)
if channel then break end
if session.session:last_errno() ~= ssh.ERROR_EAGAIN then
return nil, size
end
if not waitsocket(session, 5.0) then
return nil, 'timeout'
end
end
return channel, size
end
local function channel_scp_send(session, dest, mode, size)
local channel, err
while true do
channel, err = session.session:scp_send(dest, mode, size)
if channel then break end
if session.session:last_errno() ~= ssh.ERROR_EAGAIN then
return nil, err
end
if not waitsocket(session, 5.0) then
return nil, 'timeout'
end
end
return channel
end
local function channel_call_c_wrap(session, channel, timeout, func, ...)
timeout = timeout or 5.0
local deadtime = sys.uptime() + timeout
while sys.uptime() < deadtime do
local ok, err = channel[func](channel, ...)
if ok then return true end
if err ~= ssh.ERROR_EAGAIN then
return nil, session.session:last_error()
end
if not waitsocket(session, deadtime - sys.uptime()) then
return nil, 'timeout'
end
end
return nil, 'timeout'
end
local function channel_exec(session, channel, cmd)
return channel_call_c_wrap(session, channel, nil, 'exec', cmd)
end
local function channel_close(session, channel)
return channel_call_c_wrap(session, channel, nil, 'close')
end
local function channel_free(session, channel)
return channel_call_c_wrap(session, channel, nil, 'free')
end
local function channel_send_eof(session, channel)
return channel_call_c_wrap(session, channel, nil, 'send_eof')
end
local function channel_wait_eof(session, channel)
return channel_call_c_wrap(session, channel, nil, 'wait_eof')
end
local function channel_wait_closed(session, channel)
return channel_call_c_wrap(session, channel, nil, 'wait_closed')
end
local function channel_signal(session, channel, signame)
return channel_call_c_wrap(session, channel, nil, 'signal', signame)
end
local function channel_exec_read_data(session, channel, stream_id, data, timeout)
timeout = timeout or 30.0
local deadtime = sys.uptime() + timeout
while sys.uptime() < deadtime do
local chunk, err = channel:read(stream_id)
if not chunk then
if err ~= ssh.ERROR_EAGAIN then
return nil, session.session:last_error()
end
if not waitsocket(session, deadtime - sys.uptime()) then
return nil, 'timeout'
end
else
if #chunk == 0 then return true end
data[#data + 1] = chunk
-- Avoid blocking for too long while receive too much
time.sleep(0.0001)
end
end
return nil, 'timeout'
end
local session_methods = {}
function session_methods:exec(cmd, timeout)
local channel, err = open_channel(self)
if not channel then
return nil, err
end
local ok, err = channel_exec(self, channel, cmd)
if not ok then
channel_free(self, channel)
return nil, err
end
local data = {}
ok, err = channel_exec_read_data(self, channel, 0, data, timeout)
if not ok then
channel_signal(self, channel, 'KILL')
channel_free(self, channel)
return nil, err
end
channel_exec_read_data(self, channel, ssh.EXTENDED_DATA_STDERR, data, timeout)
local ok, err = channel_close(self, channel)
if not ok then
channel_free(self, channel)
return nil, err
end
local exitcode = channel:get_exit_status()
local exitsignal = channel:get_exit_signal()
channel_free(self, channel)
local signals = { HUP = 1, INT = 2, QUIT = 3, ILL = 4, TRAP = 5, ABRT = 6,
BUS = 7, FPE = 8, KILL = 9, USR1 = 10, SEGV = 11, USR2 = 12, PIPE = 13,
ALRM = 14, TERM = 15, STKFLT = 16, CHLD = 17,CONT = 18, STOP = 19,
TSTP = 20, TTIN = 21, TTOU = 22, URG = 23, XCPU = 24, XFSZ = 25,
VTALRM = 26, PROF = 27, WINCH = 28, POLL = 29, PWR = 30, SYS = 31
}
if exitsignal then
exitcode = 128 + signals[exitsignal]
end
return table.concat(data), exitcode, exitsignal
end
local function scp_recv(session, channel, size, f, data)
local got = 0
while got < size do
local chunk, err = channel:read(0)
if not chunk then
if err ~= ssh.ERROR_EAGAIN then
return nil, session.session:last_error()
end
if not waitsocket(session, 3.0) then
return nil, 'timeout'
end
else
if f then
local ok, err = f:write(chunk)
if not ok then
return nil, err
end
else
data[#data + 1] = chunk
end
got = got + #chunk
-- Avoid blocking for too long while receive large file
time.sleep(0.0001)
end
end
return got
end
function session_methods:scp_recv(source, dest)
local channel, size = channel_scp_recv(self, source)
if not channel then
return nil, size
end
local data = {}
local f, err
if dest then
f, err = io.open(dest, 'w')
if not f then
channel_free(self, channel)
return nil, err
end
end
local got, err = scp_recv(self, channel, size, f, data)
if f then f:close() end
channel_free(self, channel)
if not got then
return nil, err
end
if f then
return got
else
return table.concat(data)
end
end
local function scp_send_data(session, channel, data)
local total = #data
local written = 0
while written < total do
local n, err = channel:write(data:sub(written + 1))
if not n then
if err ~= ssh.ERROR_EAGAIN then
return nil, session:last_error()
end
if not waitsocket(session, 3.0) then
return nil, 'timeout'
end
else
written = written + n
-- Avoid blocking for too long while receive large file
time.sleep(0.0001)
end
end
return true
end
function session_methods:scp_send(data, dest)
local channel, size = channel_scp_send(self, dest, file.S_IRUSR | file.S_IWUSR | file.S_IRGRP | file.S_IROTH, #data)
if not channel then
return nil, size
end
local ok, err = scp_send_data(self, channel, data)
channel_send_eof(self, channel)
channel_wait_eof(self, channel)
channel_wait_closed(self, channel)
channel_free(self, channel)
if not ok then
return nil, err
end
return true
end
function session_methods:scp_sendfile(source, dest)
local st, err = file.stat(source)
if not st then
return nil, err
end
if st['type'] ~= 'REG' then
return nil, source .. ': not a regular file'
end
local channel, size = channel_scp_send(self, dest, st.mode, st.size)
if not channel then
return nil, size
end
local f, err = io.open(source)
if not f then
channel_free(self, channel)
return nil, err
end
local ok = true
local data
while true do
data, err = f:read(1024)
if not data then break end
ok, err = scp_send_data(self, channel, data)
if not ok then break end
end
f:close()
channel_send_eof(self, channel)
channel_wait_eof(self, channel)
channel_wait_closed(self, channel)
channel_free(self, channel)
if not ok then
return nil, err
end
return true
end
function session_methods:disconnect(reaason, description)
self.disconnected = true
local deadtime = sys.uptime() + 5.0
reaason = reaason or ssh.DISCONNECT_BY_APPLICATION
description = description or 'Normal Shutdown'
while sys.uptime() < deadtime do
local ok, err = self.session:disconnect(reaason, description)
if ok then return true end
if err ~= ssh.ERROR_EAGAIN then
return nil, self.session:last_error()
end
if not waitsocket(self, deadtime - sys.uptime()) then
return nil, 'timeout'
end
end
return nil, 'timeout'
end
function session_methods:free()
if not self.disconnected then
self:disconnect()
end
local deadtime = sys.uptime() + 5.0
while sys.uptime() < deadtime do
local ok, err = self.session:free()
if ok then break end
if err ~= ssh.ERROR_EAGAIN then
break
end
if not waitsocket(self, deadtime - sys.uptime()) then
break
end
end
self.sock:close()
end
local session_metatable = {
__index = session_methods,
__gc = session_methods.free
}
function M.new(ipaddr, port, username, password)
if not socket.is_ipv4_address(ipaddr) and not socket.is_ipv6_address(ipaddr) then
return nil, 'invalid ipaddr: ' .. ipaddr
end
local sock, err = socket.connect_tcp(ipaddr, port)
if not sock then
return nil, err
end
local session = ssh.new()
local fd = sock:getfd()
local obj = { session = session, sock = sock, iow = eco.watcher(eco.IO, fd) }
local ok
while true do
ok, err = session:handshake(fd)
if ok then break end
if err ~= ssh.ERROR_EAGAIN then
err = session:last_error()
return nil, err
end
if not waitsocket(obj, 5.0) then
return nil, 'handshake timeout'
end
end
local userauth_list
while true do
userauth_list, err = session:userauth_list(username)
if userauth_list or err == '' then break end
if session:last_errno() ~= ssh.ERROR_EAGAIN then
return nil, err
end
if not waitsocket(obj, 5.0) then
return nil, 'userauth_list timeout'
end
end
if userauth_list and userauth_list:match('password') then
while true do
ok, err = session:userauth_password(username, password)
if ok then break end
if err ~= ssh.ERROR_EAGAIN then
err = session:last_error()
return nil, err
end
if not waitsocket(obj, 5.0) then
return nil, 'authentication timeout'
end
end
end
return setmetatable(obj, session_metatable)
end
return M