-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnection.cpp
More file actions
263 lines (239 loc) · 7.27 KB
/
Connection.cpp
File metadata and controls
263 lines (239 loc) · 7.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
#include <pty.h>
#include <sstream>
#include <iostream>
#include <errno.h>
#include "Client.h"
#include "Connection.h"
Spatch::Ssh::Connection::Connection(Spatch::Ssh::Client& client, const std::string& host, const int port, ssh_event event)
: _client(client), _host(host), _port(port), _event(event), _masterpty(-1), _slavepty(-1)
{
_session = ssh_new();
ssh_options_set(_session, SSH_OPTIONS_HOST, _host.c_str());
ssh_options_set(_session, SSH_OPTIONS_PORT, &_port);
_callbacks.channel_data_function = channelCallback;
_callbacks.channel_eof_function = NULL;
_callbacks.channel_close_function = chan_close;
_callbacks.userdata = this;
ssh_callbacks_init(&_callbacks);
}
Spatch::Ssh::Connection::~Connection()
{
if (_channel != nullptr)
{
ssh_event_remove_session(_event, ssh_channel_get_session(_channel));
if (!ssh_channel_is_eof(_channel))
ssh_channel_send_eof(_channel);
if (!ssh_channel_is_closed(_channel))
ssh_channel_close(_channel);
ssh_channel_free(_channel);
}
if (_masterpty != -1)
{
ssh_event_remove_fd(_event, _masterpty);
ssh_event_remove_fd(_event, _slavepty);
close(_masterpty);
_masterpty = -1;
_slavepty = -1;
}
}
void Spatch::Ssh::Connection::onClose()
{
_client.proxify(nullptr);
}
void Spatch::Ssh::Connection::chan_close(ssh_session session, ssh_channel channel, void *userdata) {
Spatch::Ssh::Connection *connection = static_cast<Spatch::Ssh::Connection *>(userdata);
return(connection->onClose());
}
bool Spatch::Ssh::Connection::connect(const std::string &username, const std::string &password, const std::string &command)
{
_username = username;
_password = password;
_command = command;
connect();
}
bool Spatch::Ssh::Connection::connect(const std::string &command)
{
if (!command.empty())
_command = command;
if (_username.empty() || _password.empty())
{
askAuth();
return true;
}
else if (_masterpty != -1)
{
ssh_event_remove_fd(_event, _masterpty);
ssh_event_remove_fd(_event, _slavepty);
close(_masterpty);
_masterpty = -1;
_slavepty = -1;
}
int rc;
rc = ssh_connect(_session);
if (rc != SSH_OK)
return false;
rc = ssh_userauth_password(_session, _username.c_str(), _password.c_str());
if (rc != SSH_AUTH_SUCCESS)
return false;
_channel = ssh_channel_new(_session);
if (_channel == NULL)
return false;
ssh_set_channel_callbacks(_channel, &_callbacks);
rc = ssh_channel_open_session(_channel);
if (rc != SSH_OK)
return false;
rc = ssh_channel_request_pty(_channel);
if (rc != SSH_OK)
return false;
rc = ssh_channel_change_pty_size(_channel, 80, 24);
if (rc != SSH_OK)
return false;
ssh_event_add_session(_event, ssh_channel_get_session(_channel));
if (_command.empty())
{
rc = ssh_channel_request_shell(_channel);
if (rc != SSH_OK)
return false;
}
else
{
rc = ssh_channel_request_exec(_channel, _command.c_str());
if (rc != SSH_OK)
return false;
}
return true;
}
int Spatch::Ssh::Connection::writeToChannel(void *data, uint32_t len)
{
if (_masterpty == -1)
return(ssh_channel_write(_channel, data, len));
else
{
if (len == 1 && ((unsigned char *)data)[0] == 0x04)
{
_client.writeToChannel("\n\r", 2);
onClose();
return (1);
}
return (write(_masterpty, data, len));
}
}
bool Spatch::Ssh::Connection::closeChannel()
{
ssh_channel_close(_channel);
return true;
}
int Spatch::Ssh::Connection::channelToClient(void *data, uint32_t len)
{
return(_client.writeToChannel(data, len));
}
int Spatch::Ssh::Connection::channelCallback(ssh_session session,
ssh_channel channel,
void *data,
uint32_t len,
int is_stderr,
void *userdata)
{
Spatch::Ssh::Connection *connection = static_cast<Spatch::Ssh::Connection *>(userdata);
return(connection->channelToClient(data, len));
}
bool Spatch::Ssh::Connection::askAuth()
{
int size;
int rc;
if (_masterpty == -1)
{
rc = openpty(&_masterpty, &_slavepty, NULL, NULL, NULL);
if (rc != 0)
return false;
if(ssh_event_add_fd(_event, _masterpty, POLLIN | POLLPRI | POLLERR | POLLHUP | POLLNVAL, Spatch::Ssh::Connection::fdMasterCallback, this) != SSH_OK)
return false;
if(ssh_event_add_fd(_event, _slavepty, POLLIN | POLLPRI | POLLERR | POLLHUP | POLLNVAL, Spatch::Ssh::Connection::slaveEventCB, this) != SSH_OK)
return false;
}
if (_username.empty())
{
size = write(_slavepty, "login as: ", strlen("login as: "));
if (size <= 0)
return false;
}
else if (_password.empty())
{
struct termios tios;
tcgetattr(_masterpty, &tios);
tios.c_lflag &= ~ECHO;
tcsetattr(_masterpty, TCSANOW, &tios);
std::stringstream ss;
ss << _username << "'s password: ";
size = write(_slavepty, ss.str().c_str(), ss.str().length());
if (size <= 0)
return false;
}
return true;
}
int Spatch::Ssh::Connection::masterPtyToChannel(int revents)
{
char buf[2048];
int sz = 0;
if(revents & POLLIN)
{
sz = read(_masterpty, buf, 2048);
return _client.writeToChannel(buf, sz);
}
return 0;
}
int Spatch::Ssh::Connection::fdMasterCallback(socket_t fd, int revents, void *userdata)
{
Spatch::Ssh::Connection *connection = static_cast<Spatch::Ssh::Connection *>(userdata);
return(connection->masterPtyToChannel(revents));
}
int Spatch::Ssh::Connection::slaveEvent(int revents)
{
char buf[2048];
int sz = 0;
if (revents & POLLHUP)
{
onClose();
return -1;
}
if(revents & POLLIN)
{
sz = read(_slavepty, buf, 2048);
if (sz <= 0)
{
onClose();
return -1;
}
buf[sz - 1] = 0;
if (_username.empty())
{
_username = buf;
if (!connect())
{
onClose();
return -1;
}
}
else
{
_password = buf;
_client.writeToChannel("\n\r", 2);
if (!connect())
{
onClose();
return -1;
}
}
}
return 0;
}
int Spatch::Ssh::Connection::slaveEventCB(socket_t fd, int revents, void *userdata)
{
Spatch::Ssh::Connection *connection = static_cast<Spatch::Ssh::Connection *>(userdata);
return(connection->slaveEvent(revents));
}