Skip to content

Commit bde5956

Browse files
committed
WL#15154 patch #2 Preliminary SocketAuthenticator refactoring
Move client_authenticate() out of SocketClient::connect() (which returns void) into a separate SocketClient::authenticate() method which can return a value. In SocketAuthenticator, change the signature of the authentication routines to return an int (which can represent a result code) rather than a bool. Results less than AuthOk represent failure, and results greater than or equal to AuthOk represent success. Remove the username and password variables from SocketAuthSimple; make them constant strings in the implementation. There are no functional changes. Change-Id: I4c25e99f1b9b692db39213dfa63352da8993a8fb
1 parent 94e0df2 commit bde5956

File tree

6 files changed

+63
-52
lines changed

6 files changed

+63
-52
lines changed

storage/ndb/include/util/SocketAuthenticator.hpp

+14-8
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,31 @@
2727

2828
#include "util/NdbSocket.h"
2929

30+
/* client_authenticate() and server_authenticate() return a value
31+
less than AuthOk on failure. They return a value greater than or
32+
equal to AuthOk on success.
33+
*/
34+
3035
class SocketAuthenticator
3136
{
3237
public:
3338
SocketAuthenticator() {}
3439
virtual ~SocketAuthenticator() {}
35-
virtual bool client_authenticate(NdbSocket &) = 0;
36-
virtual bool server_authenticate(NdbSocket &) = 0;
40+
virtual int client_authenticate(NdbSocket &) = 0;
41+
virtual int server_authenticate(NdbSocket &) = 0;
42+
43+
static constexpr int AuthOk = 0;
44+
static const char * error(int); // returns error message for code
3745
};
3846

3947

4048
class SocketAuthSimple : public SocketAuthenticator
4149
{
42-
char *m_passwd;
43-
char *m_username;
4450
public:
45-
SocketAuthSimple(const char *username, const char *passwd);
46-
~SocketAuthSimple() override;
47-
bool client_authenticate(NdbSocket &) override;
48-
bool server_authenticate(NdbSocket &) override;
51+
SocketAuthSimple() {}
52+
~SocketAuthSimple() override {}
53+
int client_authenticate(NdbSocket &) override;
54+
int server_authenticate(NdbSocket &) override;
4955
};
5056

5157

storage/ndb/include/util/SocketClient.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class SocketClient
4646
int bind(ndb_sockaddr local);
4747
ndb_socket_t connect(ndb_sockaddr server_addr);
4848
void connect(NdbSocket &, ndb_sockaddr server_addr);
49+
int authenticate(NdbSocket &);
4950

5051
ndb_socket_t m_sockfd;
5152
};

storage/ndb/src/common/transporter/Transporter.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,7 @@ Transporter::Transporter(TransporterRegistry &t_reg,
145145
m_socket_client= nullptr;
146146
else
147147
{
148-
m_socket_client= new SocketClient(new SocketAuthSimple("ndbd",
149-
"ndbd passwd"));
150-
148+
m_socket_client= new SocketClient(new SocketAuthSimple());
151149
m_socket_client->set_connect_timeout(m_timeOutMillis);
152150
}
153151

@@ -335,6 +333,14 @@ Transporter::connect_client()
335333
}
336334
}
337335
m_socket_client->connect(secureSocket, remote_addr);
336+
337+
/** Socket Authentication */
338+
if(m_socket_client->authenticate(secureSocket) < SocketAuthSimple::AuthOk)
339+
{
340+
DEBUG_FPRINTF((stderr, "Socket Authenticator failed\n"));
341+
DBUG_RETURN(false);
342+
}
343+
338344
}
339345

340346
DBUG_RETURN(connect_client(secureSocket));

storage/ndb/src/common/transporter/TransporterRegistry.cpp

