guiconfiguration.go 5.3 KB

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