2017-05-25 18:41:45 +02:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-09-18 15:15:27 +02:00
|
|
|
"context"
|
2017-05-25 18:41:45 +02:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2021-06-14 15:08:29 +02:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2017-05-25 18:41:45 +02:00
|
|
|
"os"
|
2022-03-15 12:13:33 +01:00
|
|
|
"regexp"
|
2021-06-14 15:08:29 +02:00
|
|
|
"strings"
|
2017-05-25 18:41:45 +02:00
|
|
|
|
2022-03-25 14:38:24 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup"
|
2022-05-12 11:35:35 +02:00
|
|
|
"github.com/matrix-org/dendrite/setup/base"
|
2022-03-25 14:38:24 +01:00
|
|
|
"github.com/matrix-org/dendrite/userapi/api"
|
2022-05-03 17:35:06 +02:00
|
|
|
"github.com/matrix-org/dendrite/userapi/storage"
|
2020-12-03 11:55:17 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-06-14 15:08:29 +02:00
|
|
|
"golang.org/x/term"
|
2017-05-25 18:41:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const usage = `Usage: %s
|
|
|
|
|
2020-12-03 11:55:17 +01:00
|
|
|
Creates a new user account on the homeserver.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2021-06-14 15:08:29 +02:00
|
|
|
# provide password by parameter
|
|
|
|
%s --config dendrite.yaml -username alice -password foobarbaz
|
|
|
|
# use password from file
|
|
|
|
%s --config dendrite.yaml -username alice -passwordfile my.pass
|
|
|
|
# ask user to provide password
|
2022-03-25 14:38:24 +01:00
|
|
|
%s --config dendrite.yaml -username alice
|
2021-06-14 15:08:29 +02:00
|
|
|
# read password from stdin
|
|
|
|
%s --config dendrite.yaml -username alice -passwordstdin < my.pass
|
|
|
|
cat my.pass | %s --config dendrite.yaml -username alice -passwordstdin
|
2022-02-28 13:57:56 +01:00
|
|
|
# reset password for a user, can be used with a combination above to read the password
|
|
|
|
%s --config dendrite.yaml -reset-password -username alice -password foobarbaz
|
2017-05-25 18:41:45 +02:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
var (
|
2022-03-15 12:13:33 +01:00
|
|
|
username = flag.String("username", "", "The username of the account to register (specify the localpart only, e.g. 'alice' for '@alice:domain.com')")
|
2022-03-25 14:38:24 +01:00
|
|
|
password = flag.String("password", "", "The password to associate with the account")
|
2022-03-15 12:13:33 +01:00
|
|
|
pwdFile = flag.String("passwordfile", "", "The file to use for the password (e.g. for automated account creation)")
|
|
|
|
pwdStdin = flag.Bool("passwordstdin", false, "Reads the password from stdin")
|
2022-03-25 14:38:24 +01:00
|
|
|
pwdLess = flag.Bool("passwordless", false, "Create a passwordless account, e.g. if only an accesstoken is required")
|
2022-03-15 12:13:33 +01:00
|
|
|
isAdmin = flag.Bool("admin", false, "Create an admin account")
|
|
|
|
resetPassword = flag.Bool("reset-password", false, "Resets the password for the given username")
|
|
|
|
validUsernameRegex = regexp.MustCompile(`^[0-9a-z_\-=./]+$`)
|
2017-05-25 18:41:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2021-06-14 15:08:29 +02:00
|
|
|
name := os.Args[0]
|
2017-05-25 18:41:45 +02:00
|
|
|
flag.Usage = func() {
|
2022-02-28 13:57:56 +01:00
|
|
|
_, _ = fmt.Fprintf(os.Stderr, usage, name, name, name, name, name, name, name)
|
2017-05-25 18:41:45 +02:00
|
|
|
flag.PrintDefaults()
|
|
|
|
}
|
2020-12-03 11:55:17 +01:00
|
|
|
cfg := setup.ParseFlags(true)
|
2017-05-25 18:41:45 +02:00
|
|
|
|
|
|
|
if *username == "" {
|
|
|
|
flag.Usage()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2022-03-25 14:38:24 +01:00
|
|
|
if *pwdLess && *resetPassword {
|
|
|
|
logrus.Fatalf("Can not reset to an empty password, unable to login afterwards.")
|
|
|
|
}
|
|
|
|
|
2022-03-15 12:13:33 +01:00
|
|
|
if !validUsernameRegex.MatchString(*username) {
|
|
|
|
logrus.Warn("Username can only contain characters a-z, 0-9, or '_-./='")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2022-03-25 14:38:24 +01:00
|
|
|
if len(fmt.Sprintf("@%s:%s", *username, cfg.Global.ServerName)) > 255 {
|
|
|
|
logrus.Fatalf("Username can not be longer than 255 characters: %s", fmt.Sprintf("@%s:%s", *username, cfg.Global.ServerName))
|
|
|
|
}
|
|
|
|
|
|
|
|
var pass string
|
|
|
|
var err error
|
|
|
|
if !*pwdLess {
|
|
|
|
pass, err = getPassword(*password, *pwdFile, *pwdStdin, os.Stdin)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatalln(err)
|
|
|
|
}
|
|
|
|
}
|
2021-06-14 15:08:29 +02:00
|
|
|
|
2022-05-12 11:35:35 +02:00
|
|
|
// avoid warning about open registration
|
|
|
|
cfg.ClientAPI.RegistrationDisabled = true
|
|
|
|
|
|
|
|
b := base.NewBaseDendrite(cfg, "")
|
|
|
|
defer b.Close() // nolint: errcheck
|
|
|
|
|
2022-05-03 17:35:06 +02:00
|
|
|
accountDB, err := storage.NewUserAPIDatabase(
|
2022-05-12 11:35:35 +02:00
|
|
|
b,
|
2022-05-03 17:35:06 +02:00
|
|
|
&cfg.UserAPI.AccountDatabase,
|
|
|
|
cfg.Global.ServerName,
|
|
|
|
cfg.UserAPI.BCryptCost,
|
|
|
|
cfg.UserAPI.OpenIDTokenLifetimeMS,
|
|
|
|
0, // TODO
|
|
|
|
cfg.Global.ServerNotices.LocalPart,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Fatalln("Failed to connect to the database")
|
|
|
|
}
|
2017-05-25 18:41:45 +02:00
|
|
|
|
2022-02-16 18:55:38 +01:00
|
|
|
accType := api.AccountTypeUser
|
|
|
|
if *isAdmin {
|
|
|
|
accType = api.AccountTypeAdmin
|
|
|
|
}
|
2022-03-25 14:38:24 +01:00
|
|
|
|
|
|
|
available, err := accountDB.CheckAccountAvailability(context.Background(), *username)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatalln("Unable check username existence.")
|
|
|
|
}
|
2022-02-28 13:57:56 +01:00
|
|
|
if *resetPassword {
|
2022-03-25 14:38:24 +01:00
|
|
|
if available {
|
|
|
|
logrus.Fatalln("Username could not be found.")
|
|
|
|
}
|
2022-02-28 13:57:56 +01:00
|
|
|
err = accountDB.SetPassword(context.Background(), *username, pass)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatalf("Failed to update password for user %s: %s", *username, err.Error())
|
|
|
|
}
|
|
|
|
if _, err = accountDB.RemoveAllDevices(context.Background(), *username, ""); err != nil {
|
|
|
|
logrus.Fatalf("Failed to remove all devices: %s", err.Error())
|
|
|
|
}
|
|
|
|
logrus.Infof("Updated password for user %s and invalidated all logins\n", *username)
|
|
|
|
return
|
|
|
|
}
|
2022-03-25 14:38:24 +01:00
|
|
|
if !available {
|
|
|
|
logrus.Fatalln("Username is already in use.")
|
|
|
|
}
|
2022-02-28 13:57:56 +01:00
|
|
|
|
2022-02-16 18:55:38 +01:00
|
|
|
_, err = accountDB.CreateAccount(context.Background(), *username, pass, "", accType)
|
2017-05-25 18:41:45 +02:00
|
|
|
if err != nil {
|
2020-12-03 11:55:17 +01:00
|
|
|
logrus.Fatalln("Failed to create the account:", err.Error())
|
2017-05-25 18:41:45 +02:00
|
|
|
}
|
|
|
|
|
2020-12-03 11:55:17 +01:00
|
|
|
logrus.Infoln("Created account", *username)
|
2017-05-25 18:41:45 +02:00
|
|
|
}
|
2021-06-14 15:08:29 +02:00
|
|
|
|
2022-03-25 14:38:24 +01:00
|
|
|
func getPassword(password, pwdFile string, pwdStdin bool, r io.Reader) (string, error) {
|
2021-06-14 15:08:29 +02:00
|
|
|
// read password from file
|
2022-03-25 14:38:24 +01:00
|
|
|
if pwdFile != "" {
|
|
|
|
pw, err := ioutil.ReadFile(pwdFile)
|
2021-06-14 15:08:29 +02:00
|
|
|
if err != nil {
|
2022-03-25 14:38:24 +01:00
|
|
|
return "", fmt.Errorf("Unable to read password from file: %v", err)
|
2021-06-14 15:08:29 +02:00
|
|
|
}
|
2022-03-25 14:38:24 +01:00
|
|
|
return strings.TrimSpace(string(pw)), nil
|
2021-06-14 15:08:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// read password from stdin
|
2022-03-25 14:38:24 +01:00
|
|
|
if pwdStdin {
|
2021-06-14 15:08:29 +02:00
|
|
|
data, err := ioutil.ReadAll(r)
|
|
|
|
if err != nil {
|
2022-03-25 14:38:24 +01:00
|
|
|
return "", fmt.Errorf("Unable to read password from stdin: %v", err)
|
2021-06-14 15:08:29 +02:00
|
|
|
}
|
2022-03-25 14:38:24 +01:00
|
|
|
return strings.TrimSpace(string(data)), nil
|
2021-06-14 15:08:29 +02:00
|
|
|
}
|
|
|
|
|
2022-03-25 14:38:24 +01:00
|
|
|
// If no parameter was set, ask the user to provide the password
|
|
|
|
if password == "" {
|
2021-06-14 15:08:29 +02:00
|
|
|
fmt.Print("Enter Password: ")
|
2021-11-02 17:50:59 +01:00
|
|
|
bytePassword, err := term.ReadPassword(int(os.Stdin.Fd()))
|
2021-06-14 15:08:29 +02:00
|
|
|
if err != nil {
|
2022-03-25 14:38:24 +01:00
|
|
|
return "", fmt.Errorf("Unable to read password: %v", err)
|
2021-06-14 15:08:29 +02:00
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
fmt.Print("Confirm Password: ")
|
2021-11-02 17:50:59 +01:00
|
|
|
bytePassword2, err := term.ReadPassword(int(os.Stdin.Fd()))
|
2021-06-14 15:08:29 +02:00
|
|
|
if err != nil {
|
2022-03-25 14:38:24 +01:00
|
|
|
return "", fmt.Errorf("Unable to read password: %v", err)
|
2021-06-14 15:08:29 +02:00
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
if strings.TrimSpace(string(bytePassword)) != strings.TrimSpace(string(bytePassword2)) {
|
2022-03-25 14:38:24 +01:00
|
|
|
return "", fmt.Errorf("Entered passwords don't match")
|
2021-06-14 15:08:29 +02:00
|
|
|
}
|
2022-03-25 14:38:24 +01:00
|
|
|
return strings.TrimSpace(string(bytePassword)), nil
|
2021-06-14 15:08:29 +02:00
|
|
|
}
|
|
|
|
|
2022-03-25 14:38:24 +01:00
|
|
|
return password, nil
|
2021-06-14 15:08:29 +02:00
|
|
|
}
|