+10-7
Original file line numberDiff line numberDiff line change
@@ -117,19 +117,22 @@ TransporterRegistry::get_bytes_received(NodeId node_id) const
117117
SocketServer::Session * TransporterService::newSession(ndb_socket_t sockfd)
118118
{
119119
/* The connection is currently running over a plain network socket.
120-
If m_auth is a TlsAuthenticator, it might get upgraded to a TLS socket
121-
in server_authenticate().
120+
If m_auth is a SocketAuthTls, it might get upgraded to a TLS socket.
122121
*/
123122
NdbSocket secureSocket;
124123
secureSocket.init_from_new(sockfd);
125124

126125
DBUG_ENTER("SocketServer::Session * TransporterService::newSession");
127126
DEBUG_FPRINTF((stderr, "New session created\n"));
128-
if (m_auth && !m_auth->server_authenticate(secureSocket))
127+
if(m_auth)
129128
{
130-
DEBUG_FPRINTF((stderr, "Failed to authenticate new session\n"));
131-
secureSocket.close_with_reset(true); // Close with reset
132-
DBUG_RETURN(0);
129+
int r = m_auth->server_authenticate(secureSocket);
130+
if(r < SocketAuthenticator::AuthOk)
131+
{
132+
DEBUG_FPRINTF((stderr, "Failed to authenticate new session\n"));
133+
secureSocket.close_with_reset(true); // Close with reset
134+
DBUG_RETURN(0);
135+
}
133136
}
134137

135138
BaseString msg;
@@ -3595,7 +3598,7 @@ TransporterRegistry::start_service(SocketServer& socket_server)
35953598
if(t.m_s_service_port<0)
35963599
port= -t.m_s_service_port; // is a dynamic port
35973600
TransporterService *transporter_service =
3598-
new TransporterService(new SocketAuthSimple("ndbd", "ndbd passwd"));
3601+
new TransporterService(new SocketAuthSimple());
35993602
ndb_sockaddr addr;
36003603
if (t.m_interface && Ndb_getAddr(&addr, t.m_interface))
36013604
{

storage/ndb/src/common/util/SocketAuthenticator.cpp

+12-25
Original file line numberDiff line numberDiff line change
@@ -26,49 +26,36 @@
2626
#include <SocketAuthenticator.hpp>
2727
#include <InputStream.hpp>
2828
#include <OutputStream.hpp>
29-
SocketAuthSimple::SocketAuthSimple(const char *username, const char *passwd) {
30-
if (username)
31-
m_username= strdup(username);
32-
else
33-
m_username= nullptr;
34-
if (passwd)
35-
m_passwd= strdup(passwd);
36-
else
37-
m_passwd= nullptr;
38-
}
3929

40-
SocketAuthSimple::~SocketAuthSimple()
30+
const char * SocketAuthenticator::error(int result)
4131
{
42-
if (m_passwd)
43-
free(m_passwd);
44-
if (m_username)
45-
free(m_username);
32+
return (result < AuthOk) ? "Socket Auth failure" : "Success";
4633
}
4734

48-
bool SocketAuthSimple::client_authenticate(NdbSocket & sockfd)
35+
int SocketAuthSimple::client_authenticate(NdbSocket & sockfd)
4936
{
5037
SecureSocketOutputStream s_output(sockfd);
5138
SecureSocketInputStream s_input(sockfd);
5239

5340
// Write username and password
54-
s_output.println("%s", m_username ? m_username : "");
55-
s_output.println("%s", m_passwd ? m_passwd : "");
41+
s_output.println("ndbd");
42+
s_output.println("ndbd passwd");
5643

5744
char buf[16];
5845

5946
// Read authentication result
6047
if (s_input.gets(buf, sizeof(buf)) == nullptr)
61-
return false;
48+
return -1;
6249
buf[sizeof(buf)-1]= 0;
6350

6451
// Verify authentication result
6552
if (strncmp("ok", buf, 2) == 0)
66-
return true;
53+
return 0;
6754

68-
return false;
55+
return -1;
6956
}
7057

71-
bool SocketAuthSimple::server_authenticate(NdbSocket & sockfd)
58+
int SocketAuthSimple::server_authenticate(NdbSocket & sockfd)
7259
{
7360
SecureSocketOutputStream s_output(sockfd);
7461
SecureSocketInputStream s_input(sockfd);
@@ -77,17 +64,17 @@ bool SocketAuthSimple::server_authenticate(NdbSocket & sockfd)
7764

7865
// Read username
7966
if (s_input.gets(buf, sizeof(buf)) == nullptr)
80-
return false;
67+
return -1;
8168
buf[sizeof(buf)-1]= 0;
8269

8370
// Read password
8471
if (s_input.gets(buf, sizeof(buf)) == nullptr)
85-
return false;
72+
return -1;
8673
buf[sizeof(buf)-1]= 0;
8774

8875
// Write authentication result
8976
s_output.println("ok");
9077

91-
return true;
78+
return 0;
9279
}
9380

storage/ndb/src/common/util/SocketClient.cpp

+17-9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include <ndb_global.h>
2727

2828
#include <cassert>
29+
30+
#include "EventLogger.hpp"
2931
#include "SocketClient.hpp"
3032
#include "SocketAuthenticator.hpp"
3133
#include "portlib/ndb_socket_poller.h"
@@ -209,16 +211,22 @@ SocketClient::connect(NdbSocket & secureSocket,
209211
assert(m_last_used_port == 0);
210212
ndb_socket_get_port(m_sockfd, &m_last_used_port);
211213

214+
// Transfer the fd to the NdbSocket
212215
secureSocket.init_from_new(m_sockfd);
216+
ndb_socket_invalidate(&m_sockfd);
217+
}
213218

214-
if (m_auth) {
215-
if (!m_auth->client_authenticate(secureSocket))
216-
{
217-
DEBUG_FPRINTF((stderr, "authenticate failed in connect\n"));
218-
secureSocket.close();
219-
secureSocket.invalidate();
220-
}
219+
int
220+
SocketClient::authenticate(NdbSocket & secureSocket)
221+
{
222+
assert(m_auth);
223+
int r = m_auth->client_authenticate(secureSocket);
224+
if (r < SocketAuthenticator::AuthOk)
225+
{
226+
g_eventLogger->error("Socket authentication failed: %s\n",
227+
m_auth->error(r));
228+
secureSocket.close();
229+
secureSocket.invalidate();
221230
}
222-
223-
ndb_socket_invalidate(&m_sockfd);
231+
return r;
224232
}

0 commit comments

Comments
 (0)