user.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package cmd
  2. import (
  3. "crypto/subtle"
  4. "errors"
  5. "fmt"
  6. "github.com/urfave/cli/v2"
  7. "github.com/urfave/cli/v2/altsrc"
  8. "heckel.io/ntfy/auth"
  9. "heckel.io/ntfy/util"
  10. "strings"
  11. )
  12. var flagsUser = userCommandFlags()
  13. var cmdUser = &cli.Command{
  14. Name: "user",
  15. Usage: "Manage/show users",
  16. UsageText: "ntfy user [list|add|remove|change-pass|change-role] ...",
  17. Flags: flagsUser,
  18. Before: initConfigFileInputSource("config", flagsUser),
  19. Category: categoryServer,
  20. Subcommands: []*cli.Command{
  21. {
  22. Name: "add",
  23. Aliases: []string{"a"},
  24. Usage: "add user",
  25. UsageText: "ntfy user add [--role=admin|user] USERNAME",
  26. Action: execUserAdd,
  27. Flags: []cli.Flag{
  28. &cli.StringFlag{Name: "role", Aliases: []string{"r"}, Value: string(auth.RoleUser), Usage: "user role"},
  29. },
  30. Description: `Add a new user to the ntfy user database.
  31. A user can be either a regular user, or an admin. A regular user has no read or write access (unless
  32. granted otherwise by the auth-default-access setting). An admin user has read and write access to all
  33. topics.
  34. Examples:
  35. ntfy user add phil # Add regular user phil
  36. ntfy user add --role=admin phil # Add admin user phil
  37. `,
  38. },
  39. {
  40. Name: "remove",
  41. Aliases: []string{"del", "rm"},
  42. Usage: "remove user",
  43. UsageText: "ntfy user remove USERNAME",
  44. Action: execUserDel,
  45. Description: `Remove a user from the ntfy user database.
  46. Example:
  47. ntfy user del phil
  48. `,
  49. },
  50. {
  51. Name: "change-pass",
  52. Aliases: []string{"chp"},
  53. Usage: "change user password",
  54. UsageText: "ntfy user change-pass USERNAME",
  55. Action: execUserChangePass,
  56. Description: `Change the password for the given user.
  57. The new password will be read from STDIN, and it'll be confirmed by typing
  58. it twice.
  59. Example:
  60. ntfy user change-pass phil
  61. `,
  62. },
  63. {
  64. Name: "change-role",
  65. Aliases: []string{"chr"},
  66. Usage: "change user role",
  67. UsageText: "ntfy user change-role USERNAME ROLE",
  68. Action: execUserChangeRole,
  69. Description: `Change the role for the given user to admin or user.
  70. This command can be used to change the role of a user either from a regular user
  71. to an admin user, or the other way around:
  72. - admin: an admin has read/write access to all topics
  73. - user: a regular user only has access to what was explicitly granted via 'ntfy access'
  74. When changing the role of a user to "admin", all access control entries for that
  75. user are removed, since they are no longer necessary.
  76. Example:
  77. ntfy user change-role phil admin # Make user phil an admin
  78. ntfy user change-role phil user # Remove admin role from user phil
  79. `,
  80. },
  81. {
  82. Name: "list",
  83. Aliases: []string{"l"},
  84. Usage: "list users",
  85. Action: execUserList,
  86. },
  87. },
  88. Description: `Manage users of the ntfy server.
  89. This is a server-only command. It directly manages the user.db as defined in the server config
  90. file server.yml. The command only works if 'auth-file' is properly defined. Please also refer
  91. to the related command 'ntfy access'.
  92. The command allows you to add/remove/change users in the ntfy user database, as well as change
  93. passwords or roles.
  94. Examples:
  95. ntfy user list # Shows list of users
  96. ntfy user add phil # Add regular user phil
  97. ntfy user add --role=admin phil # Add admin user phil
  98. ntfy user del phil # Delete user phil
  99. ntfy user change-pass phil # Change password for user phil
  100. ntfy user change-role phil admin # Make user phil an admin
  101. `,
  102. }
  103. func execUserAdd(c *cli.Context) error {
  104. username := c.Args().Get(0)
  105. role := auth.Role(c.String("role"))
  106. if username == "" {
  107. return errors.New("username expected, type 'ntfy user add --help' for help")
  108. } else if username == userEveryone {
  109. return errors.New("username not allowed")
  110. } else if !auth.AllowedRole(role) {
  111. return errors.New("role must be either 'user' or 'admin'")
  112. }
  113. password, err := readPassword(c)
  114. if err != nil {
  115. return err
  116. }
  117. manager, err := createAuthManager(c)
  118. if err != nil {
  119. return err
  120. }
  121. if err := manager.AddUser(username, password, auth.Role(role)); err != nil {
  122. return err
  123. }
  124. fmt.Fprintf(c.App.ErrWriter, "User %s added with role %s\n", username, role)
  125. return nil
  126. }
  127. func execUserDel(c *cli.Context) error {
  128. username := c.Args().Get(0)
  129. if username == "" {
  130. return errors.New("username expected, type 'ntfy user del --help' for help")
  131. } else if username == userEveryone {
  132. return errors.New("username not allowed")
  133. }
  134. manager, err := createAuthManager(c)
  135. if err != nil {
  136. return err
  137. }
  138. if err := manager.RemoveUser(username); err != nil {
  139. return err
  140. }
  141. fmt.Fprintf(c.App.ErrWriter, "User %s removed\n", username)
  142. return nil
  143. }
  144. func execUserChangePass(c *cli.Context) error {
  145. username := c.Args().Get(0)
  146. if username == "" {
  147. return errors.New("username expected, type 'ntfy user change-pass --help' for help")
  148. } else if username == userEveryone {
  149. return errors.New("username not allowed")
  150. }
  151. password, err := readPassword(c)
  152. if err != nil {
  153. return err
  154. }
  155. manager, err := createAuthManager(c)
  156. if err != nil {
  157. return err
  158. }
  159. if err := manager.ChangePassword(username, password); err != nil {
  160. return err
  161. }
  162. fmt.Fprintf(c.App.ErrWriter, "Changed password for user %s\n", username)
  163. return nil
  164. }
  165. func execUserChangeRole(c *cli.Context) error {
  166. username := c.Args().Get(0)
  167. role := auth.Role(c.Args().Get(1))
  168. if username == "" || !auth.AllowedRole(role) {
  169. return errors.New("username and new role expected, type 'ntfy user change-role --help' for help")
  170. } else if username == userEveryone {
  171. return errors.New("username not allowed")
  172. }
  173. manager, err := createAuthManager(c)
  174. if err != nil {
  175. return err
  176. }
  177. if err := manager.ChangeRole(username, role); err != nil {
  178. return err
  179. }
  180. fmt.Fprintf(c.App.ErrWriter, "Changed role for user %s to %s\n", username, role)
  181. return nil
  182. }
  183. func execUserList(c *cli.Context) error {
  184. manager, err := createAuthManager(c)
  185. if err != nil {
  186. return err
  187. }
  188. users, err := manager.Users()
  189. if err != nil {
  190. return err
  191. }
  192. return showUsers(c, manager, users)
  193. }
  194. func createAuthManager(c *cli.Context) (auth.Manager, error) {
  195. authFile := c.String("auth-file")
  196. authDefaultAccess := c.String("auth-default-access")
  197. if authFile == "" {
  198. return nil, errors.New("option auth-file not set; auth is unconfigured for this server")
  199. } else if !util.FileExists(authFile) {
  200. return nil, errors.New("auth-file does not exist; please start the server at least once to create it")
  201. } else if !util.InStringList([]string{"read-write", "read-only", "write-only", "deny-all"}, authDefaultAccess) {
  202. return nil, errors.New("if set, auth-default-access must start set to 'read-write', 'read-only' or 'deny-all'")
  203. }
  204. authDefaultRead := authDefaultAccess == "read-write" || authDefaultAccess == "read-only"
  205. authDefaultWrite := authDefaultAccess == "read-write" || authDefaultAccess == "write-only"
  206. return auth.NewSQLiteAuth(authFile, authDefaultRead, authDefaultWrite)
  207. }
  208. func readPassword(c *cli.Context) (string, error) {
  209. fmt.Fprint(c.App.ErrWriter, "Enter Password: ")
  210. password, err := util.ReadPassword(c.App.Reader)
  211. if err != nil {
  212. return "", err
  213. }
  214. fmt.Fprintf(c.App.ErrWriter, "\r%s\rConfirm: ", strings.Repeat(" ", 25))
  215. confirm, err := util.ReadPassword(c.App.Reader)
  216. if err != nil {
  217. return "", err
  218. }
  219. fmt.Fprintf(c.App.ErrWriter, "\r%s\r", strings.Repeat(" ", 25))
  220. if subtle.ConstantTimeCompare(confirm, password) != 1 {
  221. return "", errors.New("passwords do not match: try it again, but this time type slooowwwlly")
  222. }
  223. return string(password), nil
  224. }
  225. func userCommandFlags() []cli.Flag {
  226. return []cli.Flag{
  227. &cli.StringFlag{Name: "config", Aliases: []string{"c"}, EnvVars: []string{"NTFY_CONFIG_FILE"}, Value: "/etc/ntfy/server.yml", DefaultText: "/etc/ntfy/server.yml", Usage: "config file"},
  228. altsrc.NewStringFlag(&cli.StringFlag{Name: "auth-file", Aliases: []string{"H"}, EnvVars: []string{"NTFY_AUTH_FILE"}, Usage: "auth database file used for access control"}),
  229. altsrc.NewStringFlag(&cli.StringFlag{Name: "auth-default-access", Aliases: []string{"p"}, EnvVars: []string{"NTFY_AUTH_DEFAULT_ACCESS"}, Value: "read-write", Usage: "default permissions if no matching entries in the auth database are found"}),
  230. }
  231. }