2019-10-01 05:50:58 +02:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
namespace ircd::m
|
|
|
|
{
|
2020-05-10 04:41:10 +02:00
|
|
|
static void signon(homeserver &), signoff(homeserver &) noexcept;
|
2019-10-03 21:15:23 +02:00
|
|
|
|
|
|
|
extern conf::item<std::string> online_status_msg;
|
|
|
|
extern conf::item<std::string> offline_status_msg;
|
2019-10-01 05:50:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Linkage for the container of all active clients for iteration purposes.
|
|
|
|
template<>
|
|
|
|
decltype(ircd::util::instance_multimap<ircd::string_view, ircd::m::homeserver, std::less<>>::map)
|
|
|
|
ircd::util::instance_multimap<ircd::string_view, ircd::m::homeserver, std::less<>>::map
|
|
|
|
{};
|
|
|
|
|
|
|
|
[[gnu::hot]]
|
|
|
|
ircd::m::user::id
|
|
|
|
ircd::m::me()
|
|
|
|
{
|
|
|
|
auto &my
|
|
|
|
{
|
|
|
|
m::my()
|
|
|
|
};
|
|
|
|
|
|
|
|
return my.self;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[gnu::hot]]
|
|
|
|
ircd::m::user::id
|
|
|
|
ircd::m::me(const string_view &origin)
|
|
|
|
{
|
|
|
|
auto &my
|
|
|
|
{
|
|
|
|
m::my(origin)
|
|
|
|
};
|
|
|
|
|
|
|
|
return my.self;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[gnu::hot]]
|
|
|
|
ircd::m::homeserver &
|
|
|
|
ircd::m::my()
|
|
|
|
{
|
|
|
|
if(unlikely(!homeserver::primary))
|
|
|
|
throw m::NOT_A_HOMESERVER
|
|
|
|
{
|
|
|
|
"I do not host any homeserver here."
|
|
|
|
};
|
|
|
|
|
|
|
|
return *homeserver::primary;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[gnu::hot]]
|
|
|
|
ircd::m::homeserver &
|
|
|
|
ircd::m::my(const string_view &name)
|
|
|
|
{
|
|
|
|
const auto &it
|
|
|
|
{
|
|
|
|
homeserver::map.find(name)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(unlikely(it == end(homeserver::map)))
|
|
|
|
throw m::NOT_MY_HOMESERVER
|
|
|
|
{
|
|
|
|
"I do not host any '%s' homeserver here.",
|
|
|
|
name,
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &ptr
|
|
|
|
{
|
|
|
|
it->second
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(ptr);
|
|
|
|
return *ptr;
|
|
|
|
}
|
|
|
|
|
2019-10-04 22:39:55 +02:00
|
|
|
bool
|
|
|
|
ircd::m::myself(const m::user::id &user_id)
|
|
|
|
{
|
|
|
|
return !for_each([&user_id]
|
|
|
|
(const auto &homeserver)
|
|
|
|
{
|
|
|
|
return homeserver.self != user_id;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::my_origin(const string_view &origin)
|
|
|
|
{
|
|
|
|
return !for_each([&origin]
|
|
|
|
(const auto &homeserver)
|
|
|
|
{
|
|
|
|
return m::origin(homeserver) != origin;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-01 05:50:58 +02:00
|
|
|
bool
|
|
|
|
ircd::m::for_each(const std::function<bool (homeserver &)> &closure)
|
|
|
|
{
|
|
|
|
for(auto &[name, hs_p] : homeserver::map)
|
|
|
|
if(!closure(*hs_p))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ircd::ed25519::sk &
|
|
|
|
ircd::m::secret_key(const homeserver &homeserver)
|
|
|
|
{
|
|
|
|
assert(homeserver.key);
|
|
|
|
return homeserver.key->secret_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::m::public_key_id(const homeserver &homeserver)
|
|
|
|
{
|
|
|
|
assert(homeserver.key);
|
|
|
|
return homeserver.key->public_key_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::server_name(const homeserver &homeserver,
|
|
|
|
const string_view &server_name)
|
|
|
|
{
|
|
|
|
return server_name == m::server_name(homeserver);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::origin(const homeserver &homeserver,
|
|
|
|
const string_view &origin)
|
|
|
|
{
|
|
|
|
return origin == m::origin(homeserver);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::m::server_name(const homeserver &homeserver)
|
|
|
|
{
|
|
|
|
assert(homeserver.opts);
|
|
|
|
return homeserver.opts->server_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::m::origin(const homeserver &homeserver)
|
|
|
|
{
|
|
|
|
assert(homeserver.opts);
|
|
|
|
return homeserver.opts->origin;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// homeserver::homeserver
|
|
|
|
//
|
|
|
|
|
2020-02-28 00:46:05 +01:00
|
|
|
namespace ircd::m
|
|
|
|
{
|
|
|
|
std::unique_ptr<fetch::init> _fetch;
|
2020-05-10 07:00:33 +02:00
|
|
|
std::unique_ptr<vm::init> _vm;
|
2020-02-28 00:46:05 +01:00
|
|
|
}
|
|
|
|
|
2019-10-01 05:50:58 +02:00
|
|
|
decltype(ircd::m::homeserver::primary)
|
|
|
|
ircd::m::homeserver::primary;
|
|
|
|
|
|
|
|
IRCD_MODULE_EXPORT
|
2019-10-03 19:51:29 +02:00
|
|
|
ircd::m::homeserver *
|
2019-10-01 05:50:58 +02:00
|
|
|
ircd::m::homeserver::init(const struct opts *const opts)
|
2020-02-19 21:03:07 +01:00
|
|
|
try
|
2019-10-01 05:50:58 +02:00
|
|
|
{
|
2019-10-03 21:15:23 +02:00
|
|
|
assert(opts);
|
|
|
|
rfc3986::valid_host(opts->origin);
|
|
|
|
rfc3986::valid_host(opts->server_name);
|
2020-02-28 00:46:05 +01:00
|
|
|
|
2019-10-01 05:50:58 +02:00
|
|
|
return new homeserver
|
|
|
|
{
|
|
|
|
opts
|
|
|
|
};
|
|
|
|
}
|
2020-02-19 21:03:07 +01:00
|
|
|
catch(const m::error &e)
|
|
|
|
{
|
|
|
|
assert(opts);
|
|
|
|
// Don't rethrow m::error so the catcher doesn't depend on
|
|
|
|
// RTTI/personality from this shlib after it unloads.
|
|
|
|
throw ircd::error
|
|
|
|
{
|
|
|
|
"Failed to initialize homeserver %s of %s :%s :%s",
|
|
|
|
opts->server_name,
|
|
|
|
opts->origin,
|
|
|
|
e.errcode(),
|
|
|
|
e.errstr(),
|
|
|
|
};
|
|
|
|
}
|
2019-10-01 05:50:58 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::homeserver::fini(homeserver *const homeserver)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
delete homeserver;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// homeserver::homeserver::homeserver
|
|
|
|
//
|
|
|
|
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::homeserver::homeserver(const struct opts *const &opts)
|
2020-05-10 07:00:33 +02:00
|
|
|
try
|
2019-10-01 05:50:58 +02:00
|
|
|
:instance_multimap
|
|
|
|
{
|
|
|
|
string_view{opts->origin}
|
|
|
|
}
|
2019-10-03 21:15:23 +02:00
|
|
|
,opts{[this, &opts]
|
2019-10-01 05:50:58 +02:00
|
|
|
{
|
2019-10-04 21:48:54 +02:00
|
|
|
primary = primary?: this; //TODO: xxx
|
2019-10-03 21:15:23 +02:00
|
|
|
return opts;
|
|
|
|
}()}
|
2019-10-01 05:50:58 +02:00
|
|
|
,key
|
|
|
|
{
|
2019-10-04 21:48:54 +02:00
|
|
|
std::make_unique<struct key>(*opts)
|
2019-10-01 05:50:58 +02:00
|
|
|
}
|
|
|
|
,database
|
|
|
|
{
|
2019-10-04 21:48:54 +02:00
|
|
|
std::make_shared<dbs::init>(opts->server_name)
|
2019-10-01 05:50:58 +02:00
|
|
|
}
|
|
|
|
,self
|
|
|
|
{
|
|
|
|
"ircd", opts->origin
|
|
|
|
}
|
2019-10-04 21:48:54 +02:00
|
|
|
,conf
|
|
|
|
{
|
|
|
|
std::make_unique<struct conf>(*opts)
|
|
|
|
}
|
2019-10-03 21:15:23 +02:00
|
|
|
,modules
|
|
|
|
{
|
|
|
|
begin(matrix::module_names), end(matrix::module_names)
|
|
|
|
}
|
2019-10-01 05:50:58 +02:00
|
|
|
{
|
2020-04-25 07:47:57 +02:00
|
|
|
if(ircd::mods::autoload)
|
|
|
|
for(const auto &name : modules)
|
|
|
|
mods::imports.emplace(std::string{name}, name);
|
2020-03-17 00:44:12 +01:00
|
|
|
|
2020-05-10 07:00:33 +02:00
|
|
|
if(conf && !ircd::defaults)
|
2019-10-04 23:17:24 +02:00
|
|
|
conf->load();
|
|
|
|
|
2020-05-10 07:00:33 +02:00
|
|
|
_fetch = std::make_unique<fetch::init>();
|
|
|
|
_vm = std::make_unique<vm::init>();
|
|
|
|
const unwind_exceptional exceptional{[]
|
|
|
|
{
|
|
|
|
_fetch.reset(nullptr);
|
|
|
|
_vm.reset(nullptr);
|
|
|
|
}};
|
|
|
|
|
2020-09-13 16:16:17 +02:00
|
|
|
const bool need_bootstrap
|
|
|
|
{
|
|
|
|
(sequence(*dbs::events) == 0 || opts->bootstrap_vector_path)
|
|
|
|
&& dbs::events
|
|
|
|
};
|
|
|
|
|
|
|
|
if(need_bootstrap)
|
2020-08-24 13:06:16 +02:00
|
|
|
bootstrap();
|
2019-10-05 00:06:49 +02:00
|
|
|
|
2020-09-28 00:43:00 +02:00
|
|
|
// If the database is empty here there's nothing left to do; this is not
|
|
|
|
// an error. When something tries to use this empty homeserver that is
|
|
|
|
// where the error will be.
|
|
|
|
if(sequence(*dbs::events) == 0)
|
|
|
|
return;
|
|
|
|
|
2020-05-10 07:00:33 +02:00
|
|
|
mods::imports.emplace("net_dns_cache"s, "net_dns_cache"s);
|
2020-07-28 07:16:04 +02:00
|
|
|
|
|
|
|
if(!ircd::write_avoid)
|
|
|
|
if(key && !key->verify_keys.empty())
|
|
|
|
m::keys::cache::set(key->verify_keys);
|
|
|
|
|
2020-09-16 03:01:40 +02:00
|
|
|
if(!ircd::maintenance)
|
2020-07-28 07:16:04 +02:00
|
|
|
signon(*this);
|
2020-09-16 03:01:40 +02:00
|
|
|
|
2020-09-16 18:08:05 +02:00
|
|
|
if(!ircd::maintenance && opts->backfill)
|
2020-07-28 07:16:04 +02:00
|
|
|
m::init::backfill::init();
|
2020-05-10 07:00:33 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::logf
|
|
|
|
{
|
|
|
|
log, log::level::CRITICAL,
|
2020-05-11 03:22:10 +02:00
|
|
|
"Failed to start server '%s' on network '%s' :%s",
|
2020-05-10 07:00:33 +02:00
|
|
|
opts->server_name,
|
|
|
|
opts->origin,
|
2020-05-11 03:22:10 +02:00
|
|
|
e.what(),
|
2020-05-10 07:00:33 +02:00
|
|
|
};
|
2019-10-01 05:50:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::homeserver::~homeserver()
|
2020-05-10 07:00:33 +02:00
|
|
|
noexcept try
|
|
|
|
{
|
|
|
|
///TODO: XXX primary
|
|
|
|
server::init::interrupt();
|
|
|
|
client::terminate_all(); //TODO: XXX
|
|
|
|
server::init::close();
|
|
|
|
client::close_all();
|
|
|
|
m::init::backfill::fini();
|
|
|
|
client::wait_all();
|
|
|
|
server::init::wait();
|
|
|
|
m::sync::pool.join();
|
|
|
|
|
2020-09-16 03:01:40 +02:00
|
|
|
if(!ircd::maintenance && _vm)
|
2020-05-10 07:00:33 +02:00
|
|
|
signoff(*this);
|
|
|
|
|
|
|
|
///TODO: XXX primary
|
|
|
|
mods::imports.erase("net_dns_cache"s);
|
|
|
|
_fetch.reset(nullptr);
|
|
|
|
_vm.reset(nullptr);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
2019-10-01 05:50:58 +02:00
|
|
|
{
|
2020-05-10 07:00:33 +02:00
|
|
|
log::critical
|
2020-02-28 00:46:05 +01:00
|
|
|
{
|
2020-05-10 07:00:33 +02:00
|
|
|
log, "Homeserver shutdown failed :%s",
|
|
|
|
e.what()
|
|
|
|
};
|
2020-03-17 00:44:12 +01:00
|
|
|
|
2020-05-10 07:00:33 +02:00
|
|
|
return;
|
2020-05-10 04:56:20 +02:00
|
|
|
}
|
2020-02-28 00:46:05 +01:00
|
|
|
|
2020-05-10 04:56:20 +02:00
|
|
|
//
|
|
|
|
// homeserver modules
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::homeserver::modules::~modules()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
for(auto rit(std::rbegin(*this)); rit != std::rend(*this); ++rit)
|
|
|
|
mods::imports.erase(*rit);
|
2019-10-01 05:50:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2019-10-04 21:48:54 +02:00
|
|
|
// homeserver::key
|
2019-10-01 05:50:58 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
namespace ircd::m
|
|
|
|
{
|
|
|
|
extern conf::item<std::string> ed25519_key_dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
decltype(ircd::m::ed25519_key_dir)
|
|
|
|
ircd::m::ed25519_key_dir
|
|
|
|
{
|
|
|
|
{ "name", "ircd.keys.ed25519_key_dir" },
|
|
|
|
{ "default", fs::cwd() },
|
|
|
|
};
|
|
|
|
|
2019-10-04 23:05:47 +02:00
|
|
|
ircd::m::homeserver::key::key(const struct opts &opts)
|
|
|
|
:secret_key_path{[&opts]
|
|
|
|
() -> std::string
|
2019-10-01 05:50:58 +02:00
|
|
|
{
|
|
|
|
const std::string path_parts[]
|
|
|
|
{
|
|
|
|
std::string{ed25519_key_dir},
|
2019-10-04 23:05:47 +02:00
|
|
|
std::string{opts.origin} + ".ed25519"s,
|
2019-10-01 05:50:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const std::string sk_file
|
|
|
|
{
|
|
|
|
ircd::string(fs::PATH_MAX_LEN, [&](const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
return fs::path(buf, path_parts);
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2019-10-04 23:05:47 +02:00
|
|
|
if(!fs::exists(sk_file) && !ircd::write_avoid)
|
2019-10-01 05:50:58 +02:00
|
|
|
log::notice
|
|
|
|
{
|
|
|
|
m::log, "Creating ed25519 secret key @ `%s'", sk_file
|
|
|
|
};
|
|
|
|
|
2019-10-04 23:05:47 +02:00
|
|
|
return sk_file;
|
|
|
|
}()}
|
2019-10-03 21:15:23 +02:00
|
|
|
,secret_key
|
|
|
|
{
|
2020-09-16 02:40:10 +02:00
|
|
|
secret_key_path, &public_key, !ircd::write_avoid
|
2019-10-03 21:15:23 +02:00
|
|
|
}
|
|
|
|
,public_key_b64
|
|
|
|
{
|
|
|
|
ircd::string(96, [this](const mutable_buffer &buf)
|
|
|
|
{
|
2020-08-10 12:51:09 +02:00
|
|
|
return b64::encode_unpadded(buf, public_key);
|
2019-10-03 21:15:23 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
,public_key_id
|
|
|
|
{
|
2019-10-04 23:05:47 +02:00
|
|
|
"ed25519:"s + std::string{trunc(public_key_b64, 8)}
|
2019-10-03 21:15:23 +02:00
|
|
|
}
|
2019-10-04 22:21:56 +02:00
|
|
|
,verify_keys{[this, &opts]
|
|
|
|
() -> std::string
|
2019-10-01 05:50:58 +02:00
|
|
|
{
|
2019-10-04 22:21:56 +02:00
|
|
|
const json::strung verify_keys
|
|
|
|
{
|
|
|
|
json::members
|
2019-10-01 05:50:58 +02:00
|
|
|
{
|
2019-10-04 22:21:56 +02:00
|
|
|
{ public_key_id, json::member
|
|
|
|
{
|
2020-10-02 03:00:46 +02:00
|
|
|
"key", string_view{public_key_b64}
|
2019-10-04 22:21:56 +02:00
|
|
|
}}
|
2019-10-01 05:50:58 +02:00
|
|
|
}
|
2019-10-04 22:21:56 +02:00
|
|
|
};
|
2019-10-01 05:50:58 +02:00
|
|
|
|
2019-10-04 22:21:56 +02:00
|
|
|
const time_t ts
|
|
|
|
{
|
|
|
|
//TODO: XXX
|
|
|
|
ircd::time<milliseconds>() + (1000 * 60 * 60 * 24 * 7)
|
|
|
|
};
|
2019-10-01 05:50:58 +02:00
|
|
|
|
2019-10-04 22:21:56 +02:00
|
|
|
m::keys key;
|
|
|
|
json::get<"server_name"_>(key) = opts.origin;
|
|
|
|
json::get<"old_verify_keys"_>(key) = "{}";
|
|
|
|
json::get<"verify_keys"_>(key) = verify_keys;
|
|
|
|
json::get<"valid_until_ts"_>(key) = ts;
|
|
|
|
json::strung ret
|
2019-10-01 05:50:58 +02:00
|
|
|
{
|
2019-10-04 22:21:56 +02:00
|
|
|
key
|
2019-10-01 05:50:58 +02:00
|
|
|
};
|
|
|
|
|
2020-09-11 07:55:19 +02:00
|
|
|
if(!secret_key)
|
|
|
|
return std::move(ret);
|
|
|
|
|
2019-10-01 05:50:58 +02:00
|
|
|
const ed25519::sig sig
|
|
|
|
{
|
2019-10-04 22:21:56 +02:00
|
|
|
secret_key.sign(const_buffer(ret))
|
2019-10-01 05:50:58 +02:00
|
|
|
};
|
|
|
|
|
2019-10-04 22:21:56 +02:00
|
|
|
char buf[2][512];
|
|
|
|
const json::object sigs
|
2019-10-01 05:50:58 +02:00
|
|
|
{
|
2019-10-04 22:21:56 +02:00
|
|
|
json::stringify(mutable_buffer(buf[0]), json::members
|
2019-10-01 05:50:58 +02:00
|
|
|
{
|
2019-10-04 23:05:47 +02:00
|
|
|
{ opts.origin, json::member
|
|
|
|
{
|
2020-08-10 12:51:09 +02:00
|
|
|
public_key_id, b64::encode_unpadded(buf[1], sig)
|
2019-10-04 23:05:47 +02:00
|
|
|
}}
|
2019-10-04 22:21:56 +02:00
|
|
|
})
|
|
|
|
};
|
2019-10-01 05:50:58 +02:00
|
|
|
|
2019-10-04 22:21:56 +02:00
|
|
|
json::get<"signatures"_>(key) = sigs;
|
|
|
|
ret = json::strung
|
|
|
|
{
|
|
|
|
key
|
|
|
|
};
|
|
|
|
|
2020-02-12 20:30:37 +01:00
|
|
|
return std::move(ret);
|
2019-10-04 22:21:56 +02:00
|
|
|
}()}
|
|
|
|
{
|
2020-09-11 07:55:19 +02:00
|
|
|
if(!secret_key)
|
|
|
|
{
|
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
m::log, "Cannot issue events originating from '%s' :No signing key available.",
|
|
|
|
opts.origin,
|
|
|
|
};
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-04 23:05:47 +02:00
|
|
|
log::info
|
|
|
|
{
|
|
|
|
m::log, "Secret key for %s at `%s'. Public key is %s identified as '%s'",
|
|
|
|
opts.origin,
|
|
|
|
secret_key_path,
|
|
|
|
public_key_b64,
|
|
|
|
public_key_id,
|
|
|
|
};
|
2019-10-01 05:50:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2019-10-04 21:48:54 +02:00
|
|
|
// homeserver::conf
|
2019-10-01 05:50:58 +02:00
|
|
|
//
|
|
|
|
|
2019-10-04 21:48:54 +02:00
|
|
|
namespace ircd::m
|
2019-10-01 05:50:58 +02:00
|
|
|
{
|
2019-10-04 21:48:54 +02:00
|
|
|
static bool load_conf_item(const event &);
|
|
|
|
static bool load_conf_item(const event::idx &);
|
|
|
|
static size_t load_conf_items(const room &, const string_view &prefix);
|
|
|
|
|
|
|
|
static void handle_conf_room_hook(const event &, vm::eval &);
|
|
|
|
static void handle_item_init(const room &, conf::item<> &);
|
2019-10-01 05:50:58 +02:00
|
|
|
}
|
|
|
|
|
2019-10-04 21:48:54 +02:00
|
|
|
//
|
|
|
|
// homeserver::conf::conf
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::homeserver::conf::conf(const struct opts &opts)
|
|
|
|
:room_id
|
2019-10-01 05:50:58 +02:00
|
|
|
{
|
2019-10-04 21:48:54 +02:00
|
|
|
"conf", opts.origin
|
|
|
|
}
|
|
|
|
,room
|
|
|
|
{
|
|
|
|
room_id
|
|
|
|
}
|
|
|
|
,item_init
|
|
|
|
{
|
|
|
|
ircd::conf::on_init, [this](ircd::conf::item<> &item)
|
2019-10-01 05:50:58 +02:00
|
|
|
{
|
2019-10-04 21:48:54 +02:00
|
|
|
handle_item_init(room, item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
,conf_updated
|
|
|
|
{
|
|
|
|
handle_conf_room_hook,
|
|
|
|
{
|
|
|
|
{ "_site", "vm.effect" },
|
|
|
|
{ "room_id", room_id },
|
|
|
|
{ "type", "ircd.conf.item" },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
2020-03-08 22:29:22 +01:00
|
|
|
for(const auto &[key, item] : ircd::conf::items) try
|
|
|
|
{
|
|
|
|
assert(item);
|
|
|
|
if(item->set_cb)
|
|
|
|
item->set_cb();
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
"Failed to initialize conf item '%s' :%s",
|
|
|
|
key,
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
2019-10-04 21:48:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::homeserver::conf::store(const string_view &prefix,
|
|
|
|
const bool &force)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
size_t ret(0);
|
|
|
|
for(const auto &[key, item] : ircd::conf::items) try
|
|
|
|
{
|
|
|
|
if(prefix && !startswith(key, prefix))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Conf items marked with a persist=false property are not written
|
|
|
|
// to the conf room; regardless of force=true
|
|
|
|
if(!item->feature.get("persist", true))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
thread_local char buf[4_KiB];
|
|
|
|
const auto &val
|
2019-10-01 05:50:58 +02:00
|
|
|
{
|
2019-10-04 21:48:54 +02:00
|
|
|
item->get(buf)
|
2019-10-01 05:50:58 +02:00
|
|
|
};
|
|
|
|
|
2019-10-04 21:48:54 +02:00
|
|
|
bool dup{false}, exists{false};
|
|
|
|
if(!force)
|
|
|
|
get(key, [&exists, &dup, &val](const string_view &val_)
|
|
|
|
{
|
|
|
|
exists = true;
|
|
|
|
dup = val == val_;
|
|
|
|
});
|
|
|
|
|
|
|
|
// No reason to store the same value
|
|
|
|
if(!force && dup)
|
2019-10-01 05:50:58 +02:00
|
|
|
continue;
|
|
|
|
|
2019-10-04 21:48:54 +02:00
|
|
|
const json::string &default_value
|
|
|
|
{
|
|
|
|
item->feature["default"]
|
|
|
|
};
|
|
|
|
|
|
|
|
// When the !conf room has nothing for a key, and this store request
|
|
|
|
// is asking us to write the default value, that is rejected here.
|
|
|
|
if(!force && !exists && val == default_value)
|
2019-10-01 05:50:58 +02:00
|
|
|
continue;
|
|
|
|
|
2019-10-04 21:48:54 +02:00
|
|
|
set(key, val);
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
"Failed to create conf item '%s' :%s",
|
|
|
|
key,
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::homeserver::conf::load(const string_view &prefix)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return load_conf_items(room, prefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::homeserver::conf::defaults(const string_view &prefix)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
size_t ret(0);
|
|
|
|
for(const auto &[key, item] : ircd::conf::items)
|
|
|
|
{
|
|
|
|
if(prefix && !startswith(key, prefix))
|
2019-10-01 05:50:58 +02:00
|
|
|
continue;
|
|
|
|
|
2019-10-04 21:48:54 +02:00
|
|
|
assert(item);
|
|
|
|
const json::string &default_value
|
|
|
|
{
|
|
|
|
item->feature["default"]
|
|
|
|
};
|
|
|
|
|
|
|
|
ret += ircd::conf::set(key, default_value);
|
2019-10-01 05:50:58 +02:00
|
|
|
}
|
|
|
|
|
2019-10-04 21:48:54 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::homeserver::conf::set(const string_view &key,
|
|
|
|
const string_view &val)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
// Branch for conf items that do not persist. We don't send a message to
|
|
|
|
// the conf room to update them; the value is put directly into the item.
|
|
|
|
if(ircd::conf::exists(key) && !ircd::conf::persists(key))
|
|
|
|
{
|
|
|
|
ircd::conf::set(key, val);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const m::user::id::buf sender
|
|
|
|
{
|
|
|
|
"ircd", room_id.hostname()
|
|
|
|
};
|
|
|
|
|
|
|
|
return send(room, sender, "ircd.conf.item", key, json::members
|
|
|
|
{
|
|
|
|
{ "value", val }
|
|
|
|
});
|
2019-10-01 05:50:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-10-04 21:48:54 +02:00
|
|
|
ircd::m::homeserver::conf::get(const string_view &key,
|
|
|
|
const std::function<void (const string_view &)> &closure)
|
|
|
|
const
|
2019-10-01 05:50:58 +02:00
|
|
|
{
|
2019-10-04 21:48:54 +02:00
|
|
|
const auto &event_idx
|
2019-10-01 05:50:58 +02:00
|
|
|
{
|
2019-10-04 21:48:54 +02:00
|
|
|
room.get("ircd.conf.item", key)
|
2019-10-01 05:50:58 +02:00
|
|
|
};
|
|
|
|
|
2019-10-04 21:48:54 +02:00
|
|
|
return m::get(std::nothrow, event_idx, "content", [&closure]
|
|
|
|
(const json::object &content)
|
|
|
|
{
|
|
|
|
const json::string &value
|
|
|
|
{
|
|
|
|
content["value"]
|
|
|
|
};
|
|
|
|
|
|
|
|
closure(value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::handle_item_init(const room &room,
|
|
|
|
conf::item<> &item)
|
|
|
|
{
|
|
|
|
const auto event_idx
|
|
|
|
{
|
|
|
|
room.get(std::nothrow, "ircd.conf.item", item.name)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!event_idx)
|
|
|
|
return;
|
|
|
|
|
|
|
|
load_conf_item(event_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::handle_conf_room_hook(const event &event,
|
|
|
|
vm::eval &eval)
|
|
|
|
{
|
|
|
|
if(!homeserver::primary)
|
|
|
|
return;
|
|
|
|
|
|
|
|
assert(homeserver::primary->conf);
|
|
|
|
const m::room::id &primary_room
|
|
|
|
{
|
|
|
|
homeserver::primary->conf->room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
// Only the primary homeserver controls the global conf items.
|
|
|
|
if(json::get<"room_id"_>(event) != primary_room)
|
|
|
|
return;
|
|
|
|
|
|
|
|
load_conf_item(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::load_conf_items(const m::room &room,
|
|
|
|
const string_view &prefix)
|
|
|
|
{
|
|
|
|
const m::room::state state
|
|
|
|
{
|
|
|
|
room
|
|
|
|
};
|
|
|
|
|
|
|
|
state.for_each("ircd.conf.item", [&prefix]
|
|
|
|
(const auto &, const auto &state_key, const auto &event_idx)
|
|
|
|
{
|
|
|
|
static const m::event::fetch::opts fopts
|
|
|
|
{
|
|
|
|
m::event::keys::include { "content", "state_key" }
|
|
|
|
};
|
|
|
|
|
|
|
|
if(prefix && !startswith(state_key, prefix))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
m::prefetch(event_idx, fopts);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
size_t ret(0);
|
|
|
|
state.for_each("ircd.conf.item", [&ret, &prefix]
|
|
|
|
(const auto &, const auto &state_key, const auto &event_idx)
|
|
|
|
{
|
|
|
|
if(prefix && !startswith(state_key, prefix))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(!conf::exists(state_key))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
ret += load_conf_item(event_idx);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::load_conf_item(const m::event::idx &event_idx)
|
|
|
|
{
|
|
|
|
static const m::event::fetch::opts fopts
|
|
|
|
{
|
|
|
|
m::event::keys::include { "content", "state_key" }
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::event::fetch event
|
|
|
|
{
|
2020-04-03 04:50:00 +02:00
|
|
|
std::nothrow, event_idx, fopts
|
2019-10-04 21:48:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return event.valid && load_conf_item(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::load_conf_item(const m::event &event)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto &key
|
|
|
|
{
|
|
|
|
at<"state_key"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &content
|
|
|
|
{
|
|
|
|
at<"content"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::string &value
|
|
|
|
{
|
|
|
|
content.get("value")
|
|
|
|
};
|
|
|
|
|
|
|
|
// Conf items marked with a persist=false property are not read from
|
|
|
|
// the conf room into the item, even if the value exists in the room.
|
|
|
|
if(conf::exists(key) && !conf::persists(key))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
"Updating conf [%s] => [%s]", key, value
|
|
|
|
};
|
|
|
|
|
|
|
|
ircd::conf::set(key, value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
"Failed to set conf item '%s' :%s",
|
|
|
|
json::get<"state_key"_>(event),
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
2019-10-01 05:50:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// signon/signoff greetings
|
|
|
|
//
|
|
|
|
|
2019-10-03 21:15:23 +02:00
|
|
|
decltype(ircd::m::online_status_msg)
|
|
|
|
ircd::m::online_status_msg
|
2019-10-01 05:50:58 +02:00
|
|
|
{
|
|
|
|
{ "name", "ircd.me.online.status_msg" },
|
|
|
|
{ "default", "Wanna chat? IRCd at your service!" }
|
|
|
|
};
|
|
|
|
|
2019-10-03 21:15:23 +02:00
|
|
|
decltype(ircd::m::offline_status_msg)
|
|
|
|
ircd::m::offline_status_msg
|
2019-10-01 05:50:58 +02:00
|
|
|
{
|
|
|
|
{ "name", "ircd.me.offline.status_msg" },
|
|
|
|
{ "default", "Catch ya on the flip side..." }
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2019-10-03 21:15:23 +02:00
|
|
|
ircd::m::signon(homeserver &homeserver)
|
2019-10-01 05:50:58 +02:00
|
|
|
{
|
2020-09-16 03:01:40 +02:00
|
|
|
if(vm::sequence::retired != 0)
|
2019-10-03 21:15:23 +02:00
|
|
|
presence::set(homeserver.self, "online", online_status_msg);
|
2019-10-01 05:50:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-10-03 21:15:23 +02:00
|
|
|
ircd::m::signoff(homeserver &homeserver)
|
2020-05-10 04:41:10 +02:00
|
|
|
noexcept try
|
2019-10-01 05:50:58 +02:00
|
|
|
{
|
2020-09-16 03:01:40 +02:00
|
|
|
if(!std::uncaught_exceptions() && vm::sequence::retired != 0)
|
2019-10-03 21:15:23 +02:00
|
|
|
presence::set(homeserver.self, "offline", offline_status_msg);
|
2019-10-01 05:50:58 +02:00
|
|
|
}
|
2020-05-10 04:41:10 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "homeserver signoff failure :%s",
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
log, "Unknown error during homeserver signoff"
|
|
|
|
};
|
|
|
|
}
|
2019-10-01 05:50:58 +02:00
|
|
|
|
2019-10-04 21:48:54 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// m/self.h
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// !!! DEPRECATED !!!
|
|
|
|
//
|
|
|
|
// These items are being replaced, but their widespread use throughout the
|
|
|
|
// codebase is keeping them here for now.
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace ircd::m::self
|
|
|
|
{
|
|
|
|
static bool match(const net::hostport &a, const net::hostport &b) noexcept;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get network name (origin) of the primary homeserver. Use of this function
|
|
|
|
/// is discouraged, though it's not marked as deprecated to reduce warnings
|
|
|
|
/// for now until an actual effort is made to eliminate all callsites. Instead
|
|
|
|
/// of using this function, try to obtain a more specific homeserver instance
|
|
|
|
/// being hosted from this server based on the context of the callsite.
|
|
|
|
ircd::string_view
|
|
|
|
ircd::m::self::my_host()
|
|
|
|
{
|
|
|
|
return origin(my());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::self::my_host(const string_view &name)
|
|
|
|
{
|
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
homeserver::map.find(name)
|
|
|
|
};
|
|
|
|
|
|
|
|
return it != end(homeserver::map);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Determine if argument string is one of my homeserver's network names. This
|
|
|
|
/// is not a simple string comparison; strings postfixed with port :8448 are
|
|
|
|
/// compared equal to strings without a port.
|
|
|
|
bool
|
|
|
|
ircd::m::self::host(const string_view &other)
|
|
|
|
{
|
2020-03-05 18:48:17 +01:00
|
|
|
assert(m::canon_port == 8448);
|
2019-10-04 21:48:54 +02:00
|
|
|
const net::hostport other_host{other};
|
|
|
|
for(const auto &[my_network, hs_p] : homeserver::map)
|
|
|
|
if(match(my_network, other))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::self::match(const net::hostport &a,
|
|
|
|
const net::hostport &b)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
// port() is 0 when the origin has no port (and implies 8448)
|
|
|
|
const auto my_port
|
|
|
|
{
|
2020-03-05 18:48:17 +01:00
|
|
|
port(a)?: m::canon_port
|
2019-10-04 21:48:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// If my_host has a non-canonical port number, then the argument must
|
|
|
|
// also have the same port number, or there is no possible match.
|
2020-03-05 18:48:17 +01:00
|
|
|
if(my_port != m::canon_port)
|
2019-10-04 21:48:54 +02:00
|
|
|
return my_port == port(b) && host(a) == host(b);
|
|
|
|
|
|
|
|
// Since my host is on the canonical port, if other host has some
|
|
|
|
// different port number, there is no possible match.
|
2020-03-05 18:48:17 +01:00
|
|
|
if(port(b) != m::canon_port)
|
2019-10-04 21:48:54 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Both myself and input are using 8448; now the name has to match.
|
|
|
|
return host(a) == host(b);
|
|
|
|
}
|