sydent.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014 OpenMarket Ltd
  3. # Copyright 2018 New Vector Ltd
  4. # Copyright 2019 The Matrix.org Foundation C.I.C.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. from __future__ import absolute_import
  18. from six.moves import configparser
  19. import copy
  20. import logging
  21. import logging.handlers
  22. import os
  23. import twisted.internet.reactor
  24. from twisted.internet import task
  25. from twisted.python import log
  26. from sydent.db.sqlitedb import SqliteDatabase
  27. from sydent.http.httpcommon import SslComponents
  28. from sydent.http.httpserver import (
  29. ClientApiHttpServer, ReplicationHttpsServer,
  30. InternalApiHttpServer,
  31. )
  32. from sydent.http.httpsclient import ReplicationHttpsClient
  33. from sydent.http.servlets.blindlysignstuffservlet import BlindlySignStuffServlet
  34. from sydent.http.servlets.pubkeyservlets import EphemeralPubkeyIsValidServlet, PubkeyIsValidServlet
  35. from sydent.http.servlets.termsservlet import TermsServlet
  36. from sydent.validators.emailvalidator import EmailValidator
  37. from sydent.validators.msisdnvalidator import MsisdnValidator
  38. from sydent.hs_federation.verifier import Verifier
  39. from sydent.util.hash import sha256_and_url_safe_base64
  40. from sydent.util.tokenutils import generateAlphanumericTokenOfLength
  41. from sydent.sign.ed25519 import SydentEd25519
  42. from sydent.http.servlets.emailservlet import EmailRequestCodeServlet, EmailValidateCodeServlet
  43. from sydent.http.servlets.msisdnservlet import MsisdnRequestCodeServlet, MsisdnValidateCodeServlet
  44. from sydent.http.servlets.lookupservlet import LookupServlet
  45. from sydent.http.servlets.bulklookupservlet import BulkLookupServlet
  46. from sydent.http.servlets.lookupv2servlet import LookupV2Servlet
  47. from sydent.http.servlets.hashdetailsservlet import HashDetailsServlet
  48. from sydent.http.servlets.pubkeyservlets import Ed25519Servlet
  49. from sydent.http.servlets.threepidbindservlet import ThreePidBindServlet
  50. from sydent.http.servlets.threepidunbindservlet import ThreePidUnbindServlet
  51. from sydent.http.servlets.replication import ReplicationPushServlet
  52. from sydent.http.servlets.getvalidated3pidservlet import GetValidated3pidServlet
  53. from sydent.http.servlets.store_invite_servlet import StoreInviteServlet
  54. from sydent.http.servlets.v1_servlet import V1Servlet
  55. from sydent.http.servlets.accountservlet import AccountServlet
  56. from sydent.http.servlets.registerservlet import RegisterServlet
  57. from sydent.http.servlets.logoutservlet import LogoutServlet
  58. from sydent.http.servlets.v2_servlet import V2Servlet
  59. from sydent.db.valsession import ThreePidValSessionStore
  60. from sydent.db.hashing_metadata import HashingMetadataStore
  61. from sydent.threepid.bind import ThreepidBinder
  62. from sydent.replication.pusher import Pusher
  63. logger = logging.getLogger(__name__)
  64. CONFIG_DEFAULTS = {
  65. 'general': {
  66. 'server.name': '',
  67. 'log.path': '',
  68. 'log.level': 'INFO',
  69. 'pidfile.path': 'sydent.pid',
  70. 'terms.path': '',
  71. 'address_lookup_limit': '10000', # Maximum amount of addresses in a single /lookup request
  72. # The following can be added to your local config file to enable prometheus
  73. # support.
  74. # 'prometheus_port': '8080', # The port to serve metrics on
  75. # 'prometheus_addr': '', # The address to bind to. Empty string means bind to all.
  76. # The following can be added to your local config file to enable sentry support.
  77. # 'sentry_dsn': 'https://...' # The DSN has configured in the sentry instance project.
  78. # Whether clients and homeservers can register an association using v1 endpoints.
  79. 'enable_v1_associations': 'true',
  80. 'delete_tokens_on_bind': 'true',
  81. },
  82. 'db': {
  83. 'db.file': 'sydent.db',
  84. },
  85. 'http': {
  86. 'clientapi.http.bind_address': '::',
  87. 'clientapi.http.port': '8090',
  88. 'internalapi.http.bind_address': '::1',
  89. 'internalapi.http.port': '',
  90. 'replication.https.certfile': '',
  91. 'replication.https.cacert': '', # This should only be used for testing
  92. 'replication.https.bind_address': '::',
  93. 'replication.https.port': '4434',
  94. 'obey_x_forwarded_for': 'False',
  95. 'federation.verifycerts': 'True',
  96. 'verify_response_template': '',
  97. 'client_http_base': '',
  98. },
  99. 'email': {
  100. 'email.template': 'res/email.template',
  101. 'email.invite_template': 'res/invite.template',
  102. 'email.from': 'Sydent Validation <noreply@{hostname}>',
  103. 'email.subject': 'Your Validation Token',
  104. 'email.invite.subject': '%(sender_display_name)s has invited you to chat',
  105. 'email.smtphost': 'localhost',
  106. 'email.smtpport': '25',
  107. 'email.smtpusername': '',
  108. 'email.smtppassword': '',
  109. 'email.hostname': '',
  110. 'email.tlsmode': '0',
  111. },
  112. 'sms': {
  113. 'bodyTemplate': 'Your code is {token}',
  114. 'username': '',
  115. 'password': '',
  116. },
  117. 'crypto': {
  118. 'ed25519.signingkey': '',
  119. },
  120. }
  121. class Sydent:
  122. def __init__(self, cfg, reactor=twisted.internet.reactor):
  123. self.reactor = reactor
  124. self.config_file = get_config_file_path()
  125. self.cfg = cfg
  126. logger.info("Starting Sydent server")
  127. self.pidfile = self.cfg.get('general', "pidfile.path");
  128. self.db = SqliteDatabase(self).db
  129. self.server_name = self.cfg.get('general', 'server.name')
  130. if self.server_name == '':
  131. self.server_name = os.uname()[1]
  132. logger.warn(("You had not specified a server name. I have guessed that this server is called '%s' "
  133. + "and saved this in the config file. If this is incorrect, you should edit server.name in "
  134. + "the config file.") % (self.server_name,))
  135. self.cfg.set('general', 'server.name', self.server_name)
  136. self.save_config()
  137. if self.cfg.has_option("general", "sentry_dsn"):
  138. # Only import and start sentry SDK if configured.
  139. import sentry_sdk
  140. sentry_sdk.init(
  141. dsn=self.cfg.get("general", "sentry_dsn"),
  142. )
  143. with sentry_sdk.configure_scope() as scope:
  144. scope.set_tag("sydent_server_name", self.server_name)
  145. if self.cfg.has_option("general", "prometheus_port"):
  146. import prometheus_client
  147. prometheus_client.start_http_server(
  148. port=self.cfg.getint("general", "prometheus_port"),
  149. addr=self.cfg.get("general", "prometheus_addr"),
  150. )
  151. self.enable_v1_associations = parse_cfg_bool(
  152. self.cfg.get("general", "enable_v1_associations")
  153. )
  154. self.delete_tokens_on_bind = parse_cfg_bool(
  155. self.cfg.get("general", "delete_tokens_on_bind")
  156. )
  157. # See if a pepper already exists in the database
  158. # Note: This MUST be run before we start serving requests, otherwise lookups for
  159. # 3PID hashes may come in before we've completed generating them
  160. hashing_metadata_store = HashingMetadataStore(self)
  161. lookup_pepper = hashing_metadata_store.get_lookup_pepper()
  162. if not lookup_pepper:
  163. # No pepper defined in the database, generate one
  164. lookup_pepper = generateAlphanumericTokenOfLength(5)
  165. # Store it in the database and rehash 3PIDs
  166. hashing_metadata_store.store_lookup_pepper(sha256_and_url_safe_base64,
  167. lookup_pepper)
  168. self.validators = Validators()
  169. self.validators.email = EmailValidator(self)
  170. self.validators.msisdn = MsisdnValidator(self)
  171. self.keyring = Keyring()
  172. self.keyring.ed25519 = SydentEd25519(self).signing_key
  173. self.keyring.ed25519.alg = 'ed25519'
  174. self.sig_verifier = Verifier(self)
  175. self.servlets = Servlets()
  176. self.servlets.v1 = V1Servlet(self)
  177. self.servlets.v2 = V2Servlet(self)
  178. self.servlets.emailRequestCode = EmailRequestCodeServlet(self)
  179. self.servlets.emailValidate = EmailValidateCodeServlet(self)
  180. self.servlets.msisdnRequestCode = MsisdnRequestCodeServlet(self)
  181. self.servlets.msisdnValidate = MsisdnValidateCodeServlet(self)
  182. self.servlets.lookup = LookupServlet(self)
  183. self.servlets.bulk_lookup = BulkLookupServlet(self)
  184. self.servlets.hash_details = HashDetailsServlet(self, lookup_pepper)
  185. self.servlets.lookup_v2 = LookupV2Servlet(self, lookup_pepper)
  186. self.servlets.pubkey_ed25519 = Ed25519Servlet(self)
  187. self.servlets.pubkeyIsValid = PubkeyIsValidServlet(self)
  188. self.servlets.ephemeralPubkeyIsValid = EphemeralPubkeyIsValidServlet(self)
  189. self.servlets.threepidBind = ThreePidBindServlet(self)
  190. self.servlets.threepidUnbind = ThreePidUnbindServlet(self)
  191. self.servlets.replicationPush = ReplicationPushServlet(self)
  192. self.servlets.getValidated3pid = GetValidated3pidServlet(self)
  193. self.servlets.storeInviteServlet = StoreInviteServlet(self)
  194. self.servlets.blindlySignStuffServlet = BlindlySignStuffServlet(self)
  195. self.servlets.termsServlet = TermsServlet(self)
  196. self.servlets.accountServlet = AccountServlet(self)
  197. self.servlets.registerServlet = RegisterServlet(self)
  198. self.servlets.logoutServlet = LogoutServlet(self)
  199. self.threepidBinder = ThreepidBinder(self)
  200. self.sslComponents = SslComponents(self)
  201. self.clientApiHttpServer = ClientApiHttpServer(self)
  202. self.replicationHttpsServer = ReplicationHttpsServer(self)
  203. self.replicationHttpsClient = ReplicationHttpsClient(self)
  204. self.pusher = Pusher(self)
  205. # A dedicated validation session store just to clean up old sessions every N minutes
  206. self.cleanupValSession = ThreePidValSessionStore(self)
  207. cb = task.LoopingCall(self.cleanupValSession.deleteOldSessions)
  208. cb.clock = self.reactor
  209. cb.start(10 * 60.0)
  210. def save_config(self):
  211. fp = open(self.config_file, 'w')
  212. self.cfg.write(fp)
  213. fp.close()
  214. def run(self):
  215. self.clientApiHttpServer.setup()
  216. self.replicationHttpsServer.setup()
  217. self.pusher.setup()
  218. internalport = self.cfg.get('http', 'internalapi.http.port')
  219. if internalport:
  220. try:
  221. interface = self.cfg.get('http', 'internalapi.http.bind_address')
  222. except configparser.NoOptionError:
  223. interface = '::1'
  224. self.internalApiHttpServer = InternalApiHttpServer(self)
  225. self.internalApiHttpServer.setup(interface, int(internalport))
  226. if self.pidfile:
  227. with open(self.pidfile, 'w') as pidfile:
  228. pidfile.write(str(os.getpid()) + "\n")
  229. self.reactor.run()
  230. def ip_from_request(self, request):
  231. if (self.cfg.get('http', 'obey_x_forwarded_for') and
  232. request.requestHeaders.hasHeader("X-Forwarded-For")):
  233. return request.requestHeaders.getRawHeaders("X-Forwarded-For")[0]
  234. return request.getClientIP()
  235. class Validators:
  236. pass
  237. class Servlets:
  238. pass
  239. class Keyring:
  240. pass
  241. def parse_config_dict(config_dict):
  242. """Parse the given config from a dictionary, populating missing items and sections
  243. Args:
  244. config_dict (dict): the configuration dictionary to be parsed
  245. """
  246. # Build a config dictionary from the defaults merged with the given dictionary
  247. config = copy.deepcopy(CONFIG_DEFAULTS)
  248. for section, section_dict in config_dict.items():
  249. if section not in config:
  250. config[section] = {}
  251. for option in section_dict.keys():
  252. config[section][option] = config_dict[section][option]
  253. # Build a ConfigParser from the merged dictionary
  254. cfg = configparser.ConfigParser()
  255. for section, section_dict in config.items():
  256. cfg.add_section(section)
  257. for option, value in section_dict.items():
  258. cfg.set(section, option, value)
  259. return cfg
  260. def parse_config_file(config_file):
  261. """Parse the given config from a filepath, populating missing items and
  262. sections
  263. Args:
  264. config_file (str): the file to be parsed
  265. """
  266. # if the config file doesn't exist, prepopulate the config object
  267. # with the defaults, in the right section.
  268. #
  269. # otherwise, we have to put the defaults in the DEFAULT section,
  270. # to ensure that they don't override anyone's settings which are
  271. # in their config file in the default section (which is likely,
  272. # because sydent used to be braindead).
  273. use_defaults = not os.path.exists(config_file)
  274. cfg = configparser.ConfigParser()
  275. for sect, entries in CONFIG_DEFAULTS.items():
  276. cfg.add_section(sect)
  277. for k, v in entries.items():
  278. cfg.set(configparser.DEFAULTSECT if use_defaults else sect, k, v)
  279. cfg.read(config_file)
  280. return cfg
  281. def setup_logging(cfg):
  282. log_format = (
  283. "%(asctime)s - %(name)s - %(lineno)d - %(levelname)s"
  284. " - %(message)s"
  285. )
  286. formatter = logging.Formatter(log_format)
  287. logPath = cfg.get('general', "log.path")
  288. if logPath != '':
  289. handler = logging.handlers.TimedRotatingFileHandler(
  290. logPath, when='midnight', backupCount=365
  291. )
  292. handler.setFormatter(formatter)
  293. def sighup(signum, stack):
  294. logger.info("Closing log file due to SIGHUP")
  295. handler.doRollover()
  296. logger.info("Opened new log file due to SIGHUP")
  297. else:
  298. handler = logging.StreamHandler()
  299. handler.setFormatter(formatter)
  300. rootLogger = logging.getLogger('')
  301. rootLogger.setLevel(cfg.get('general', 'log.level'))
  302. rootLogger.addHandler(handler)
  303. observer = log.PythonLoggingObserver()
  304. observer.start()
  305. def get_config_file_path():
  306. return os.environ.get('SYDENT_CONF', "sydent.conf")
  307. def parse_cfg_bool(value):
  308. return value.lower() == "true"
  309. if __name__ == '__main__':
  310. cfg = parse_config_file(get_config_file_path())
  311. setup_logging(cfg)
  312. syd = Sydent(cfg)
  313. syd.run()