OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
PPTServer.cc
Go to the documentation of this file.
1 // PPTServer.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include <config.h>
34 
35 #include <string>
36 #include <sstream>
37 #include <cstdlib>
38 
39 using std::string ;
40 using std::ostringstream ;
41 
42 #include "PPTServer.h"
43 #include "BESInternalError.h"
44 #include "BESSyntaxUserError.h"
45 #include "PPTProtocol.h"
46 #include "SocketListener.h"
47 #include "ServerHandler.h"
48 #include "Socket.h"
49 #include "TheBESKeys.h"
50 #include "BESDebug.h"
51 
52 #include "config.h"
53 #if defined HAVE_OPENSSL && defined NOTTHERE
54 #include "SSLServer.h"
55 #endif
56 
57 #define PPT_SERVER_DEFAULT_TIMEOUT 1
58 
60  SocketListener *listener,
61  bool isSecure )
63  _handler( handler ),
64  _listener( listener ),
65  _secure( isSecure )
66 {
67  if( !handler )
68  {
69  string err( "Null handler passed to PPTServer" ) ;
70  throw BESInternalError( err, __FILE__, __LINE__ ) ;
71  }
72  if( !listener )
73  {
74  string err( "Null listener passed to PPTServer" ) ;
75  throw BESInternalError( err, __FILE__, __LINE__ ) ;
76  }
77 #if !defined HAVE_OPENSSL && defined NOTTHERE
78  if( _secure )
79  {
80  string err("Server requested to be secure but OpenSSL is not built in");
81  throw BESInternalError( err, __FILE__, __LINE__ ) ;
82  }
83 #endif
84 
85  // get the certificate and key file information
86  if( _secure )
87  {
88  get_secure_files() ;
89  }
90 }
91 
93 {
94 }
95 
96 void
97 PPTServer::get_secure_files()
98 {
99  bool found = false ;
100  TheBESKeys::TheKeys()->get_value( "BES.ServerCertFile", _cfile, found ) ;
101  if( !found || _cfile.empty() )
102  {
103  string err = "Unable to determine server certificate file." ;
104  throw BESSyntaxUserError( err, __FILE__, __LINE__ ) ;
105  }
106 
107  found = false ;
108  TheBESKeys::TheKeys()->get_value( "BES.ServerCertAuthFile", _cafile, found);
109  if( !found || _cafile.empty() )
110  {
111  string err = "Unable to determine server certificate authority file." ;
112  throw BESSyntaxUserError( err, __FILE__, __LINE__ ) ;
113  }
114 
115  found = false ;
116  TheBESKeys::TheKeys()->get_value( "BES.ServerKeyFile", _kfile, found ) ;
117  if( !found || _kfile.empty() )
118  {
119  string err = "Unable to determine server key file." ;
120  throw BESSyntaxUserError( err, __FILE__, __LINE__ ) ;
121  }
122 
123  found = false ;
124  string portstr ;
125  TheBESKeys::TheKeys()->get_value( "BES.ServerSecurePort", portstr, found ) ;
126  if( !found || portstr.empty() )
127  {
128  string err = "Unable to determine secure connection port." ;
129  throw BESSyntaxUserError( err, __FILE__, __LINE__ ) ;
130  }
131  _securePort = atoi( portstr.c_str() ) ;
132  if( !_securePort )
133  {
134  string err = (string)"Unable to determine secure connection port "
135  + "from string " + portstr ;
136  throw BESSyntaxUserError( err, __FILE__, __LINE__ ) ;
137  }
138 }
139 
146 {
147  for (;;)
148  {
149  _mySock = _listener->accept();
150  if (_mySock)
151  {
152  if (_mySock->allowConnection() == true)
153  {
154  // welcome the client
155  if (welcomeClient() != -1) {
156  // now hand it off to the handler
157  _handler->handle(this);
158 
159  // Added this call to close - when the PPTServer class is used by
160  // a server that gets a number of connections on the same port,
161  // one per command, not closing the sockets after a command results
162  // in lots of sockets in the 'CLOSE_WAIT' status.
163  _mySock->close();
164  }
165  }
166  else
167  {
168  _mySock->close();
169  }
170  }
171  }
172 }
173 
174 void
176 {
177  if( _mySock )
178  _mySock->close() ;
179 }
180 
181 int PPTServer::welcomeClient()
182 {
183  // Doing a non blocking read in case the connection is being initiated
184  // by a non-bes client. Don't want this to block. pcw - 3/5/07
185  // int bytesRead = _mySock->receive( inBuff, ppt_buffer_size ) ;
186  //
187  // We are receiving handshaking tokens, so the buffer doesn't need to be
188  // all that big. pcw - 05/31/08
189  unsigned int ppt_buffer_size = 64;
190  char *inBuff = new char[ppt_buffer_size + 1];
191  int bytesRead = readBufferNonBlocking(inBuff, ppt_buffer_size);
192 
193  // if the read of the initial connection fails or blocks, then return
194  if (bytesRead == -1)
195  {
196  _mySock->close();
197  delete[] inBuff;
198  return -1;
199  }
200 
201  string status(inBuff, bytesRead);
202  delete[] inBuff;
203 
205  {
206  /* If cannot negotiate with the client then we don't want to exit
207  * by throwing an exception, we want to return and let the caller
208  * clean up the connection
209  */
210  string err("PPT cannot negotiate, ");
211  err += " client started the connection with " + status;
212  BESDEBUG( "ppt", err << endl );
213  //throw BESInternalError( err, __FILE__, __LINE__ ) ;
214  send(err);
215  _mySock->close();
216  return -1;
217  }
218 
219  if (!_secure)
220  {
222  }
223  else
224  {
225  authenticateClient();
226  }
227 
228  return 0;
229 }
230 
231 void
232 PPTServer::authenticateClient()
233 {
234 #if defined HAVE_OPENSSL && defined NOTTHERE
235  BESDEBUG( "ppt", "requiring secure connection: port = "
236  << _securePort << endl ) ;
237  // let the client know that it needs to authenticate
239 
240  // wait for the client request for the secure port
241  // We are waiting for a ppt tocken requesting the secure port number.
242  // The buffer doesn't need to be all that big. pcw - 05/31/08
243  unsigned int ppt_buffer_size = 64 ;
244  char *inBuff = new char[ppt_buffer_size] ;
245  int bytesRead = _mySock->receive( inBuff, ppt_buffer_size ) ;
246  string portRequest( inBuff, bytesRead ) ;
247  delete [] inBuff ;
248  if( portRequest != PPTProtocol::PPTCLIENT_REQUEST_AUTHPORT )
249  {
250  string err( "Secure connection ... expecting request for port" ) ;
251  err += " client requested " + portRequest ;
252  throw BESInternalError( err, __FILE__, __LINE__ ) ;
253  }
254 
255  // send the secure port number back to the client
256  ostringstream portResponse ;
257  portResponse << _securePort << PPTProtocol::PPT_COMPLETE_DATA_TRANSMITION ;
258  send( portResponse.str() ) ;
259 
260  // create a secure server object and authenticate
261  SSLServer server( _securePort, _cfile, _cafile, _kfile ) ;
262  server.initConnection() ;
263  server.closeConnection() ;
264 
265  // if it authenticates, good, if not, an exception is thrown, no need to
266  // do anything else here.
267 #else
268  string err = (string)"Authentication requested for this server "
269  + "but OpenSSL is not built into the server" ;
270  throw BESInternalError( err, __FILE__, __LINE__ ) ;
271 #endif
272 }
273 
280 void
281 PPTServer::dump( ostream &strm ) const
282 {
283  strm << BESIndent::LMarg << "PPTServer::dump - ("
284  << (void *)this << ")" << endl ;
286  if( _handler )
287  {
288  strm << BESIndent::LMarg << "server handler:" << endl ;
290  _handler->dump( strm ) ;
292  }
293  else
294  {
295  strm << BESIndent::LMarg << "server handler: null" << endl ;
296  }
297  if( _listener )
298  {
299  strm << BESIndent::LMarg << "listener:" << endl ;
301  _listener->dump( strm ) ;
303  }
304  else
305  {
306  strm << BESIndent::LMarg << "listener: null" << endl ;
307  }
308  strm << BESIndent::LMarg << "secure? " << _secure << endl ;
309  if( _secure )
310  {
312  strm << BESIndent::LMarg << "cert file: " << _cfile << endl ;
313  strm << BESIndent::LMarg << "cert authority file: " << _cafile << endl ;
314  strm << BESIndent::LMarg << "key file: " << _kfile << endl ;
315  strm << BESIndent::LMarg << "secure port: " << _securePort << endl ;
317  }
318  PPTConnection::dump( strm ) ;
320 }
321 
static string PPTCLIENT_REQUEST_AUTHPORT
Definition: PPTProtocol.h:51
exception thrown if inernal error encountered
static string PPT_COMPLETE_DATA_TRANSMITION
Definition: PPTProtocol.h:46
static string PPTCLIENT_TESTING_CONNECTION
Definition: PPTProtocol.h:50
virtual bool allowConnection()=0
#define PPT_SERVER_DEFAULT_TIMEOUT
Definition: PPTServer.cc:57
static void Indent()
Definition: BESIndent.cc:38
error thrown if there is a user syntax error in the request or any other user error ...
static string PPTSERVER_AUTHENTICATE
Definition: PPTProtocol.h:55
virtual void initConnection()
Using the info passed into the SocketLister, wait for an inbound request (see SocketListener::accept(...
Definition: PPTServer.cc:145
PPTServer(ServerHandler *handler, SocketListener *listener, bool isSecure)
Definition: PPTServer.cc:59
virtual void closeConnection()
Definition: PPTServer.cc:175
virtual void handle(Connection *c)=0
virtual Socket * accept()
Use the select() system call to wait for an incoming connection.
static ostream & LMarg(ostream &strm)
Definition: BESIndent.cc:73
virtual ~PPTServer()
Definition: PPTServer.cc:92
virtual int readBufferNonBlocking(char *inBuff, const int buff_size)
read a buffer of data from the socket without blocking
virtual void close()
Definition: Socket.cc:81
virtual int receive(char *inBuff, const int inSize)
Definition: Socket.cc:108
virtual void dump(ostream &strm) const
dumps information about this object
void get_value(const string &s, string &val, bool &found)
Retrieve the value of a given key, if set.
Definition: BESKeys.cc:453
virtual void dump(ostream &strm) const =0
dump the contents of this object to the specified ostream
virtual void dump(ostream &strm) const
dumps information about this object
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
Definition: BESDebug.h:64
static void UnIndent()
Definition: BESIndent.cc:44
static BESKeys * TheKeys()
Definition: TheBESKeys.cc:48
virtual void send(const string &buffer)
sends the buffer to the socket
Socket * _mySock
Definition: Connection.h:50
static string PPTSERVER_CONNECTION_OK
Definition: PPTProtocol.h:54
virtual void dump(ostream &strm) const
dumps information about this object
Definition: PPTServer.cc:281