4741 lines
202 KiB
C++
4741 lines
202 KiB
C++
/**
|
||
* @file main.cpp
|
||
* @brief vde_plugin_tool —— 微内核插件/清单/ABI 诊断 CLI(P0-6)
|
||
*
|
||
* 用法:
|
||
* vde_plugin_tool --list <dir> 扫描目录(递归 2 层)并列出发现的插件
|
||
* vde_plugin_tool --inspect <file> 解析并打印单个 vde-plugin.json
|
||
* vde_plugin_tool --abi 打印宿主插件 ABI / 内核版本 / 结构体尺寸
|
||
* vde_plugin_tool verify <dir> [--pubkey <path|inline>] [--key-id <id>] [--hash]
|
||
* 校验清单签名 / artifacts SHA-256 / key_id 与吊销
|
||
* vde_plugin_tool lint <dir> 校验目录下所有 vde-plugin.json(schema/字段/entry/capabilities/ABI)
|
||
* vde_plugin_tool --check <dir|manifest>
|
||
* 自检:清单必填字段/kind/ABI 兼容性/插件 query
|
||
* 一致性/artifacts SHA-256(不要求签名)
|
||
* vde_plugin_tool --build-index <plugins_dir> -o <index.json>
|
||
* [--sign <seed>] [--key-id <id>] [--publisher <name>]
|
||
* 扫描插件包生成仓库索引(artifact SHA-256;可选签名)
|
||
* vde_plugin_tool --verify-index <index.json> [--pubkey <key>] [--allow-unsigned]
|
||
* 校验索引 schema/签名/工件哈希/ABI 兼容性
|
||
* vde_plugin_tool --install <index.json|plugin_dir> --dest <dir>
|
||
* [--id <id>] [--version <x.y.z>] [--pubkey <key>]
|
||
* [--allow-unsigned] [--force]
|
||
* 校验后安装插件到 <dest>/<id>/
|
||
* vde_plugin_tool --gen-keypair [--out-dir <dir>]
|
||
* 生成 Ed25519 签名种子/公钥(测试/开发用)
|
||
* vde_plugin_tool scaffold <kind> <name> --out <dir>
|
||
* [--templates <dir>] [--force]
|
||
* 从 plugins/templates 生成可构建插件工程
|
||
* vde_plugin_tool --fetch-index <url|file> -o <index.json>
|
||
* [--pubkey <key>] [--allow-unsigned] [--verify-artifacts]
|
||
* 获取远程/本地仓库索引并校验 schema/签名/哈希
|
||
* vde_plugin_tool --help
|
||
*
|
||
* 退出码:0 成功;2 参数/用法错误;3 文件读取/清单解析/校验失败;4 加密校验不可用。
|
||
*/
|
||
#include "vde/plugin/plugin_manager.h"
|
||
#include "vde/plugin/plugin_security.h"
|
||
#include "vde/plugin/vde_plugin_api.h"
|
||
#include "vde/core/version.h"
|
||
#include "vde_platform/platform.h"
|
||
|
||
#include <nlohmann/json.hpp>
|
||
|
||
#if defined(_WIN32)
|
||
#ifndef WIN32_LEAN_AND_MEAN
|
||
#define WIN32_LEAN_AND_MEAN
|
||
#endif
|
||
#ifndef NOMINMAX
|
||
#define NOMINMAX
|
||
#endif
|
||
#include <windows.h>
|
||
#include <winhttp.h>
|
||
#endif
|
||
|
||
// P3-1: verify 命令的 Ed25519 实现由宿主(本工具)注入——内核 vde_plugin 不依赖
|
||
// vde_license;仅当 tools 构建时 license 模块可用才启用真实验签。
|
||
#if defined(VDE_TOOL_HAS_ED25519)
|
||
#include "vde_license/license_manager.h"
|
||
#endif
|
||
|
||
#include <algorithm>
|
||
#include <cstdint>
|
||
#include <cstdio>
|
||
#include <cstdlib>
|
||
#include <cstring>
|
||
#include <ctime>
|
||
#include <chrono>
|
||
#include "vde/fs_compat.h"
|
||
#include <map>
|
||
#include <functional>
|
||
#include <fstream>
|
||
#include <random>
|
||
#include <sstream>
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
namespace {
|
||
|
||
using vde::plugin::FormatPluginInfo;
|
||
using vde::plugin::PluginKind;
|
||
using vde::plugin::PluginManager;
|
||
using vde::plugin::PluginManifest;
|
||
using vde::plugin::SemanticVersion;
|
||
|
||
std::string version_text(const SemanticVersion& v) {
|
||
if (!v.valid) return "invalid";
|
||
char buf[64];
|
||
std::snprintf(buf, sizeof(buf), "%d.%d.%d", v.major, v.minor, v.patch);
|
||
return buf;
|
||
}
|
||
|
||
std::string join(const std::vector<std::string>& items, const char* sep) {
|
||
std::string out;
|
||
for (size_t i = 0; i < items.size(); ++i) {
|
||
if (i) out += sep;
|
||
out += items[i];
|
||
}
|
||
return out.empty() ? "-" : out;
|
||
}
|
||
|
||
void print_usage(const char* exe) {
|
||
std::printf(
|
||
"vde_plugin_tool (VDE microkernel P0-6)\n"
|
||
"\n"
|
||
"Usage:\n"
|
||
" %s --list <dir> list plugins recursively under an install root\n"
|
||
" %s package <plugin_dir> -o <file.vdeplugin>\n"
|
||
" deterministic archive (manifest+entry+data) + sha256 sidecar\n"
|
||
" %s upgrade <file.vdeplugin> --root <install_root> [--force] [--rollback]\n"
|
||
" verify + semver gate + atomic stage/backup/rename\n"
|
||
" %s unpack <file.vdeplugin> --dest <dir>\n"
|
||
" verify checksums and safely extract a .vdeplugin archive\n"
|
||
" %s verify-package <file.vdeplugin>\n"
|
||
" verify archive metadata/checksums (no extraction)\n"
|
||
" %s list <install_root>\n"
|
||
" list plugins: id/version/kind/abi/entry/status\n"
|
||
" %s uninstall <plugin-id> --root <install_root>\n"
|
||
" safe uninstall (manifest id match; alias --dest)\n"
|
||
" %s --inspect <file> parse and print one plugin manifest\n"
|
||
" %s verify <dir> [--pubkey <path|inline>] [--key-id <id>] [--hash]\n"
|
||
" verify manifest signature (ed25519:<base64>), artifacts\n"
|
||
" sha256 and key_id/revocation (spec: docs/design/\n"
|
||
" plugin-security.md); --hash prints artifact hashes\n"
|
||
" %s --check <dir|manifest>\n"
|
||
" self-check a plugin dir/manifest: required fields, kind/ABI\n"
|
||
" compatibility, vde_plugin_query consistency and artifacts\n"
|
||
" sha256 (no signature required; use verify for signatures)\n"
|
||
" %s lint <dir> lint every vde-plugin.json under <dir> (static schema/\n"
|
||
" required fields/kind/ABI/entry/capabilities checks;\n"
|
||
" exit 0 all PASS, 2 failures/usage)\n"
|
||
" %s --build-index <plugins_dir> -o <index.json> [--sign <seed>] [--key-id <id>]\n"
|
||
" [--publisher <name>]\n"
|
||
" scan plugin packages into a repository index (artifact\n"
|
||
" sha256; optional Ed25519 signature)\n"
|
||
" %s --verify-index <index.json> [--pubkey <path|inline>] [--allow-unsigned]\n"
|
||
" [--revoked-keys <file>] [--crl <file|url>]\n"
|
||
" [--crl-pubkey <path|inline>]\n"
|
||
" verify index schema/signature/artifact hashes/ABI compat;\n"
|
||
" reject a signature whose key_id is revoked (--revoked-keys\n"
|
||
" and/or signed CRL); CRL verified with --crl-pubkey (falls\n"
|
||
" back to --pubkey), expiry/signature checked\n"
|
||
" reject a signature whose key_id is in the revoked list\n"
|
||
" %s --install <index.json|url|plugin_dir> --dest <dir> [--id <id>]\n"
|
||
" [--version <x.y.z>] [--fetch-packages] [--verify-artifacts]\n"
|
||
" [--pubkey <path|inline>] [--allow-unsigned] [--force]\n"
|
||
" [--pubkey <path|inline>] [--allow-unsigned] [--force]\n"
|
||
" [--revoked-keys <file>] [--crl <file|url>]\n"
|
||
" [--crl-pubkey <path|inline>]\n"
|
||
" verify index/artifacts (SHA-256) then atomically install\n"
|
||
" the plugin package and its requires into <dest>/<id>/\n"
|
||
" %s --uninstall <plugin-id> --dest <dir>\n"
|
||
" safely remove an installed plugin directory\n"
|
||
" %s --upgrade <index.json|url> --id <plugin-id> --dest <dir> [--version <x.y.z>]\n"
|
||
" [--fetch-packages] [--pubkey <path|inline>] [--allow-unsigned]\n"
|
||
" [--force] [--revoked-keys <file>] [--crl <file|url>]\n"
|
||
" [--crl-pubkey <path|inline>]\n"
|
||
" download+verify new version, atomically replace,\n"
|
||
" rollback to old version on failure\n"
|
||
" %s --gen-keypair [--out-dir <dir>]\n"
|
||
" generate Ed25519 signing seed/public key files (test/dev)\n"
|
||
" %s --sign-crl <payload.json> -o <crl.json> --sign <seed>\n"
|
||
" sign a CRL payload (version/issued_at/expires_at/\n"
|
||
" revoked_key_ids) with Ed25519 canonical signature\n"
|
||
" %s scaffold <kind> <name> --out <dir> [--templates <dir>] [--force]\n"
|
||
" generate a buildable plugin project from plugins/templates\n"
|
||
" (kind: format|cae|cam|gcs; name = plugin id)\n"
|
||
" %s --fetch-index <url|file> -o <index.json> [--pubkey <key>]\n"
|
||
" [--allow-unsigned] [--verify-artifacts]\n"
|
||
" fetch a repository index (http(s)/file) and verify it\n"
|
||
" matrix <dir> [--json] ecosystem coverage matrix (kind counts/ABI range/\n"
|
||
" capabilities/duplicate ids/missing kinds; exit 0/2)\n"
|
||
" %s --abi print host plugin ABI / kernel version / struct sizes\n"
|
||
" %s --help show this help\n"
|
||
"\n"
|
||
"exit codes: 0 ok, 2 usage/bad public key, 3 validation failed (manifest/ABI/\n"
|
||
"query/artifacts/signature/CRL format/CRL expired/CRL signature), 4 unavailable\n"
|
||
" (signed index/verify/CRL without pubkey, CRL fetch failed, or tool built\n"
|
||
"without vde_license Ed25519 support).\n",
|
||
exe, exe, exe, exe, exe, exe, exe, exe, exe, exe, exe, exe, exe, exe, exe,
|
||
exe, exe, exe, exe, exe, exe, exe);
|
||
}
|
||
|
||
int cmd_abi() {
|
||
const vde::core::VersionInfo info = vde::core::vde_version();
|
||
std::printf("vde_plugin_tool (VDE microkernel plugin SDK diagnostics)\n");
|
||
std::printf(" kernel version : %s\n", info.version_string.c_str());
|
||
std::printf(" kernel build : %s, %s, %s\n", info.build_type.c_str(),
|
||
info.compiler_info.c_str(), info.cpp_standard.c_str());
|
||
std::printf(" plugin ABI : %d.%d (host major must equal plugin major;\n"
|
||
" plugin minor <= host minor)\n",
|
||
VDE_PLUGIN_ABI_MAJOR, VDE_PLUGIN_ABI_MINOR);
|
||
std::printf(" API version : %d.%d\n", VDE_API_VERSION_MAJOR,
|
||
VDE_API_VERSION_MINOR);
|
||
std::printf(" C struct sizes : VdePluginHost=%zu VdePluginInfo=%zu "
|
||
"VdePluginRegistration=%zu VdeFormatPlugin=%zu "
|
||
"VdeMesherPlugin=%zu VdeOptimizerPlugin=%zu VdeOptimizerOptions=%zu "
|
||
"VdeOptimizerResult=%zu VdeIoBatchPlugin=%zu VdeIoBatchOptions=%zu "
|
||
"VdeIoBatchResult=%zu VdeBodyInfo=%zu VdeMeshData=%zu\n",
|
||
sizeof(VdePluginHost), sizeof(VdePluginInfo),
|
||
sizeof(VdePluginRegistration), sizeof(VdeFormatPlugin),
|
||
sizeof(VdeMesherPlugin), sizeof(VdeOptimizerPlugin),
|
||
sizeof(VdeOptimizerOptions), sizeof(VdeOptimizerResult),
|
||
sizeof(VdeIoBatchPlugin), sizeof(VdeIoBatchOptions),
|
||
sizeof(VdeIoBatchResult), sizeof(VdeBodyInfo), sizeof(VdeMeshData));
|
||
std::printf(" extension points : format/generic; cae_solver/cam_strategy (ABI 1.2); "
|
||
"gcs_solver (1.3); mesher (1.4); optimizer/io_batch (1.5); "
|
||
"io_batch run_v2 (1.6); geom_op (1.7); ui/binding (1.8)\n");
|
||
std::printf(" plugin exports : %s / %s / %s\n",
|
||
VDE_PLUGIN_SYMBOL_QUERY, VDE_PLUGIN_SYMBOL_INIT,
|
||
VDE_PLUGIN_SYMBOL_SHUTDOWN);
|
||
std::printf(" manifest file : vde-plugin.json (id/version/kind/entry/abi/"
|
||
"capabilities)\n");
|
||
return 0;
|
||
}
|
||
|
||
int cmd_inspect(const std::string& path) {
|
||
std::ifstream in(path, std::ios::binary);
|
||
if (!in) {
|
||
std::fprintf(stderr, "inspect: cannot open '%s'\n", path.c_str());
|
||
return 3;
|
||
}
|
||
std::ostringstream ss;
|
||
ss << in.rdbuf();
|
||
const std::string text = ss.str();
|
||
PluginManifest manifest;
|
||
std::string error;
|
||
if (!vde::plugin::parse_plugin_manifest(text, path, &manifest, &error)) {
|
||
std::fprintf(stderr, "inspect: parse failed for '%s': %s\n", path.c_str(),
|
||
error.empty() ? "unknown error" : error.c_str());
|
||
return 3;
|
||
}
|
||
std::printf("plugin manifest: %s\n", path.c_str());
|
||
std::printf(" id : %s\n", manifest.id.c_str());
|
||
std::printf(" name : %s\n",
|
||
manifest.name.empty() ? "(none)" : manifest.name.c_str());
|
||
std::printf(" version : %s\n", version_text(manifest.version).c_str());
|
||
std::printf(" kind : %s (%u)\n",
|
||
vde::plugin::plugin_kind_name(manifest.kind),
|
||
static_cast<unsigned>(manifest.kind));
|
||
std::printf(" abi : %d.%d\n", manifest.abi_major, manifest.abi_minor);
|
||
std::printf(" entry : %s\n",
|
||
manifest.entry.empty() ? "(none)" : manifest.entry.c_str());
|
||
std::printf(" library : %s\n",
|
||
manifest.library_path.empty() ? "(not resolved)"
|
||
: manifest.library_path.c_str());
|
||
std::printf(" capabilities : %s\n", join(manifest.capabilities, "; ").c_str());
|
||
std::printf(" dependencies : %s\n", join(manifest.dependencies, "; ").c_str());
|
||
std::printf(" license : %s\n",
|
||
manifest.license.empty() ? "(none)" : manifest.license.c_str());
|
||
std::printf(" signature : %s\n",
|
||
manifest.signature.empty() ? "(none)" : "(present)");
|
||
return 0;
|
||
}
|
||
|
||
std::string read_file_text(const vde::fs::path& path) {
|
||
std::ifstream in(path.string(), std::ios::binary);
|
||
if (!in) return std::string();
|
||
std::ostringstream ss;
|
||
ss << in.rdbuf();
|
||
return ss.str();
|
||
}
|
||
|
||
// P4-37:lint <dir> —— 仅静态校验 manifest(不 dlopen 插件)
|
||
// JSON schema/必填字段/id/kind/abi/entry 文件存在/capabilities 非空/entry 平台名后缀/ABI 兼容。
|
||
// 输出逐清单 PASS/FAIL + 汇总;退出码 0(全部 PASS 且至少 1 个清单)/ 2(失败或用法错误)。
|
||
int cmd_lint(const std::string& dir) {
|
||
std::error_code ec;
|
||
if (!vde::fs::is_directory(dir, ec) || ec) {
|
||
std::fprintf(stderr, "lint: not a directory: '%s'\n", dir.c_str());
|
||
return 2;
|
||
}
|
||
PluginManager& pm = PluginManager::instance();
|
||
pm.clear();
|
||
std::string scan_error;
|
||
const std::size_t discovered = pm.scan_directory(dir, &scan_error);
|
||
std::printf("lint: scan '%s': %zu manifest(s) passed parse/discovery\n", dir.c_str(),
|
||
discovered);
|
||
int failed = 0;
|
||
std::size_t passed = 0;
|
||
const std::string platform_suffix =
|
||
#if defined(_WIN32)
|
||
".dll";
|
||
#elif defined(__APPLE__)
|
||
".dylib";
|
||
#else
|
||
".so";
|
||
#endif
|
||
for (const auto& manifest : pm.plugins()) {
|
||
std::string reason;
|
||
if (manifest.capabilities.empty()) {
|
||
reason = "capabilities is empty";
|
||
} else if (manifest.abi_major != VDE_PLUGIN_ABI_MAJOR ||
|
||
manifest.abi_minor > VDE_PLUGIN_ABI_MINOR) {
|
||
reason = "abi " + std::to_string(manifest.abi_major) + "." +
|
||
std::to_string(manifest.abi_minor) +
|
||
" incompatible with host " +
|
||
std::to_string(VDE_PLUGIN_ABI_MAJOR) + "." +
|
||
std::to_string(VDE_PLUGIN_ABI_MINOR);
|
||
} else {
|
||
const std::string extension =
|
||
vde::fs::path(manifest.entry).extension().string();
|
||
if (extension != platform_suffix) {
|
||
reason = "entry platform suffix mismatch: '" + manifest.entry +
|
||
"' (expected '" + platform_suffix + "')";
|
||
}
|
||
}
|
||
if (reason.empty()) {
|
||
++passed;
|
||
std::printf(" [PASS] %s (%s, ABI %d.%d) %s\n", manifest.id.c_str(),
|
||
vde::plugin::plugin_kind_name(manifest.kind), manifest.abi_major,
|
||
manifest.abi_minor, manifest.manifest_path.c_str());
|
||
} else {
|
||
++failed;
|
||
std::printf(" [FAIL] %s (%s): %s\n",
|
||
manifest.manifest_path.c_str(), manifest.id.c_str(),
|
||
reason.c_str());
|
||
}
|
||
}
|
||
// scan 诊断(坏 JSON/缺字段/缺 entry 库/重复 id 等)逐行计为 FAIL
|
||
std::size_t begin = 0;
|
||
while (begin <= scan_error.size()) {
|
||
const std::size_t end = scan_error.find('\n', begin);
|
||
const std::string line =
|
||
scan_error.substr(begin, end == std::string::npos ? std::string::npos
|
||
: end - begin);
|
||
if (!line.empty()) {
|
||
++failed;
|
||
std::printf(" [FAIL] %s\n", line.c_str());
|
||
}
|
||
if (end == std::string::npos) break;
|
||
begin = end + 1;
|
||
}
|
||
if (passed == 0 && failed == 0) {
|
||
std::printf("lint: no vde-plugin.json found under '%s'\n", dir.c_str());
|
||
return 2;
|
||
}
|
||
std::printf("lint: %zu passed, %d failed\n", passed, failed);
|
||
return failed == 0 ? 0 : 2;
|
||
}
|
||
int cmd_verify(const std::string& target, const std::string& pubkey_arg,
|
||
const std::string& expected_key_id, bool show_hashes) {
|
||
std::error_code ec;
|
||
vde::fs::path manifest_path(target);
|
||
if (vde::fs::is_directory(manifest_path, ec)) {
|
||
manifest_path /= "vde-plugin.json";
|
||
}
|
||
if (!vde::fs::is_regular_file(manifest_path, ec)) {
|
||
std::fprintf(stderr, "verify: manifest not found: '%s'\n",
|
||
manifest_path.string().c_str());
|
||
return 3;
|
||
}
|
||
const std::string manifest_json = read_file_text(manifest_path);
|
||
if (manifest_json.empty()) {
|
||
std::fprintf(stderr, "verify: cannot read manifest: '%s'\n",
|
||
manifest_path.string().c_str());
|
||
return 3;
|
||
}
|
||
|
||
// 清单基础信息(审查用;安全性由 plugin_security 评估)
|
||
PluginManifest manifest;
|
||
std::string parse_error;
|
||
const bool manifest_ok = vde::plugin::parse_plugin_manifest(
|
||
manifest_json, manifest_path.string(), &manifest, &parse_error);
|
||
std::printf("verify plugin manifest: %s\n", manifest_path.string().c_str());
|
||
if (manifest_ok) {
|
||
std::printf(" id : %s\n", manifest.id.c_str());
|
||
std::printf(" version : %s\n", version_text(manifest.version).c_str());
|
||
std::printf(" signature : %s\n",
|
||
manifest.signature.empty() ? "(none)" : "(present)");
|
||
} else {
|
||
std::fprintf(stderr, "verify: warning: manifest parse: %s\n",
|
||
parse_error.empty() ? "unknown error" : parse_error.c_str());
|
||
}
|
||
|
||
vde::plugin::PluginSecurityPolicy policy =
|
||
vde::plugin::strict_plugin_security_policy();
|
||
// verify 的职责是签名/发布者/工件校验;权限仅展示(宿主 grant 列表不在 CLI 上下文,
|
||
// 因此不在此处做默认拒绝判定;权限强制由宿主策略在加载时执行)。
|
||
policy.permissions_default_deny = false;
|
||
// P3-5:默认只校验"已声明"的 artifacts;--hash 显式要求全部工件哈希通过。
|
||
policy.require_artifact_hashes = show_hashes;
|
||
const vde::fs::path plugin_dir =
|
||
manifest_path.has_parent_path() ? manifest_path.parent_path()
|
||
: vde::fs::path(".");
|
||
vde::plugin::PluginSecurityReport report;
|
||
|
||
if (pubkey_arg.empty()) {
|
||
// 无公钥无法验签:仍给出结构/策略判定,但退出码 4 表示"加密校验不可用"
|
||
(void)vde::plugin::evaluate_plugin_security(manifest_json, plugin_dir.string(),
|
||
policy, nullptr, {}, &report);
|
||
std::fprintf(stderr,
|
||
"verify: no public key provided (use --pubkey <path|inline>); "
|
||
"cryptographic verification unavailable. status=%s (%s)\n",
|
||
vde::plugin::plugin_security_error_name(report.code),
|
||
report.message.c_str());
|
||
return 4;
|
||
}
|
||
|
||
std::string key_text;
|
||
const vde::fs::path key_path(pubkey_arg);
|
||
if (vde::fs::is_regular_file(key_path, ec)) {
|
||
key_text = read_file_text(key_path);
|
||
} else {
|
||
key_text = pubkey_arg; // 允许内联 "ed25519:<base64>" / base64 / hex
|
||
}
|
||
std::vector<uint8_t> public_key;
|
||
std::string key_error;
|
||
if (!vde::plugin::parse_public_key_text(key_text, &public_key, &key_error)) {
|
||
std::fprintf(stderr, "verify: invalid public key: %s\n", key_error.c_str());
|
||
return 2;
|
||
}
|
||
|
||
// P3-5:--hash 打印 artifacts 的期望/实际 SHA-256(含缺失/路径非法)。
|
||
if (show_hashes) {
|
||
std::vector<vde::plugin::PluginArtifact> artifacts;
|
||
std::string artifact_error;
|
||
bool declared = false;
|
||
if (!vde::plugin::parse_plugin_artifacts(manifest_json, &artifacts,
|
||
&artifact_error, &declared)) {
|
||
std::fprintf(stderr, "verify: artifacts schema error: %s\n",
|
||
artifact_error.c_str());
|
||
return 3;
|
||
}
|
||
std::printf(" artifacts : %s\n",
|
||
declared ? "(declared)" : "(none declared)");
|
||
for (const auto& artifact : artifacts) {
|
||
std::string path_error;
|
||
if (!vde::plugin::is_artifact_path_safe(artifact.path, &path_error)) {
|
||
std::printf(" %-32s INVALID (%s)\n", artifact.path.c_str(),
|
||
path_error.c_str());
|
||
continue;
|
||
}
|
||
std::string actual;
|
||
std::string file_error;
|
||
const auto full = plugin_dir / vde::fs::path(artifact.path);
|
||
if (!vde::plugin::sha256_file(full.string(), &actual, &file_error)) {
|
||
std::printf(" %-32s MISSING (%s)\n", artifact.path.c_str(),
|
||
file_error.c_str());
|
||
continue;
|
||
}
|
||
const bool match = actual == artifact.sha256_hex;
|
||
std::printf(" %-32s %s\n expected %s\n actual %s\n",
|
||
artifact.path.c_str(), match ? "OK" : "HASH_MISMATCH",
|
||
artifact.sha256_hex.c_str(), actual.c_str());
|
||
}
|
||
}
|
||
|
||
#if defined(VDE_TOOL_HAS_ED25519)
|
||
// 宿主注入:vde_license 的 Ed25519(RFC 8032)实现;内核库不依赖它。
|
||
class LicenseEd25519Verifier final : public vde::plugin::SignatureVerifier {
|
||
public:
|
||
bool verify(const uint8_t* payload, size_t payload_size,
|
||
const uint8_t* signature, size_t signature_size,
|
||
const uint8_t* public_key, size_t public_key_size) override {
|
||
if (payload == nullptr || signature == nullptr || public_key == nullptr) {
|
||
return false;
|
||
}
|
||
const std::vector<uint8_t> message(payload, payload + payload_size);
|
||
const std::vector<uint8_t> sig(signature, signature + signature_size);
|
||
const std::vector<uint8_t> key(public_key, public_key + public_key_size);
|
||
return vde::license::crypto::ed25519_verify(key, message, sig);
|
||
}
|
||
} verifier;
|
||
const bool ok = vde::plugin::evaluate_plugin_security(
|
||
manifest_json, plugin_dir.string(), policy, &verifier, public_key, &report);
|
||
#else
|
||
const bool ok = vde::plugin::evaluate_plugin_security(
|
||
manifest_json, plugin_dir.string(), policy, nullptr, public_key, &report);
|
||
#endif
|
||
|
||
std::printf(" publisher : %s\n",
|
||
report.publisher.empty() ? "(none)" : report.publisher.c_str());
|
||
std::printf(" signature : %s (%s, %zu bytes)\n",
|
||
report.signature_present ? "present" : "absent",
|
||
report.signature_algorithm.empty() ? "-"
|
||
: report.signature_algorithm.c_str(),
|
||
report.signature_size);
|
||
std::printf(" key_id : %s%s%s\n",
|
||
report.key_id.empty() ? "(none)" : report.key_id.c_str(),
|
||
report.key_trusted ? " (trusted)" : "",
|
||
report.key_revoked ? " (REVOKED)" : "");
|
||
std::printf(" permissions : requested=[%s] granted=[%s] denied=[%s]\n",
|
||
join(report.requested_permissions, ", ").c_str(),
|
||
join(report.granted_permissions, ", ").c_str(),
|
||
join(report.denied_permissions, ", ").c_str());
|
||
if (!show_hashes) {
|
||
std::printf(" artifacts : declared=%s required=%s checked=%zu\n",
|
||
report.artifacts_declared ? "yes" : "no",
|
||
report.artifacts_required ? "yes" : "no",
|
||
report.artifact_reports.size());
|
||
}
|
||
|
||
if (!expected_key_id.empty() && report.key_id != expected_key_id) {
|
||
std::fprintf(stderr,
|
||
"verify: key_id mismatch: manifest='%s' expected='%s'\n",
|
||
report.key_id.c_str(), expected_key_id.c_str());
|
||
return 3;
|
||
}
|
||
|
||
if (ok) {
|
||
std::printf("verify: OK (%s)\n", report.message.c_str());
|
||
return 0;
|
||
}
|
||
std::fprintf(stderr, "verify: FAILED %s: %s\n",
|
||
vde::plugin::plugin_security_error_name(report.code),
|
||
report.message.c_str());
|
||
#if !defined(VDE_TOOL_HAS_ED25519)
|
||
if (report.code == vde::plugin::PluginSecurityErrorCode::VerifierUnavailable) {
|
||
return 4;
|
||
}
|
||
#endif
|
||
return 3;
|
||
}
|
||
|
||
bool write_text_file(const vde::fs::path& path, const std::string& text,
|
||
std::string* error);
|
||
|
||
int cmd_list(const std::string& dir) {
|
||
namespace fs = vde::fs;
|
||
std::error_code ec;
|
||
if (!fs::is_directory(dir, ec) || ec) {
|
||
std::fprintf(stderr, "list: not a directory: '%s'\n", dir.c_str());
|
||
return 2;
|
||
}
|
||
const fs::path root(dir);
|
||
std::vector<fs::path> manifests;
|
||
fs::recursive_directory_iterator it(root, fs::directory_options::skip_permission_denied, ec);
|
||
const fs::recursive_directory_iterator end;
|
||
while (!ec && it != end) {
|
||
std::error_code entry_ec;
|
||
if (it->is_regular_file(entry_ec) && !entry_ec &&
|
||
it->path().filename() == "vde-plugin.json") {
|
||
manifests.push_back(it->path());
|
||
}
|
||
it.increment(ec);
|
||
}
|
||
std::sort(manifests.begin(), manifests.end(),
|
||
[](const fs::path& a, const fs::path& b) {
|
||
return a.generic_string() < b.generic_string();
|
||
});
|
||
std::printf("install root: %s\n", root.string().c_str());
|
||
std::printf("manifests: %zu\n", manifests.size());
|
||
std::size_t ok = 0;
|
||
std::size_t failed = 0;
|
||
for (const auto& manifest_path : manifests) {
|
||
const std::string text = read_file_text(manifest_path);
|
||
vde::plugin::PluginManifest manifest;
|
||
std::string error;
|
||
if (text.empty() ||
|
||
!vde::plugin::parse_plugin_manifest(text, manifest_path.string(), &manifest,
|
||
&error)) {
|
||
std::printf("\n- manifest : %s\n", manifest_path.string().c_str());
|
||
std::printf(" status : INVALID\n");
|
||
std::printf(" error : %s\n", error.c_str());
|
||
++failed;
|
||
continue;
|
||
}
|
||
const fs::path plugin_dir = manifest_path.parent_path();
|
||
const bool installed = plugin_dir.filename() == manifest.id &&
|
||
plugin_dir.parent_path() == root;
|
||
std::printf("\n- id : %s\n", manifest.id.c_str());
|
||
std::printf(" version : %s\n", version_text(manifest.version).c_str());
|
||
std::printf(" kind : %s (%u)\n",
|
||
vde::plugin::plugin_kind_name(manifest.kind),
|
||
static_cast<unsigned>(manifest.kind));
|
||
std::printf(" abi : %d.%d\n", manifest.abi_major, manifest.abi_minor);
|
||
std::printf(" entry : %s\n",
|
||
manifest.entry.empty() ? "(none)" : manifest.entry.c_str());
|
||
std::printf(" capabilities : %s\n", join(manifest.capabilities, "; ").c_str());
|
||
std::printf(" manifest : %s\n", manifest_path.string().c_str());
|
||
std::printf(" status : %s\n", installed ? "installed" : "discovered");
|
||
++ok;
|
||
}
|
||
std::printf("\nsummary: %zu valid (%zu installed), %zu invalid\n", ok, ok, failed);
|
||
return failed == 0 ? 0 : 3;
|
||
}
|
||
|
||
|
||
// ── P4-53:生态覆盖矩阵(kind/ABI/capabilities 确定性汇总)──
|
||
|
||
struct MatrixManifest {
|
||
vde::fs::path path;
|
||
PluginManifest manifest;
|
||
};
|
||
|
||
int cmd_matrix(const std::string& dir, bool json_output,
|
||
const std::vector<std::string>& allowed_missing) {
|
||
namespace fs = vde::fs;
|
||
std::error_code ec;
|
||
if (!fs::is_directory(dir, ec) || ec) {
|
||
std::fprintf(stderr, "matrix: not a directory: '%s'\n", dir.c_str());
|
||
return 2;
|
||
}
|
||
const fs::path root(dir);
|
||
std::vector<fs::path> manifest_paths;
|
||
fs::recursive_directory_iterator it(root, fs::directory_options::skip_permission_denied, ec);
|
||
const fs::recursive_directory_iterator end;
|
||
while (!ec && it != end) {
|
||
std::error_code entry_ec;
|
||
if (it->is_regular_file(entry_ec) && !entry_ec &&
|
||
it->path().filename() == "vde-plugin.json") {
|
||
manifest_paths.push_back(it->path());
|
||
}
|
||
it.increment(ec);
|
||
}
|
||
std::sort(manifest_paths.begin(), manifest_paths.end(),
|
||
[](const fs::path& a, const fs::path& b) {
|
||
return a.generic_string() < b.generic_string();
|
||
});
|
||
|
||
const std::vector<PluginKind> expected_kinds = {
|
||
PluginKind::Format, PluginKind::GeomOp, PluginKind::CaeSolver,
|
||
PluginKind::CamStrategy, PluginKind::GcsSolver, PluginKind::Mesher,
|
||
PluginKind::Optimizer, PluginKind::IoBatch, PluginKind::Ui,
|
||
PluginKind::Binding,
|
||
};
|
||
std::map<PluginKind, std::size_t> kind_counts;
|
||
for (const auto kind : expected_kinds) kind_counts[kind] = 0;
|
||
std::vector<MatrixManifest> valid;
|
||
std::vector<vde::fs::path> invalid;
|
||
std::map<std::string, std::size_t> id_counts;
|
||
std::map<std::string, std::size_t> capability_counts;
|
||
std::size_t min_minor = 0;
|
||
std::size_t max_minor = 0;
|
||
bool abi_compatible = true;
|
||
for (const auto& manifest_path : manifest_paths) {
|
||
const std::string text = read_file_text(manifest_path);
|
||
PluginManifest manifest;
|
||
std::string error;
|
||
if (text.empty() ||
|
||
!vde::plugin::parse_plugin_manifest(text, manifest_path.string(), &manifest,
|
||
&error)) {
|
||
invalid.push_back(manifest_path);
|
||
continue;
|
||
}
|
||
valid.push_back(MatrixManifest{manifest_path, manifest});
|
||
++id_counts[manifest.id];
|
||
auto kind_it = kind_counts.find(manifest.kind);
|
||
if (kind_it != kind_counts.end()) ++kind_it->second;
|
||
for (const auto& capability : manifest.capabilities)
|
||
++capability_counts[capability];
|
||
const std::size_t minor = static_cast<std::size_t>(manifest.abi_minor);
|
||
if (valid.size() == 1 || minor < min_minor) min_minor = minor;
|
||
if (valid.size() == 1 || minor > max_minor) max_minor = minor;
|
||
if (manifest.abi_major != VDE_PLUGIN_ABI_MAJOR ||
|
||
manifest.abi_minor > VDE_PLUGIN_ABI_MINOR)
|
||
abi_compatible = false;
|
||
}
|
||
std::vector<std::string> duplicate_ids;
|
||
for (const auto& entry : id_counts)
|
||
if (entry.second > 1) duplicate_ids.push_back(entry.first);
|
||
std::vector<std::string> missing_kinds;
|
||
std::vector<std::string> missing_required_kinds;
|
||
for (const auto kind : expected_kinds) {
|
||
if (kind_counts[kind] == 0) {
|
||
const std::string name = vde::plugin::plugin_kind_name(kind);
|
||
missing_kinds.push_back(name);
|
||
const bool allowed =
|
||
std::find(allowed_missing.begin(), allowed_missing.end(), name) !=
|
||
allowed_missing.end();
|
||
if (!allowed) missing_required_kinds.push_back(name);
|
||
}
|
||
}
|
||
const bool pass = invalid.empty() && duplicate_ids.empty() &&
|
||
missing_required_kinds.empty() && abi_compatible;
|
||
|
||
if (json_output) {
|
||
nlohmann::json root_json;
|
||
root_json["dir"] = root.string();
|
||
root_json["manifests"] = manifest_paths.size();
|
||
root_json["valid"] = valid.size();
|
||
root_json["invalid"] = invalid.size();
|
||
nlohmann::json kinds = nlohmann::json::object();
|
||
for (const auto kind : expected_kinds) {
|
||
kinds[vde::plugin::plugin_kind_name(kind)] = kind_counts[kind];
|
||
}
|
||
root_json["kinds"] = kinds;
|
||
root_json["missing_kinds"] = missing_kinds;
|
||
root_json["missing_required_kinds"] = missing_required_kinds;
|
||
root_json["allowed_missing_kinds"] = allowed_missing;
|
||
nlohmann::json abi;
|
||
abi["host_major"] = VDE_PLUGIN_ABI_MAJOR;
|
||
abi["host_minor"] = VDE_PLUGIN_ABI_MINOR;
|
||
abi["min_minor"] = valid.empty() ? 0 : min_minor;
|
||
abi["max_minor"] = valid.empty() ? 0 : max_minor;
|
||
abi["compatible"] = abi_compatible;
|
||
root_json["abi"] = abi;
|
||
root_json["duplicate_ids"] = duplicate_ids;
|
||
std::vector<std::string> capabilities;
|
||
for (const auto& entry : capability_counts) capabilities.push_back(entry.first);
|
||
root_json["capabilities"] = capabilities;
|
||
root_json["status"] = pass ? "PASS" : "FAIL";
|
||
std::printf("%s\n", root_json.dump(2).c_str());
|
||
} else {
|
||
std::printf("ecosystem matrix: %s\n", root.string().c_str());
|
||
std::printf("manifests: %zu (valid=%zu invalid=%zu)\n", manifest_paths.size(),
|
||
valid.size(), invalid.size());
|
||
std::printf("kind counts:\n");
|
||
for (const auto kind : expected_kinds) {
|
||
std::printf(" %-12s : %zu\n", vde::plugin::plugin_kind_name(kind),
|
||
kind_counts[kind]);
|
||
}
|
||
if (missing_kinds.empty()) {
|
||
std::printf("missing kinds: (none)\n");
|
||
} else {
|
||
std::printf("missing kinds: %s\n", join(missing_kinds, ", ").c_str());
|
||
if (!missing_required_kinds.empty())
|
||
std::printf("missing required kinds: %s\n",
|
||
join(missing_required_kinds, ", ").c_str());
|
||
if (!allowed_missing.empty())
|
||
std::printf("allowed missing kinds (external providers): %s\n",
|
||
join(allowed_missing, ", ").c_str());
|
||
}
|
||
std::printf("abi: host=%d.%d manifest_min=%zu manifest_max=%zu compatible=%s\n",
|
||
VDE_PLUGIN_ABI_MAJOR, VDE_PLUGIN_ABI_MINOR,
|
||
valid.empty() ? 0 : min_minor, valid.empty() ? 0 : max_minor,
|
||
abi_compatible ? "true" : "false");
|
||
if (duplicate_ids.empty()) {
|
||
std::printf("duplicate ids: (none)\n");
|
||
} else {
|
||
std::printf("duplicate ids: %s\n", join(duplicate_ids, ", ").c_str());
|
||
}
|
||
std::vector<std::string> capabilities;
|
||
for (const auto& entry : capability_counts) capabilities.push_back(entry.first);
|
||
std::printf("capabilities (%zu unique): %s\n", capabilities.size(),
|
||
join(capabilities, "; ").c_str());
|
||
if (!invalid.empty()) {
|
||
for (const auto& path : invalid)
|
||
std::printf("invalid manifest: %s\n", path.string().c_str());
|
||
}
|
||
std::printf("status: %s\n", pass ? "PASS" : "FAIL");
|
||
}
|
||
return pass ? 0 : 2;
|
||
}
|
||
// ── P4-41:package / unpack / verify-package(确定性 .vdeplugin 归档)──
|
||
constexpr const char* kPackageMagic = "VDEPKG1";
|
||
|
||
bool collect_plugin_files(const vde::fs::path& dir, const vde::fs::path& skip_path,
|
||
std::vector<std::string>* relative_paths,
|
||
std::uint64_t* total_bytes, std::string* error) {
|
||
namespace fs = vde::fs;
|
||
relative_paths->clear();
|
||
*total_bytes = 0;
|
||
std::error_code ec;
|
||
fs::recursive_directory_iterator it(dir, fs::directory_options::skip_permission_denied, ec);
|
||
const fs::recursive_directory_iterator end;
|
||
while (!ec && it != end) {
|
||
std::error_code entry_ec;
|
||
const fs::path path = it->path();
|
||
if (it->is_regular_file(entry_ec) && !entry_ec) {
|
||
const fs::path relative = fs::relative(path, dir, entry_ec);
|
||
if (entry_ec) return false;
|
||
const std::string relative_text = relative.generic_string();
|
||
std::string path_error;
|
||
if (!vde::plugin::is_artifact_path_safe(relative_text, &path_error)) {
|
||
if (error) *error = "unsafe file path '" + relative_text + "': " + path_error;
|
||
return false;
|
||
}
|
||
std::error_code canonical_ec;
|
||
if (!skip_path.empty() &&
|
||
fs::weakly_canonical(path, canonical_ec) ==
|
||
fs::weakly_canonical(skip_path, canonical_ec)) {
|
||
it.increment(ec);
|
||
continue;
|
||
}
|
||
std::error_code size_ec;
|
||
const std::uintmax_t size = fs::file_size(path, size_ec);
|
||
if (size_ec) return false;
|
||
*total_bytes += static_cast<std::uint64_t>(size);
|
||
relative_paths->push_back(relative_text);
|
||
}
|
||
it.increment(ec);
|
||
}
|
||
if (ec) {
|
||
if (error) *error = "directory iteration failed: " + ec.message();
|
||
return false;
|
||
}
|
||
std::sort(relative_paths->begin(), relative_paths->end());
|
||
if (relative_paths->size() > 10000) {
|
||
if (error) *error = "package contains more than 10000 files";
|
||
return false;
|
||
}
|
||
if (*total_bytes > 1024ull * 1024ull * 1024ull) {
|
||
if (error) *error = "package exceeds 1GiB";
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
int cmd_package(const std::string& dir_text, const std::string& output_text) {
|
||
namespace fs = vde::fs;
|
||
if (dir_text.empty() || output_text.empty()) {
|
||
std::fprintf(stderr, "package requires <plugin_dir> and -o <file.vdeplugin>\n");
|
||
return 2;
|
||
}
|
||
const fs::path dir(dir_text);
|
||
std::error_code ec;
|
||
if (!fs::is_directory(dir, ec) || ec) {
|
||
std::fprintf(stderr, "package: not a directory: '%s'\n", dir_text.c_str());
|
||
return 2;
|
||
}
|
||
const fs::path manifest_path = dir / "vde-plugin.json";
|
||
if (!fs::is_regular_file(manifest_path, ec) || ec) {
|
||
std::fprintf(stderr, "package: vde-plugin.json not found in '%s'\n",
|
||
dir_text.c_str());
|
||
return 3;
|
||
}
|
||
const std::string manifest_text = read_file_text(manifest_path);
|
||
vde::plugin::PluginManifest manifest;
|
||
std::string error;
|
||
if (manifest_text.empty() ||
|
||
!vde::plugin::parse_plugin_manifest(manifest_text, manifest_path.string(),
|
||
&manifest, &error)) {
|
||
std::fprintf(stderr, "package: invalid manifest: %s\n", error.c_str());
|
||
return 3;
|
||
}
|
||
if (manifest.id.empty() || !manifest.version.valid || manifest.entry.empty()) {
|
||
std::fprintf(stderr, "package: manifest missing id/version/entry\n");
|
||
return 3;
|
||
}
|
||
if (manifest.abi_major != VDE_PLUGIN_ABI_MAJOR ||
|
||
manifest.abi_minor > VDE_PLUGIN_ABI_MINOR) {
|
||
std::fprintf(stderr, "package: incompatible ABI %d.%d (host %d.%d)\n",
|
||
manifest.abi_major, manifest.abi_minor, VDE_PLUGIN_ABI_MAJOR,
|
||
VDE_PLUGIN_ABI_MINOR);
|
||
return 3;
|
||
}
|
||
if (manifest.capabilities.empty()) {
|
||
std::fprintf(stderr, "package: manifest has empty capabilities\n");
|
||
return 3;
|
||
}
|
||
std::string path_error;
|
||
if (!vde::plugin::is_artifact_path_safe(manifest.entry, &path_error)) {
|
||
std::fprintf(stderr, "package: unsafe entry path '%s': %s\n",
|
||
manifest.entry.c_str(), path_error.c_str());
|
||
return 3;
|
||
}
|
||
if (!fs::is_regular_file(dir / manifest.entry, ec) || ec) {
|
||
std::fprintf(stderr, "package: entry library not found: %s\n",
|
||
(dir / manifest.entry).string().c_str());
|
||
return 3;
|
||
}
|
||
const fs::path output(output_text);
|
||
if (output.has_parent_path()) fs::create_directories(output.parent_path(), ec);
|
||
std::vector<std::string> relative_paths;
|
||
std::uint64_t total_bytes = 0;
|
||
if (!collect_plugin_files(dir, output, &relative_paths, &total_bytes, &error)) {
|
||
std::fprintf(stderr, "package: %s\n", error.c_str());
|
||
return 3;
|
||
}
|
||
if (relative_paths.empty()) {
|
||
std::fprintf(stderr, "package: no files to package\n");
|
||
return 3;
|
||
}
|
||
std::string archive;
|
||
archive.reserve(static_cast<std::size_t>(total_bytes) + 4096);
|
||
archive += std::string(kPackageMagic) + "\n";
|
||
nlohmann::json metadata;
|
||
metadata["id"] = manifest.id;
|
||
metadata["version"] = version_text(manifest.version);
|
||
metadata["kind"] = vde::plugin::plugin_kind_name(manifest.kind);
|
||
metadata["abi_major"] = manifest.abi_major;
|
||
metadata["abi_minor"] = manifest.abi_minor;
|
||
metadata["entry"] = manifest.entry;
|
||
metadata["files"] = relative_paths.size();
|
||
archive += metadata.dump() + "\n";
|
||
std::vector<std::string> payloads;
|
||
payloads.reserve(relative_paths.size());
|
||
for (const auto& relative : relative_paths) {
|
||
const fs::path full = dir / relative;
|
||
std::string sha256;
|
||
if (!vde::plugin::sha256_file(full.string(), &sha256, &error)) {
|
||
std::fprintf(stderr, "package: sha256 failed for %s: %s\n", relative.c_str(),
|
||
error.c_str());
|
||
return 3;
|
||
}
|
||
const std::string bytes = read_file_text(full);
|
||
archive += "FILE " + sha256 + " " + std::to_string(bytes.size()) + " " + relative +
|
||
"\n";
|
||
payloads.push_back(bytes);
|
||
}
|
||
// 归档布局:magic + metadata + N 个 FILE 头行 + 按序 payload(确定性)。
|
||
for (const auto& bytes : payloads) archive += bytes;
|
||
if (!write_text_file(output, archive, &error)) {
|
||
std::fprintf(stderr, "package: %s\n", error.c_str());
|
||
return 3;
|
||
}
|
||
std::string archive_sha256;
|
||
if (!vde::plugin::sha256_file(output.string(), &archive_sha256, &error)) {
|
||
std::fprintf(stderr, "package: archive sha256 failed: %s\n", error.c_str());
|
||
return 3;
|
||
}
|
||
const fs::path sidecar(output.string() + ".sha256");
|
||
if (!write_text_file(sidecar, archive_sha256 + " " + output.filename().string() + "\n",
|
||
&error)) {
|
||
std::fprintf(stderr, "package: cannot write sha256 sidecar: %s\n", error.c_str());
|
||
return 3;
|
||
}
|
||
std::printf("package: %s %s\n", manifest.id.c_str(),
|
||
version_text(manifest.version).c_str());
|
||
std::printf(" files : %zu\n", relative_paths.size());
|
||
std::printf(" bytes : %llu\n",
|
||
static_cast<unsigned long long>(archive.size()));
|
||
std::printf(" sha256 : %s\n", archive_sha256.c_str());
|
||
std::printf(" archive : %s\n", output.string().c_str());
|
||
std::printf(" sidecar : %s\n", sidecar.string().c_str());
|
||
std::printf("package: PASS\n");
|
||
return 0;
|
||
}
|
||
|
||
struct PackageEntry {
|
||
std::string sha256;
|
||
std::string path;
|
||
std::uint64_t size = 0;
|
||
};
|
||
|
||
bool parse_package_bytes(const std::string& archive, nlohmann::json* metadata,
|
||
std::vector<PackageEntry>* entries, std::string* error) {
|
||
std::size_t pos = 0;
|
||
const auto read_line = [&](std::string* line) -> bool {
|
||
if (pos >= archive.size()) return false;
|
||
const std::size_t eol = archive.find('\n', pos);
|
||
if (eol == std::string::npos) return false;
|
||
*line = archive.substr(pos, eol - pos);
|
||
pos = eol + 1;
|
||
return true;
|
||
};
|
||
std::string line;
|
||
if (!read_line(&line) || line != kPackageMagic) {
|
||
if (error) *error = "not a .vdeplugin archive (bad magic)";
|
||
return false;
|
||
}
|
||
std::string metadata_line;
|
||
if (!read_line(&metadata_line)) {
|
||
if (error) *error = "archive metadata missing";
|
||
return false;
|
||
}
|
||
try {
|
||
*metadata = nlohmann::json::parse(metadata_line);
|
||
} catch (const std::exception& e) {
|
||
if (error) *error = std::string("archive metadata JSON: ") + e.what();
|
||
return false;
|
||
}
|
||
std::size_t file_count = 0;
|
||
if (!metadata->contains("files") || !(*metadata)["files"].is_number_unsigned()) {
|
||
if (error) *error = "archive metadata missing files count";
|
||
return false;
|
||
}
|
||
file_count = (*metadata)["files"].get<std::size_t>();
|
||
if (file_count == 0 || file_count > 10000) {
|
||
if (error) *error = "archive files count invalid";
|
||
return false;
|
||
}
|
||
entries->clear();
|
||
std::uint64_t total = 0;
|
||
for (std::size_t i = 0; i < file_count; ++i) {
|
||
std::string file_line;
|
||
if (!read_line(&file_line)) {
|
||
if (error) *error = "archive truncated (file header)";
|
||
return false;
|
||
}
|
||
if (file_line.rfind("FILE ", 0) != 0) {
|
||
if (error) *error = "archive bad FILE header";
|
||
return false;
|
||
}
|
||
const std::size_t sha_begin = 5;
|
||
const std::size_t sha_end = file_line.find(' ', sha_begin);
|
||
const std::size_t size_begin = (sha_end == std::string::npos) ? std::string::npos
|
||
: sha_end + 1;
|
||
const std::size_t size_end = (size_begin == std::string::npos)
|
||
? std::string::npos
|
||
: file_line.find(' ', size_begin);
|
||
if (sha_end == std::string::npos || size_end == std::string::npos) {
|
||
if (error) *error = "archive malformed FILE header";
|
||
return false;
|
||
}
|
||
PackageEntry entry;
|
||
entry.sha256 = file_line.substr(sha_begin, sha_end - sha_begin);
|
||
const std::string size_text = file_line.substr(size_begin, size_end - size_begin);
|
||
entry.path = file_line.substr(size_end + 1);
|
||
char* parse_end = nullptr;
|
||
const unsigned long long parsed_size =
|
||
std::strtoull(size_text.c_str(), &parse_end, 10);
|
||
if (parse_end == size_text.c_str() || *parse_end != '\0') {
|
||
if (error) *error = "archive bad file size";
|
||
return false;
|
||
}
|
||
entry.size = parsed_size;
|
||
std::string path_error;
|
||
if (!vde::plugin::is_artifact_path_safe(entry.path, &path_error)) {
|
||
if (error) *error = "archive unsafe path '" + entry.path + "': " + path_error;
|
||
return false;
|
||
}
|
||
total += entry.size;
|
||
if (total > 1024ull * 1024ull * 1024ull) {
|
||
if (error) *error = "archive exceeds 1GiB";
|
||
return false;
|
||
}
|
||
entries->push_back(entry);
|
||
}
|
||
return true;
|
||
}
|
||
|
||
std::size_t find_package_payload_offset(const std::string& archive) {
|
||
std::size_t pos = 0;
|
||
for (int line_index = 0; line_index < 2; ++line_index) {
|
||
const std::size_t eol = archive.find('\n', pos);
|
||
if (eol == std::string::npos) return std::string::npos;
|
||
pos = eol + 1;
|
||
}
|
||
// FILE header lines: count from metadata
|
||
std::size_t file_count = 0;
|
||
{
|
||
const std::size_t metadata_begin = archive.find('\n') + 1;
|
||
const std::size_t metadata_end = archive.find('\n', metadata_begin);
|
||
try {
|
||
const nlohmann::json metadata =
|
||
nlohmann::json::parse(archive.substr(metadata_begin,
|
||
metadata_end - metadata_begin));
|
||
file_count = metadata.value("files", 0u);
|
||
} catch (...) {
|
||
return std::string::npos;
|
||
}
|
||
}
|
||
for (std::size_t i = 0; i < file_count; ++i) {
|
||
const std::size_t eol = archive.find('\n', pos);
|
||
if (eol == std::string::npos) return std::string::npos;
|
||
pos = eol + 1;
|
||
}
|
||
return pos;
|
||
}
|
||
|
||
int cmd_verify_package(const std::string& archive_path) {
|
||
const std::string archive = read_file_text(archive_path);
|
||
if (archive.empty()) {
|
||
std::fprintf(stderr, "verify-package: cannot read archive '%s'\n",
|
||
archive_path.c_str());
|
||
return 3;
|
||
}
|
||
nlohmann::json metadata;
|
||
std::vector<PackageEntry> entries;
|
||
std::string error;
|
||
if (!parse_package_bytes(archive, &metadata, &entries, &error)) {
|
||
std::fprintf(stderr, "verify-package: FAILED: %s\n", error.c_str());
|
||
return 3;
|
||
}
|
||
std::size_t pos = 0;
|
||
const std::size_t payload = find_package_payload_offset(archive);
|
||
if (payload == std::string::npos) {
|
||
std::fprintf(stderr, "verify-package: FAILED: payload offset\n");
|
||
return 3;
|
||
}
|
||
pos = payload;
|
||
for (const auto& entry : entries) {
|
||
if (pos + entry.size > archive.size()) {
|
||
std::fprintf(stderr, "verify-package: FAILED: truncated payload %s\n",
|
||
entry.path.c_str());
|
||
return 3;
|
||
}
|
||
const std::string bytes = archive.substr(pos, static_cast<std::size_t>(entry.size));
|
||
pos += static_cast<std::size_t>(entry.size);
|
||
const std::string actual = vde::plugin::sha256_hex(bytes);
|
||
if (actual != entry.sha256) {
|
||
std::fprintf(stderr, "verify-package: FAILED: sha256 mismatch for %s\n",
|
||
entry.path.c_str());
|
||
return 3;
|
||
}
|
||
}
|
||
std::printf("verify-package: PASS\n");
|
||
std::printf(" id : %s\n", metadata.value("id", std::string()).c_str());
|
||
std::printf(" version : %s\n", metadata.value("version", std::string()).c_str());
|
||
std::printf(" kind : %s\n", metadata.value("kind", std::string()).c_str());
|
||
std::printf(" files : %zu\n", entries.size());
|
||
return 0;
|
||
}
|
||
|
||
int cmd_unpack(const std::string& archive_path, const std::string& dest_text) {
|
||
namespace fs = vde::fs;
|
||
if (dest_text.empty()) {
|
||
std::fprintf(stderr, "unpack requires --dest <dir>\n");
|
||
return 2;
|
||
}
|
||
const std::string archive = read_file_text(archive_path);
|
||
nlohmann::json metadata;
|
||
std::vector<PackageEntry> entries;
|
||
std::string error;
|
||
if (archive.empty() || !parse_package_bytes(archive, &metadata, &entries, &error)) {
|
||
std::fprintf(stderr, "unpack: FAILED: %s\n",
|
||
archive.empty() ? "cannot read archive" : error.c_str());
|
||
return 3;
|
||
}
|
||
const std::size_t payload = find_package_payload_offset(archive);
|
||
if (payload == std::string::npos) return 3;
|
||
const fs::path dest(dest_text);
|
||
std::error_code ec;
|
||
fs::create_directories(dest, ec);
|
||
if (ec) {
|
||
std::fprintf(stderr, "unpack: cannot create dest: %s\n", ec.message().c_str());
|
||
return 3;
|
||
}
|
||
const fs::path canonical_dest = fs::weakly_canonical(dest, ec);
|
||
std::size_t pos = payload;
|
||
for (const auto& entry : entries) {
|
||
if (pos + entry.size > archive.size()) {
|
||
std::fprintf(stderr, "unpack: truncated payload %s\n", entry.path.c_str());
|
||
return 3;
|
||
}
|
||
const std::string bytes = archive.substr(pos, static_cast<std::size_t>(entry.size));
|
||
pos += static_cast<std::size_t>(entry.size);
|
||
if (vde::plugin::sha256_hex(bytes) != entry.sha256) {
|
||
std::fprintf(stderr, "unpack: sha256 mismatch for %s\n", entry.path.c_str());
|
||
return 20;
|
||
}
|
||
const fs::path target = dest / entry.path;
|
||
const fs::path canonical_target = fs::weakly_canonical(target, ec);
|
||
if (ec || canonical_target.string().rfind(canonical_dest.string(), 0) != 0) {
|
||
std::fprintf(stderr, "unpack: refused path escape %s\n", entry.path.c_str());
|
||
return 3;
|
||
}
|
||
if (!write_text_file(target, bytes, &error)) {
|
||
std::fprintf(stderr, "unpack: %s\n", error.c_str());
|
||
return 3;
|
||
}
|
||
}
|
||
std::printf("unpack: PASS (%zu files -> %s)\n", entries.size(), dest.string().c_str());
|
||
return 0;
|
||
}
|
||
|
||
/// --check:kind ↔ 能力前缀的约定(仅 warning;不同插件的自定义 capability 可不同)
|
||
std::string expected_capability_prefix(PluginKind kind) {
|
||
switch (kind) {
|
||
case PluginKind::Format: return "format:";
|
||
case PluginKind::CaeSolver: return "cae_solver:";
|
||
case PluginKind::CamStrategy: return "cam_strategy:";
|
||
case PluginKind::GcsSolver: return "gcs:";
|
||
case PluginKind::Mesher: return "mesher:";
|
||
case PluginKind::Optimizer: return "optimizer:";
|
||
case PluginKind::IoBatch: return "io_batch:";
|
||
default: return std::string();
|
||
}
|
||
}
|
||
|
||
bool starts_with(const std::string& text, const std::string& prefix) {
|
||
return text.size() >= prefix.size() &&
|
||
text.compare(0, prefix.size(), prefix) == 0;
|
||
}
|
||
|
||
int cmd_check(const std::string& target) {
|
||
namespace fs = vde::fs;
|
||
std::error_code ec;
|
||
fs::path manifest_path(target);
|
||
if (fs::is_directory(manifest_path, ec)) manifest_path /= "vde-plugin.json";
|
||
std::printf("check plugin: %s\n", target.c_str());
|
||
if (!fs::is_regular_file(manifest_path, ec)) {
|
||
std::fprintf(stderr, "check: [FAIL] manifest not found: '%s'\n",
|
||
manifest_path.string().c_str());
|
||
return 3;
|
||
}
|
||
const std::string manifest_json = read_file_text(manifest_path);
|
||
if (manifest_json.empty()) {
|
||
std::fprintf(stderr, "check: [FAIL] cannot read manifest: '%s'\n",
|
||
manifest_path.string().c_str());
|
||
return 3;
|
||
}
|
||
PluginManifest manifest;
|
||
std::string parse_error;
|
||
if (!vde::plugin::parse_plugin_manifest(manifest_json, manifest_path.string(),
|
||
&manifest, &parse_error)) {
|
||
std::fprintf(stderr, "check: [FAIL] manifest parse: %s\n",
|
||
parse_error.empty() ? "unknown error" : parse_error.c_str());
|
||
return 3;
|
||
}
|
||
bool ok = true;
|
||
std::printf(" [ OK ] manifest parse: %s\n", manifest_path.string().c_str());
|
||
if (!manifest.id.empty()) {
|
||
std::printf(" [ OK ] id: %s\n", manifest.id.c_str());
|
||
} else {
|
||
std::printf(" [FAIL] id: empty\n");
|
||
ok = false;
|
||
}
|
||
if (manifest.version.valid) {
|
||
std::printf(" [ OK ] version: %s\n", version_text(manifest.version).c_str());
|
||
} else {
|
||
std::printf(" [FAIL] version: invalid (need x.y.z)\n");
|
||
ok = false;
|
||
}
|
||
if (!manifest.entry.empty()) {
|
||
std::printf(" [ OK ] entry: %s\n", manifest.entry.c_str());
|
||
} else {
|
||
std::printf(" [FAIL] entry: empty\n");
|
||
ok = false;
|
||
}
|
||
|
||
const std::string prefix = expected_capability_prefix(manifest.kind);
|
||
if (manifest.capabilities.empty()) {
|
||
std::printf(" [WARN] capabilities: none declared (recommended for discovery)\n");
|
||
} else {
|
||
bool prefix_ok = prefix.empty();
|
||
for (const auto& capability : manifest.capabilities) {
|
||
if (!prefix.empty() && starts_with(capability, prefix)) prefix_ok = true;
|
||
}
|
||
if (!prefix_ok) {
|
||
std::printf(" [WARN] capabilities: none starts with '%s' (kind=%s)\n",
|
||
prefix.c_str(), vde::plugin::plugin_kind_name(manifest.kind));
|
||
} else {
|
||
std::printf(" [ OK ] capabilities: %s\n",
|
||
join(manifest.capabilities, "; ").c_str());
|
||
}
|
||
}
|
||
|
||
const bool abi_compatible = manifest.abi_major == VDE_PLUGIN_ABI_MAJOR &&
|
||
manifest.abi_minor <= VDE_PLUGIN_ABI_MINOR;
|
||
if (abi_compatible) {
|
||
std::printf(" [ OK ] abi: %d.%d compatible with host %d.%d\n", manifest.abi_major,
|
||
manifest.abi_minor, VDE_PLUGIN_ABI_MAJOR, VDE_PLUGIN_ABI_MINOR);
|
||
} else {
|
||
std::printf(" [FAIL] abi: %d.%d incompatible with host %d.%d (need major==%d, "
|
||
"minor<=%d)\n",
|
||
manifest.abi_major, manifest.abi_minor, VDE_PLUGIN_ABI_MAJOR,
|
||
VDE_PLUGIN_ABI_MINOR, VDE_PLUGIN_ABI_MAJOR, VDE_PLUGIN_ABI_MINOR);
|
||
ok = false;
|
||
}
|
||
|
||
// 动态库存在性已由 parse_plugin_manifest 校验;这里再校验 query 元信息一致性。
|
||
const std::string library_path =
|
||
manifest.library_path.empty() ? manifest.entry : manifest.library_path;
|
||
vde::platform::LibHandle library = nullptr;
|
||
if (library_path.empty()) {
|
||
std::printf(" [FAIL] entry library: unresolved\n");
|
||
ok = false;
|
||
} else {
|
||
library = vde::platform::load_library(library_path);
|
||
if (!library) {
|
||
std::printf(" [FAIL] load '%s': %s\n", library_path.c_str(),
|
||
vde::platform::last_error().c_str());
|
||
ok = false;
|
||
}
|
||
}
|
||
if (library) {
|
||
const auto query = reinterpret_cast<VdePluginQueryFn>(
|
||
vde::platform::find_symbol(library, VDE_PLUGIN_SYMBOL_QUERY));
|
||
if (!query) {
|
||
std::printf(" [FAIL] missing exported symbol '%s'\n",
|
||
VDE_PLUGIN_SYMBOL_QUERY);
|
||
ok = false;
|
||
} else {
|
||
VdePluginInfo info{};
|
||
info.struct_size = static_cast<uint32_t>(sizeof(VdePluginInfo));
|
||
int rc = VDE_PLUGIN_ERR_INTERNAL;
|
||
try {
|
||
rc = query(static_cast<uint32_t>(VDE_PLUGIN_ABI_MAJOR), &info);
|
||
} catch (...) {
|
||
rc = VDE_PLUGIN_ERR_INTERNAL;
|
||
}
|
||
if (rc != VDE_PLUGIN_OK) {
|
||
std::printf(" [FAIL] vde_plugin_query rc=%d\n", rc);
|
||
ok = false;
|
||
} else if (info.struct_size < sizeof(VdePluginInfo)) {
|
||
std::printf(" [FAIL] vde_plugin_query struct_size too small\n");
|
||
ok = false;
|
||
} else {
|
||
const std::string reported_id =
|
||
info.id ? std::string(info.id) : std::string("<null>");
|
||
const std::string reported_version =
|
||
info.version ? std::string(info.version) : std::string("<null>");
|
||
if (reported_id == manifest.id) {
|
||
std::printf(" [ OK ] query id: %s\n", reported_id.c_str());
|
||
} else {
|
||
std::printf(" [FAIL] query id mismatch: manifest='%s' query='%s'\n",
|
||
manifest.id.c_str(), reported_id.c_str());
|
||
ok = false;
|
||
}
|
||
if (reported_version == version_text(manifest.version)) {
|
||
std::printf(" [ OK ] query version: %s\n", reported_version.c_str());
|
||
} else {
|
||
std::printf(
|
||
" [FAIL] query version mismatch: manifest='%s' query='%s'\n",
|
||
version_text(manifest.version).c_str(), reported_version.c_str());
|
||
ok = false;
|
||
}
|
||
if (info.kind == static_cast<uint32_t>(manifest.kind)) {
|
||
std::printf(" [ OK ] query kind: %s (%u)\n",
|
||
vde::plugin::plugin_kind_name(manifest.kind), info.kind);
|
||
} else {
|
||
std::printf(" [FAIL] query kind mismatch: manifest=%u query=%u\n",
|
||
static_cast<unsigned>(manifest.kind), info.kind);
|
||
ok = false;
|
||
}
|
||
// ABI 尾扩展语义(与 PluginManager 一致):major 相等;
|
||
// manifest.minor <= query.minor <= host.minor。
|
||
const bool abi_major_ok =
|
||
static_cast<int>(info.abi_major) == manifest.abi_major;
|
||
const bool abi_minor_ok =
|
||
static_cast<int>(info.abi_minor) >= manifest.abi_minor &&
|
||
static_cast<int>(info.abi_minor) <= VDE_PLUGIN_ABI_MINOR;
|
||
if (abi_major_ok && abi_minor_ok) {
|
||
std::printf(" [ OK ] query abi: %u.%u (manifest %d.%d, host %d.%d)\n",
|
||
info.abi_major, info.abi_minor, manifest.abi_major,
|
||
manifest.abi_minor, VDE_PLUGIN_ABI_MAJOR,
|
||
VDE_PLUGIN_ABI_MINOR);
|
||
} else {
|
||
std::printf(" [FAIL] query abi mismatch: manifest=%d.%d query=%u.%u "
|
||
"(need major==%d, %d<=query.minor<=%d)\n",
|
||
manifest.abi_major, manifest.abi_minor, info.abi_major,
|
||
info.abi_minor, manifest.abi_major, manifest.abi_minor,
|
||
VDE_PLUGIN_ABI_MINOR);
|
||
ok = false;
|
||
}
|
||
}
|
||
}
|
||
vde::platform::unload_library(library);
|
||
}
|
||
|
||
// artifacts:声明时逐项路径安全/存在性/SHA-256;未声明时跳过(可选)。
|
||
std::vector<vde::plugin::PluginArtifact> artifacts;
|
||
bool artifacts_declared = false;
|
||
std::string artifacts_error;
|
||
if (!vde::plugin::parse_plugin_artifacts(manifest_json, &artifacts, &artifacts_error,
|
||
&artifacts_declared)) {
|
||
std::printf(" [FAIL] artifacts schema: %s\n", artifacts_error.c_str());
|
||
ok = false;
|
||
} else if (!artifacts_declared) {
|
||
std::printf(" [info] artifacts: none declared (optional)\n");
|
||
} else {
|
||
vde::plugin::PluginSecurityPolicy policy =
|
||
vde::plugin::strict_plugin_security_policy();
|
||
policy.permissions_default_deny = false;
|
||
policy.require_artifact_hashes = true;
|
||
std::vector<vde::plugin::PluginArtifactReport> reports;
|
||
vde::plugin::PluginSecurityErrorCode first_error =
|
||
vde::plugin::PluginSecurityErrorCode::Ok;
|
||
std::string artifacts_message;
|
||
const fs::path plugin_dir =
|
||
manifest_path.has_parent_path() ? manifest_path.parent_path() : fs::path(".");
|
||
const bool artifacts_ok = vde::plugin::verify_plugin_artifacts(
|
||
manifest_json, plugin_dir.string(), policy, &reports, &first_error,
|
||
&artifacts_message);
|
||
for (const auto& report : reports) {
|
||
const bool entry_ok = report.code == vde::plugin::PluginSecurityErrorCode::Ok;
|
||
std::printf(" [%s] artifact %s%s%s\n", entry_ok ? " OK " : "FAIL",
|
||
report.path.c_str(),
|
||
report.message.empty() ? "" : ": ", report.message.c_str());
|
||
}
|
||
if (artifacts_ok) {
|
||
std::printf(" [ OK ] artifacts: %zu hash(es) verified\n", reports.size());
|
||
} else {
|
||
std::printf(" [FAIL] artifacts: %s\n",
|
||
artifacts_message.empty()
|
||
? vde::plugin::plugin_security_error_name(first_error)
|
||
: artifacts_message.c_str());
|
||
ok = false;
|
||
}
|
||
}
|
||
|
||
if (!manifest.signature.empty()) {
|
||
std::printf(
|
||
" [WARN] signature present; use verify <dir> --pubkey <key> to check "
|
||
"signature/revocation\n");
|
||
} else {
|
||
std::printf(" [info] signature: none (verify command checks signatures)\n");
|
||
}
|
||
|
||
std::printf("check: %s\n", ok ? "PASS" : "FAILED");
|
||
return ok ? 0 : 3;
|
||
}
|
||
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// P3-13:插件仓库索引(plugins/index.json)+ 签名分发/安装
|
||
// ═══════════════════════════════════════════════════════════
|
||
//
|
||
// index.json schema v1(顶层):
|
||
// schema_version, generated_at, generator, publisher?, key_id?,
|
||
// plugins: [{ id, name?, version, kind, abi:{major,minor}, capabilities[],
|
||
// license?, entry, path, manifest_sha256, artifacts:[{path,sha256}] }],
|
||
// signature?: "ed25519:<base64(64B)>"
|
||
// 签名载荷:canonical_manifest_payload()(键排序/无空白/去掉顶层 signature),
|
||
// 复用 P3-5 的 Ed25519 规范;签名/公钥注入方式与 verify 一致(vde_license 可用时)。
|
||
|
||
using nlohmann::json;
|
||
|
||
constexpr int kIndexSchemaVersion = 1;
|
||
|
||
struct IndexArtifact {
|
||
std::string path;
|
||
std::string sha256;
|
||
std::string url; // 可选:绝对/相对索引 URL 的下载地址(P3-17c)
|
||
};
|
||
|
||
/// 依赖声明:id + 简化 semver 范围(exact / >= / > / <= / < / ^ / ~ / *)
|
||
struct VersionRequirement {
|
||
std::string id;
|
||
std::string version_range;
|
||
};
|
||
|
||
struct IndexEntry {
|
||
std::string id;
|
||
std::string name;
|
||
std::string version;
|
||
std::string kind;
|
||
int abi_major = 0;
|
||
int abi_minor = 0;
|
||
std::vector<std::string> capabilities;
|
||
std::vector<VersionRequirement> dependencies; // P3-17c
|
||
std::string license;
|
||
std::string entry;
|
||
std::string package_path; // 相对 index.json 所在目录
|
||
std::string manifest_sha256;
|
||
std::string manifest_signature;
|
||
std::vector<IndexArtifact> artifacts;
|
||
};
|
||
|
||
struct IndexCheckResult {
|
||
std::string key_id;
|
||
std::string signature;
|
||
std::string publisher;
|
||
bool signed_index = false;
|
||
bool signature_valid = false;
|
||
std::vector<IndexEntry> entries;
|
||
};
|
||
|
||
// ── P3-17c:简化 semver 范围(exact / >= / > / <= / < / ^ / ~ / *,空格分隔=AND)──
|
||
struct SemverTriple {
|
||
int major = 0;
|
||
int minor = 0;
|
||
int patch = 0;
|
||
};
|
||
|
||
bool parse_semver_triple(const std::string& text, SemverTriple* out) {
|
||
const auto parsed = vde::plugin::parse_semantic_version(text);
|
||
if (!parsed.valid) return false;
|
||
out->major = parsed.major;
|
||
out->minor = parsed.minor;
|
||
out->patch = parsed.patch;
|
||
return true;
|
||
}
|
||
|
||
int compare_semver(const SemverTriple& a, const SemverTriple& b) {
|
||
if (a.major != b.major) return a.major < b.major ? -1 : 1;
|
||
if (a.minor != b.minor) return a.minor < b.minor ? -1 : 1;
|
||
if (a.patch != b.patch) return a.patch < b.patch ? -1 : 1;
|
||
return 0;
|
||
}
|
||
|
||
std::string trim_ws(const std::string& text) {
|
||
const auto begin = text.find_first_not_of(" \t\r\n");
|
||
if (begin == std::string::npos) return std::string();
|
||
const auto end = text.find_last_not_of(" \t\r\n");
|
||
return text.substr(begin, end - begin + 1);
|
||
}
|
||
|
||
bool version_satisfies(const std::string& version, const std::string& range_text,
|
||
std::string* error) {
|
||
SemverTriple current;
|
||
if (!parse_semver_triple(version, ¤t)) {
|
||
if (error) *error = "invalid installed version '" + version + "'";
|
||
return false;
|
||
}
|
||
const std::string range = trim_ws(range_text);
|
||
if (range.empty() || range == "*") return true;
|
||
std::string remaining = range;
|
||
while (!remaining.empty()) {
|
||
const std::size_t space = remaining.find(' ');
|
||
const std::string token = trim_ws(remaining.substr(0, space));
|
||
remaining = space == std::string::npos ? std::string() : trim_ws(remaining.substr(space + 1));
|
||
if (token.empty()) continue;
|
||
std::string comparator;
|
||
std::string version_text = token;
|
||
for (const char* prefix : {">=", "<=", "==", ">", "<", "^", "~", "="}) {
|
||
const std::string p(prefix);
|
||
if (token.rfind(p, 0) == 0) {
|
||
comparator = p;
|
||
version_text = trim_ws(token.substr(p.size()));
|
||
break;
|
||
}
|
||
}
|
||
SemverTriple required;
|
||
if (!parse_semver_triple(version_text, &required)) {
|
||
if (error) *error = "invalid version range token '" + token + "'";
|
||
return false;
|
||
}
|
||
const int cmp = compare_semver(current, required);
|
||
bool ok = true;
|
||
if (comparator == ">=") ok = cmp >= 0;
|
||
else if (comparator == ">") ok = cmp > 0;
|
||
else if (comparator == "<=") ok = cmp <= 0;
|
||
else if (comparator == "<") ok = cmp < 0;
|
||
else if (comparator == "^") {
|
||
ok = current.major == required.major && cmp >= 0;
|
||
if (ok && required.major == 0) ok = current.minor == required.minor;
|
||
} else if (comparator == "~") {
|
||
ok = current.major == required.major && current.minor == required.minor && cmp >= 0;
|
||
} else {
|
||
ok = cmp == 0; // exact / "=" / "=="
|
||
}
|
||
if (!ok) return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
bool parse_requirements_json(const json& value, std::vector<VersionRequirement>* out,
|
||
std::string* error) {
|
||
out->clear();
|
||
if (!value.is_array()) {
|
||
if (error) *error = "'requires' must be an array";
|
||
return false;
|
||
}
|
||
for (const auto& item : value) {
|
||
if (!item.is_object()) {
|
||
if (error) *error = "'requires' entries must be objects";
|
||
return false;
|
||
}
|
||
VersionRequirement requirement;
|
||
if (!item.contains("id") || !item["id"].is_string() ||
|
||
!item.contains("version_range") || !item["version_range"].is_string()) {
|
||
if (error) *error = "'requires' entry requires string id/version_range";
|
||
return false;
|
||
}
|
||
requirement.id = item["id"].get<std::string>();
|
||
requirement.version_range = item["version_range"].get<std::string>();
|
||
if (requirement.id.empty()) {
|
||
if (error) *error = "'requires' entry id must not be empty";
|
||
return false;
|
||
}
|
||
std::string range_error;
|
||
// 语法预校验(任意版本上 unknown 也算错误)
|
||
if (!version_satisfies("0.0.0", requirement.version_range, &range_error)) {
|
||
// 0.0.0 不满足是正常的;只有解析错误才需要失败。重新以宽松方式检查:
|
||
SemverTriple dummy;
|
||
std::string token_text = requirement.version_range;
|
||
bool parsed_ok = true;
|
||
if (token_text != "*" && !token_text.empty()) {
|
||
const std::size_t space = token_text.find(' ');
|
||
std::string first = trim_ws(token_text.substr(0, space));
|
||
for (const char* prefix : {">=", "<=", "==", ">", "<", "^", "~", "="}) {
|
||
const std::string p(prefix);
|
||
if (first.rfind(p, 0) == 0) {
|
||
first = trim_ws(first.substr(p.size()));
|
||
break;
|
||
}
|
||
}
|
||
parsed_ok = parse_semver_triple(first, &dummy);
|
||
}
|
||
if (!parsed_ok) {
|
||
if (error) *error = "invalid version_range '" + requirement.version_range + "'";
|
||
return false;
|
||
}
|
||
}
|
||
out->push_back(std::move(requirement));
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/// 从插件清单文本读取 requires(可选;缺失=空)。失败返回 false。
|
||
bool parse_requirements_from_manifest_text(const std::string& manifest_text,
|
||
std::vector<VersionRequirement>* out,
|
||
std::string* error) {
|
||
out->clear();
|
||
json root;
|
||
try {
|
||
root = json::parse(manifest_text);
|
||
} catch (const std::exception& e) {
|
||
if (error) *error = std::string("manifest JSON parse failed: ") + e.what();
|
||
return false;
|
||
}
|
||
if (!root.is_object()) {
|
||
if (error) *error = "manifest root must be an object";
|
||
return false;
|
||
}
|
||
if (!root.contains("requires")) return true;
|
||
return parse_requirements_json(root["requires"], out, error);
|
||
}
|
||
|
||
std::string now_utc_iso8601() {
|
||
const std::time_t now = std::time(nullptr);
|
||
std::tm utc{};
|
||
#if defined(_WIN32)
|
||
gmtime_s(&utc, &now);
|
||
#else
|
||
gmtime_r(&now, &utc);
|
||
#endif
|
||
char buffer[32] = {};
|
||
std::strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%SZ", &utc);
|
||
return buffer;
|
||
}
|
||
|
||
bool write_text_file(const vde::fs::path& path, const std::string& text,
|
||
std::string* error) {
|
||
std::error_code ec;
|
||
if (path.has_parent_path()) vde::fs::create_directories(path.parent_path(), ec);
|
||
std::ofstream out(path.string(), std::ios::binary | std::ios::trunc);
|
||
if (!out) {
|
||
if (error) *error = "cannot write file: " + path.string();
|
||
return false;
|
||
}
|
||
out << text;
|
||
if (!out.good()) {
|
||
if (error) *error = "write failed: " + path.string();
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
bool parse_hex_seed(const std::string& argument, std::vector<uint8_t>* seed,
|
||
std::string* error) {
|
||
std::string text = argument;
|
||
std::error_code ec;
|
||
if (vde::fs::is_regular_file(text, ec)) {
|
||
const std::string file_text = read_file_text(text);
|
||
if (file_text.empty()) {
|
||
if (error) *error = "seed file is empty: " + text;
|
||
return false;
|
||
}
|
||
text = file_text;
|
||
}
|
||
// trim whitespace/newlines
|
||
const auto begin = text.find_first_not_of(" \t\r\n");
|
||
const auto end = text.find_last_not_of(" \t\r\n");
|
||
if (begin == std::string::npos) {
|
||
if (error) *error = "seed is empty";
|
||
return false;
|
||
}
|
||
text = text.substr(begin, end - begin + 1);
|
||
if (text.size() == 66 && (text.rfind("ed25519:", 0) == 0)) text = text.substr(8);
|
||
std::vector<uint8_t> bytes;
|
||
if (text.size() == 64) {
|
||
for (std::size_t i = 0; i + 1 < text.size(); i += 2) {
|
||
const std::string byte_text = text.substr(i, 2);
|
||
char* end_ptr = nullptr;
|
||
const long value = std::strtol(byte_text.c_str(), &end_ptr, 16);
|
||
if (end_ptr == byte_text.c_str() || *end_ptr != '\0') {
|
||
bytes.clear();
|
||
break;
|
||
}
|
||
bytes.push_back(static_cast<uint8_t>(value));
|
||
}
|
||
}
|
||
if (bytes.size() != 32) {
|
||
if (error) *error = "seed must be 32-byte hex (64 hex chars) or a file containing it";
|
||
return false;
|
||
}
|
||
*seed = std::move(bytes);
|
||
return true;
|
||
}
|
||
|
||
std::string hex_encode(const std::vector<uint8_t>& bytes) {
|
||
static const char* kHex = "0123456789abcdef";
|
||
std::string out;
|
||
out.reserve(bytes.size() * 2);
|
||
for (const uint8_t byte : bytes) {
|
||
out.push_back(kHex[(byte >> 4) & 0x0F]);
|
||
out.push_back(kHex[byte & 0x0F]);
|
||
}
|
||
return out;
|
||
}
|
||
|
||
bool verify_index_signature(const std::string& index_text,
|
||
const std::string& signature_text,
|
||
const std::vector<uint8_t>& public_key, std::string* error) {
|
||
std::string payload;
|
||
if (!vde::plugin::canonical_manifest_payload(index_text, &payload, error)) return false;
|
||
vde::plugin::PluginSignature signature;
|
||
if (!vde::plugin::parse_plugin_signature(signature_text, &signature, error)) return false;
|
||
if (!signature.present || signature.bytes.size() != 64) {
|
||
if (error) *error = "index signature must be ed25519:<base64(64B)>";
|
||
return false;
|
||
}
|
||
#if defined(VDE_TOOL_HAS_ED25519)
|
||
const std::vector<uint8_t> message(payload.begin(), payload.end());
|
||
if (!vde::license::crypto::ed25519_verify(public_key, message, signature.bytes)) {
|
||
if (error) *error = "index Ed25519 signature verification failed";
|
||
return false;
|
||
}
|
||
return true;
|
||
#else
|
||
(void)public_key;
|
||
if (error) *error = "tool built without vde_license Ed25519 support (crypto unavailable)";
|
||
return false;
|
||
#endif
|
||
}
|
||
|
||
/// 收集 package 目录下全部普通文件(相对路径 + SHA-256),按路径排序保证可重复。
|
||
bool collect_package_artifacts(const vde::fs::path& package_dir,
|
||
std::vector<IndexArtifact>* artifacts,
|
||
std::string* error) {
|
||
artifacts->clear();
|
||
std::error_code ec;
|
||
if (!vde::fs::is_directory(package_dir, ec)) {
|
||
if (error) *error = "not a directory: " + package_dir.string();
|
||
return false;
|
||
}
|
||
std::vector<vde::fs::path> files;
|
||
for (vde::fs::recursive_directory_iterator it(
|
||
package_dir, vde::fs::directory_options::skip_permission_denied, ec),
|
||
end;
|
||
it != end; it.increment(ec)) {
|
||
if (ec) break;
|
||
if (it->is_symlink(ec) || ec) continue;
|
||
if (!it->is_regular_file(ec) || ec) continue;
|
||
files.push_back(vde::fs::relative(it->path(), package_dir, ec));
|
||
if (ec) {
|
||
if (error) *error = "cannot compute relative path for " + it->path().string();
|
||
return false;
|
||
}
|
||
}
|
||
std::sort(files.begin(), files.end(),
|
||
[](const vde::fs::path& a, const vde::fs::path& b) {
|
||
return a.generic_string() < b.generic_string();
|
||
});
|
||
for (const auto& relative : files) {
|
||
const std::string relative_text = relative.generic_string();
|
||
std::string path_error;
|
||
if (!vde::plugin::is_artifact_path_safe(relative_text, &path_error)) {
|
||
if (error) *error = "unsafe package path '" + relative_text + "': " + path_error;
|
||
return false;
|
||
}
|
||
IndexArtifact artifact;
|
||
artifact.path = relative_text;
|
||
if (!vde::plugin::sha256_file((package_dir / relative).string(), &artifact.sha256,
|
||
error)) {
|
||
return false;
|
||
}
|
||
artifacts->push_back(std::move(artifact));
|
||
}
|
||
return !artifacts->empty();
|
||
}
|
||
|
||
bool find_manifest_files(const vde::fs::path& root,
|
||
std::vector<vde::fs::path>* manifests, std::string* error) {
|
||
manifests->clear();
|
||
std::error_code ec;
|
||
if (!vde::fs::is_directory(root, ec)) {
|
||
if (error) *error = "not a directory: " + root.string();
|
||
return false;
|
||
}
|
||
for (vde::fs::recursive_directory_iterator it(
|
||
root, vde::fs::directory_options::skip_permission_denied, ec),
|
||
end;
|
||
it != end; it.increment(ec)) {
|
||
if (ec) break;
|
||
if (it->is_symlink(ec) || ec) continue;
|
||
if (it->is_regular_file(ec) && it->path().filename() == "vde-plugin.json") {
|
||
manifests->push_back(it->path());
|
||
}
|
||
}
|
||
std::sort(manifests->begin(), manifests->end());
|
||
if (manifests->empty()) {
|
||
if (error) *error = "no vde-plugin.json found under " + root.string();
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/// 构建索引(不写文件):扫描 plugins_dir 下所有插件包,生成 index JSON 对象。
|
||
bool build_index_json(const vde::fs::path& plugins_dir,
|
||
const vde::fs::path& index_dir,
|
||
const std::string& publisher, const std::string& key_id,
|
||
const std::vector<uint8_t>* seed, json* index,
|
||
std::vector<IndexEntry>* entries, std::string* error) {
|
||
std::vector<vde::fs::path> manifests;
|
||
if (!find_manifest_files(plugins_dir, &manifests, error)) return false;
|
||
json plugins = json::array();
|
||
entries->clear();
|
||
for (const auto& manifest_path : manifests) {
|
||
vde::plugin::PluginManifest manifest;
|
||
std::string parse_error;
|
||
if (!vde::plugin::parse_plugin_manifest(read_file_text(manifest_path),
|
||
manifest_path.string(), &manifest,
|
||
&parse_error)) {
|
||
if (error) *error = "manifest parse failed (" + manifest_path.string() + "): " + parse_error;
|
||
return false;
|
||
}
|
||
const vde::fs::path package_dir = manifest_path.parent_path();
|
||
std::error_code ec;
|
||
const vde::fs::path relative =
|
||
vde::fs::relative(package_dir, index_dir, ec);
|
||
if (ec) {
|
||
if (error) *error = "cannot compute package path relative to index: " + package_dir.string();
|
||
return false;
|
||
}
|
||
const std::string package_path = relative.generic_string();
|
||
std::string path_error;
|
||
if (package_path.empty() || package_path == "." ||
|
||
!vde::plugin::is_artifact_path_safe(package_path, &path_error)) {
|
||
if (error) *error = "unsafe/invalid package path '" + package_path + "': " + path_error;
|
||
return false;
|
||
}
|
||
std::vector<IndexArtifact> artifacts;
|
||
if (!collect_package_artifacts(package_dir, &artifacts, error)) return false;
|
||
|
||
IndexEntry entry;
|
||
if (!parse_requirements_from_manifest_text(read_file_text(manifest_path),
|
||
&entry.dependencies, error)) {
|
||
if (error) *error = "requires parse failed (" + manifest_path.string() + "): " + *error;
|
||
return false;
|
||
}
|
||
entry.id = manifest.id;
|
||
entry.name = manifest.name;
|
||
entry.version = version_text(manifest.version);
|
||
entry.kind = vde::plugin::plugin_kind_name(manifest.kind);
|
||
entry.abi_major = manifest.abi_major;
|
||
entry.abi_minor = manifest.abi_minor;
|
||
entry.capabilities = manifest.capabilities;
|
||
entry.license = manifest.license;
|
||
entry.entry = manifest.entry;
|
||
entry.package_path = package_path;
|
||
entry.manifest_signature = manifest.signature;
|
||
for (const auto& artifact : artifacts) {
|
||
if (artifact.path == "vde-plugin.json") entry.manifest_sha256 = artifact.sha256;
|
||
}
|
||
entry.artifacts = std::move(artifacts);
|
||
entries->push_back(entry);
|
||
}
|
||
std::sort(entries->begin(), entries->end(),
|
||
[](const IndexEntry& a, const IndexEntry& b) {
|
||
if (a.id != b.id) return a.id < b.id;
|
||
return a.version < b.version;
|
||
});
|
||
for (const auto& entry : *entries) {
|
||
json plugin;
|
||
plugin["id"] = entry.id;
|
||
if (!entry.name.empty()) plugin["name"] = entry.name;
|
||
plugin["version"] = entry.version;
|
||
plugin["kind"] = entry.kind;
|
||
plugin["abi"] = {{"major", entry.abi_major}, {"minor", entry.abi_minor}};
|
||
if (!entry.capabilities.empty()) plugin["capabilities"] = entry.capabilities;
|
||
if (!entry.dependencies.empty()) {
|
||
json requires_json = json::array();
|
||
for (const auto& requirement : entry.dependencies) {
|
||
requires_json.push_back({{"id", requirement.id},
|
||
{"version_range", requirement.version_range}});
|
||
}
|
||
plugin["requires"] = std::move(requires_json);
|
||
}
|
||
if (!entry.license.empty()) plugin["license"] = entry.license;
|
||
plugin["entry"] = entry.entry;
|
||
plugin["path"] = entry.package_path;
|
||
if (!entry.manifest_sha256.empty())
|
||
plugin["manifest_sha256"] = entry.manifest_sha256;
|
||
if (!entry.manifest_signature.empty())
|
||
plugin["signature"] = entry.manifest_signature;
|
||
json artifacts = json::array();
|
||
for (const auto& artifact : entry.artifacts) {
|
||
artifacts.push_back({{"path", artifact.path}, {"sha256", artifact.sha256}});
|
||
}
|
||
plugin["artifacts"] = std::move(artifacts);
|
||
plugins.push_back(std::move(plugin));
|
||
}
|
||
json built;
|
||
built["schema_version"] = kIndexSchemaVersion;
|
||
built["generated_at"] = now_utc_iso8601();
|
||
built["generator"] = "vde_plugin_tool";
|
||
if (!publisher.empty()) built["publisher"] = publisher;
|
||
if (!key_id.empty()) built["key_id"] = key_id;
|
||
built["plugins"] = std::move(plugins);
|
||
if (seed) {
|
||
const std::string pre_signature = built.dump(2);
|
||
std::string payload;
|
||
if (!vde::plugin::canonical_manifest_payload(pre_signature, &payload, error)) return false;
|
||
#if defined(VDE_TOOL_HAS_ED25519)
|
||
std::vector<uint8_t> public_key;
|
||
if (!vde::license::crypto::ed25519_public_key(*seed, public_key)) {
|
||
if (error) *error = "cannot derive Ed25519 public key from seed";
|
||
return false;
|
||
}
|
||
const std::vector<uint8_t> message(payload.begin(), payload.end());
|
||
const std::vector<uint8_t> signature = vde::license::crypto::ed25519_sign(*seed, message);
|
||
if (signature.size() != 64) {
|
||
if (error) *error = "Ed25519 signing failed";
|
||
return false;
|
||
}
|
||
built["signature"] = "ed25519:" + vde::plugin::base64_encode(signature);
|
||
std::printf(" public key : ed25519:%s\n",
|
||
hex_encode(public_key).c_str());
|
||
#else
|
||
(void)payload;
|
||
if (error) *error = "tool built without vde_license Ed25519 support (cannot sign)";
|
||
return false;
|
||
#endif
|
||
}
|
||
*index = std::move(built);
|
||
return true;
|
||
}
|
||
|
||
int cmd_build_index(const std::string& plugins_dir_text, const std::string& output_text,
|
||
const std::string& seed_arg, const std::string& key_id,
|
||
const std::string& publisher) {
|
||
namespace fs = vde::fs;
|
||
const fs::path plugins_dir(plugins_dir_text);
|
||
const fs::path output_path(output_text);
|
||
std::error_code ec;
|
||
if (!fs::is_directory(plugins_dir, ec)) {
|
||
std::fprintf(stderr, "build-index: not a directory: '%s'\n", plugins_dir_text.c_str());
|
||
return 2;
|
||
}
|
||
fs::path index_dir = output_path.has_parent_path() ? output_path.parent_path() : fs::path(".");
|
||
if (index_dir.empty()) index_dir = ".";
|
||
vde::fs::create_directories(index_dir, ec);
|
||
index_dir = fs::weakly_canonical(index_dir, ec);
|
||
if (ec) {
|
||
std::fprintf(stderr, "build-index: cannot resolve output directory: %s\n",
|
||
ec.message().c_str());
|
||
return 3;
|
||
}
|
||
std::vector<uint8_t> seed;
|
||
const bool sign = !seed_arg.empty();
|
||
if (sign) {
|
||
std::string seed_error;
|
||
if (!parse_hex_seed(seed_arg, &seed, &seed_error)) {
|
||
std::fprintf(stderr, "build-index: invalid signing seed: %s\n", seed_error.c_str());
|
||
return 2;
|
||
}
|
||
}
|
||
json index;
|
||
std::vector<IndexEntry> entries;
|
||
std::string error;
|
||
std::printf("build-index: scan '%s' -> '%s'\n", plugins_dir_text.c_str(),
|
||
output_path.string().c_str());
|
||
if (!build_index_json(plugins_dir, index_dir, publisher, key_id, sign ? &seed : nullptr,
|
||
&index, &entries, &error)) {
|
||
std::fprintf(stderr, "build-index: FAILED: %s\n", error.c_str());
|
||
return 3;
|
||
}
|
||
const std::string text = index.dump(2) + "\n";
|
||
if (!write_text_file(output_path, text, &error)) {
|
||
std::fprintf(stderr, "build-index: FAILED: %s\n", error.c_str());
|
||
return 3;
|
||
}
|
||
std::printf(" plugins : %zu\n", entries.size());
|
||
for (const auto& entry : entries) {
|
||
std::printf(" %-32s %-10s abi %d.%d artifacts %zu\n", entry.id.c_str(),
|
||
entry.version.c_str(), entry.abi_major, entry.abi_minor,
|
||
entry.artifacts.size());
|
||
}
|
||
std::printf(" signature : %s\n", sign ? "ed25519 (signed)" : "(unsigned)");
|
||
std::printf("build-index: OK -> %s\n", output_path.string().c_str());
|
||
return 0;
|
||
}
|
||
|
||
/// 吊销 key_id 输入:支持每行一个 key_id(可含 # 注释)或 JSON {"revoked_key_ids":[...]}。
|
||
/// 语义:CLI/宿主策略文件是可信输入;不读取索引内自带的吊销列表(避免被签名者自证吊销)。
|
||
std::string trim_key_text(const std::string& text) {
|
||
const auto begin = text.find_first_not_of(" \t\r\n");
|
||
if (begin == std::string::npos) return std::string();
|
||
const auto end = text.find_last_not_of(" \t\r\n");
|
||
return text.substr(begin, end - begin + 1);
|
||
}
|
||
|
||
bool load_revoked_key_ids(const std::string& path, std::vector<std::string>* ids,
|
||
std::string* error) {
|
||
ids->clear();
|
||
const std::string raw = read_file_text(path);
|
||
if (raw.empty()) {
|
||
if (error) *error = "revoked keys file is empty or unreadable: " + path;
|
||
return false;
|
||
}
|
||
const std::string trimmed = trim_key_text(raw);
|
||
if (!trimmed.empty() && trimmed.front() == '{') {
|
||
json root;
|
||
try {
|
||
root = json::parse(trimmed);
|
||
} catch (const std::exception& e) {
|
||
if (error) *error = std::string("revoked keys JSON parse failed: ") + e.what();
|
||
return false;
|
||
}
|
||
if (!root.is_object() || !root.contains("revoked_key_ids") ||
|
||
!root["revoked_key_ids"].is_array()) {
|
||
if (error)
|
||
*error = "revoked keys JSON must be {\"revoked_key_ids\": [\"key-id\", ...]}";
|
||
return false;
|
||
}
|
||
for (const auto& item : root["revoked_key_ids"]) {
|
||
if (!item.is_string()) {
|
||
if (error) *error = "revoked keys JSON entries must be strings";
|
||
return false;
|
||
}
|
||
const std::string id = trim_key_text(item.get<std::string>());
|
||
if (!id.empty()) ids->push_back(id);
|
||
}
|
||
} else {
|
||
std::size_t start = 0;
|
||
while (start <= raw.size()) {
|
||
const std::size_t end = raw.find('\n', start);
|
||
std::string line = trim_key_text(raw.substr(start, end == std::string::npos
|
||
? std::string::npos
|
||
: end - start));
|
||
if (!line.empty() && line.front() != '#') {
|
||
// 一行也允许逗号分隔的多个 key_id
|
||
std::size_t part_start = 0;
|
||
while (part_start <= line.size()) {
|
||
const std::size_t part_end = line.find(',', part_start);
|
||
const std::string part = trim_key_text(
|
||
line.substr(part_start, part_end == std::string::npos
|
||
? std::string::npos
|
||
: part_end - part_start));
|
||
if (!part.empty() && part.front() != '#') ids->push_back(part);
|
||
if (part_end == std::string::npos) break;
|
||
part_start = part_end + 1;
|
||
}
|
||
}
|
||
if (end == std::string::npos) break;
|
||
start = end + 1;
|
||
}
|
||
}
|
||
// 去重(保持顺序)
|
||
std::vector<std::string> unique;
|
||
for (const auto& id : *ids) {
|
||
bool seen = false;
|
||
for (const auto& existing : unique) {
|
||
if (existing == id) {
|
||
seen = true;
|
||
break;
|
||
}
|
||
}
|
||
if (!seen) unique.push_back(id);
|
||
}
|
||
*ids = std::move(unique);
|
||
if (ids->empty()) {
|
||
if (error) *error = "revoked keys file contains no key_id: " + path;
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
bool key_id_revoked(const std::string& key_id, const std::vector<std::string>& revoked_ids) {
|
||
for (const auto& id : revoked_ids) {
|
||
if (id == key_id) return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/// 读取并校验 index(schema、路径安全、artifact hash、ABI 兼容、签名)。
|
||
/// 返回 0 成功 / 2 用法 / 3 校验失败 / 4 加密不可用(缺 pubkey 或 tool 无 license)。
|
||
int check_index_file(const std::string& index_text_path, const std::string& pubkey_arg,
|
||
bool allow_unsigned, bool check_artifact_files,
|
||
const std::vector<std::string>& revoked_key_ids,
|
||
IndexCheckResult* result, std::string* error) {
|
||
namespace fs = vde::fs;
|
||
const fs::path index_path(index_text_path);
|
||
std::error_code ec;
|
||
if (!fs::is_regular_file(index_path, ec)) {
|
||
if (error) *error = "index not found: " + index_text_path;
|
||
return 3;
|
||
}
|
||
const std::string index_text = read_file_text(index_path);
|
||
if (index_text.empty()) {
|
||
if (error) *error = "cannot read index: " + index_text_path;
|
||
return 3;
|
||
}
|
||
json root;
|
||
try {
|
||
root = json::parse(index_text);
|
||
} catch (const std::exception& e) {
|
||
if (error) *error = std::string("index JSON parse failed: ") + e.what();
|
||
return 3;
|
||
}
|
||
if (!root.is_object()) {
|
||
if (error) *error = "index root must be a JSON object";
|
||
return 3;
|
||
}
|
||
if (!root.contains("schema_version") || !root["schema_version"].is_number_integer() ||
|
||
root["schema_version"].get<int>() != kIndexSchemaVersion) {
|
||
if (error) *error = "unsupported/invalid schema_version (expected " +
|
||
std::to_string(kIndexSchemaVersion) + ")";
|
||
return 3;
|
||
}
|
||
if (!root.contains("plugins") || !root["plugins"].is_array()) {
|
||
if (error) *error = "index.plugins must be an array";
|
||
return 3;
|
||
}
|
||
const fs::path index_dir =
|
||
index_path.has_parent_path() ? index_path.parent_path() : fs::path(".");
|
||
|
||
IndexCheckResult checked;
|
||
if (root.contains("key_id") && root["key_id"].is_string())
|
||
checked.key_id = root["key_id"].get<std::string>();
|
||
if (root.contains("publisher") && root["publisher"].is_string())
|
||
checked.publisher = root["publisher"].get<std::string>();
|
||
if (root.contains("signature") && root["signature"].is_string())
|
||
checked.signature = root["signature"].get<std::string>();
|
||
checked.signed_index = !checked.signature.empty();
|
||
|
||
// 签名校验(先验签再信任内容;canonical payload 去掉顶层 signature)
|
||
if (checked.signed_index) {
|
||
if (pubkey_arg.empty()) {
|
||
if (error) *error = "index is signed; provide --pubkey <path|inline>";
|
||
return 4;
|
||
}
|
||
std::vector<uint8_t> public_key;
|
||
std::string key_error;
|
||
// --pubkey 支持文件路径或内联文本(与 verify 命令一致;P3-13b 回归顺带修正)。
|
||
std::string key_text = pubkey_arg;
|
||
const fs::path key_path(pubkey_arg);
|
||
std::error_code key_ec;
|
||
if (fs::is_regular_file(key_path, key_ec)) {
|
||
key_text = read_file_text(key_path);
|
||
if (key_text.empty()) {
|
||
if (error) *error = "invalid public key: cannot read " + pubkey_arg;
|
||
return 2;
|
||
}
|
||
}
|
||
if (!vde::plugin::parse_public_key_text(key_text, &public_key, &key_error)) {
|
||
if (error) *error = "invalid public key: " + key_error;
|
||
return 2;
|
||
}
|
||
std::string signature_error;
|
||
if (!verify_index_signature(index_text, checked.signature, public_key,
|
||
&signature_error)) {
|
||
if (error) *error = signature_error;
|
||
return signature_error.find("built without") != std::string::npos ? 4 : 3;
|
||
}
|
||
checked.signature_valid = true;
|
||
// P3-13b:索引验签通过后,按调用方提供的吊销列表强制拒绝已吊销签名 key_id。
|
||
// 吊销列表是 CLI/宿主策略的可信输入,不读取索引内自带列表(避免自证吊销)。
|
||
if (!checked.key_id.empty() &&
|
||
key_id_revoked(checked.key_id, revoked_key_ids)) {
|
||
if (error) {
|
||
*error = "index signing key_id '" + checked.key_id +
|
||
"' is revoked (see --revoked-keys)";
|
||
}
|
||
return 3;
|
||
}
|
||
} else if (!allow_unsigned) {
|
||
if (error) *error = "index is unsigned; use --allow-unsigned to accept";
|
||
return 3;
|
||
}
|
||
|
||
std::size_t plugin_index = 0;
|
||
for (const auto& plugin : root["plugins"]) {
|
||
++plugin_index;
|
||
if (!plugin.is_object()) {
|
||
if (error) *error = "plugins[" + std::to_string(plugin_index - 1) + "] must be an object";
|
||
return 3;
|
||
}
|
||
IndexEntry entry;
|
||
if (!plugin.contains("id") || !plugin["id"].is_string() ||
|
||
!plugin.contains("version") || !plugin["version"].is_string() ||
|
||
!plugin.contains("kind") || !plugin["kind"].is_string() ||
|
||
!plugin.contains("abi") || !plugin["abi"].is_object() ||
|
||
!plugin.contains("path") || !plugin["path"].is_string() ||
|
||
!plugin.contains("artifacts") || !plugin["artifacts"].is_array()) {
|
||
if (error) *error = "plugins[" + std::to_string(plugin_index - 1) +
|
||
"] missing required fields (id/version/kind/abi/path/artifacts)";
|
||
return 3;
|
||
}
|
||
entry.id = plugin["id"].get<std::string>();
|
||
entry.version = plugin["version"].get<std::string>();
|
||
entry.kind = plugin["kind"].get<std::string>();
|
||
if (!plugin["abi"].contains("major") || !plugin["abi"].contains("minor") ||
|
||
!plugin["abi"]["major"].is_number_integer() ||
|
||
!plugin["abi"]["minor"].is_number_integer()) {
|
||
if (error) *error = "abi must be {major:int, minor:int} for plugin " + entry.id;
|
||
return 3;
|
||
}
|
||
entry.abi_major = plugin["abi"]["major"].get<int>();
|
||
entry.abi_minor = plugin["abi"]["minor"].get<int>();
|
||
entry.package_path = plugin["path"].get<std::string>();
|
||
std::string path_error;
|
||
if (entry.id.empty() || entry.id.find('/') != std::string::npos ||
|
||
entry.id.find('\\') != std::string::npos) {
|
||
if (error) *error = "invalid plugin id (empty or contains path separator): '" + entry.id + "'";
|
||
return 3;
|
||
}
|
||
if (!vde::plugin::parse_semantic_version(entry.version).valid) {
|
||
if (error) *error = "invalid semantic version '" + entry.version + "' for " + entry.id;
|
||
return 3;
|
||
}
|
||
if (entry.package_path.empty() || entry.package_path == "." ||
|
||
!vde::plugin::is_artifact_path_safe(entry.package_path, &path_error)) {
|
||
if (error) *error = "unsafe package path '" + entry.package_path + "' for " +
|
||
entry.id + ": " + path_error;
|
||
return 3;
|
||
}
|
||
const bool abi_compatible = entry.abi_major == VDE_PLUGIN_ABI_MAJOR &&
|
||
entry.abi_minor <= VDE_PLUGIN_ABI_MINOR;
|
||
if (!abi_compatible) {
|
||
if (error) *error = "plugin " + entry.id + " ABI " +
|
||
std::to_string(entry.abi_major) + "." +
|
||
std::to_string(entry.abi_minor) +
|
||
" incompatible with host " +
|
||
std::to_string(VDE_PLUGIN_ABI_MAJOR) + "." +
|
||
std::to_string(VDE_PLUGIN_ABI_MINOR);
|
||
return 3;
|
||
}
|
||
if (plugin.contains("capabilities") && plugin["capabilities"].is_array()) {
|
||
for (const auto& cap : plugin["capabilities"])
|
||
if (cap.is_string()) entry.capabilities.push_back(cap.get<std::string>());
|
||
}
|
||
if (plugin.contains("entry") && plugin["entry"].is_string())
|
||
entry.entry = plugin["entry"].get<std::string>();
|
||
if (plugin.contains("requires")) {
|
||
if (!parse_requirements_json(plugin["requires"], &entry.dependencies, error)) return 3;
|
||
}
|
||
if (plugin.contains("manifest_sha256") && plugin["manifest_sha256"].is_string())
|
||
entry.manifest_sha256 = plugin["manifest_sha256"].get<std::string>();
|
||
const fs::path package_dir = index_dir / fs::path(entry.package_path);
|
||
std::vector<std::pair<std::string, std::string>> declared;
|
||
for (const auto& artifact : plugin["artifacts"]) {
|
||
if (!artifact.is_object() || !artifact.contains("path") ||
|
||
!artifact["path"].is_string() || !artifact.contains("sha256") ||
|
||
!artifact["sha256"].is_string()) {
|
||
if (error) *error = "invalid artifact entry for plugin " + entry.id;
|
||
return 3;
|
||
}
|
||
IndexArtifact item;
|
||
item.path = artifact["path"].get<std::string>();
|
||
item.sha256 = artifact["sha256"].get<std::string>();
|
||
if (artifact.contains("url") && artifact["url"].is_string())
|
||
item.url = artifact["url"].get<std::string>();
|
||
if (!vde::plugin::is_artifact_path_safe(item.path, &path_error)) {
|
||
if (error) *error = "unsafe artifact path '" + item.path + "' for " +
|
||
entry.id + ": " + path_error;
|
||
return 3;
|
||
}
|
||
declared.emplace_back(item.path, item.sha256);
|
||
entry.artifacts.push_back(std::move(item));
|
||
}
|
||
if (entry.artifacts.empty()) {
|
||
if (error) *error = "plugin " + entry.id + " declares no artifacts";
|
||
return 3;
|
||
}
|
||
if (check_artifact_files) {
|
||
for (const auto& [relative_path, expected_sha] : declared) {
|
||
const fs::path full = package_dir / fs::path(relative_path);
|
||
std::string actual_sha;
|
||
std::string file_error;
|
||
if (!vde::plugin::sha256_file(full.string(), &actual_sha, &file_error)) {
|
||
if (error) *error = "artifact missing for " + entry.id + ": " +
|
||
relative_path + " (" + file_error + ")";
|
||
return 3;
|
||
}
|
||
if (actual_sha != expected_sha) {
|
||
if (error) *error = "artifact hash mismatch for " + entry.id + ": " +
|
||
relative_path;
|
||
return 3;
|
||
}
|
||
}
|
||
}
|
||
checked.entries.push_back(std::move(entry));
|
||
}
|
||
// 空 plugins 数组合法(空仓库/占位索引);install 时再按 id 报 not found。
|
||
// 重复 id+version 拒绝(安装选择歧义)
|
||
for (std::size_t i = 0; i < checked.entries.size(); ++i) {
|
||
for (std::size_t j = i + 1; j < checked.entries.size(); ++j) {
|
||
if (checked.entries[i].id == checked.entries[j].id &&
|
||
checked.entries[i].version == checked.entries[j].version) {
|
||
if (error) *error = "duplicate plugin entry: " + checked.entries[i].id + " " +
|
||
checked.entries[i].version;
|
||
return 3;
|
||
}
|
||
}
|
||
}
|
||
*result = std::move(checked);
|
||
return 0;
|
||
}
|
||
|
||
void print_index_entry(const IndexEntry& entry) {
|
||
std::printf(" %-32s %-10s abi %d.%d %s (%zu artifacts)\n", entry.id.c_str(),
|
||
entry.version.c_str(), entry.abi_major, entry.abi_minor, entry.kind.c_str(),
|
||
entry.artifacts.size());
|
||
}
|
||
|
||
int cmd_verify_index(const std::string& index_path, const std::string& pubkey,
|
||
bool allow_unsigned,
|
||
const std::vector<std::string>& revoked_key_ids) {
|
||
IndexCheckResult result;
|
||
std::string error;
|
||
const int rc = check_index_file(index_path, pubkey, allow_unsigned,
|
||
/*check_artifact_files=*/true, revoked_key_ids, &result,
|
||
&error);
|
||
if (rc != 0) {
|
||
std::fprintf(stderr, "verify-index: FAILED: %s\n", error.c_str());
|
||
return rc;
|
||
}
|
||
std::printf("verify-index: %s\n", index_path.c_str());
|
||
std::printf(" schema : %d\n", kIndexSchemaVersion);
|
||
std::printf(" plugins : %zu\n", result.entries.size());
|
||
for (const auto& entry : result.entries) print_index_entry(entry);
|
||
if (result.signed_index) {
|
||
std::printf(" signature : ed25519 OK (key_id=%s, publisher=%s)\n",
|
||
result.key_id.empty() ? "-" : result.key_id.c_str(),
|
||
result.publisher.empty() ? "-" : result.publisher.c_str());
|
||
} else {
|
||
std::printf(" signature : none (accepted with --allow-unsigned)\n");
|
||
}
|
||
if (!revoked_key_ids.empty()) {
|
||
std::printf(" revoked check : %zu key_id(s) from --revoked-keys\n",
|
||
revoked_key_ids.size());
|
||
}
|
||
std::printf("verify-index: PASS\n");
|
||
return 0;
|
||
}
|
||
|
||
bool fetch_index_bytes(const std::string& location, std::string* bytes,
|
||
std::string* error);
|
||
|
||
// ── P3-17c:远程包获取 / 依赖解析 / 原子安装(install/upgrade/uninstall)──
|
||
|
||
struct InstalledPlugin {
|
||
std::string id;
|
||
std::string version;
|
||
std::vector<VersionRequirement> dependencies;
|
||
vde::fs::path path;
|
||
};
|
||
|
||
struct IndexSource {
|
||
bool remote = false;
|
||
std::string url_base; // remote: 索引所在目录 URL(含尾斜杠)
|
||
vde::fs::path local_dir; // 本地索引目录 / 远程索引临时目录
|
||
vde::fs::path index_file; // 已落盘的索引路径(本地或临时)
|
||
vde::fs::path temp_dir; // 远程索引临时目录(清理用)
|
||
};
|
||
|
||
std::string make_unique_suffix() {
|
||
return std::to_string(static_cast<long long>(
|
||
std::chrono::steady_clock::now().time_since_epoch().count()));
|
||
}
|
||
|
||
bool scan_installed_plugins(const vde::fs::path& dest,
|
||
std::map<std::string, InstalledPlugin>* out,
|
||
std::string* error) {
|
||
out->clear();
|
||
std::error_code ec;
|
||
if (!vde::fs::is_directory(dest, ec)) return true;
|
||
vde::fs::directory_iterator it(dest, ec);
|
||
const vde::fs::directory_iterator end;
|
||
while (!ec && it != end) {
|
||
std::error_code entry_ec;
|
||
if (it->is_directory(entry_ec) && !entry_ec) {
|
||
const auto manifest_path = it->path() / "vde-plugin.json";
|
||
std::error_code file_ec;
|
||
if (vde::fs::is_regular_file(manifest_path, file_ec) && !file_ec) {
|
||
const std::string text = read_file_text(manifest_path);
|
||
vde::plugin::PluginManifest manifest;
|
||
std::string parse_error;
|
||
if (vde::plugin::parse_plugin_manifest(text, manifest_path.string(), &manifest,
|
||
&parse_error)) {
|
||
InstalledPlugin installed;
|
||
installed.id = manifest.id;
|
||
installed.version = version_text(manifest.version);
|
||
installed.path = it->path();
|
||
std::string requires_error;
|
||
if (!parse_requirements_from_manifest_text(text, &installed.dependencies,
|
||
&requires_error)) {
|
||
if (error) *error = "installed manifest requires parse failed: " +
|
||
manifest_path.string() + ": " + requires_error;
|
||
return false;
|
||
}
|
||
(*out)[installed.id] = std::move(installed);
|
||
}
|
||
}
|
||
}
|
||
it.increment(ec);
|
||
}
|
||
return true;
|
||
}
|
||
|
||
bool is_remote_location(const std::string& location) {
|
||
return location.rfind("http://", 0) == 0 || location.rfind("https://", 0) == 0;
|
||
}
|
||
|
||
std::string url_directory(const std::string& url) {
|
||
const std::size_t slash = url.find_last_of('/');
|
||
if (slash == std::string::npos) return url + "/";
|
||
return url.substr(0, slash + 1);
|
||
}
|
||
|
||
/// 依据索引来源(URL/本地目录)解析 artifact 的实际获取位置。
|
||
std::string artifact_location(const IndexSource& source, const IndexEntry& entry,
|
||
const IndexArtifact& artifact) {
|
||
if (!artifact.url.empty()) {
|
||
if (is_remote_location(artifact.url) || artifact.url.rfind("file://", 0) == 0)
|
||
return artifact.url;
|
||
if (source.remote) return source.url_base + artifact.url;
|
||
return (source.local_dir / artifact.url).string();
|
||
}
|
||
const std::string relative =
|
||
entry.package_path == "." ? artifact.path : entry.package_path + "/" + artifact.path;
|
||
if (source.remote) return source.url_base + relative;
|
||
return (source.local_dir / relative).string();
|
||
}
|
||
|
||
bool read_location_bytes(const std::string& location, std::string* bytes, std::string* error) {
|
||
if (is_remote_location(location) || location.rfind("file://", 0) == 0) {
|
||
return fetch_index_bytes(location, bytes, error);
|
||
}
|
||
namespace fs = vde::fs;
|
||
std::error_code ec;
|
||
const fs::path path(location);
|
||
if (!fs::is_regular_file(path, ec) || ec) {
|
||
if (error) *error = "file not found: " + location;
|
||
return false;
|
||
}
|
||
*bytes = read_file_text(path);
|
||
return true;
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// P4-08:签名 CRL(key 吊销列表)解析/验签/过期检查
|
||
//
|
||
// CRL JSON:{"version":1, "issued_at":"YYYY-MM-DDTHH:MM:SSZ",
|
||
// "expires_at":"...", "revoked_key_ids":["key-id", ...],
|
||
// "signature":"ed25519:<base64(64B)>"}
|
||
// 签名载荷 = 去掉顶层 signature 后的 canonical JSON(与插件/索引签名同一规范:
|
||
// canonical_manifest_payload,对象键字典序、无空白)。
|
||
// 失败退出码:3 = 格式/过期/验签失败;4 = CRL 不可获取或加密能力不可用。
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
bool parse_iso8601_utc(const std::string& text, std::int64_t* out_epoch,
|
||
std::string* error) {
|
||
int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
|
||
char suffix = 0;
|
||
if (std::sscanf(text.c_str(), "%4d-%2d-%2dT%2d:%2d:%2d%c", &year, &month, &day,
|
||
&hour, &minute, &second, &suffix) != 7 ||
|
||
suffix != 'Z') {
|
||
if (error) *error = "timestamp must be ISO8601 UTC 'YYYY-MM-DDTHH:MM:SSZ': " + text;
|
||
return false;
|
||
}
|
||
if (month < 1 || month > 12 || day < 1 || day > 31 || hour < 0 || hour > 23 ||
|
||
minute < 0 || minute > 59 || second < 0 || second > 60) {
|
||
if (error) *error = "invalid timestamp fields: " + text;
|
||
return false;
|
||
}
|
||
// days_from_civil (Howard Hinnant)
|
||
const int y = year - (month <= 2 ? 1 : 0);
|
||
const int era = (y >= 0 ? y : y - 399) / 400;
|
||
const int yoe = y - era * 400;
|
||
const int doy = (153 * (month > 2 ? month - 3 : month + 9) + 2) / 5 + day - 1;
|
||
const int doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
|
||
std::int64_t epoch = static_cast<std::int64_t>(era) * 146097 + doe - 719468;
|
||
epoch = epoch * 86400 + hour * 3600 + minute * 60 + second;
|
||
*out_epoch = epoch;
|
||
return true;
|
||
}
|
||
|
||
struct CrlPayload {
|
||
int version = 0;
|
||
std::string issued_at;
|
||
std::string expires_at;
|
||
std::int64_t issued_epoch = 0;
|
||
std::int64_t expires_epoch = 0;
|
||
std::vector<std::string> revoked_key_ids;
|
||
std::string signature;
|
||
};
|
||
|
||
bool parse_crl_payload(const std::string& text, CrlPayload* out, bool require_signature,
|
||
std::string* error) {
|
||
if (out == nullptr) {
|
||
if (error) *error = "CRL parser: null output";
|
||
return false;
|
||
}
|
||
json root;
|
||
try {
|
||
root = json::parse(text);
|
||
} catch (const std::exception& e) {
|
||
if (error) *error = std::string("CRL JSON parse failed: ") + e.what();
|
||
return false;
|
||
}
|
||
if (!root.is_object()) {
|
||
if (error) *error = "CRL root must be an object";
|
||
return false;
|
||
}
|
||
if (!root.contains("version") || !root["version"].is_number_integer()) {
|
||
if (error) *error = "CRL requires integer version";
|
||
return false;
|
||
}
|
||
out->version = root["version"].get<int>();
|
||
if (out->version != 1) {
|
||
if (error) *error = "unsupported CRL version " + std::to_string(out->version) +
|
||
" (expected 1)";
|
||
return false;
|
||
}
|
||
if (!root.contains("issued_at") || !root["issued_at"].is_string() ||
|
||
!root.contains("expires_at") || !root["expires_at"].is_string()) {
|
||
if (error) *error = "CRL requires issued_at/expires_at strings";
|
||
return false;
|
||
}
|
||
out->issued_at = root["issued_at"].get<std::string>();
|
||
out->expires_at = root["expires_at"].get<std::string>();
|
||
std::string time_error;
|
||
if (!parse_iso8601_utc(out->issued_at, &out->issued_epoch, &time_error) ||
|
||
!parse_iso8601_utc(out->expires_at, &out->expires_epoch, &time_error)) {
|
||
if (error) *error = time_error;
|
||
return false;
|
||
}
|
||
if (out->expires_epoch <= out->issued_epoch) {
|
||
if (error) *error = "CRL expires_at must be after issued_at";
|
||
return false;
|
||
}
|
||
if (!root.contains("revoked_key_ids") || !root["revoked_key_ids"].is_array()) {
|
||
if (error) *error = "CRL requires revoked_key_ids array";
|
||
return false;
|
||
}
|
||
out->revoked_key_ids.clear();
|
||
for (const auto& item : root["revoked_key_ids"]) {
|
||
if (!item.is_string()) {
|
||
if (error) *error = "CRL revoked_key_ids entries must be strings";
|
||
return false;
|
||
}
|
||
const std::string id = item.get<std::string>();
|
||
if (id.empty()) {
|
||
if (error) *error = "CRL revoked_key_ids entries must be non-empty";
|
||
return false;
|
||
}
|
||
out->revoked_key_ids.push_back(id);
|
||
}
|
||
if (root.contains("signature")) {
|
||
if (!root["signature"].is_string()) {
|
||
if (error) *error = "CRL signature must be a string";
|
||
return false;
|
||
}
|
||
out->signature = root["signature"].get<std::string>();
|
||
}
|
||
if (require_signature && out->signature.empty()) {
|
||
if (error) *error = "CRL requires signature (ed25519:<base64(64B)>)";
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/// 读取/验签 CRL 并合并吊销 key_id。
|
||
/// 成功返回 true 并把 CRL 内 key_id 追加到 revoked_ids(去重)。
|
||
/// 失败返回 false 并设置 exit_code:3 格式/过期/验签失败;4 CRL/加密不可用。
|
||
bool load_crl_revoked_key_ids(const std::string& location, const std::string& pubkey_arg,
|
||
std::vector<std::string>* revoked_ids, int* exit_code,
|
||
std::string* error) {
|
||
const auto fail = [&](int code, const std::string& message) {
|
||
if (exit_code) *exit_code = code;
|
||
if (error) *error = message;
|
||
return false;
|
||
};
|
||
std::string text;
|
||
std::string read_error;
|
||
if (!read_location_bytes(location, &text, &read_error)) {
|
||
return fail(4, "CRL unavailable: " + read_error);
|
||
}
|
||
CrlPayload crl;
|
||
std::string parse_error;
|
||
if (!parse_crl_payload(text, &crl, /*require_signature=*/true, &parse_error)) {
|
||
return fail(3, "invalid CRL: " + parse_error);
|
||
}
|
||
#if defined(VDE_TOOL_HAS_ED25519)
|
||
if (pubkey_arg.empty()) {
|
||
return fail(4, "no CRL public key (use --crl-pubkey <path|inline> or --pubkey)");
|
||
}
|
||
std::string key_text;
|
||
std::error_code ec;
|
||
const vde::fs::path key_path(pubkey_arg);
|
||
if (vde::fs::is_regular_file(key_path, ec)) {
|
||
key_text = read_file_text(key_path);
|
||
} else {
|
||
key_text = pubkey_arg;
|
||
}
|
||
std::vector<uint8_t> public_key;
|
||
std::string key_error;
|
||
if (!vde::plugin::parse_public_key_text(key_text, &public_key, &key_error)) {
|
||
return fail(3, "invalid CRL public key: " + key_error);
|
||
}
|
||
std::string payload_text;
|
||
std::string payload_error;
|
||
if (!vde::plugin::canonical_manifest_payload(text, &payload_text, &payload_error)) {
|
||
return fail(3, "CRL canonical payload failed: " + payload_error);
|
||
}
|
||
vde::plugin::PluginSignature signature;
|
||
std::string signature_error;
|
||
if (!vde::plugin::parse_plugin_signature(crl.signature, &signature,
|
||
&signature_error)) {
|
||
return fail(3, "CRL signature invalid: " + signature_error);
|
||
}
|
||
if (!signature.present || signature.bytes.size() != 64) {
|
||
return fail(3, "CRL signature must be ed25519:<base64(64B)>");
|
||
}
|
||
const std::vector<uint8_t> message(payload_text.begin(), payload_text.end());
|
||
if (!vde::license::crypto::ed25519_verify(public_key, message, signature.bytes)) {
|
||
return fail(3, "CRL Ed25519 signature verification failed (tampered or wrong key)");
|
||
}
|
||
#else
|
||
(void)pubkey_arg;
|
||
return fail(4, "tool built without vde_license Ed25519 support (crypto unavailable)");
|
||
#endif
|
||
const std::int64_t now = static_cast<std::int64_t>(std::time(nullptr));
|
||
if (now < crl.issued_epoch) {
|
||
return fail(3, "CRL not yet valid (issued_at=" + crl.issued_at + ")");
|
||
}
|
||
if (now > crl.expires_epoch) {
|
||
return fail(3, "CRL expired (expires_at=" + crl.expires_at + ")");
|
||
}
|
||
if (revoked_ids != nullptr) {
|
||
for (const auto& id : crl.revoked_key_ids) {
|
||
if (std::find(revoked_ids->begin(), revoked_ids->end(), id) ==
|
||
revoked_ids->end()) {
|
||
revoked_ids->push_back(id);
|
||
}
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
bool verify_and_write_artifact(const std::string& location, const IndexArtifact& artifact,
|
||
const vde::fs::path& target_dir, std::string* error) {
|
||
std::string bytes;
|
||
std::string read_error;
|
||
if (!read_location_bytes(location, &bytes, &read_error)) {
|
||
if (error) *error = "fetch failed for " + artifact.path + ": " + read_error;
|
||
return false;
|
||
}
|
||
std::string expected = artifact.sha256;
|
||
std::transform(expected.begin(), expected.end(), expected.begin(),
|
||
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
|
||
std::string actual = vde::plugin::sha256_hex(bytes);
|
||
std::transform(actual.begin(), actual.end(), actual.begin(),
|
||
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
|
||
if (actual != expected) {
|
||
if (error) *error = "SHA-256 mismatch for artifact '" + artifact.path + "' (expected " +
|
||
artifact.sha256 + ", actual " + actual + ")";
|
||
return false;
|
||
}
|
||
const vde::fs::path target = target_dir / artifact.path;
|
||
std::error_code ec;
|
||
if (target.has_parent_path()) vde::fs::create_directories(target.parent_path(), ec);
|
||
if (ec || !write_text_file(target, bytes, error)) {
|
||
if (error && error->empty()) *error = "cannot write artifact: " + target.string();
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/// 把计划中的包下载/复制到 staging_root/<id>/ 并逐个校验哈希。
|
||
bool stage_packages(const IndexSource& source, const std::vector<const IndexEntry*>& plan,
|
||
const vde::fs::path& staging_root,
|
||
std::map<std::string, vde::fs::path>* staged, std::string* error) {
|
||
std::error_code ec;
|
||
for (const IndexEntry* entry : plan) {
|
||
const vde::fs::path package_dir = staging_root / entry->id;
|
||
vde::fs::create_directories(package_dir, ec);
|
||
if (ec) {
|
||
if (error) *error = "cannot create staging dir: " + package_dir.string();
|
||
return false;
|
||
}
|
||
for (const auto& artifact : entry->artifacts) {
|
||
if (!verify_and_write_artifact(artifact_location(source, *entry, artifact), artifact,
|
||
package_dir, error)) {
|
||
if (error) *error = "package '" + entry->id + "': " + *error;
|
||
return false;
|
||
}
|
||
}
|
||
(*staged)[entry->id] = package_dir;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/// 计算依赖闭包:installed 满足的依赖跳过;否则从 index 选最高满足版本(postorder = 依赖先装)。
|
||
bool resolve_dependency_plan(const std::vector<IndexEntry>& entries, const IndexEntry* root_entry,
|
||
const std::map<std::string, InstalledPlugin>& installed,
|
||
bool force_root_install,
|
||
std::vector<const IndexEntry*>* plan_order, std::string* error) {
|
||
std::map<std::string, int> color; // 0=white 1=gray 2=black
|
||
std::map<std::string, std::vector<std::string>> constraints;
|
||
std::map<std::string, const IndexEntry*> selected;
|
||
std::map<std::string, bool> installed_satisfied;
|
||
plan_order->clear();
|
||
int recursion_guard = 0;
|
||
|
||
const auto select_from_index = [&](const std::string& id,
|
||
const std::vector<std::string>& ranges) -> const IndexEntry* {
|
||
const IndexEntry* best = nullptr;
|
||
for (const auto& candidate : entries) {
|
||
if (candidate.id != id) continue;
|
||
bool satisfies_all = true;
|
||
for (const auto& range : ranges) {
|
||
std::string range_error;
|
||
if (!version_satisfies(candidate.version, range, &range_error)) {
|
||
satisfies_all = false;
|
||
break;
|
||
}
|
||
}
|
||
if (!satisfies_all) continue;
|
||
if (best == nullptr) {
|
||
best = &candidate;
|
||
continue;
|
||
}
|
||
const auto best_version = vde::plugin::parse_semantic_version(best->version);
|
||
const auto candidate_version = vde::plugin::parse_semantic_version(candidate.version);
|
||
const bool newer = candidate_version.major != best_version.major
|
||
? candidate_version.major > best_version.major
|
||
: (candidate_version.minor != best_version.minor
|
||
? candidate_version.minor > best_version.minor
|
||
: candidate_version.patch > best_version.patch);
|
||
if (newer) best = &candidate;
|
||
}
|
||
return best;
|
||
};
|
||
|
||
std::function<bool(const std::string&, const std::string&, bool)> visit;
|
||
visit = [&](const std::string& id, const std::string& range, bool force_install) -> bool {
|
||
if (++recursion_guard > 4096) {
|
||
if (error) *error = "dependency resolution recursion limit exceeded";
|
||
return false;
|
||
}
|
||
if (!range.empty()) constraints[id].push_back(range);
|
||
int& state = color[id];
|
||
if (state == 1) {
|
||
if (error) *error = "dependency cycle detected involving '" + id + "'";
|
||
return false;
|
||
}
|
||
// 重新选择候选(可能因新增约束而变化)
|
||
const IndexEntry* candidate = nullptr;
|
||
bool satisfied_by_installed = false;
|
||
if (root_entry != nullptr && id == root_entry->id) {
|
||
const auto within = std::find_if(entries.begin(), entries.end(),
|
||
[&](const IndexEntry& entry) {
|
||
return entry.id == root_entry->id &&
|
||
entry.version == root_entry->version;
|
||
});
|
||
candidate = within == entries.end() ? root_entry : &(*within);
|
||
} else {
|
||
const auto installed_it = installed.find(id);
|
||
if (installed_it != installed.end()) {
|
||
bool installed_ok = true;
|
||
for (const auto& required_range : constraints[id]) {
|
||
std::string range_error;
|
||
if (!version_satisfies(installed_it->second.version, required_range,
|
||
&range_error)) {
|
||
installed_ok = false;
|
||
break;
|
||
}
|
||
}
|
||
if (installed_ok) {
|
||
satisfied_by_installed = true;
|
||
installed_satisfied[id] = true;
|
||
}
|
||
}
|
||
if (!satisfied_by_installed)
|
||
candidate = select_from_index(id, constraints[id]);
|
||
}
|
||
if (satisfied_by_installed) {
|
||
// 已安装包自身的 requires 仍需满足(可能装缺失依赖)
|
||
const auto installed_it = installed.find(id);
|
||
for (const auto& requirement : installed_it->second.dependencies) {
|
||
if (!visit(requirement.id, requirement.version_range, false)) return false;
|
||
}
|
||
color[id] = 2;
|
||
return true;
|
||
}
|
||
if (candidate == nullptr) {
|
||
if (error) {
|
||
std::string ranges_text;
|
||
for (const auto& required_range : constraints[id]) {
|
||
if (!ranges_text.empty()) ranges_text += ", ";
|
||
ranges_text += required_range;
|
||
}
|
||
*error = "unresolved dependency '" + id + "' (required range: " +
|
||
(ranges_text.empty() ? "*" : ranges_text) +
|
||
"); not installed and no matching package in index";
|
||
}
|
||
return false;
|
||
}
|
||
const bool revalidate = (state == 2);
|
||
if (revalidate && selected[id] == candidate) {
|
||
return true; // 约束仍满足
|
||
}
|
||
selected[id] = candidate;
|
||
color[id] = 1;
|
||
for (const auto& requirement : candidate->dependencies) {
|
||
if (!visit(requirement.id, requirement.version_range, false)) return false;
|
||
}
|
||
color[id] = 2;
|
||
if (std::find(plan_order->begin(), plan_order->end(), candidate) == plan_order->end())
|
||
plan_order->push_back(candidate);
|
||
return true;
|
||
};
|
||
|
||
return visit(root_entry->id, std::string(), force_root_install);
|
||
}
|
||
|
||
struct CommittedPackage {
|
||
vde::fs::path target;
|
||
vde::fs::path backup;
|
||
bool backup_created = false;
|
||
};
|
||
|
||
bool commit_staged_package(const vde::fs::path& staged, const vde::fs::path& target,
|
||
bool allow_replace, CommittedPackage* committed,
|
||
std::string* error) {
|
||
namespace fs = vde::fs;
|
||
std::error_code ec;
|
||
committed->target = target;
|
||
if (fs::exists(target, ec)) {
|
||
if (!allow_replace) {
|
||
if (error) *error = "destination exists: " + target.string();
|
||
return false;
|
||
}
|
||
committed->backup = target.parent_path() /
|
||
(target.filename().string() + ".vde-bak-" + make_unique_suffix());
|
||
fs::rename(target, committed->backup, ec);
|
||
if (ec) {
|
||
if (error) *error = "cannot backup existing package: " + ec.message();
|
||
return false;
|
||
}
|
||
committed->backup_created = true;
|
||
}
|
||
fs::create_directories(target.parent_path(), ec);
|
||
fs::rename(staged, target, ec);
|
||
if (ec) {
|
||
if (error) *error = "cannot move staged package into place: " + ec.message();
|
||
if (committed->backup_created) {
|
||
std::error_code restore_ec;
|
||
fs::rename(committed->backup, target, restore_ec);
|
||
committed->backup_created = false;
|
||
}
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
void rollback_commits(const std::vector<CommittedPackage>& committed) {
|
||
namespace fs = vde::fs;
|
||
for (auto it = committed.rbegin(); it != committed.rend(); ++it) {
|
||
std::error_code ec;
|
||
if (it->backup_created) {
|
||
fs::remove_all(it->target, ec);
|
||
fs::rename(it->backup, it->target, ec);
|
||
} else {
|
||
fs::remove_all(it->target, ec);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// 准备/校验索引来源(本地文件或 http(s) URL;远程索引落盘到临时文件)。
|
||
bool prepare_index_source(const std::string& target_text, bool fetch_packages,
|
||
IndexSource* source, IndexCheckResult* result,
|
||
const std::string& pubkey, bool allow_unsigned,
|
||
const std::vector<std::string>& revoked_key_ids,
|
||
std::string* error) {
|
||
namespace fs = vde::fs;
|
||
source->remote = is_remote_location(target_text);
|
||
source->temp_dir.clear();
|
||
std::error_code ec;
|
||
if (source->remote) {
|
||
if (!fetch_packages) {
|
||
if (error) *error = "remote index URL requires --fetch-packages";
|
||
return false;
|
||
}
|
||
std::string bytes;
|
||
if (!fetch_index_bytes(target_text, &bytes, error)) return false;
|
||
const fs::path temp_dir =
|
||
fs::temp_directory_path() / ("vde_plugin_tool_index_" + make_unique_suffix());
|
||
fs::create_directories(temp_dir, ec);
|
||
if (ec) {
|
||
if (error) *error = "cannot create temp dir for index: " + ec.message();
|
||
return false;
|
||
}
|
||
const fs::path index_file = temp_dir / "index.json";
|
||
if (!write_text_file(index_file, bytes, error)) {
|
||
fs::remove_all(temp_dir, ec);
|
||
return false;
|
||
}
|
||
source->temp_dir = temp_dir;
|
||
source->index_file = index_file;
|
||
source->local_dir = temp_dir;
|
||
source->url_base = url_directory(target_text);
|
||
} else {
|
||
std::string path = target_text;
|
||
if (path.rfind("file://", 0) == 0) path = path.substr(7);
|
||
const fs::path index_path(path);
|
||
if (!fs::is_regular_file(index_path, ec) || ec) {
|
||
if (error) *error = "index/dir not found: " + target_text;
|
||
return false;
|
||
}
|
||
source->index_file = index_path;
|
||
source->local_dir = index_path.has_parent_path() ? index_path.parent_path() : fs::path(".");
|
||
}
|
||
const int rc = check_index_file(source->index_file.string(), pubkey, allow_unsigned,
|
||
/*check_artifact_files=*/!fetch_packages, revoked_key_ids,
|
||
result, error);
|
||
return rc == 0;
|
||
}
|
||
|
||
bool install_plan_from_index(const IndexSource& source, const IndexCheckResult& index,
|
||
const std::string& dest_text, const std::string& plugin_id,
|
||
const std::string& version, bool force, bool fetch_packages,
|
||
const std::string& root_id_override, bool upgrade_mode,
|
||
std::string* error) {
|
||
namespace fs = vde::fs;
|
||
const IndexEntry* root = nullptr;
|
||
for (const auto& entry : index.entries) {
|
||
if (!root_id_override.empty() ? entry.id != root_id_override
|
||
: (!plugin_id.empty() && entry.id != plugin_id))
|
||
continue;
|
||
if (!version.empty() && entry.version != version) continue;
|
||
if (root == nullptr) {
|
||
root = &entry;
|
||
continue;
|
||
}
|
||
const auto current = vde::plugin::parse_semantic_version(root->version);
|
||
const auto candidate = vde::plugin::parse_semantic_version(entry.version);
|
||
const bool newer = candidate.major != current.major
|
||
? candidate.major > current.major
|
||
: (candidate.minor != current.minor ? candidate.minor > current.minor
|
||
: candidate.patch > current.patch);
|
||
if (newer) root = &entry;
|
||
}
|
||
if (root == nullptr) {
|
||
if (error) *error = "plugin not found in index: id='" + plugin_id + "' version='" + version + "'";
|
||
return false;
|
||
}
|
||
const fs::path dest(dest_text);
|
||
std::error_code ec;
|
||
fs::create_directories(dest, ec);
|
||
if (ec) {
|
||
if (error) *error = "cannot create destination: " + ec.message();
|
||
return false;
|
||
}
|
||
std::map<std::string, InstalledPlugin> installed;
|
||
if (!scan_installed_plugins(dest, &installed, error)) return false;
|
||
if (upgrade_mode) {
|
||
const auto installed_it = installed.find(root->id);
|
||
if (installed_it == installed.end()) {
|
||
if (error) *error = "upgrade: plugin not installed: " + root->id;
|
||
return false;
|
||
}
|
||
if (installed_it->second.version == root->version) {
|
||
if (error) *error = "upgrade: version " + root->version + " already installed for " + root->id;
|
||
return false;
|
||
}
|
||
}
|
||
std::vector<const IndexEntry*> plan_order;
|
||
if (!resolve_dependency_plan(index.entries, root, installed, upgrade_mode, &plan_order, error))
|
||
return false;
|
||
// root 必须在计划中
|
||
if (std::find(plan_order.begin(), plan_order.end(), root) == plan_order.end())
|
||
plan_order.push_back(root);
|
||
|
||
const fs::path staging_root = dest / (".vde-staging-" + make_unique_suffix());
|
||
fs::create_directories(staging_root, ec);
|
||
if (ec) {
|
||
if (error) *error = "cannot create staging dir: " + ec.message();
|
||
return false;
|
||
}
|
||
std::map<std::string, fs::path> staged;
|
||
bool staged_ok = false;
|
||
if (fetch_packages) {
|
||
staged_ok = stage_packages(source, plan_order, staging_root, &staged, error);
|
||
} else {
|
||
// 本地模式:从索引目录复制(原子安装仍走 staging)
|
||
staged_ok = true;
|
||
for (const IndexEntry* entry : plan_order) {
|
||
const fs::path package_dir = staging_root / entry->id;
|
||
fs::create_directories(package_dir, ec);
|
||
for (const auto& artifact : entry->artifacts) {
|
||
const fs::path from = source.local_dir / entry->package_path / artifact.path;
|
||
std::string bytes;
|
||
std::string read_error;
|
||
if (!read_location_bytes(from.string(), &bytes, &read_error)) {
|
||
if (error) *error = "local artifact missing for '" + entry->id + "': " +
|
||
from.string() + " (" + read_error +
|
||
"); use --fetch-packages for remote packages";
|
||
staged_ok = false;
|
||
break;
|
||
}
|
||
if (!verify_and_write_artifact(from.string(), artifact, package_dir, error)) {
|
||
staged_ok = false;
|
||
break;
|
||
}
|
||
}
|
||
if (!staged_ok) break;
|
||
staged[entry->id] = package_dir;
|
||
}
|
||
}
|
||
if (!staged_ok) {
|
||
fs::remove_all(staging_root, ec);
|
||
return false;
|
||
}
|
||
// 提交:依赖先装;root 在 upgrade 模式下允许替换并保留旧版备份
|
||
std::vector<CommittedPackage> committed;
|
||
bool ok = true;
|
||
for (const IndexEntry* entry : plan_order) {
|
||
const bool is_root = (entry == root);
|
||
const auto staged_it = staged.find(entry->id);
|
||
if (staged_it == staged.end()) continue;
|
||
if (upgrade_mode && !is_root) {
|
||
// upgrade 时已安装且满足的依赖不会出现;缺失依赖正常安装
|
||
}
|
||
CommittedPackage package;
|
||
const bool allow_replace = force || (upgrade_mode && is_root);
|
||
if (!commit_staged_package(staged_it->second, dest / entry->id, allow_replace, &package,
|
||
error)) {
|
||
ok = false;
|
||
break;
|
||
}
|
||
committed.push_back(std::move(package));
|
||
}
|
||
fs::remove_all(staging_root, ec);
|
||
if (!ok) {
|
||
rollback_commits(committed);
|
||
return false;
|
||
}
|
||
for (const auto& package : committed) {
|
||
if (package.backup_created) {
|
||
std::error_code cleanup_ec;
|
||
fs::remove_all(package.backup, cleanup_ec);
|
||
}
|
||
}
|
||
std::printf("install: %s %s -> %s (%zu package(s)%s)\n", root->id.c_str(),
|
||
root->version.c_str(), (dest / root->id).string().c_str(), plan_order.size(),
|
||
upgrade_mode ? ", upgrade" : "");
|
||
for (const auto& entry : plan_order)
|
||
std::printf(" package : %s %s\n", entry->id.c_str(), entry->version.c_str());
|
||
return true;
|
||
}
|
||
|
||
int cmd_uninstall(const std::string& plugin_id, const std::string& dest_text) {
|
||
namespace fs = vde::fs;
|
||
if (plugin_id.empty() || dest_text.empty()) {
|
||
std::fprintf(stderr, "uninstall requires <plugin-id> and --dest <dir>\n");
|
||
return 2;
|
||
}
|
||
if (plugin_id.find('/') != std::string::npos || plugin_id.find('\\') != std::string::npos ||
|
||
plugin_id == "." || plugin_id == "..") {
|
||
std::fprintf(stderr, "uninstall: invalid plugin id '%s'\n", plugin_id.c_str());
|
||
return 3;
|
||
}
|
||
const fs::path dest(dest_text);
|
||
const fs::path target = dest / plugin_id;
|
||
std::error_code ec;
|
||
const fs::path canonical_dest = fs::weakly_canonical(dest, ec);
|
||
if (ec || !fs::is_directory(dest, ec)) {
|
||
std::fprintf(stderr, "uninstall: destination not found: %s\n", dest_text.c_str());
|
||
return 3;
|
||
}
|
||
const fs::path canonical_target = fs::weakly_canonical(target, ec);
|
||
if (ec) {
|
||
std::fprintf(stderr, "uninstall: plugin not installed: %s\n", plugin_id.c_str());
|
||
return 3;
|
||
}
|
||
const std::string dest_text_canonical = canonical_dest.string();
|
||
const std::string target_text_canonical = canonical_target.string();
|
||
if (target_text_canonical.size() <= dest_text_canonical.size() ||
|
||
target_text_canonical.rfind(dest_text_canonical, 0) != 0) {
|
||
std::fprintf(stderr, "uninstall: refused path outside destination\n");
|
||
return 3;
|
||
}
|
||
std::error_code parent_ec;
|
||
if (fs::weakly_canonical(target.parent_path(), parent_ec) != canonical_dest) {
|
||
std::fprintf(stderr, "uninstall: refused path outside install root\n");
|
||
return 3;
|
||
}
|
||
if (!fs::is_regular_file(target / "vde-plugin.json", ec) || ec) {
|
||
std::fprintf(stderr, "uninstall: not an installed plugin dir: %s\n", target.string().c_str());
|
||
return 3;
|
||
}
|
||
// id 匹配:目录内 manifest 的 id 必须等于请求 id,防止误删
|
||
{
|
||
const std::string manifest_text = read_file_text(target / "vde-plugin.json");
|
||
vde::plugin::PluginManifest installed;
|
||
std::string manifest_error;
|
||
if (manifest_text.empty() ||
|
||
!vde::plugin::parse_plugin_manifest(manifest_text,
|
||
(target / "vde-plugin.json").string(),
|
||
&installed, &manifest_error)) {
|
||
std::fprintf(stderr, "uninstall: invalid installed manifest: %s\n",
|
||
manifest_error.c_str());
|
||
return 3;
|
||
}
|
||
if (installed.id != plugin_id) {
|
||
std::fprintf(stderr, "uninstall: id mismatch: requested '%s' but directory manifest is '%s'\n",
|
||
plugin_id.c_str(), installed.id.c_str());
|
||
return 3;
|
||
}
|
||
}
|
||
fs::remove_all(target, ec);
|
||
if (ec) {
|
||
std::fprintf(stderr, "uninstall: remove failed: %s\n", ec.message().c_str());
|
||
return 3;
|
||
}
|
||
std::printf("uninstall: removed %s\n", target.string().c_str());
|
||
return 0;
|
||
}
|
||
|
||
/// P4-48:把已验证的 .vdeplugin 归档安全解包到 dest(逐文件 sha256 + 路径逃逸校验)。
|
||
bool extract_package_files(const std::string& archive,
|
||
const std::vector<PackageEntry>& entries,
|
||
const vde::fs::path& dest, std::string* error) {
|
||
namespace fs = vde::fs;
|
||
std::error_code ec;
|
||
fs::create_directories(dest, ec);
|
||
if (ec) {
|
||
if (error) *error = "cannot create dir " + dest.string() + ": " + ec.message();
|
||
return false;
|
||
}
|
||
const fs::path canonical_dest = fs::weakly_canonical(dest, ec);
|
||
if (ec) {
|
||
if (error) *error = "cannot canonicalize " + dest.string();
|
||
return false;
|
||
}
|
||
const std::size_t payload = find_package_payload_offset(archive);
|
||
if (payload == std::string::npos) {
|
||
if (error) *error = "archive payload offset invalid";
|
||
return false;
|
||
}
|
||
std::size_t pos = payload;
|
||
for (const auto& entry : entries) {
|
||
if (pos + entry.size > archive.size()) {
|
||
if (error) *error = "archive truncated payload " + entry.path;
|
||
return false;
|
||
}
|
||
const std::string bytes = archive.substr(pos, static_cast<std::size_t>(entry.size));
|
||
pos += static_cast<std::size_t>(entry.size);
|
||
if (vde::plugin::sha256_hex(bytes) != entry.sha256) {
|
||
if (error) *error = "archive sha256 mismatch: " + entry.path;
|
||
return false;
|
||
}
|
||
const fs::path target = dest / entry.path;
|
||
const fs::path canonical_target = fs::weakly_canonical(target, ec);
|
||
if (ec) {
|
||
if (error) *error = "cannot canonicalize package path " + entry.path;
|
||
return false;
|
||
}
|
||
const fs::path relative = fs::relative(canonical_target, canonical_dest, ec);
|
||
if (ec || relative.empty() || relative.generic_string().rfind("..", 0) == 0) {
|
||
if (error) *error = "refused package path escape: " + entry.path;
|
||
return false;
|
||
}
|
||
if (!write_text_file(target, bytes, error)) return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/// 读取并完整校验 .vdeplugin(magic/metadata/FILE 头/payload sha256)。
|
||
bool load_verified_package(const std::string& path, nlohmann::json* metadata,
|
||
std::string* archive_out,
|
||
std::vector<PackageEntry>* entries, std::string* error) {
|
||
*archive_out = read_file_text(path);
|
||
if (archive_out->empty()) {
|
||
if (error) *error = "cannot read package: " + path;
|
||
return false;
|
||
}
|
||
if (!parse_package_bytes(*archive_out, metadata, entries, error)) return false;
|
||
const std::size_t payload = find_package_payload_offset(*archive_out);
|
||
if (payload == std::string::npos) {
|
||
if (error) *error = "package payload offset invalid";
|
||
return false;
|
||
}
|
||
std::size_t pos = payload;
|
||
for (const auto& entry : *entries) {
|
||
if (pos + entry.size > archive_out->size()) {
|
||
if (error) *error = "package truncated payload: " + entry.path;
|
||
return false;
|
||
}
|
||
const std::string bytes =
|
||
archive_out->substr(pos, static_cast<std::size_t>(entry.size));
|
||
pos += static_cast<std::size_t>(entry.size);
|
||
if (vde::plugin::sha256_hex(bytes) != entry.sha256) {
|
||
if (error) *error = "package sha256 mismatch: " + entry.path;
|
||
return false;
|
||
}
|
||
}
|
||
if (!metadata->contains("id") || !(*metadata)["id"].is_string() ||
|
||
!metadata->contains("version") || !(*metadata)["version"].is_string()) {
|
||
if (error) *error = "package metadata missing id/version";
|
||
return false;
|
||
}
|
||
bool has_manifest = false;
|
||
for (const auto& entry : *entries)
|
||
if (entry.path == "vde-plugin.json") has_manifest = true;
|
||
if (!has_manifest) {
|
||
if (error) *error = "package missing vde-plugin.json";
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
bool safe_plugin_id(const std::string& id) {
|
||
if (id.empty() || id == "." || id == "..") return false;
|
||
if (id.find('/') != std::string::npos || id.find('\\') != std::string::npos)
|
||
return false;
|
||
if (id[0] == '.') return false;
|
||
return true;
|
||
}
|
||
|
||
int cmd_upgrade_package(const std::string& package_path, const std::string& root_text,
|
||
bool force, bool rollback_mode) {
|
||
namespace fs = vde::fs;
|
||
if (package_path.empty() || root_text.empty()) {
|
||
std::fprintf(stderr,
|
||
"upgrade requires <file.vdeplugin> and --root <install_root>\n");
|
||
return 2;
|
||
}
|
||
nlohmann::json metadata;
|
||
std::string archive;
|
||
std::vector<PackageEntry> entries;
|
||
std::string error;
|
||
if (!load_verified_package(package_path, &metadata, &archive, &entries, &error)) {
|
||
std::fprintf(stderr, "upgrade: package verification FAILED: %s\n", error.c_str());
|
||
return 3;
|
||
}
|
||
const std::string plugin_id = metadata["id"].get<std::string>();
|
||
const std::string package_version = metadata["version"].get<std::string>();
|
||
SemverTriple package_semver;
|
||
if (!safe_plugin_id(plugin_id)) {
|
||
std::fprintf(stderr, "upgrade: unsafe package id '%s'\n", plugin_id.c_str());
|
||
return 3;
|
||
}
|
||
if (!parse_semver_triple(package_version, &package_semver)) {
|
||
std::fprintf(stderr, "upgrade: invalid package version '%s'\n",
|
||
package_version.c_str());
|
||
return 3;
|
||
}
|
||
std::error_code ec;
|
||
const fs::path root(root_text);
|
||
fs::create_directories(root, ec);
|
||
if (ec || !fs::is_directory(root, ec)) {
|
||
std::fprintf(stderr, "upgrade: cannot create/access root '%s'\n",
|
||
root_text.c_str());
|
||
return 3;
|
||
}
|
||
const fs::path canonical_root = fs::weakly_canonical(root, ec);
|
||
if (ec) {
|
||
std::fprintf(stderr, "upgrade: cannot canonicalize root\n");
|
||
return 3;
|
||
}
|
||
const fs::path target = root / plugin_id;
|
||
if (fs::weakly_canonical(target.parent_path(), ec) != canonical_root) {
|
||
std::fprintf(stderr, "upgrade: refused path outside install root\n");
|
||
return 3;
|
||
}
|
||
|
||
// --rollback:恢复最近一次 <root>/<id>.vde-bak-* 备份。
|
||
if (rollback_mode) {
|
||
std::vector<fs::path> backups;
|
||
fs::directory_iterator it(root, fs::directory_options::skip_permission_denied, ec);
|
||
const fs::directory_iterator end;
|
||
while (!ec && it != end) {
|
||
std::error_code entry_ec;
|
||
const std::string name = it->path().filename().string();
|
||
if (it->is_directory(entry_ec) && !entry_ec &&
|
||
name.rfind(plugin_id + ".vde-bak-", 0) == 0) {
|
||
backups.push_back(it->path());
|
||
}
|
||
it.increment(ec);
|
||
}
|
||
std::sort(backups.begin(), backups.end());
|
||
if (backups.empty()) {
|
||
std::fprintf(stderr, "upgrade --rollback: no backup found for '%s'\n",
|
||
plugin_id.c_str());
|
||
return 3;
|
||
}
|
||
const fs::path backup = backups.back();
|
||
const fs::path old_current =
|
||
root / (plugin_id + ".vde-old-" + make_unique_suffix());
|
||
bool had_current = false;
|
||
if (fs::exists(target, ec)) {
|
||
fs::rename(target, old_current, ec);
|
||
if (ec) {
|
||
std::fprintf(stderr, "upgrade --rollback: cannot move current version: %s\n",
|
||
ec.message().c_str());
|
||
return 3;
|
||
}
|
||
had_current = true;
|
||
}
|
||
fs::rename(backup, target, ec);
|
||
if (ec) {
|
||
if (had_current) {
|
||
std::error_code restore_ec;
|
||
fs::rename(old_current, target, restore_ec);
|
||
}
|
||
std::fprintf(stderr, "upgrade --rollback: cannot restore backup: %s\n",
|
||
ec.message().c_str());
|
||
return 3;
|
||
}
|
||
if (had_current) fs::remove_all(old_current, ec);
|
||
std::printf("upgrade --rollback: restored %s from %s\n", plugin_id.c_str(),
|
||
backup.filename().string().c_str());
|
||
return 0;
|
||
}
|
||
|
||
// 读取已安装版本并做 semver 门控:默认只允许严格更高版本。
|
||
std::string installed_version;
|
||
const fs::path installed_manifest = target / "vde-plugin.json";
|
||
if (fs::exists(target, ec)) {
|
||
if (!fs::is_directory(target, ec) ||
|
||
!fs::is_regular_file(installed_manifest, ec)) {
|
||
std::fprintf(stderr, "upgrade: target exists but is not an installed plugin: %s\n",
|
||
target.string().c_str());
|
||
return 3;
|
||
}
|
||
vde::plugin::PluginManifest installed;
|
||
std::string manifest_error;
|
||
if (!vde::plugin::parse_plugin_manifest(read_file_text(installed_manifest),
|
||
installed_manifest.string(), &installed,
|
||
&manifest_error)) {
|
||
std::fprintf(stderr, "upgrade: installed manifest invalid: %s\n",
|
||
manifest_error.c_str());
|
||
return 3;
|
||
}
|
||
if (installed.id != plugin_id) {
|
||
std::fprintf(stderr,
|
||
"upgrade: installed id mismatch: requested '%s' but manifest is '%s'\n",
|
||
plugin_id.c_str(), installed.id.c_str());
|
||
return 3;
|
||
}
|
||
installed_version = version_text(installed.version);
|
||
SemverTriple installed_semver;
|
||
if (!parse_semver_triple(installed_version, &installed_semver)) {
|
||
std::fprintf(stderr, "upgrade: installed version invalid '%s'\n",
|
||
installed_version.c_str());
|
||
return 3;
|
||
}
|
||
const int comparison = compare_semver(package_semver, installed_semver);
|
||
if (comparison < 0) {
|
||
if (!force) {
|
||
std::fprintf(stderr,
|
||
"upgrade: downgrade refused (installed %s, package %s; use --force)\n",
|
||
installed_version.c_str(), package_version.c_str());
|
||
return 3;
|
||
}
|
||
} else if (comparison == 0 && !force) {
|
||
std::fprintf(stderr,
|
||
"upgrade: already installed same version %s (use --force to reinstall)\n",
|
||
installed_version.c_str());
|
||
return 3;
|
||
}
|
||
}
|
||
|
||
// stage 解包到 root 下临时目录,校验 manifest 后原子备份/替换。
|
||
const fs::path stage =
|
||
root / (".vde-stage-" + plugin_id + "-" + make_unique_suffix());
|
||
const auto cleanup_stage = [&]() {
|
||
std::error_code removal_ec;
|
||
fs::remove_all(stage, removal_ec);
|
||
};
|
||
if (!extract_package_files(archive, entries, stage, &error)) {
|
||
std::fprintf(stderr, "upgrade: stage extraction FAILED: %s\n", error.c_str());
|
||
cleanup_stage();
|
||
return 3;
|
||
}
|
||
vde::plugin::PluginManifest staged;
|
||
if (!vde::plugin::parse_plugin_manifest(read_file_text(stage / "vde-plugin.json"),
|
||
(stage / "vde-plugin.json").string(), &staged,
|
||
&error)) {
|
||
std::fprintf(stderr, "upgrade: staged manifest invalid: %s\n", error.c_str());
|
||
cleanup_stage();
|
||
return 3;
|
||
}
|
||
if (staged.id != plugin_id || version_text(staged.version) != package_version ||
|
||
staged.entry.empty()) {
|
||
std::fprintf(stderr,
|
||
"upgrade: staged manifest id/version/entry mismatch (metadata %s/%s, "
|
||
"manifest %s/%s)\n",
|
||
plugin_id.c_str(), package_version.c_str(), staged.id.c_str(),
|
||
version_text(staged.version).c_str());
|
||
cleanup_stage();
|
||
return 3;
|
||
}
|
||
std::string path_error;
|
||
if (!vde::plugin::is_artifact_path_safe(staged.entry, &path_error) ||
|
||
!fs::is_regular_file(stage / staged.entry, ec)) {
|
||
std::fprintf(stderr, "upgrade: staged entry missing/unsafe: %s\n",
|
||
staged.entry.c_str());
|
||
cleanup_stage();
|
||
return 3;
|
||
}
|
||
|
||
CommittedPackage committed;
|
||
if (!commit_staged_package(stage, target, /*allow_replace=*/true, &committed,
|
||
&error)) {
|
||
cleanup_stage();
|
||
std::fprintf(stderr, "upgrade: install FAILED (old version restored): %s\n",
|
||
error.c_str());
|
||
return 3;
|
||
}
|
||
if (committed.backup_created) {
|
||
std::error_code cleanup_ec;
|
||
fs::remove_all(committed.backup, cleanup_ec);
|
||
}
|
||
std::printf("upgrade: %s %s -> %s\n", plugin_id.c_str(),
|
||
installed_version.empty() ? "(not installed)" : installed_version.c_str(),
|
||
package_version.c_str());
|
||
std::printf("upgrade: PASS\n");
|
||
return 0;
|
||
}
|
||
|
||
int cmd_upgrade(const std::string& target_text, const std::string& dest_text,
|
||
const std::string& plugin_id, const std::string& version,
|
||
const std::string& pubkey, bool allow_unsigned, bool force,
|
||
bool fetch_packages, const std::vector<std::string>& revoked_key_ids) {
|
||
if (plugin_id.empty() || dest_text.empty()) {
|
||
std::fprintf(stderr, "upgrade requires --id <plugin-id> and --dest <dir>\n");
|
||
return 2;
|
||
}
|
||
IndexSource source;
|
||
IndexCheckResult result;
|
||
std::string error;
|
||
if (!prepare_index_source(target_text, fetch_packages, &source, &result, pubkey,
|
||
allow_unsigned, revoked_key_ids, &error)) {
|
||
std::fprintf(stderr, "upgrade: index verification FAILED: %s\n", error.c_str());
|
||
if (!source.temp_dir.empty()) vde::fs::remove_all(source.temp_dir);
|
||
return 3;
|
||
}
|
||
const bool ok = install_plan_from_index(source, result, dest_text, plugin_id, version, force,
|
||
fetch_packages, plugin_id, /*upgrade_mode=*/true,
|
||
&error);
|
||
std::error_code ec;
|
||
if (!source.temp_dir.empty()) vde::fs::remove_all(source.temp_dir, ec);
|
||
if (!ok) {
|
||
std::fprintf(stderr, "upgrade: FAILED: %s\n", error.c_str());
|
||
return 3;
|
||
}
|
||
std::printf("upgrade: OK\n");
|
||
return 0;
|
||
}
|
||
|
||
int install_local_dir(const std::string& source_text, const std::string& dest_text,
|
||
bool force) {
|
||
namespace fs = vde::fs;
|
||
const fs::path source(source_text);
|
||
std::error_code ec;
|
||
if (!fs::is_directory(source, ec)) {
|
||
std::fprintf(stderr, "install: not a plugin directory: '%s'\n", source_text.c_str());
|
||
return 2;
|
||
}
|
||
vde::plugin::PluginManifest manifest;
|
||
std::string error;
|
||
if (!vde::plugin::parse_plugin_manifest(read_file_text(source / "vde-plugin.json"),
|
||
(source / "vde-plugin.json").string(), &manifest,
|
||
&error)) {
|
||
std::fprintf(stderr, "install: plugin manifest invalid: %s\n", error.c_str());
|
||
return 3;
|
||
}
|
||
std::vector<IndexArtifact> artifacts;
|
||
if (!collect_package_artifacts(source, &artifacts, &error)) {
|
||
std::fprintf(stderr, "install: package scan failed: %s\n", error.c_str());
|
||
return 3;
|
||
}
|
||
const fs::path dest(dest_text);
|
||
const fs::path target = dest / manifest.id;
|
||
if (fs::exists(target, ec) && !force) {
|
||
std::fprintf(stderr, "install: destination exists (use --force): %s\n",
|
||
target.string().c_str());
|
||
return 3;
|
||
}
|
||
if (force) vde::fs::remove_all(target, ec);
|
||
vde::fs::create_directories(target, ec);
|
||
if (ec) {
|
||
std::fprintf(stderr, "install: cannot create %s: %s\n", target.string().c_str(),
|
||
ec.message().c_str());
|
||
return 3;
|
||
}
|
||
for (const auto& artifact : artifacts) {
|
||
const fs::path from = source / artifact.path;
|
||
const fs::path to = target / artifact.path;
|
||
if (to.has_parent_path()) vde::fs::create_directories(to.parent_path(), ec);
|
||
vde::fs::copy_file(from, to, vde::fs::copy_options::overwrite_existing,
|
||
ec);
|
||
if (ec) {
|
||
std::fprintf(stderr, "install: copy failed %s: %s\n", from.string().c_str(),
|
||
ec.message().c_str());
|
||
vde::fs::remove_all(target, ec);
|
||
return 3;
|
||
}
|
||
}
|
||
std::printf("install: local dir '%s' -> '%s' (%zu files)\n", source_text.c_str(),
|
||
target.string().c_str(), artifacts.size());
|
||
std::printf("install: OK (unsigned local directory; use index for signature/artifacts)\n");
|
||
return 0;
|
||
}
|
||
|
||
int cmd_install(const std::string& target_text, const std::string& dest_text,
|
||
const std::string& plugin_id, const std::string& version,
|
||
const std::string& pubkey, bool allow_unsigned, bool force,
|
||
bool fetch_packages, const std::vector<std::string>& revoked_key_ids) {
|
||
namespace fs = vde::fs;
|
||
std::error_code ec;
|
||
if (dest_text.empty()) {
|
||
std::fprintf(stderr, "install: --dest is required\n");
|
||
return 2;
|
||
}
|
||
const fs::path target(target_text);
|
||
if (!is_remote_location(target_text) && fs::is_directory(target, ec))
|
||
return install_local_dir(target_text, dest_text, force);
|
||
IndexSource source;
|
||
IndexCheckResult result;
|
||
std::string error;
|
||
if (!prepare_index_source(target_text, fetch_packages, &source, &result, pubkey,
|
||
allow_unsigned, revoked_key_ids, &error)) {
|
||
std::fprintf(stderr, "install: index verification FAILED: %s\n", error.c_str());
|
||
if (!source.temp_dir.empty()) vde::fs::remove_all(source.temp_dir);
|
||
return 3;
|
||
}
|
||
const bool ok = install_plan_from_index(source, result, dest_text, plugin_id, version, force,
|
||
fetch_packages, std::string(),
|
||
/*upgrade_mode=*/false, &error);
|
||
if (!source.temp_dir.empty()) vde::fs::remove_all(source.temp_dir, ec);
|
||
if (!ok) {
|
||
std::fprintf(stderr, "install: FAILED: %s\n", error.c_str());
|
||
return 3;
|
||
}
|
||
std::printf("install: OK\n");
|
||
return 0;
|
||
}
|
||
|
||
int cmd_gen_keypair(const std::string& out_dir_text) {
|
||
namespace fs = vde::fs;
|
||
#if defined(VDE_TOOL_HAS_ED25519)
|
||
std::vector<uint8_t> seed(32);
|
||
std::random_device device;
|
||
for (auto& byte : seed) byte = static_cast<uint8_t>(device() & 0xFF);
|
||
std::vector<uint8_t> public_key;
|
||
if (!vde::license::crypto::ed25519_public_key(seed, public_key)) {
|
||
std::fprintf(stderr, "gen-keypair: cannot derive public key\n");
|
||
return 3;
|
||
}
|
||
const fs::path out_dir = out_dir_text.empty() ? fs::path(".") : fs::path(out_dir_text);
|
||
std::error_code ec;
|
||
vde::fs::create_directories(out_dir, ec);
|
||
std::string error;
|
||
if (!write_text_file(out_dir / "plugin_signing.key", hex_encode(seed) + "\n", &error) ||
|
||
!write_text_file(out_dir / "plugin_signing.pub", hex_encode(public_key) + "\n",
|
||
&error)) {
|
||
std::fprintf(stderr, "gen-keypair: %s\n", error.c_str());
|
||
return 3;
|
||
}
|
||
std::printf("gen-keypair: seed -> %s\n", (out_dir / "plugin_signing.key").string().c_str());
|
||
std::printf("gen-keypair: pubkey-> %s\n", (out_dir / "plugin_signing.pub").string().c_str());
|
||
std::printf(" public key : ed25519:%s\n", hex_encode(public_key).c_str());
|
||
std::printf(" seed (hex) : %s\n", hex_encode(seed).c_str());
|
||
std::printf("gen-keypair: OK (keep the seed private; distribute only the public key)\n");
|
||
return 0;
|
||
#else
|
||
(void)out_dir_text;
|
||
std::fprintf(stderr,
|
||
"gen-keypair: tool built without vde_license Ed25519 support (crypto unavailable)\n");
|
||
return 4;
|
||
#endif
|
||
}
|
||
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// P3-17:scaffold(模板生成工程)+ 远程索引 fetch
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
/// P4-08:给 CRL payload 签名(发布方使用;输出带 signature 的 CRL JSON)。
|
||
int cmd_sign_crl(const std::string& payload_path, const std::string& output_path,
|
||
const std::string& seed_arg) {
|
||
const std::string payload_text = read_file_text(vde::fs::path(payload_path));
|
||
if (payload_text.empty()) {
|
||
std::fprintf(stderr, "sign-crl: cannot read payload: %s\n", payload_path.c_str());
|
||
return 3;
|
||
}
|
||
CrlPayload crl;
|
||
std::string parse_error;
|
||
if (!parse_crl_payload(payload_text, &crl, /*require_signature=*/false, &parse_error)) {
|
||
std::fprintf(stderr, "sign-crl: invalid CRL payload: %s\n", parse_error.c_str());
|
||
return 3;
|
||
}
|
||
#if defined(VDE_TOOL_HAS_ED25519)
|
||
std::vector<uint8_t> seed;
|
||
std::string seed_error;
|
||
if (!parse_hex_seed(seed_arg, &seed, &seed_error)) {
|
||
std::fprintf(stderr, "sign-crl: invalid --sign: %s\n", seed_error.c_str());
|
||
return 2;
|
||
}
|
||
std::string canonical;
|
||
std::string canonical_error;
|
||
if (!vde::plugin::canonical_manifest_payload(payload_text, &canonical,
|
||
&canonical_error)) {
|
||
std::fprintf(stderr, "sign-crl: canonical payload failed: %s\n",
|
||
canonical_error.c_str());
|
||
return 3;
|
||
}
|
||
const std::vector<uint8_t> message(canonical.begin(), canonical.end());
|
||
const std::vector<uint8_t> signature = vde::license::crypto::ed25519_sign(seed, message);
|
||
if (signature.size() != 64) {
|
||
std::fprintf(stderr, "sign-crl: Ed25519 signing failed\n");
|
||
return 4;
|
||
}
|
||
json root = json::parse(payload_text);
|
||
root["signature"] = "ed25519:" + vde::plugin::base64_encode(signature);
|
||
std::string write_error;
|
||
if (!write_text_file(vde::fs::path(output_path), root.dump(2) + "\n",
|
||
&write_error)) {
|
||
std::fprintf(stderr, "sign-crl: %s\n", write_error.c_str());
|
||
return 3;
|
||
}
|
||
std::vector<uint8_t> public_key;
|
||
if (!vde::license::crypto::ed25519_public_key(seed, public_key)) {
|
||
std::fprintf(stderr, "sign-crl: cannot derive public key\n");
|
||
return 4;
|
||
}
|
||
std::printf("sign-crl: wrote %s (version=%d, revoked=%zu, expires_at=%s)\n",
|
||
output_path.c_str(), crl.version, crl.revoked_key_ids.size(),
|
||
crl.expires_at.c_str());
|
||
std::printf(" public key : ed25519:%s\n", hex_encode(public_key).c_str());
|
||
return 0;
|
||
#else
|
||
(void)seed_arg;
|
||
(void)output_path;
|
||
std::fprintf(stderr,
|
||
"sign-crl: tool built without vde_license Ed25519 support (crypto unavailable)\n");
|
||
return 4;
|
||
#endif
|
||
}
|
||
|
||
std::string replace_all(std::string text, const std::string& from, const std::string& to) {
|
||
if (from.empty()) return text;
|
||
std::size_t position = 0;
|
||
while ((position = text.find(from, position)) != std::string::npos) {
|
||
text.replace(position, from.size(), to);
|
||
position += to.size();
|
||
}
|
||
return text;
|
||
}
|
||
|
||
std::string sanitize_slug(const std::string& text) {
|
||
std::string slug;
|
||
for (const char c : text) {
|
||
if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
|
||
slug.push_back(c);
|
||
} else if (c >= 'A' && c <= 'Z') {
|
||
slug.push_back(static_cast<char>(c - 'A' + 'a'));
|
||
} else if (c == '.' || c == '_' || c == '-') {
|
||
slug.push_back('_');
|
||
}
|
||
}
|
||
if (slug.empty()) slug = "plugin";
|
||
if (!(slug[0] >= 'a' && slug[0] <= 'z')) slug = "p_" + slug;
|
||
return slug;
|
||
}
|
||
|
||
std::string uppercase_ascii(std::string text) {
|
||
for (char& c : text) {
|
||
if (c >= 'a' && c <= 'z') c = static_cast<char>(c - 'a' + 'A');
|
||
}
|
||
return text;
|
||
}
|
||
|
||
enum class ScaffoldKind { Format, Cae, Cam, Gcs };
|
||
|
||
struct ScaffoldSpec {
|
||
const char* directory;
|
||
const char* template_id;
|
||
const char* target_token;
|
||
const char* display_name;
|
||
};
|
||
|
||
bool parse_scaffold_kind(const std::string& text, ScaffoldKind* kind,
|
||
const ScaffoldSpec** spec) {
|
||
static const ScaffoldSpec kFormat{"format_plugin", "com.example.vde.format",
|
||
"vde_template_format", "VDE Format Plugin Template"};
|
||
static const ScaffoldSpec kCae{"cae_solver", "com.example.vde.cae",
|
||
"vde_template_cae", "VDE CAE Solver Template"};
|
||
static const ScaffoldSpec kCam{"cam_strategy", "com.example.vde.cam",
|
||
"vde_template_cam", "VDE CAM Strategy Template"};
|
||
static const ScaffoldSpec kGcs{"gcs_solver", "com.example.vde.gcs",
|
||
"vde_template_gcs", "VDE GCS Solver Template"};
|
||
if (text == "format" || text == "format_plugin" || text == "fmt") {
|
||
*kind = ScaffoldKind::Format;
|
||
*spec = &kFormat;
|
||
return true;
|
||
}
|
||
if (text == "cae" || text == "cae_solver") {
|
||
*kind = ScaffoldKind::Cae;
|
||
*spec = &kCae;
|
||
return true;
|
||
}
|
||
if (text == "cam" || text == "cam_strategy") {
|
||
*kind = ScaffoldKind::Cam;
|
||
*spec = &kCam;
|
||
return true;
|
||
}
|
||
if (text == "gcs" || text == "gcs_solver") {
|
||
*kind = ScaffoldKind::Gcs;
|
||
*spec = &kGcs;
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
bool copy_template_tree(const vde::fs::path& source,
|
||
const vde::fs::path& target, bool force,
|
||
std::string* error) {
|
||
std::error_code ec;
|
||
for (vde::fs::recursive_directory_iterator it(
|
||
source, vde::fs::directory_options::skip_permission_denied, ec),
|
||
end;
|
||
it != end; it.increment(ec)) {
|
||
if (ec) {
|
||
if (error) *error = "cannot read template: " + ec.message();
|
||
return false;
|
||
}
|
||
const vde::fs::path relative =
|
||
vde::fs::relative(it->path(), source, ec);
|
||
if (ec) {
|
||
if (error) *error = "cannot compute template relative path";
|
||
return false;
|
||
}
|
||
const vde::fs::path destination = target / relative;
|
||
if (it->is_directory(ec)) {
|
||
vde::fs::create_directories(destination, ec);
|
||
if (ec) {
|
||
if (error) *error = "cannot create " + destination.string() + ": " + ec.message();
|
||
return false;
|
||
}
|
||
continue;
|
||
}
|
||
if (!it->is_regular_file(ec) || ec) continue;
|
||
if (vde::fs::exists(destination, ec) && !force) {
|
||
if (error) *error = "target file exists (use --force): " + destination.string();
|
||
return false;
|
||
}
|
||
if (destination.has_parent_path())
|
||
vde::fs::create_directories(destination.parent_path(), ec);
|
||
vde::fs::copy_file(it->path(), destination,
|
||
vde::fs::copy_options::overwrite_existing, ec);
|
||
if (ec) {
|
||
if (error) *error = "cannot copy " + it->path().string() + ": " + ec.message();
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
std::vector<std::pair<std::string, std::string>> scaffold_replacements(
|
||
ScaffoldKind kind, const ScaffoldSpec& spec, const std::string& id,
|
||
const std::string& target, const std::string& slug, const std::string& display) {
|
||
std::vector<std::pair<std::string, std::string>> replacements;
|
||
replacements.emplace_back(spec.template_id, id);
|
||
replacements.emplace_back(spec.target_token, target);
|
||
replacements.emplace_back(spec.display_name, display);
|
||
switch (kind) {
|
||
case ScaffoldKind::Format: {
|
||
const std::string upper = uppercase_ascii(slug);
|
||
replacements.emplace_back("VDE-EXAMPLE", "VDE-" + upper);
|
||
replacements.emplace_back(".example", "." + slug);
|
||
replacements.emplace_back("kFormatId = \"example\"", "kFormatId = \"" + slug + "\"");
|
||
replacements.emplace_back("format:example.", "format:" + slug + ".");
|
||
break;
|
||
}
|
||
case ScaffoldKind::Cae:
|
||
replacements.emplace_back("cae_solver:example.linear_static",
|
||
"cae_solver:" + slug + ".linear_static");
|
||
replacements.emplace_back("example.linear_static", slug + ".linear_static");
|
||
break;
|
||
case ScaffoldKind::Cam:
|
||
replacements.emplace_back("cam_strategy:example.contour",
|
||
"cam_strategy:" + slug + ".contour");
|
||
replacements.emplace_back("example.contour", slug + ".contour");
|
||
break;
|
||
case ScaffoldKind::Gcs:
|
||
replacements.emplace_back("example.gcs.2d", slug + ".gcs.2d");
|
||
break;
|
||
}
|
||
return replacements;
|
||
}
|
||
|
||
bool rewrite_scaffold_manifest(const vde::fs::path& manifest_path,
|
||
const ScaffoldSpec& spec, const std::string& id,
|
||
const std::string& display, const std::string& target,
|
||
const std::string& slug, std::string* error) {
|
||
json manifest;
|
||
try {
|
||
manifest = json::parse(read_file_text(manifest_path));
|
||
} catch (const std::exception& e) {
|
||
if (error) *error = std::string("manifest parse failed: ") + e.what();
|
||
return false;
|
||
}
|
||
manifest["id"] = id;
|
||
manifest["name"] = display;
|
||
if (manifest.contains("entry") && manifest["entry"].is_object()) {
|
||
for (auto& item : manifest["entry"].items()) {
|
||
if (item.value().is_string()) {
|
||
item.value() = replace_all(item.value().get<std::string>(), spec.target_token,
|
||
target);
|
||
}
|
||
}
|
||
}
|
||
if (manifest.contains("capabilities") && manifest["capabilities"].is_array()) {
|
||
for (auto& cap : manifest["capabilities"]) {
|
||
if (!cap.is_string()) continue;
|
||
std::string value = cap.get<std::string>();
|
||
value = replace_all(value, "format:example.", "format:" + slug + ".");
|
||
value = replace_all(value, "cae_solver:example.", "cae_solver:" + slug + ".");
|
||
value = replace_all(value, "cam_strategy:example.", "cam_strategy:" + slug + ".");
|
||
value = replace_all(value, "example.gcs.2d", slug + ".gcs.2d");
|
||
cap = value;
|
||
}
|
||
}
|
||
if (!write_text_file(manifest_path, manifest.dump(2) + "\n", error)) return false;
|
||
return true;
|
||
}
|
||
|
||
/// 模板文本替换:先按“模板原文 token”替换为占位符,再把占位符替换为最终值。
|
||
/// 这样可以避免新 id/name 中含模板 token(如 `.example`、`vde_template_*`)时被
|
||
/// 后续通用替换二次改写(P3-17b:format scaffold 的 id 曾被 `.example` 规则污染)。
|
||
std::string apply_scaffold_rewrite(
|
||
const std::string& text,
|
||
const std::vector<std::pair<std::string, std::string>>& replacements) {
|
||
std::vector<std::string> markers;
|
||
markers.reserve(replacements.size());
|
||
std::string out = text;
|
||
for (std::size_t i = 0; i < replacements.size(); ++i) {
|
||
const std::string marker = "\x01VDE_SCAFFOLD_" + std::to_string(i) + "\x02";
|
||
markers.push_back(marker);
|
||
out = replace_all(std::move(out), replacements[i].first, marker);
|
||
}
|
||
for (std::size_t i = 0; i < replacements.size(); ++i) {
|
||
out = replace_all(std::move(out), markers[i], replacements[i].second);
|
||
}
|
||
return out;
|
||
}
|
||
|
||
bool rewrite_scaffold_text_files(
|
||
const vde::fs::path& root,
|
||
const std::vector<std::pair<std::string, std::string>>& replacements,
|
||
std::string* error) {
|
||
std::error_code ec;
|
||
for (vde::fs::recursive_directory_iterator it(
|
||
root, vde::fs::directory_options::skip_permission_denied, ec),
|
||
end;
|
||
it != end; it.increment(ec)) {
|
||
if (ec) break;
|
||
if (!it->is_regular_file(ec) || ec) continue;
|
||
const vde::fs::path path = it->path();
|
||
if (path.filename() == "vde-plugin.json") continue; // JSON 已单独处理
|
||
const std::string text = read_file_text(path);
|
||
if (text.empty()) continue;
|
||
const std::string rewritten = apply_scaffold_rewrite(text, replacements);
|
||
if (rewritten != text && !write_text_file(path, rewritten, error)) return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
vde::fs::path resolve_templates_root(const std::string& explicit_dir) {
|
||
namespace fs = vde::fs;
|
||
std::error_code ec;
|
||
// 显式 --templates 必须有效(不做静默回退,避免生成到非预期模板)。
|
||
if (!explicit_dir.empty()) {
|
||
if (fs::is_directory(fs::path(explicit_dir) / "format_plugin", ec))
|
||
return fs::path(explicit_dir);
|
||
return {};
|
||
}
|
||
std::vector<fs::path> candidates;
|
||
if (const char* env = std::getenv("VDE_PLUGIN_TEMPLATE_DIR")) candidates.emplace_back(env);
|
||
candidates.emplace_back("plugins/templates");
|
||
for (const auto& candidate : candidates) {
|
||
if (fs::is_directory(candidate / "format_plugin", ec)) return candidate;
|
||
}
|
||
return {};
|
||
}
|
||
|
||
int cmd_scaffold(const std::string& kind_text, const std::string& name,
|
||
const std::string& out_text, const std::string& templates_text,
|
||
bool force) {
|
||
namespace fs = vde::fs;
|
||
if (name.empty() || name.find('/') != std::string::npos ||
|
||
name.find('\\') != std::string::npos || name.find(' ') != std::string::npos) {
|
||
std::fprintf(stderr, "scaffold: invalid plugin id/name '%s' (no spaces/slashes)\n",
|
||
name.c_str());
|
||
return 2;
|
||
}
|
||
ScaffoldKind kind = ScaffoldKind::Format;
|
||
const ScaffoldSpec* spec = nullptr;
|
||
if (!parse_scaffold_kind(kind_text, &kind, &spec) || !spec) {
|
||
std::fprintf(stderr, "scaffold: unknown kind '%s' (expected format/cae/cam/gcs)\n",
|
||
kind_text.c_str());
|
||
return 2;
|
||
}
|
||
const fs::path templates_root = resolve_templates_root(templates_text);
|
||
if (templates_root.empty()) {
|
||
std::fprintf(stderr,
|
||
"scaffold: templates dir not found; use --templates <VDE>/plugins/templates "
|
||
"or set VDE_PLUGIN_TEMPLATE_DIR\n");
|
||
return 3;
|
||
}
|
||
const fs::path source = templates_root / spec->directory;
|
||
std::error_code ec;
|
||
if (!fs::is_directory(source, ec)) {
|
||
std::fprintf(stderr, "scaffold: template missing: %s\n", source.string().c_str());
|
||
return 3;
|
||
}
|
||
const fs::path out = out_text.empty() ? fs::path(name) : fs::path(out_text);
|
||
if (fs::exists(out, ec)) {
|
||
if (!fs::is_directory(out, ec)) {
|
||
std::fprintf(stderr, "scaffold: output exists and is not a directory: %s\n",
|
||
out.string().c_str());
|
||
return 3;
|
||
}
|
||
if (!force && !fs::is_empty(out, ec)) {
|
||
std::fprintf(stderr, "scaffold: output directory not empty (use --force): %s\n",
|
||
out.string().c_str());
|
||
return 3;
|
||
}
|
||
}
|
||
fs::create_directories(out, ec);
|
||
if (ec) {
|
||
std::fprintf(stderr, "scaffold: cannot create output directory: %s\n",
|
||
ec.message().c_str());
|
||
return 3;
|
||
}
|
||
std::string error;
|
||
if (!copy_template_tree(source, out, force, &error)) {
|
||
std::fprintf(stderr, "scaffold: copy failed: %s\n", error.c_str());
|
||
return 3;
|
||
}
|
||
const std::string slug = sanitize_slug(name);
|
||
const std::string target = "vde_" + slug;
|
||
const std::string display = name;
|
||
const auto replacements = scaffold_replacements(kind, *spec, name, target, slug, display);
|
||
if (!rewrite_scaffold_manifest(out / "vde-plugin.json", *spec, name, display, target, slug,
|
||
&error) ||
|
||
!rewrite_scaffold_text_files(out, replacements, &error)) {
|
||
std::fprintf(stderr, "scaffold: rewrite failed: %s\n", error.c_str());
|
||
return 3;
|
||
}
|
||
std::printf("scaffold: OK -> %s\n", out.string().c_str());
|
||
std::printf(" template : %s\n", source.string().c_str());
|
||
std::printf(" plugin id : %s\n", name.c_str());
|
||
std::printf(" target/entry : %s (windows) / lib%s.so (linux) / lib%s.dylib (macos)\n",
|
||
(target + ".dll").c_str(), target.c_str(), target.c_str());
|
||
std::printf(" next steps:\n");
|
||
std::printf(" cmake -S \"%s\" -B \"%s\" -G Ninja -DCMAKE_BUILD_TYPE=Release "
|
||
"-DVDE_SDK_DIR=<VDE source root|install prefix>\n",
|
||
out.string().c_str(), (out / "build").string().c_str());
|
||
std::printf(" cmake --build \"%s\"\n", (out / "build").string().c_str());
|
||
std::printf(" vde_plugin_tool --check \"%s\"\n",
|
||
(out / "build" / "plugin").string().c_str());
|
||
std::printf(" vde_plugin_tool --list \"%s\"\n",
|
||
(out / "build" / "plugin").string().c_str());
|
||
return 0;
|
||
}
|
||
|
||
/// 远程/本地索引获取:Windows 构建用 WinHTTP(http/https);其他平台仅本地文件。
|
||
bool fetch_index_bytes(const std::string& location, std::string* bytes,
|
||
std::string* error) {
|
||
namespace fs = vde::fs;
|
||
std::error_code ec;
|
||
std::string path = location;
|
||
if (path.rfind("file://", 0) == 0) path = path.substr(7);
|
||
if (fs::is_regular_file(path, ec)) {
|
||
*bytes = read_file_text(path);
|
||
if (bytes->empty()) {
|
||
if (error) *error = "empty/unreadable local index: " + path;
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
const bool is_http = location.rfind("http://", 0) == 0;
|
||
const bool is_https = location.rfind("https://", 0) == 0;
|
||
if (!is_http && !is_https) {
|
||
if (error) *error = "not found and not an http(s)/file URL: " + location;
|
||
return false;
|
||
}
|
||
#if defined(_WIN32)
|
||
const std::wstring wide_url(location.begin(), location.end());
|
||
URL_COMPONENTS components{};
|
||
components.dwStructSize = sizeof(components);
|
||
wchar_t host[256] = {};
|
||
wchar_t url_path[2048] = {};
|
||
components.lpszHostName = host;
|
||
components.dwHostNameLength = 255;
|
||
components.lpszUrlPath = url_path;
|
||
components.dwUrlPathLength = 2047;
|
||
if (!WinHttpCrackUrl(wide_url.c_str(), 0, 0, &components)) {
|
||
if (error) *error = "invalid URL (WinHttpCrackUrl): " + location;
|
||
return false;
|
||
}
|
||
HINTERNET session = WinHttpOpen(L"vde_plugin_tool/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
|
||
WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
|
||
if (!session) {
|
||
if (error) *error = "WinHttpOpen failed";
|
||
return false;
|
||
}
|
||
HINTERNET connection = WinHttpConnect(
|
||
session, std::wstring(host, components.dwHostNameLength).c_str(), components.nPort, 0);
|
||
HINTERNET request = nullptr;
|
||
if (connection) {
|
||
request = WinHttpOpenRequest(
|
||
connection, L"GET", std::wstring(url_path, components.dwUrlPathLength).c_str(),
|
||
nullptr, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES,
|
||
components.nScheme == INTERNET_SCHEME_HTTPS ? WINHTTP_FLAG_SECURE : 0);
|
||
}
|
||
bool ok = false;
|
||
if (request && WinHttpSendRequest(request, WINHTTP_NO_ADDITIONAL_HEADERS, 0,
|
||
WINHTTP_NO_REQUEST_DATA, 0, 0, 0) &&
|
||
WinHttpReceiveResponse(request, nullptr)) {
|
||
DWORD status = 0;
|
||
DWORD status_size = sizeof(status);
|
||
WinHttpQueryHeaders(request, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,
|
||
WINHTTP_HEADER_NAME_BY_INDEX, &status, &status_size,
|
||
WINHTTP_NO_HEADER_INDEX);
|
||
if (status != 200) {
|
||
if (error) *error = "HTTP status " + std::to_string(status) + " for " + location;
|
||
} else {
|
||
std::string body;
|
||
for (;;) {
|
||
DWORD available = 0;
|
||
if (!WinHttpQueryDataAvailable(request, &available)) break;
|
||
if (available == 0) {
|
||
ok = true;
|
||
break;
|
||
}
|
||
std::string chunk(available, '\0');
|
||
DWORD read = 0;
|
||
if (!WinHttpReadData(request, chunk.data(), available, &read)) break;
|
||
body.append(chunk.data(), read);
|
||
}
|
||
if (ok) *bytes = std::move(body);
|
||
}
|
||
} else if (error && error->empty()) {
|
||
*error = "HTTP request failed: " + location;
|
||
}
|
||
if (request) WinHttpCloseHandle(request);
|
||
if (connection) WinHttpCloseHandle(connection);
|
||
if (session) WinHttpCloseHandle(session);
|
||
return ok;
|
||
#else
|
||
(void)is_https;
|
||
if (error) {
|
||
*error = "URL fetch is only supported by Windows builds (WinHTTP); "
|
||
"use a local file/file:// on this platform";
|
||
}
|
||
return false;
|
||
#endif
|
||
}
|
||
|
||
int cmd_fetch_index(const std::string& location, const std::string& output,
|
||
const std::string& pubkey, bool allow_unsigned,
|
||
bool verify_artifacts) {
|
||
if (output.empty()) {
|
||
std::fprintf(stderr, "fetch-index: -o <index.json> is required\n");
|
||
return 2;
|
||
}
|
||
std::string bytes;
|
||
std::string error;
|
||
std::printf("fetch-index: %s\n", location.c_str());
|
||
if (!fetch_index_bytes(location, &bytes, &error)) {
|
||
std::fprintf(stderr, "fetch-index: FAILED: %s\n", error.c_str());
|
||
return 3;
|
||
}
|
||
std::printf(" bytes : %zu\n", bytes.size());
|
||
if (!write_text_file(output, bytes, &error)) {
|
||
std::fprintf(stderr, "fetch-index: FAILED: %s\n", error.c_str());
|
||
return 3;
|
||
}
|
||
IndexCheckResult result;
|
||
const int rc = check_index_file(output, pubkey, allow_unsigned, verify_artifacts, {},
|
||
&result, &error);
|
||
if (rc != 0) {
|
||
std::fprintf(stderr, "fetch-index: verification FAILED: %s\n", error.c_str());
|
||
return rc;
|
||
}
|
||
std::printf(" saved : %s\n", output.c_str());
|
||
std::printf(" schema : %d, plugins: %zu\n", kIndexSchemaVersion,
|
||
result.entries.size());
|
||
for (const auto& entry : result.entries) print_index_entry(entry);
|
||
std::printf(" signature : %s\n",
|
||
result.signed_index ? "ed25519 OK" : "none (--allow-unsigned)");
|
||
std::printf(" artifacts : %s\n",
|
||
verify_artifacts ? "hashes verified against local packages"
|
||
: "skipped (use --verify-artifacts with local packages)");
|
||
std::printf("fetch-index: PASS\n");
|
||
return 0;
|
||
}
|
||
|
||
} // namespace
|
||
|
||
int main(int argc, char** argv) {
|
||
if (argc < 2) {
|
||
print_usage(argv[0]);
|
||
return 2;
|
||
}
|
||
const std::string cmd = argv[1];
|
||
if (cmd == "--help" || cmd == "-h" || cmd == "help") {
|
||
print_usage(argv[0]);
|
||
return 0;
|
||
}
|
||
if (cmd == "--abi") {
|
||
if (argc != 2) {
|
||
std::fprintf(stderr, "--abi takes no arguments\n");
|
||
return 2;
|
||
}
|
||
return cmd_abi();
|
||
}
|
||
if (cmd == "--inspect") {
|
||
if (argc != 3) {
|
||
std::fprintf(stderr, "--inspect requires a manifest path\n");
|
||
print_usage(argv[0]);
|
||
return 2;
|
||
}
|
||
return cmd_inspect(argv[2]);
|
||
}
|
||
if (cmd == "list") {
|
||
if (argc != 3) {
|
||
std::fprintf(stderr, "list requires an install root directory\n");
|
||
print_usage(argv[0]);
|
||
return 2;
|
||
}
|
||
return cmd_list(argv[2]);
|
||
}
|
||
if (cmd == "package") {
|
||
std::string plugin_dir;
|
||
std::string output;
|
||
for (int i = 2; i < argc; ++i) {
|
||
const std::string arg = argv[i];
|
||
if (arg == "-o" || arg == "--output") {
|
||
if (i + 1 >= argc) {
|
||
std::fprintf(stderr, "package: -o requires a value\n");
|
||
return 2;
|
||
}
|
||
output = argv[++i];
|
||
} else if (!arg.empty() && arg[0] != '-') {
|
||
plugin_dir = arg;
|
||
} else {
|
||
std::fprintf(stderr, "package: unknown argument: %s\n", arg.c_str());
|
||
return 2;
|
||
}
|
||
}
|
||
return cmd_package(plugin_dir, output);
|
||
}
|
||
if (cmd == "unpack") {
|
||
std::string archive;
|
||
std::string dest;
|
||
for (int i = 2; i < argc; ++i) {
|
||
const std::string arg = argv[i];
|
||
if (arg == "--dest" || arg == "-d") {
|
||
if (i + 1 >= argc) return 2;
|
||
dest = argv[++i];
|
||
} else if (!arg.empty() && arg[0] != '-') {
|
||
archive = arg;
|
||
} else {
|
||
std::fprintf(stderr, "unpack: unknown argument: %s\n", arg.c_str());
|
||
return 2;
|
||
}
|
||
}
|
||
return cmd_unpack(archive, dest);
|
||
}
|
||
if (cmd == "verify-package") {
|
||
if (argc != 3) {
|
||
std::fprintf(stderr, "verify-package requires <archive.vdeplugin>\n");
|
||
return 2;
|
||
}
|
||
return cmd_verify_package(argv[2]);
|
||
}
|
||
if (cmd == "upgrade") {
|
||
std::string package_path;
|
||
std::string root;
|
||
bool force = false;
|
||
bool rollback = false;
|
||
for (int i = 2; i < argc; ++i) {
|
||
const std::string arg = argv[i];
|
||
if (arg == "--root" || arg == "--dest") {
|
||
if (i + 1 >= argc) {
|
||
std::fprintf(stderr, "upgrade: --root requires a value\n");
|
||
return 2;
|
||
}
|
||
root = argv[++i];
|
||
} else if (arg == "--force") {
|
||
force = true;
|
||
} else if (arg == "--rollback") {
|
||
rollback = true;
|
||
} else if (!arg.empty() && arg[0] != '-') {
|
||
package_path = arg;
|
||
} else {
|
||
std::fprintf(stderr, "upgrade: unknown argument: %s\n", arg.c_str());
|
||
return 2;
|
||
}
|
||
}
|
||
return cmd_upgrade_package(package_path, root, force, rollback);
|
||
}
|
||
if (cmd == "uninstall") {
|
||
std::string plugin_id;
|
||
std::string dest;
|
||
for (int i = 2; i < argc; ++i) {
|
||
const std::string arg = argv[i];
|
||
if (arg == "--root" || arg == "--dest") {
|
||
if (i + 1 >= argc) return 2;
|
||
dest = argv[++i];
|
||
} else if (!arg.empty() && arg[0] != '-') {
|
||
plugin_id = arg;
|
||
} else {
|
||
std::fprintf(stderr, "uninstall: unknown argument: %s\n", arg.c_str());
|
||
return 2;
|
||
}
|
||
}
|
||
return cmd_uninstall(plugin_id, dest);
|
||
}
|
||
if (cmd == "--list") {
|
||
if (argc != 3) {
|
||
std::fprintf(stderr, "--list requires a directory\n");
|
||
print_usage(argv[0]);
|
||
return 2;
|
||
}
|
||
return cmd_list(argv[2]);
|
||
}
|
||
if (cmd == "lint") {
|
||
if (argc != 3) {
|
||
std::fprintf(stderr, "lint requires a plugin directory\n");
|
||
print_usage(argv[0]);
|
||
return 2;
|
||
}
|
||
return cmd_lint(argv[2]);
|
||
}
|
||
if (cmd == "matrix") {
|
||
bool json_output = false;
|
||
std::string matrix_dir;
|
||
std::vector<std::string> allowed_missing;
|
||
for (int i = 2; i < argc; ++i) {
|
||
const std::string arg = argv[i];
|
||
if (arg == "--json") {
|
||
json_output = true;
|
||
} else if (arg == "--allow-missing-kind") {
|
||
if (i + 1 >= argc) return 2;
|
||
allowed_missing.emplace_back(argv[++i]);
|
||
} else if (!arg.empty() && arg[0] != '-') {
|
||
matrix_dir = arg;
|
||
} else {
|
||
std::fprintf(stderr, "matrix: unknown argument: %s\n", arg.c_str());
|
||
return 2;
|
||
}
|
||
}
|
||
if (matrix_dir.empty()) {
|
||
std::fprintf(stderr, "matrix requires a plugin directory\n");
|
||
print_usage(argv[0]);
|
||
return 2;
|
||
}
|
||
return cmd_matrix(matrix_dir, json_output, allowed_missing);
|
||
}
|
||
|
||
if (cmd == "--check") {
|
||
if (argc != 3) {
|
||
std::fprintf(stderr, "--check requires a plugin directory or manifest path\n");
|
||
print_usage(argv[0]);
|
||
return 2;
|
||
}
|
||
return cmd_check(argv[2]);
|
||
}
|
||
if (cmd == "--build-index") {
|
||
std::string plugins_dir;
|
||
std::string output;
|
||
std::string seed;
|
||
std::string key_id;
|
||
std::string publisher;
|
||
for (int i = 2; i < argc; ++i) {
|
||
const std::string arg = argv[i];
|
||
const auto need_value = [&](const char* option) -> const char* {
|
||
if (i + 1 >= argc) {
|
||
std::fprintf(stderr, "build-index: %s requires a value\n", option);
|
||
return nullptr;
|
||
}
|
||
return argv[++i];
|
||
};
|
||
if (arg == "-o" || arg == "--output") {
|
||
const char* value = need_value("--output");
|
||
if (!value) return 2;
|
||
output = value;
|
||
} else if (arg == "--sign") {
|
||
const char* value = need_value("--sign");
|
||
if (!value) return 2;
|
||
seed = value;
|
||
} else if (arg == "--key-id") {
|
||
const char* value = need_value("--key-id");
|
||
if (!value) return 2;
|
||
key_id = value;
|
||
} else if (arg == "--publisher") {
|
||
const char* value = need_value("--publisher");
|
||
if (!value) return 2;
|
||
publisher = value;
|
||
} else if (!arg.empty() && arg[0] != '-') {
|
||
plugins_dir = arg;
|
||
} else {
|
||
std::fprintf(stderr, "build-index: unknown argument: %s\n", arg.c_str());
|
||
return 2;
|
||
}
|
||
}
|
||
if (plugins_dir.empty() || output.empty()) {
|
||
std::fprintf(stderr,
|
||
"build-index requires <plugins_dir> and -o <index.json>\n");
|
||
return 2;
|
||
}
|
||
return cmd_build_index(plugins_dir, output, seed, key_id, publisher);
|
||
}
|
||
if (cmd == "--verify-index") {
|
||
std::string target;
|
||
std::string pubkey;
|
||
std::string revoked_file;
|
||
std::string crl_location;
|
||
std::string crl_pubkey;
|
||
bool allow_unsigned = false;
|
||
for (int i = 2; i < argc; ++i) {
|
||
const std::string arg = argv[i];
|
||
if (arg == "--pubkey") {
|
||
if (i + 1 >= argc) {
|
||
std::fprintf(stderr, "verify-index: --pubkey requires a value\n");
|
||
return 2;
|
||
}
|
||
pubkey = argv[++i];
|
||
} else if (arg == "--revoked-keys") {
|
||
if (i + 1 >= argc) {
|
||
std::fprintf(stderr, "verify-index: --revoked-keys requires a value\n");
|
||
return 2;
|
||
}
|
||
revoked_file = argv[++i];
|
||
} else if (arg == "--crl") {
|
||
if (i + 1 >= argc) {
|
||
std::fprintf(stderr, "%s: --crl requires a value\n", cmd.c_str());
|
||
return 2;
|
||
}
|
||
crl_location = argv[++i];
|
||
} else if (arg == "--crl-pubkey") {
|
||
if (i + 1 >= argc) {
|
||
std::fprintf(stderr, "%s: --crl-pubkey requires a value\n", cmd.c_str());
|
||
return 2;
|
||
}
|
||
crl_pubkey = argv[++i];
|
||
} else if (arg == "--allow-unsigned") {
|
||
allow_unsigned = true;
|
||
} else if (!arg.empty() && arg[0] != '-') {
|
||
target = arg;
|
||
} else {
|
||
std::fprintf(stderr, "verify-index: unknown argument: %s\n", arg.c_str());
|
||
return 2;
|
||
}
|
||
}
|
||
if (target.empty()) {
|
||
std::fprintf(stderr, "verify-index requires <index.json>\n");
|
||
return 2;
|
||
}
|
||
std::vector<std::string> revoked_key_ids;
|
||
if (!revoked_file.empty()) {
|
||
std::string revoke_error;
|
||
if (!load_revoked_key_ids(revoked_file, &revoked_key_ids, &revoke_error)) {
|
||
std::fprintf(stderr, "verify-index: invalid --revoked-keys: %s\n",
|
||
revoke_error.c_str());
|
||
return 3;
|
||
}
|
||
}
|
||
if (!crl_location.empty()) {
|
||
std::vector<std::string> crl_revoked;
|
||
int crl_exit = 3;
|
||
std::string crl_error;
|
||
const std::string& crl_key = crl_pubkey.empty() ? pubkey : crl_pubkey;
|
||
if (!load_crl_revoked_key_ids(crl_location, crl_key, &crl_revoked,
|
||
&crl_exit, &crl_error)) {
|
||
std::fprintf(stderr, "%s: --crl failed: %s\n", cmd.c_str(),
|
||
crl_error.c_str());
|
||
return crl_exit;
|
||
}
|
||
for (const auto& id : crl_revoked) {
|
||
if (std::find(revoked_key_ids.begin(), revoked_key_ids.end(), id) ==
|
||
revoked_key_ids.end()) {
|
||
revoked_key_ids.push_back(id);
|
||
}
|
||
}
|
||
std::printf("%s: CRL merged %zu revoked key_id(s)\n", cmd.c_str(),
|
||
crl_revoked.size());
|
||
}
|
||
return cmd_verify_index(target, pubkey, allow_unsigned, revoked_key_ids);
|
||
}
|
||
if (cmd == "--install") {
|
||
std::string target;
|
||
std::string dest;
|
||
std::string plugin_id;
|
||
std::string version;
|
||
std::string pubkey;
|
||
std::string revoked_file;
|
||
std::string crl_location;
|
||
std::string crl_pubkey;
|
||
bool allow_unsigned = false;
|
||
bool force = false;
|
||
bool fetch_packages = false;
|
||
for (int i = 2; i < argc; ++i) {
|
||
const std::string arg = argv[i];
|
||
const auto value = [&](const char* option) -> std::string {
|
||
if (i + 1 >= argc) {
|
||
std::fprintf(stderr, "install: %s requires a value\n", option);
|
||
return std::string();
|
||
}
|
||
return argv[++i];
|
||
};
|
||
if (arg == "--dest") {
|
||
dest = value("--dest");
|
||
} else if (arg == "--id") {
|
||
plugin_id = value("--id");
|
||
} else if (arg == "--version") {
|
||
version = value("--version");
|
||
} else if (arg == "--pubkey") {
|
||
pubkey = value("--pubkey");
|
||
} else if (arg == "--revoked-keys") {
|
||
revoked_file = value("--revoked-keys");
|
||
} else if (arg == "--crl") {
|
||
if (i + 1 >= argc) {
|
||
std::fprintf(stderr, "%s: --crl requires a value\n", cmd.c_str());
|
||
return 2;
|
||
}
|
||
crl_location = argv[++i];
|
||
} else if (arg == "--crl-pubkey") {
|
||
if (i + 1 >= argc) {
|
||
std::fprintf(stderr, "%s: --crl-pubkey requires a value\n", cmd.c_str());
|
||
return 2;
|
||
}
|
||
crl_pubkey = argv[++i];
|
||
} else if (arg == "--allow-unsigned") {
|
||
allow_unsigned = true;
|
||
} else if (arg == "--force") {
|
||
force = true;
|
||
} else if (arg == "--fetch-packages") {
|
||
fetch_packages = true;
|
||
} else if (arg == "--verify-artifacts") {
|
||
fetch_packages = true; // 语义合并:要求每个工件从索引获取并校验哈希
|
||
} else if (!arg.empty() && arg[0] != '-') {
|
||
target = arg;
|
||
} else {
|
||
std::fprintf(stderr, "install: unknown argument: %s\n", arg.c_str());
|
||
return 2;
|
||
}
|
||
}
|
||
if (target.empty() || dest.empty()) {
|
||
std::fprintf(stderr,
|
||
"install requires <index.json|url|plugin_dir> and --dest <dir> "
|
||
"[--fetch-packages]\n");
|
||
return 2;
|
||
}
|
||
std::vector<std::string> revoked_key_ids;
|
||
if (!revoked_file.empty()) {
|
||
std::string revoke_error;
|
||
if (!load_revoked_key_ids(revoked_file, &revoked_key_ids, &revoke_error)) {
|
||
std::fprintf(stderr, "install: invalid --revoked-keys: %s\n",
|
||
revoke_error.c_str());
|
||
return 3;
|
||
}
|
||
}
|
||
if (!crl_location.empty()) {
|
||
std::vector<std::string> crl_revoked;
|
||
int crl_exit = 3;
|
||
std::string crl_error;
|
||
const std::string& crl_key = crl_pubkey.empty() ? pubkey : crl_pubkey;
|
||
if (!load_crl_revoked_key_ids(crl_location, crl_key, &crl_revoked,
|
||
&crl_exit, &crl_error)) {
|
||
std::fprintf(stderr, "%s: --crl failed: %s\n", cmd.c_str(),
|
||
crl_error.c_str());
|
||
return crl_exit;
|
||
}
|
||
for (const auto& id : crl_revoked) {
|
||
if (std::find(revoked_key_ids.begin(), revoked_key_ids.end(), id) ==
|
||
revoked_key_ids.end()) {
|
||
revoked_key_ids.push_back(id);
|
||
}
|
||
}
|
||
std::printf("%s: CRL merged %zu revoked key_id(s)\n", cmd.c_str(),
|
||
crl_revoked.size());
|
||
}
|
||
return cmd_install(target, dest, plugin_id, version, pubkey, allow_unsigned, force,
|
||
fetch_packages, revoked_key_ids);
|
||
}
|
||
if (cmd == "--uninstall") {
|
||
std::string plugin_id;
|
||
std::string dest;
|
||
for (int i = 2; i < argc; ++i) {
|
||
const std::string arg = argv[i];
|
||
if (arg == "--dest" || arg == "--root") {
|
||
if (i + 1 >= argc) {
|
||
std::fprintf(stderr, "uninstall: --dest/--root requires a value\n");
|
||
return 2;
|
||
}
|
||
dest = argv[++i];
|
||
} else if (!arg.empty() && arg[0] != '-') {
|
||
plugin_id = arg;
|
||
} else {
|
||
std::fprintf(stderr, "uninstall: unknown argument: %s\n", arg.c_str());
|
||
return 2;
|
||
}
|
||
}
|
||
if (plugin_id.empty() || dest.empty()) {
|
||
std::fprintf(stderr, "uninstall requires <plugin-id> and --dest <dir>\n");
|
||
return 2;
|
||
}
|
||
return cmd_uninstall(plugin_id, dest);
|
||
}
|
||
if (cmd == "--upgrade") {
|
||
std::string target;
|
||
std::string dest;
|
||
std::string plugin_id;
|
||
std::string version;
|
||
std::string pubkey;
|
||
std::string revoked_file;
|
||
std::string crl_location;
|
||
std::string crl_pubkey;
|
||
bool allow_unsigned = false;
|
||
bool force = false;
|
||
bool fetch_packages = false;
|
||
for (int i = 2; i < argc; ++i) {
|
||
const std::string arg = argv[i];
|
||
const auto value = [&](const char* option) -> std::string {
|
||
if (i + 1 >= argc) {
|
||
std::fprintf(stderr, "upgrade: %s requires a value\n", option);
|
||
return std::string();
|
||
}
|
||
return argv[++i];
|
||
};
|
||
if (arg == "--dest") {
|
||
dest = value("--dest");
|
||
} else if (arg == "--id") {
|
||
plugin_id = value("--id");
|
||
} else if (arg == "--version") {
|
||
version = value("--version");
|
||
} else if (arg == "--pubkey") {
|
||
pubkey = value("--pubkey");
|
||
} else if (arg == "--revoked-keys") {
|
||
revoked_file = value("--revoked-keys");
|
||
} else if (arg == "--crl") {
|
||
if (i + 1 >= argc) {
|
||
std::fprintf(stderr, "%s: --crl requires a value\n", cmd.c_str());
|
||
return 2;
|
||
}
|
||
crl_location = argv[++i];
|
||
} else if (arg == "--crl-pubkey") {
|
||
if (i + 1 >= argc) {
|
||
std::fprintf(stderr, "%s: --crl-pubkey requires a value\n", cmd.c_str());
|
||
return 2;
|
||
}
|
||
crl_pubkey = argv[++i];
|
||
} else if (arg == "--allow-unsigned") {
|
||
allow_unsigned = true;
|
||
} else if (arg == "--force") {
|
||
force = true;
|
||
} else if (arg == "--fetch-packages") {
|
||
fetch_packages = true;
|
||
} else if (!arg.empty() && arg[0] != '-') {
|
||
target = arg;
|
||
} else {
|
||
std::fprintf(stderr, "upgrade: unknown argument: %s\n", arg.c_str());
|
||
return 2;
|
||
}
|
||
}
|
||
if (plugin_id.empty() || dest.empty() || target.empty()) {
|
||
std::fprintf(stderr,
|
||
"upgrade requires <index.json|url> --id <plugin-id> --dest <dir> "
|
||
"[--version x.y.z] [--fetch-packages]\n");
|
||
return 2;
|
||
}
|
||
std::vector<std::string> revoked_key_ids;
|
||
if (!revoked_file.empty()) {
|
||
std::string revoke_error;
|
||
if (!load_revoked_key_ids(revoked_file, &revoked_key_ids, &revoke_error)) {
|
||
std::fprintf(stderr, "upgrade: invalid --revoked-keys: %s\n",
|
||
revoke_error.c_str());
|
||
return 3;
|
||
}
|
||
}
|
||
if (!crl_location.empty()) {
|
||
std::vector<std::string> crl_revoked;
|
||
int crl_exit = 3;
|
||
std::string crl_error;
|
||
const std::string& crl_key = crl_pubkey.empty() ? pubkey : crl_pubkey;
|
||
if (!load_crl_revoked_key_ids(crl_location, crl_key, &crl_revoked,
|
||
&crl_exit, &crl_error)) {
|
||
std::fprintf(stderr, "%s: --crl failed: %s\n", cmd.c_str(),
|
||
crl_error.c_str());
|
||
return crl_exit;
|
||
}
|
||
for (const auto& id : crl_revoked) {
|
||
if (std::find(revoked_key_ids.begin(), revoked_key_ids.end(), id) ==
|
||
revoked_key_ids.end()) {
|
||
revoked_key_ids.push_back(id);
|
||
}
|
||
}
|
||
std::printf("%s: CRL merged %zu revoked key_id(s)\n", cmd.c_str(),
|
||
crl_revoked.size());
|
||
}
|
||
return cmd_upgrade(target, dest, plugin_id, version, pubkey, allow_unsigned, force,
|
||
fetch_packages, revoked_key_ids);
|
||
}
|
||
if (cmd == "--gen-keypair") {
|
||
std::string out_dir;
|
||
for (int i = 2; i < argc; ++i) {
|
||
const std::string arg = argv[i];
|
||
if (arg == "--out-dir") {
|
||
if (i + 1 >= argc) {
|
||
std::fprintf(stderr, "gen-keypair: --out-dir requires a value\n");
|
||
return 2;
|
||
}
|
||
out_dir = argv[++i];
|
||
} else {
|
||
std::fprintf(stderr, "gen-keypair: unknown argument: %s\n", arg.c_str());
|
||
return 2;
|
||
}
|
||
}
|
||
return cmd_gen_keypair(out_dir);
|
||
}
|
||
if (cmd == "scaffold") {
|
||
if (argc < 4) {
|
||
std::fprintf(stderr,
|
||
"scaffold requires <kind> <name> --out <dir> [--templates <dir>] "
|
||
"[--force]\n");
|
||
print_usage(argv[0]);
|
||
return 2;
|
||
}
|
||
const std::string kind_text = argv[2];
|
||
const std::string name = argv[3];
|
||
std::string out_dir;
|
||
std::string templates_dir;
|
||
bool force = false;
|
||
for (int i = 4; i < argc; ++i) {
|
||
const std::string arg = argv[i];
|
||
if (arg == "--out" || arg == "-o") {
|
||
if (i + 1 >= argc) {
|
||
std::fprintf(stderr, "scaffold: %s requires a value\n", arg.c_str());
|
||
return 2;
|
||
}
|
||
out_dir = argv[++i];
|
||
} else if (arg == "--templates") {
|
||
if (i + 1 >= argc) {
|
||
std::fprintf(stderr, "scaffold: --templates requires a value\n");
|
||
return 2;
|
||
}
|
||
templates_dir = argv[++i];
|
||
} else if (arg == "--force") {
|
||
force = true;
|
||
} else {
|
||
std::fprintf(stderr, "scaffold: unknown argument: %s\n", arg.c_str());
|
||
return 2;
|
||
}
|
||
}
|
||
if (out_dir.empty()) {
|
||
std::fprintf(stderr, "scaffold: --out <dir> is required\n");
|
||
return 2;
|
||
}
|
||
return cmd_scaffold(kind_text, name, out_dir, templates_dir, force);
|
||
}
|
||
if (cmd == "--sign-crl") {
|
||
std::string payload_path;
|
||
std::string output;
|
||
std::string seed;
|
||
for (int i = 2; i < argc; ++i) {
|
||
const std::string arg = argv[i];
|
||
const auto need_value = [&](const char* option) -> const char* {
|
||
if (i + 1 >= argc) {
|
||
std::fprintf(stderr, "sign-crl: %s requires a value\n", option);
|
||
return nullptr;
|
||
}
|
||
return argv[++i];
|
||
};
|
||
if (arg == "-o" || arg == "--output") {
|
||
const char* value = need_value("--output");
|
||
if (!value) return 2;
|
||
output = value;
|
||
} else if (arg == "--sign") {
|
||
const char* value = need_value("--sign");
|
||
if (!value) return 2;
|
||
seed = value;
|
||
} else if (!arg.empty() && arg[0] != '-') {
|
||
payload_path = arg;
|
||
} else {
|
||
std::fprintf(stderr, "sign-crl: unknown argument: %s\n", arg.c_str());
|
||
return 2;
|
||
}
|
||
}
|
||
if (payload_path.empty() || output.empty() || seed.empty()) {
|
||
std::fprintf(stderr,
|
||
"sign-crl requires <payload.json> -o <crl.json> --sign <seed>\n");
|
||
return 2;
|
||
}
|
||
return cmd_sign_crl(payload_path, output, seed);
|
||
}
|
||
if (cmd == "--fetch-index") {
|
||
std::string location;
|
||
std::string output;
|
||
std::string pubkey;
|
||
bool allow_unsigned = false;
|
||
bool verify_artifacts = false;
|
||
for (int i = 2; i < argc; ++i) {
|
||
const std::string arg = argv[i];
|
||
if (arg == "-o" || arg == "--output") {
|
||
if (i + 1 >= argc) {
|
||
std::fprintf(stderr, "fetch-index: %s requires a value\n", arg.c_str());
|
||
return 2;
|
||
}
|
||
output = argv[++i];
|
||
} else if (arg == "--pubkey") {
|
||
if (i + 1 >= argc) {
|
||
std::fprintf(stderr, "fetch-index: --pubkey requires a value\n");
|
||
return 2;
|
||
}
|
||
pubkey = argv[++i];
|
||
} else if (arg == "--allow-unsigned") {
|
||
allow_unsigned = true;
|
||
} else if (arg == "--verify-artifacts") {
|
||
verify_artifacts = true;
|
||
} else if (!arg.empty() && arg[0] != '-') {
|
||
location = arg;
|
||
} else {
|
||
std::fprintf(stderr, "fetch-index: unknown argument: %s\n", arg.c_str());
|
||
return 2;
|
||
}
|
||
}
|
||
if (location.empty() || output.empty()) {
|
||
std::fprintf(stderr,
|
||
"fetch-index requires <url|file> and -o <index.json>\n");
|
||
return 2;
|
||
}
|
||
return cmd_fetch_index(location, output, pubkey, allow_unsigned, verify_artifacts);
|
||
}
|
||
if (cmd == "verify") {
|
||
if (argc < 3) {
|
||
std::fprintf(stderr, "verify requires a plugin directory or manifest path\n");
|
||
print_usage(argv[0]);
|
||
return 2;
|
||
}
|
||
std::string target = argv[2];
|
||
std::string pubkey;
|
||
std::string key_id;
|
||
bool show_hashes = false;
|
||
for (int i = 3; i < argc; ++i) {
|
||
const std::string arg = argv[i];
|
||
if (arg == "--pubkey") {
|
||
if (i + 1 >= argc) {
|
||
std::fprintf(stderr, "verify: --pubkey requires a value\n");
|
||
return 2;
|
||
}
|
||
pubkey = argv[++i];
|
||
} else if (arg == "--key-id") {
|
||
if (i + 1 >= argc) {
|
||
std::fprintf(stderr, "verify: --key-id requires a value\n");
|
||
return 2;
|
||
}
|
||
key_id = argv[++i];
|
||
} else if (arg == "--hash") {
|
||
show_hashes = true;
|
||
} else {
|
||
std::fprintf(stderr, "verify: unknown argument: %s\n", arg.c_str());
|
||
print_usage(argv[0]);
|
||
return 2;
|
||
}
|
||
}
|
||
return cmd_verify(target, pubkey, key_id, show_hashes);
|
||
}
|
||
std::fprintf(stderr, "unknown command: %s\n", cmd.c_str());
|
||
print_usage(argv[0]);
|
||
return 2;
|
||
}
|