guiconfiguration.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package config
  7. import (
  8. "net/url"
  9. "os"
  10. "regexp"
  11. "strconv"
  12. "strings"
  13. "golang.org/x/crypto/bcrypt"
  14. "github.com/syncthing/syncthing/lib/rand"
  15. )
  16. func (c GUIConfiguration) IsAuthEnabled() bool {
  17. return c.AuthMode == AuthModeLDAP || (len(c.User) > 0 && len(c.Password) > 0)
  18. }
  19. func (GUIConfiguration) IsOverridden() bool {
  20. return os.Getenv("STGUIADDRESS") != ""
  21. }
  22. func (c GUIConfiguration) Address() string {
  23. if override := os.Getenv("STGUIADDRESS"); override != "" {
  24. // This value may be of the form "scheme://address:port" or just
  25. // "address:port". We need to chop off the scheme. We try to parse it as
  26. // an URL if it contains a slash. If that fails, return it as is and let
  27. // some other error handling handle it.
  28. if strings.Contains(override, "/") {
  29. url, err := url.Parse(override)
  30. if err != nil {
  31. return override
  32. }
  33. if strings.HasPrefix(url.Scheme, "unix") {
  34. return url.Path
  35. }
  36. return url.Host
  37. }
  38. return override
  39. }
  40. return c.RawAddress
  41. }
  42. func (c GUIConfiguration) UnixSocketPermissions() os.FileMode {
  43. perm, err := strconv.ParseUint(c.RawUnixSocketPermissions, 8, 32)
  44. if err != nil {
  45. // ignore incorrectly formatted permissions
  46. return 0
  47. }
  48. return os.FileMode(perm) & os.ModePerm
  49. }
  50. func (c GUIConfiguration) Network() string {
  51. if override := os.Getenv("STGUIADDRESS"); override != "" {
  52. url, err := url.Parse(override)
  53. if err == nil && strings.HasPrefix(url.Scheme, "unix") {
  54. return "unix"
  55. }
  56. return "tcp"
  57. }
  58. if strings.HasPrefix(c.RawAddress, "/") {
  59. return "unix"
  60. }
  61. return "tcp"
  62. }
  63. func (c GUIConfiguration) UseTLS() bool {
  64. if override := os.Getenv("STGUIADDRESS"); override != "" {
  65. return strings.HasPrefix(override, "https:") || strings.HasPrefix(override, "unixs:")
  66. }
  67. return c.RawUseTLS
  68. }
  69. func (c GUIConfiguration) URL() string {
  70. if c.Network() == "unix" {
  71. if c.UseTLS() {
  72. return "unixs://" + c.Address()
  73. }
  74. return "unix://" + c.Address()
  75. }
  76. u := url.URL{
  77. Scheme: "http",
  78. Host: c.Address(),
  79. Path: "/",
  80. }
  81. if c.UseTLS() {
  82. u.Scheme = "https"
  83. }
  84. if strings.HasPrefix(u.Host, ":") {
  85. // Empty host, i.e. ":port", use IPv4 localhost
  86. u.Host = "127.0.0.1" + u.Host
  87. } else if strings.HasPrefix(u.Host, "0.0.0.0:") {
  88. // IPv4 all zeroes host, convert to IPv4 localhost
  89. u.Host = "127.0.0.1" + u.Host[7:]
  90. } else if strings.HasPrefix(u.Host, "[::]:") {
  91. // IPv6 all zeroes host, convert to IPv6 localhost
  92. u.Host = "[::1]" + u.Host[4:]
  93. }
  94. return u.String()
  95. }
  96. // matches a bcrypt hash and not too much else
  97. var bcryptExpr = regexp.MustCompile(`^\$2[aby]\$\d+\$.{50,}`)
  98. // SetPassword takes a bcrypt hash or a plaintext password and stores it.
  99. // Plaintext passwords are hashed. Returns an error if the password is not
  100. // valid.
  101. func (c *GUIConfiguration) SetPassword(password string) error {
  102. if bcryptExpr.MatchString(password) {
  103. // Already hashed
  104. c.Password = password
  105. return nil
  106. }
  107. hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
  108. if err != nil {
  109. return err
  110. }
  111. c.Password = string(hash)
  112. return nil
  113. }
  114. // CompareHashedPassword returns nil when the given plaintext password matches the stored hash.
  115. func (c GUIConfiguration) CompareHashedPassword(password string) error {
  116. configPasswordBytes := []byte(c.Password)
  117. passwordBytes := []byte(password)
  118. return bcrypt.CompareHashAndPassword(configPasswordBytes, passwordBytes)
  119. }
  120. // IsValidAPIKey returns true when the given API key is valid, including both
  121. // the value in config and any overrides
  122. func (c GUIConfiguration) IsValidAPIKey(apiKey string) bool {
  123. switch apiKey {
  124. case "":
  125. return false
  126. case c.APIKey, os.Getenv("STGUIAPIKEY"):
  127. return true
  128. default:
  129. return false
  130. }
  131. }
  132. func (c *GUIConfiguration) prepare() {
  133. if c.APIKey == "" {
  134. c.APIKey = rand.String(32)
  135. }
  136. }
  137. func (c GUIConfiguration) Copy() GUIConfiguration {
  138. return c
  139. }