41 using std::ostringstream;
45 #include "PPTServer.h"
46 #include "ServerExitConditions.h"
47 #include "BESInternalError.h"
48 #include "BESInternalFatalError.h"
49 #include "BESSyntaxUserError.h"
50 #include "PPTProtocol.h"
51 #include "SocketListener.h"
52 #include "ServerHandler.h"
54 #include "TheBESKeys.h"
58 #if defined HAVE_OPENSSL && defined NOTTHERE
59 #include "SSLServer.h"
62 #define PPT_SERVER_DEFAULT_TIMEOUT 1
65 PPTConnection(PPT_SERVER_DEFAULT_TIMEOUT), _handler(handler), _listener(listener), _secure(isSecure),
66 _securePort(0), d_num_children(0)
69 string err(
"Null handler passed to PPTServer");
73 string err(
"Null listener passed to PPTServer");
76 #if !defined HAVE_OPENSSL && defined NOTTHERE
79 string err(
"Server requested to be secure but OpenSSL is not built in");
90 PPTServer::~PPTServer()
94 void PPTServer::get_secure_files()
98 if (!found || _cfile.empty()) {
99 string err =
"Unable to determine server certificate file.";
105 if (!found || _cafile.empty()) {
106 string err =
"Unable to determine server certificate authority file.";
112 if (!found || _kfile.empty()) {
113 string err =
"Unable to determine server key file.";
120 if (!found || portstr.empty()) {
121 string err =
"Unable to determine secure connection port.";
124 _securePort = atoi(portstr.c_str());
126 string err = (string)
"Unable to determine secure connection port " +
"from string " + portstr;
138 _mySock = _listener->
accept();
141 if (_mySock->allowConnection() ==
true) {
143 BESDEBUG(
"ppt2",
"PPTServer::initConnection() - Calling welcomeClient()" << endl);
144 if (welcomeClient() != -1) {
147 BESDEBUG(
"ppt2",
"PPTServer; number of children: " << get_num_children() << endl);
150 _handler->handle(
this);
160 BESDEBUG(
"ppt2",
"PPTServer::initConnection() - allowConnection() is FALSE! Closing Socket. " << endl);
166 void PPTServer::closeConnection()
168 if (_mySock) _mySock->close();
171 int PPTServer::welcomeClient()
173 const unsigned int ppt_buffer_size = 64;
174 char inBuff[ppt_buffer_size + 1];
189 int bytesRead =
readBuffer(inBuff, ppt_buffer_size);
191 BESDEBUG(
"ppt2",
"In welcomeClient; bytesRead: " << bytesRead << endl);
194 if (bytesRead == -1) {
199 string status(inBuff, bytesRead);
201 if (status != PPTProtocol::PPTCLIENT_TESTING_CONNECTION) {
207 string err =
"PPT cannot negotiate, client started the connection with " + status;
209 BESDEBUG(
"ppt",
"Sent '" << err <<
"' to PPT client." << endl);
222 send(PPTProtocol::PPTSERVER_CONNECTION_OK);
223 BESDEBUG(
"ppt",
"Sent " << PPTProtocol::PPTSERVER_CONNECTION_OK <<
" to PPT client." << endl);
226 authenticateClient();
232 void PPTServer::authenticateClient()
234 #if defined HAVE_OPENSSL && defined NOTTHERE
235 BESDEBUG(
"ppt",
"requiring secure connection: port = " << _securePort << endl );
237 send( PPTProtocol::PPTSERVER_AUTHENTICATE );
242 const unsigned int ppt_buffer_size = 64;
244 char inBuff[ppt_buffer_size];
245 int bytesRead = _mySock->receive( inBuff, ppt_buffer_size );
246 string portRequest( inBuff, bytesRead );
248 if( portRequest != PPTProtocol::PPTCLIENT_REQUEST_AUTHPORT )
249 throw BESInternalError(
string(
"Secure connection ... expecting request for port client requested ") + portRequest, __FILE__, __LINE__ );
252 ostringstream portResponse;
253 portResponse << _securePort << PPTProtocol::PPT_COMPLETE_DATA_TRANSMITION;
254 send( portResponse.str() );
257 SSLServer server( _securePort, _cfile, _cafile, _kfile );
258 server.initConnection();
259 server.closeConnection();
264 throw BESInternalError(
"Authentication requested for this server but OpenSSL is not built into the server", __FILE__, __LINE__);
276 strm << BESIndent::LMarg <<
"PPTServer::dump - (" << (
void *)
this <<
")" << endl;
279 strm << BESIndent::LMarg <<
"server handler:" << endl;
281 _handler->
dump(strm);
282 BESIndent::UnIndent();
285 strm << BESIndent::LMarg <<
"server handler: null" << endl;
288 strm << BESIndent::LMarg <<
"listener:" << endl;
290 _listener->
dump(strm);
291 BESIndent::UnIndent();
294 strm << BESIndent::LMarg <<
"listener: null" << endl;
296 strm << BESIndent::LMarg <<
"secure? " << _secure << endl;
299 strm << BESIndent::LMarg <<
"cert file: " << _cfile << endl;
300 strm << BESIndent::LMarg <<
"cert authority file: " << _cafile << endl;
301 strm << BESIndent::LMarg <<
"key file: " << _kfile << endl;
302 strm << BESIndent::LMarg <<
"secure port: " << _securePort << endl;
303 BESIndent::UnIndent();
306 BESIndent::UnIndent();