sydent.py 15 KB

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