Skip to content

Commit

Permalink
WL#15130 Socket-level TLS patch mysql#8: SocketAuthenticator
Browse files Browse the repository at this point in the history
Change-Id: I813f12061158da5949200fa40f35b8a01db4a949
  • Loading branch information
jdduncan committed Oct 7, 2022
1 parent 603c917 commit 307e094
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
24 changes: 19 additions & 5 deletions storage/ndb/include/util/SocketAuthenticator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,40 @@
#ifndef SOCKET_AUTHENTICATOR_HPP
#define SOCKET_AUTHENTICATOR_HPP

#include "portlib/ndb_socket.h"
#include "util/NdbSocket.h"

class SocketAuthenticator
{
public:
SocketAuthenticator() {}
virtual ~SocketAuthenticator() {}
virtual bool client_authenticate(ndb_socket_t sockfd) = 0;
virtual bool server_authenticate(ndb_socket_t sockfd) = 0;
bool client_authenticate(ndb_socket_t);
bool server_authenticate(ndb_socket_t);
virtual bool client_authenticate(NdbSocket &) = 0;
virtual bool server_authenticate(NdbSocket &) = 0;
};

inline bool SocketAuthenticator::client_authenticate(ndb_socket_t fd) {
NdbSocket socket(fd, NdbSocket::From::Existing);
return client_authenticate(socket);
}

inline bool SocketAuthenticator::server_authenticate(ndb_socket_t fd) {
NdbSocket socket(fd, NdbSocket::From::Existing);
return server_authenticate(socket);
}


class SocketAuthSimple : public SocketAuthenticator
{
char *m_passwd;
char *m_username;
public:
SocketAuthSimple(const char *username, const char *passwd);
~SocketAuthSimple() override;
bool client_authenticate(ndb_socket_t sockfd) override;
bool server_authenticate(ndb_socket_t sockfd) override;
bool client_authenticate(NdbSocket &) override;
bool server_authenticate(NdbSocket &) override;
};


#endif // SOCKET_AUTHENTICATOR_HPP
12 changes: 6 additions & 6 deletions storage/ndb/src/common/util/SocketAuthenticator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ SocketAuthSimple::~SocketAuthSimple()
free(m_username);
}

bool SocketAuthSimple::client_authenticate(ndb_socket_t sockfd)
bool SocketAuthSimple::client_authenticate(NdbSocket & sockfd)
{
SocketOutputStream s_output(sockfd);
SocketInputStream s_input(sockfd);
SecureSocketOutputStream s_output(sockfd);
SecureSocketInputStream s_input(sockfd);

// Write username and password
s_output.println("%s", m_username ? m_username : "");
Expand All @@ -68,10 +68,10 @@ bool SocketAuthSimple::client_authenticate(ndb_socket_t sockfd)
return false;
}

bool SocketAuthSimple::server_authenticate(ndb_socket_t sockfd)
bool SocketAuthSimple::server_authenticate(NdbSocket & sockfd)
{
SocketOutputStream s_output(sockfd);
SocketInputStream s_input(sockfd);
SecureSocketOutputStream s_output(sockfd);
SecureSocketInputStream s_input(sockfd);

char buf[256];

Expand Down
10 changes: 5 additions & 5 deletions storage/ndb/src/common/util/SocketClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,16 @@ SocketClient::connect(NdbSocket & secureSocket,
assert(m_last_used_port == 0);
ndb_socket_get_port(m_sockfd, &m_last_used_port);

secureSocket.init_from_new(m_sockfd);

if (m_auth) {
if (!m_auth->client_authenticate(m_sockfd))
if (!m_auth->client_authenticate(secureSocket))
{
DEBUG_FPRINTF((stderr, "authenticate failed in connect\n"));
ndb_socket_close(m_sockfd);
ndb_socket_invalidate(&m_sockfd);
secureSocket.close();
secureSocket.invalidate();
}
}

secureSocket.init_from_new(m_sockfd);

ndb_socket_invalidate(&m_sockfd);
}

0 comments on commit 307e094

Please sign in to comment.