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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
|
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2006 Tatsuhiro Tsujikawa
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
/* copyright --> */
#include "FtpConnection.h"
#include <array>
#include <cstring>
#include <cstdio>
#include <cassert>
#include "Request.h"
#include "Segment.h"
#include "Option.h"
#include "util.h"
#include "message.h"
#include "prefs.h"
#include "LogFactory.h"
#include "Logger.h"
#include "AuthConfigFactory.h"
#include "AuthConfig.h"
#include "DlRetryEx.h"
#include "DlAbortEx.h"
#include "SocketCore.h"
#include "A2STR.h"
#include "fmt.h"
#include "AuthConfig.h"
#include "a2functional.h"
#include "util.h"
#include "error_code.h"
namespace aria2 {
FtpConnection::FtpConnection(cuid_t cuid,
const std::shared_ptr<SocketCore>& socket,
const std::shared_ptr<Request>& req,
const std::shared_ptr<AuthConfig>& authConfig,
const Option* op)
: cuid_(cuid),
socket_(socket),
req_(req),
authConfig_(authConfig),
option_(op),
socketBuffer_(socket),
baseWorkingDir_("/")
{
}
FtpConnection::~FtpConnection() = default;
bool FtpConnection::sendUser()
{
if (socketBuffer_.sendBufferIsEmpty()) {
std::string request = "USER ";
request += authConfig_->getUser();
request += "\r\n";
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, "USER ********"));
socketBuffer_.pushStr(std::move(request));
}
socketBuffer_.send();
return socketBuffer_.sendBufferIsEmpty();
}
bool FtpConnection::sendPass()
{
if (socketBuffer_.sendBufferIsEmpty()) {
std::string request = "PASS ";
request += authConfig_->getPassword();
request += "\r\n";
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, "PASS ********"));
socketBuffer_.pushStr(std::move(request));
}
socketBuffer_.send();
return socketBuffer_.sendBufferIsEmpty();
}
bool FtpConnection::sendType()
{
if (socketBuffer_.sendBufferIsEmpty()) {
std::string request = "TYPE ";
request += (option_->get(PREF_FTP_TYPE) == V_ASCII ? 'A' : 'I');
request += "\r\n";
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str()));
socketBuffer_.pushStr(std::move(request));
}
socketBuffer_.send();
return socketBuffer_.sendBufferIsEmpty();
}
bool FtpConnection::sendPwd()
{
if (socketBuffer_.sendBufferIsEmpty()) {
std::string request = "PWD\r\n";
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str()));
socketBuffer_.pushStr(std::move(request));
}
socketBuffer_.send();
return socketBuffer_.sendBufferIsEmpty();
}
bool FtpConnection::sendCwd(const std::string& dir)
{
if (socketBuffer_.sendBufferIsEmpty()) {
std::string request = "CWD ";
request += util::percentDecode(dir.begin(), dir.end());
request += "\r\n";
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str()));
socketBuffer_.pushStr(std::move(request));
}
socketBuffer_.send();
return socketBuffer_.sendBufferIsEmpty();
}
bool FtpConnection::sendMdtm()
{
if (socketBuffer_.sendBufferIsEmpty()) {
std::string request = "MDTM ";
request +=
util::percentDecode(req_->getFile().begin(), req_->getFile().end());
request += "\r\n";
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str()));
socketBuffer_.pushStr(std::move(request));
}
socketBuffer_.send();
return socketBuffer_.sendBufferIsEmpty();
}
bool FtpConnection::sendSize()
{
if (socketBuffer_.sendBufferIsEmpty()) {
std::string request = "SIZE ";
request +=
util::percentDecode(req_->getFile().begin(), req_->getFile().end());
request += "\r\n";
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str()));
socketBuffer_.pushStr(std::move(request));
}
socketBuffer_.send();
return socketBuffer_.sendBufferIsEmpty();
}
bool FtpConnection::sendEpsv()
{
if (socketBuffer_.sendBufferIsEmpty()) {
std::string request("EPSV\r\n");
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str()));
socketBuffer_.pushStr(std::move(request));
}
socketBuffer_.send();
return socketBuffer_.sendBufferIsEmpty();
}
bool FtpConnection::sendPasv()
{
if (socketBuffer_.sendBufferIsEmpty()) {
std::string request("PASV\r\n");
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str()));
socketBuffer_.pushStr(std::move(request));
}
socketBuffer_.send();
return socketBuffer_.sendBufferIsEmpty();
}
std::shared_ptr<SocketCore> FtpConnection::createServerSocket()
{
auto endpoint = socket_->getAddrInfo();
auto serverSocket = std::make_shared<SocketCore>();
serverSocket->bind(endpoint.addr.c_str(), 0, AF_UNSPEC);
serverSocket->beginListen();
return serverSocket;
}
bool FtpConnection::sendEprt(const std::shared_ptr<SocketCore>& serverSocket)
{
if (socketBuffer_.sendBufferIsEmpty()) {
auto endpoint = serverSocket->getAddrInfo();
auto request =
fmt("EPRT |%d|%s|%u|\r\n", endpoint.family == AF_INET ? 1 : 2,
endpoint.addr.c_str(), endpoint.port);
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str()));
socketBuffer_.pushStr(std::move(request));
}
socketBuffer_.send();
return socketBuffer_.sendBufferIsEmpty();
}
bool FtpConnection::sendPort(const std::shared_ptr<SocketCore>& serverSocket)
{
if (socketBuffer_.sendBufferIsEmpty()) {
auto endpoint = socket_->getAddrInfo();
int ipaddr[4];
sscanf(endpoint.addr.c_str(), "%d.%d.%d.%d", &ipaddr[0], &ipaddr[1],
&ipaddr[2], &ipaddr[3]);
auto svEndpoint = serverSocket->getAddrInfo();
auto request =
fmt("PORT %d,%d,%d,%d,%d,%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2],
ipaddr[3], svEndpoint.port / 256, svEndpoint.port % 256);
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str()));
socketBuffer_.pushStr(std::move(request));
}
socketBuffer_.send();
return socketBuffer_.sendBufferIsEmpty();
}
bool FtpConnection::sendRest(const std::shared_ptr<Segment>& segment)
{
if (socketBuffer_.sendBufferIsEmpty()) {
std::string request =
fmt("REST %" PRId64 "\r\n", segment ? segment->getPositionToWrite()
: static_cast<int64_t>(0LL));
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str()));
socketBuffer_.pushStr(std::move(request));
}
socketBuffer_.send();
return socketBuffer_.sendBufferIsEmpty();
}
bool FtpConnection::sendRetr()
{
if (socketBuffer_.sendBufferIsEmpty()) {
std::string request = "RETR ";
request +=
util::percentDecode(req_->getFile().begin(), req_->getFile().end());
request += "\r\n";
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str()));
socketBuffer_.pushStr(std::move(request));
}
socketBuffer_.send();
return socketBuffer_.sendBufferIsEmpty();
}
int FtpConnection::getStatus(const std::string& response) const
{
int status;
// When the response is not like "%d %*s",
// we return 0.
if (response.find_first_not_of("0123456789") != 3 ||
!(response.find(" ") == 3 || response.find("-") == 3)) {
return 0;
}
if (sscanf(response.c_str(), "%d %*s", &status) == 1) {
return status;
}
else {
return 0;
}
}
// Returns the length of the response if the whole response has been received.
// The length includes \r\n.
// If the whole response has not been received, then returns std::string::npos.
std::string::size_type
FtpConnection::findEndOfResponse(int status, const std::string& buf) const
{
if (buf.size() <= 4) {
return std::string::npos;
}
// if 4th character of buf is '-', then multi line response is expected.
if (buf.at(3) == '-') {
// multi line response
std::string::size_type p;
p = buf.find(fmt("\r\n%d ", status));
if (p == std::string::npos) {
return std::string::npos;
}
p = buf.find("\r\n", p + 6);
if (p == std::string::npos) {
return std::string::npos;
}
else {
return p + 2;
}
}
else {
// single line response
std::string::size_type p = buf.find("\r\n");
if (p == std::string::npos) {
return std::string::npos;
}
else {
return p + 2;
}
}
}
bool FtpConnection::bulkReceiveResponse(std::pair<int, std::string>& response)
{
std::array<char, 1_k> buf;
while (1) {
size_t size = buf.size();
socket_->readData(buf.data(), size);
if (size == 0) {
if (socket_->wantRead() || socket_->wantWrite()) {
break;
}
else {
throw DL_RETRY_EX(EX_GOT_EOF);
}
}
if (strbuf_.size() + size > MAX_RECV_BUFFER) {
throw DL_RETRY_EX(fmt("Max FTP recv buffer reached. length=%lu",
static_cast<unsigned long>(strbuf_.size() + size)));
}
strbuf_.append(std::begin(buf), std::begin(buf) + size);
}
int status;
if (strbuf_.size() >= 4) {
status = getStatus(strbuf_);
if (status == 0) {
throw DL_ABORT_EX2(EX_INVALID_RESPONSE, error_code::FTP_PROTOCOL_ERROR);
}
}
else {
return false;
}
std::string::size_type length;
if ((length = findEndOfResponse(status, strbuf_)) != std::string::npos) {
response.first = status;
response.second.assign(strbuf_.begin(), strbuf_.begin() + length);
A2_LOG_INFO(fmt(MSG_RECEIVE_RESPONSE, cuid_, response.second.c_str()));
strbuf_.erase(0, length);
return true;
}
else {
// didn't receive response fully.
return false;
}
}
int FtpConnection::receiveResponse()
{
std::pair<int, std::string> response;
if (bulkReceiveResponse(response)) {
return response.first;
}
else {
return 0;
}
}
#ifdef __MINGW32__
# define LONGLONG_PRINTF "%I64d"
# define ULONGLONG_PRINTF "%I64u"
# define LONGLONG_SCANF "%I64d"
# define ULONGLONG_SCANF "%I64u"
#else
# define LONGLONG_PRINTF "%" PRId64 ""
# define ULONGLONG_PRINTF "%llu"
# define LONGLONG_SCANF "%Ld"
// Mac OSX uses "%llu" for 64bits integer.
# define ULONGLONG_SCANF "%Lu"
#endif // __MINGW32__
int FtpConnection::receiveSizeResponse(int64_t& size)
{
std::pair<int, std::string> response;
if (bulkReceiveResponse(response)) {
if (response.first == 213) {
auto rp = util::divide(std::begin(response.second),
std::end(response.second), ' ');
if (!util::parseLLIntNoThrow(
size, std::string(rp.second.first, rp.second.second)) ||
size < 0) {
throw DL_ABORT_EX("Size must be positive integer");
}
}
return response.first;
}
else {
return 0;
}
}
namespace {
template <typename T>
bool getInteger(T* dest, const char* first, const char* last)
{
int res = 0;
// We assume *dest won't overflow.
for (; first != last; ++first) {
if (util::isDigit(*first)) {
res *= 10;
res += (*first) - '0';
}
else {
return false;
}
}
*dest = res;
return true;
}
} // namespace
int FtpConnection::receiveMdtmResponse(Time& time)
{
// MDTM command, specified in RFC3659.
std::pair<int, std::string> response;
if (bulkReceiveResponse(response)) {
if (response.first == 213) {
char buf[15]; // YYYYMMDDhhmmss+\0, milli second part is dropped.
sscanf(response.second.c_str(), "%*u %14s", buf);
if (strlen(buf) == 14) {
// We don't use Time::parse(buf,"%Y%m%d%H%M%S") here because Mac OS X
// and included strptime doesn't parse data for this format.
struct tm tm;
memset(&tm, 0, sizeof(tm));
if (getInteger(&tm.tm_sec, &buf[12], &buf[14]) &&
getInteger(&tm.tm_min, &buf[10], &buf[12]) &&
getInteger(&tm.tm_hour, &buf[8], &buf[10]) &&
getInteger(&tm.tm_mday, &buf[6], &buf[8]) &&
getInteger(&tm.tm_mon, &buf[4], &buf[6]) &&
getInteger(&tm.tm_year, &buf[0], &buf[4])) {
tm.tm_mon -= 1;
tm.tm_year -= 1900;
time = Time(timegm(&tm));
}
else {
time = Time::null();
}
}
else {
time = Time::null();
}
}
return response.first;
}
else {
return 0;
}
}
int FtpConnection::receiveEpsvResponse(uint16_t& port)
{
std::pair<int, std::string> response;
if (bulkReceiveResponse(response)) {
if (response.first == 229) {
port = 0;
std::string::size_type leftParen = response.second.find("(");
std::string::size_type rightParen = response.second.find(")");
if (leftParen == std::string::npos || rightParen == std::string::npos ||
leftParen > rightParen) {
return response.first;
}
std::vector<Scip> rd;
util::splitIter(response.second.begin() + leftParen + 1,
response.second.begin() + rightParen,
std::back_inserter(rd), '|', true, true);
uint32_t portTemp = 0;
if (rd.size() == 5 &&
util::parseUIntNoThrow(portTemp,
std::string(rd[3].first, rd[3].second))) {
if (0 < portTemp && portTemp <= UINT16_MAX) {
port = portTemp;
}
}
}
return response.first;
}
else {
return 0;
}
}
int FtpConnection::receivePasvResponse(std::pair<std::string, uint16_t>& dest)
{
std::pair<int, std::string> response;
if (bulkReceiveResponse(response)) {
if (response.first == 227) {
// we assume the format of response is "227 Entering Passive
// Mode (h1,h2,h3,h4,p1,p2)."
int h1, h2, h3, h4, p1, p2;
std::string::size_type p = response.second.find("(");
if (p >= 4) {
sscanf(response.second.c_str() + p, "(%d,%d,%d,%d,%d,%d).", &h1, &h2,
&h3, &h4, &p1, &p2);
// ip address
dest.first = fmt("%d.%d.%d.%d", h1, h2, h3, h4);
// port number
dest.second = 256 * p1 + p2;
}
else {
throw DL_RETRY_EX(EX_INVALID_RESPONSE);
}
}
return response.first;
}
else {
return 0;
}
}
int FtpConnection::receivePwdResponse(std::string& pwd)
{
std::pair<int, std::string> response;
if (bulkReceiveResponse(response)) {
if (response.first == 257) {
std::string::size_type first;
std::string::size_type last;
if ((first = response.second.find("\"")) != std::string::npos &&
(last = response.second.find("\"", ++first)) != std::string::npos) {
pwd.assign(response.second.begin() + first,
response.second.begin() + last);
}
else {
throw DL_ABORT_EX2(EX_INVALID_RESPONSE, error_code::FTP_PROTOCOL_ERROR);
}
}
return response.first;
}
else {
return 0;
}
}
void FtpConnection::setBaseWorkingDir(const std::string& baseWorkingDir)
{
baseWorkingDir_ = baseWorkingDir;
}
const std::string& FtpConnection::getUser() const
{
return authConfig_->getUser();
}
} // namespace aria2
|