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"
|
|
|
|
|
|
|
|
|
|
#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;
|
2016-04-07 13:14:12 +00:00
|
|
|
|
size_t sigsNeeded;
|
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
|
|
|
|
{
|
|
|
|
|
mkFlag(0, "no-contents", "do not verify the contents of each store path", &noContents);
|
2016-04-07 13:14:12 +00:00
|
|
|
|
mkFlag(0, "no-trust", "do not verify whether each store path is trusted", &noTrust);
|
2016-03-30 09:18:54 +00:00
|
|
|
|
mkFlag('s', "substituter", {"store-uri"}, "use signatures from specified store", 1,
|
|
|
|
|
[&](Strings ss) { substituterUris.push_back(ss.front()); });
|
2016-04-07 13:14:12 +00:00
|
|
|
|
mkIntFlag('n', "sigs-needed", "require that each path has at least N valid signatures", &sigsNeeded);
|
2016-03-29 12:29:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-14 19:14:29 +00:00
|
|
|
|
std::string name() override
|
|
|
|
|
{
|
|
|
|
|
return "verify";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string description() override
|
|
|
|
|
{
|
|
|
|
|
return "verify the integrity of store paths";
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-21 12:58:32 +00:00
|
|
|
|
Examples examples() override
|
|
|
|
|
{
|
|
|
|
|
return {
|
|
|
|
|
Example{
|
|
|
|
|
"To verify the entire Nix store:",
|
|
|
|
|
"nix verify --all"
|
|
|
|
|
},
|
|
|
|
|
Example{
|
|
|
|
|
"To check whether each path in the closure of Firefox has at least 2 signatures:",
|
|
|
|
|
"nix verify -r -n2 --no-contents $(type -p firefox)"
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-14 19:14:29 +00:00
|
|
|
|
void run(ref<Store> store, Paths 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)
|
|
|
|
|
substituters.push_back(openStoreAt(s));
|
|
|
|
|
|
2016-03-29 12:29:50 +00:00
|
|
|
|
auto publicKeys = getDefaultPublicKeys();
|
|
|
|
|
|
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};
|
|
|
|
|
|
2016-04-25 13:26:07 +00:00
|
|
|
|
std::string doneLabel("paths checked");
|
|
|
|
|
std::string untrustedLabel("untrusted");
|
|
|
|
|
std::string corruptedLabel("corrupted");
|
|
|
|
|
std::string failedLabel("failed");
|
|
|
|
|
logger->setExpected(doneLabel, storePaths.size());
|
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();
|
|
|
|
|
|
2016-04-25 13:26:07 +00:00
|
|
|
|
Activity act(*logger, lvlInfo, format("checking ‘%s’") % storePath);
|
2016-03-29 12:29:50 +00:00
|
|
|
|
|
|
|
|
|
auto info = store->queryPathInfo(storePath);
|
|
|
|
|
|
|
|
|
|
if (!noContents) {
|
|
|
|
|
|
2016-04-19 16:50:15 +00:00
|
|
|
|
HashSink sink(info->narHash.type);
|
2016-04-21 15:53:47 +00:00
|
|
|
|
store->narFromPath(info->path, sink);
|
2016-03-29 12:29:50 +00:00
|
|
|
|
|
|
|
|
|
auto hash = sink.finish();
|
|
|
|
|
|
2016-04-19 16:50:15 +00:00
|
|
|
|
if (hash.first != info->narHash) {
|
2016-04-25 13:26:07 +00:00
|
|
|
|
logger->incProgress(corruptedLabel);
|
2016-03-29 12:29:50 +00:00
|
|
|
|
corrupted = 1;
|
|
|
|
|
printMsg(lvlError,
|
|
|
|
|
format("path ‘%s’ was modified! expected hash ‘%s’, got ‘%s’")
|
2016-04-21 15:53:47 +00:00
|
|
|
|
% info->path % printHash(info->narHash) % printHash(hash.first));
|
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;
|
|
|
|
|
size_t actualSigsNeeded = sigsNeeded ? sigsNeeded : 1;
|
|
|
|
|
size_t validSigs = 0;
|
|
|
|
|
|
|
|
|
|
auto doSigs = [&](StringSet sigs) {
|
|
|
|
|
for (auto sig : sigs) {
|
|
|
|
|
if (sigsSeen.count(sig)) continue;
|
|
|
|
|
sigsSeen.insert(sig);
|
2016-04-19 16:50:15 +00:00
|
|
|
|
if (info->checkSignature(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) {
|
|
|
|
|
printMsg(lvlError, format(ANSI_RED "error:" ANSI_NORMAL " %s") % e.what());
|
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-04-25 13:26:07 +00:00
|
|
|
|
logger->incProgress(untrustedLabel);
|
2016-03-29 12:29:50 +00:00
|
|
|
|
untrusted++;
|
2016-04-21 15:53:47 +00:00
|
|
|
|
printMsg(lvlError, format("path ‘%s’ is untrusted") % info->path);
|
2016-03-29 12:29:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-25 13:26:07 +00:00
|
|
|
|
logger->incProgress(doneLabel);
|
2016-03-29 12:29:50 +00:00
|
|
|
|
done++;
|
|
|
|
|
|
|
|
|
|
} catch (Error & e) {
|
|
|
|
|
printMsg(lvlError, format(ANSI_RED "error:" ANSI_NORMAL " %s") % e.what());
|
2016-04-25 13:26:07 +00:00
|
|
|
|
logger->incProgress(failedLabel);
|
2016-03-29 12:29:50 +00:00
|
|
|
|
failed++;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (auto & storePath : storePaths)
|
|
|
|
|
pool.enqueue(std::bind(doPath, storePath));
|
|
|
|
|
|
|
|
|
|
pool.process();
|
|
|
|
|
|
2016-04-25 13:26:07 +00:00
|
|
|
|
printMsg(lvlInfo, format("%d paths checked, %d untrusted, %d corrupted, %d failed")
|
|
|
|
|
% done % untrusted % corrupted % failed);
|
2016-03-29 12:29:50 +00:00
|
|
|
|
|
|
|
|
|
throw Exit(
|
|
|
|
|
(corrupted ? 1 : 0) |
|
|
|
|
|
(untrusted ? 2 : 0) |
|
|
|
|
|
(failed ? 4 : 0));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-04-14 19:14:29 +00:00
|
|
|
|
static RegisterCommand r1(make_ref<CmdVerify>());
|