blob: 97b03bbce1001353e9145552836d21fafc43d712 [file] [log] [blame]
Zhiyi Zhang08e0e982017-03-01 10:10:42 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2017, Regents of the University of California.
4 *
5 * This file is part of ndncert, a certificate management system based on NDN.
6 *
7 * ndncert is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License along with
16 * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ndncert authors and contributors.
19 */
20
21#include "client-module.hpp"
22#include "challenge-module.hpp"
23#include "logging.hpp"
24
25#include <boost/program_options/options_description.hpp>
26#include <boost/program_options/variables_map.hpp>
27#include <boost/program_options/parsers.hpp>
28
29namespace ndn {
30namespace ndncert {
31
32_LOG_INIT(ndncert.clientTool);
33
34class ClientTool
35{
36public:
37 ClientTool(ClientModule& clientModule)
38 : client(clientModule)
39 {
40 }
41
42 void
43 errorCb(const std::string& errorInfo)
44 {
45 _LOG_TRACE("Error: " << errorInfo);
46 }
47
48 void
49 validateCb(const shared_ptr<RequestState> state, int& nStep)
50 {
51 if (state->m_status == ChallengeModule::SUCCESS) {
52 _LOG_TRACE("Certificate has already been issued");
53 return;
54 }
55
56 auto challenge = ChallengeModule::createChallengeModule(state->m_challengeType);
57 auto requirementList = challenge->getRequirementForValidate(state->m_status);
58
59 std::cerr << "Step" << nStep++ << ": Please satisfy following instruction(s)" << std::endl;
60 for (auto requirement : requirementList) {
61 std::cerr << "\t" << requirement << std::endl;
62 }
63 std::list<std::string> paraList;
64 for (size_t i = 0; i < requirementList.size(); i++) {
65 std::string tempParam;
66 std::cin >> tempParam;
67 paraList.push_back(tempParam);
68 }
69 auto paramJson = challenge->genValidateParamsJson(state->m_status, paraList);
70 client.sendValidate(state, paramJson,
71 bind(&ClientTool::validateCb, this, _1, nStep),
72 bind(&ClientTool::errorCb, this, _1));
73 }
74
75 void
76 selectCb(const shared_ptr<RequestState> state, int& nStep)
77 {
78 auto challenge = ChallengeModule::createChallengeModule(state->m_challengeType);
79 auto requirementList = challenge->getRequirementForValidate(state->m_status);
80
81 std::cerr << "Step" << nStep++ << ": Please satisfy following instruction(s)" << std::endl;
82 for (auto item : requirementList) {
83 std::cerr << "\t" << item << std::endl;
84 }
85 std::list<std::string> paraList;
86 for (size_t i = 0; i < requirementList.size(); i++) {
87 std::string tempParam;
88 std::cin >> tempParam;
89 paraList.push_back(tempParam);
90 }
91
92 auto paramJson = challenge->genValidateParamsJson(state->m_status, paraList);
93 client.sendValidate(state, paramJson,
94 bind(&ClientTool::validateCb, this, _1, nStep),
95 bind(&ClientTool::errorCb, this, _1));
96 }
97
98 void
99 newCb(const shared_ptr<RequestState> state, int& nStep)
100 {
101 std::cerr << "Step" << nStep++ << ": Please select one challenge from following types." << std::endl;
102 for (auto item : state->m_challengeList) {
103 std::cerr << "\t" << item << std::endl;
104 }
105 std::string choice;
106 std::cin >> choice;
107
108 auto challenge = ChallengeModule::createChallengeModule(choice);
109 auto requirementList = challenge->getRequirementForSelect();
110 std::list<std::string> paraList;
111 if (requirementList.size() != 0) {
112 std::cerr << "Step" << nStep++ << ": Please satisfy following instruction(s)" << std::endl;
113 for (auto item : requirementList) {
114 std::cerr << "\t" << item << std::endl;
115 }
116 for (size_t i = 0; i < requirementList.size(); i++) {
117 std::string tempParam;
118 std::cin >> tempParam;
119 paraList.push_back(tempParam);
120 }
121 }
122 auto paramJson = challenge->genSelectParamsJson(state->m_status, paraList);
123 client.sendSelect(state, choice, paramJson,
124 bind(&ClientTool::selectCb, this, _1, nStep),
125 bind(&ClientTool::errorCb, this, _1));
126 }
127
128public:
129 ClientModule& client;
130};
131
132int
133main(int argc, char* argv[])
134{
135 namespace po = boost::program_options;
136 std::string configFilePath = std::string(SYSCONFDIR) + "/ndncert/client.conf";
137 po::options_description description("General Usage\n ndncert-client [-h] [-f] configFilePath-file\n");
138 description.add_options()
139 ("help,h", "produce help message")
140 ("config-file,f", po::value<std::string>(&configFilePath), "config file name");
141 po::positional_options_description p;
142
143 po::variables_map vm;
144 try {
145 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
146 po::notify(vm);
147 }
148 catch (const std::exception& e) {
149 std::cerr << "ERROR: " << e.what() << std::endl;
150 return 1;
151 }
152 if (vm.count("help") != 0) {
153 std::cerr << description << std::endl;
154 return 0;
155 }
156
157 Face face;
158 security::v2::KeyChain keyChain;
159 ClientModule client(face, keyChain);
160 client.getClientConf().load(configFilePath);
161
162 ClientTool tool(client);
163
164 auto caList = client.getClientConf().m_caItems;
165 std::cerr << "Index \t CA Namespace \t CA Introduction" << std::endl;
166 int count = 0;
167 for (auto item : caList) {
168 std::cerr << count++ << "\t"
169 << item.m_caName << "\t"
170 << item.m_caInfo << std::endl;
171 }
172 std::vector<ClientCaItem> caVector{std::begin(caList), std::end(caList)};
173 int nStep = 0;
174 std::cerr << "Step" << nStep++ << ": Please type in the CA namespace index that you want to apply" << std::endl;
175 std::string caIndexS;
176 std::cin >> caIndexS;
177 int caIndex = std::stoi(caIndexS);
178
179 BOOST_ASSERT(caIndex <= count);
180
181 auto targetCaItem = caVector[caIndex];
182 if (targetCaItem.m_probe != "") {
183 std::cerr <<"Step" << nStep++ << ": Probe Requirement-" << targetCaItem.m_probe << std::endl;
184 std::string probeInfo;
185 std::cin >> probeInfo;
186 client.sendProbe(targetCaItem, probeInfo,
187 bind(&ClientTool::newCb, &tool, _1, nStep),
188 bind(&ClientTool::errorCb, &tool, _1));
189 }
190 else {
191 std::cerr <<"Step" << nStep++ << ": Please type in the identity name" << std::endl;
192 std::string nameComponent;
193 std::cin >> nameComponent;
194 Name identityName(targetCaItem.m_caName);
195 identityName.append(nameComponent);
196 client.sendNew(targetCaItem, identityName,
197 bind(&ClientTool::newCb, &tool, _1, nStep),
198 bind(&ClientTool::errorCb, &tool, _1));
199 }
200 face.processEvents();
201 return 0;
202}
203
204} // namespace ndncert
205} // namespace ndn
206
207int
208main(int argc, char* argv[])
209{
210 return ndn::ndncert::main(argc, argv);
211}