blob: 7f082d8170fd18a17da2a3f7cf8283d21f3c0cd2 [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
Zhiyi Zhangb6fab0f2017-09-21 16:26:27 -070025#include <iostream>
26
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080027#include <boost/program_options/options_description.hpp>
28#include <boost/program_options/variables_map.hpp>
29#include <boost/program_options/parsers.hpp>
30
31namespace ndn {
32namespace ndncert {
33
34_LOG_INIT(ndncert.clientTool);
35
36class ClientTool
37{
38public:
39 ClientTool(ClientModule& clientModule)
40 : client(clientModule)
41 {
42 }
43
44 void
45 errorCb(const std::string& errorInfo)
46 {
47 _LOG_TRACE("Error: " << errorInfo);
48 }
49
50 void
Zhiyi Zhang4d89fe02017-04-28 18:51:51 -070051 downloadCb(const shared_ptr<RequestState>& state, int& nStep)
52 {
53 _LOG_TRACE("Certificate has already been installed to local keychain");
54 return;
55 }
56
57 void
58 validateCb(const shared_ptr<RequestState>& state, int& nStep)
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080059 {
60 if (state->m_status == ChallengeModule::SUCCESS) {
61 _LOG_TRACE("Certificate has already been issued");
Zhiyi Zhang4d89fe02017-04-28 18:51:51 -070062 client.requestDownload(state,
63 bind(&ClientTool::downloadCb, this, _1, nStep),
64 bind(&ClientTool::errorCb, this, _1));
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080065 return;
66 }
67
68 auto challenge = ChallengeModule::createChallengeModule(state->m_challengeType);
69 auto requirementList = challenge->getRequirementForValidate(state->m_status);
70
71 std::cerr << "Step" << nStep++ << ": Please satisfy following instruction(s)" << std::endl;
72 for (auto requirement : requirementList) {
73 std::cerr << "\t" << requirement << std::endl;
74 }
75 std::list<std::string> paraList;
76 for (size_t i = 0; i < requirementList.size(); i++) {
77 std::string tempParam;
78 std::cin >> tempParam;
79 paraList.push_back(tempParam);
80 }
81 auto paramJson = challenge->genValidateParamsJson(state->m_status, paraList);
82 client.sendValidate(state, paramJson,
83 bind(&ClientTool::validateCb, this, _1, nStep),
84 bind(&ClientTool::errorCb, this, _1));
85 }
86
87 void
Zhiyi Zhang4d89fe02017-04-28 18:51:51 -070088 selectCb(const shared_ptr<RequestState>& state, int& nStep)
Zhiyi Zhang08e0e982017-03-01 10:10:42 -080089 {
90 auto challenge = ChallengeModule::createChallengeModule(state->m_challengeType);
91 auto requirementList = challenge->getRequirementForValidate(state->m_status);
92
93 std::cerr << "Step" << nStep++ << ": Please satisfy following instruction(s)" << std::endl;
94 for (auto item : requirementList) {
95 std::cerr << "\t" << item << std::endl;
96 }
97 std::list<std::string> paraList;
98 for (size_t i = 0; i < requirementList.size(); i++) {
99 std::string tempParam;
100 std::cin >> tempParam;
101 paraList.push_back(tempParam);
102 }
103
104 auto paramJson = challenge->genValidateParamsJson(state->m_status, paraList);
105 client.sendValidate(state, paramJson,
106 bind(&ClientTool::validateCb, this, _1, nStep),
107 bind(&ClientTool::errorCb, this, _1));
108 }
109
110 void
Zhiyi Zhang4d89fe02017-04-28 18:51:51 -0700111 newCb(const shared_ptr<RequestState>& state, int& nStep)
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800112 {
113 std::cerr << "Step" << nStep++ << ": Please select one challenge from following types." << std::endl;
114 for (auto item : state->m_challengeList) {
115 std::cerr << "\t" << item << std::endl;
116 }
117 std::string choice;
118 std::cin >> choice;
119
120 auto challenge = ChallengeModule::createChallengeModule(choice);
121 auto requirementList = challenge->getRequirementForSelect();
122 std::list<std::string> paraList;
123 if (requirementList.size() != 0) {
124 std::cerr << "Step" << nStep++ << ": Please satisfy following instruction(s)" << std::endl;
125 for (auto item : requirementList) {
126 std::cerr << "\t" << item << std::endl;
127 }
128 for (size_t i = 0; i < requirementList.size(); i++) {
129 std::string tempParam;
130 std::cin >> tempParam;
131 paraList.push_back(tempParam);
132 }
133 }
134 auto paramJson = challenge->genSelectParamsJson(state->m_status, paraList);
135 client.sendSelect(state, choice, paramJson,
136 bind(&ClientTool::selectCb, this, _1, nStep),
137 bind(&ClientTool::errorCb, this, _1));
138 }
139
140public:
141 ClientModule& client;
142};
143
144int
145main(int argc, char* argv[])
146{
147 namespace po = boost::program_options;
148 std::string configFilePath = std::string(SYSCONFDIR) + "/ndncert/client.conf";
149 po::options_description description("General Usage\n ndncert-client [-h] [-f] configFilePath-file\n");
150 description.add_options()
151 ("help,h", "produce help message")
152 ("config-file,f", po::value<std::string>(&configFilePath), "config file name");
153 po::positional_options_description p;
154
155 po::variables_map vm;
156 try {
157 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
158 po::notify(vm);
159 }
160 catch (const std::exception& e) {
161 std::cerr << "ERROR: " << e.what() << std::endl;
162 return 1;
163 }
164 if (vm.count("help") != 0) {
165 std::cerr << description << std::endl;
166 return 0;
167 }
168
169 Face face;
170 security::v2::KeyChain keyChain;
171 ClientModule client(face, keyChain);
172 client.getClientConf().load(configFilePath);
173
174 ClientTool tool(client);
175
176 auto caList = client.getClientConf().m_caItems;
177 std::cerr << "Index \t CA Namespace \t CA Introduction" << std::endl;
178 int count = 0;
179 for (auto item : caList) {
180 std::cerr << count++ << "\t"
181 << item.m_caName << "\t"
182 << item.m_caInfo << std::endl;
183 }
184 std::vector<ClientCaItem> caVector{std::begin(caList), std::end(caList)};
185 int nStep = 0;
186 std::cerr << "Step" << nStep++ << ": Please type in the CA namespace index that you want to apply" << std::endl;
187 std::string caIndexS;
188 std::cin >> caIndexS;
189 int caIndex = std::stoi(caIndexS);
190
191 BOOST_ASSERT(caIndex <= count);
192
193 auto targetCaItem = caVector[caIndex];
194 if (targetCaItem.m_probe != "") {
195 std::cerr <<"Step" << nStep++ << ": Probe Requirement-" << targetCaItem.m_probe << std::endl;
196 std::string probeInfo;
197 std::cin >> probeInfo;
198 client.sendProbe(targetCaItem, probeInfo,
199 bind(&ClientTool::newCb, &tool, _1, nStep),
200 bind(&ClientTool::errorCb, &tool, _1));
201 }
202 else {
203 std::cerr <<"Step" << nStep++ << ": Please type in the identity name" << std::endl;
204 std::string nameComponent;
205 std::cin >> nameComponent;
Zhiyi Zhang4d89fe02017-04-28 18:51:51 -0700206 Name identityName = targetCaItem.m_caName.getPrefix(-1);
Zhiyi Zhang08e0e982017-03-01 10:10:42 -0800207 identityName.append(nameComponent);
208 client.sendNew(targetCaItem, identityName,
209 bind(&ClientTool::newCb, &tool, _1, nStep),
210 bind(&ClientTool::errorCb, &tool, _1));
211 }
212 face.processEvents();
213 return 0;
214}
215
216} // namespace ndncert
217} // namespace ndn
218
219int
220main(int argc, char* argv[])
221{
222 return ndn::ndncert::main(argc, argv);
223}