forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFTPSClientSession.cpp
More file actions
129 lines (110 loc) · 2.92 KB
/
FTPSClientSession.cpp
File metadata and controls
129 lines (110 loc) · 2.92 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
//
// FTPSClientSession.cpp
//
// Library: Net
// Package: FTP
// Module: FTPSClientSession
//
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Net/FTPSClientSession.h"
#include "Poco/Net/SecureStreamSocket.h"
#include "Poco/Net/SecureStreamSocketImpl.h"
#include "Poco/Net/SSLManager.h"
#include "Poco/Net/NetException.h"
namespace Poco {
namespace Net {
FTPSClientSession::FTPSClientSession() :
FTPClientSession()
{
}
FTPSClientSession::~FTPSClientSession()
{
}
FTPSClientSession::FTPSClientSession(const StreamSocket& socket) :
FTPClientSession(socket)
{
}
FTPSClientSession::FTPSClientSession(const std::string& host,
Poco::UInt16 port,
const std::string& username,
const std::string& password) :
FTPClientSession(host, port, username, password)
{
}
void FTPSClientSession::tryFTPSmode(bool bTryFTPS)
{
_bTryFTPS = bTryFTPS;
}
void FTPSClientSession::beforeCreateDataSocket()
{
if (_bSecureDataConnection)
return;
_bSecureDataConnection = false;
if (!_pControlSocket->secure())
return;
std::string sResponse;
int status = sendCommand("PBSZ 0", sResponse);
if (isPositiveCompletion(status))
{
status = sendCommand("PROT P", sResponse);
if (isPositiveCompletion(status))
_bSecureDataConnection = true;
}
}
void FTPSClientSession::afterCreateControlSocket()
{
if (!_bTryFTPS)
return;
_pControlSocket->setNoDelay(true);
if (_pControlSocket->secure())
return;
std::string sResponse;
int status = sendCommand("AUTH TLS", sResponse);
if (!isPositiveCompletion(status))
status = sendCommand("AUTH SSL", sResponse);
if (isPositiveCompletion(status))
{
//Server support FTPS
try
{
Poco::Net::SecureStreamSocket sss(Poco::Net::SecureStreamSocket::attach(*_pControlSocket, Poco::Net::SSLManager::instance().defaultClientContext()));
*_pControlSocket = sss;
}
catch (Poco::Exception&)
{
throw;
}
}
else
{
_bTryFTPS = false;
}
}
StreamSocket FTPSClientSession::establishDataConnection(const std::string& command, const std::string& arg)
{
beforeCreateDataSocket();
StreamSocket ss = FTPClientSession::establishDataConnection(command, arg);
ss.setNoDelay(true);
//SSL nogotiating of data socket
if ((_bSecureDataConnection) && (_pControlSocket->secure()))
{
//We need to reuse the control socket SSL session so the server can ensure that client that opened control socket is the same using data socket
Poco::Net::SecureStreamSocketImpl* pSecure = dynamic_cast<Poco::Net::SecureStreamSocketImpl*>(_pControlSocket->impl());
if (pSecure != nullptr)
{
Poco::Net::SecureStreamSocket sss(Poco::Net::SecureStreamSocket::attach(ss, pSecure->context(), pSecure->currentSession()));
ss = sss;
}
}
return ss;
}
void FTPSClientSession::receiveServerReadyReply()
{
FTPClientSession::receiveServerReadyReply();
afterCreateControlSocket();
}
}} // namespace Poco::Net