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 
145 void
147 {
148  for(;;)
149  {
150  _mySock = _listener->accept() ;
151  if( _mySock )
152  {
153  if( _mySock->allowConnection() == true )
154  {
155  // welcome the client
156  if( welcomeClient( ) != -1 )
157  {
158  // now hand it off to the handler
159  _handler->handle( this ) ;
160  }
161  }
162  else
163  {
164  _mySock->close();
165  }
166  }
167  }
168 }
169 
170 void
172 {
173  if( _mySock ) _mySock->close() ;
174 }
175 
176 int
177 PPTServer::welcomeClient()
178 {
179  // Doing a non blocking read in case the connection is being initiated
180  // by a non-bes client. Don't want this to block. pcw - 3/5/07
181  // int bytesRead = _mySock->receive( inBuff, ppt_buffer_size ) ;
182  //
183  // We are receiving handshaking tokens, so the buffer doesn't need to be
184  // all that big. pcw - 05/31/08
185  unsigned int ppt_buffer_size = 64 ;
186  char *inBuff = new char[ppt_buffer_size+1] ;
187  int bytesRead = readBufferNonBlocking( inBuff, ppt_buffer_size ) ;
188 
189  // if the read of the initial connection fails or blocks, then return
190  if( bytesRead == -1 )
191  {
192  _mySock->close() ;
193  delete [] inBuff ;
194  return -1 ;
195  }
196 
197  string status( inBuff, bytesRead ) ;
198  delete [] inBuff ;
199 
201  {
202  /* If cannot negotiate with the client then we don't want to exit
203  * by throwing an exception, we want to return and let the caller
204  * clean up the connection
205  */
206  string err( "PPT cannot negotiate, " ) ;
207  err += " client started the connection with " + status ;
208  BESDEBUG( "ppt", err << endl ) ;
209  //throw BESInternalError( err, __FILE__, __LINE__ ) ;
210  send( err ) ;
211  _mySock->close() ;
212  return -1 ;
213  }
214 
215  if( !_secure )
216  {
218  }
219  else
220  {
221  authenticateClient() ;
222  }
223 
224  return 0 ;
225 }
226 
227 void
228 PPTServer::authenticateClient()
229 {
230 #if defined HAVE_OPENSSL && defined NOTTHERE
231  BESDEBUG( "ppt", "requiring secure connection: port = "
232  << _securePort << endl ) ;
233  // let the client know that it needs to authenticate
235 
236  // wait for the client request for the secure port
237  // We are waiting for a ppt tocken requesting the secure port number.
238  // The buffer doesn't need to be all that big. pcw - 05/31/08
239  unsigned int ppt_buffer_size = 64 ;
240  char *inBuff = new char[ppt_buffer_size] ;
241  int bytesRead = _mySock->receive( inBuff, ppt_buffer_size ) ;
242  string portRequest( inBuff, bytesRead ) ;
243  delete [] inBuff ;
244  if( portRequest != PPTProtocol::PPTCLIENT_REQUEST_AUTHPORT )
245  {
246  string err( "Secure connection ... expecting request for port" ) ;
247  err += " client requested " + portRequest ;
248  throw BESInternalError( err, __FILE__, __LINE__ ) ;
249  }
250 
251  // send the secure port number back to the client
252  ostringstream portResponse ;
253  portResponse << _securePort << PPTProtocol::PPT_COMPLETE_DATA_TRANSMITION ;
254  send( portResponse.str() ) ;
255 
256  // create a secure server object and authenticate
257  SSLServer server( _securePort, _cfile, _cafile, _kfile ) ;
258  server.initConnection() ;
259  server.closeConnection() ;
260 
261  // if it authenticates, good, if not, an exception is thrown, no need to
262  // do anything else here.
263 #else
264  string err = (string)"Authentication requested for this server "
265  + "but OpenSSL is not built into the server" ;
266  throw BESInternalError( err, __FILE__, __LINE__ ) ;
267 #endif
268 }
269 
276 void
277 PPTServer::dump( ostream &strm ) const
278 {
279  strm << BESIndent::LMarg << "PPTServer::dump - ("
280  << (void *)this << ")" << endl ;
282  if( _handler )
283  {
284  strm << BESIndent::LMarg << "server handler:" << endl ;
286  _handler->dump( strm ) ;
288  }
289  else
290  {
291  strm << BESIndent::LMarg << "server handler: null" << endl ;
292  }
293  if( _listener )
294  {
295  strm << BESIndent::LMarg << "listener:" << endl ;
297  _listener->dump( strm ) ;
299  }
300  else
301  {
302  strm << BESIndent::LMarg << "listener: null" << endl ;
303  }
304  strm << BESIndent::LMarg << "secure? " << _secure << endl ;
305  if( _secure )
306  {
308  strm << BESIndent::LMarg << "cert file: " << _cfile << endl ;
309  strm << BESIndent::LMarg << "cert authority file: " << _cafile << endl ;
310  strm << BESIndent::LMarg << "key file: " << _kfile << endl ;
311  strm << BESIndent::LMarg << "secure port: " << _securePort << endl ;
313  }
314  PPTConnection::dump( strm ) ;
316 }
317