2022-05-21 15:46:49 +02:00
|
|
|
//go:build !noserver
|
|
|
|
|
2022-01-24 06:54:28 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/urfave/cli/v2"
|
2022-12-25 17:41:38 +01:00
|
|
|
"heckel.io/ntfy/user"
|
2022-01-24 06:54:28 +01:00
|
|
|
"heckel.io/ntfy/util"
|
|
|
|
)
|
|
|
|
|
2022-05-09 17:03:40 +02:00
|
|
|
func init() {
|
|
|
|
commands = append(commands, cmdAccess)
|
|
|
|
}
|
|
|
|
|
2022-01-24 06:54:28 +01:00
|
|
|
const (
|
|
|
|
userEveryone = "everyone"
|
|
|
|
)
|
|
|
|
|
|
|
|
var flagsAccess = append(
|
2022-05-30 04:14:14 +02:00
|
|
|
flagsUser,
|
2022-01-24 06:54:28 +01:00
|
|
|
&cli.BoolFlag{Name: "reset", Aliases: []string{"r"}, Usage: "reset access for user (and topic)"},
|
|
|
|
)
|
|
|
|
|
|
|
|
var cmdAccess = &cli.Command{
|
|
|
|
Name: "access",
|
|
|
|
Usage: "Grant/revoke access to a topic, or show access",
|
|
|
|
UsageText: "ntfy access [USERNAME [TOPIC [PERMISSION]]]",
|
|
|
|
Flags: flagsAccess,
|
2022-06-01 22:57:35 +02:00
|
|
|
Before: initConfigFileInputSourceFunc("config", flagsAccess, initLogFunc),
|
2022-01-24 06:54:28 +01:00
|
|
|
Action: execUserAccess,
|
|
|
|
Category: categoryServer,
|
2022-02-01 22:40:33 +01:00
|
|
|
Description: `Manage the access control list for the ntfy server.
|
|
|
|
|
|
|
|
This is a server-only command. It directly manages the user.db as defined in the server config
|
|
|
|
file server.yml. The command only works if 'auth-file' is properly defined. Please also refer
|
|
|
|
to the related command 'ntfy user'.
|
|
|
|
|
|
|
|
The command allows you to show the access control list, as well as change it, depending on how
|
|
|
|
it is called.
|
|
|
|
|
|
|
|
Usage:
|
2022-02-04 02:07:23 +01:00
|
|
|
ntfy access # Shows access control list (alias: 'ntfy user list')
|
2022-02-01 22:40:33 +01:00
|
|
|
ntfy access USERNAME # Shows access control entries for USERNAME
|
|
|
|
ntfy access USERNAME TOPIC PERMISSION # Allow/deny access for USERNAME to TOPIC
|
|
|
|
|
|
|
|
Arguments:
|
2022-02-02 05:39:57 +01:00
|
|
|
USERNAME an existing user, as created with 'ntfy user add', or "everyone"/"*"
|
|
|
|
to define access rules for anonymous/unauthenticated clients
|
2022-02-01 22:40:33 +01:00
|
|
|
TOPIC name of a topic with optional wildcards, e.g. "mytopic*"
|
|
|
|
PERMISSION one of the following:
|
|
|
|
- read-write (alias: rw)
|
|
|
|
- read-only (aliases: read, ro)
|
|
|
|
- write-only (aliases: write, wo)
|
|
|
|
- deny (alias: none)
|
|
|
|
|
|
|
|
Examples:
|
2022-02-04 02:07:23 +01:00
|
|
|
ntfy access # Shows access control list (alias: 'ntfy user list')
|
2022-02-01 22:40:33 +01:00
|
|
|
ntfy access phil # Shows access for user phil
|
|
|
|
ntfy access phil mytopic rw # Allow read-write access to mytopic for user phil
|
|
|
|
ntfy access everyone mytopic rw # Allow anonymous read-write access to mytopic
|
|
|
|
ntfy access everyone "up*" write # Allow anonymous write-only access to topics "up..."
|
|
|
|
ntfy access --reset # Reset entire access control list
|
|
|
|
ntfy access --reset phil # Reset all access for user phil
|
|
|
|
ntfy access --reset phil mytopic # Reset access for user phil and topic mytopic
|
|
|
|
`,
|
2022-01-24 06:54:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func execUserAccess(c *cli.Context) error {
|
2022-02-01 22:40:33 +01:00
|
|
|
if c.NArg() > 3 {
|
|
|
|
return errors.New("too many arguments, please check 'ntfy access --help' for usage details")
|
|
|
|
}
|
2022-01-24 06:54:28 +01:00
|
|
|
manager, err := createAuthManager(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
username := c.Args().Get(0)
|
|
|
|
if username == userEveryone {
|
2022-12-25 17:41:38 +01:00
|
|
|
username = user.Everyone
|
2022-01-24 06:54:28 +01:00
|
|
|
}
|
|
|
|
topic := c.Args().Get(1)
|
|
|
|
perms := c.Args().Get(2)
|
|
|
|
reset := c.Bool("reset")
|
|
|
|
if reset {
|
2022-02-01 22:40:33 +01:00
|
|
|
if perms != "" {
|
|
|
|
return errors.New("too many arguments, please check 'ntfy access --help' for usage details")
|
|
|
|
}
|
2022-01-24 06:54:28 +01:00
|
|
|
return resetAccess(c, manager, username, topic)
|
|
|
|
} else if perms == "" {
|
2022-02-04 02:07:23 +01:00
|
|
|
if topic != "" {
|
|
|
|
return errors.New("invalid syntax, please check 'ntfy access --help' for usage details")
|
|
|
|
}
|
2022-01-24 06:54:28 +01:00
|
|
|
return showAccess(c, manager, username)
|
|
|
|
}
|
|
|
|
return changeAccess(c, manager, username, topic, perms)
|
|
|
|
}
|
|
|
|
|
2022-12-25 17:41:38 +01:00
|
|
|
func changeAccess(c *cli.Context, manager user.Manager, username string, topic string, perms string) error {
|
2022-10-01 21:50:48 +02:00
|
|
|
if !util.Contains([]string{"", "read-write", "rw", "read-only", "read", "ro", "write-only", "write", "wo", "none", "deny"}, perms) {
|
2022-01-24 06:54:28 +01:00
|
|
|
return errors.New("permission must be one of: read-write, read-only, write-only, or deny (or the aliases: read, ro, write, wo, none)")
|
|
|
|
}
|
2022-10-01 21:50:48 +02:00
|
|
|
read := util.Contains([]string{"read-write", "rw", "read-only", "read", "ro"}, perms)
|
|
|
|
write := util.Contains([]string{"read-write", "rw", "write-only", "write", "wo"}, perms)
|
2022-12-25 17:41:38 +01:00
|
|
|
u, err := manager.User(username)
|
|
|
|
if err == user.ErrNotFound {
|
2022-02-04 02:20:50 +01:00
|
|
|
return fmt.Errorf("user %s does not exist", username)
|
2022-12-25 17:41:38 +01:00
|
|
|
} else if u.Role == user.RoleAdmin {
|
2022-02-04 02:20:50 +01:00
|
|
|
return fmt.Errorf("user %s is an admin user, access control entries have no effect", username)
|
|
|
|
}
|
2022-01-24 06:54:28 +01:00
|
|
|
if err := manager.AllowAccess(username, topic, read, write); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if read && write {
|
2022-02-04 02:20:50 +01:00
|
|
|
fmt.Fprintf(c.App.ErrWriter, "granted read-write access to topic %s\n\n", topic)
|
2022-01-24 06:54:28 +01:00
|
|
|
} else if read {
|
2022-02-04 02:20:50 +01:00
|
|
|
fmt.Fprintf(c.App.ErrWriter, "granted read-only access to topic %s\n\n", topic)
|
2022-01-24 06:54:28 +01:00
|
|
|
} else if write {
|
2022-02-04 02:20:50 +01:00
|
|
|
fmt.Fprintf(c.App.ErrWriter, "granted write-only access to topic %s\n\n", topic)
|
2022-01-24 06:54:28 +01:00
|
|
|
} else {
|
2022-02-04 02:20:50 +01:00
|
|
|
fmt.Fprintf(c.App.ErrWriter, "revoked all access to topic %s\n\n", topic)
|
2022-01-24 06:54:28 +01:00
|
|
|
}
|
|
|
|
return showUserAccess(c, manager, username)
|
|
|
|
}
|
|
|
|
|
2022-12-25 17:41:38 +01:00
|
|
|
func resetAccess(c *cli.Context, manager user.Manager, username, topic string) error {
|
2022-01-24 06:54:28 +01:00
|
|
|
if username == "" {
|
|
|
|
return resetAllAccess(c, manager)
|
|
|
|
} else if topic == "" {
|
|
|
|
return resetUserAccess(c, manager, username)
|
|
|
|
}
|
|
|
|
return resetUserTopicAccess(c, manager, username, topic)
|
|
|
|
}
|
|
|
|
|
2022-12-25 17:41:38 +01:00
|
|
|
func resetAllAccess(c *cli.Context, manager user.Manager) error {
|
2022-01-24 06:54:28 +01:00
|
|
|
if err := manager.ResetAccess("", ""); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-04 02:20:50 +01:00
|
|
|
fmt.Fprintln(c.App.ErrWriter, "reset access for all users")
|
2022-01-24 06:54:28 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-25 17:41:38 +01:00
|
|
|
func resetUserAccess(c *cli.Context, manager user.Manager, username string) error {
|
2022-01-24 06:54:28 +01:00
|
|
|
if err := manager.ResetAccess(username, ""); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-04 02:20:50 +01:00
|
|
|
fmt.Fprintf(c.App.ErrWriter, "reset access for user %s\n\n", username)
|
2022-01-24 06:54:28 +01:00
|
|
|
return showUserAccess(c, manager, username)
|
|
|
|
}
|
|
|
|
|
2022-12-25 17:41:38 +01:00
|
|
|
func resetUserTopicAccess(c *cli.Context, manager user.Manager, username string, topic string) error {
|
2022-01-24 06:54:28 +01:00
|
|
|
if err := manager.ResetAccess(username, topic); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-04 02:20:50 +01:00
|
|
|
fmt.Fprintf(c.App.ErrWriter, "reset access for user %s and topic %s\n\n", username, topic)
|
2022-01-24 06:54:28 +01:00
|
|
|
return showUserAccess(c, manager, username)
|
|
|
|
}
|
|
|
|
|
2022-12-25 17:41:38 +01:00
|
|
|
func showAccess(c *cli.Context, manager user.Manager, username string) error {
|
2022-01-24 06:54:28 +01:00
|
|
|
if username == "" {
|
|
|
|
return showAllAccess(c, manager)
|
|
|
|
}
|
|
|
|
return showUserAccess(c, manager, username)
|
|
|
|
}
|
|
|
|
|
2022-12-25 17:41:38 +01:00
|
|
|
func showAllAccess(c *cli.Context, manager user.Manager) error {
|
2022-01-24 06:54:28 +01:00
|
|
|
users, err := manager.Users()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return showUsers(c, manager, users)
|
|
|
|
}
|
|
|
|
|
2022-12-25 17:41:38 +01:00
|
|
|
func showUserAccess(c *cli.Context, manager user.Manager, username string) error {
|
2022-01-24 06:54:28 +01:00
|
|
|
users, err := manager.User(username)
|
2022-12-25 17:41:38 +01:00
|
|
|
if err == user.ErrNotFound {
|
2022-02-04 02:07:23 +01:00
|
|
|
return fmt.Errorf("user %s does not exist", username)
|
|
|
|
} else if err != nil {
|
2022-01-24 06:54:28 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-12-25 17:41:38 +01:00
|
|
|
return showUsers(c, manager, []*user.User{users})
|
2022-01-24 06:54:28 +01:00
|
|
|
}
|
|
|
|
|
2022-12-25 17:41:38 +01:00
|
|
|
func showUsers(c *cli.Context, manager user.Manager, users []*user.User) error {
|
|
|
|
for _, u := range users {
|
|
|
|
fmt.Fprintf(c.App.ErrWriter, "user %s (%s)\n", u.Name, u.Role)
|
|
|
|
if u.Role == user.RoleAdmin {
|
2022-01-24 06:54:28 +01:00
|
|
|
fmt.Fprintf(c.App.ErrWriter, "- read-write access to all topics (admin role)\n")
|
2022-12-25 17:41:38 +01:00
|
|
|
} else if len(u.Grants) > 0 {
|
|
|
|
for _, grant := range u.Grants {
|
2022-02-01 01:33:22 +01:00
|
|
|
if grant.AllowRead && grant.AllowWrite {
|
2022-01-31 17:47:30 +01:00
|
|
|
fmt.Fprintf(c.App.ErrWriter, "- read-write access to topic %s\n", grant.TopicPattern)
|
2022-02-01 01:33:22 +01:00
|
|
|
} else if grant.AllowRead {
|
2022-01-31 17:47:30 +01:00
|
|
|
fmt.Fprintf(c.App.ErrWriter, "- read-only access to topic %s\n", grant.TopicPattern)
|
2022-02-01 01:33:22 +01:00
|
|
|
} else if grant.AllowWrite {
|
2022-01-31 17:47:30 +01:00
|
|
|
fmt.Fprintf(c.App.ErrWriter, "- write-only access to topic %s\n", grant.TopicPattern)
|
2022-01-24 06:54:28 +01:00
|
|
|
} else {
|
2022-01-31 17:47:30 +01:00
|
|
|
fmt.Fprintf(c.App.ErrWriter, "- no access to topic %s\n", grant.TopicPattern)
|
2022-01-24 06:54:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(c.App.ErrWriter, "- no topic-specific permissions\n")
|
|
|
|
}
|
2022-12-25 17:41:38 +01:00
|
|
|
if u.Name == user.Everyone {
|
2022-01-24 06:54:28 +01:00
|
|
|
defaultRead, defaultWrite := manager.DefaultAccess()
|
|
|
|
if defaultRead && defaultWrite {
|
|
|
|
fmt.Fprintln(c.App.ErrWriter, "- read-write access to all (other) topics (server config)")
|
|
|
|
} else if defaultRead {
|
|
|
|
fmt.Fprintln(c.App.ErrWriter, "- read-only access to all (other) topics (server config)")
|
|
|
|
} else if defaultWrite {
|
|
|
|
fmt.Fprintln(c.App.ErrWriter, "- write-only access to all (other) topics (server config)")
|
|
|
|
} else {
|
|
|
|
fmt.Fprintln(c.App.ErrWriter, "- no access to any (other) topics (server config)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|