2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
2014-06-01 20:50:14 +00:00
|
|
|
|
2014-12-22 10:03:17 +00:00
|
|
|
// +build !noupgrade
|
2014-06-16 07:52:14 +00:00
|
|
|
|
2014-07-31 14:01:23 +00:00
|
|
|
package upgrade
|
2014-05-02 08:01:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
2014-12-22 10:03:17 +00:00
|
|
|
"archive/zip"
|
|
|
|
"bytes"
|
2014-05-02 08:01:09 +00:00
|
|
|
"compress/gzip"
|
2015-08-21 08:13:31 +00:00
|
|
|
"crypto/tls"
|
2014-05-02 08:01:09 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2014-05-02 15:04:45 +00:00
|
|
|
"io/ioutil"
|
2014-05-02 08:01:09 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2014-12-22 10:03:17 +00:00
|
|
|
"runtime"
|
2015-03-28 09:21:00 +00:00
|
|
|
"sort"
|
2014-05-02 08:01:09 +00:00
|
|
|
"strings"
|
2015-08-21 08:13:31 +00:00
|
|
|
|
2015-10-13 18:52:22 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/dialer"
|
2015-08-21 08:13:31 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/signature"
|
2014-05-02 08:01:09 +00:00
|
|
|
)
|
|
|
|
|
2015-09-06 14:36:09 +00:00
|
|
|
const DisabledByCompilation = false
|
|
|
|
|
2015-08-21 08:13:31 +00:00
|
|
|
// This is an HTTP/HTTPS client that does *not* perform certificate
|
|
|
|
// validation. We do this because some systems where Syncthing runs have
|
|
|
|
// issues with old or missing CA roots. It doesn't actually matter that we
|
|
|
|
// load the upgrade insecurely as we verify an ECDSA signature of the actual
|
|
|
|
// binary contents before accepting the upgrade.
|
|
|
|
var insecureHTTP = &http.Client{
|
|
|
|
Transport: &http.Transport{
|
2015-10-13 18:52:22 +00:00
|
|
|
Dial: dialer.Dial,
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
2015-08-21 08:13:31 +00:00
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-11-23 23:23:41 +00:00
|
|
|
// FetchLatestReleases returns the latest releases, including prereleases or
|
2015-04-28 20:32:10 +00:00
|
|
|
// not depending on the argument
|
2015-11-24 00:37:42 +00:00
|
|
|
func FetchLatestReleases(releasesURL, version string) []Release {
|
2015-09-10 12:16:44 +00:00
|
|
|
resp, err := insecureHTTP.Get(releasesURL)
|
2014-07-14 08:45:29 +00:00
|
|
|
if err != nil {
|
2015-11-23 23:41:50 +00:00
|
|
|
l.Infoln("Couldn't fetch release information:", err)
|
|
|
|
return nil
|
2014-07-14 08:45:29 +00:00
|
|
|
}
|
2014-07-31 08:26:45 +00:00
|
|
|
if resp.StatusCode > 299 {
|
2016-01-20 02:32:33 +00:00
|
|
|
l.Infoln("API call returned HTTP error:", resp.Status)
|
2015-11-23 23:41:50 +00:00
|
|
|
return nil
|
2014-07-31 08:26:45 +00:00
|
|
|
}
|
2014-07-14 08:45:29 +00:00
|
|
|
|
2014-07-31 14:01:23 +00:00
|
|
|
var rels []Release
|
2014-07-14 08:45:29 +00:00
|
|
|
json.NewDecoder(resp.Body).Decode(&rels)
|
|
|
|
resp.Body.Close()
|
|
|
|
|
2015-11-23 23:41:50 +00:00
|
|
|
return rels
|
2015-03-28 09:21:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SortByRelease []Release
|
|
|
|
|
|
|
|
func (s SortByRelease) Len() int {
|
|
|
|
return len(s)
|
|
|
|
}
|
|
|
|
func (s SortByRelease) Swap(i, j int) {
|
|
|
|
s[i], s[j] = s[j], s[i]
|
|
|
|
}
|
|
|
|
func (s SortByRelease) Less(i, j int) bool {
|
|
|
|
return CompareVersions(s[i].Tag, s[j].Tag) > 0
|
|
|
|
}
|
|
|
|
|
2015-09-10 12:16:44 +00:00
|
|
|
func LatestRelease(releasesURL, version string) (Release, error) {
|
2015-11-23 23:41:50 +00:00
|
|
|
rels := FetchLatestReleases(releasesURL, version)
|
2015-04-09 20:44:36 +00:00
|
|
|
return SelectLatestRelease(version, rels)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SelectLatestRelease(version string, rels []Release) (Release, error) {
|
2014-07-31 07:04:57 +00:00
|
|
|
if len(rels) == 0 {
|
2015-11-24 00:37:42 +00:00
|
|
|
return Release{}, ErrNoVersionToSelect
|
2014-07-31 07:04:57 +00:00
|
|
|
}
|
|
|
|
|
2015-03-28 09:21:00 +00:00
|
|
|
sort.Sort(SortByRelease(rels))
|
|
|
|
// Check for a beta build
|
2015-10-22 06:28:35 +00:00
|
|
|
beta := strings.Contains(version, "-")
|
2014-12-08 15:36:15 +00:00
|
|
|
|
|
|
|
for _, rel := range rels {
|
2015-03-28 09:21:00 +00:00
|
|
|
if rel.Prerelease && !beta {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, asset := range rel.Assets {
|
|
|
|
assetName := path.Base(asset.Name)
|
|
|
|
// Check for the architecture
|
|
|
|
expectedRelease := releaseName(rel.Tag)
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("expected release asset %q", expectedRelease)
|
|
|
|
l.Debugln("considering release", assetName)
|
2015-03-28 09:21:00 +00:00
|
|
|
if strings.HasPrefix(assetName, expectedRelease) {
|
|
|
|
return rel, nil
|
|
|
|
}
|
2014-07-24 10:55:41 +00:00
|
|
|
}
|
2014-07-14 08:45:29 +00:00
|
|
|
}
|
2015-11-24 00:37:42 +00:00
|
|
|
return Release{}, ErrNoReleaseDownload
|
2014-05-02 08:01:09 +00:00
|
|
|
}
|
|
|
|
|
2014-12-22 10:03:17 +00:00
|
|
|
// Upgrade to the given release, saving the previous binary with a ".old" extension.
|
|
|
|
func upgradeTo(binary string, rel Release) error {
|
|
|
|
expectedRelease := releaseName(rel.Tag)
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("expected release asset %q", expectedRelease)
|
2014-12-22 10:03:17 +00:00
|
|
|
for _, asset := range rel.Assets {
|
|
|
|
assetName := path.Base(asset.Name)
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln("considering release", assetName)
|
2014-12-22 10:03:17 +00:00
|
|
|
|
|
|
|
if strings.HasPrefix(assetName, expectedRelease) {
|
2016-05-06 13:30:35 +00:00
|
|
|
return upgradeToURL(assetName, binary, asset.URL)
|
2014-12-22 10:03:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-24 00:37:42 +00:00
|
|
|
return ErrNoReleaseDownload
|
2014-12-22 10:03:17 +00:00
|
|
|
}
|
|
|
|
|
2014-12-22 11:07:04 +00:00
|
|
|
// Upgrade to the given release, saving the previous binary with a ".old" extension.
|
2016-05-06 13:30:35 +00:00
|
|
|
func upgradeToURL(archiveName, binary string, url string) error {
|
|
|
|
fname, err := readRelease(archiveName, filepath.Dir(binary), url)
|
2014-12-22 11:07:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
old := binary + ".old"
|
2015-01-14 20:33:12 +00:00
|
|
|
os.Remove(old)
|
2014-12-22 11:07:04 +00:00
|
|
|
err = os.Rename(binary, old)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = os.Rename(fname, binary)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-06 13:30:35 +00:00
|
|
|
func readRelease(archiveName, dir, url string) (string, error) {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("loading %q", url)
|
2014-08-07 13:57:20 +00:00
|
|
|
|
2014-05-02 08:01:09 +00:00
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("Accept", "application/octet-stream")
|
2015-08-21 08:13:31 +00:00
|
|
|
resp, err := insecureHTTP.Do(req)
|
2014-05-02 08:01:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2014-12-22 10:03:17 +00:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "windows":
|
2016-05-06 13:30:35 +00:00
|
|
|
return readZip(archiveName, dir, resp.Body)
|
2014-12-22 10:03:17 +00:00
|
|
|
default:
|
2016-05-06 13:30:35 +00:00
|
|
|
return readTarGz(archiveName, dir, resp.Body)
|
2014-12-22 10:03:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-06 13:30:35 +00:00
|
|
|
func readTarGz(archiveName, dir string, r io.Reader) (string, error) {
|
2014-12-22 10:03:17 +00:00
|
|
|
gr, err := gzip.NewReader(r)
|
2014-05-02 08:01:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
tr := tar.NewReader(gr)
|
|
|
|
|
2015-08-21 08:13:31 +00:00
|
|
|
var tempName string
|
|
|
|
var sig []byte
|
2014-12-22 10:03:17 +00:00
|
|
|
|
2014-05-02 08:01:09 +00:00
|
|
|
// Iterate through the files in the archive.
|
|
|
|
for {
|
|
|
|
hdr, err := tr.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
// end of tar archive
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-12-22 10:03:17 +00:00
|
|
|
|
2015-11-09 22:32:26 +00:00
|
|
|
err = archiveFileVisitor(dir, &tempName, &sig, hdr.Name, tr)
|
2015-08-21 08:13:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-12-22 10:03:17 +00:00
|
|
|
|
2015-08-21 08:13:31 +00:00
|
|
|
if tempName != "" && sig != nil {
|
|
|
|
break
|
2014-12-22 10:03:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-06 13:30:35 +00:00
|
|
|
if err := verifyUpgrade(archiveName, tempName, sig); err != nil {
|
2015-08-21 08:13:31 +00:00
|
|
|
return "", err
|
2014-12-22 10:03:17 +00:00
|
|
|
}
|
2015-08-21 08:13:31 +00:00
|
|
|
|
|
|
|
return tempName, nil
|
2014-12-22 10:03:17 +00:00
|
|
|
}
|
|
|
|
|
2016-05-06 13:30:35 +00:00
|
|
|
func readZip(archiveName, dir string, r io.Reader) (string, error) {
|
2014-12-22 10:03:17 +00:00
|
|
|
body, err := ioutil.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
archive, err := zip.NewReader(bytes.NewReader(body), int64(len(body)))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-08-21 08:13:31 +00:00
|
|
|
var tempName string
|
|
|
|
var sig []byte
|
2014-12-22 10:03:17 +00:00
|
|
|
|
|
|
|
// Iterate through the files in the archive.
|
|
|
|
for _, file := range archive.File {
|
2015-08-21 08:13:31 +00:00
|
|
|
inFile, err := file.Open()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-12-22 10:03:17 +00:00
|
|
|
|
2015-11-09 22:32:26 +00:00
|
|
|
err = archiveFileVisitor(dir, &tempName, &sig, file.Name, inFile)
|
2015-08-21 08:13:31 +00:00
|
|
|
inFile.Close()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-12-22 10:03:17 +00:00
|
|
|
|
2015-08-21 08:13:31 +00:00
|
|
|
if tempName != "" && sig != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2014-05-02 08:01:09 +00:00
|
|
|
|
2016-05-06 13:30:35 +00:00
|
|
|
if err := verifyUpgrade(archiveName, tempName, sig); err != nil {
|
2015-08-21 08:13:31 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2014-12-22 09:42:47 +00:00
|
|
|
|
2015-08-21 08:13:31 +00:00
|
|
|
return tempName, nil
|
|
|
|
}
|
2014-12-22 10:03:17 +00:00
|
|
|
|
2015-08-21 08:13:31 +00:00
|
|
|
// archiveFileVisitor is called for each file in an archive. It may set
|
|
|
|
// tempFile and signature.
|
2015-11-09 22:32:26 +00:00
|
|
|
func archiveFileVisitor(dir string, tempFile *string, signature *[]byte, archivePath string, filedata io.Reader) error {
|
2015-08-21 08:13:31 +00:00
|
|
|
var err error
|
2015-11-09 22:32:26 +00:00
|
|
|
filename := path.Base(archivePath)
|
|
|
|
archiveDir := path.Dir(archivePath)
|
|
|
|
l.Debugf("considering file %s", archivePath)
|
2015-08-21 08:13:31 +00:00
|
|
|
switch filename {
|
|
|
|
case "syncthing", "syncthing.exe":
|
2016-05-06 13:30:35 +00:00
|
|
|
archiveDirs := strings.Split(archiveDir, "/")
|
|
|
|
if len(archiveDirs) > 1 {
|
|
|
|
// Don't consider "syncthing" files found too deeply, as they may be
|
|
|
|
// other things.
|
|
|
|
return nil
|
|
|
|
}
|
2015-11-09 22:32:26 +00:00
|
|
|
l.Debugf("found upgrade binary %s", archivePath)
|
2015-08-21 08:13:31 +00:00
|
|
|
*tempFile, err = writeBinary(dir, filedata)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-12-22 10:03:17 +00:00
|
|
|
}
|
|
|
|
|
2016-05-06 13:30:35 +00:00
|
|
|
case "release.sig":
|
2015-11-09 22:32:26 +00:00
|
|
|
l.Debugf("found signature %s", archivePath)
|
2015-08-21 08:13:31 +00:00
|
|
|
*signature, err = ioutil.ReadAll(filedata)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-05-02 08:01:09 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-21 08:13:31 +00:00
|
|
|
|
|
|
|
return nil
|
2014-05-02 08:01:09 +00:00
|
|
|
}
|
2014-12-22 10:03:17 +00:00
|
|
|
|
2016-05-06 13:30:35 +00:00
|
|
|
func verifyUpgrade(archiveName, tempName string, sig []byte) error {
|
2015-08-21 08:13:31 +00:00
|
|
|
if tempName == "" {
|
|
|
|
return fmt.Errorf("no upgrade found")
|
|
|
|
}
|
|
|
|
if sig == nil {
|
|
|
|
return fmt.Errorf("no signature found")
|
|
|
|
}
|
|
|
|
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("checking signature\n%s", sig)
|
2015-08-21 08:13:31 +00:00
|
|
|
|
|
|
|
fd, err := os.Open(tempName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-05-06 13:30:35 +00:00
|
|
|
|
|
|
|
// Create a new reader that will serve reads from, in order:
|
|
|
|
//
|
|
|
|
// - the archive name ("syncthing-linux-amd64-v0.13.0-beta.4.tar.gz")
|
|
|
|
// followed by a newline
|
|
|
|
//
|
|
|
|
// - the temp file contents
|
|
|
|
//
|
|
|
|
// We then verify the release signature against the contents of this
|
|
|
|
// multireader. This ensures that it is not only a bonafide syncthing
|
|
|
|
// binary, but it it also of exactly the platform and version we expect.
|
|
|
|
|
|
|
|
mr := io.MultiReader(bytes.NewBufferString(archiveName+"\n"), fd)
|
|
|
|
err = signature.Verify(SigningKey, sig, mr)
|
2015-08-21 08:13:31 +00:00
|
|
|
fd.Close()
|
|
|
|
|
2014-12-22 10:03:17 +00:00
|
|
|
if err != nil {
|
2015-08-21 08:13:31 +00:00
|
|
|
os.Remove(tempName)
|
|
|
|
return err
|
2014-12-22 10:03:17 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 08:13:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeBinary(dir string, inFile io.Reader) (filename string, err error) {
|
|
|
|
// Write the binary to a temporary file.
|
2014-12-22 10:03:17 +00:00
|
|
|
|
2015-08-21 08:13:31 +00:00
|
|
|
outFile, err := ioutil.TempFile(dir, "syncthing")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-12-22 10:03:17 +00:00
|
|
|
|
2015-08-21 08:13:31 +00:00
|
|
|
_, err = io.Copy(outFile, inFile)
|
2014-12-22 10:03:17 +00:00
|
|
|
if err != nil {
|
|
|
|
os.Remove(outFile.Name())
|
2015-08-21 08:13:31 +00:00
|
|
|
return "", err
|
2014-12-22 10:03:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = outFile.Close()
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(outFile.Name())
|
2015-08-21 08:13:31 +00:00
|
|
|
return "", err
|
2014-12-22 10:03:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Chmod(outFile.Name(), os.FileMode(0755))
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(outFile.Name())
|
2015-08-21 08:13:31 +00:00
|
|
|
return "", err
|
2014-12-22 10:03:17 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 08:13:31 +00:00
|
|
|
return outFile.Name(), nil
|
2014-12-22 10:03:17 +00:00
|
|
|
}
|