| 1 |
//go:build !noserver |
| 2 |
|
| 3 |
package cmd |
| 4 |
|
| 5 |
import ( |
| 6 |
"crypto/subtle" |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"os" |
| 10 |
"strings" |
| 11 |
"time" |
| 12 |
|
| 13 |
"github.com/urfave/cli/v2" |
| 14 |
"github.com/urfave/cli/v2/altsrc" |
| 15 |
"heckel.io/ntfy/v2/db" |
| 16 |
"heckel.io/ntfy/v2/db/pg" |
| 17 |
"heckel.io/ntfy/v2/mail" |
| 18 |
"heckel.io/ntfy/v2/server" |
| 19 |
"heckel.io/ntfy/v2/user" |
| 20 |
"heckel.io/ntfy/v2/util" |
| 21 |
) |
| 22 |
|
| 23 |
const ( |
| 24 |
tierReset = "-" |
| 25 |
) |
| 26 |
|
| 27 |
func init() { |
| 28 |
commands = append(commands, cmdUser) |
| 29 |
} |
| 30 |
|
| 31 |
var flagsUser = append( |
| 32 |
append([]cli.Flag{}, flagsDefault...), |
| 33 |
&cli.StringFlag{Name: "config", Aliases: []string{"c"}, EnvVars: []string{"NTFY_CONFIG_FILE"}, Value: server.DefaultConfigFile, DefaultText: server.DefaultConfigFile, Usage: "config file"}, |
| 34 |
altsrc.NewStringFlag(&cli.StringFlag{Name: "auth-file", Aliases: []string{"auth_file", "H"}, EnvVars: []string{"NTFY_AUTH_FILE"}, Usage: "auth database file used for access control"}), |
| 35 |
altsrc.NewStringFlag(&cli.StringFlag{Name: "auth-default-access", Aliases: []string{"auth_default_access", "p"}, EnvVars: []string{"NTFY_AUTH_DEFAULT_ACCESS"}, Value: "read-write", Usage: "default permissions if no matching entries in the auth database are found"}), |
| 36 |
altsrc.NewStringFlag(&cli.StringFlag{Name: "database-url", Aliases: []string{"database_url"}, EnvVars: []string{"NTFY_DATABASE_URL"}, Usage: "PostgreSQL connection string for database-backed stores"}), |
| 37 |
altsrc.NewStringFlag(&cli.StringFlag{Name: "base-url", Aliases: []string{"base_url", "B"}, EnvVars: []string{"NTFY_BASE_URL"}, Usage: "externally visible base URL for this host (e.g. https://ntfy.sh)"}), |
| 38 |
altsrc.NewStringFlag(&cli.StringFlag{Name: "smtp-sender-addr", Aliases: []string{"smtp_sender_addr"}, EnvVars: []string{"NTFY_SMTP_SENDER_ADDR"}, Usage: "SMTP server address (host:port) for outgoing emails"}), |
| 39 |
altsrc.NewStringFlag(&cli.StringFlag{Name: "smtp-sender-user", Aliases: []string{"smtp_sender_user"}, EnvVars: []string{"NTFY_SMTP_SENDER_USER"}, Usage: "SMTP user (if e-mail sending is enabled)"}), |
| 40 |
altsrc.NewStringFlag(&cli.StringFlag{Name: "smtp-sender-pass", Aliases: []string{"smtp_sender_pass"}, EnvVars: []string{"NTFY_SMTP_SENDER_PASS"}, Usage: "SMTP password (if e-mail sending is enabled)"}), |
| 41 |
altsrc.NewStringFlag(&cli.StringFlag{Name: "smtp-sender-from", Aliases: []string{"smtp_sender_from"}, EnvVars: []string{"NTFY_SMTP_SENDER_FROM"}, Usage: "SMTP sender address (if e-mail sending is enabled)"}), |
| 42 |
) |
| 43 |
|
| 44 |
var cmdUser = &cli.Command{ |
| 45 |
Name: "user", |
| 46 |
Usage: "Manage/show users", |
| 47 |
UsageText: "ntfy user [list|add|remove|change-pass|reset-pass|change-role] ...", |
| 48 |
Flags: flagsUser, |
| 49 |
Before: initConfigFileInputSourceFunc("config", flagsUser, initLogFunc), |
| 50 |
Category: categoryServer, |
| 51 |
Subcommands: []*cli.Command{ |
| 52 |
{ |
| 53 |
Name: "add", |
| 54 |
Aliases: []string{"a"}, |
| 55 |
Usage: "Adds a new user", |
| 56 |
UsageText: "ntfy user add [--role=admin|user] USERNAME\nNTFY_PASSWORD=... ntfy user add [--role=admin|user] USERNAME\nNTFY_PASSWORD_HASH=... ntfy user add [--role=admin|user] USERNAME", |
| 57 |
Action: execUserAdd, |
| 58 |
Flags: []cli.Flag{ |
| 59 |
&cli.StringFlag{Name: "role", Aliases: []string{"r"}, Value: string(user.RoleUser), Usage: "user role"}, |
| 60 |
&cli.BoolFlag{Name: "ignore-exists", Usage: "if the user already exists, perform no action and exit"}, |
| 61 |
}, |
| 62 |
Description: `Add a new user to the ntfy user database. |
| 63 |
|
| 64 |
A user can be either a regular user, or an admin. A regular user has no read or write access (unless |
| 65 |
granted otherwise by the auth-default-access setting). An admin user has read and write access to all |
| 66 |
topics. |
| 67 |
|
| 68 |
Examples: |
| 69 |
ntfy user add phil # Add regular user phil |
| 70 |
ntfy user add --role=admin phil # Add admin user phil |
| 71 |
NTFY_PASSWORD=... ntfy user add phil # Add user, using env variable to set password (for scripts) |
| 72 |
NTFY_PASSWORD_HASH=... ntfy user add phil # Add user, using env variable to set password hash (for scripts) |
| 73 |
|
| 74 |
You may set the NTFY_PASSWORD environment variable to pass the password, or NTFY_PASSWORD_HASH to pass |
| 75 |
directly the bcrypt hash. This is useful if you are creating users via scripts. |
| 76 |
`, |
| 77 |
}, |
| 78 |
{ |
| 79 |
Name: "remove", |
| 80 |
Aliases: []string{"del", "rm"}, |
| 81 |
Usage: "Removes a user", |
| 82 |
UsageText: "ntfy user remove USERNAME", |
| 83 |
Action: execUserDel, |
| 84 |
Description: `Remove a user from the ntfy user database. |
| 85 |
|
| 86 |
Example: |
| 87 |
ntfy user del phil |
| 88 |
`, |
| 89 |
}, |
| 90 |
{ |
| 91 |
Name: "change-pass", |
| 92 |
Aliases: []string{"chp"}, |
| 93 |
Usage: "Changes a user's password", |
| 94 |
UsageText: "ntfy user change-pass USERNAME\nNTFY_PASSWORD=... ntfy user change-pass USERNAME\nNTFY_PASSWORD_HASH=... ntfy user change-pass USERNAME", |
| 95 |
Action: execUserChangePass, |
| 96 |
Description: `Change the password for the given user. |
| 97 |
|
| 98 |
The new password will be read from STDIN, and it'll be confirmed by typing |
| 99 |
it twice. |
| 100 |
|
| 101 |
Example: |
| 102 |
ntfy user change-pass phil |
| 103 |
NTFY_PASSWORD=.. ntfy user change-pass phil |
| 104 |
NTFY_PASSWORD_HASH=.. ntfy user change-pass phil |
| 105 |
|
| 106 |
You may set the NTFY_PASSWORD environment variable to pass the new password or NTFY_PASSWORD_HASH to pass |
| 107 |
directly the bcrypt hash. This is useful if you are updating users via scripts. |
| 108 |
`, |
| 109 |
}, |
| 110 |
{ |
| 111 |
Name: "reset-pass", |
| 112 |
Aliases: []string{"rp"}, |
| 113 |
Usage: "Generates a password reset link for a user", |
| 114 |
UsageText: "ntfy user reset-pass [--send-email] USERNAME", |
| 115 |
Action: execUserResetPass, |
| 116 |
Flags: []cli.Flag{ |
| 117 |
&cli.BoolFlag{Name: "send-email", Aliases: []string{"e"}, Usage: "also email the reset link to the user's primary email"}, |
| 118 |
}, |
| 119 |
Description: `Generate a password reset link for the given user and print it to stdout. |
| 120 |
|
| 121 |
The user completes the reset by opening the link in a browser and choosing a new password; |
| 122 |
the admin never learns or chooses the new password. The link is single-use and expires after |
| 123 |
one hour. This is an admin override of the self-service reset flow -- unlike self-service, it |
| 124 |
does not require the user to have a verified primary email (the token is bound to the user). |
| 125 |
|
| 126 |
With --send-email, the link is additionally emailed to the user's primary email address (this |
| 127 |
requires SMTP to be configured and the user to have a verified primary email). |
| 128 |
|
| 129 |
Example: |
| 130 |
ntfy user reset-pass phil # Print a reset link for user phil |
| 131 |
ntfy user reset-pass --send-email phil # Print and email the reset link |
| 132 |
`, |
| 133 |
}, |
| 134 |
{ |
| 135 |
Name: "change-role", |
| 136 |
Aliases: []string{"chr"}, |
| 137 |
Usage: "Changes the role of a user", |
| 138 |
UsageText: "ntfy user change-role USERNAME ROLE", |
| 139 |
Action: execUserChangeRole, |
| 140 |
Description: `Change the role for the given user to admin or user. |
| 141 |
|
| 142 |
This command can be used to change the role of a user either from a regular user |
| 143 |
to an admin user, or the other way around: |
| 144 |
|
| 145 |
- admin: an admin has read/write access to all topics |
| 146 |
- user: a regular user only has access to what was explicitly granted via 'ntfy access' |
| 147 |
|
| 148 |
When changing the role of a user to "admin", all access control entries for that |
| 149 |
user are removed, since they are no longer necessary. |
| 150 |
|
| 151 |
Example: |
| 152 |
ntfy user change-role phil admin # Make user phil an admin |
| 153 |
ntfy user change-role phil user # Remove admin role from user phil |
| 154 |
`, |
| 155 |
}, |
| 156 |
{ |
| 157 |
Name: "change-tier", |
| 158 |
Aliases: []string{"cht"}, |
| 159 |
Usage: "Changes the tier of a user", |
| 160 |
UsageText: "ntfy user change-tier USERNAME (TIER|-)", |
| 161 |
Action: execUserChangeTier, |
| 162 |
Description: `Change the tier for the given user. |
| 163 |
|
| 164 |
This command can be used to change the tier of a user. Tiers define usage limits, such |
| 165 |
as messages per day, attachment file sizes, etc. |
| 166 |
|
| 167 |
Example: |
| 168 |
ntfy user change-tier phil pro # Change tier to "pro" for user "phil" |
| 169 |
ntfy user change-tier phil - # Remove tier from user "phil" entirely |
| 170 |
`, |
| 171 |
}, |
| 172 |
{ |
| 173 |
Name: "hash", |
| 174 |
Usage: "Create password hash for a predefined user", |
| 175 |
UsageText: "ntfy user hash", |
| 176 |
Action: execUserHash, |
| 177 |
Description: `Asks for a password and creates a bcrypt password hash. |
| 178 |
|
| 179 |
This command is useful to create a password hash for a user, which can then be used |
| 180 |
for predefined users in the server config file, in auth-users. |
| 181 |
|
| 182 |
Example: |
| 183 |
$ ntfy user hash |
| 184 |
(asks for password and confirmation) |
| 185 |
$2a$10$YLiO8U21sX1uhZamTLJXHuxgVC0Z/GKISibrKCLohPgtG7yIxSk4C |
| 186 |
`, |
| 187 |
}, |
| 188 |
{ |
| 189 |
Name: "list", |
| 190 |
Aliases: []string{"l"}, |
| 191 |
Usage: "Shows a list of users", |
| 192 |
Action: execUserList, |
| 193 |
Description: `Shows a list of all configured users, including the everyone ('*') user. |
| 194 |
|
| 195 |
This command is an alias to calling 'ntfy access' (display access control list). |
| 196 |
|
| 197 |
This is a server-only command. It directly reads from user.db as defined in the server config |
| 198 |
file server.yml. The command only works if 'auth-file' is properly defined. |
| 199 |
`, |
| 200 |
}, |
| 201 |
}, |
| 202 |
Description: `Manage users of the ntfy server. |
| 203 |
|
| 204 |
The command allows you to add/remove/change users in the ntfy user database, as well as change |
| 205 |
passwords or roles. |
| 206 |
|
| 207 |
This is a server-only command. It directly manages the user.db as defined in the server config |
| 208 |
file server.yml. The command only works if 'auth-file' is properly defined. Please also refer |
| 209 |
to the related command 'ntfy access'. |
| 210 |
|
| 211 |
Examples: |
| 212 |
ntfy user list # Shows list of users (alias: 'ntfy access') |
| 213 |
ntfy user add phil # Add regular user phil |
| 214 |
NTFY_PASSWORD=... ntfy user add phil # As above, using env variable to set password (for scripts) |
| 215 |
ntfy user add --role=admin phil # Add admin user phil |
| 216 |
ntfy user del phil # Delete user phil |
| 217 |
ntfy user change-pass phil # Change password for user phil |
| 218 |
NTFY_PASSWORD=.. ntfy user change-pass phil # As above, using env variable to set password (for scripts) |
| 219 |
ntfy user change-role phil admin # Make user phil an admin |
| 220 |
|
| 221 |
For the 'ntfy user add' and 'ntfy user change-pass' commands, you may set the NTFY_PASSWORD environment |
| 222 |
variable to pass the new password. This is useful if you are creating/updating users via scripts. |
| 223 |
`, |
| 224 |
} |
| 225 |
|
| 226 |
func execUserAdd(c *cli.Context) error { |
| 227 |
username := c.Args().Get(0) |
| 228 |
role := user.Role(c.String("role")) |
| 229 |
password, hashed := os.LookupEnv("NTFY_PASSWORD_HASH") |
| 230 |
|
| 231 |
if !hashed { |
| 232 |
password = os.Getenv("NTFY_PASSWORD") |
| 233 |
} |
| 234 |
|
| 235 |
if username == "" { |
| 236 |
return errors.New("username expected, type 'ntfy user add --help' for help") |
| 237 |
} else if username == userEveryone || username == user.Everyone { |
| 238 |
return errors.New("username not allowed") |
| 239 |
} else if !user.AllowedRole(role) { |
| 240 |
return errors.New("role must be either 'user' or 'admin'") |
| 241 |
} |
| 242 |
manager, err := createUserManager(c) |
| 243 |
if err != nil { |
| 244 |
return err |
| 245 |
} |
| 246 |
if user, _ := manager.User(username); user != nil { |
| 247 |
if c.Bool("ignore-exists") { |
| 248 |
fmt.Fprintf(c.App.Writer, "user %s already exists (exited successfully)\n", username) |
| 249 |
return nil |
| 250 |
} |
| 251 |
return fmt.Errorf("user %s already exists", username) |
| 252 |
} |
| 253 |
if password == "" { |
| 254 |
p, err := readPasswordAndConfirm(c) |
| 255 |
if err != nil { |
| 256 |
return err |
| 257 |
} |
| 258 |
password = p |
| 259 |
} |
| 260 |
if err := manager.AddUser(username, password, role, hashed); err != nil { |
| 261 |
return err |
| 262 |
} |
| 263 |
fmt.Fprintf(c.App.Writer, "user %s added with role %s\n", username, role) |
| 264 |
return nil |
| 265 |
} |
| 266 |
|
| 267 |
func execUserDel(c *cli.Context) error { |
| 268 |
username := c.Args().Get(0) |
| 269 |
if username == "" { |
| 270 |
return errors.New("username expected, type 'ntfy user del --help' for help") |
| 271 |
} else if username == userEveryone || username == user.Everyone { |
| 272 |
return errors.New("username not allowed") |
| 273 |
} |
| 274 |
manager, err := createUserManager(c) |
| 275 |
if err != nil { |
| 276 |
return err |
| 277 |
} |
| 278 |
if _, err := manager.User(username); errors.Is(err, user.ErrUserNotFound) { |
| 279 |
return fmt.Errorf("user %s does not exist", username) |
| 280 |
} |
| 281 |
if err := manager.RemoveUser(username); err != nil { |
| 282 |
return err |
| 283 |
} |
| 284 |
fmt.Fprintf(c.App.Writer, "user %s removed\n", username) |
| 285 |
return nil |
| 286 |
} |
| 287 |
|
| 288 |
func execUserChangePass(c *cli.Context) error { |
| 289 |
username := c.Args().Get(0) |
| 290 |
password, hashed := os.LookupEnv("NTFY_PASSWORD_HASH") |
| 291 |
if !hashed { |
| 292 |
password = os.Getenv("NTFY_PASSWORD") |
| 293 |
} |
| 294 |
if username == "" { |
| 295 |
return errors.New("username expected, type 'ntfy user change-pass --help' for help") |
| 296 |
} else if username == userEveryone || username == user.Everyone { |
| 297 |
return errors.New("username not allowed") |
| 298 |
} |
| 299 |
manager, err := createUserManager(c) |
| 300 |
if err != nil { |
| 301 |
return err |
| 302 |
} |
| 303 |
if _, err := manager.User(username); errors.Is(err, user.ErrUserNotFound) { |
| 304 |
return fmt.Errorf("user %s does not exist", username) |
| 305 |
} |
| 306 |
if password == "" { |
| 307 |
password, err = readPasswordAndConfirm(c) |
| 308 |
if err != nil { |
| 309 |
return err |
| 310 |
} |
| 311 |
} |
| 312 |
if err := manager.ChangePassword(username, password, hashed); err != nil { |
| 313 |
return err |
| 314 |
} |
| 315 |
fmt.Fprintf(c.App.Writer, "changed password for user %s\n", username) |
| 316 |
return nil |
| 317 |
} |
| 318 |
|
| 319 |
func execUserResetPass(c *cli.Context) error { |
| 320 |
username := c.Args().Get(0) |
| 321 |
sendEmail := c.Bool("send-email") |
| 322 |
baseURL := strings.TrimSuffix(c.String("base-url"), "/") |
| 323 |
if username == "" { |
| 324 |
return errors.New("username expected, type 'ntfy user reset-pass --help' for help") |
| 325 |
} else if username == userEveryone || username == user.Everyone { |
| 326 |
return errors.New("username not allowed") |
| 327 |
} else if baseURL == "" { |
| 328 |
return errors.New("base-url must be configured to generate a reset link") |
| 329 |
} |
| 330 |
manager, err := createUserManager(c) |
| 331 |
if err != nil { |
| 332 |
return err |
| 333 |
} |
| 334 |
u, err := manager.User(username) |
| 335 |
if errors.Is(err, user.ErrUserNotFound) { |
| 336 |
return fmt.Errorf("user %s does not exist", username) |
| 337 |
} else if err != nil { |
| 338 |
return err |
| 339 |
} else if u.Provisioned { |
| 340 |
return fmt.Errorf("user %s is provisioned in the config file; its password cannot be reset", username) |
| 341 |
} |
| 342 |
// Resolve the primary email up front if we need to send -- fail before creating a token |
| 343 |
var primaryEmail string |
| 344 |
if sendEmail { |
| 345 |
primaryEmail, err = manager.PrimaryEmail(u.ID) |
| 346 |
if err != nil { |
| 347 |
return err |
| 348 |
} else if primaryEmail == "" { |
| 349 |
return fmt.Errorf("user %s has no primary email; cannot send reset link (omit --send-email to just print it)", username) |
| 350 |
} |
| 351 |
} |
| 352 |
// The reset token is bound to the user, not an email -- so this works even with no SMTP |
| 353 |
token, err := manager.AddMagicLink(user.MagicLinkKindPasswordReset, u.ID, "", time.Hour) |
| 354 |
if err != nil { |
| 355 |
return err |
| 356 |
} |
| 357 |
link := baseURL + "/account/password/reset/" + token |
| 358 |
fmt.Fprintln(c.App.Writer, link) |
| 359 |
if sendEmail { |
| 360 |
sender := mail.NewSender(&mail.Config{ |
| 361 |
SMTPAddr: c.String("smtp-sender-addr"), |
| 362 |
SMTPUser: c.String("smtp-sender-user"), |
| 363 |
SMTPPass: c.String("smtp-sender-pass"), |
| 364 |
From: c.String("smtp-sender-from"), |
| 365 |
}) |
| 366 |
if err := sender.SendPasswordReset(primaryEmail, link); err != nil { |
| 367 |
return fmt.Errorf("failed to send reset email to %s: %w", primaryEmail, err) |
| 368 |
} |
| 369 |
fmt.Fprintf(c.App.ErrWriter, "reset link emailed to %s\n", primaryEmail) |
| 370 |
} |
| 371 |
return nil |
| 372 |
} |
| 373 |
|
| 374 |
func execUserChangeRole(c *cli.Context) error { |
| 375 |
username := c.Args().Get(0) |
| 376 |
role := user.Role(c.Args().Get(1)) |
| 377 |
if username == "" || !user.AllowedRole(role) { |
| 378 |
return errors.New("username and new role expected, type 'ntfy user change-role --help' for help") |
| 379 |
} else if username == userEveryone || username == user.Everyone { |
| 380 |
return errors.New("username not allowed") |
| 381 |
} |
| 382 |
manager, err := createUserManager(c) |
| 383 |
if err != nil { |
| 384 |
return err |
| 385 |
} |
| 386 |
if _, err := manager.User(username); errors.Is(err, user.ErrUserNotFound) { |
| 387 |
return fmt.Errorf("user %s does not exist", username) |
| 388 |
} |
| 389 |
if err := manager.ChangeRole(username, role); err != nil { |
| 390 |
return err |
| 391 |
} |
| 392 |
fmt.Fprintf(c.App.Writer, "changed role for user %s to %s\n", username, role) |
| 393 |
return nil |
| 394 |
} |
| 395 |
|
| 396 |
func execUserHash(c *cli.Context) error { |
| 397 |
password, err := readPasswordAndConfirm(c) |
| 398 |
if err != nil { |
| 399 |
return err |
| 400 |
} |
| 401 |
hash, err := user.HashPassword(password, user.DefaultUserPasswordBcryptCost) |
| 402 |
if err != nil { |
| 403 |
return fmt.Errorf("failed to hash password: %w", err) |
| 404 |
} |
| 405 |
fmt.Fprintln(c.App.Writer, hash) |
| 406 |
return nil |
| 407 |
} |
| 408 |
|
| 409 |
func execUserChangeTier(c *cli.Context) error { |
| 410 |
username := c.Args().Get(0) |
| 411 |
tier := c.Args().Get(1) |
| 412 |
if username == "" { |
| 413 |
return errors.New("username and new tier expected, type 'ntfy user change-tier --help' for help") |
| 414 |
} else if !user.AllowedTier(tier) && tier != tierReset { |
| 415 |
return errors.New("invalid tier, must be tier code, or - to reset") |
| 416 |
} else if username == userEveryone || username == user.Everyone { |
| 417 |
return errors.New("username not allowed") |
| 418 |
} |
| 419 |
manager, err := createUserManager(c) |
| 420 |
if err != nil { |
| 421 |
return err |
| 422 |
} |
| 423 |
if _, err := manager.User(username); errors.Is(err, user.ErrUserNotFound) { |
| 424 |
return fmt.Errorf("user %s does not exist", username) |
| 425 |
} |
| 426 |
if tier == tierReset { |
| 427 |
if err := manager.ResetTier(username); err != nil { |
| 428 |
return err |
| 429 |
} |
| 430 |
fmt.Fprintf(c.App.Writer, "removed tier from user %s\n", username) |
| 431 |
} else { |
| 432 |
if err := manager.ChangeTier(username, tier); err != nil { |
| 433 |
return err |
| 434 |
} |
| 435 |
fmt.Fprintf(c.App.Writer, "changed tier for user %s to %s\n", username, tier) |
| 436 |
} |
| 437 |
return nil |
| 438 |
} |
| 439 |
|
| 440 |
func execUserList(c *cli.Context) error { |
| 441 |
manager, err := createUserManager(c) |
| 442 |
if err != nil { |
| 443 |
return err |
| 444 |
} |
| 445 |
users, err := manager.Users() |
| 446 |
if err != nil { |
| 447 |
return err |
| 448 |
} |
| 449 |
return showUsers(c, manager, users) |
| 450 |
} |
| 451 |
|
| 452 |
func createUserManager(c *cli.Context) (*user.Manager, error) { |
| 453 |
authFile := c.String("auth-file") |
| 454 |
authStartupQueries := c.String("auth-startup-queries") |
| 455 |
authDefaultAccess := c.String("auth-default-access") |
| 456 |
databaseURL := c.String("database-url") |
| 457 |
authDefault, err := user.ParsePermission(authDefaultAccess) |
| 458 |
if err != nil { |
| 459 |
return nil, errors.New("if set, auth-default-access must start set to 'read-write', 'read-only', 'write-only' or 'deny-all'") |
| 460 |
} |
| 461 |
authConfig := &user.Config{ |
| 462 |
DefaultAccess: authDefault, |
| 463 |
ProvisionEnabled: false, // Hack: Do not re-provision users on manager initialization |
| 464 |
BcryptCost: user.DefaultUserPasswordBcryptCost, |
| 465 |
QueueWriterInterval: user.DefaultUserStatsQueueWriterInterval, |
| 466 |
AccessCacheEnabled: false, // Do not cache for CLI commands |
| 467 |
} |
| 468 |
if databaseURL != "" { |
| 469 |
host, dbErr := pg.Open(databaseURL) |
| 470 |
if dbErr != nil { |
| 471 |
return nil, dbErr |
| 472 |
} |
| 473 |
return user.NewPostgresManager(db.New(host, nil), authConfig) |
| 474 |
} else if authFile != "" { |
| 475 |
if !util.FileExists(authFile) { |
| 476 |
return nil, errors.New("auth-file does not exist; please start the server at least once to create it") |
| 477 |
} |
| 478 |
return user.NewSQLiteManager(authFile, authStartupQueries, authConfig) |
| 479 |
} |
| 480 |
return nil, errors.New("option database-url or auth-file not set; auth is unconfigured for this server") |
| 481 |
} |
| 482 |
|
| 483 |
func readPasswordAndConfirm(c *cli.Context) (string, error) { |
| 484 |
fmt.Fprint(c.App.ErrWriter, "password: ") |
| 485 |
password, err := util.ReadPassword(c.App.Reader) |
| 486 |
if err != nil { |
| 487 |
return "", err |
| 488 |
} else if len(password) == 0 { |
| 489 |
return "", errors.New("password cannot be empty") |
| 490 |
} |
| 491 |
fmt.Fprintf(c.App.ErrWriter, "\r%s\rconfirm: ", strings.Repeat(" ", 25)) |
| 492 |
confirm, err := util.ReadPassword(c.App.Reader) |
| 493 |
if err != nil { |
| 494 |
return "", err |
| 495 |
} |
| 496 |
fmt.Fprintf(c.App.ErrWriter, "\r%s\r", strings.Repeat(" ", 25)) |
| 497 |
if subtle.ConstantTimeCompare(confirm, password) != 1 { |
| 498 |
return "", errors.New("passwords do not match: try it again, but this time type slooowwwlly") |
| 499 |
} |
| 500 |
return string(password), nil |
| 501 |
} |