blob: b810109714976fffa34811afa6d91e19d4cc0119 [file] [log] [blame]
Shock Jiangae429122014-10-24 09:04:58 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yumin Xia2c509c22017-02-09 14:37:36 -08002/*
Davide Pesavento1bff1b22020-06-08 18:46:05 -04003 * Copyright (c) 2014-2020, Regents of the University of California.
Shock Jiangae429122014-10-24 09:04:58 -07004 *
5 * This file is part of NDNS (Named Data Networking Domain Name Service).
6 * See AUTHORS.md for complete list of NDNS authors and contributors.
7 *
8 * NDNS is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "clients/response.hpp"
21#include "clients/query.hpp"
22#include "ndns-label.hpp"
Yumin Xia99c821a2017-04-07 11:01:08 -070023#include "validator/validator.hpp"
Shock Jiangae429122014-10-24 09:04:58 -070024#include "ndns-enum.hpp"
25#include "ndns-tlv.hpp"
26#include "logger.hpp"
27#include "daemon/db-mgr.hpp"
28#include "util/util.hpp"
Yumin Xia2c509c22017-02-09 14:37:36 -080029#include "util/cert-helper.hpp"
Shock Jiangae429122014-10-24 09:04:58 -070030
31#include <ndn-cxx/security/key-chain.hpp>
Yumin Xia2c509c22017-02-09 14:37:36 -080032#include <ndn-cxx/security/signing-helpers.hpp>
Shock Jiangae429122014-10-24 09:04:58 -070033#include <ndn-cxx/data.hpp>
34#include <ndn-cxx/util/io.hpp>
Yumin Xia2c509c22017-02-09 14:37:36 -080035#include <ndn-cxx/util/regex.hpp>
Shock Jiangae429122014-10-24 09:04:58 -070036#include <ndn-cxx/encoding/block.hpp>
37#include <ndn-cxx/encoding/block-helpers.hpp>
38#include <boost/noncopyable.hpp>
39#include <boost/program_options.hpp>
40#include <boost/asio.hpp>
41#include <boost/filesystem.hpp>
42
43#include <string>
44#include <tuple>
45
46namespace ndn {
47namespace ndns {
Alexander Afanasyevc7c99002015-10-09 17:27:30 -070048
Alexander Afanasyev08d18742018-03-15 16:31:28 -040049NDNS_LOG_INIT(NdnsUpdate);
Shock Jiangae429122014-10-24 09:04:58 -070050
51class NdnsUpdate : noncopyable
52{
53public:
Yumin Xia6343c5b2016-10-20 15:45:50 -070054 NdnsUpdate(const Name& zone, const shared_ptr<Data>& update, Face& face)
55 : m_zone(zone)
Shock Jiangae429122014-10-24 09:04:58 -070056 , m_interestLifetime(DEFAULT_INTEREST_LIFETIME)
57 , m_face(face)
Yumin Xia2c509c22017-02-09 14:37:36 -080058 , m_validator(NdnsValidatorBuilder::create(face))
Shock Jiang43e59b42014-12-02 15:05:02 -080059 , m_update(update)
Shock Jiangae429122014-10-24 09:04:58 -070060 , m_hasError(false)
61 {
Shock Jiangae429122014-10-24 09:04:58 -070062 }
63
64 void
Shock Jiang43e59b42014-12-02 15:05:02 -080065 start()
Shock Jiangae429122014-10-24 09:04:58 -070066 {
Shock Jiang43e59b42014-12-02 15:05:02 -080067 NDNS_LOG_INFO(" ================ "
Shock Jiangae429122014-10-24 09:04:58 -070068 << "start to update RR at Zone = " << this->m_zone
Shock Jiang43e59b42014-12-02 15:05:02 -080069 << " new RR is: " << m_update->getName()
70 <<" =================== ");
Davide Pesavento1bff1b22020-06-08 18:46:05 -040071 NDNS_LOG_INFO("new RR is signed by: " << m_update->getKeyLocator()->getName());
Shock Jiangae429122014-10-24 09:04:58 -070072
73 Interest interest = this->makeUpdateInterest();
Shock Jiang43e59b42014-12-02 15:05:02 -080074 NDNS_LOG_TRACE("[* <- *] send Update: " << m_update->getName().toUri());
Shock Jiangae429122014-10-24 09:04:58 -070075 m_face.expressInterest(interest,
76 bind(&NdnsUpdate::onData, this, _1, _2),
Alexander Afanasyevf4193ea2017-06-12 15:35:13 -070077 bind(&NdnsUpdate::onTimeout, this, _1), // nack
Shock Jiangae429122014-10-24 09:04:58 -070078 bind(&NdnsUpdate::onTimeout, this, _1) //dynamic binding
79 );
Shock Jiangae429122014-10-24 09:04:58 -070080 }
81
82 void
83 stop()
84 {
85 m_face.getIoService().stop();
86 }
87
88private:
89 void
Davide Pesavento1bff1b22020-06-08 18:46:05 -040090 onData(const Interest&, const Data& data)
Shock Jiangae429122014-10-24 09:04:58 -070091 {
92 NDNS_LOG_INFO("get response of Update");
93 int ret = -1;
94 std::string msg;
95 std::tie(ret, msg) = this->parseResponse(data);
96 NDNS_LOG_INFO("Return Code: " << ret << ", and Update "
97 << (ret == UPDATE_OK ? "succeeds" : "fails"));
98 if (ret != UPDATE_OK)
99 m_hasError = true;
100
101 if (!msg.empty()) {
102 NDNS_LOG_INFO("Return Msg: " << msg);
103 }
104
105 NDNS_LOG_INFO("to verify the response");
Yumin Xia2c509c22017-02-09 14:37:36 -0800106 m_validator->validate(data,
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400107 bind(&NdnsUpdate::onDataValidated, this, _1),
108 bind(&NdnsUpdate::onDataValidationFailed, this, _1, _2));
Shock Jiangae429122014-10-24 09:04:58 -0700109 }
110
111 std::tuple<int, std::string>
112 parseResponse(const Data& data)
113 {
114 int ret = -1;
115 std::string msg;
116 Block blk = data.getContent();
117 blk.parse();
118 Block block = blk.blockFromValue();
119 block.parse();
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400120 auto val = block.elements_begin();
Shock Jiangae429122014-10-24 09:04:58 -0700121 for (; val != block.elements_end(); ++val) {
122 if (val->type() == ndns::tlv::UpdateReturnCode) { // the first must be return code
123 ret = readNonNegativeInteger(*val);
124 }
125 else if (val->type() == ndns::tlv::UpdateReturnMsg) {
126 msg = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
127 }
128 }
129
130 return std::make_tuple(ret, msg);
131 }
132
133 /**
134 * @brief construct a query (interest) which contains the update information
135 */
136 Interest
137 makeUpdateInterest()
138 {
Yumin Xia6343c5b2016-10-20 15:45:50 -0700139 Query q(m_zone, label::NDNS_ITERATIVE_QUERY);
Shock Jiang43e59b42014-12-02 15:05:02 -0800140 q.setRrLabel(Name().append(m_update->wireEncode()));
Shock Jiangae429122014-10-24 09:04:58 -0700141 q.setRrType(label::NDNS_UPDATE_LABEL);
142 q.setInterestLifetime(m_interestLifetime);
143
144 return q.toInterest();
145 }
146
147private:
148 void
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400149 onTimeout(const ndn::Interest&)
Shock Jiangae429122014-10-24 09:04:58 -0700150 {
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400151 NDNS_LOG_TRACE("Update timeout");
Shock Jiangae429122014-10-24 09:04:58 -0700152 m_hasError = true;
153 this->stop();
154 }
155
156 void
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400157 onDataValidated(const Data&)
Shock Jiangae429122014-10-24 09:04:58 -0700158 {
159 NDNS_LOG_INFO("data pass verification");
160 this->stop();
161 }
162
163 void
Alexander Afanasyev60514ec2020-06-03 14:18:53 -0400164 onDataValidationFailed(const Data&, const security::ValidationError&)
Shock Jiangae429122014-10-24 09:04:58 -0700165 {
166 NDNS_LOG_INFO("data does not pass verification");
167 m_hasError = true;
168 this->stop();
169 }
170
171public:
Shock Jiangae429122014-10-24 09:04:58 -0700172 void
173 setInterestLifetime(const time::milliseconds& interestLifetime)
174 {
175 m_interestLifetime = interestLifetime;
176 }
177
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800178 bool
Shock Jiangae429122014-10-24 09:04:58 -0700179 hasError() const
180 {
181 return m_hasError;
182 }
183
184private:
Shock Jiangae429122014-10-24 09:04:58 -0700185 Name m_zone;
Shock Jiang43e59b42014-12-02 15:05:02 -0800186
Shock Jiangae429122014-10-24 09:04:58 -0700187 time::milliseconds m_interestLifetime;
188
189 Face& m_face;
Alexander Afanasyev60514ec2020-06-03 14:18:53 -0400190 unique_ptr<security::Validator> m_validator;
Shock Jiangae429122014-10-24 09:04:58 -0700191 KeyChain m_keyChain;
192
Shock Jiang43e59b42014-12-02 15:05:02 -0800193 shared_ptr<Data> m_update;
Shock Jiangae429122014-10-24 09:04:58 -0700194 bool m_hasError;
195};
196
197} // namespace ndns
198} // namespace ndn
199
200int
201main(int argc, char* argv[])
202{
Shock Jiangae429122014-10-24 09:04:58 -0700203 using std::string;
204 using namespace ndn;
205 using namespace ndn::ndns;
206
Shock Jiangae429122014-10-24 09:04:58 -0700207 Name zone;
208 int ttl = 4;
209 Name rrLabel;
210 string rrType = "TXT";
Yumin Xiaa484ba72016-11-10 20:40:12 -0800211 string contentTypeStr = "resp";
Shock Jiangae429122014-10-24 09:04:58 -0700212 Name certName;
Shock Jiang06cd2142014-11-23 17:36:02 -0800213 std::vector<string> contents;
Shock Jiangae429122014-10-24 09:04:58 -0700214 string contentFile;
Shock Jiang43e59b42014-12-02 15:05:02 -0800215 shared_ptr<Data> update;
216
Shock Jiangae429122014-10-24 09:04:58 -0700217 try {
218 namespace po = boost::program_options;
219 po::variables_map vm;
220
221 po::options_description generic("Generic Options");
222 generic.add_options()("help,h", "print help message");
223
224 po::options_description config("Configuration");
225 config.add_options()
Shock Jiangae429122014-10-24 09:04:58 -0700226 ("ttl,T", po::value<int>(&ttl), "TTL of query. default: 4 sec")
227 ("rrtype,t", po::value<string>(&rrType), "set request RR Type. default: TXT")
Yumin Xiaa484ba72016-11-10 20:40:12 -0800228 ("contentType,n", po::value<string>(&contentTypeStr), "Set the contentType of the resource record. "
229 "Potential values are [blob|link|nack|auth|resp]. Default: resp")
Shock Jiangae429122014-10-24 09:04:58 -0700230 ("cert,c", po::value<Name>(&certName), "set the name of certificate to sign the update")
Shock Jiang06cd2142014-11-23 17:36:02 -0800231 ("content,o", po::value<std::vector<string>>(&contents)->multitoken(),
232 "set the content of the RR")
Shock Jiangae429122014-10-24 09:04:58 -0700233 ("contentFile,f", po::value<string>(&contentFile), "set the path of file which contain"
Shock Jiang43e59b42014-12-02 15:05:02 -0800234 " Response packet in base64 format")
Shock Jiangae429122014-10-24 09:04:58 -0700235 ;
236
237 po::options_description hidden("Hidden Options");
238 hidden.add_options()
239 ("zone,z", po::value<Name>(&zone), "zone the record is delegated")
240 ("rrlabel,l", po::value<Name>(&rrLabel), "set request RR Label")
241 ;
242 po::positional_options_description postion;
243 postion.add("zone", 1);
244 postion.add("rrlabel", 1);
245
246 po::options_description cmdline_options;
247 cmdline_options.add(generic).add(config).add(hidden);
248
249 po::options_description config_file_options;
250 config_file_options.add(config).add(hidden);
251
Shock Jiang43e59b42014-12-02 15:05:02 -0800252 po::options_description visible("Usage: ndns-update zone rrLabel [-t rrType] [-T TTL] "
Yumin Xiaa484ba72016-11-10 20:40:12 -0800253 "[-n NdnsContentType] [-c cert] "
Shock Jiang43e59b42014-12-02 15:05:02 -0800254 "[-f contentFile]|[-o content]\n"
255 "Allowed options");
256
Shock Jiangae429122014-10-24 09:04:58 -0700257 visible.add(generic).add(config);
258
259 po::parsed_options parsed =
260 po::command_line_parser(argc, argv).options(cmdline_options).positional(postion).run();
261
262 po::store(parsed, vm);
263 po::notify(vm);
264
265 if (vm.count("help")) {
Shock Jiangae429122014-10-24 09:04:58 -0700266 std::cout << visible << std::endl;
267 return 0;
268 }
269
Shock Jiang06cd2142014-11-23 17:36:02 -0800270
Shock Jiangae429122014-10-24 09:04:58 -0700271 if (vm.count("content") && vm.count("contentFile")) {
Shock Jiang43e59b42014-12-02 15:05:02 -0800272 std::cerr << "both -o content and -f contentFile are set. Only one is allowed" << std::endl;
273 return 1;
Shock Jiangae429122014-10-24 09:04:58 -0700274 }
275
Shock Jiang43e59b42014-12-02 15:05:02 -0800276 if (!vm.count("contentFile")) {
277 NDNS_LOG_TRACE("content option is set. try to figure out the certificate");
278 if (!vm.count("zone") || !vm.count("rrlabel")) {
279 std::cerr << "-o option must be set together with -z zone and -r rrLabel" << std::endl;
280 return 1;
281 }
282
283 KeyChain keyChain;
284 if (certName.empty()) {
285 Name name = Name().append(zone).append(rrLabel);
286 // choosing the longest match of the identity who also have default certificate
287 for (size_t i = name.size() + 1; i > 0; --i) { // i >=0 will present warnning
288 Name tmp = name.getPrefix(i - 1);
Yumin Xia2c509c22017-02-09 14:37:36 -0800289 if (CertHelper::doesIdentityExist(keyChain, tmp)) {
Shock Jiang43e59b42014-12-02 15:05:02 -0800290 try {
Yumin Xia2c509c22017-02-09 14:37:36 -0800291 certName = CertHelper::getDefaultCertificateNameOfIdentity(keyChain, tmp);
Shock Jiang43e59b42014-12-02 15:05:02 -0800292 break;
293 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800294 catch (const std::exception&) {
Shock Jiang43e59b42014-12-02 15:05:02 -0800295 // If it cannot get a default certificate from one identity,
296 // just ignore this one try next identity.
297 ;
298 }
299 }
300 } // for
301
302 if (certName.empty()) {
303 std::cerr << "cannot figure out the certificate automatically. "
304 << "please set it with -c CERT_NAEME" << std::endl;
305 return 1;
306 }
307 }
Shock Jiang43e59b42014-12-02 15:05:02 -0800308
Yumin Xiaa484ba72016-11-10 20:40:12 -0800309 NdnsContentType contentType = toNdnsContentType(contentTypeStr);
Shock Jiang43e59b42014-12-02 15:05:02 -0800310
Yumin Xiaa484ba72016-11-10 20:40:12 -0800311 if (contentType == ndns::NDNS_UNKNOWN) {
312 std::cerr << "unknown NdnsContentType: " << contentTypeStr << std::endl;
Shock Jiang43e59b42014-12-02 15:05:02 -0800313 return 1;
314 }
315
316 Response re;
317 re.setZone(zone);
318 re.setRrLabel(rrLabel);
Yumin Xia918343d2017-03-17 19:04:55 -0700319 name::Component qType = ndns::label::NDNS_ITERATIVE_QUERY;
Shock Jiang43e59b42014-12-02 15:05:02 -0800320
321 re.setQueryType(qType);
322 re.setRrType(name::Component(rrType));
Yumin Xiaa484ba72016-11-10 20:40:12 -0800323 re.setContentType(contentType);
Shock Jiang43e59b42014-12-02 15:05:02 -0800324
325 for (const auto& content : contents) {
Junxiao Shi767f35c2016-07-23 01:54:42 +0000326 re.addRr(makeBinaryBlock(ndns::tlv::RrData, content.c_str(), content.size()));
Shock Jiang43e59b42014-12-02 15:05:02 -0800327
328 // re.addRr(content);
329 }
330
331 update = re.toData();
Yumin Xia2c509c22017-02-09 14:37:36 -0800332 keyChain.sign(*update, security::signingByCertificate(certName));
Shock Jiang43e59b42014-12-02 15:05:02 -0800333 }
334 else {
335 try {
336 update = ndn::io::load<ndn::Data>(contentFile);
337 NDNS_LOG_TRACE("load data " << update->getName() << " from content file: " << contentFile);
338 }
339 catch (const std::exception& e) {
340 std::cerr << "Error: load Data packet from file: " << contentFile
341 << ". Due to: " << e.what() << std::endl;
342 return 1;
343 }
344
345 try {
346 // must check the Data is a legal Response with right name
Yumin Xia918343d2017-03-17 19:04:55 -0700347 shared_ptr<Regex> regex = make_shared<Regex>("(<>*)<NDNS>(<>+)<CERT><>*");
Shock Jiang43e59b42014-12-02 15:05:02 -0800348 shared_ptr<Regex> regex2 = make_shared<Regex>("(<>*)<NDNS>(<>+)");
349
350 Name zone2;
351 if (regex->match(update->getName())) {
352 zone2 = regex->expand("\\1");
353 }
354 else if (regex2->match(update->getName())) {
355 zone2 = regex2->expand("\\1");
356 }
357 else {
358 std::cerr << "The loaded Data packet cannot be stored in NDNS "
359 "since its does not have a proper name" << std::endl;
360 return 1;
361 }
362
363 if (vm.count("zone") && zone != zone2) {
364 std::cerr << "The loaded Data packet is supposed to be stored at zone: " << zone2
365 << " instead of zone: " << zone << std::endl;
366 return 1;
367 }
368 else {
369 zone = zone2;
370 }
371
372 Response re;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700373 re.fromData(zone, *update);
Shock Jiang43e59b42014-12-02 15:05:02 -0800374
375 if (vm.count("rrlabel") && rrLabel != re.getRrLabel()) {
376 std::cerr << "The loaded Data packet is supposed to have rrLabel: " << re.getRrLabel()
377 << " instead of label: " << rrLabel << std::endl;
378 return 1;
379 }
380
381 if (vm.count("rrtype") && name::Component(rrType) != re.getRrType()) {
382 std::cerr << "The loaded Data packet is supposed to have rrType: " << re.getRrType()
383 << " instead of label: " << rrType << std::endl;
384 return 1;
385 }
386 }
387 catch (const std::exception& e) {
388 std::cerr << "Error: the loaded Data packet cannot parse to a Response stored at zone: "
389 << zone << std::endl;
390 return 1;
391 }
392
Shock Jiangae429122014-10-24 09:04:58 -0700393 }
Shock Jiangae429122014-10-24 09:04:58 -0700394 }
395 catch (const std::exception& ex) {
396 std::cerr << "Parameter Error: " << ex.what() << std::endl;
Shock Jiang43e59b42014-12-02 15:05:02 -0800397 return 1;
Shock Jiangae429122014-10-24 09:04:58 -0700398 }
399
400 Face face;
Shock Jiang43e59b42014-12-02 15:05:02 -0800401 try {
Yumin Xia6343c5b2016-10-20 15:45:50 -0700402 NdnsUpdate updater(zone, update, face);
Shock Jiang43e59b42014-12-02 15:05:02 -0800403 updater.setInterestLifetime(ndn::time::seconds(ttl));
Shock Jiangae429122014-10-24 09:04:58 -0700404
Shock Jiang43e59b42014-12-02 15:05:02 -0800405 updater.start();
406 face.processEvents();
407 if (updater.hasError())
408 return 1;
409 else
Shock Jiang06cd2142014-11-23 17:36:02 -0800410 return 0;
Shock Jiangae429122014-10-24 09:04:58 -0700411 }
Shock Jiang43e59b42014-12-02 15:05:02 -0800412 catch (const std::exception& e) {
413 std::cerr << "Error: " << e.what() << std::endl;
414 return 1;
415 }
Shock Jiangae429122014-10-24 09:04:58 -0700416}