-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathPPTServer.cc
More file actions
306 lines (264 loc) · 9.7 KB
/
PPTServer.cc
File metadata and controls
306 lines (264 loc) · 9.7 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
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
// PPTServer.cc
// This file is part of bes, A C++ back-end server implementation framework
// for the OPeNDAP Data Access Protocol.
// Copyright (c) 2004-2009 University Corporation for Atmospheric Research
// Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
// You can contact University Corporation for Atmospheric Research at
// 3080 Center Green Drive, Boulder, CO 80301
// (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
// Please read the full copyright statement in the file COPYRIGHT_UCAR.
//
// Authors:
// pwest Patrick West <pwest@ucar.edu>
// jgarcia Jose Garcia <jgarcia@ucar.edu>
#include "config.h"
#include <unistd.h>
#include <string>
#include <sstream>
#include <cstdlib>
#include "PPTServer.h"
#include "ServerExitConditions.h"
#include "BESInternalError.h"
#include "BESInternalFatalError.h"
#include "BESSyntaxUserError.h"
#include "PPTProtocolNames.h"
#include "SocketListener.h"
#include "ServerHandler.h"
#include "Socket.h"
#include "TheBESKeys.h"
#include "BESLog.h"
#include "BESDebug.h"
using std::string;
using std::ostringstream;
using std::endl;
using std::ostream;
#if defined HAVE_OPENSSL && defined NOTTHERE
#include "SSLServer.h"
#endif
#define MODULE "ppt"
#define prolog string("PPTServer::").append(__func__).append("() - ")
#define PPT_SERVER_DEFAULT_TIMEOUT 1
PPTServer::PPTServer(ServerHandler *handler, SocketListener *listener, bool isSecure) :
PPTConnection(PPT_SERVER_DEFAULT_TIMEOUT), _handler(handler), _listener(listener), _secure(isSecure)
{
if (!handler) {
string err("Null handler passed to PPTServer");
throw BESInternalError(err, __FILE__, __LINE__);
}
if (!listener) {
string err("Null listener passed to PPTServer");
throw BESInternalError(err, __FILE__, __LINE__);
}
#if !defined HAVE_OPENSSL && defined NOTTHERE
if( _secure )
{
string err("Server requested to be secure but OpenSSL is not built in");
throw BESInternalError( err, __FILE__, __LINE__ );
}
#endif
// get the certificate and key file information
if (_secure) {
get_secure_files();
}
}
void PPTServer::get_secure_files()
{
bool found = false;
TheBESKeys::TheKeys()->get_value("BES.ServerCertFile", _cfile, found);
if (!found || _cfile.empty()) {
string err = "Unable to determine server certificate file.";
throw BESSyntaxUserError(err, __FILE__, __LINE__);
}
found = false;
TheBESKeys::TheKeys()->get_value("BES.ServerCertAuthFile", _cafile, found);
if (!found || _cafile.empty()) {
string err = "Unable to determine server certificate authority file.";
throw BESSyntaxUserError(err, __FILE__, __LINE__);
}
found = false;
TheBESKeys::TheKeys()->get_value("BES.ServerKeyFile", _kfile, found);
if (!found || _kfile.empty()) {
string err = "Unable to determine server key file.";
throw BESSyntaxUserError(err, __FILE__, __LINE__);
}
found = false;
string portstr;
TheBESKeys::TheKeys()->get_value("BES.ServerSecurePort", portstr, found);
if (!found || portstr.empty()) {
string err = "Unable to determine secure connection port.";
throw BESSyntaxUserError(err, __FILE__, __LINE__);
}
_securePort = atoi(portstr.c_str());
if (!_securePort) {
string err = (string) "Unable to determine secure connection port " + "from string " + portstr;
throw BESSyntaxUserError(err, __FILE__, __LINE__);
}
}
/** Using the info passed into the SocketLister, wait for an inbound
request (see SocketListener::accept()). When one is found, do the
welcome message stuff (welcomeClient()) and then pass \c this to
the handler's \c handle method. Note that \c this is a pointer to
a PPTServer which is a kind of Connection. */
void PPTServer::initConnection()
{
_mySock = _listener->accept();
if (_mySock) {
if (_mySock->allowConnection() == true) {
// welcome the client
BESDEBUG(MODULE, prolog << "Calling welcomeClient()" << endl);
if (welcomeClient() != -1) {
incr_num_children();
BESDEBUG(MODULE, prolog << "number of children: " << get_num_children() << endl);
// now hand it off to the handler
_handler->handle(this);
// Added this call to close - when the PPTServer class is used by
// a server that gets a number of connections on the same port,
// one per command, not closing the sockets after a command results
// in lots of sockets in the 'CLOSE_WAIT' status.
_mySock->close();
}
}
else {
BESDEBUG(MODULE, prolog << "allowConnection() is FALSE! Closing Socket. " << endl);
_mySock->close();
}
}
}
void PPTServer::closeConnection()
{
if (_mySock) _mySock->close();
}
int PPTServer::welcomeClient()
{
const unsigned int ppt_buffer_size = 64;
char inBuff[ppt_buffer_size + 1];
// Doing a non-blocking read in case the connection is being initiated
// by a non-bes client. Don't want this to block. pcw - 3/5/07
// int bytesRead = _mySock->receive( inBuff, ppt_buffer_size ) ;
//
// We are receiving handshaking tokens, so the buffer doesn't need to be
// all that big. pcw - 05/31/08
// The non-blocking read user here was the cause of problems reported in
// tickets #823, #1525, #1551, #2023 and #2025. Using a blocking read
// fixed the problem. 2/27/14 jhrg.
//
// int bytesRead = readBufferNonBlocking(inBuff, ppt_buffer_size);
int bytesRead = readBuffer(inBuff, ppt_buffer_size);
BESDEBUG(MODULE, prolog << "bytesRead: " << bytesRead << endl);
// if the read of the initial connection fails or blocks, then return
if (bytesRead == -1) {
_mySock->close();
return -1;
}
string status(inBuff, bytesRead);
if (status != PPT_CLIENT_TESTING_CONNECTION) {
/* If cannot negotiate with the client then we don't want to exit
* by throwing an exception, we want to return and let the caller
* clean up the connection
*/
string err = "PPT cannot negotiate, client started the connection with " + status;
send(err);
BESDEBUG(MODULE, prolog << "Sent '" << err << "' to PPT client." << endl);
// I think it would be better to send back a previously defined
// constant like this... but I don't want to break client code.
// jhrg 2/27/14
// send(PPT_PROTOCOL_UNDEFINED);
// BESDEBUG(MODULE, prolog << "Sent " << PPT_PROTOCOL_UNDEFINED << " to PPT client." << endl);
_mySock->close();
return -1;
}
if (!_secure) {
send(PPT_SERVER_CONNECTION_OK);
BESDEBUG(MODULE, prolog << "Sent " << PPT_SERVER_CONNECTION_OK << " to PPT client." << endl);
}
else {
authenticateClient();
}
return 0;
}
void PPTServer::authenticateClient()
{
#if defined HAVE_OPENSSL && defined NOTTHERE
BESDEBUG( MODULE, prolog << "Requiring secure connection: port = " << _securePort << endl );
// let the client know that it needs to authenticate
send(PPT_SERVER_AUTHENTICATE );
// wait for the client request for the secure port
// We are waiting for a ppt tocken requesting the secure port number.
// The buffer doesn't need to be all that big. pcw - 05/31/08
const unsigned int ppt_buffer_size = 64;
// char *inBuff = new char[ppt_buffer_size]; jhrg 3/5/14
char inBuff[ppt_buffer_size];
int bytesRead = _mySock->receive( inBuff, ppt_buffer_size );
string portRequest( inBuff, bytesRead );
// delete [] inBuff; jhrg 3/5/14
if( portRequest != PPT_CLIENT_REQUEST_AUTHPORT )
throw BESInternalError( string("Secure connection ... expecting request for port client requested ") + portRequest, __FILE__, __LINE__ );
// send the secure port number back to the client
ostringstream portResponse;
portResponse << _securePort << PPT_COMPLETE_DATA_TRANSMISSION;
send( portResponse.str() );
// create a secure server object and authenticate
SSLServer server( _securePort, _cfile, _cafile, _kfile );
server.initConnection();
server.closeConnection();
// if it authenticates, good, if not, an exception is thrown, no need to
// do anything else here.
#else
throw BESInternalError("Authentication requested for this server but OpenSSL is not built into the server", __FILE__, __LINE__);
#endif
}
/** @brief dumps information about this object
*
* Displays the pointer value of this instance
*
* @param strm C++ i/o stream to dump the information to
*/
void PPTServer::dump(ostream &strm) const
{
strm << BESIndent::LMarg << "PPTServer::dump - (" << (void *) this << ")" << endl;
BESIndent::Indent();
if (_handler) {
strm << BESIndent::LMarg << "server handler:" << endl;
BESIndent::Indent();
_handler->dump(strm);
BESIndent::UnIndent();
}
else {
strm << BESIndent::LMarg << "server handler: null" << endl;
}
if (_listener) {
strm << BESIndent::LMarg << "listener:" << endl;
BESIndent::Indent();
_listener->dump(strm);
BESIndent::UnIndent();
}
else {
strm << BESIndent::LMarg << "listener: null" << endl;
}
strm << BESIndent::LMarg << "secure? " << _secure << endl;
if (_secure) {
BESIndent::Indent();
strm << BESIndent::LMarg << "cert file: " << _cfile << endl;
strm << BESIndent::LMarg << "cert authority file: " << _cafile << endl;
strm << BESIndent::LMarg << "key file: " << _kfile << endl;
strm << BESIndent::LMarg << "secure port: " << _securePort << endl;
BESIndent::UnIndent();
}
PPTConnection::dump(strm);
BESIndent::UnIndent();
}