Add command line tools
Change-Id: I5bce4074324b35e5079b330dc20e14c22fc74c6b
diff --git a/tools/ndncert-client.cpp b/tools/ndncert-client.cpp
new file mode 100644
index 0000000..97b03bb
--- /dev/null
+++ b/tools/ndncert-client.cpp
@@ -0,0 +1,211 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2017, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#include "client-module.hpp"
+#include "challenge-module.hpp"
+#include "logging.hpp"
+
+#include <boost/program_options/options_description.hpp>
+#include <boost/program_options/variables_map.hpp>
+#include <boost/program_options/parsers.hpp>
+
+namespace ndn {
+namespace ndncert {
+
+_LOG_INIT(ndncert.clientTool);
+
+class ClientTool
+{
+public:
+ ClientTool(ClientModule& clientModule)
+ : client(clientModule)
+ {
+ }
+
+ void
+ errorCb(const std::string& errorInfo)
+ {
+ _LOG_TRACE("Error: " << errorInfo);
+ }
+
+ void
+ validateCb(const shared_ptr<RequestState> state, int& nStep)
+ {
+ if (state->m_status == ChallengeModule::SUCCESS) {
+ _LOG_TRACE("Certificate has already been issued");
+ return;
+ }
+
+ auto challenge = ChallengeModule::createChallengeModule(state->m_challengeType);
+ auto requirementList = challenge->getRequirementForValidate(state->m_status);
+
+ std::cerr << "Step" << nStep++ << ": Please satisfy following instruction(s)" << std::endl;
+ for (auto requirement : requirementList) {
+ std::cerr << "\t" << requirement << std::endl;
+ }
+ std::list<std::string> paraList;
+ for (size_t i = 0; i < requirementList.size(); i++) {
+ std::string tempParam;
+ std::cin >> tempParam;
+ paraList.push_back(tempParam);
+ }
+ auto paramJson = challenge->genValidateParamsJson(state->m_status, paraList);
+ client.sendValidate(state, paramJson,
+ bind(&ClientTool::validateCb, this, _1, nStep),
+ bind(&ClientTool::errorCb, this, _1));
+ }
+
+ void
+ selectCb(const shared_ptr<RequestState> state, int& nStep)
+ {
+ auto challenge = ChallengeModule::createChallengeModule(state->m_challengeType);
+ auto requirementList = challenge->getRequirementForValidate(state->m_status);
+
+ std::cerr << "Step" << nStep++ << ": Please satisfy following instruction(s)" << std::endl;
+ for (auto item : requirementList) {
+ std::cerr << "\t" << item << std::endl;
+ }
+ std::list<std::string> paraList;
+ for (size_t i = 0; i < requirementList.size(); i++) {
+ std::string tempParam;
+ std::cin >> tempParam;
+ paraList.push_back(tempParam);
+ }
+
+ auto paramJson = challenge->genValidateParamsJson(state->m_status, paraList);
+ client.sendValidate(state, paramJson,
+ bind(&ClientTool::validateCb, this, _1, nStep),
+ bind(&ClientTool::errorCb, this, _1));
+ }
+
+ void
+ newCb(const shared_ptr<RequestState> state, int& nStep)
+ {
+ std::cerr << "Step" << nStep++ << ": Please select one challenge from following types." << std::endl;
+ for (auto item : state->m_challengeList) {
+ std::cerr << "\t" << item << std::endl;
+ }
+ std::string choice;
+ std::cin >> choice;
+
+ auto challenge = ChallengeModule::createChallengeModule(choice);
+ auto requirementList = challenge->getRequirementForSelect();
+ std::list<std::string> paraList;
+ if (requirementList.size() != 0) {
+ std::cerr << "Step" << nStep++ << ": Please satisfy following instruction(s)" << std::endl;
+ for (auto item : requirementList) {
+ std::cerr << "\t" << item << std::endl;
+ }
+ for (size_t i = 0; i < requirementList.size(); i++) {
+ std::string tempParam;
+ std::cin >> tempParam;
+ paraList.push_back(tempParam);
+ }
+ }
+ auto paramJson = challenge->genSelectParamsJson(state->m_status, paraList);
+ client.sendSelect(state, choice, paramJson,
+ bind(&ClientTool::selectCb, this, _1, nStep),
+ bind(&ClientTool::errorCb, this, _1));
+ }
+
+public:
+ ClientModule& client;
+};
+
+int
+main(int argc, char* argv[])
+{
+ namespace po = boost::program_options;
+ std::string configFilePath = std::string(SYSCONFDIR) + "/ndncert/client.conf";
+ po::options_description description("General Usage\n ndncert-client [-h] [-f] configFilePath-file\n");
+ description.add_options()
+ ("help,h", "produce help message")
+ ("config-file,f", po::value<std::string>(&configFilePath), "config file name");
+ po::positional_options_description p;
+
+ po::variables_map vm;
+ try {
+ po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
+ po::notify(vm);
+ }
+ catch (const std::exception& e) {
+ std::cerr << "ERROR: " << e.what() << std::endl;
+ return 1;
+ }
+ if (vm.count("help") != 0) {
+ std::cerr << description << std::endl;
+ return 0;
+ }
+
+ Face face;
+ security::v2::KeyChain keyChain;
+ ClientModule client(face, keyChain);
+ client.getClientConf().load(configFilePath);
+
+ ClientTool tool(client);
+
+ auto caList = client.getClientConf().m_caItems;
+ std::cerr << "Index \t CA Namespace \t CA Introduction" << std::endl;
+ int count = 0;
+ for (auto item : caList) {
+ std::cerr << count++ << "\t"
+ << item.m_caName << "\t"
+ << item.m_caInfo << std::endl;
+ }
+ std::vector<ClientCaItem> caVector{std::begin(caList), std::end(caList)};
+ int nStep = 0;
+ std::cerr << "Step" << nStep++ << ": Please type in the CA namespace index that you want to apply" << std::endl;
+ std::string caIndexS;
+ std::cin >> caIndexS;
+ int caIndex = std::stoi(caIndexS);
+
+ BOOST_ASSERT(caIndex <= count);
+
+ auto targetCaItem = caVector[caIndex];
+ if (targetCaItem.m_probe != "") {
+ std::cerr <<"Step" << nStep++ << ": Probe Requirement-" << targetCaItem.m_probe << std::endl;
+ std::string probeInfo;
+ std::cin >> probeInfo;
+ client.sendProbe(targetCaItem, probeInfo,
+ bind(&ClientTool::newCb, &tool, _1, nStep),
+ bind(&ClientTool::errorCb, &tool, _1));
+ }
+ else {
+ std::cerr <<"Step" << nStep++ << ": Please type in the identity name" << std::endl;
+ std::string nameComponent;
+ std::cin >> nameComponent;
+ Name identityName(targetCaItem.m_caName);
+ identityName.append(nameComponent);
+ client.sendNew(targetCaItem, identityName,
+ bind(&ClientTool::newCb, &tool, _1, nStep),
+ bind(&ClientTool::errorCb, &tool, _1));
+ }
+ face.processEvents();
+ return 0;
+}
+
+} // namespace ndncert
+} // namespace ndn
+
+int
+main(int argc, char* argv[])
+{
+ return ndn::ndncert::main(argc, argv);
+}