2016-03-29 12:29:50 +00:00
|
|
|
#include "command.hh"
|
|
|
|
#include "shared.hh"
|
|
|
|
#include "store-api.hh"
|
|
|
|
#include "sync.hh"
|
|
|
|
#include "thread-pool.hh"
|
2018-03-29 22:56:13 +00:00
|
|
|
#include "references.hh"
|
2016-03-29 12:29:50 +00:00
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
2016-04-14 19:14:29 +00:00
|
|
|
struct CmdVerify : StorePathsCommand
|
2016-03-29 12:29:50 +00:00
|
|
|
{
|
|
|
|
bool noContents = false;
|
2016-04-07 13:14:12 +00:00
|
|
|
bool noTrust = false;
|
2016-03-30 09:18:54 +00:00
|
|
|
Strings substituterUris;
|
2019-10-10 13:03:01 +00:00
|
|
|
size_t sigsNeeded = 0;
|
2016-03-29 12:29:50 +00:00
|
|
|
|
2016-04-14 19:14:29 +00:00
|
|
|
CmdVerify()
|
2016-03-29 12:29:50 +00:00
|
|
|
{
|
2021-01-27 11:06:03 +00:00
|
|
|
addFlag({
|
|
|
|
.longName = "no-contents",
|
|
|
|
.description = "Do not verify the contents of each store path.",
|
|
|
|
.handler = {&noContents, true},
|
|
|
|
});
|
|
|
|
|
|
|
|
addFlag({
|
|
|
|
.longName = "no-trust",
|
|
|
|
.description = "Do not verify whether each store path is trusted.",
|
|
|
|
.handler = {&noTrust, true},
|
|
|
|
});
|
2021-01-08 09:44:55 +00:00
|
|
|
|
2020-05-04 20:40:19 +00:00
|
|
|
addFlag({
|
|
|
|
.longName = "substituter",
|
|
|
|
.shortName = 's',
|
2021-01-13 13:18:04 +00:00
|
|
|
.description = "Use signatures from the specified store.",
|
2020-05-04 20:40:19 +00:00
|
|
|
.labels = {"store-uri"},
|
|
|
|
.handler = {[&](std::string s) { substituterUris.push_back(s); }}
|
|
|
|
});
|
2021-01-08 09:44:55 +00:00
|
|
|
|
|
|
|
addFlag({
|
|
|
|
.longName = "sigs-needed",
|
|
|
|
.shortName = 'n',
|
2021-01-13 13:18:04 +00:00
|
|
|
.description = "Require that each path has at least *n* valid signatures.",
|
2021-01-08 09:44:55 +00:00
|
|
|
.labels = {"n"},
|
|
|
|
.handler = {&sigsNeeded}
|
|
|
|
});
|
2016-03-29 12:29:50 +00:00
|
|
|
}
|
|
|
|
|
2016-04-14 19:14:29 +00:00
|
|
|
std::string description() override
|
|
|
|
{
|
|
|
|
return "verify the integrity of store paths";
|
|
|
|
}
|
|
|
|
|
2020-12-09 22:45:06 +00:00
|
|
|
std::string doc() override
|
2016-04-21 12:58:32 +00:00
|
|
|
{
|
2020-12-09 22:45:06 +00:00
|
|
|
return
|
|
|
|
#include "verify.md"
|
|
|
|
;
|
2016-04-21 12:58:32 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
void run(ref<Store> store, StorePaths storePaths) override
|
2016-03-29 12:29:50 +00:00
|
|
|
{
|
2016-03-30 09:18:54 +00:00
|
|
|
std::vector<ref<Store>> substituters;
|
|
|
|
for (auto & s : substituterUris)
|
2016-09-02 10:35:48 +00:00
|
|
|
substituters.push_back(openStore(s));
|
2016-03-30 09:18:54 +00:00
|
|
|
|
2016-03-29 12:29:50 +00:00
|
|
|
auto publicKeys = getDefaultPublicKeys();
|
|
|
|
|
2020-06-18 22:09:22 +00:00
|
|
|
Activity act(*logger, actVerifyPaths);
|
2017-08-16 17:23:46 +00:00
|
|
|
|
2016-04-25 13:26:07 +00:00
|
|
|
std::atomic<size_t> done{0};
|
2016-03-29 12:29:50 +00:00
|
|
|
std::atomic<size_t> untrusted{0};
|
|
|
|
std::atomic<size_t> corrupted{0};
|
|
|
|
std::atomic<size_t> failed{0};
|
2017-08-16 17:23:46 +00:00
|
|
|
std::atomic<size_t> active{0};
|
2016-03-29 12:29:50 +00:00
|
|
|
|
2017-08-16 17:23:46 +00:00
|
|
|
auto update = [&]() {
|
|
|
|
act.progress(done, storePaths.size(), active, failed);
|
|
|
|
};
|
2016-03-29 12:29:50 +00:00
|
|
|
|
|
|
|
ThreadPool pool;
|
|
|
|
|
|
|
|
auto doPath = [&](const Path & storePath) {
|
|
|
|
try {
|
2016-03-30 09:18:54 +00:00
|
|
|
checkInterrupt();
|
|
|
|
|
2017-08-16 17:23:46 +00:00
|
|
|
MaintainCount<std::atomic<size_t>> mcActive(active);
|
|
|
|
update();
|
2016-03-29 12:29:50 +00:00
|
|
|
|
2019-12-05 18:11:09 +00:00
|
|
|
auto info = store->queryPathInfo(store->parseStorePath(storePath));
|
2016-03-29 12:29:50 +00:00
|
|
|
|
2020-07-13 18:12:44 +00:00
|
|
|
// Note: info->path can be different from storePath
|
|
|
|
// for binary cache stores when using --all (since we
|
|
|
|
// can't enumerate names efficiently).
|
|
|
|
Activity act2(*logger, lvlInfo, actUnknown, fmt("checking '%s'", store->printStorePath(info->path)));
|
|
|
|
|
2016-03-29 12:29:50 +00:00
|
|
|
if (!noContents) {
|
|
|
|
|
2021-05-26 07:39:29 +00:00
|
|
|
auto hashSink = HashSink(info->narHash.type);
|
2016-03-29 12:29:50 +00:00
|
|
|
|
2021-05-26 07:39:29 +00:00
|
|
|
store->narFromPath(info->path, hashSink);
|
2018-03-29 22:56:13 +00:00
|
|
|
|
2021-05-26 07:39:29 +00:00
|
|
|
auto hash = hashSink.finish();
|
2016-03-29 12:29:50 +00:00
|
|
|
|
2020-08-05 18:42:48 +00:00
|
|
|
if (hash.first != info->narHash) {
|
2017-08-16 17:23:46 +00:00
|
|
|
corrupted++;
|
2020-06-18 22:09:22 +00:00
|
|
|
act2.result(resCorruptedPath, store->printStorePath(info->path));
|
2021-01-20 23:27:36 +00:00
|
|
|
printError("path '%s' was modified! expected hash '%s', got '%s'",
|
|
|
|
store->printStorePath(info->path),
|
|
|
|
info->narHash.to_string(Base32, true),
|
|
|
|
hash.first.to_string(Base32, true));
|
2016-03-29 12:29:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-07 13:14:12 +00:00
|
|
|
if (!noTrust) {
|
2016-03-29 12:29:50 +00:00
|
|
|
|
2016-03-30 09:18:54 +00:00
|
|
|
bool good = false;
|
|
|
|
|
2016-04-19 16:50:15 +00:00
|
|
|
if (info->ultimate && !sigsNeeded)
|
2016-03-30 09:18:54 +00:00
|
|
|
good = true;
|
|
|
|
|
2016-04-07 13:14:12 +00:00
|
|
|
else {
|
|
|
|
|
|
|
|
StringSet sigsSeen;
|
2019-10-10 13:03:01 +00:00
|
|
|
size_t actualSigsNeeded = std::max(sigsNeeded, (size_t) 1);
|
2016-04-07 13:14:12 +00:00
|
|
|
size_t validSigs = 0;
|
|
|
|
|
|
|
|
auto doSigs = [&](StringSet sigs) {
|
|
|
|
for (auto sig : sigs) {
|
2019-10-09 13:51:52 +00:00
|
|
|
if (!sigsSeen.insert(sig).second) continue;
|
2019-12-05 18:11:09 +00:00
|
|
|
if (validSigs < ValidPathInfo::maxSigs && info->checkSignature(*store, publicKeys, sig))
|
2016-04-07 13:14:12 +00:00
|
|
|
validSigs++;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-03 11:17:11 +00:00
|
|
|
if (info->isContentAddressed(*store)) validSigs = ValidPathInfo::maxSigs;
|
|
|
|
|
2016-04-19 16:50:15 +00:00
|
|
|
doSigs(info->sigs);
|
2016-03-30 09:18:54 +00:00
|
|
|
|
|
|
|
for (auto & store2 : substituters) {
|
2016-04-07 13:14:12 +00:00
|
|
|
if (validSigs >= actualSigsNeeded) break;
|
|
|
|
try {
|
2016-08-03 11:17:11 +00:00
|
|
|
auto info2 = store2->queryPathInfo(info->path);
|
|
|
|
if (info2->isContentAddressed(*store)) validSigs = ValidPathInfo::maxSigs;
|
|
|
|
doSigs(info2->sigs);
|
2016-04-19 16:50:15 +00:00
|
|
|
} catch (InvalidPath &) {
|
2016-04-07 13:14:12 +00:00
|
|
|
} catch (Error & e) {
|
2020-04-30 22:31:47 +00:00
|
|
|
logError(e.info());
|
2016-03-30 09:18:54 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-07 13:14:12 +00:00
|
|
|
|
|
|
|
if (validSigs >= actualSigsNeeded)
|
|
|
|
good = true;
|
2016-03-30 09:18:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!good) {
|
2016-03-29 12:29:50 +00:00
|
|
|
untrusted++;
|
2020-06-18 22:09:22 +00:00
|
|
|
act2.result(resUntrustedPath, store->printStorePath(info->path));
|
2021-01-20 23:27:36 +00:00
|
|
|
printError("path '%s' is untrusted", store->printStorePath(info->path));
|
2016-03-29 12:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
done++;
|
|
|
|
|
|
|
|
} catch (Error & e) {
|
2020-04-30 22:31:47 +00:00
|
|
|
logError(e.info());
|
2016-03-29 12:29:50 +00:00
|
|
|
failed++;
|
|
|
|
}
|
2017-08-16 17:23:46 +00:00
|
|
|
|
|
|
|
update();
|
2016-03-29 12:29:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (auto & storePath : storePaths)
|
2019-12-05 18:11:09 +00:00
|
|
|
pool.enqueue(std::bind(doPath, store->printStorePath(storePath)));
|
2016-03-29 12:29:50 +00:00
|
|
|
|
|
|
|
pool.process();
|
|
|
|
|
|
|
|
throw Exit(
|
|
|
|
(corrupted ? 1 : 0) |
|
|
|
|
(untrusted ? 2 : 0) |
|
|
|
|
(failed ? 4 : 0));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-24 18:38:56 +00:00
|
|
|
static auto rCmdVerify = registerCommand2<CmdVerify>({"store", "verify"});
|