2019-02-21 03:34:22 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2019 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.
|
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
2019-08-26 02:35:47 +02:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
using user_devices_map = std::map<m::user::id, json::object>;
|
|
|
|
using host_users_map = std::map<string_view, user_devices_map>;
|
2020-03-06 04:35:20 +01:00
|
|
|
using query_map = std::map<string_view, m::fed::user::keys::claim>;
|
2019-08-26 02:35:47 +02:00
|
|
|
using failure_map = std::map<string_view, std::exception_ptr, std::less<>>;
|
|
|
|
using buffer_list = std::vector<unique_buffer<mutable_buffer>>;
|
|
|
|
}
|
2019-02-22 22:14:22 +01:00
|
|
|
|
|
|
|
static host_users_map
|
|
|
|
parse_user_request(const json::object &device_keys);
|
|
|
|
|
|
|
|
static bool
|
|
|
|
send_request(const string_view &,
|
|
|
|
const user_devices_map &,
|
|
|
|
failure_map &,
|
|
|
|
buffer_list &,
|
|
|
|
query_map &);
|
|
|
|
|
|
|
|
static query_map
|
|
|
|
send_requests(const host_users_map &,
|
|
|
|
buffer_list &,
|
|
|
|
failure_map &);
|
|
|
|
|
|
|
|
static void
|
|
|
|
recv_response(const string_view &,
|
2020-03-06 04:35:20 +01:00
|
|
|
m::fed::user::keys::claim &,
|
2019-02-22 22:14:22 +01:00
|
|
|
failure_map &,
|
|
|
|
json::stack::object &,
|
2019-09-23 05:02:23 +02:00
|
|
|
const system_point &);
|
2019-02-22 22:14:22 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
recv_responses(query_map &,
|
|
|
|
failure_map &,
|
|
|
|
json::stack::object &,
|
|
|
|
const milliseconds &);
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_failures(const failure_map &,
|
|
|
|
json::stack::object &);
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
static m::resource::response
|
2019-02-22 22:14:22 +01:00
|
|
|
post__keys_claim(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request);
|
2019-02-22 22:14:22 +01:00
|
|
|
|
2019-02-21 03:34:22 +01:00
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Client 14.11.5.2 :Key management API"
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
ircd::m::resource
|
2019-02-21 03:34:22 +01:00
|
|
|
claim_resource
|
|
|
|
{
|
|
|
|
"/_matrix/client/r0/keys/claim",
|
|
|
|
{
|
|
|
|
"(14.11.5.2.2) Keys claim",
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::method
|
2019-02-22 22:14:22 +01:00
|
|
|
method_post
|
|
|
|
{
|
|
|
|
claim_resource, "POST", post__keys_claim,
|
|
|
|
{
|
|
|
|
method_post.REQUIRES_AUTH
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
conf::item<milliseconds>
|
|
|
|
claim_timeout_default
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.keys.claim.timeout.default" },
|
2020-04-10 08:34:09 +02:00
|
|
|
{ "default", 20000L },
|
2019-02-22 22:14:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
conf::item<milliseconds>
|
|
|
|
claim_timeout_min
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.keys.claim.timeout.min" },
|
|
|
|
{ "default", 5000L },
|
|
|
|
};
|
|
|
|
|
|
|
|
conf::item<milliseconds>
|
|
|
|
claim_timeout_max
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.keys.claim.timeout.max" },
|
2020-04-10 08:34:09 +02:00
|
|
|
{ "default", 30000L },
|
2019-02-22 22:14:22 +01:00
|
|
|
};
|
|
|
|
|
2020-04-27 22:26:04 +02:00
|
|
|
conf::item<size_t>
|
|
|
|
claim_limit
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.keys.claim.limit" },
|
|
|
|
{ "default", 4096L },
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response
|
2019-02-21 03:34:22 +01:00
|
|
|
post__keys_claim(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request &request)
|
2019-02-21 03:34:22 +01:00
|
|
|
{
|
2019-10-10 22:33:43 +02:00
|
|
|
const milliseconds
|
|
|
|
timeout_min{claim_timeout_min},
|
|
|
|
timeout_max{claim_timeout_max},
|
|
|
|
timeout_default{claim_timeout_default},
|
|
|
|
timeout
|
2019-02-22 22:14:22 +01:00
|
|
|
{
|
2019-10-10 22:33:43 +02:00
|
|
|
std::clamp(request.get("timeout", timeout_default), timeout_min, timeout_max)
|
|
|
|
};
|
2019-02-22 22:14:22 +01:00
|
|
|
|
|
|
|
const json::object &one_time_keys
|
|
|
|
{
|
|
|
|
request.at("one_time_keys")
|
|
|
|
};
|
|
|
|
|
|
|
|
const host_users_map map
|
|
|
|
{
|
|
|
|
parse_user_request(one_time_keys)
|
|
|
|
};
|
|
|
|
|
|
|
|
buffer_list buffers;
|
|
|
|
failure_map failures;
|
|
|
|
query_map queries
|
|
|
|
{
|
|
|
|
send_requests(map, buffers, failures)
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response::chunked response
|
2019-02-21 03:34:22 +01:00
|
|
|
{
|
|
|
|
client, http::OK
|
|
|
|
};
|
2019-02-22 22:14:22 +01:00
|
|
|
|
|
|
|
json::stack out
|
|
|
|
{
|
|
|
|
response.buf, response.flusher()
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object top
|
|
|
|
{
|
|
|
|
out
|
|
|
|
};
|
|
|
|
|
|
|
|
recv_responses(queries, failures, top, timeout);
|
|
|
|
handle_failures(failures, top);
|
|
|
|
return {};
|
2019-02-21 03:34:22 +01:00
|
|
|
}
|
|
|
|
|
2019-02-22 22:14:22 +01:00
|
|
|
void
|
|
|
|
handle_failures(const failure_map &failures,
|
|
|
|
json::stack::object &out)
|
2019-02-21 03:34:22 +01:00
|
|
|
{
|
2019-02-22 22:14:22 +01:00
|
|
|
json::stack::object response_failures
|
2019-02-21 03:34:22 +01:00
|
|
|
{
|
2019-02-22 22:14:22 +01:00
|
|
|
out, "failures"
|
|
|
|
};
|
|
|
|
|
|
|
|
for(const auto &[hostname, eptr] : failures)
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
response_failures, hostname, what(eptr)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
recv_responses(query_map &queries,
|
|
|
|
failure_map &failures,
|
|
|
|
json::stack::object &out,
|
|
|
|
const milliseconds &timeout)
|
|
|
|
{
|
2019-09-23 05:02:23 +02:00
|
|
|
const system_point timedout
|
2019-02-22 22:14:22 +01:00
|
|
|
{
|
2019-09-23 05:02:23 +02:00
|
|
|
ircd::now<system_point>() + timeout
|
2019-02-22 22:14:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object one_time_keys
|
|
|
|
{
|
|
|
|
out, "one_time_keys"
|
|
|
|
};
|
|
|
|
|
|
|
|
for(auto &[remote, request] : queries)
|
|
|
|
{
|
|
|
|
assert(!failures.count(remote));
|
|
|
|
if(failures.count(remote))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
recv_response(remote, request, failures, one_time_keys, timedout);
|
2019-02-21 03:34:22 +01:00
|
|
|
}
|
2019-02-22 22:14:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
recv_response(const string_view &remote,
|
2020-03-06 04:35:20 +01:00
|
|
|
m::fed::user::keys::claim &request,
|
2019-02-22 22:14:22 +01:00
|
|
|
failure_map &failures,
|
|
|
|
json::stack::object &object,
|
2019-09-23 05:02:23 +02:00
|
|
|
const system_point &timeout)
|
2019-02-22 22:14:22 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto code
|
|
|
|
{
|
2020-04-01 01:02:14 +02:00
|
|
|
request.get_until(timeout)
|
2019-02-22 22:14:22 +01:00
|
|
|
};
|
|
|
|
|
2019-06-23 01:12:14 +02:00
|
|
|
const json::object response
|
|
|
|
{
|
|
|
|
request
|
|
|
|
};
|
|
|
|
|
2019-02-22 22:14:22 +01:00
|
|
|
const json::object &one_time_keys
|
|
|
|
{
|
|
|
|
response["one_time_keys"]
|
|
|
|
};
|
|
|
|
|
|
|
|
for(const auto &[user_id, keys] : one_time_keys)
|
|
|
|
json::stack::member
|
|
|
|
{
|
2020-04-01 01:02:14 +02:00
|
|
|
object, user_id, json::object{keys}
|
2019-02-22 22:14:22 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
m::log, "user keys claim from %s :%s",
|
|
|
|
remote,
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
failures.emplace(remote, std::current_exception());
|
|
|
|
}
|
|
|
|
|
|
|
|
query_map
|
|
|
|
send_requests(const host_users_map &hosts,
|
|
|
|
buffer_list &buffers,
|
|
|
|
failure_map &failures)
|
|
|
|
{
|
|
|
|
query_map ret;
|
|
|
|
for(const auto &[remote, user_devices] : hosts)
|
|
|
|
send_request(remote, user_devices, failures, buffers, ret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
send_request(const string_view &remote,
|
|
|
|
const user_devices_map &queries,
|
|
|
|
failure_map &failures,
|
|
|
|
buffer_list &buffers,
|
|
|
|
query_map &ret)
|
|
|
|
try
|
|
|
|
{
|
2020-04-27 22:13:26 +02:00
|
|
|
static const size_t buffer_unit_size
|
|
|
|
{
|
|
|
|
m::user::id::MAX_SIZE + 1 // 256
|
|
|
|
+ 128 // device_id
|
|
|
|
+ 128 // algorithm
|
|
|
|
};
|
|
|
|
|
2021-02-17 18:51:10 +01:00
|
|
|
static_assert(math::is_pow2(buffer_unit_size));
|
2020-04-27 22:13:26 +02:00
|
|
|
const size_t buffer_size
|
|
|
|
{
|
2020-04-28 01:02:38 +02:00
|
|
|
8_KiB + // headers
|
2020-04-27 22:26:04 +02:00
|
|
|
buffer_unit_size * std::min(queries.size(), size_t(claim_limit))
|
2020-04-27 22:13:26 +02:00
|
|
|
};
|
|
|
|
|
2019-02-22 22:14:22 +01:00
|
|
|
const auto &buffer
|
|
|
|
{
|
2020-04-27 22:13:26 +02:00
|
|
|
buffers.emplace_back(buffer_size)
|
2019-02-22 22:14:22 +01:00
|
|
|
};
|
|
|
|
|
2020-04-27 22:13:26 +02:00
|
|
|
m::fed::user::keys::claim::opts opts;
|
|
|
|
opts.remote = remote;
|
2019-02-22 22:14:22 +01:00
|
|
|
ret.emplace
|
|
|
|
(
|
|
|
|
std::piecewise_construct,
|
|
|
|
std::forward_as_tuple(remote),
|
|
|
|
std::forward_as_tuple(queries, buffer, std::move(opts))
|
|
|
|
);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
2020-04-17 00:46:20 +02:00
|
|
|
m::log, "user keys claim to %s for %zu users :%s",
|
2019-02-22 22:14:22 +01:00
|
|
|
remote,
|
2020-04-17 00:46:20 +02:00
|
|
|
queries.size(),
|
2019-02-22 22:14:22 +01:00
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
failures.emplace(remote, std::current_exception());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
host_users_map
|
|
|
|
parse_user_request(const json::object &one_time_keys)
|
|
|
|
{
|
|
|
|
host_users_map ret;
|
|
|
|
for(const auto &[user_id, devices] : one_time_keys)
|
|
|
|
{
|
|
|
|
const string_view &host
|
|
|
|
{
|
|
|
|
m::user::id{user_id}.host()
|
|
|
|
};
|
|
|
|
|
|
|
|
auto it(ret.lower_bound(host));
|
|
|
|
if(it == end(ret) || it->first != host)
|
|
|
|
it = ret.emplace_hint(it, host, user_devices_map{});
|
|
|
|
|
|
|
|
user_devices_map &users(it->second);
|
|
|
|
{
|
|
|
|
auto it(users.lower_bound(user_id));
|
|
|
|
if(it == end(users) || it->first != user_id)
|
|
|
|
it = users.emplace_hint(it, user_id, json::object{});
|
|
|
|
|
|
|
|
it->second = json::object{devices};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-23 01:04:36 +02:00
|
|
|
return ret;
|
2019-02-22 22:14:22 +01:00
|
|
|
}
|