| 1 |
//go:build !noserver |
| 2 |
|
| 3 |
package cmd |
| 4 |
|
| 5 |
import ( |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"github.com/urfave/cli/v2" |
| 9 |
"heckel.io/ntfy/v2/user" |
| 10 |
"heckel.io/ntfy/v2/util" |
| 11 |
) |
| 12 |
|
| 13 |
func init() { |
| 14 |
commands = append(commands, cmdAccess) |
| 15 |
} |
| 16 |
|
| 17 |
const ( |
| 18 |
userEveryone = "everyone" |
| 19 |
) |
| 20 |
|
| 21 |
var flagsAccess = append( |
| 22 |
append([]cli.Flag{}, flagsUser...), |
| 23 |
&cli.BoolFlag{Name: "reset", Aliases: []string{"r"}, Usage: "reset access for user (and topic)"}, |
| 24 |
) |
| 25 |
|
| 26 |
var cmdAccess = &cli.Command{ |
| 27 |
Name: "access", |
| 28 |
Usage: "Grant/revoke access to a topic, or show access", |
| 29 |
UsageText: "ntfy access [USERNAME [TOPIC [PERMISSION]]]", |
| 30 |
Flags: flagsAccess, |
| 31 |
Before: initConfigFileInputSourceFunc("config", flagsAccess, initLogFunc), |
| 32 |
Action: execUserAccess, |
| 33 |
Category: categoryServer, |
| 34 |
Description: `Manage the access control list for the ntfy server. |
| 35 |
|
| 36 |
This is a server-only command. It directly manages the user.db as defined in the server config |
| 37 |
file server.yml. The command only works if 'auth-file' is properly defined. Please also refer |
| 38 |
to the related command 'ntfy user'. |
| 39 |
|
| 40 |
The command allows you to show the access control list, as well as change it, depending on how |
| 41 |
it is called. |
| 42 |
|
| 43 |
Usage: |
| 44 |
ntfy access # Shows access control list (alias: 'ntfy user list') |
| 45 |
ntfy access USERNAME # Shows access control entries for USERNAME |
| 46 |
ntfy access USERNAME TOPIC PERMISSION # Allow/deny access for USERNAME to TOPIC |
| 47 |
|
| 48 |
Arguments: |
| 49 |
USERNAME an existing user, as created with 'ntfy user add', or "everyone"/"*" |
| 50 |
to define access rules for anonymous/unauthenticated clients |
| 51 |
TOPIC name of a topic with optional wildcards, e.g. "mytopic*" |
| 52 |
PERMISSION one of the following: |
| 53 |
- read-write (alias: rw) |
| 54 |
- read-only (aliases: read, ro) |
| 55 |
- write-only (aliases: write, wo) |
| 56 |
- deny (alias: none) |
| 57 |
|
| 58 |
Examples: |
| 59 |
ntfy access # Shows access control list (alias: 'ntfy user list') |
| 60 |
ntfy access phil # Shows access for user phil |
| 61 |
ntfy access phil mytopic rw # Allow read-write access to mytopic for user phil |
| 62 |
ntfy access everyone mytopic rw # Allow anonymous read-write access to mytopic |
| 63 |
ntfy access everyone "up*" write # Allow anonymous write-only access to topics "up..." |
| 64 |
ntfy access --reset # Reset entire access control list |
| 65 |
ntfy access --reset phil # Reset all access for user phil |
| 66 |
ntfy access --reset phil mytopic # Reset access for user phil and topic mytopic |
| 67 |
`, |
| 68 |
} |
| 69 |
|
| 70 |
func execUserAccess(c *cli.Context) error { |
| 71 |
if c.NArg() > 3 { |
| 72 |
return errors.New("too many arguments, please check 'ntfy access --help' for usage details") |
| 73 |
} |
| 74 |
manager, err := createUserManager(c) |
| 75 |
if err != nil { |
| 76 |
return err |
| 77 |
} |
| 78 |
username := c.Args().Get(0) |
| 79 |
if username == userEveryone { |
| 80 |
username = user.Everyone |
| 81 |
} |
| 82 |
topic := c.Args().Get(1) |
| 83 |
perms := c.Args().Get(2) |
| 84 |
reset := c.Bool("reset") |
| 85 |
if reset { |
| 86 |
if perms != "" { |
| 87 |
return errors.New("too many arguments, please check 'ntfy access --help' for usage details") |
| 88 |
} |
| 89 |
return resetAccess(c, manager, username, topic) |
| 90 |
} else if perms == "" { |
| 91 |
if topic != "" { |
| 92 |
return errors.New("invalid syntax, please check 'ntfy access --help' for usage details") |
| 93 |
} |
| 94 |
return showAccess(c, manager, username) |
| 95 |
} |
| 96 |
return changeAccess(c, manager, username, topic, perms) |
| 97 |
} |
| 98 |
|
| 99 |
func changeAccess(c *cli.Context, manager *user.Manager, username string, topic string, perms string) error { |
| 100 |
if !util.Contains([]string{"", "read-write", "rw", "read-only", "read", "ro", "write-only", "write", "wo", "none", "deny"}, perms) { |
| 101 |
return errors.New("permission must be one of: read-write, read-only, write-only, or deny (or the aliases: read, ro, write, wo, none)") |
| 102 |
} |
| 103 |
permission, err := user.ParsePermission(perms) |
| 104 |
if err != nil { |
| 105 |
return err |
| 106 |
} |
| 107 |
u, err := manager.User(username) |
| 108 |
if errors.Is(err, user.ErrUserNotFound) { |
| 109 |
return fmt.Errorf("user %s does not exist", username) |
| 110 |
} else if err != nil { |
| 111 |
return err |
| 112 |
} else if u.Role == user.RoleAdmin { |
| 113 |
return fmt.Errorf("user %s is an admin user, access control entries have no effect", username) |
| 114 |
} |
| 115 |
if err := manager.AllowAccess(username, topic, permission); err != nil { |
| 116 |
return err |
| 117 |
} |
| 118 |
if permission.IsReadWrite() { |
| 119 |
fmt.Fprintf(c.App.Writer, "granted read-write access to topic %s\n\n", topic) |
| 120 |
} else if permission.IsRead() { |
| 121 |
fmt.Fprintf(c.App.Writer, "granted read-only access to topic %s\n\n", topic) |
| 122 |
} else if permission.IsWrite() { |
| 123 |
fmt.Fprintf(c.App.Writer, "granted write-only access to topic %s\n\n", topic) |
| 124 |
} else { |
| 125 |
fmt.Fprintf(c.App.Writer, "revoked all access to topic %s\n\n", topic) |
| 126 |
} |
| 127 |
return showUserAccess(c, manager, username) |
| 128 |
} |
| 129 |
|
| 130 |
func resetAccess(c *cli.Context, manager *user.Manager, username, topic string) error { |
| 131 |
if username == "" { |
| 132 |
return resetAllAccess(c, manager) |
| 133 |
} else if topic == "" { |
| 134 |
return resetUserAccess(c, manager, username) |
| 135 |
} |
| 136 |
return resetUserTopicAccess(c, manager, username, topic) |
| 137 |
} |
| 138 |
|
| 139 |
func resetAllAccess(c *cli.Context, manager *user.Manager) error { |
| 140 |
if err := manager.ResetAccess("", ""); err != nil { |
| 141 |
return err |
| 142 |
} |
| 143 |
fmt.Fprintln(c.App.Writer, "reset access for all users") |
| 144 |
return nil |
| 145 |
} |
| 146 |
|
| 147 |
func resetUserAccess(c *cli.Context, manager *user.Manager, username string) error { |
| 148 |
if err := manager.ResetAccess(username, ""); err != nil { |
| 149 |
return err |
| 150 |
} |
| 151 |
fmt.Fprintf(c.App.Writer, "reset access for user %s\n\n", username) |
| 152 |
return showUserAccess(c, manager, username) |
| 153 |
} |
| 154 |
|
| 155 |
func resetUserTopicAccess(c *cli.Context, manager *user.Manager, username string, topic string) error { |
| 156 |
if err := manager.ResetAccess(username, topic); err != nil { |
| 157 |
return err |
| 158 |
} |
| 159 |
fmt.Fprintf(c.App.Writer, "reset access for user %s and topic %s\n\n", username, topic) |
| 160 |
return showUserAccess(c, manager, username) |
| 161 |
} |
| 162 |
|
| 163 |
func showAccess(c *cli.Context, manager *user.Manager, username string) error { |
| 164 |
if username == "" { |
| 165 |
return showAllAccess(c, manager) |
| 166 |
} |
| 167 |
return showUserAccess(c, manager, username) |
| 168 |
} |
| 169 |
|
| 170 |
func showAllAccess(c *cli.Context, manager *user.Manager) error { |
| 171 |
users, err := manager.Users() |
| 172 |
if err != nil { |
| 173 |
return err |
| 174 |
} |
| 175 |
return showUsers(c, manager, users) |
| 176 |
} |
| 177 |
|
| 178 |
func showUserAccess(c *cli.Context, manager *user.Manager, username string) error { |
| 179 |
users, err := manager.User(username) |
| 180 |
if errors.Is(err, user.ErrUserNotFound) { |
| 181 |
return fmt.Errorf("user %s does not exist", username) |
| 182 |
} else if err != nil { |
| 183 |
return err |
| 184 |
} |
| 185 |
return showUsers(c, manager, []*user.User{users}) |
| 186 |
} |
| 187 |
|
| 188 |
func showUsers(c *cli.Context, manager *user.Manager, users []*user.User) error { |
| 189 |
for _, u := range users { |
| 190 |
grants, err := manager.Grants(u.Name) |
| 191 |
if err != nil { |
| 192 |
return err |
| 193 |
} |
| 194 |
tier := "none" |
| 195 |
if u.Tier != nil { |
| 196 |
tier = u.Tier.Name |
| 197 |
} |
| 198 |
provisioned := "" |
| 199 |
if u.Provisioned { |
| 200 |
provisioned = ", server config" |
| 201 |
} |
| 202 |
fmt.Fprintf(c.App.Writer, "user %s (role: %s, tier: %s%s)\n", u.Name, u.Role, tier, provisioned) |
| 203 |
if u.Role == user.RoleAdmin { |
| 204 |
fmt.Fprintf(c.App.Writer, "- read-write access to all topics (admin role)\n") |
| 205 |
} else if len(grants) > 0 { |
| 206 |
for _, grant := range grants { |
| 207 |
grantProvisioned := "" |
| 208 |
if grant.Provisioned { |
| 209 |
grantProvisioned = " (server config)" |
| 210 |
} |
| 211 |
if grant.Permission.IsReadWrite() { |
| 212 |
fmt.Fprintf(c.App.Writer, "- read-write access to topic %s%s\n", grant.TopicPattern, grantProvisioned) |
| 213 |
} else if grant.Permission.IsRead() { |
| 214 |
fmt.Fprintf(c.App.Writer, "- read-only access to topic %s%s\n", grant.TopicPattern, grantProvisioned) |
| 215 |
} else if grant.Permission.IsWrite() { |
| 216 |
fmt.Fprintf(c.App.Writer, "- write-only access to topic %s%s\n", grant.TopicPattern, grantProvisioned) |
| 217 |
} else { |
| 218 |
fmt.Fprintf(c.App.Writer, "- no access to topic %s%s\n", grant.TopicPattern, grantProvisioned) |
| 219 |
} |
| 220 |
} |
| 221 |
} else { |
| 222 |
fmt.Fprintf(c.App.Writer, "- no topic-specific permissions\n") |
| 223 |
} |
| 224 |
if u.Name == user.Everyone { |
| 225 |
access := manager.DefaultAccess() |
| 226 |
if access.IsReadWrite() { |
| 227 |
fmt.Fprintln(c.App.Writer, "- read-write access to all (other) topics (server config)") |
| 228 |
} else if access.IsRead() { |
| 229 |
fmt.Fprintln(c.App.Writer, "- read-only access to all (other) topics (server config)") |
| 230 |
} else if access.IsWrite() { |
| 231 |
fmt.Fprintln(c.App.Writer, "- write-only access to all (other) topics (server config)") |
| 232 |
} else { |
| 233 |
fmt.Fprintln(c.App.Writer, "- no access to any (other) topics (server config)") |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
return nil |
| 238 |
} |