utils.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. "fmt"
  10. "io"
  11. "io/ioutil"
  12. "os"
  13. "github.com/pkg/errors"
  14. "github.com/syncthing/syncthing/lib/config"
  15. "github.com/syncthing/syncthing/lib/db"
  16. "github.com/syncthing/syncthing/lib/events"
  17. "github.com/syncthing/syncthing/lib/fs"
  18. "github.com/syncthing/syncthing/lib/locations"
  19. "github.com/syncthing/syncthing/lib/protocol"
  20. "github.com/syncthing/syncthing/lib/tlsutil"
  21. )
  22. func LoadOrGenerateCertificate(certFile, keyFile string) (tls.Certificate, error) {
  23. cert, err := tls.LoadX509KeyPair(
  24. locations.Get(locations.CertFile),
  25. locations.Get(locations.KeyFile),
  26. )
  27. if err != nil {
  28. l.Infof("Generating ECDSA key and certificate for %s...", tlsDefaultCommonName)
  29. return tlsutil.NewCertificate(
  30. locations.Get(locations.CertFile),
  31. locations.Get(locations.KeyFile),
  32. tlsDefaultCommonName,
  33. deviceCertLifetimeDays,
  34. )
  35. }
  36. return cert, nil
  37. }
  38. func DefaultConfig(path string, myID protocol.DeviceID, evLogger events.Logger, noDefaultFolder bool) (config.Wrapper, error) {
  39. newCfg, err := config.NewWithFreePorts(myID)
  40. if err != nil {
  41. return nil, err
  42. }
  43. if noDefaultFolder {
  44. l.Infoln("We will skip creation of a default folder on first start")
  45. return config.Wrap(path, newCfg, evLogger), nil
  46. }
  47. newCfg.Folders = append(newCfg.Folders, config.NewFolderConfiguration(myID, "default", "Default Folder", fs.FilesystemTypeBasic, locations.Get(locations.DefFolder)))
  48. l.Infoln("Default folder created and/or linked to new config")
  49. return config.Wrap(path, newCfg, evLogger), nil
  50. }
  51. // LoadConfigAtStartup loads an existing config. If it doesn't yet exist, it
  52. // creates a default one, without the default folder if noDefaultFolder is ture.
  53. // Otherwise it checks the version, and archives and upgrades the config if
  54. // necessary or returns an error, if the version isn't compatible.
  55. func LoadConfigAtStartup(path string, cert tls.Certificate, evLogger events.Logger, allowNewerConfig, noDefaultFolder bool) (config.Wrapper, error) {
  56. myID := protocol.NewDeviceID(cert.Certificate[0])
  57. cfg, err := config.Load(path, myID, evLogger)
  58. if fs.IsNotExist(err) {
  59. cfg, err = DefaultConfig(path, myID, evLogger, noDefaultFolder)
  60. if err != nil {
  61. return nil, errors.Wrap(err, "failed to generate default config")
  62. }
  63. err = cfg.Save()
  64. if err != nil {
  65. return nil, errors.Wrap(err, "failed to save default config")
  66. }
  67. l.Infof("Default config saved. Edit %s to taste (with Syncthing stopped) or use the GUI", cfg.ConfigPath())
  68. } else if err == io.EOF {
  69. return nil, errors.New("failed to load config: unexpected end of file. Truncated or empty configuration?")
  70. } else if err != nil {
  71. return nil, errors.Wrap(err, "failed to load config")
  72. }
  73. if cfg.RawCopy().OriginalVersion != config.CurrentVersion {
  74. if cfg.RawCopy().OriginalVersion == config.CurrentVersion+1101 {
  75. 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.")
  76. }
  77. if cfg.RawCopy().OriginalVersion > config.CurrentVersion && !allowNewerConfig {
  78. return nil, fmt.Errorf("config file version (%d) is newer than supported version (%d). If this is expected, use -allow-newer-config to override.", cfg.RawCopy().OriginalVersion, config.CurrentVersion)
  79. }
  80. err = archiveAndSaveConfig(cfg)
  81. if err != nil {
  82. return nil, errors.Wrap(err, "config archive")
  83. }
  84. }
  85. return cfg, nil
  86. }
  87. func archiveAndSaveConfig(cfg config.Wrapper) error {
  88. // Copy the existing config to an archive copy
  89. archivePath := cfg.ConfigPath() + fmt.Sprintf(".v%d", cfg.RawCopy().OriginalVersion)
  90. l.Infoln("Archiving a copy of old config file format at:", archivePath)
  91. if err := copyFile(cfg.ConfigPath(), archivePath); err != nil {
  92. return err
  93. }
  94. // Do a regular atomic config sve
  95. return cfg.Save()
  96. }
  97. func copyFile(src, dst string) error {
  98. bs, err := ioutil.ReadFile(src)
  99. if err != nil {
  100. return err
  101. }
  102. if err := ioutil.WriteFile(dst, bs, 0600); err != nil {
  103. // Attempt to clean up
  104. os.Remove(dst)
  105. return err
  106. }
  107. return nil
  108. }
  109. func OpenGoleveldb(path string, tuning config.Tuning) (*db.Lowlevel, error) {
  110. return db.Open(path, db.Tuning(tuning))
  111. }