From 2da6725e73aebbdeb26e3c858a0c674b15a8210d Mon Sep 17 00:00:00 2001 From: Louis Maillard <louis.maillard@savoirfairelinux.com> Date: Tue, 23 Jul 2024 14:17:16 -0400 Subject: [PATCH] tools: fix segfault when invalid identity given Giving no or invalid identity to dnc, dsh or dvpn was giving a segfault. They now print an error message and exit with a non-zero status correctly. dhtnet-crtmgr was also suffering segfault when no -o option, even for --help or --version options. This is now fixed. Change-Id: I7871db9ab73205c9ad024260d7687cc20ae1a983 --- tools/dhtnet_crtmgr/main.cpp | 4 ++-- tools/dnc/main.cpp | 4 ++++ tools/dsh/main.cpp | 4 ++++ tools/dvpn/main.cpp | 4 ++++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/tools/dhtnet_crtmgr/main.cpp b/tools/dhtnet_crtmgr/main.cpp index 95687cc..3dea0d2 100644 --- a/tools/dhtnet_crtmgr/main.cpp +++ b/tools/dhtnet_crtmgr/main.cpp @@ -86,8 +86,8 @@ parse_args(int argc, char** argv) } } - if (params.id.empty() && !params.pkid) { - std::cerr << "Error: The path to save the generated certificate is not provided.\n Please specify the path using the -i option.\n"; + if (params.id.empty() && !params.pkid && !params.help && !params.version) { + std::cerr << "Error: The path to save the generated certificate is not provided.\n Please specify the path using the -o option.\n"; exit(EXIT_FAILURE); } return params; diff --git a/tools/dnc/main.cpp b/tools/dnc/main.cpp index 441ef56..ea7659d 100644 --- a/tools/dnc/main.cpp +++ b/tools/dnc/main.cpp @@ -236,6 +236,10 @@ main(int argc, char** argv) } auto identity = dhtnet::loadIdentity(params.privateKey, params.cert); + if (!identity.first || !identity.second) { + fmt::print(stderr, "Hint: To generate new identity files, run: dhtnet-crtmgr --interactive\n"); + return EXIT_FAILURE; + } fmt::print("Loaded identity: {}\n", identity.second->getId()); fmt::print("dnc 1.0\n"); diff --git a/tools/dsh/main.cpp b/tools/dsh/main.cpp index 3b2592d..12dae12 100644 --- a/tools/dsh/main.cpp +++ b/tools/dsh/main.cpp @@ -219,6 +219,10 @@ main(int argc, char** argv) fmt::print("dsh 1.0\n"); auto identity = dhtnet::loadIdentity(params.privateKey, params.cert); + if (!identity.first || !identity.second) { + fmt::print(stderr, "Hint: To generate new identity files, run: dhtnet-crtmgr --interactive\n"); + return EXIT_FAILURE; + } fmt::print("Loaded identity: {} \n", identity.second->getId()); std::unique_ptr<dhtnet::Dsh> dhtsh; diff --git a/tools/dvpn/main.cpp b/tools/dvpn/main.cpp index 153a607..38899c4 100644 --- a/tools/dvpn/main.cpp +++ b/tools/dvpn/main.cpp @@ -217,6 +217,10 @@ main(int argc, char** argv) fmt::print("dvpn 1.0\n"); auto identity = dhtnet::loadIdentity(params.privateKey, params.cert); + if (!identity.first || !identity.second) { + fmt::print(stderr, "Hint: To generate new identity files, run: dhtnet-crtmgr --interactive\n"); + return EXIT_FAILURE; + } fmt::print("Loaded identity: {}\n", identity.second->getId()); std::unique_ptr<dhtnet::Dvpn> dvpn; -- GitLab