forked from OPENDAP/bes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPPTClient.cc
263 lines (233 loc) · 7.23 KB
/
PPTClient.cc
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
// PPTClient.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 <[email protected]> and Jose Garcia <[email protected]>
//
// 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 <[email protected]>
// jgarcia Jose Garcia <[email protected]>
#include <string>
#include <iostream>
#include <sstream>
using std::string ;
using std::cerr ;
using std::cout ;
using std::endl ;
using std::ostringstream ;
using std::ostream;
#include "PPTClient.h"
#include "TcpSocket.h"
#include "UnixSocket.h"
#include "PPTProtocolNames.h"
#include "BESInternalError.h"
#include "BESSyntaxUserError.h"
#include "TheBESKeys.h"
#include "config.h"
#if defined HAVE_OPENSSL && defined NOTTHERE
#include "SSLClient.h"
#endif
PPTClient::PPTClient( const string &hostStr, int portVal, int timeout )
: PPTConnection( timeout ),
_connected( false ),
_host( hostStr )
{
// connect to the specified host at the specified socket to handle the
// secure connection
_mySock = new TcpSocket( hostStr, portVal ) ;
_mySock->connect() ;
_connected = _mySock->isConnected();
}
PPTClient::PPTClient( const string &unix_socket, int timeout )
: PPTConnection( timeout ),
_connected( false )
{
// connect to the specified unix socket to handle the secure connection
_mySock = new UnixSocket( unix_socket ) ;
_mySock->connect() ;
_connected = true ;
}
void
PPTClient::get_secure_files()
{
bool found = false ;
TheBESKeys::TheKeys()->get_value( "BES.ClientCertFile", _cfile, found ) ;
if( !found || _cfile.empty() )
{
string err = "Unable to determine client certificate file." ;
throw BESSyntaxUserError( err, __FILE__, __LINE__ ) ;
}
found = false ;
TheBESKeys::TheKeys()->get_value( "BES.ClientCertAuthFile", _cafile, found);
if( !found || _cafile.empty() )
{
string err = "Unable to determine client certificate authority file." ;
throw BESSyntaxUserError( err, __FILE__, __LINE__ ) ;
}
found = false ;
TheBESKeys::TheKeys()->get_value( "BES.ClientKeyFile", _kfile, found ) ;
if( !found || _kfile.empty() )
{
string err = "Unable to determine client key file." ;
throw BESSyntaxUserError( err, __FILE__, __LINE__ ) ;
}
}
PPTClient::~PPTClient()
{
if( _mySock )
{
if( _connected )
{
closeConnection() ;
}
delete _mySock ;
_mySock = 0 ;
}
}
void
PPTClient::initConnection()
{
try
{
send(PPT_CLIENT_TESTING_CONNECTION ) ;
}
catch( BESInternalError &e )
{
string msg = "Failed to initialize connection to server\n" ;
msg += e.get_message() ;
throw BESInternalError( msg, __FILE__, __LINE__ ) ;
}
// we're just getting tokens, not a big buffer, so don't need that big
// of a buffer. pcw 05/31/08
const int ppt_buffer_size = 64 ;
char *inBuff = new char[ppt_buffer_size+1] ;
int bytesRead = readBufferNonBlocking( inBuff, ppt_buffer_size ) ;
if( bytesRead < 1 )
{
delete [] inBuff ;
string err = "Could not connect to server, server may be down or busy" ;
throw BESInternalError( err, __FILE__, __LINE__) ;
}
if( bytesRead > ppt_buffer_size )
bytesRead = ppt_buffer_size ;
inBuff[bytesRead] = '\0' ;
string status( inBuff, 0, bytesRead ) ;
delete [] inBuff ;
if( status == PPT_PROTOCOL_UNDEFINED )
{
string err = "Could not connect to server, server may be down or busy" ;
throw BESInternalError( err, __FILE__, __LINE__ ) ;
}
if(status == PPT_SERVER_AUTHENTICATE )
{
authenticateWithServer() ;
}
else if(status != PPT_SERVER_CONNECTION_OK )
{
string err = "Server reported an invalid connection, \""
+ status + "\"" ;
throw BESInternalError( err, __FILE__, __LINE__ ) ;
}
}
void
PPTClient::authenticateWithServer()
{
#if defined HAVE_OPENSSL && defined NOTTHERE
// get the certificate and key file information
get_secure_files() ;
// send request for the authentication port
send(PPT_CLIENT_REQUEST_AUTHPORT ) ;
// receive response with port, terminated with TERMINATE token. We are
// exchanging a port number and a terminating token. The buffer doesn't
// need to be too big. pcw 05/31/08
const int ppt_buffer_size = 64 ;
char *inBuff = new char[ppt_buffer_size+1] ;
int bytesRead = readBufferNonBlocking( inBuff, ppt_buffer_size ) ;
if( bytesRead < 1 )
{
delete [] inBuff ;
string err = "Expecting secure port number response" ;
throw BESInternalError( err, __FILE__, __LINE__ ) ;
}
if( bytesRead > ppt_buffer_size )
{
bytesRead = ppt_buffer_size ;
}
inBuff[bytesRead] = '\0' ;
ostringstream portResponse( inBuff ) ;
delete [] inBuff ;
int portVal = atoi( portResponse.str().c_str() ) ;
if( portVal == 0 )
{
string err = "Expecting valid secure port number response" ;
throw BESInternalError( err, __FILE__, __LINE__ ) ;
}
// authenticate using SSLClient
SSLClient client( _host, portVal, _cfile, _cafile, _kfile ) ;
client.initConnection() ;
client.closeConnection() ;
// If it authenticates, good, if not then an exception is thrown. We
// don't need to do anything else here.
#else
throw BESInternalError( "Server has requested authentication, but OpenSSL is not built into this client", __FILE__, __LINE__ ) ;
#endif
}
void
PPTClient::closeConnection()
{
if( _connected )
{
if( !_brokenPipe )
{
try
{
sendExit() ;
}
catch( BESInternalError &e )
{
cerr << "Failed to inform server that the client is exiting, "
<< "continuing" << endl ;
cerr << e.get_message() << endl ;
}
}
_mySock->close() ;
_connected = false ;
_brokenPipe = false ;
}
}
/** @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
PPTClient::dump( ostream &strm ) const
{
strm << BESIndent::LMarg << "PPTClient::dump - ("
<< (void *)this << ")" << endl ;
BESIndent::Indent() ;
strm << BESIndent::LMarg << "connected? " << _connected << endl ;
strm << BESIndent::LMarg << "host: " << _host << endl ;
PPTConnection::dump( strm ) ;
BESIndent::UnIndent() ;
}