-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilezillaadminconnection.cpp
308 lines (264 loc) · 7.08 KB
/
filezillaadminconnection.cpp
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
#include "filezillaadminconnection.h"
#include "md5.h"
#include <memory>
#include <cstdint>
FilezillaAdminConnection::FilezillaAdminConnection(QObject *parent) :
QObject(parent)
{
mSocket = new QTcpSocket();
connect(mSocket, SIGNAL(readyRead()), this, SLOT(bytesToRead()));
connect(mSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError)));
}
FilezillaAdminConnection::~FilezillaAdminConnection()
{
mSocket->abort();
delete mSocket;
}
void FilezillaAdminConnection::connectToHost(const QString &host, int port, const QString &password)
{
mSocket->abort();
mDataRead.clear();
mConnState = INIT;
mPassword = password;
serverDescription.isValid = false;
mSocket->connectToHost(host, port);
}
bool FilezillaAdminConnection::SendCommand(int type, char *data, int32_t length)
{
QByteArray arr;
arr.resize(length+5);
arr[0] = type << 2;
int8_t *p = reinterpret_cast<int8_t*>(&length);
arr[1] = p[0];
arr[2] = p[1];
arr[3] = p[2];
arr[4] = p[3];
if(data)
{
for(int i=0; i<length; i++)
{
arr[i+5] = data[i];
}
}
mSocket->write(arr);
if(!mSocket->waitForBytesWritten())
{
emit connFail("Failed to send bytes");
mSocket->abort();
return false;
}
return true;
}
bool FilezillaAdminConnection::SendCommand(int type)
{
QByteArray arr;
arr.resize(5);
arr[0] = type << 2;
arr[1] = 0;
arr[2] = 0;
arr[3] = 0;
arr[4] = 0;
mSocket->write(arr);
if(!mSocket->waitForBytesWritten())
{
emit connFail("Failed to send bytes");
mSocket->abort();
return false;
}
return false;
}
bool FilezillaAdminConnection::parseData()
{
// Init and auth data are handled by this class
// normal data is parsed and packed and a signal is emitted
// that data is available to be handled.
if(mConnState == INIT)
{
return parseInitData();
}
else if(mConnState == AUTH)
{
return parseAuthData();
}
return parseNormalData();
}
bool FilezillaAdminConnection::parseInitData()
{
int res = serverDescription.Parse(mDataRead);
if(res > 0)
{
removeParsedData(res);
mConnState = AUTH;
return true;
}
return false;
}
bool FilezillaAdminConnection::parseAuthData()
{
if(mDataRead.length() < 5)
{
emit connFail("Data length below 5");
return false;
}
if((mDataRead[0] & 0x03) > 2)
{
emit connFail("Unknown command type");
mSocket->abort();
return false;
}
int len = *reinterpret_cast<const uint32_t*>(&mDataRead.data()[1]);
if(len + 5 <= mDataRead.length())
{
int iCmdId = (mDataRead[0] & 0x7C) >> 2;
int iType = mDataRead[0] & 0x03;
if(iCmdId == 0 && iType == 0)
{
if(len < 4)
{
emit connFail("Invalid auth data");
mSocket->abort();
return false;
}
int nonceLen1 = mDataRead[5]*256+mDataRead[6];
if((nonceLen1+2) > (len-2))
{
emit connFail("Invalid auth data 2");
mSocket->abort();
return false;
}
int nonceLen2 = mDataRead[5+2+nonceLen1]*256 + mDataRead[5+2+1+nonceLen1];
if((nonceLen1+nonceLen2+4) > len)
{
emit connFail("Invalid auth data 3");
mSocket->abort();
return false;
}
MD5 md5;
if(nonceLen1)
{
md5.update(reinterpret_cast<const unsigned char*>(&mDataRead.data()[7]), nonceLen1);
}
md5.update(reinterpret_cast<const unsigned char*>(mPassword.toUtf8().data()), mPassword.length());
if(nonceLen2)
{
md5.update(reinterpret_cast<const unsigned char*>(&mDataRead.data()[5+4+nonceLen1]), nonceLen2);
}
md5.finalize();
//removeParsedData(len-5);
mDataRead.clear();
char *digest = reinterpret_cast<char*>(md5.raw_digest());
SendCommand(0, digest, 16);
mConnState = NORMAL;
}
else if(iType == 1 && iCmdId == 0)
{
mDataRead.clear();
SendCommand(2);
mConnState = NORMAL;
}
else
{
emit connFail("Protocol error, unknown command ID " + QString().setNum(iCmdId));
mSocket->abort();
return false;
}
if(mConnState == NORMAL)
{
emit authSuccess();
}
return true;
}
emit connFail("Invalid len: " + QString().setNum(len));
return false;
}
bool FilezillaAdminConnection::parseNormalData()
{
if(mDataRead.length() < 5)
{
return false;
}
int type = mDataRead[0] & 0x03;
int id = (mDataRead[0] & 0x7C) >> 2;
if(type > 2 || type < 1)
{
emit connFail("Protocol error: unknown command type");
mSocket->abort();
}
else
{
int len = *reinterpret_cast<const int32_t*>(&mDataRead.data()[1]);
if(len > 0xFFFFFF)
{
emit connFail("Invalid data length");
mSocket->abort();
}
else if(len+5 > mDataRead.length())
{
return false;
}
else
{
if(type == 1)
{
FilezillaReply reply;
reply.type = type;
reply.id = id;
reply.length = len;
for(int i=0; i<len; i++)
{
reply.data.append(mDataRead[i+5]);
}
emit replyReceived(reply);;
}
else if(type == 2)
{
QString text = QString::fromUtf8(mDataRead.data()+6, len-1);
if(id == 1)
{
mSocket->abort();
emit connMessage(text);
}
}
else
{
emit connMessage("Command type not implemented");
}
removeParsedData(len+5);
return true;
}
}
return false;
}
void FilezillaAdminConnection::bytesToRead()
{
QByteArray bytes = mSocket->readAll();
mDataRead.append(bytes);
while(parseData())
{
}
}
// TODO: implement
void FilezillaAdminConnection::socketError(QAbstractSocket::SocketError error)
{
switch(error)
{
case QAbstractSocket::ConnectionRefusedError:
emit connFail("Connection refused");
break;
case QAbstractSocket::NetworkError:
emit connFail("Network error");
break;
case QAbstractSocket::SocketTimeoutError:
emit connFail("Connection timeout");
break;
case QAbstractSocket::RemoteHostClosedError:
emit connectionClosed();
break;
default:
break;
}
}
void FilezillaAdminConnection::removeParsedData(int numBytes)
{
mDataRead = mDataRead.right(mDataRead.length()-numBytes);
}