2018-10-26 07:05:22 -04:00
|
|
|
# Copyright 2015, 2016 OpenMarket Ltd
|
|
|
|
# Copyright 2018 New Vector
|
2022-08-25 12:27:46 -04:00
|
|
|
# Copyright 2021-22 The Matrix.org Foundation C.I.C.
|
2018-10-26 07:05:22 -04:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import getpass
|
|
|
|
import hashlib
|
|
|
|
import hmac
|
|
|
|
import logging
|
|
|
|
import sys
|
2022-08-25 10:29:08 -04:00
|
|
|
from typing import Any, Callable, Dict, Optional
|
2018-10-26 07:05:22 -04:00
|
|
|
|
Disallow untyped defs in synapse._scripts (#12422)
Of note:
* No untyped defs in `register_new_matrix_user`
This one might be contraversial. `request_registration` has three
dependency-injection arguments used for testing. I'm removing the
injection of the `requests` module and using `unitest.mock.patch` in the
test cases instead.
Doing `reveal_type(requests)` and `reveal_type(requests.get)` before the
change:
```
synapse/_scripts/register_new_matrix_user.py:45: note: Revealed type is "Any"
synapse/_scripts/register_new_matrix_user.py:46: note: Revealed type is "Any"
```
And after:
```
synapse/_scripts/register_new_matrix_user.py:44: note: Revealed type is "types.ModuleType"
synapse/_scripts/register_new_matrix_user.py:45: note: Revealed type is "def (url: Union[builtins.str, builtins.bytes], params: Union[Union[_typeshed.SupportsItems[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]], Tuple[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]], typing.Iterable[Tuple[Union[builtins.str, builtins.bytes, builtins.int, builtins.float], Union[builtins.str, builtins.bytes, builtins.int, builtins.float, typing.Iterable[Union[builtins.str, builtins.bytes, builtins.int, builtins.float]], None]]], builtins.str, builtins.bytes], None] =, data: Union[Any, None] =, headers: Union[Any, None] =, cookies: Union[Any, None] =, files: Union[Any, None] =, auth: Union[Any, None] =, timeout: Union[Any, None] =, allow_redirects: builtins.bool =, proxies: Union[Any, None] =, hooks: Union[Any, None] =, stream: Union[Any, None] =, verify: Union[Any, None] =, cert: Union[Any, None] =, json: Union[Any, None] =) -> requests.models.Response"
```
* Drive-by comment in `synapse.storage.types`
* No untyped defs in `synapse_port_db`
This was by far the most painful. I'm happy to break this up into
smaller pieces for review if it's not managable as-is.
2022-04-11 07:41:55 -04:00
|
|
|
import requests
|
2018-10-26 07:05:22 -04:00
|
|
|
import yaml
|
|
|
|
|
2022-08-25 12:27:46 -04:00
|
|
|
_CONFLICTING_SHARED_SECRET_OPTS_ERROR = """\
|
|
|
|
Conflicting options 'registration_shared_secret' and 'registration_shared_secret_path'
|
|
|
|
are both defined in config file.
|
|
|
|
"""
|
|
|
|
|
|
|
|
_NO_SHARED_SECRET_OPTS_ERROR = """\
|
|
|
|
No 'registration_shared_secret' or 'registration_shared_secret_path' defined in config.
|
|
|
|
"""
|
|
|
|
|
2022-08-25 10:29:08 -04:00
|
|
|
_DEFAULT_SERVER_URL = "http://localhost:8008"
|
|
|
|
|
2018-10-26 07:05:22 -04:00
|
|
|
|
|
|
|
def request_registration(
|
2021-11-10 12:55:32 -05:00
|
|
|
user: str,
|
|
|
|
password: str,
|
|
|
|
server_location: str,
|
|
|
|
shared_secret: str,
|
|
|
|
admin: bool = False,
|
|
|
|
user_type: Optional[str] = None,
|
|
|
|
_print: Callable[[str], None] = print,
|
|
|
|
exit: Callable[[int], None] = sys.exit,
|
|
|
|
) -> None:
|
2020-11-26 05:57:26 -05:00
|
|
|
url = "%s/_synapse/admin/v1/register" % (server_location.rstrip("/"),)
|
2018-10-26 07:05:22 -04:00
|
|
|
|
|
|
|
# Get the nonce
|
2023-10-23 07:38:51 -04:00
|
|
|
r = requests.get(url)
|
2018-10-26 07:05:22 -04:00
|
|
|
|
2019-01-30 05:53:17 -05:00
|
|
|
if r.status_code != 200:
|
2018-10-26 07:05:22 -04:00
|
|
|
_print("ERROR! Received %d %s" % (r.status_code, r.reason))
|
|
|
|
if 400 <= r.status_code < 500:
|
|
|
|
try:
|
|
|
|
_print(r.json()["error"])
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
return exit(1)
|
|
|
|
|
|
|
|
nonce = r.json()["nonce"]
|
|
|
|
|
|
|
|
mac = hmac.new(key=shared_secret.encode("utf8"), digestmod=hashlib.sha1)
|
|
|
|
|
|
|
|
mac.update(nonce.encode("utf8"))
|
|
|
|
mac.update(b"\x00")
|
|
|
|
mac.update(user.encode("utf8"))
|
|
|
|
mac.update(b"\x00")
|
|
|
|
mac.update(password.encode("utf8"))
|
|
|
|
mac.update(b"\x00")
|
|
|
|
mac.update(b"admin" if admin else b"notadmin")
|
2018-12-14 13:20:59 -05:00
|
|
|
if user_type:
|
|
|
|
mac.update(b"\x00")
|
|
|
|
mac.update(user_type.encode("utf8"))
|
2018-10-26 07:05:22 -04:00
|
|
|
|
2021-11-10 12:55:32 -05:00
|
|
|
hex_mac = mac.hexdigest()
|
2018-10-26 07:05:22 -04:00
|
|
|
|
|
|
|
data = {
|
|
|
|
"nonce": nonce,
|
|
|
|
"username": user,
|
|
|
|
"password": password,
|
2021-11-10 12:55:32 -05:00
|
|
|
"mac": hex_mac,
|
2018-10-26 07:05:22 -04:00
|
|
|
"admin": admin,
|
2018-12-14 13:20:59 -05:00
|
|
|
"user_type": user_type,
|
2018-10-26 07:05:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_print("Sending registration request...")
|
2023-10-23 07:38:51 -04:00
|
|
|
r = requests.post(url, json=data)
|
2018-10-26 07:05:22 -04:00
|
|
|
|
2019-01-30 05:53:17 -05:00
|
|
|
if r.status_code != 200:
|
2018-10-26 07:05:22 -04:00
|
|
|
_print("ERROR! Received %d %s" % (r.status_code, r.reason))
|
|
|
|
if 400 <= r.status_code < 500:
|
|
|
|
try:
|
|
|
|
_print(r.json()["error"])
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
return exit(1)
|
|
|
|
|
|
|
|
_print("Success!")
|
|
|
|
|
|
|
|
|
2021-11-10 12:55:32 -05:00
|
|
|
def register_new_user(
|
|
|
|
user: str,
|
|
|
|
password: str,
|
|
|
|
server_location: str,
|
|
|
|
shared_secret: str,
|
|
|
|
admin: Optional[bool],
|
|
|
|
user_type: Optional[str],
|
|
|
|
) -> None:
|
2018-10-26 07:05:22 -04:00
|
|
|
if not user:
|
|
|
|
try:
|
2021-11-10 12:55:32 -05:00
|
|
|
default_user: Optional[str] = getpass.getuser()
|
2018-10-26 07:05:22 -04:00
|
|
|
except Exception:
|
|
|
|
default_user = None
|
|
|
|
|
|
|
|
if default_user:
|
|
|
|
user = input("New user localpart [%s]: " % (default_user,))
|
|
|
|
if not user:
|
|
|
|
user = default_user
|
|
|
|
else:
|
|
|
|
user = input("New user localpart: ")
|
|
|
|
|
|
|
|
if not user:
|
|
|
|
print("Invalid user name")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if not password:
|
|
|
|
password = getpass.getpass("Password: ")
|
|
|
|
|
|
|
|
if not password:
|
|
|
|
print("Password cannot be blank.")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
confirm_password = getpass.getpass("Confirm password: ")
|
|
|
|
|
|
|
|
if password != confirm_password:
|
|
|
|
print("Passwords do not match")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if admin is None:
|
2021-11-10 12:55:32 -05:00
|
|
|
admin_inp = input("Make admin [no]: ")
|
|
|
|
if admin_inp in ("y", "yes", "true"):
|
2018-10-26 07:05:22 -04:00
|
|
|
admin = True
|
|
|
|
else:
|
|
|
|
admin = False
|
|
|
|
|
2018-12-14 13:20:59 -05:00
|
|
|
request_registration(
|
|
|
|
user, password, server_location, shared_secret, bool(admin), user_type
|
|
|
|
)
|
2018-10-26 07:05:22 -04:00
|
|
|
|
|
|
|
|
2021-11-10 12:55:32 -05:00
|
|
|
def main() -> None:
|
2018-10-26 07:05:22 -04:00
|
|
|
logging.captureWarnings(True)
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(
|
2019-11-12 08:08:12 -05:00
|
|
|
description="Used to register new users with a given homeserver when"
|
|
|
|
" registration has been disabled. The homeserver must be"
|
2018-10-26 07:05:22 -04:00
|
|
|
" configured with the 'registration_shared_secret' option"
|
|
|
|
" set."
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-u",
|
|
|
|
"--user",
|
|
|
|
default=None,
|
|
|
|
help="Local part of the new user. Will prompt if omitted.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-p",
|
|
|
|
"--password",
|
|
|
|
default=None,
|
|
|
|
help="New password for user. Will prompt if omitted.",
|
|
|
|
)
|
2018-12-14 13:20:59 -05:00
|
|
|
parser.add_argument(
|
|
|
|
"-t",
|
|
|
|
"--user_type",
|
|
|
|
default=None,
|
|
|
|
help="User type as specified in synapse.api.constants.UserTypes",
|
|
|
|
)
|
2018-10-26 07:05:22 -04:00
|
|
|
admin_group = parser.add_mutually_exclusive_group()
|
|
|
|
admin_group.add_argument(
|
|
|
|
"-a",
|
|
|
|
"--admin",
|
|
|
|
action="store_true",
|
|
|
|
help=(
|
|
|
|
"Register new user as an admin. "
|
|
|
|
"Will prompt if --no-admin is not set either."
|
|
|
|
),
|
|
|
|
)
|
|
|
|
admin_group.add_argument(
|
|
|
|
"--no-admin",
|
|
|
|
action="store_true",
|
|
|
|
help=(
|
|
|
|
"Register new user as a regular user. "
|
|
|
|
"Will prompt if --admin is not set either."
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
group = parser.add_mutually_exclusive_group(required=True)
|
|
|
|
group.add_argument(
|
|
|
|
"-c",
|
|
|
|
"--config",
|
|
|
|
type=argparse.FileType("r"),
|
|
|
|
help="Path to server config file. Used to read in shared secret.",
|
|
|
|
)
|
|
|
|
|
|
|
|
group.add_argument(
|
|
|
|
"-k", "--shared-secret", help="Shared secret as defined in server config file."
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
"server_url",
|
|
|
|
nargs="?",
|
2022-08-25 10:29:08 -04:00
|
|
|
help="URL to use to talk to the homeserver. By default, tries to find a "
|
|
|
|
"suitable URL from the configuration file. Otherwise, defaults to "
|
|
|
|
f"'{_DEFAULT_SERVER_URL}'.",
|
2018-10-26 07:05:22 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2022-12-07 08:39:27 -05:00
|
|
|
config: Optional[Dict[str, Any]] = None
|
2018-10-26 07:05:22 -04:00
|
|
|
if "config" in args and args.config:
|
|
|
|
config = yaml.safe_load(args.config)
|
2022-08-25 10:29:08 -04:00
|
|
|
|
|
|
|
if args.shared_secret:
|
|
|
|
secret = args.shared_secret
|
|
|
|
else:
|
|
|
|
# argparse should check that we have either config or shared secret
|
2022-12-07 08:39:27 -05:00
|
|
|
assert config is not None
|
2022-08-25 10:29:08 -04:00
|
|
|
|
2022-08-25 12:27:46 -04:00
|
|
|
secret = config.get("registration_shared_secret")
|
|
|
|
secret_file = config.get("registration_shared_secret_path")
|
|
|
|
if secret_file:
|
|
|
|
if secret:
|
|
|
|
print(_CONFLICTING_SHARED_SECRET_OPTS_ERROR, file=sys.stderr)
|
|
|
|
sys.exit(1)
|
|
|
|
secret = _read_file(secret_file, "registration_shared_secret_path").strip()
|
2018-10-26 07:05:22 -04:00
|
|
|
if not secret:
|
2022-08-25 12:27:46 -04:00
|
|
|
print(_NO_SHARED_SECRET_OPTS_ERROR, file=sys.stderr)
|
2018-10-26 07:05:22 -04:00
|
|
|
sys.exit(1)
|
2022-08-25 10:29:08 -04:00
|
|
|
|
|
|
|
if args.server_url:
|
|
|
|
server_url = args.server_url
|
2022-12-07 08:39:27 -05:00
|
|
|
elif config is not None:
|
2022-08-25 10:29:08 -04:00
|
|
|
server_url = _find_client_listener(config)
|
|
|
|
if not server_url:
|
|
|
|
server_url = _DEFAULT_SERVER_URL
|
|
|
|
print(
|
|
|
|
"Unable to find a suitable HTTP listener in the configuration file. "
|
|
|
|
f"Trying {server_url} as a last resort.",
|
|
|
|
file=sys.stderr,
|
|
|
|
)
|
2018-10-26 07:05:22 -04:00
|
|
|
else:
|
2022-08-25 10:29:08 -04:00
|
|
|
server_url = _DEFAULT_SERVER_URL
|
|
|
|
print(
|
|
|
|
f"No server url or configuration file given. Defaulting to {server_url}.",
|
|
|
|
file=sys.stderr,
|
|
|
|
)
|
2018-10-26 07:05:22 -04:00
|
|
|
|
|
|
|
admin = None
|
|
|
|
if args.admin or args.no_admin:
|
|
|
|
admin = args.admin
|
|
|
|
|
2018-12-14 13:20:59 -05:00
|
|
|
register_new_user(
|
2022-08-25 10:29:08 -04:00
|
|
|
args.user, args.password, server_url, secret, admin, args.user_type
|
2018-12-14 13:20:59 -05:00
|
|
|
)
|
2018-10-26 07:05:22 -04:00
|
|
|
|
|
|
|
|
2022-08-25 12:27:46 -04:00
|
|
|
def _read_file(file_path: Any, config_path: str) -> str:
|
|
|
|
"""Check the given file exists, and read it into a string
|
|
|
|
|
|
|
|
If it does not, exit with an error indicating the problem
|
|
|
|
|
|
|
|
Args:
|
|
|
|
file_path: the file to be read
|
|
|
|
config_path: where in the configuration file_path came from, so that a useful
|
|
|
|
error can be emitted if it does not exist.
|
|
|
|
Returns:
|
|
|
|
content of the file.
|
|
|
|
"""
|
|
|
|
if not isinstance(file_path, str):
|
|
|
|
print(f"{config_path} setting is not a string", file=sys.stderr)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(file_path) as file_stream:
|
|
|
|
return file_stream.read()
|
|
|
|
except OSError as e:
|
|
|
|
print(f"Error accessing file {file_path}: {e}", file=sys.stderr)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2022-08-25 10:29:08 -04:00
|
|
|
def _find_client_listener(config: Dict[str, Any]) -> Optional[str]:
|
|
|
|
# try to find a listener in the config. Returns a host:port pair
|
|
|
|
for listener in config.get("listeners", []):
|
|
|
|
if listener.get("type") != "http" or listener.get("tls", False):
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not any(
|
|
|
|
name == "client"
|
|
|
|
for resource in listener.get("resources", [])
|
|
|
|
for name in resource.get("names", [])
|
|
|
|
):
|
|
|
|
continue
|
|
|
|
|
|
|
|
# TODO: consider bind_addresses
|
|
|
|
return f"http://localhost:{listener['port']}"
|
|
|
|
|
|
|
|
# no suitable listeners?
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2018-10-26 07:05:22 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|