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
143 lines (114 loc) · 3.22 KB
/
FTPSClientSession.cpp
File metadata and controls
143 lines (114 loc) · 3.22 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
//
// FTPSClientSession.cpp
//
// Library: NetSSL_OpenSSL
// Package: FTPS
// 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(Context::Ptr pContext):
FTPClientSession(),
_pContext(pContext)
{
}
FTPSClientSession::FTPSClientSession(const StreamSocket& socket, bool readWelcomeMessage, bool enableFTPS, Context::Ptr pContext):
FTPClientSession(socket, readWelcomeMessage),
_enableFTPS(enableFTPS),
_pContext(pContext)
{
}
FTPSClientSession::FTPSClientSession(const std::string& host, Poco::UInt16 port, const std::string& username, const std::string& password, Context::Ptr pContext):
FTPClientSession(host, port, username, password),
_pContext(pContext)
{
}
FTPSClientSession::~FTPSClientSession()
{
}
void FTPSClientSession::enableFTPS(bool enable)
{
_enableFTPS = enable;
}
void FTPSClientSession::beforeCreateDataSocket()
{
if (_secureDataConnection) return;
_secureDataConnection = 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))
{
_secureDataConnection = true;
}
}
}
void FTPSClientSession::afterCreateControlSocket()
{
if (!_enableFTPS) 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
{
if (!_pContext) _pContext = Poco::Net::SSLManager::instance().defaultClientContext();
Poco::Net::SecureStreamSocket sss(Poco::Net::SecureStreamSocket::attach(*_pControlSocket, _pContext));
*_pControlSocket = sss;
}
catch (Poco::Exception&)
{
throw;
}
}
else
{
_enableFTPS = 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 (_secureDataConnection && _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