resetpwd.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (C) 2019-2023 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package cmd
  15. import (
  16. "bytes"
  17. "fmt"
  18. "os"
  19. "github.com/rs/zerolog"
  20. "github.com/spf13/cobra"
  21. "github.com/spf13/viper"
  22. "golang.org/x/term"
  23. "github.com/drakkan/sftpgo/v2/internal/config"
  24. "github.com/drakkan/sftpgo/v2/internal/dataprovider"
  25. "github.com/drakkan/sftpgo/v2/internal/logger"
  26. "github.com/drakkan/sftpgo/v2/internal/util"
  27. )
  28. var (
  29. resetPwdAdmin string
  30. resetPwdCmd = &cobra.Command{
  31. Use: "resetpwd",
  32. Short: "Reset the password for the specified administrator",
  33. Long: `This command reads the data provider connection details from the specified
  34. configuration file and resets the password for the specified administrator.
  35. This command is not supported for the memory provider.
  36. For embedded providers like bolt and SQLite you should stop the running SFTPGo
  37. instance to avoid database corruption.
  38. Please take a look at the usage below to customize the options.`,
  39. Run: func(_ *cobra.Command, _ []string) {
  40. logger.DisableLogger()
  41. logger.EnableConsoleLogger(zerolog.DebugLevel)
  42. configDir = util.CleanDirInput(configDir)
  43. err := config.LoadConfig(configDir, configFile)
  44. if err != nil {
  45. logger.WarnToConsole("Unable to load configuration: %v", err)
  46. os.Exit(1)
  47. }
  48. kmsConfig := config.GetKMSConfig()
  49. err = kmsConfig.Initialize()
  50. if err != nil {
  51. logger.ErrorToConsole("unable to initialize KMS: %v", err)
  52. os.Exit(1)
  53. }
  54. mfaConfig := config.GetMFAConfig()
  55. err = mfaConfig.Initialize()
  56. if err != nil {
  57. logger.ErrorToConsole("Unable to initialize MFA: %v", err)
  58. os.Exit(1)
  59. }
  60. providerConf := config.GetProviderConf()
  61. if providerConf.Driver == dataprovider.MemoryDataProviderName {
  62. logger.ErrorToConsole("memory provider is not supported")
  63. os.Exit(1)
  64. }
  65. logger.InfoToConsole("Initializing provider: %q config file: %q", providerConf.Driver, viper.ConfigFileUsed())
  66. err = dataprovider.Initialize(providerConf, configDir, false)
  67. if err != nil {
  68. logger.ErrorToConsole("Unable to initialize data provider: %v", err)
  69. os.Exit(1)
  70. }
  71. admin, err := dataprovider.AdminExists(resetPwdAdmin)
  72. if err != nil {
  73. logger.ErrorToConsole("Unable to get admin %q: %v", resetPwdAdmin, err)
  74. os.Exit(1)
  75. }
  76. fmt.Printf("Enter Password: ")
  77. pwd, err := term.ReadPassword(int(os.Stdin.Fd()))
  78. if err != nil {
  79. logger.ErrorToConsole("Unable to read the password: %v", err)
  80. os.Exit(1)
  81. }
  82. fmt.Println("")
  83. fmt.Printf("Confirm Password: ")
  84. confirmPwd, err := term.ReadPassword(int(os.Stdin.Fd()))
  85. if err != nil {
  86. logger.ErrorToConsole("Unable to read the password: %v", err)
  87. os.Exit(1)
  88. }
  89. fmt.Println("")
  90. if !bytes.Equal(pwd, confirmPwd) {
  91. logger.ErrorToConsole("Passwords do not match")
  92. os.Exit(1)
  93. }
  94. admin.Password = string(pwd)
  95. if err := dataprovider.UpdateAdmin(&admin, dataprovider.ActionExecutorSystem, "", ""); err != nil {
  96. logger.ErrorToConsole("Unable to update password: %v", err)
  97. os.Exit(1)
  98. }
  99. logger.InfoToConsole("Password updated for admin %q", resetPwdAdmin)
  100. },
  101. }
  102. )
  103. func init() {
  104. addConfigFlags(resetPwdCmd)
  105. resetPwdCmd.Flags().StringVar(&resetPwdAdmin, "admin", "", `Administrator username whose password to reset`)
  106. resetPwdCmd.MarkFlagRequired("admin") //nolint:errcheck
  107. rootCmd.AddCommand(resetPwdCmd)
  108. }