federation_server.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from twisted.internet import defer
  16. from .federation_base import FederationBase
  17. from .units import Transaction, Edu
  18. from synapse.util.async import Linearizer
  19. from synapse.util.logutils import log_function
  20. from synapse.util.caches.response_cache import ResponseCache
  21. from synapse.events import FrozenEvent
  22. import synapse.metrics
  23. from synapse.api.errors import AuthError, FederationError, SynapseError
  24. from synapse.crypto.event_signing import compute_event_signature
  25. import simplejson as json
  26. import logging
  27. logger = logging.getLogger(__name__)
  28. # synapse.federation.federation_server is a silly name
  29. metrics = synapse.metrics.get_metrics_for("synapse.federation.server")
  30. received_pdus_counter = metrics.register_counter("received_pdus")
  31. received_edus_counter = metrics.register_counter("received_edus")
  32. received_queries_counter = metrics.register_counter("received_queries", labels=["type"])
  33. class FederationServer(FederationBase):
  34. def __init__(self, hs):
  35. super(FederationServer, self).__init__(hs)
  36. self.auth = hs.get_auth()
  37. self._room_pdu_linearizer = Linearizer()
  38. self._server_linearizer = Linearizer()
  39. # We cache responses to state queries, as they take a while and often
  40. # come in waves.
  41. self._state_resp_cache = ResponseCache(hs, timeout_ms=30000)
  42. def set_handler(self, handler):
  43. """Sets the handler that the replication layer will use to communicate
  44. receipt of new PDUs from other home servers. The required methods are
  45. documented on :py:class:`.ReplicationHandler`.
  46. """
  47. self.handler = handler
  48. def register_edu_handler(self, edu_type, handler):
  49. if edu_type in self.edu_handlers:
  50. raise KeyError("Already have an EDU handler for %s" % (edu_type,))
  51. self.edu_handlers[edu_type] = handler
  52. def register_query_handler(self, query_type, handler):
  53. """Sets the handler callable that will be used to handle an incoming
  54. federation Query of the given type.
  55. Args:
  56. query_type (str): Category name of the query, which should match
  57. the string used by make_query.
  58. handler (callable): Invoked to handle incoming queries of this type
  59. handler is invoked as:
  60. result = handler(args)
  61. where 'args' is a dict mapping strings to strings of the query
  62. arguments. It should return a Deferred that will eventually yield an
  63. object to encode as JSON.
  64. """
  65. if query_type in self.query_handlers:
  66. raise KeyError(
  67. "Already have a Query handler for %s" % (query_type,)
  68. )
  69. self.query_handlers[query_type] = handler
  70. @defer.inlineCallbacks
  71. @log_function
  72. def on_backfill_request(self, origin, room_id, versions, limit):
  73. with (yield self._server_linearizer.queue((origin, room_id))):
  74. pdus = yield self.handler.on_backfill_request(
  75. origin, room_id, versions, limit
  76. )
  77. res = self._transaction_from_pdus(pdus).get_dict()
  78. defer.returnValue((200, res))
  79. @defer.inlineCallbacks
  80. @log_function
  81. def on_incoming_transaction(self, transaction_data):
  82. transaction = Transaction(**transaction_data)
  83. received_pdus_counter.inc_by(len(transaction.pdus))
  84. for p in transaction.pdus:
  85. if "unsigned" in p:
  86. unsigned = p["unsigned"]
  87. if "age" in unsigned:
  88. p["age"] = unsigned["age"]
  89. if "age" in p:
  90. p["age_ts"] = int(self._clock.time_msec()) - int(p["age"])
  91. del p["age"]
  92. pdu_list = [
  93. self.event_from_pdu_json(p) for p in transaction.pdus
  94. ]
  95. logger.debug("[%s] Got transaction", transaction.transaction_id)
  96. response = yield self.transaction_actions.have_responded(transaction)
  97. if response:
  98. logger.debug(
  99. "[%s] We've already responed to this request",
  100. transaction.transaction_id
  101. )
  102. defer.returnValue(response)
  103. return
  104. logger.debug("[%s] Transaction is new", transaction.transaction_id)
  105. results = []
  106. for pdu in pdu_list:
  107. try:
  108. yield self._handle_new_pdu(transaction.origin, pdu)
  109. results.append({})
  110. except FederationError as e:
  111. self.send_failure(e, transaction.origin)
  112. results.append({"error": str(e)})
  113. except Exception as e:
  114. results.append({"error": str(e)})
  115. logger.exception("Failed to handle PDU")
  116. if hasattr(transaction, "edus"):
  117. for edu in (Edu(**x) for x in transaction.edus):
  118. yield self.received_edu(
  119. transaction.origin,
  120. edu.edu_type,
  121. edu.content
  122. )
  123. for failure in getattr(transaction, "pdu_failures", []):
  124. logger.info("Got failure %r", failure)
  125. logger.debug("Returning: %s", str(results))
  126. response = {
  127. "pdus": dict(zip(
  128. (p.event_id for p in pdu_list), results
  129. )),
  130. }
  131. yield self.transaction_actions.set_response(
  132. transaction,
  133. 200, response
  134. )
  135. defer.returnValue((200, response))
  136. @defer.inlineCallbacks
  137. def received_edu(self, origin, edu_type, content):
  138. received_edus_counter.inc()
  139. if edu_type in self.edu_handlers:
  140. try:
  141. yield self.edu_handlers[edu_type](origin, content)
  142. except SynapseError as e:
  143. logger.info("Failed to handle edu %r: %r", edu_type, e)
  144. except Exception as e:
  145. logger.exception("Failed to handle edu %r", edu_type)
  146. else:
  147. logger.warn("Received EDU of type %s with no handler", edu_type)
  148. @defer.inlineCallbacks
  149. @log_function
  150. def on_context_state_request(self, origin, room_id, event_id):
  151. if not event_id:
  152. raise NotImplementedError("Specify an event")
  153. in_room = yield self.auth.check_host_in_room(room_id, origin)
  154. if not in_room:
  155. raise AuthError(403, "Host not in room.")
  156. result = self._state_resp_cache.get((room_id, event_id))
  157. if not result:
  158. with (yield self._server_linearizer.queue((origin, room_id))):
  159. resp = yield self._state_resp_cache.set(
  160. (room_id, event_id),
  161. self._on_context_state_request_compute(room_id, event_id)
  162. )
  163. else:
  164. resp = yield result
  165. defer.returnValue((200, resp))
  166. @defer.inlineCallbacks
  167. def on_state_ids_request(self, origin, room_id, event_id):
  168. if not event_id:
  169. raise NotImplementedError("Specify an event")
  170. in_room = yield self.auth.check_host_in_room(room_id, origin)
  171. if not in_room:
  172. raise AuthError(403, "Host not in room.")
  173. state_ids = yield self.handler.get_state_ids_for_pdu(
  174. room_id, event_id,
  175. )
  176. auth_chain_ids = yield self.store.get_auth_chain_ids(state_ids)
  177. defer.returnValue((200, {
  178. "pdu_ids": state_ids,
  179. "auth_chain_ids": auth_chain_ids,
  180. }))
  181. @defer.inlineCallbacks
  182. def _on_context_state_request_compute(self, room_id, event_id):
  183. pdus = yield self.handler.get_state_for_pdu(
  184. room_id, event_id,
  185. )
  186. auth_chain = yield self.store.get_auth_chain(
  187. [pdu.event_id for pdu in pdus]
  188. )
  189. for event in auth_chain:
  190. # We sign these again because there was a bug where we
  191. # incorrectly signed things the first time round
  192. if self.hs.is_mine_id(event.event_id):
  193. event.signatures.update(
  194. compute_event_signature(
  195. event,
  196. self.hs.hostname,
  197. self.hs.config.signing_key[0]
  198. )
  199. )
  200. defer.returnValue({
  201. "pdus": [pdu.get_pdu_json() for pdu in pdus],
  202. "auth_chain": [pdu.get_pdu_json() for pdu in auth_chain],
  203. })
  204. @defer.inlineCallbacks
  205. @log_function
  206. def on_pdu_request(self, origin, event_id):
  207. pdu = yield self._get_persisted_pdu(origin, event_id)
  208. if pdu:
  209. defer.returnValue(
  210. (200, self._transaction_from_pdus([pdu]).get_dict())
  211. )
  212. else:
  213. defer.returnValue((404, ""))
  214. @defer.inlineCallbacks
  215. @log_function
  216. def on_pull_request(self, origin, versions):
  217. raise NotImplementedError("Pull transactions not implemented")
  218. @defer.inlineCallbacks
  219. def on_query_request(self, query_type, args):
  220. received_queries_counter.inc(query_type)
  221. if query_type in self.query_handlers:
  222. response = yield self.query_handlers[query_type](args)
  223. defer.returnValue((200, response))
  224. else:
  225. defer.returnValue(
  226. (404, "No handler for Query type '%s'" % (query_type,))
  227. )
  228. @defer.inlineCallbacks
  229. def on_make_join_request(self, room_id, user_id):
  230. pdu = yield self.handler.on_make_join_request(room_id, user_id)
  231. time_now = self._clock.time_msec()
  232. defer.returnValue({"event": pdu.get_pdu_json(time_now)})
  233. @defer.inlineCallbacks
  234. def on_invite_request(self, origin, content):
  235. pdu = self.event_from_pdu_json(content)
  236. ret_pdu = yield self.handler.on_invite_request(origin, pdu)
  237. time_now = self._clock.time_msec()
  238. defer.returnValue((200, {"event": ret_pdu.get_pdu_json(time_now)}))
  239. @defer.inlineCallbacks
  240. def on_send_join_request(self, origin, content):
  241. logger.debug("on_send_join_request: content: %s", content)
  242. pdu = self.event_from_pdu_json(content)
  243. logger.debug("on_send_join_request: pdu sigs: %s", pdu.signatures)
  244. res_pdus = yield self.handler.on_send_join_request(origin, pdu)
  245. time_now = self._clock.time_msec()
  246. defer.returnValue((200, {
  247. "state": [p.get_pdu_json(time_now) for p in res_pdus["state"]],
  248. "auth_chain": [
  249. p.get_pdu_json(time_now) for p in res_pdus["auth_chain"]
  250. ],
  251. }))
  252. @defer.inlineCallbacks
  253. def on_make_leave_request(self, room_id, user_id):
  254. pdu = yield self.handler.on_make_leave_request(room_id, user_id)
  255. time_now = self._clock.time_msec()
  256. defer.returnValue({"event": pdu.get_pdu_json(time_now)})
  257. @defer.inlineCallbacks
  258. def on_send_leave_request(self, origin, content):
  259. logger.debug("on_send_leave_request: content: %s", content)
  260. pdu = self.event_from_pdu_json(content)
  261. logger.debug("on_send_leave_request: pdu sigs: %s", pdu.signatures)
  262. yield self.handler.on_send_leave_request(origin, pdu)
  263. defer.returnValue((200, {}))
  264. @defer.inlineCallbacks
  265. def on_event_auth(self, origin, room_id, event_id):
  266. with (yield self._server_linearizer.queue((origin, room_id))):
  267. time_now = self._clock.time_msec()
  268. auth_pdus = yield self.handler.on_event_auth(event_id)
  269. res = {
  270. "auth_chain": [a.get_pdu_json(time_now) for a in auth_pdus],
  271. }
  272. defer.returnValue((200, res))
  273. @defer.inlineCallbacks
  274. def on_query_auth_request(self, origin, content, room_id, event_id):
  275. """
  276. Content is a dict with keys::
  277. auth_chain (list): A list of events that give the auth chain.
  278. missing (list): A list of event_ids indicating what the other
  279. side (`origin`) think we're missing.
  280. rejects (dict): A mapping from event_id to a 2-tuple of reason
  281. string and a proof (or None) of why the event was rejected.
  282. The keys of this dict give the list of events the `origin` has
  283. rejected.
  284. Args:
  285. origin (str)
  286. content (dict)
  287. event_id (str)
  288. Returns:
  289. Deferred: Results in `dict` with the same format as `content`
  290. """
  291. with (yield self._server_linearizer.queue((origin, room_id))):
  292. auth_chain = [
  293. self.event_from_pdu_json(e)
  294. for e in content["auth_chain"]
  295. ]
  296. signed_auth = yield self._check_sigs_and_hash_and_fetch(
  297. origin, auth_chain, outlier=True
  298. )
  299. ret = yield self.handler.on_query_auth(
  300. origin,
  301. event_id,
  302. signed_auth,
  303. content.get("rejects", []),
  304. content.get("missing", []),
  305. )
  306. time_now = self._clock.time_msec()
  307. send_content = {
  308. "auth_chain": [
  309. e.get_pdu_json(time_now)
  310. for e in ret["auth_chain"]
  311. ],
  312. "rejects": ret.get("rejects", []),
  313. "missing": ret.get("missing", []),
  314. }
  315. defer.returnValue(
  316. (200, send_content)
  317. )
  318. @log_function
  319. def on_query_client_keys(self, origin, content):
  320. return self.on_query_request("client_keys", content)
  321. @defer.inlineCallbacks
  322. @log_function
  323. def on_claim_client_keys(self, origin, content):
  324. query = []
  325. for user_id, device_keys in content.get("one_time_keys", {}).items():
  326. for device_id, algorithm in device_keys.items():
  327. query.append((user_id, device_id, algorithm))
  328. results = yield self.store.claim_e2e_one_time_keys(query)
  329. json_result = {}
  330. for user_id, device_keys in results.items():
  331. for device_id, keys in device_keys.items():
  332. for key_id, json_bytes in keys.items():
  333. json_result.setdefault(user_id, {})[device_id] = {
  334. key_id: json.loads(json_bytes)
  335. }
  336. defer.returnValue({"one_time_keys": json_result})
  337. @defer.inlineCallbacks
  338. @log_function
  339. def on_get_missing_events(self, origin, room_id, earliest_events,
  340. latest_events, limit, min_depth):
  341. with (yield self._server_linearizer.queue((origin, room_id))):
  342. logger.info(
  343. "on_get_missing_events: earliest_events: %r, latest_events: %r,"
  344. " limit: %d, min_depth: %d",
  345. earliest_events, latest_events, limit, min_depth
  346. )
  347. missing_events = yield self.handler.on_get_missing_events(
  348. origin, room_id, earliest_events, latest_events, limit, min_depth
  349. )
  350. if len(missing_events) < 5:
  351. logger.info(
  352. "Returning %d events: %r", len(missing_events), missing_events
  353. )
  354. else:
  355. logger.info("Returning %d events", len(missing_events))
  356. time_now = self._clock.time_msec()
  357. defer.returnValue({
  358. "events": [ev.get_pdu_json(time_now) for ev in missing_events],
  359. })
  360. @log_function
  361. def on_openid_userinfo(self, token):
  362. ts_now_ms = self._clock.time_msec()
  363. return self.store.get_user_id_for_open_id_token(token, ts_now_ms)
  364. @log_function
  365. def _get_persisted_pdu(self, origin, event_id, do_auth=True):
  366. """ Get a PDU from the database with given origin and id.
  367. Returns:
  368. Deferred: Results in a `Pdu`.
  369. """
  370. return self.handler.get_persisted_pdu(
  371. origin, event_id, do_auth=do_auth
  372. )
  373. def _transaction_from_pdus(self, pdu_list):
  374. """Returns a new Transaction containing the given PDUs suitable for
  375. transmission.
  376. """
  377. time_now = self._clock.time_msec()
  378. pdus = [p.get_pdu_json(time_now) for p in pdu_list]
  379. return Transaction(
  380. origin=self.server_name,
  381. pdus=pdus,
  382. origin_server_ts=int(time_now),
  383. destination=None,
  384. )
  385. @defer.inlineCallbacks
  386. @log_function
  387. def _handle_new_pdu(self, origin, pdu, get_missing=True):
  388. # We reprocess pdus when we have seen them only as outliers
  389. existing = yield self._get_persisted_pdu(
  390. origin, pdu.event_id, do_auth=False
  391. )
  392. # FIXME: Currently we fetch an event again when we already have it
  393. # if it has been marked as an outlier.
  394. already_seen = (
  395. existing and (
  396. not existing.internal_metadata.is_outlier()
  397. or pdu.internal_metadata.is_outlier()
  398. )
  399. )
  400. if already_seen:
  401. logger.debug("Already seen pdu %s", pdu.event_id)
  402. return
  403. # Check signature.
  404. try:
  405. pdu = yield self._check_sigs_and_hash(pdu)
  406. except SynapseError as e:
  407. raise FederationError(
  408. "ERROR",
  409. e.code,
  410. e.msg,
  411. affected=pdu.event_id,
  412. )
  413. state = None
  414. auth_chain = []
  415. have_seen = yield self.store.have_events(
  416. [ev for ev, _ in pdu.prev_events]
  417. )
  418. fetch_state = False
  419. # Get missing pdus if necessary.
  420. if not pdu.internal_metadata.is_outlier():
  421. # We only backfill backwards to the min depth.
  422. min_depth = yield self.handler.get_min_depth_for_context(
  423. pdu.room_id
  424. )
  425. logger.debug(
  426. "_handle_new_pdu min_depth for %s: %d",
  427. pdu.room_id, min_depth
  428. )
  429. prevs = {e_id for e_id, _ in pdu.prev_events}
  430. seen = set(have_seen.keys())
  431. if min_depth and pdu.depth < min_depth:
  432. # This is so that we don't notify the user about this
  433. # message, to work around the fact that some events will
  434. # reference really really old events we really don't want to
  435. # send to the clients.
  436. pdu.internal_metadata.outlier = True
  437. elif min_depth and pdu.depth > min_depth:
  438. if get_missing and prevs - seen:
  439. # If we're missing stuff, ensure we only fetch stuff one
  440. # at a time.
  441. with (yield self._room_pdu_linearizer.queue(pdu.room_id)):
  442. # We recalculate seen, since it may have changed.
  443. have_seen = yield self.store.have_events(prevs)
  444. seen = set(have_seen.keys())
  445. if prevs - seen:
  446. latest = yield self.store.get_latest_event_ids_in_room(
  447. pdu.room_id
  448. )
  449. # We add the prev events that we have seen to the latest
  450. # list to ensure the remote server doesn't give them to us
  451. latest = set(latest)
  452. latest |= seen
  453. logger.info(
  454. "Missing %d events for room %r: %r...",
  455. len(prevs - seen), pdu.room_id, list(prevs - seen)[:5]
  456. )
  457. missing_events = yield self.get_missing_events(
  458. origin,
  459. pdu.room_id,
  460. earliest_events_ids=list(latest),
  461. latest_events=[pdu],
  462. limit=10,
  463. min_depth=min_depth,
  464. )
  465. # We want to sort these by depth so we process them and
  466. # tell clients about them in order.
  467. missing_events.sort(key=lambda x: x.depth)
  468. for e in missing_events:
  469. yield self._handle_new_pdu(
  470. origin,
  471. e,
  472. get_missing=False
  473. )
  474. have_seen = yield self.store.have_events(
  475. [ev for ev, _ in pdu.prev_events]
  476. )
  477. prevs = {e_id for e_id, _ in pdu.prev_events}
  478. seen = set(have_seen.keys())
  479. if prevs - seen:
  480. logger.info(
  481. "Still missing %d events for room %r: %r...",
  482. len(prevs - seen), pdu.room_id, list(prevs - seen)[:5]
  483. )
  484. fetch_state = True
  485. if fetch_state:
  486. # We need to get the state at this event, since we haven't
  487. # processed all the prev events.
  488. logger.debug(
  489. "_handle_new_pdu getting state for %s",
  490. pdu.room_id
  491. )
  492. try:
  493. state, auth_chain = yield self.get_state_for_room(
  494. origin, pdu.room_id, pdu.event_id,
  495. )
  496. except:
  497. logger.exception("Failed to get state for event: %s", pdu.event_id)
  498. yield self.handler.on_receive_pdu(
  499. origin,
  500. pdu,
  501. state=state,
  502. auth_chain=auth_chain,
  503. )
  504. def __str__(self):
  505. return "<ReplicationLayer(%s)>" % self.server_name
  506. def event_from_pdu_json(self, pdu_json, outlier=False):
  507. event = FrozenEvent(
  508. pdu_json
  509. )
  510. event.internal_metadata.outlier = outlier
  511. return event
  512. @defer.inlineCallbacks
  513. def exchange_third_party_invite(
  514. self,
  515. sender_user_id,
  516. target_user_id,
  517. room_id,
  518. signed,
  519. ):
  520. ret = yield self.handler.exchange_third_party_invite(
  521. sender_user_id,
  522. target_user_id,
  523. room_id,
  524. signed,
  525. )
  526. defer.returnValue(ret)
  527. @defer.inlineCallbacks
  528. def on_exchange_third_party_invite_request(self, origin, room_id, event_dict):
  529. ret = yield self.handler.on_exchange_third_party_invite_request(
  530. origin, room_id, event_dict
  531. )
  532. defer.returnValue(ret)