2017-05-25 11:41:45 -05: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 08:15:27 -05:00
|
|
|
"context"
|
2017-05-25 11:41:45 -05:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2021-06-14 08:08:29 -05:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2017-05-25 11:41:45 -05:00
|
|
|
"os"
|
2021-06-14 08:08:29 -05:00
|
|
|
"strings"
|
2017-05-25 11:41:45 -05:00
|
|
|
|
2020-12-03 04:55:17 -06:00
|
|
|
"github.com/matrix-org/dendrite/setup"
|
2020-12-02 11:41:00 -06:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2020-06-17 06:05:56 -05:00
|
|
|
"github.com/matrix-org/dendrite/userapi/storage/accounts"
|
2020-12-03 04:55:17 -06:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-03-08 07:19:02 -06:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2021-06-14 08:08:29 -05:00
|
|
|
"golang.org/x/term"
|
2017-05-25 11:41:45 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const usage = `Usage: %s
|
|
|
|
|
2020-12-03 04:55:17 -06:00
|
|
|
Creates a new user account on the homeserver.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2021-06-14 08:08:29 -05: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
|
|
|
|
%s --config dendrite.yaml -username alice -ask-pass
|
|
|
|
# read password from stdin
|
|
|
|
%s --config dendrite.yaml -username alice -passwordstdin < my.pass
|
|
|
|
cat my.pass | %s --config dendrite.yaml -username alice -passwordstdin
|
2017-05-25 11:41:45 -05:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
var (
|
2020-12-03 04:55:17 -06:00
|
|
|
username = flag.String("username", "", "The username of the account to register (specify the localpart only, e.g. 'alice' for '@alice:domain.com')")
|
|
|
|
password = flag.String("password", "", "The password to associate with the account (optional, account will be password-less if not specified)")
|
2021-06-14 08:08:29 -05: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")
|
|
|
|
askPass = flag.Bool("ask-pass", false, "Ask for the password to use")
|
2017-05-25 11:41:45 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2021-06-14 08:08:29 -05:00
|
|
|
name := os.Args[0]
|
2017-05-25 11:41:45 -05:00
|
|
|
flag.Usage = func() {
|
2021-06-14 08:08:29 -05:00
|
|
|
_, _ = fmt.Fprintf(os.Stderr, usage, name, name, name, name, name, name)
|
2017-05-25 11:41:45 -05:00
|
|
|
flag.PrintDefaults()
|
|
|
|
}
|
2020-12-03 04:55:17 -06:00
|
|
|
cfg := setup.ParseFlags(true)
|
2017-05-25 11:41:45 -05:00
|
|
|
|
|
|
|
if *username == "" {
|
|
|
|
flag.Usage()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2021-06-14 08:08:29 -05:00
|
|
|
pass := getPassword(password, pwdFile, pwdStdin, askPass, os.Stdin)
|
|
|
|
|
2020-08-10 08:18:04 -05:00
|
|
|
accountDB, err := accounts.NewDatabase(&config.DatabaseOptions{
|
2020-12-03 04:55:17 -06:00
|
|
|
ConnectionString: cfg.UserAPI.AccountDatabase.ConnectionString,
|
2021-04-07 07:26:20 -05:00
|
|
|
}, cfg.Global.ServerName, bcrypt.DefaultCost, cfg.UserAPI.OpenIDTokenLifetimeMS)
|
2017-05-25 11:41:45 -05:00
|
|
|
if err != nil {
|
2020-12-03 04:55:17 -06:00
|
|
|
logrus.Fatalln("Failed to connect to the database:", err.Error())
|
2017-05-25 11:41:45 -05:00
|
|
|
}
|
|
|
|
|
2022-02-15 07:15:18 -06:00
|
|
|
policyVersion := ""
|
|
|
|
if cfg.Global.UserConsentOptions.Enabled() {
|
|
|
|
policyVersion = cfg.Global.UserConsentOptions.Version
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = accountDB.CreateAccount(context.Background(), *username, pass, "", policyVersion)
|
2017-05-25 11:41:45 -05:00
|
|
|
if err != nil {
|
2020-12-03 04:55:17 -06:00
|
|
|
logrus.Fatalln("Failed to create the account:", err.Error())
|
2017-05-25 11:41:45 -05:00
|
|
|
}
|
|
|
|
|
2020-12-03 04:55:17 -06:00
|
|
|
logrus.Infoln("Created account", *username)
|
2017-05-25 11:41:45 -05:00
|
|
|
}
|
2021-06-14 08:08:29 -05:00
|
|
|
|
|
|
|
func getPassword(password, pwdFile *string, pwdStdin, askPass *bool, r io.Reader) string {
|
|
|
|
// no password option set, use empty password
|
|
|
|
if password == nil && pwdFile == nil && pwdStdin == nil && askPass == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
// password defined as parameter
|
|
|
|
if password != nil && *password != "" {
|
|
|
|
return *password
|
|
|
|
}
|
|
|
|
|
|
|
|
// read password from file
|
|
|
|
if pwdFile != nil && *pwdFile != "" {
|
|
|
|
pw, err := ioutil.ReadFile(*pwdFile)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatalln("Unable to read password from file:", err)
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(string(pw))
|
|
|
|
}
|
|
|
|
|
|
|
|
// read password from stdin
|
|
|
|
if pwdStdin != nil && *pwdStdin {
|
|
|
|
data, err := ioutil.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatalln("Unable to read password from stdin:", err)
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(string(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ask the user to provide the password
|
|
|
|
if *askPass {
|
|
|
|
fmt.Print("Enter Password: ")
|
2021-11-02 11:50:59 -05:00
|
|
|
bytePassword, err := term.ReadPassword(int(os.Stdin.Fd()))
|
2021-06-14 08:08:29 -05:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatalln("Unable to read password:", err)
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
fmt.Print("Confirm Password: ")
|
2021-11-02 11:50:59 -05:00
|
|
|
bytePassword2, err := term.ReadPassword(int(os.Stdin.Fd()))
|
2021-06-14 08:08:29 -05:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatalln("Unable to read password:", err)
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
if strings.TrimSpace(string(bytePassword)) != strings.TrimSpace(string(bytePassword2)) {
|
|
|
|
logrus.Fatalln("Entered passwords don't match")
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(string(bytePassword))
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|