utils.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 syncthing
  7. import (
  8. "crypto/tls"
  9. "errors"
  10. "fmt"
  11. "io"
  12. "os"
  13. "github.com/syncthing/syncthing/lib/config"
  14. "github.com/syncthing/syncthing/lib/db/backend"
  15. "github.com/syncthing/syncthing/lib/events"
  16. "github.com/syncthing/syncthing/lib/fs"
  17. "github.com/syncthing/syncthing/lib/locations"
  18. "github.com/syncthing/syncthing/lib/protocol"
  19. "github.com/syncthing/syncthing/lib/tlsutil"
  20. )
  21. func EnsureDir(dir string, mode fs.FileMode) error {
  22. fs := fs.NewFilesystem(fs.FilesystemTypeBasic, dir)
  23. err := fs.MkdirAll(".", mode)
  24. if err != nil {
  25. return err
  26. }
  27. if fi, err := fs.Stat("."); err == nil {
  28. // Apparently the stat may fail even though the mkdirall passed. If it
  29. // does, we'll just assume things are in order and let other things
  30. // fail (like loading or creating the config...).
  31. currentMode := fi.Mode() & 0o777
  32. if currentMode != mode {
  33. err := fs.Chmod(".", mode)
  34. // This can fail on crappy filesystems, nothing we can do about it.
  35. if err != nil {
  36. l.Warnln(err)
  37. }
  38. }
  39. }
  40. return nil
  41. }
  42. func LoadOrGenerateCertificate(certFile, keyFile string) (tls.Certificate, error) {
  43. cert, err := tls.LoadX509KeyPair(certFile, keyFile)
  44. if err != nil {
  45. return GenerateCertificate(certFile, keyFile)
  46. }
  47. return cert, nil
  48. }
  49. func GenerateCertificate(certFile, keyFile string) (tls.Certificate, error) {
  50. l.Infof("Generating ECDSA key and certificate for %s...", tlsDefaultCommonName)
  51. return tlsutil.NewCertificate(certFile, keyFile, tlsDefaultCommonName, deviceCertLifetimeDays)
  52. }
  53. func DefaultConfig(path string, myID protocol.DeviceID, evLogger events.Logger, noDefaultFolder, skipPortProbing bool) (config.Wrapper, error) {
  54. newCfg := config.New(myID)
  55. if skipPortProbing {
  56. l.Infoln("Using default network port numbers instead of probing for free ports")
  57. // Record address override initially
  58. newCfg.GUI.RawAddress = newCfg.GUI.Address()
  59. } else if err := newCfg.ProbeFreePorts(); err != nil {
  60. return nil, err
  61. }
  62. if noDefaultFolder {
  63. l.Infoln("We will skip creation of a default folder on first start")
  64. return config.Wrap(path, newCfg, myID, evLogger), nil
  65. }
  66. fcfg := newCfg.Defaults.Folder.Copy()
  67. fcfg.ID = "default"
  68. fcfg.Label = "Default Folder"
  69. fcfg.FilesystemType = config.FilesystemTypeBasic
  70. fcfg.Path = locations.Get(locations.DefFolder)
  71. newCfg.Folders = append(newCfg.Folders, fcfg)
  72. l.Infoln("Default folder created and/or linked to new config")
  73. return config.Wrap(path, newCfg, myID, evLogger), nil
  74. }
  75. // LoadConfigAtStartup loads an existing config. If it doesn't yet exist, it
  76. // creates a default one, without the default folder if noDefaultFolder is true.
  77. // Otherwise it checks the version, and archives and upgrades the config if
  78. // necessary or returns an error, if the version isn't compatible.
  79. func LoadConfigAtStartup(path string, cert tls.Certificate, evLogger events.Logger, allowNewerConfig, noDefaultFolder, skipPortProbing bool) (config.Wrapper, error) {
  80. myID := protocol.NewDeviceID(cert.Certificate[0])
  81. cfg, originalVersion, err := config.Load(path, myID, evLogger)
  82. if fs.IsNotExist(err) {
  83. cfg, err = DefaultConfig(path, myID, evLogger, noDefaultFolder, skipPortProbing)
  84. if err != nil {
  85. return nil, fmt.Errorf("failed to generate default config: %w", err)
  86. }
  87. err = cfg.Save()
  88. if err != nil {
  89. return nil, fmt.Errorf("failed to save default config: %w", err)
  90. }
  91. l.Infof("Default config saved. Edit %s to taste (with Syncthing stopped) or use the GUI", cfg.ConfigPath())
  92. } else if err == io.EOF {
  93. return nil, errors.New("failed to load config: unexpected end of file. Truncated or empty configuration?")
  94. } else if err != nil {
  95. return nil, fmt.Errorf("failed to load config: %w", err)
  96. }
  97. if originalVersion != config.CurrentVersion {
  98. if originalVersion == config.CurrentVersion+1101 {
  99. l.Infof("Now, THAT's what we call a config from the future! Don't worry. As long as you hit that wire with the connecting hook at precisely eighty-eight miles per hour the instant the lightning strikes the tower... everything will be fine.")
  100. }
  101. if originalVersion > config.CurrentVersion && !allowNewerConfig {
  102. return nil, fmt.Errorf("config file version (%d) is newer than supported version (%d). If this is expected, use --allow-newer-config to override.", originalVersion, config.CurrentVersion)
  103. }
  104. err = archiveAndSaveConfig(cfg, originalVersion)
  105. if err != nil {
  106. return nil, fmt.Errorf("config archive: %w", err)
  107. }
  108. }
  109. return cfg, nil
  110. }
  111. func archiveAndSaveConfig(cfg config.Wrapper, originalVersion int) error {
  112. // Copy the existing config to an archive copy
  113. archivePath := cfg.ConfigPath() + fmt.Sprintf(".v%d", originalVersion)
  114. l.Infoln("Archiving a copy of old config file format at:", archivePath)
  115. if err := copyFile(cfg.ConfigPath(), archivePath); err != nil {
  116. return err
  117. }
  118. // Do a regular atomic config sve
  119. return cfg.Save()
  120. }
  121. func copyFile(src, dst string) error {
  122. bs, err := os.ReadFile(src)
  123. if err != nil {
  124. return err
  125. }
  126. if err := os.WriteFile(dst, bs, 0o600); err != nil {
  127. // Attempt to clean up
  128. os.Remove(dst)
  129. return err
  130. }
  131. return nil
  132. }
  133. func OpenDBBackend(path string, tuning config.Tuning) (backend.Backend, error) {
  134. return backend.Open(path, backend.Tuning(tuning))
  135. }