guiconfiguration.go 4.1 KB

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