register_new_matrix_user.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 2016 OpenMarket Ltd
  3. # Copyright 2018 New Vector
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from __future__ import print_function
  17. import argparse
  18. import getpass
  19. import hashlib
  20. import hmac
  21. import logging
  22. import sys
  23. from six.moves import input
  24. import requests as _requests
  25. import yaml
  26. def request_registration(
  27. user,
  28. password,
  29. server_location,
  30. shared_secret,
  31. admin=False,
  32. user_type=None,
  33. requests=_requests,
  34. _print=print,
  35. exit=sys.exit,
  36. ):
  37. url = "%s/_matrix/client/r0/admin/register" % (server_location,)
  38. # Get the nonce
  39. r = requests.get(url, verify=False)
  40. if r.status_code is not 200:
  41. _print("ERROR! Received %d %s" % (r.status_code, r.reason))
  42. if 400 <= r.status_code < 500:
  43. try:
  44. _print(r.json()["error"])
  45. except Exception:
  46. pass
  47. return exit(1)
  48. nonce = r.json()["nonce"]
  49. mac = hmac.new(key=shared_secret.encode('utf8'), digestmod=hashlib.sha1)
  50. mac.update(nonce.encode('utf8'))
  51. mac.update(b"\x00")
  52. mac.update(user.encode('utf8'))
  53. mac.update(b"\x00")
  54. mac.update(password.encode('utf8'))
  55. mac.update(b"\x00")
  56. mac.update(b"admin" if admin else b"notadmin")
  57. if user_type:
  58. mac.update(b"\x00")
  59. mac.update(user_type.encode('utf8'))
  60. mac = mac.hexdigest()
  61. data = {
  62. "nonce": nonce,
  63. "username": user,
  64. "password": password,
  65. "mac": mac,
  66. "admin": admin,
  67. "user_type": user_type,
  68. }
  69. _print("Sending registration request...")
  70. r = requests.post(url, json=data, verify=False)
  71. if r.status_code is not 200:
  72. _print("ERROR! Received %d %s" % (r.status_code, r.reason))
  73. if 400 <= r.status_code < 500:
  74. try:
  75. _print(r.json()["error"])
  76. except Exception:
  77. pass
  78. return exit(1)
  79. _print("Success!")
  80. def register_new_user(user, password, server_location, shared_secret, admin, user_type):
  81. if not user:
  82. try:
  83. default_user = getpass.getuser()
  84. except Exception:
  85. default_user = None
  86. if default_user:
  87. user = input("New user localpart [%s]: " % (default_user,))
  88. if not user:
  89. user = default_user
  90. else:
  91. user = input("New user localpart: ")
  92. if not user:
  93. print("Invalid user name")
  94. sys.exit(1)
  95. if not password:
  96. password = getpass.getpass("Password: ")
  97. if not password:
  98. print("Password cannot be blank.")
  99. sys.exit(1)
  100. confirm_password = getpass.getpass("Confirm password: ")
  101. if password != confirm_password:
  102. print("Passwords do not match")
  103. sys.exit(1)
  104. if admin is None:
  105. admin = input("Make admin [no]: ")
  106. if admin in ("y", "yes", "true"):
  107. admin = True
  108. else:
  109. admin = False
  110. request_registration(user, password, server_location, shared_secret,
  111. bool(admin), user_type)
  112. def main():
  113. logging.captureWarnings(True)
  114. parser = argparse.ArgumentParser(
  115. description="Used to register new users with a given home server when"
  116. " registration has been disabled. The home server must be"
  117. " configured with the 'registration_shared_secret' option"
  118. " set."
  119. )
  120. parser.add_argument(
  121. "-u",
  122. "--user",
  123. default=None,
  124. help="Local part of the new user. Will prompt if omitted.",
  125. )
  126. parser.add_argument(
  127. "-p",
  128. "--password",
  129. default=None,
  130. help="New password for user. Will prompt if omitted.",
  131. )
  132. parser.add_argument(
  133. "-t",
  134. "--user_type",
  135. default=None,
  136. help="User type as specified in synapse.api.constants.UserTypes",
  137. )
  138. admin_group = parser.add_mutually_exclusive_group()
  139. admin_group.add_argument(
  140. "-a",
  141. "--admin",
  142. action="store_true",
  143. help=(
  144. "Register new user as an admin. "
  145. "Will prompt if --no-admin is not set either."
  146. ),
  147. )
  148. admin_group.add_argument(
  149. "--no-admin",
  150. action="store_true",
  151. help=(
  152. "Register new user as a regular user. "
  153. "Will prompt if --admin is not set either."
  154. ),
  155. )
  156. group = parser.add_mutually_exclusive_group(required=True)
  157. group.add_argument(
  158. "-c",
  159. "--config",
  160. type=argparse.FileType('r'),
  161. help="Path to server config file. Used to read in shared secret.",
  162. )
  163. group.add_argument(
  164. "-k", "--shared-secret", help="Shared secret as defined in server config file."
  165. )
  166. parser.add_argument(
  167. "server_url",
  168. default="https://localhost:8448",
  169. nargs='?',
  170. help="URL to use to talk to the home server. Defaults to "
  171. " 'https://localhost:8448'.",
  172. )
  173. args = parser.parse_args()
  174. if "config" in args and args.config:
  175. config = yaml.safe_load(args.config)
  176. secret = config.get("registration_shared_secret", None)
  177. if not secret:
  178. print("No 'registration_shared_secret' defined in config.")
  179. sys.exit(1)
  180. else:
  181. secret = args.shared_secret
  182. admin = None
  183. if args.admin or args.no_admin:
  184. admin = args.admin
  185. register_new_user(args.user, args.password, args.server_url, secret,
  186. admin, args.user_type)
  187. if __name__ == "__main__":
  188. main()