|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package resources |
| 17 | + |
| 18 | +import ( |
| 19 | + "net/url" |
| 20 | + "path" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/arduino/arduino-cli/arduino" |
| 24 | + "github.com/arduino/arduino-cli/arduino/httpclient" |
| 25 | + "github.com/arduino/arduino-cli/arduino/security" |
| 26 | + rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" |
| 27 | + "github.com/arduino/go-paths-helper" |
| 28 | + "go.bug.st/downloader/v2" |
| 29 | +) |
| 30 | + |
| 31 | +// IndexResource is a reference to an index file URL with an optional signature. |
| 32 | +type IndexResource struct { |
| 33 | + URL *url.URL |
| 34 | + SignatureURL *url.URL |
| 35 | +} |
| 36 | + |
| 37 | +// Download will download the index and possibly check the signature using the Arduino's public key. |
| 38 | +// If the file is in .gz format it will be unpacked first. |
| 39 | +func (res *IndexResource) Download(destDir *paths.Path, downloadCB rpc.DownloadProgressCB) error { |
| 40 | + // Create destination directory |
| 41 | + if err := destDir.MkdirAll(); err != nil { |
| 42 | + return &arduino.PermissionDeniedError{Message: tr("Can't create data directory %s", destDir), Cause: err} |
| 43 | + } |
| 44 | + |
| 45 | + // Create a temp dir to stage all downloads |
| 46 | + tmp, err := paths.MkTempDir("", "library_index_download") |
| 47 | + if err != nil { |
| 48 | + return &arduino.TempDirCreationFailedError{Cause: err} |
| 49 | + } |
| 50 | + defer tmp.RemoveAll() |
| 51 | + |
| 52 | + // Download index file |
| 53 | + indexFileName := path.Base(res.URL.Path) // == package_index.json[.gz] |
| 54 | + tmpIndexPath := tmp.Join(indexFileName) |
| 55 | + if err := httpclient.DownloadFile(tmpIndexPath, res.URL.String(), tr("Downloading index: %s", indexFileName), downloadCB, nil, downloader.NoResume); err != nil { |
| 56 | + return &arduino.FailedDownloadError{Message: tr("Error downloading index '%s'", res.URL), Cause: err} |
| 57 | + } |
| 58 | + |
| 59 | + // Expand the index if it is compressed |
| 60 | + if strings.HasSuffix(indexFileName, ".gz") { |
| 61 | + indexFileName = strings.TrimSuffix(indexFileName, ".gz") // == package_index.json |
| 62 | + tmpUnzippedIndexPath := tmp.Join(indexFileName) |
| 63 | + if err := paths.GUnzip(tmpIndexPath, tmpUnzippedIndexPath); err != nil { |
| 64 | + return &arduino.PermissionDeniedError{Message: tr("Error extracting %s", indexFileName), Cause: err} |
| 65 | + } |
| 66 | + tmpIndexPath = tmpUnzippedIndexPath |
| 67 | + } |
| 68 | + |
| 69 | + // Check the signature if needed |
| 70 | + var signaturePath, tmpSignaturePath *paths.Path |
| 71 | + if res.SignatureURL != nil { |
| 72 | + // Compose signature URL |
| 73 | + signatureFileName := path.Base(res.SignatureURL.Path) |
| 74 | + |
| 75 | + // Download signature |
| 76 | + signaturePath = destDir.Join(signatureFileName) |
| 77 | + tmpSignaturePath = tmp.Join(signatureFileName) |
| 78 | + if err := httpclient.DownloadFile(tmpSignaturePath, res.SignatureURL.String(), tr("Downloading index signature: %s", signatureFileName), downloadCB, nil, downloader.NoResume); err != nil { |
| 79 | + return &arduino.FailedDownloadError{Message: tr("Error downloading index signature '%s'", res.SignatureURL), Cause: err} |
| 80 | + } |
| 81 | + |
| 82 | + // Check signature... |
| 83 | + if valid, _, err := security.VerifyArduinoDetachedSignature(tmpIndexPath, tmpSignaturePath); err != nil { |
| 84 | + return &arduino.PermissionDeniedError{Message: tr("Error verifying signature"), Cause: err} |
| 85 | + } else if !valid { |
| 86 | + return &arduino.SignatureVerificationFailedError{File: res.URL.String()} |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // TODO: Implement a ResourceValidator |
| 91 | + // if !validate(tmpIndexPath) { return error } |
| 92 | + |
| 93 | + // Make a backup copy of old index and signature so the defer function can rollback in case of errors. |
| 94 | + indexPath := destDir.Join(indexFileName) |
| 95 | + oldIndex := tmp.Join("old_index") |
| 96 | + if indexPath.Exist() { |
| 97 | + if err := indexPath.CopyTo(oldIndex); err != nil { |
| 98 | + return &arduino.PermissionDeniedError{Message: tr("Error saving downloaded index"), Cause: err} |
| 99 | + } |
| 100 | + defer oldIndex.CopyTo(indexPath) // will silently fail in case of success |
| 101 | + } |
| 102 | + oldSignature := tmp.Join("old_signature") |
| 103 | + if oldSignature.Exist() { |
| 104 | + if err := signaturePath.CopyTo(oldSignature); err != nil { |
| 105 | + return &arduino.PermissionDeniedError{Message: tr("Error saving downloaded index signature"), Cause: err} |
| 106 | + } |
| 107 | + defer oldSignature.CopyTo(signaturePath) // will silently fail in case of success |
| 108 | + } |
| 109 | + if err := tmpIndexPath.CopyTo(indexPath); err != nil { |
| 110 | + return &arduino.PermissionDeniedError{Message: tr("Error saving downloaded index"), Cause: err} |
| 111 | + } |
| 112 | + if res.SignatureURL != nil { |
| 113 | + if err := tmpSignaturePath.CopyTo(signaturePath); err != nil { |
| 114 | + return &arduino.PermissionDeniedError{Message: tr("Error saving downloaded index signature"), Cause: err} |
| 115 | + } |
| 116 | + } |
| 117 | + oldIndex.Remove() |
| 118 | + oldSignature.Remove() |
| 119 | + return nil |
| 120 | +} |
0 commit comments