register_new_matrix_user.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. requests=_requests,
  33. _print=print,
  34. exit=sys.exit,
  35. ):
  36. url = "%s/_matrix/client/r0/admin/register" % (server_location,)
  37. # Get the nonce
  38. r = requests.get(url, verify=False)
  39. if r.status_code is not 200:
  40. _print("ERROR! Received %d %s" % (r.status_code, r.reason))
  41. if 400 <= r.status_code < 500:
  42. try:
  43. _print(r.json()["error"])
  44. except Exception:
  45. pass
  46. return exit(1)
  47. nonce = r.json()["nonce"]
  48. mac = hmac.new(key=shared_secret.encode('utf8'), digestmod=hashlib.sha1)
  49. mac.update(nonce.encode('utf8'))
  50. mac.update(b"\x00")
  51. mac.update(user.encode('utf8'))
  52. mac.update(b"\x00")
  53. mac.update(password.encode('utf8'))
  54. mac.update(b"\x00")
  55. mac.update(b"admin" if admin else b"notadmin")
  56. mac = mac.hexdigest()
  57. data = {
  58. "nonce": nonce,
  59. "username": user,
  60. "password": password,
  61. "mac": mac,
  62. "admin": admin,
  63. }
  64. _print("Sending registration request...")
  65. r = requests.post(url, json=data, verify=False)
  66. if r.status_code is not 200:
  67. _print("ERROR! Received %d %s" % (r.status_code, r.reason))
  68. if 400 <= r.status_code < 500:
  69. try:
  70. _print(r.json()["error"])
  71. except Exception:
  72. pass
  73. return exit(1)
  74. _print("Success!")
  75. def register_new_user(user, password, server_location, shared_secret, admin):
  76. if not user:
  77. try:
  78. default_user = getpass.getuser()
  79. except Exception:
  80. default_user = None
  81. if default_user:
  82. user = input("New user localpart [%s]: " % (default_user,))
  83. if not user:
  84. user = default_user
  85. else:
  86. user = input("New user localpart: ")
  87. if not user:
  88. print("Invalid user name")
  89. sys.exit(1)
  90. if not password:
  91. password = getpass.getpass("Password: ")
  92. if not password:
  93. print("Password cannot be blank.")
  94. sys.exit(1)
  95. confirm_password = getpass.getpass("Confirm password: ")
  96. if password != confirm_password:
  97. print("Passwords do not match")
  98. sys.exit(1)
  99. if admin is None:
  100. admin = input("Make admin [no]: ")
  101. if admin in ("y", "yes", "true"):
  102. admin = True
  103. else:
  104. admin = False
  105. request_registration(user, password, server_location, shared_secret, bool(admin))
  106. def main():
  107. logging.captureWarnings(True)
  108. parser = argparse.ArgumentParser(
  109. description="Used to register new users with a given home server when"
  110. " registration has been disabled. The home server must be"
  111. " configured with the 'registration_shared_secret' option"
  112. " set."
  113. )
  114. parser.add_argument(
  115. "-u",
  116. "--user",
  117. default=None,
  118. help="Local part of the new user. Will prompt if omitted.",
  119. )
  120. parser.add_argument(
  121. "-p",
  122. "--password",
  123. default=None,
  124. help="New password for user. Will prompt if omitted.",
  125. )
  126. admin_group = parser.add_mutually_exclusive_group()
  127. admin_group.add_argument(
  128. "-a",
  129. "--admin",
  130. action="store_true",
  131. help=(
  132. "Register new user as an admin. "
  133. "Will prompt if --no-admin is not set either."
  134. ),
  135. )
  136. admin_group.add_argument(
  137. "--no-admin",
  138. action="store_true",
  139. help=(
  140. "Register new user as a regular user. "
  141. "Will prompt if --admin is not set either."
  142. ),
  143. )
  144. group = parser.add_mutually_exclusive_group(required=True)
  145. group.add_argument(
  146. "-c",
  147. "--config",
  148. type=argparse.FileType('r'),
  149. help="Path to server config file. Used to read in shared secret.",
  150. )
  151. group.add_argument(
  152. "-k", "--shared-secret", help="Shared secret as defined in server config file."
  153. )
  154. parser.add_argument(
  155. "server_url",
  156. default="https://localhost:8448",
  157. nargs='?',
  158. help="URL to use to talk to the home server. Defaults to "
  159. " 'https://localhost:8448'.",
  160. )
  161. args = parser.parse_args()
  162. if "config" in args and args.config:
  163. config = yaml.safe_load(args.config)
  164. secret = config.get("registration_shared_secret", None)
  165. if not secret:
  166. print("No 'registration_shared_secret' defined in config.")
  167. sys.exit(1)
  168. else:
  169. secret = args.shared_secret
  170. admin = None
  171. if args.admin or args.no_admin:
  172. admin = args.admin
  173. register_new_user(args.user, args.password, args.server_url, secret, admin)
  174. if __name__ == "__main__":
  175. main()