casefold_db.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #!/usr/bin/env python
  2. # Copyright 2021 The Matrix.org Foundation C.I.C.
  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. import argparse
  16. import json
  17. import os
  18. import sqlite3
  19. import sys
  20. from typing import Any, Dict, List, Tuple
  21. import signedjson.sign
  22. from sydent.config import SydentConfig
  23. from sydent.sydent import Sydent
  24. from sydent.util import json_decoder
  25. from sydent.util.emailutils import sendEmail
  26. from sydent.util.hash import sha256_and_url_safe_base64
  27. from tests.utils import ResolvingMemoryReactorClock
  28. def calculate_lookup_hash(sydent, address):
  29. cur = sydent.db.cursor()
  30. pepper_result = cur.execute("SELECT lookup_pepper from hashing_metadata")
  31. pepper = pepper_result.fetchone()[0]
  32. combo = "%s %s %s" % (address, "email", pepper)
  33. lookup_hash = sha256_and_url_safe_base64(combo)
  34. return lookup_hash
  35. def update_local_associations(
  36. sydent: Sydent, db: sqlite3.Connection, send_email: bool, dry_run: bool
  37. ):
  38. """Update the DB table local_threepid_associations so that all stored
  39. emails are casefolded, and any duplicate mxid's associated with the
  40. given email are deleted.
  41. :return: None
  42. """
  43. cur = db.cursor()
  44. res = cur.execute(
  45. "SELECT address, mxid FROM local_threepid_associations WHERE medium = 'email'"
  46. "ORDER BY ts DESC"
  47. )
  48. # a dict that associates an email address with correspoinding mxids and lookup hashes
  49. associations: Dict[str, List[Tuple[str, str, str]]] = {}
  50. # iterate through selected associations, casefold email, rehash it, and add to
  51. # associations dict
  52. for address, mxid in res.fetchall():
  53. casefold_address = address.casefold()
  54. # rehash email since hashes are case-sensitive
  55. lookup_hash = calculate_lookup_hash(sydent, casefold_address)
  56. if casefold_address in associations:
  57. associations[casefold_address].append((address, mxid, lookup_hash))
  58. else:
  59. associations[casefold_address] = [(address, mxid, lookup_hash)]
  60. # list of arguments to update db with
  61. db_update_args: List[Tuple[str, str, str, str]] = []
  62. # list of mxids to delete
  63. to_delete: List[Tuple[str]] = []
  64. # list of mxids to send emails to letting them know the mxid has been deleted
  65. mxids: List[Tuple[str, str]] = []
  66. for casefold_address, assoc_tuples in associations.items():
  67. db_update_args.append(
  68. (
  69. casefold_address,
  70. assoc_tuples[0][2],
  71. assoc_tuples[0][0],
  72. assoc_tuples[0][1],
  73. )
  74. )
  75. if len(assoc_tuples) > 1:
  76. # Iterate over all associations except for the first one, since we've already
  77. # processed it.
  78. for address, mxid, _ in assoc_tuples[1:]:
  79. to_delete.append((address,))
  80. mxids.append((mxid, address))
  81. # iterate through the mxids and send email, let's only send one email per mxid
  82. if send_email and not dry_run:
  83. for mxid, address in mxids:
  84. processed_mxids = []
  85. if mxid in processed_mxids:
  86. continue
  87. else:
  88. if sydent.config.email.template is None:
  89. templateFile = sydent.get_branded_template(
  90. None,
  91. "migration_template.eml",
  92. )
  93. else:
  94. templateFile = sydent.config.email.template
  95. sendEmail(
  96. sydent,
  97. templateFile,
  98. address,
  99. {"mxid": "mxid", "subject_header_value": "MatrixID Update"},
  100. )
  101. processed_mxids.append(mxid)
  102. print(
  103. f"{len(to_delete)} rows to delete, {len(db_update_args)} rows to update in local_threepid_associations"
  104. )
  105. if not dry_run:
  106. if len(to_delete) > 0:
  107. cur.executemany(
  108. "DELETE FROM local_threepid_associations WHERE address = ?", to_delete
  109. )
  110. if len(db_update_args) > 0:
  111. cur.executemany(
  112. "UPDATE local_threepid_associations SET address = ?, lookup_hash = ? WHERE address = ? AND mxid = ?",
  113. db_update_args,
  114. )
  115. # We've finished updating the database, committing the transaction.
  116. db.commit()
  117. def update_global_associations(
  118. sydent: Sydent, db: sqlite3.Connection, send_email: bool, dry_run: bool
  119. ):
  120. """Update the DB table global_threepid_associations so that all stored
  121. emails are casefolded, the signed association is re-signed and any duplicate
  122. mxid's associated with the given email are deleted.
  123. :return: None
  124. """
  125. # get every row where the local server is origin server and medium is email
  126. origin_server = sydent.config.general.server_name
  127. medium = "email"
  128. cur = db.cursor()
  129. res = cur.execute(
  130. "SELECT address, mxid, sgAssoc FROM global_threepid_associations WHERE medium = ?"
  131. "AND originServer = ? ORDER BY ts DESC",
  132. (medium, origin_server),
  133. )
  134. # dict that stores email address with mxid, email address, lookup hash, and
  135. # signed association
  136. associations: Dict[str, List[Tuple[str, str, str, str]]] = {}
  137. # iterate through selected associations, casefold email, rehash it, re-sign the
  138. # associations and add to associations dict
  139. for address, mxid, sg_assoc in res.fetchall():
  140. casefold_address = address.casefold()
  141. # rehash the email since hash functions are case-sensitive
  142. lookup_hash = calculate_lookup_hash(sydent, casefold_address)
  143. # update signed associations with new casefolded address and re-sign
  144. sg_assoc = json_decoder.decode(sg_assoc)
  145. sg_assoc["address"] = address.casefold()
  146. sg_assoc = json.dumps(
  147. signedjson.sign.sign_json(
  148. sg_assoc, sydent.config.general.server_name, sydent.keyring.ed25519
  149. )
  150. )
  151. if casefold_address in associations:
  152. associations[casefold_address].append(
  153. (address, mxid, lookup_hash, sg_assoc)
  154. )
  155. else:
  156. associations[casefold_address] = [(address, mxid, lookup_hash, sg_assoc)]
  157. # list of arguments to update db with
  158. db_update_args: List[Tuple[Any, str, str, str, str]] = []
  159. # list of mxids to delete
  160. to_delete: List[Tuple[str]] = []
  161. for casefold_address, assoc_tuples in associations.items():
  162. db_update_args.append(
  163. (
  164. casefold_address,
  165. assoc_tuples[0][2],
  166. assoc_tuples[0][3],
  167. assoc_tuples[0][0],
  168. assoc_tuples[0][1],
  169. )
  170. )
  171. if len(assoc_tuples) > 1:
  172. # Iterate over all associations except for the first one, since we've already
  173. # processed it.
  174. for address, mxid, _, _ in assoc_tuples[1:]:
  175. to_delete.append((address,))
  176. print(
  177. f"{len(to_delete)} rows to delete, {len(db_update_args)} rows to update in global_threepid_associations"
  178. )
  179. if not dry_run:
  180. if len(to_delete) > 0:
  181. cur.executemany(
  182. "DELETE FROM global_threepid_associations WHERE address = ?", to_delete
  183. )
  184. if len(db_update_args) > 0:
  185. cur.executemany(
  186. "UPDATE global_threepid_associations SET address = ?, lookup_hash = ?, sgAssoc = ? WHERE address = ? AND mxid = ?",
  187. db_update_args,
  188. )
  189. db.commit()
  190. if __name__ == "__main__":
  191. parser = argparse.ArgumentParser(description="Casefold email addresses in database")
  192. parser.add_argument(
  193. "--no-email", action="store_true", help="run script but do not send emails"
  194. )
  195. parser.add_argument(
  196. "--dry-run",
  197. action="store_true",
  198. help="run script but do not send emails or alter database",
  199. )
  200. parser.add_argument("config_path", help="path to the sydent configuration file")
  201. args = parser.parse_args()
  202. # if the path the user gives us doesn't work, find it for them
  203. if not os.path.exists(args.config_path):
  204. print(f"The config file '{args.config_path}' does not exist.")
  205. sys.exit(1)
  206. sydent_config = SydentConfig()
  207. sydent_config.parse_config_file(args.config_path)
  208. reactor = ResolvingMemoryReactorClock()
  209. sydent = Sydent(sydent_config, reactor, False)
  210. update_global_associations(sydent, sydent.db, not args.no_email, args.dry_run)
  211. update_local_associations(sydent, sydent.db, not args.no_email, args.dry_run)