forked from mscdex/ssh2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-openssh.js
261 lines (236 loc) · 7.14 KB
/
test-openssh.js
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
'use strict';
const assert = require('assert');
const { inspect } = require('util');
const {
fixture,
mustCall,
mustCallAtLeast,
setup: setup_,
} = require('./common.js');
const debug = false;
const clientCfg = { username: 'foo', password: 'bar' };
const serverCfg = { hostKeys: [ fixture('ssh_host_rsa_key') ] };
{
const { client, server } = setup_(
'Exec with OpenSSH agent forwarding',
{
client: {
...clientCfg,
agent: '/path/to/agent',
},
server: serverCfg,
debug,
},
);
server.on('connection', mustCall((conn) => {
conn.on('authentication', mustCall((ctx) => {
ctx.accept();
})).on('ready', mustCall(() => {
conn.on('session', mustCall((accept, reject) => {
let sawAuthAgent = false;
accept().on('auth-agent', mustCall((accept, reject) => {
sawAuthAgent = true;
accept && accept();
})).on('exec', mustCall((accept, reject, info) => {
assert(sawAuthAgent, 'Expected auth-agent before exec');
assert(info.command === 'foo --bar',
`Wrong exec command: ${info.command}`);
const stream = accept();
stream.exit(100);
stream.end();
conn.end();
}));
}));
}));
}));
client.on('ready', mustCall(() => {
client.exec('foo --bar', { agentForward: true }, mustCall((err, stream) => {
assert(!err, `Unexpected exec error: ${err}`);
stream.resume();
}));
}));
}
{
const { client, server } = setup_(
'OpenSSH forwarded UNIX socket connection',
{
client: clientCfg,
server: {
...serverCfg,
ident: 'OpenSSH_7.1',
},
debug,
},
);
const socketPath = '/foo';
const events = [];
const expected = [
['client', 'openssh_forwardInStreamLocal'],
['server', '[email protected]', { socketPath }],
['client', 'forward callback'],
['client', 'unix connection', { socketPath }],
['client', 'socket data', '1'],
['server', 'socket data', '2'],
['client', 'socket end'],
['server', '[email protected]', { socketPath }],
['client', 'cancel callback']
];
server.on('connection', mustCall((conn) => {
conn.on('authentication', mustCall((ctx) => {
ctx.accept();
})).on('ready', mustCall(() => {
conn.once('request', mustCall((accept, reject, name, info) => {
events.push(['server', name, info]);
assert(name === '[email protected]',
`Wrong request name: ${name}`);
accept();
conn.openssh_forwardOutStreamLocal(socketPath,
mustCall((err, ch) => {
assert(!err, `Unexpected error: ${err}`);
ch.write('1');
ch.on('data', mustCallAtLeast((data) => {
events.push(['server', 'socket data', data.toString()]);
ch.close();
}));
}));
conn.on('request', mustCall((accept, reject, name, info) => {
events.push(['server', name, info]);
assert(name === '[email protected]',
`Wrong request name: ${name}`);
accept();
}));
}));
}));
}));
client.on('ready', mustCall(() => {
// request forwarding
events.push(['client', 'openssh_forwardInStreamLocal']);
client.openssh_forwardInStreamLocal(socketPath, mustCall((err) => {
assert(!err, `Unexpected error: ${err}`);
events.push(['client', 'forward callback']);
}));
client.on('unix connection', mustCall((info, accept, reject) => {
events.push(['client', 'unix connection', info]);
const stream = accept();
stream.on('data', mustCallAtLeast((data) => {
events.push(['client', 'socket data', data.toString()]);
stream.write('2');
})).on('end', mustCall(() => {
events.push(['client', 'socket end']);
client.openssh_unforwardInStreamLocal(socketPath,
mustCall((err) => {
assert(!err, `Unexpected error: ${err}`);
events.push(['client', 'cancel callback']);
client.end();
}));
}));
}));
})).on('close', mustCall(() => {
assert.deepStrictEqual(
events,
expected,
'Events mismatch\n'
+ `Actual:\n${inspect(events)}\n`
+ `Expected:\n${inspect(expected)}`
);
}));
}
{
const { client, server } = setup_(
'OpenSSH UNIX socket connection',
{
client: clientCfg,
server: {
...serverCfg,
ident: 'OpenSSH_8.0',
},
debug,
},
);
const socketPath = '/foo/bar/baz';
const response = 'Hello World';
server.on('connection', mustCall((conn) => {
conn.on('authentication', mustCall((ctx) => {
ctx.accept();
})).on('ready', mustCall(() => {
conn.on('openssh.streamlocal', mustCall((accept, reject, info) => {
assert.deepStrictEqual(
info,
{ socketPath },
`Wrong info: ${inspect(info)}`
);
const stream = accept();
stream.on('close', mustCall(() => {
client.end();
})).end(response);
stream.resume();
}));
}));
}));
client.on('ready', mustCall(() => {
client.openssh_forwardOutStreamLocal(socketPath, mustCall((err, stream) => {
assert(!err, `Unexpected error: ${err}`);
let buf = '';
stream.on('data', mustCallAtLeast((data) => {
buf += data;
})).on('close', mustCall(() => {
assert(buf === response, `Wrong response: ${inspect(buf)}`);
}));
}));
}));
}
{
const { client, server } = setup_(
'OpenSSH 5.x workaround for binding on port 0',
{
client: clientCfg,
server: {
...serverCfg,
ident: 'OpenSSH_5.3',
},
debug,
},
);
const boundAddr = 'good';
const boundPort = 1337;
const tcpInfo = {
destIP: boundAddr,
destPort: boundPort,
srcIP: 'remote',
srcPort: 12345,
};
server.on('connection', mustCall((conn) => {
conn.on('authentication', mustCall((ctx) => {
ctx.accept();
})).on('ready', mustCall(() => {
conn.on('request', mustCall((accept, reject, name, info) => {
assert(name === 'tcpip-forward', `Unexpected request: ${name}`);
assert(info.bindAddr === boundAddr, `Wrong addr: ${info.bindAddr}`);
assert(info.bindPort === 0, `Wrong port: ${info.bindPort}`);
accept(boundPort);
conn.forwardOut(boundAddr,
0,
tcpInfo.srcIP,
tcpInfo.srcPort,
mustCall((err, ch) => {
assert(!err, `Unexpected error: ${err}`);
client.end();
}));
}));
}));
}));
client.on('ready', mustCall(() => {
// request forwarding
client.forwardIn(boundAddr, 0, mustCall((err, port) => {
assert(!err, `Unexpected error: ${err}`);
assert(port === boundPort, `Bad bound port: ${port}`);
}));
})).on('tcp connection', mustCall((details, accept, reject) => {
assert.deepStrictEqual(
details,
tcpInfo,
`Wrong tcp details: ${inspect(details)}`
);
accept();
}));
}