access.go 7.7 KB

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