access.go 7.8 KB

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