-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathupdater.cpp
680 lines (525 loc) · 22.5 KB
/
updater.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
// system headers
#include <deque>
#include <iostream>
#include <libgen.h>
#include <memory>
#include <mutex>
#include <string>
#include <thread>
#include <unistd.h>
// library headers
#include <zsclient.h>
#include <cpr/cpr.h>
#include <ftw.h>
// local headers
#include "appimage/update.h"
#include "updateinformation/updateinformation.h"
#include "appimage.h"
#include "util.h"
// convenience declaration
namespace {
typedef std::lock_guard<std::mutex> lock_guard;
}
namespace appimage::update {
using namespace util;
class Updater::Private {
public:
explicit Private(const std::string& pathToAppImage) : state(INITIALIZED),
appImage(pathToAppImage),
zSyncClient(nullptr),
thread(nullptr),
mutex(),
overwrite(false),
rawUpdateInformation(appImage.readRawUpdateInformation())
{};
public:
AppImage appImage;
// we call this "raw" update information to highlight the difference between strings and the new
// UpdateInformation infrastructure
std::string rawUpdateInformation;
// state
State state;
// ZSync client -- will be instantiated only if necessary
std::shared_ptr<zsync2::ZSyncClient> zSyncClient;
// threading
std::thread* thread;
std::mutex mutex;
// status messages
std::deque<std::string> statusMessages;
// defines whether to overwrite original file
bool overwrite;
public:
void issueStatusMessage(const std::string& message) {
statusMessages.push_back(message);
}
StatusMessageCallback makeIssueStatusMessageCallback() {
return [this](const std::string& message) {issueStatusMessage(message);};
}
void validateAppImage() {
// first check whether there's update information at all
// note that we skip this check when custom update information is set intentionally
if (this->rawUpdateInformation.empty()) {
const auto rawUpdateInformationFromAppImage = appImage.readRawUpdateInformation();
if (rawUpdateInformationFromAppImage.empty()) {
std::ostringstream oss;
oss << "Could not find update information in the AppImage. "
<< "Please contact the author of the AppImage and ask them to embed update information.";
throw AppImageError(oss.str());
}
}
const auto updateInformationPtr = makeUpdateInformation(rawUpdateInformation);
const auto zsyncUrl = updateInformationPtr->buildUrl(makeIssueStatusMessageCallback());
// now check whether a ZSync URL could be composed by readAppImage
// this is the only supported update type at the moment
if (zsyncUrl.empty()) {
std::ostringstream oss;
oss << "ZSync URL not available. See previous messages for details.";
throw AppImageError(oss.str());
}
}
// thread runner
void runUpdate() {
// initialization
try {
lock_guard guard(mutex);
// make sure it runs only once at a time
// should never occur, but you never know
if (state != INITIALIZED)
return;
// if there is a ZSync client (e.g., because an update check has been run), clean it up
// this ensures that a fresh instance will be used for the update run
if (zSyncClient != nullptr) {
zSyncClient.reset();
}
validateAppImage();
const auto updateInformationPtr = makeUpdateInformation(rawUpdateInformation);
if (updateInformationPtr->type() == ZSYNC_GITHUB_RELEASES) {
issueStatusMessage("Updating from GitHub Releases via ZSync");
} else if (updateInformationPtr->type() == ZSYNC_GENERIC) {
issueStatusMessage("Updating from generic server via ZSync");
} else if (updateInformationPtr->type() == ZSYNC_PLING_V1) {
issueStatusMessage("Updating from Pling v1 server via ZSync");
} else {
throw AppImageError("Unknown update information type");
}
const auto zsyncUrl = updateInformationPtr->buildUrl(makeIssueStatusMessageCallback());
// doesn't matter which type it is exactly, they all work like the same
zSyncClient = std::make_shared<zsync2::ZSyncClient>(zsyncUrl, appImage.path(), overwrite);
// enable ranges optimizations
zSyncClient->setRangesOptimizationThreshold(64 * 4096);
// make sure the new AppImage goes into the same directory as the old one
// unfortunately, to be able to use dirname(), one has to copy the C string first
auto path = makeBuffer(appImage.path());
std::string dirPath = dirname(path.data());
zSyncClient->setCwd(dirPath);
state = RUNNING;
} catch (const AppImageError& e) {
issueStatusMessage("Error reading AppImage: " + std::string(e.what()));
state = ERROR;
return;
} catch (const UpdateInformationError& e) {
issueStatusMessage("Failed to parse update information: " + std::string(e.what()));
state = ERROR;
return;
}
// keep state -- by default, an error (false) is assumed
bool result = false;
// run phase
{
// check whether it's a zsync operation
if (zSyncClient != nullptr) {
result = zSyncClient->run();
}
}
// end phase
{
lock_guard guard(mutex);
if (result) {
state = SUCCESS;
} else {
state = ERROR;
}
}
}
bool checkForChanges(bool& updateAvailable, const unsigned int method = 0) {
lock_guard guard(mutex);
if (state != INITIALIZED)
return false;
// validate AppImage
try {
validateAppImage();
} catch (const AppImageError& e) {
issueStatusMessage(e.what());
return false;
}
try {
auto updateInformationPtr = makeUpdateInformation(rawUpdateInformation);
const auto zsyncUrl = updateInformationPtr->buildUrl(makeIssueStatusMessageCallback());
zSyncClient.reset(new zsync2::ZSyncClient(zsyncUrl, appImage.path()));
return zSyncClient->checkForChanges(updateAvailable, method);
} catch (const UpdateInformationError& e) {
zSyncClient.reset();
// return error in case of unknown update information
issueStatusMessage(e.what());
issueStatusMessage("Unknown update information type, aborting.");
return false;
}
}
};
Updater::Updater(const std::string& pathToAppImage, bool overwrite) : d(new Updater::Private(ailfsRealpath(pathToAppImage))) {
// workaround for AppImageLauncher filesystem
d->overwrite = overwrite;
// check whether file exists, otherwise throw exception
std::ifstream f(d->appImage.path());
if(!f || !f.good()) {
auto errorMessage = std::strerror(errno);
throw std::invalid_argument(errorMessage + std::string(": ") + d->appImage.path());
}
}
Updater::~Updater() = default;
void Updater::runUpdate() {
// alias for private function
return d->runUpdate();
}
bool Updater::start() {
// lock mutex
lock_guard guard(d->mutex);
// prevent multiple start calls
if(d->state != INITIALIZED)
return false;
// if there's a thread managed by this class already, should not start another one and lose access to
// this one
if(d->thread)
return false;
// create thread
d->thread = new std::thread(&Updater::runUpdate, this);
return true;
}
bool Updater::isDone() {
lock_guard guard(d->mutex);
return d->state != INITIALIZED && d->state != RUNNING && d->state != STOPPING;
}
bool Updater::hasError() {
lock_guard guard(d->mutex);
return d->state == ERROR;
}
bool Updater::progress(double& progress) {
lock_guard guard(d->mutex);
if (d->state == INITIALIZED) {
// this protects update checks from returning progress, which would only occur when using method 0
progress = 0;
return true;
} else if (d->state == SUCCESS || d->state == ERROR) {
progress = 1;
return true;
}
if (d->zSyncClient != nullptr) {
progress = d->zSyncClient->progress();
return true;
}
return false;
}
bool Updater::stop() {
throw std::runtime_error("not implemented");
}
bool Updater::nextStatusMessage(std::string& message) {
// first, check own message queue
if (!d->statusMessages.empty()) {
message = d->statusMessages.front();
d->statusMessages.pop_front();
return true;
}
// next, check zsync client for a message
if (d->zSyncClient != nullptr) {
std::string zsyncMessage;
if (!d->zSyncClient->nextStatusMessage(zsyncMessage))
return false;
// show that the message is coming from zsync2
message = "zsync2: " + zsyncMessage;
return true;
}
return false;
}
Updater::State Updater::state() {
return d->state;
}
bool Updater::checkForChanges(bool &updateAvailable, const unsigned int method) {
return d->checkForChanges(updateAvailable, method);
}
bool Updater::describeAppImage(std::string& description) const {
std::ostringstream oss;
bool success = true;
try {
oss << "Parsing file: " << d->appImage.path() << std::endl;
oss << "AppImage type: " << d->appImage.appImageType() << std::endl;
const auto rawUpdateInformation = d->appImage.readRawUpdateInformation();
oss << "Raw update information: ";
if (rawUpdateInformation.empty())
oss << "<empty>";
else
oss << rawUpdateInformation;
oss << std::endl;
auto updateInformation = makeUpdateInformation(rawUpdateInformation);
oss << "Update information type: ";
if (updateInformation->type() == ZSYNC_GENERIC)
oss << "Generic ZSync URL";
else if (updateInformation->type() == ZSYNC_GITHUB_RELEASES)
oss << "ZSync via GitHub Releases";
else if (updateInformation->type() == ZSYNC_PLING_V1)
oss << "ZSync via OCS";
else
throw std::runtime_error("unsupported update information type");
oss << std::endl;
try {
auto url = updateInformation->buildUrl(d->makeIssueStatusMessageCallback());
oss << "Assembled ZSync URL: " << url << std::endl;
} catch (const UpdateInformationError& e) {
oss << "Failed to assemble ZSync URL. AppImageUpdate can not be used with this AppImage. "
<< "See below for more information"
<< std::endl
<< e.what();
}
} catch (const UpdateInformationError& e) {
oss << e.what();
success = false;
}
description = oss.str();
return success;
}
bool Updater::pathToNewFile(std::string& path) const {
// only available update method is via ZSync
if (d->zSyncClient)
return d->zSyncClient->pathToNewFile(path);
return false;
}
bool Updater::remoteFileSize(off_t& fileSize) const {
// only available update method is via ZSync
if (d->zSyncClient != nullptr)
return d->zSyncClient->remoteFileSize(fileSize);
return false;
}
Updater::ValidationState Updater::validateSignature() {
std::string pathToNewAppImage;
if (!this->pathToNewFile(pathToNewAppImage)) {
// return generic error
return VALIDATION_FAILED;
}
AppImage newAppImage(pathToNewAppImage);
auto pathToOldAppImage = abspath(d->appImage.path());
if (pathToOldAppImage == pathToNewAppImage) {
pathToOldAppImage = pathToNewAppImage + ".zs-old";
}
AppImage oldAppImage(pathToOldAppImage);
auto oldSignature = oldAppImage.readSignature();
auto newSignature = newAppImage.readSignature();
// remove any spaces and/or newline characters there might be on the left or right of the payload
zsync2::trim(oldSignature, '\n');
zsync2::trim(newSignature, '\n');
zsync2::trim(oldSignature);
zsync2::trim(newSignature);
auto oldSigned = !oldSignature.empty();
auto newSigned = !newSignature.empty();
auto oldDigest = oldAppImage.calculateHash();
auto newDigest = newAppImage.calculateHash();
std::string tempDir;
{
auto buffer = makeBuffer("/tmp/AppImageUpdate-XXXXXX");
if (mkdtemp(buffer.data()) == nullptr) {
const int error = errno;
const auto* errorMessage = strerror(error);
d->issueStatusMessage("Failed to create temporary directory: " + std::string(errorMessage));
return VALIDATION_TEMPDIR_CREATION_FAILED;
}
tempDir = buffer.data();
}
auto tempFile = [&tempDir](const std::string& filename, const std::string& contents) {
std::stringstream path;
path << tempDir << "/" << filename;
auto x = path.str();
std::ofstream ofs(path.str());
ofs.write(contents.c_str(), contents.size());
return path.str();
};
if (!oldSigned && !newSigned)
return VALIDATION_NOT_SIGNED;
else if (oldSigned && !newSigned)
return VALIDATION_NO_LONGER_SIGNED;
// store digests and signatures in files so they can be passed to gpg
auto oldDigestFilename = tempFile("old-digest", oldDigest);
auto newDigestFilename = tempFile("new-digest", newDigest);
auto oldSignatureFilename = tempFile("old-signature", oldSignature);
auto newSignatureFilename = tempFile("new-signature", newSignature);
auto cleanup = [&tempDir]() {
// cleanup
auto unlinkCb = [](const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
int rv = remove(fpath);
if (rv)
perror(fpath);
return rv;
};
nftw(tempDir.c_str(), unlinkCb, 64, FTW_DEPTH | FTW_PHYS);
rmdir(tempDir.c_str());
};
// find gpg binary
auto gpgPath = findInPATH("gpg2");
if (gpgPath.empty()) {
gpgPath = findInPATH("gpg");
if (gpgPath.empty()) {
cleanup();
return VALIDATION_GPG_MISSING;
}
}
const auto tempKeyRingPath = tempDir + "/keyring";
// create keyring file, otherwise GPG will complain
std::ofstream ofs(tempKeyRingPath);
ofs.close();
auto importKeyFromAppImage = [&tempKeyRingPath, &gpgPath](const std::string& path) {
auto key = readElfSection(path, ".sig_key");
if (key.empty())
return false;
std::ostringstream oss;
oss << "'" << gpgPath << "' "
<< "--no-default-keyring --keyring '" << tempKeyRingPath << "' --import 2>/dev/null";
auto command = oss.str();
auto proc = popen(oss.str().c_str(), "w");
fwrite(key.c_str(), key.size(), sizeof(char), proc);
auto retval = pclose(proc);
return retval == 0;
};
importKeyFromAppImage(pathToOldAppImage);
importKeyFromAppImage(pathToNewAppImage);
auto verifySignature = [this, &gpgPath, &tempKeyRingPath](
const std::string& signatureFile, const std::string& digestFile,
bool& keyFound, bool& goodSignature,
std::string& keyID, std::string& keyOwner
) {
std::ostringstream oss;
oss << "'" << gpgPath << "'"
<< " --keyring '" << tempKeyRingPath << "'"
<< " --verify '" << signatureFile << "' '" << digestFile << "' 2>&1";
auto command = oss.str();
// make sure output is in English
setenv("LANGUAGE", "C", 1);
setenv("LANG", "C", 1);
setenv("LC_ALL", "C", 1);
auto* proc = popen(command.c_str(), "r");
if (proc == nullptr)
return false;
char* currentLine = nullptr;
size_t lineSize = 0;
keyFound = true;
goodSignature = false;
while (getline(¤tLine, &lineSize, proc) != -1) {
std::string line = currentLine;
trim(line, '\n');
trim(line);
d->issueStatusMessage(gpgPath + std::string(": ") + line);
auto splitOwner = [&line]() {
auto parts = split(line, '"');
if (parts.size() < 2)
return std::string();
auto skip = parts[0].length();
return line.substr(skip + 1, line.length() - skip - 2);
};
if (stringStartsWith(line, "gpg: Signature made")) {
// extract key
auto parts = split(line);
if (*(parts.end() - 3) == "key" && *(parts.end() - 2) == "ID")
keyID = parts.back();
} else if (stringStartsWith(line, "gpg: Good signature from")) {
goodSignature = true;
keyOwner = splitOwner();
} else if (stringStartsWith(line, "gpg: BAD signature from")) {
goodSignature = false;
keyOwner = splitOwner();
} else if (stringStartsWith(line, "gpg: Can't check signature: No public key")) {
keyFound = false;
}
}
auto rv = pclose(proc);
return true;
};
bool oldKeyFound = false, newKeyFound = false, oldSignatureGood = false, newSignatureGood = false;
std::string oldKeyID, newKeyID, oldKeyOwner, newKeyOwner;
if (oldSigned) {
if (!verifySignature(oldSignatureFilename, oldDigestFilename, oldKeyFound, oldSignatureGood, oldKeyID, oldKeyOwner)) {
cleanup();
return VALIDATION_GPG_CALL_FAILED;
}
}
if (newSigned) {
if (!verifySignature(newSignatureFilename, newDigestFilename, newKeyFound, newSignatureGood, newKeyID, newKeyOwner)) {
cleanup();
return VALIDATION_GPG_CALL_FAILED;
}
}
if (!oldKeyFound || !newKeyFound) {
// if the keys haven't been embedded in the AppImages, we treat them as not signed
// see https://github.com/AppImage/AppImageUpdate/issues/16#issuecomment-370932698 for details
cleanup();
return VALIDATION_NOT_SIGNED;
}
if (!oldSignatureGood || !newSignatureGood) {
cleanup();
return VALIDATION_BAD_SIGNATURE;
}
if (oldSigned && newSigned) {
if (oldKeyID != newKeyID) {
cleanup();
return VALIDATION_KEY_CHANGED;
}
}
cleanup();
return VALIDATION_PASSED;
}
std::string Updater::signatureValidationMessage(const Updater::ValidationState& state) {
static const std::map<ValidationState, std::string> validationMessages = {
{VALIDATION_PASSED, "Signature validation successful"},
// warning states
{VALIDATION_WARNING, "Signature validation warning"},
{VALIDATION_NOT_SIGNED, "AppImage not signed"},
{VALIDATION_GPG_MISSING, "gpg2 command not found, please install"},
// error states
{VALIDATION_FAILED, "Signature validation failed"},
{VALIDATION_GPG_CALL_FAILED, "Call to gpg2 failed"},
{VALIDATION_TEMPDIR_CREATION_FAILED, "Failed to create temporary directory"},
{VALIDATION_NO_LONGER_SIGNED, "AppImage no longer comes with signature"},
{VALIDATION_BAD_SIGNATURE, "Bad signature"},
{VALIDATION_KEY_CHANGED, "Key changed for signing AppImages"},
};
if (validationMessages.count(state) > 0) {
return validationMessages.at(state);
}
return "Unknown validation state";
}
void Updater::restoreOriginalFile() {
std::string newFilePath;
if (!pathToNewFile(newFilePath)) {
throw std::runtime_error("Failed to get path to new file");
}
// make sure to compare absolute, resolved paths
newFilePath = abspath(newFilePath);
const auto& oldFilePath = abspath(d->appImage.path());
// restore original file
std::remove(newFilePath.c_str());
if (oldFilePath == newFilePath) {
std::rename((newFilePath + ".zs-old").c_str(), newFilePath.c_str());
}
}
void Updater::copyPermissionsToNewFile() {
std::string oldFilePath = abspath(d->appImage.path());
std::string newFilePath;
if (!pathToNewFile(newFilePath)) {
throw std::runtime_error("Failed to get path to new file");
}
// make sure to compare absolute, resolved paths
newFilePath = abspath(newFilePath);
appimage::update::copyPermissions(oldFilePath, newFilePath);
}
std::string Updater::updateInformation() const {
return d->rawUpdateInformation;
}
void Updater::setUpdateInformation(std::string newUpdateInformation) {
d->rawUpdateInformation = std::move(newUpdateInformation);
}
}