From 91beed59bd7c623d6ffc15a327b47caf1b59d610 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 26 Sep 2019 14:00:07 -0700 Subject: [PATCH] ircd::m: Generate conf room id on the fly; various static linting. --- matrix/Makefile.am | 1 + matrix/conf.cc | 43 ++++++--- matrix/matrix.cc | 222 ++++----------------------------------------- matrix/self.cc | 183 +++++++++++++++++++++++++++++++++++++ 4 files changed, 232 insertions(+), 217 deletions(-) create mode 100644 matrix/self.cc diff --git a/matrix/Makefile.am b/matrix/Makefile.am index f1d953190..46deac9dd 100644 --- a/matrix/Makefile.am +++ b/matrix/Makefile.am @@ -57,6 +57,7 @@ libircd_matrix_la_SOURCES =# libircd_matrix_la_SOURCES += name.cc libircd_matrix_la_SOURCES += id.cc libircd_matrix_la_SOURCES += dbs.cc +libircd_matrix_la_SOURCES += self.cc libircd_matrix_la_SOURCES += matrix.cc libircd_matrix_la_SOURCES += event.cc libircd_matrix_la_SOURCES += room.cc diff --git a/matrix/conf.cc b/matrix/conf.cc index 1b30f1610..7128c4b1c 100644 --- a/matrix/conf.cc +++ b/matrix/conf.cc @@ -8,6 +8,11 @@ // copyright notice and this permission notice is present in all copies. The // full license for this software is available in the LICENSE file. +namespace ircd::m +{ + struct conf_room; +} + using namespace ircd; extern "C" void default_conf(const string_view &prefix); @@ -16,6 +21,24 @@ extern "C" void reload_conf(); extern "C" void refresh_conf(); static void init_conf_item(conf::item<> &); +struct ircd::m::conf_room +{ + m::room::id::buf room_id + { + "conf", my_host() + }; + + m::room room + { + room_id + }; + + operator const m::room &() const + { + return room; + } +}; + // This module registers with conf::on_init to be called back // when a conf item is initialized; when this module is unloaded // we have to unregister that listener using this state. @@ -44,18 +67,6 @@ item_error_log true }; -const m::room::id::buf -conf_room_id -{ - "conf", ircd::my_host() -}; - -const m::room -conf_room -{ - conf_room_id -}; - extern "C" m::event::id::buf set_conf_item(const m::user::id &sender, const string_view &key, @@ -67,6 +78,7 @@ set_conf_item(const m::user::id &sender, return {}; } + const m::conf_room conf_room; return send(conf_room, sender, "ircd.conf.item", key, { { "value", val } @@ -82,6 +94,7 @@ get_conf_item(const string_view &key, m::event::keys::include { "content" } }; + const m::conf_room conf_room; const m::room::state state { conf_room, &fopts @@ -197,6 +210,7 @@ init_conf_items() m::event::keys::include { "content", "state_key" } }; + const m::conf_room conf_room; const m::room::state state { conf_room, &fopts @@ -217,6 +231,7 @@ init_conf_items() static void init_conf_item(conf::item<> &item) { + const m::conf_room conf_room; const m::room::state state { conf_room @@ -282,7 +297,8 @@ static void create_conf_room(const m::event &, m::vm::eval &) { - m::create(conf_room_id, m::me.user_id); + const m::conf_room conf_room; + m::create(conf_room.room_id, m::me.user_id); //rehash_conf({}, true); } @@ -301,6 +317,7 @@ void rehash_conf(const string_view &prefix, const bool &existing) { + const m::conf_room conf_room; const m::room::state state { conf_room diff --git a/matrix/matrix.cc b/matrix/matrix.cc index 15d95a1f2..d68028b76 100644 --- a/matrix/matrix.cc +++ b/matrix/matrix.cc @@ -10,7 +10,6 @@ namespace ircd::m { - std::unique_ptr _self; std::unique_ptr _dbs; std::unique_ptr _modules; @@ -55,7 +54,9 @@ void ircd::m::on_load() try { - _self = std::make_unique(); + assert(ircd::run::level == run::level::START); + m::self::init::keys(); + _dbs = std::make_unique(ircd::server_name, std::string{}); _modules = std::make_unique(); @@ -99,7 +100,6 @@ noexcept try _modules.reset(nullptr); _dbs.reset(nullptr); - _self.reset(nullptr); //TODO: remove this for non-interfering shutdown server::interrupt_all(); @@ -326,183 +326,6 @@ ircd::m::module_names_optional "web_hook", }; -/////////////////////////////////////////////////////////////////////////////// -// -// m/self.h -// - -std::string -ircd::m::self::origin -{}; - -std::string -ircd::m::self::servername -{}; - -ircd::ed25519::sk -ircd::m::self::secret_key -{}; - -ircd::ed25519::pk -ircd::m::self::public_key -{}; - -std::string -ircd::m::self::public_key_b64 -{}; - -std::string -ircd::m::self::public_key_id -{}; - -std::string -ircd::m::self::tls_cert_der -{}; - -std::string -ircd::m::self::tls_cert_der_sha256_b64 -{}; - -// -// my user -// - -ircd::m::user::id::buf -ircd_user_id -{ - "ircd", "localhost" // gets replaced after conf init -}; - -ircd::m::user -ircd::m::me -{ - ircd_user_id -}; - -// -// my room -// - -ircd::m::room::id::buf -ircd_room_id -{ - "ircd", "localhost" // replaced after conf init -}; - -ircd::m::room -ircd::m::my_room -{ - ircd_room_id -}; - -// -// my node -// - -std::array -ircd_node_id -{ - "localhost" // replaced after conf init -}; - -ircd::m::node -ircd::m::my_node -{ - ircd_node_id -}; - -bool -ircd::m::self::host(const string_view &other) -{ - // port() is 0 when the origin has no port (and implies 8448) - const auto port - { - net::port(hostport(origin)) - }; - - // If my_host has a port number, then the argument must also have the - // same port number. - if(port) - return host() == other; - - /// If my_host has no port number, then the argument can have port - /// 8448 or no port number, which will initialize net::hostport.port to - /// the "canon_port" of 8448. - assert(net::canon_port == 8448); - const net::hostport _other{other}; - if(net::port(_other) != net::canon_port) - return false; - - if(host() != host(_other)) - return false; - - return true; -} - -ircd::string_view -ircd::m::self::host() -{ - return m::self::origin; -} - -// -// init -// - -//TODO: XXX -extern ircd::m::room::id::buf users_room_id; -extern ircd::m::room::id::buf tokens_room_id; - -ircd::m::self::init::init() -try -{ - self::origin = string_view{ircd::network_name}; - self::servername = string_view{ircd::server_name}; - - // Sanity check that these are valid hostname strings. This was likely - // already checked, so these validators will simply throw without very - // useful error messages if invalid strings ever make it this far. - rfc3986::valid_host(origin); - rfc3986::valid_host(servername); - - m::my_node = string_view{strlcpy - { - ircd_node_id, origin - }}; - - ircd_user_id = {"ircd", origin}; - m::me = {ircd_user_id}; - - ircd_room_id = {"ircd", origin}; - m::my_room = {ircd_room_id}; - - tokens_room_id = {"tokens", origin}; - m::user::tokens = {tokens_room_id}; - - if(origin == "localhost") - log::warning - { - "The origin is configured or has defaulted to 'localhost'" - }; - - // Loading the keys module in runlevel::START will do further - // inits of m::self::globals. Calling the inits directly from - // here makes the module dependent on libircd and unloadable. - assert(ircd::run::level == run::level::START); - m::self::init::keys(); -} -catch(const std::exception &e) -{ - log::critical - { - m::log, "Failed to init self origin[%s] servername[%s]", - origin, - servername, - }; - - throw; -} - /////////////////////////////////////////////////////////////////////////////// // // m/sync.h @@ -1866,14 +1689,24 @@ const // ircd::m::node::room::room(const string_view &node_id) -:room{m::node{node_id}} -{} +:room +{ + m::node{node_id} +} +{ +} ircd::m::node::room::room(const m::node &node) -:node{node} -,room_id{node.room_id()} +:node { - static_cast(*this) = room_id; + node +} +,room_id +{ + node.room_id() +} +{ + this->m::room::room_id = this->room_id; } /////////////////////////////////////////////////////////////////////////////// @@ -2203,25 +2036,6 @@ ircd::m::membership(const event &event) // m/user.h // -/// ID of the room which stores ephemeral tokens (an instance of the room is -/// provided below). -ircd::m::room::id::buf -tokens_room_id -{ - "tokens", ircd::my_host() -}; - -/// The tokens room serves as a key-value lookup for various tokens to -/// users, etc. It primarily serves to store access tokens for users. This -/// is a separate room from the users room because in the future it may -/// have an optimized configuration as well as being more easily cleared. -/// -ircd::m::room -ircd::m::user::tokens -{ - tokens_room_id -}; - bool ircd::m::exists(const user::id &user_id) { diff --git a/matrix/self.cc b/matrix/self.cc new file mode 100644 index 000000000..130602624 --- /dev/null +++ b/matrix/self.cc @@ -0,0 +1,183 @@ +// Matrix Construct +// +// Copyright (C) Matrix Construct Developers, Authors & Contributors +// Copyright (C) 2016-2018 Jason Volk +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice is present in all copies. The +// full license for this software is available in the LICENSE file. + +std::string +ircd::m::self::origin +{ + ircd::string_view{ircd::network_name} +}; + +std::string +ircd::m::self::servername +{ + ircd::string_view{ircd::server_name} +}; + +ircd::ed25519::sk +ircd::m::self::secret_key +{}; + +ircd::ed25519::pk +ircd::m::self::public_key +{}; + +std::string +ircd::m::self::public_key_b64 +{}; + +std::string +ircd::m::self::public_key_id +{}; + +std::string +ircd::m::self::tls_cert_der +{}; + +std::string +ircd::m::self::tls_cert_der_sha256_b64 +{}; + +// +// my user +// + +ircd::m::user::id::buf +ircd_user_id +{ + "ircd", ircd::my_host() +}; + +ircd::m::user +ircd::m::me +{ + ircd_user_id +}; + +// +// my room +// + +ircd::m::room::id::buf +ircd_room_id +{ + "ircd", ircd::my_host() +}; + +ircd::m::room +ircd::m::my_room +{ + ircd_room_id +}; + +// +// my node +// + +ircd::m::node +ircd::m::my_node +{ + ircd::my_host() +}; + +bool +ircd::m::self::host(const string_view &other) +{ + // port() is 0 when the origin has no port (and implies 8448) + const auto port + { + net::port(hostport(origin)) + }; + + // If my_host has a port number, then the argument must also have the + // same port number. + if(port) + return host() == other; + + /// If my_host has no port number, then the argument can have port + /// 8448 or no port number, which will initialize net::hostport.port to + /// the "canon_port" of 8448. + assert(net::canon_port == 8448); + const net::hostport _other{other}; + if(net::port(_other) != net::canon_port) + return false; + + if(host() != host(_other)) + return false; + + return true; +} + +ircd::string_view +ircd::m::self::host() +{ + return m::self::origin; +} + +// +// init +// + +/// ID of the room which stores ephemeral tokens (an instance of the room is +/// provided below). +ircd::m::room::id::buf +tokens_room_id +{ + "tokens", ircd::my_host() +}; + +/// The tokens room serves as a key-value lookup for various tokens to +/// users, etc. It primarily serves to store access tokens for users. This +/// is a separate room from the users room because in the future it may +/// have an optimized configuration as well as being more easily cleared. +/// +ircd::m::room +ircd::m::user::tokens +{ + tokens_room_id +}; + +ircd::m::self::init::init() +try +{ + // Sanity check that these are valid hostname strings. This was likely + // already checked, so these validators will simply throw without very + // useful error messages if invalid strings ever make it this far. + rfc3986::valid_host(origin); + rfc3986::valid_host(servername); + + ircd_user_id = {"ircd", origin}; + m::me = {ircd_user_id}; + + ircd_room_id = {"ircd", origin}; + m::my_room = {ircd_room_id}; + + if(origin == "localhost") + log::warning + { + "The origin is configured or has defaulted to 'localhost'" + }; +} +catch(const std::exception &e) +{ + log::critical + { + m::log, "Failed to init self origin[%s] servername[%s]", + origin, + servername, + }; + + throw; +} + +static const auto self_init{[] +{ + ircd::m::self::init(); + return true; +}()};