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
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 @@
         }
     }
 
-    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 @@
     }
 
     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 @@
     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 @@
     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;