41 using std::ostringstream;
43 #include "PPTServer.h" 44 #include "ServerExitConditions.h" 45 #include "BESInternalError.h" 46 #include "BESInternalFatalError.h" 47 #include "BESSyntaxUserError.h" 48 #include "PPTProtocol.h" 49 #include "SocketListener.h" 50 #include "ServerHandler.h" 52 #include "TheBESKeys.h" 56 #if defined HAVE_OPENSSL && defined NOTTHERE 57 #include "SSLServer.h" 60 #define PPT_SERVER_DEFAULT_TIMEOUT 1 63 PPTConnection(PPT_SERVER_DEFAULT_TIMEOUT), _handler(handler), _listener(listener), _secure(isSecure),
64 _securePort(0), d_num_children(0)
67 string err(
"Null handler passed to PPTServer");
71 string err(
"Null listener passed to PPTServer");
74 #if !defined HAVE_OPENSSL && defined NOTTHERE 77 string err(
"Server requested to be secure but OpenSSL is not built in");
88 PPTServer::~PPTServer()
92 void PPTServer::get_secure_files()
96 if (!found || _cfile.empty()) {
97 string err =
"Unable to determine server certificate file.";
103 if (!found || _cafile.empty()) {
104 string err =
"Unable to determine server certificate authority file.";
110 if (!found || _kfile.empty()) {
111 string err =
"Unable to determine server key file.";
118 if (!found || portstr.empty()) {
119 string err =
"Unable to determine secure connection port.";
122 _securePort = atoi(portstr.c_str());
124 string err = (string)
"Unable to determine secure connection port " +
"from string " + portstr;
136 _mySock = _listener->accept();
139 if (_mySock->allowConnection() ==
true) {
141 BESDEBUG(
"ppt2",
"PPTServer::initConnection() - Calling welcomeClient()" << endl);
142 if (welcomeClient() != -1) {
145 BESDEBUG(
"ppt2",
"PPTServer; number of children: " << get_num_children() << endl);
148 _handler->handle(
this);
158 BESDEBUG(
"ppt2",
"PPTServer::initConnection() - allowConnection() is FALSE! Closing Socket. " << endl);
164 void PPTServer::closeConnection()
166 if (_mySock) _mySock->close();
169 int PPTServer::welcomeClient()
171 const unsigned int ppt_buffer_size = 64;
172 char inBuff[ppt_buffer_size + 1];
187 int bytesRead = readBuffer(inBuff, ppt_buffer_size);
189 BESDEBUG(
"ppt2",
"In welcomeClient; bytesRead: " << bytesRead << endl);
192 if (bytesRead == -1) {
197 string status(inBuff, bytesRead);
199 if (status != PPTProtocol::PPTCLIENT_TESTING_CONNECTION) {
205 string err =
"PPT cannot negotiate, client started the connection with " + status;
207 BESDEBUG(
"ppt",
"Sent '" << err <<
"' to PPT client." << endl);
220 send(PPTProtocol::PPTSERVER_CONNECTION_OK);
221 BESDEBUG(
"ppt",
"Sent " << PPTProtocol::PPTSERVER_CONNECTION_OK <<
" to PPT client." << endl);
224 authenticateClient();
230 void PPTServer::authenticateClient()
232 #if defined HAVE_OPENSSL && defined NOTTHERE 233 BESDEBUG(
"ppt",
"requiring secure connection: port = " << _securePort << endl );
235 send( PPTProtocol::PPTSERVER_AUTHENTICATE );
240 const unsigned int ppt_buffer_size = 64;
242 char inBuff[ppt_buffer_size];
243 int bytesRead = _mySock->receive( inBuff, ppt_buffer_size );
244 string portRequest( inBuff, bytesRead );
246 if( portRequest != PPTProtocol::PPTCLIENT_REQUEST_AUTHPORT )
247 throw BESInternalError(
string(
"Secure connection ... expecting request for port client requested ") + portRequest, __FILE__, __LINE__ );
250 ostringstream portResponse;
251 portResponse << _securePort << PPTProtocol::PPT_COMPLETE_DATA_TRANSMITION;
252 send( portResponse.str() );
255 SSLServer server( _securePort, _cfile, _cafile, _kfile );
256 server.initConnection();
257 server.closeConnection();
262 throw BESInternalError(
"Authentication requested for this server but OpenSSL is not built into the server", __FILE__, __LINE__);
274 strm << BESIndent::LMarg <<
"PPTServer::dump - (" << (
void *)
this <<
")" << endl;
277 strm << BESIndent::LMarg <<
"server handler:" << endl;
279 _handler->dump(strm);
280 BESIndent::UnIndent();
283 strm << BESIndent::LMarg <<
"server handler: null" << endl;
286 strm << BESIndent::LMarg <<
"listener:" << endl;
288 _listener->dump(strm);
289 BESIndent::UnIndent();
292 strm << BESIndent::LMarg <<
"listener: null" << endl;
294 strm << BESIndent::LMarg <<
"secure? " << _secure << endl;
297 strm << BESIndent::LMarg <<
"cert file: " << _cfile << endl;
298 strm << BESIndent::LMarg <<
"cert authority file: " << _cafile << endl;
299 strm << BESIndent::LMarg <<
"key file: " << _kfile << endl;
300 strm << BESIndent::LMarg <<
"secure port: " << _securePort << endl;
301 BESIndent::UnIndent();
304 BESIndent::UnIndent();
exception thrown if inernal error encountered
error thrown if there is a user syntax error in the request or any other user error ...
virtual void initConnection()
void get_value(const string &s, string &val, bool &found)
Retrieve the value of a given key, if set.
virtual void dump(ostream &strm) const
dumps information about this object
static BESKeys * TheKeys()
virtual void dump(ostream &strm) const
dumps information about this object