From d872c2b49214c0a4db40a9e2d860802d9eedc563 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Tue, 15 Nov 2016 00:23:19 +0100 Subject: Support the ident protocol fix #3211 --- src/identd/identd_server.hpp | 38 ++++++++++++++++++++++++ src/identd/identd_socket.cpp | 69 ++++++++++++++++++++++++++++++++++++++++++++ src/identd/identd_socket.hpp | 44 ++++++++++++++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 src/identd/identd_server.hpp create mode 100644 src/identd/identd_socket.cpp create mode 100644 src/identd/identd_socket.hpp (limited to 'src/identd') diff --git a/src/identd/identd_server.hpp b/src/identd/identd_server.hpp new file mode 100644 index 0000000..4270749 --- /dev/null +++ b/src/identd/identd_server.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include +#include +#include + +class BiboumiComponent; + +class IdentdServer: public TcpSocketServer +{ + public: + IdentdServer(const BiboumiComponent& biboumi_component, std::shared_ptr poller, const uint16_t port): + TcpSocketServer(poller, port), + biboumi_component(biboumi_component) + {} + + const BiboumiComponent& get_biboumi_component() const + { + return this->biboumi_component; + } + void shutdown() + { + if (this->poller->is_managing_socket(this->socket)) + this->poller->remove_socket_handler(this->socket); + ::close(this->socket); + } + void clean() + { + this->sockets.erase(std::remove_if(this->sockets.begin(), this->sockets.end(), + [](const std::unique_ptr& socket) + { + return socket->get_socket() == -1; + }), + this->sockets.end()); + } + private: + const BiboumiComponent& biboumi_component; +}; diff --git a/src/identd/identd_socket.cpp b/src/identd/identd_socket.cpp new file mode 100644 index 0000000..c1ad55e --- /dev/null +++ b/src/identd/identd_socket.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +#include + +#include + +#include + +IdentdSocket::IdentdSocket(std::shared_ptr poller, const socket_t socket, TcpSocketServer& server): + TCPSocketHandler(poller), + server(dynamic_cast(server)) +{ + this->socket = socket; +} + +void IdentdSocket::parse_in_buffer(const std::size_t) +{ + while (true) + { + const auto line_end = this->in_buf.find('\n'); + if (line_end == std::string::npos) + break; + std::istringstream line(this->in_buf.substr(0, line_end)); + this->consume_in_buffer(line_end + 1); + + uint16_t local_port; + uint16_t remote_port; + char sep; + line >> local_port >> sep >> remote_port; + const auto& xmpp = this->server.get_biboumi_component(); + auto response = this->generate_answer(xmpp, local_port, remote_port); + + this->send_data(std::move(response)); + } +} + +static std::string hash_jid(const std::string& jid) +{ + sha1nfo sha1; + sha1_init(&sha1); + sha1_write(&sha1, jid.data(), jid.size()); + const uint8_t* res = sha1_result(&sha1); + std::ostringstream result; + for (int i = 0; i < HASH_LENGTH; i++) + result << std::hex << std::setfill('0') << std::setw(2) << static_cast(res[i]); + return result.str(); +} + +std::string IdentdSocket::generate_answer(const BiboumiComponent& biboumi, uint16_t local, uint16_t remote) +{ + for (const Bridge* bridge: biboumi.get_bridges()) + { + for (const auto& pair: bridge->get_irc_clients()) + { + if (pair.second->match_port_pairt(local, remote)) + { + std::ostringstream os; + os << local << " , " << remote << " : USERID : OTHER : " << hash_jid(bridge->get_bare_jid()); + log_debug("Identd, sending: ", os.str()); + return os.str(); + } + } + } + std::ostringstream os; + os << local << " , " << remote << " ERROR : NO-USER"; + log_debug("Identd, sending: ", os.str()); + return os.str(); +} diff --git a/src/identd/identd_socket.hpp b/src/identd/identd_socket.hpp new file mode 100644 index 0000000..6d6cc1d --- /dev/null +++ b/src/identd/identd_socket.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include + +#include +#include + +#include +#include + +class XmppComponent; +class IdentdSocket; +class IdentdServer; +template +class TcpSocketServer; + +class IdentdSocket: public TCPSocketHandler +{ + public: + IdentdSocket(std::shared_ptr poller, const socket_t socket, TcpSocketServer& server); + ~IdentdSocket() = default; + std::string generate_answer(const BiboumiComponent& biboumi, uint16_t local, uint16_t remote); + + void parse_in_buffer(const std::size_t size) override final; + void on_connection_close(const std::string& message) override final + {} + + bool is_connected() const override final + { + return true; + } + bool is_connecting() const override final + { + return false; + } + + private: + void connect() override + { assert(false); } + void on_connection_failed(const std::string&) override final + { assert(false); } + + IdentdServer& server; +}; -- cgit v1.2.3 From 4ab3cd653f3eb19ff82838f777984c9d16e64a80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Tue, 15 Nov 2016 00:49:55 +0100 Subject: Fix some little compilation errors with some configs, from last commit --- src/identd/identd_server.hpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/identd') diff --git a/src/identd/identd_server.hpp b/src/identd/identd_server.hpp index 4270749..5f74976 100644 --- a/src/identd/identd_server.hpp +++ b/src/identd/identd_server.hpp @@ -2,6 +2,7 @@ #include #include +#include #include class BiboumiComponent; -- cgit v1.2.3 From b5a7c66bea8f0bced4bb4ce5c52f17bc30317ec4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Wed, 16 Nov 2016 20:30:16 +0100 Subject: =?UTF-8?q?fix=20an=20=E2=80=9Cunused=20parameter=E2=80=9D=20warni?= =?UTF-8?q?ng?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/identd/identd_socket.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/identd') diff --git a/src/identd/identd_socket.hpp b/src/identd/identd_socket.hpp index 6d6cc1d..98077f2 100644 --- a/src/identd/identd_socket.hpp +++ b/src/identd/identd_socket.hpp @@ -22,7 +22,7 @@ class IdentdSocket: public TCPSocketHandler std::string generate_answer(const BiboumiComponent& biboumi, uint16_t local, uint16_t remote); void parse_in_buffer(const std::size_t size) override final; - void on_connection_close(const std::string& message) override final + void on_connection_close(const std::string&) override final {} bool is_connected() const override final -- cgit v1.2.3 From 732f53d798c86558e1e625c22e957243bb2d6467 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Fri, 18 Nov 2016 20:54:41 +0100 Subject: Clean a few more things --- src/identd/identd_socket.hpp | 7 ------- 1 file changed, 7 deletions(-) (limited to 'src/identd') diff --git a/src/identd/identd_socket.hpp b/src/identd/identd_socket.hpp index 98077f2..1c2bd27 100644 --- a/src/identd/identd_socket.hpp +++ b/src/identd/identd_socket.hpp @@ -22,8 +22,6 @@ class IdentdSocket: public TCPSocketHandler std::string generate_answer(const BiboumiComponent& biboumi, uint16_t local, uint16_t remote); void parse_in_buffer(const std::size_t size) override final; - void on_connection_close(const std::string&) override final - {} bool is_connected() const override final { @@ -35,10 +33,5 @@ class IdentdSocket: public TCPSocketHandler } private: - void connect() override - { assert(false); } - void on_connection_failed(const std::string&) override final - { assert(false); } - IdentdServer& server; }; -- cgit v1.2.3 From 75778f418751bf06687ccc99454683bb43a9daf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Wed, 7 Dec 2016 18:28:29 +0100 Subject: Add some missing includes --- src/identd/identd_socket.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/identd') diff --git a/src/identd/identd_socket.cpp b/src/identd/identd_socket.cpp index c1ad55e..46553ca 100644 --- a/src/identd/identd_socket.cpp +++ b/src/identd/identd_socket.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include -- cgit v1.2.3 From 99a4ddedaf903d27b781341108433ae2d9533ad1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Mon, 6 Mar 2017 00:51:43 +0100 Subject: Remove the embedded sha1 code, and use one of botan or gcrypt This adds a hard dependency on one of Botan or gcrypt. Botan is already a recommended dependency, and gcrypt is probably packaged almost everywhere, so this should not be a big deal. ref #3241 --- src/identd/identd_socket.cpp | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) (limited to 'src/identd') diff --git a/src/identd/identd_socket.cpp b/src/identd/identd_socket.cpp index 46553ca..a94f172 100644 --- a/src/identd/identd_socket.cpp +++ b/src/identd/identd_socket.cpp @@ -38,14 +38,7 @@ void IdentdSocket::parse_in_buffer(const std::size_t) static std::string hash_jid(const std::string& jid) { - sha1nfo sha1; - sha1_init(&sha1); - sha1_write(&sha1, jid.data(), jid.size()); - const uint8_t* res = sha1_result(&sha1); - std::ostringstream result; - for (int i = 0; i < HASH_LENGTH; i++) - result << std::hex << std::setfill('0') << std::setw(2) << static_cast(res[i]); - return result.str(); + return sha1(jid); } std::string IdentdSocket::generate_answer(const BiboumiComponent& biboumi, uint16_t local, uint16_t remote) -- cgit v1.2.3 From f0bc6c83a8eb548d0a3edbf7c16a6922bfd24ba5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Wed, 8 Mar 2017 19:04:15 +0100 Subject: Pass the shared_ptr by reference, to avoid useless copies --- src/identd/identd_server.hpp | 2 +- src/identd/identd_socket.cpp | 2 +- src/identd/identd_socket.hpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src/identd') diff --git a/src/identd/identd_server.hpp b/src/identd/identd_server.hpp index 5f74976..b1c8ec8 100644 --- a/src/identd/identd_server.hpp +++ b/src/identd/identd_server.hpp @@ -10,7 +10,7 @@ class BiboumiComponent; class IdentdServer: public TcpSocketServer { public: - IdentdServer(const BiboumiComponent& biboumi_component, std::shared_ptr poller, const uint16_t port): + IdentdServer(const BiboumiComponent& biboumi_component, std::shared_ptr& poller, const uint16_t port): TcpSocketServer(poller, port), biboumi_component(biboumi_component) {} diff --git a/src/identd/identd_socket.cpp b/src/identd/identd_socket.cpp index a94f172..b85257c 100644 --- a/src/identd/identd_socket.cpp +++ b/src/identd/identd_socket.cpp @@ -8,7 +8,7 @@ #include -IdentdSocket::IdentdSocket(std::shared_ptr poller, const socket_t socket, TcpSocketServer& server): +IdentdSocket::IdentdSocket(std::shared_ptr& poller, const socket_t socket, TcpSocketServer& server): TCPSocketHandler(poller), server(dynamic_cast(server)) { diff --git a/src/identd/identd_socket.hpp b/src/identd/identd_socket.hpp index 1c2bd27..10cb797 100644 --- a/src/identd/identd_socket.hpp +++ b/src/identd/identd_socket.hpp @@ -17,7 +17,7 @@ class TcpSocketServer; class IdentdSocket: public TCPSocketHandler { public: - IdentdSocket(std::shared_ptr poller, const socket_t socket, TcpSocketServer& server); + IdentdSocket(std::shared_ptr& poller, const socket_t socket, TcpSocketServer& server); ~IdentdSocket() = default; std::string generate_answer(const BiboumiComponent& biboumi, uint16_t local, uint16_t remote); -- cgit v1.2.3 From 5402a256d1f0ebbeafa32d250d000cf38fe748fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Fri, 7 Apr 2017 18:45:24 +0200 Subject: Apply all the clang-tidy modernize-* fixes --- src/identd/identd_socket.hpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src/identd') diff --git a/src/identd/identd_socket.hpp b/src/identd/identd_socket.hpp index 10cb797..a386d80 100644 --- a/src/identd/identd_socket.hpp +++ b/src/identd/identd_socket.hpp @@ -2,7 +2,6 @@ #include -#include #include #include -- cgit v1.2.3