Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
packagemanagerinterface.cc
1 #include "packagemanagerinterface.h"
2 
3 #include "http/httpclient.h"
4 #include "logging/logging.h"
5 
7  DownloadMetaStruct(Uptane::Target target_in, FetcherProgressCb progress_cb_in, const api::FlowControlToken* token_in)
8  : hash_type{target_in.hashes()[0].type()},
9  target{std::move(target_in)},
10  token{token_in},
11  progress_cb{std::move(progress_cb_in)} {}
12  uint64_t downloaded_length{0};
13  unsigned int last_progress{0};
14  std::unique_ptr<StorageTargetWHandle> fhandle;
15  const Uptane::Hash::Type hash_type;
16  MultiPartHasher& hasher() {
17  switch (hash_type) {
18  case Uptane::Hash::Type::kSha256:
19  return sha256_hasher;
20  case Uptane::Hash::Type::kSha512:
21  return sha512_hasher;
22  default:
23  throw std::runtime_error("Unknown hash algorithm");
24  }
25  }
26  Uptane::Target target;
27  const api::FlowControlToken* token;
28  FetcherProgressCb progress_cb;
29 
30  private:
31  MultiPartSHA256Hasher sha256_hasher;
32  MultiPartSHA512Hasher sha512_hasher;
33 };
34 
35 static size_t DownloadHandler(char* contents, size_t size, size_t nmemb, void* userp) {
36  assert(userp);
37  auto ds = static_cast<DownloadMetaStruct*>(userp);
38  uint64_t downloaded = size * nmemb;
39  uint64_t expected = ds->target.length();
40  if ((ds->downloaded_length + downloaded) > expected) {
41  return downloaded + 1; // curl will abort if return unexpected size;
42  }
43 
44  // incomplete writes will stop the download (written_size != nmemb*size)
45  size_t written_size = ds->fhandle->wfeed(reinterpret_cast<uint8_t*>(contents), downloaded);
46  ds->hasher().update(reinterpret_cast<const unsigned char*>(contents), written_size);
47 
48  ds->downloaded_length += downloaded;
49  return written_size;
50 }
51 
52 static int ProgressHandler(void* clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) {
53  (void)dltotal;
54  (void)dlnow;
55  (void)ultotal;
56  (void)ulnow;
57  auto ds = static_cast<DownloadMetaStruct*>(clientp);
58 
59  uint64_t expected = ds->target.length();
60  auto progress = static_cast<unsigned int>((ds->downloaded_length * 100) / expected);
61  if (ds->progress_cb && progress > ds->last_progress) {
62  ds->last_progress = progress;
63  ds->progress_cb(ds->target, "Downloading", progress);
64  }
65  if (ds->token != nullptr && !ds->token->canContinue(false)) {
66  return 1;
67  }
68  return 0;
69 }
70 
71 static void restoreHasherState(MultiPartHasher& hasher, StorageTargetRHandle* data) {
72  size_t data_len;
73  size_t buf_len = 1024;
74  uint8_t buf[buf_len];
75  do {
76  data_len = data->rread(buf, buf_len);
77  hasher.update(buf, data_len);
78  } while (data_len != 0);
79 }
80 
81 bool PackageManagerInterface::fetchTarget(const Uptane::Target& target, Uptane::Fetcher& fetcher,
82  const KeyManager& keys, FetcherProgressCb progress_cb,
83  const api::FlowControlToken* token) {
84  (void)keys;
85  bool result = false;
86  try {
87  if (target.hashes().empty()) {
88  throw Uptane::Exception("image", "No hash defined for the target");
89  }
90  TargetStatus exists = PackageManagerInterface::verifyTarget(target);
91  if (exists == TargetStatus::kGood) {
92  LOG_INFO << "Image already downloaded; skipping download";
93  return true;
94  }
95  if (target.length() == 0) {
96  LOG_WARNING << "Skipping download of target with length 0";
97  return true;
98  }
99  std::unique_ptr<DownloadMetaStruct> ds = std_::make_unique<DownloadMetaStruct>(target, progress_cb, token);
100  if (exists == TargetStatus::kIncomplete) {
101  auto target_check = storage_->checkTargetFile(target);
102  ds->downloaded_length = target_check->first;
103  auto target_handle = storage_->openTargetFile(target);
104  ::restoreHasherState(ds->hasher(), target_handle.get());
105  target_handle->rclose();
106  ds->fhandle = target_handle->toWriteHandle();
107  } else {
108  // If the target was found, but is oversized or the hash doesn't match,
109  // just start over.
110  ds->fhandle = storage_->allocateTargetFile(false, target);
111  }
112 
113  const uint64_t required_bytes = target.length() - ds->downloaded_length;
114  if (!storage_->checkAvailableDiskSpace(required_bytes)) {
115  throw std::runtime_error("Insufficient disk space available to download target");
116  }
117 
118  std::string target_url = target.uri();
119  if (target_url.empty()) {
120  target_url = fetcher.getRepoServer() + "/targets/" + Utils::urlEncode(target.filename());
121  }
122 
123  HttpResponse response;
124  for (;;) {
125  response = http_->download(target_url, DownloadHandler, ProgressHandler, ds.get(),
126  static_cast<curl_off_t>(ds->downloaded_length));
127 
128  if (response.curl_code == CURLE_RANGE_ERROR) {
129  LOG_WARNING << "The image server doesn't support byte range requests,"
130  " try to download the image from the beginning: "
131  << target_url;
132  ds = std_::make_unique<DownloadMetaStruct>(target, progress_cb, token);
133  ds->fhandle = storage_->allocateTargetFile(false, target);
134  continue;
135  }
136 
137  if (!response.wasInterrupted()) {
138  break;
139  }
140  ds->fhandle.reset();
141  // sleep if paused or abort the download
142  if (!token->canContinue()) {
143  throw Uptane::Exception("image", "Download of a target was aborted");
144  }
145  ds->fhandle = storage_->openTargetFile(target)->toWriteHandle();
146  }
147  LOG_TRACE << "Download status: " << response.getStatusStr() << std::endl;
148  if (!response.isOk()) {
149  if (response.curl_code == CURLE_WRITE_ERROR) {
150  throw Uptane::OversizedTarget(target.filename());
151  }
152  throw Uptane::Exception("image", "Could not download file, error: " + response.error_message);
153  }
154  if (!target.MatchHash(Uptane::Hash(ds->hash_type, ds->hasher().getHexDigest()))) {
155  ds->fhandle->wabort();
156  throw Uptane::TargetHashMismatch(target.filename());
157  }
158  ds->fhandle->wcommit();
159  result = true;
160  } catch (const Uptane::Exception& e) {
161  LOG_WARNING << "Error while downloading a target: " << e.what();
162  }
163  return result;
164 }
165 
166 TargetStatus PackageManagerInterface::verifyTarget(const Uptane::Target& target) const {
167  auto target_exists = storage_->checkTargetFile(target);
168  if (!target_exists) {
169  return TargetStatus::kNotFound;
170  } else if (target_exists->first < target.length()) {
171  return TargetStatus::kIncomplete;
172  } else if (target_exists->first > target.length()) {
173  return TargetStatus::kOversized;
174  }
175 
176  // Even if the file exists and the length matches, recheck the hash.
177  DownloadMetaStruct ds(target, nullptr, nullptr);
178  ds.downloaded_length = target_exists->first;
179  auto target_handle = storage_->openTargetFile(target);
180  ::restoreHasherState(ds.hasher(), target_handle.get());
181  target_handle->rclose();
182  if (!target.MatchHash(Uptane::Hash(ds.hash_type, ds.hasher().getHexDigest()))) {
183  LOG_ERROR << "Target exists with expected length, but hash does not match metadata! " << target;
184  return TargetStatus::kHashMismatch;
185  }
186 
187  return TargetStatus::kGood;
188 }
Uptane::Fetcher
Definition: fetcher.h:33
KeyManager
Definition: keymanager.h:13
MultiPartSHA256Hasher
Definition: crypto.h:83
data
General data structures.
Definition: types.cc:44
HttpResponse
Definition: httpinterface.h:17
MultiPartSHA512Hasher
Definition: crypto.h:68
Uptane::Hash
The hash of a file or TUF metadata.
Definition: tuf.h:209
Uptane::OversizedTarget
Definition: exceptions.h:34
DownloadMetaStruct
Definition: packagemanagerinterface.cc:6
api::FlowControlToken::canContinue
bool canContinue(bool blocking=true) const
Called by the controlled thread to query the currently requested state.
Definition: apiqueue.cc:33
Uptane::Exception
Definition: exceptions.h:10
api::FlowControlToken
Provides a thread-safe way to pause and terminate task execution.
Definition: apiqueue.h:19
Uptane::TargetHashMismatch
Definition: exceptions.h:27
StorageTargetRHandle
Definition: invstorage.h:61
result
Results of libaktualizr API calls.
Definition: results.h:13
Uptane::Target
Definition: tuf.h:238
MultiPartHasher
Definition: crypto.h:61