You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

admin_auth_stmp.go 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package cmd
  4. import (
  5. "errors"
  6. "strings"
  7. auth_model "code.gitea.io/gitea/models/auth"
  8. "code.gitea.io/gitea/modules/util"
  9. "code.gitea.io/gitea/services/auth/source/smtp"
  10. "github.com/urfave/cli/v2"
  11. )
  12. var (
  13. smtpCLIFlags = []cli.Flag{
  14. &cli.StringFlag{
  15. Name: "name",
  16. Value: "",
  17. Usage: "Application Name",
  18. },
  19. &cli.StringFlag{
  20. Name: "auth-type",
  21. Value: "PLAIN",
  22. Usage: "SMTP Authentication Type (PLAIN/LOGIN/CRAM-MD5) default PLAIN",
  23. },
  24. &cli.StringFlag{
  25. Name: "host",
  26. Value: "",
  27. Usage: "SMTP Host",
  28. },
  29. &cli.IntFlag{
  30. Name: "port",
  31. Usage: "SMTP Port",
  32. },
  33. &cli.BoolFlag{
  34. Name: "force-smtps",
  35. Usage: "SMTPS is always used on port 465. Set this to force SMTPS on other ports.",
  36. Value: true,
  37. },
  38. &cli.BoolFlag{
  39. Name: "skip-verify",
  40. Usage: "Skip TLS verify.",
  41. Value: true,
  42. },
  43. &cli.StringFlag{
  44. Name: "helo-hostname",
  45. Value: "",
  46. Usage: "Hostname sent with HELO. Leave blank to send current hostname",
  47. },
  48. &cli.BoolFlag{
  49. Name: "disable-helo",
  50. Usage: "Disable SMTP helo.",
  51. Value: true,
  52. },
  53. &cli.StringFlag{
  54. Name: "allowed-domains",
  55. Value: "",
  56. Usage: "Leave empty to allow all domains. Separate multiple domains with a comma (',')",
  57. },
  58. &cli.BoolFlag{
  59. Name: "skip-local-2fa",
  60. Usage: "Skip 2FA to log on.",
  61. Value: true,
  62. },
  63. &cli.BoolFlag{
  64. Name: "active",
  65. Usage: "This Authentication Source is Activated.",
  66. Value: true,
  67. },
  68. }
  69. microcmdAuthAddSMTP = &cli.Command{
  70. Name: "add-smtp",
  71. Usage: "Add new SMTP authentication source",
  72. Action: runAddSMTP,
  73. Flags: smtpCLIFlags,
  74. }
  75. microcmdAuthUpdateSMTP = &cli.Command{
  76. Name: "update-smtp",
  77. Usage: "Update existing SMTP authentication source",
  78. Action: runUpdateSMTP,
  79. Flags: append(smtpCLIFlags[:1], append([]cli.Flag{idFlag}, smtpCLIFlags[1:]...)...),
  80. }
  81. )
  82. func parseSMTPConfig(c *cli.Context, conf *smtp.Source) error {
  83. if c.IsSet("auth-type") {
  84. conf.Auth = c.String("auth-type")
  85. validAuthTypes := []string{"PLAIN", "LOGIN", "CRAM-MD5"}
  86. if !util.SliceContainsString(validAuthTypes, strings.ToUpper(c.String("auth-type"))) {
  87. return errors.New("Auth must be one of PLAIN/LOGIN/CRAM-MD5")
  88. }
  89. conf.Auth = c.String("auth-type")
  90. }
  91. if c.IsSet("host") {
  92. conf.Host = c.String("host")
  93. }
  94. if c.IsSet("port") {
  95. conf.Port = c.Int("port")
  96. }
  97. if c.IsSet("allowed-domains") {
  98. conf.AllowedDomains = c.String("allowed-domains")
  99. }
  100. if c.IsSet("force-smtps") {
  101. conf.ForceSMTPS = c.Bool("force-smtps")
  102. }
  103. if c.IsSet("skip-verify") {
  104. conf.SkipVerify = c.Bool("skip-verify")
  105. }
  106. if c.IsSet("helo-hostname") {
  107. conf.HeloHostname = c.String("helo-hostname")
  108. }
  109. if c.IsSet("disable-helo") {
  110. conf.DisableHelo = c.Bool("disable-helo")
  111. }
  112. if c.IsSet("skip-local-2fa") {
  113. conf.SkipLocalTwoFA = c.Bool("skip-local-2fa")
  114. }
  115. return nil
  116. }
  117. func runAddSMTP(c *cli.Context) error {
  118. ctx, cancel := installSignals()
  119. defer cancel()
  120. if err := initDB(ctx); err != nil {
  121. return err
  122. }
  123. if !c.IsSet("name") || len(c.String("name")) == 0 {
  124. return errors.New("name must be set")
  125. }
  126. if !c.IsSet("host") || len(c.String("host")) == 0 {
  127. return errors.New("host must be set")
  128. }
  129. if !c.IsSet("port") {
  130. return errors.New("port must be set")
  131. }
  132. active := true
  133. if c.IsSet("active") {
  134. active = c.Bool("active")
  135. }
  136. var smtpConfig smtp.Source
  137. if err := parseSMTPConfig(c, &smtpConfig); err != nil {
  138. return err
  139. }
  140. // If not set default to PLAIN
  141. if len(smtpConfig.Auth) == 0 {
  142. smtpConfig.Auth = "PLAIN"
  143. }
  144. return auth_model.CreateSource(ctx, &auth_model.Source{
  145. Type: auth_model.SMTP,
  146. Name: c.String("name"),
  147. IsActive: active,
  148. Cfg: &smtpConfig,
  149. })
  150. }
  151. func runUpdateSMTP(c *cli.Context) error {
  152. if !c.IsSet("id") {
  153. return errors.New("--id flag is missing")
  154. }
  155. ctx, cancel := installSignals()
  156. defer cancel()
  157. if err := initDB(ctx); err != nil {
  158. return err
  159. }
  160. source, err := auth_model.GetSourceByID(ctx, c.Int64("id"))
  161. if err != nil {
  162. return err
  163. }
  164. smtpConfig := source.Cfg.(*smtp.Source)
  165. if err := parseSMTPConfig(c, smtpConfig); err != nil {
  166. return err
  167. }
  168. if c.IsSet("name") {
  169. source.Name = c.String("name")
  170. }
  171. if c.IsSet("active") {
  172. source.IsActive = c.Bool("active")
  173. }
  174. source.Cfg = smtpConfig
  175. return auth_model.UpdateSource(ctx, source)
  176. }