blob: 9c23a5bab26c56adb33c698314a51a167dd23c22 [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>
Shock Jiangae429122014-10-24 09:04:58 -070038
Davide Pesavento948c50c2020-12-26 21:30:45 -050039#include <boost/asio/io_service.hpp>
40#include <boost/program_options.hpp>
41
42#include <iostream>
Shock Jiangae429122014-10-24 09:04:58 -070043#include <tuple>
44
45namespace ndn {
46namespace ndns {
Alexander Afanasyevc7c99002015-10-09 17:27:30 -070047
Alexander Afanasyev08d18742018-03-15 16:31:28 -040048NDNS_LOG_INIT(NdnsUpdate);
Shock Jiangae429122014-10-24 09:04:58 -070049
Davide Pesavento948c50c2020-12-26 21:30:45 -050050class NdnsUpdate : boost::noncopyable
Shock Jiangae429122014-10-24 09:04:58 -070051{
52public:
Yumin Xia6343c5b2016-10-20 15:45:50 -070053 NdnsUpdate(const Name& zone, const shared_ptr<Data>& update, Face& face)
54 : m_zone(zone)
Shock Jiangae429122014-10-24 09:04:58 -070055 , m_interestLifetime(DEFAULT_INTEREST_LIFETIME)
56 , m_face(face)
Yumin Xia2c509c22017-02-09 14:37:36 -080057 , m_validator(NdnsValidatorBuilder::create(face))
Shock Jiang43e59b42014-12-02 15:05:02 -080058 , m_update(update)
Shock Jiangae429122014-10-24 09:04:58 -070059 , m_hasError(false)
60 {
Shock Jiangae429122014-10-24 09:04:58 -070061 }
62
63 void
Shock Jiang43e59b42014-12-02 15:05:02 -080064 start()
Shock Jiangae429122014-10-24 09:04:58 -070065 {
Shock Jiang43e59b42014-12-02 15:05:02 -080066 NDNS_LOG_INFO(" ================ "
Shock Jiangae429122014-10-24 09:04:58 -070067 << "start to update RR at Zone = " << this->m_zone
Shock Jiang43e59b42014-12-02 15:05:02 -080068 << " new RR is: " << m_update->getName()
69 <<" =================== ");
Davide Pesavento1bff1b22020-06-08 18:46:05 -040070 NDNS_LOG_INFO("new RR is signed by: " << m_update->getKeyLocator()->getName());
Shock Jiangae429122014-10-24 09:04:58 -070071
72 Interest interest = this->makeUpdateInterest();
Shock Jiang43e59b42014-12-02 15:05:02 -080073 NDNS_LOG_TRACE("[* <- *] send Update: " << m_update->getName().toUri());
Shock Jiangae429122014-10-24 09:04:58 -070074 m_face.expressInterest(interest,
75 bind(&NdnsUpdate::onData, this, _1, _2),
Alexander Afanasyevf4193ea2017-06-12 15:35:13 -070076 bind(&NdnsUpdate::onTimeout, this, _1), // nack
Shock Jiangae429122014-10-24 09:04:58 -070077 bind(&NdnsUpdate::onTimeout, this, _1) //dynamic binding
78 );
Shock Jiangae429122014-10-24 09:04:58 -070079 }
80
81 void
82 stop()
83 {
84 m_face.getIoService().stop();
85 }
86
87private:
88 void
Davide Pesavento1bff1b22020-06-08 18:46:05 -040089 onData(const Interest&, const Data& data)
Shock Jiangae429122014-10-24 09:04:58 -070090 {
91 NDNS_LOG_INFO("get response of Update");
92 int ret = -1;
93 std::string msg;
94 std::tie(ret, msg) = this->parseResponse(data);
95 NDNS_LOG_INFO("Return Code: " << ret << ", and Update "
96 << (ret == UPDATE_OK ? "succeeds" : "fails"));
97 if (ret != UPDATE_OK)
98 m_hasError = true;
99
100 if (!msg.empty()) {
101 NDNS_LOG_INFO("Return Msg: " << msg);
102 }
103
104 NDNS_LOG_INFO("to verify the response");
Yumin Xia2c509c22017-02-09 14:37:36 -0800105 m_validator->validate(data,
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400106 bind(&NdnsUpdate::onDataValidated, this, _1),
107 bind(&NdnsUpdate::onDataValidationFailed, this, _1, _2));
Shock Jiangae429122014-10-24 09:04:58 -0700108 }
109
110 std::tuple<int, std::string>
111 parseResponse(const Data& data)
112 {
113 int ret = -1;
114 std::string msg;
115 Block blk = data.getContent();
116 blk.parse();
117 Block block = blk.blockFromValue();
118 block.parse();
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400119 auto val = block.elements_begin();
Shock Jiangae429122014-10-24 09:04:58 -0700120 for (; val != block.elements_end(); ++val) {
121 if (val->type() == ndns::tlv::UpdateReturnCode) { // the first must be return code
122 ret = readNonNegativeInteger(*val);
123 }
124 else if (val->type() == ndns::tlv::UpdateReturnMsg) {
125 msg = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
126 }
127 }
128
129 return std::make_tuple(ret, msg);
130 }
131
132 /**
133 * @brief construct a query (interest) which contains the update information
134 */
135 Interest
136 makeUpdateInterest()
137 {
Yumin Xia6343c5b2016-10-20 15:45:50 -0700138 Query q(m_zone, label::NDNS_ITERATIVE_QUERY);
Shock Jiang43e59b42014-12-02 15:05:02 -0800139 q.setRrLabel(Name().append(m_update->wireEncode()));
Shock Jiangae429122014-10-24 09:04:58 -0700140 q.setRrType(label::NDNS_UPDATE_LABEL);
141 q.setInterestLifetime(m_interestLifetime);
142
143 return q.toInterest();
144 }
145
146private:
147 void
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400148 onTimeout(const ndn::Interest&)
Shock Jiangae429122014-10-24 09:04:58 -0700149 {
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400150 NDNS_LOG_TRACE("Update timeout");
Shock Jiangae429122014-10-24 09:04:58 -0700151 m_hasError = true;
152 this->stop();
153 }
154
155 void
Davide Pesavento1bff1b22020-06-08 18:46:05 -0400156 onDataValidated(const Data&)
Shock Jiangae429122014-10-24 09:04:58 -0700157 {
158 NDNS_LOG_INFO("data pass verification");
159 this->stop();
160 }
161
162 void
Alexander Afanasyev60514ec2020-06-03 14:18:53 -0400163 onDataValidationFailed(const Data&, const security::ValidationError&)
Shock Jiangae429122014-10-24 09:04:58 -0700164 {
165 NDNS_LOG_INFO("data does not pass verification");
166 m_hasError = true;
167 this->stop();
168 }
169
170public:
Shock Jiangae429122014-10-24 09:04:58 -0700171 void
172 setInterestLifetime(const time::milliseconds& interestLifetime)
173 {
174 m_interestLifetime = interestLifetime;
175 }
176
Alexander Afanasyev984ca9d2016-12-19 13:09:14 -0800177 bool
Shock Jiangae429122014-10-24 09:04:58 -0700178 hasError() const
179 {
180 return m_hasError;
181 }
182
183private:
Shock Jiangae429122014-10-24 09:04:58 -0700184 Name m_zone;
Shock Jiang43e59b42014-12-02 15:05:02 -0800185
Shock Jiangae429122014-10-24 09:04:58 -0700186 time::milliseconds m_interestLifetime;
187
188 Face& m_face;
Alexander Afanasyev60514ec2020-06-03 14:18:53 -0400189 unique_ptr<security::Validator> m_validator;
Shock Jiangae429122014-10-24 09:04:58 -0700190 KeyChain m_keyChain;
191
Shock Jiang43e59b42014-12-02 15:05:02 -0800192 shared_ptr<Data> m_update;
Shock Jiangae429122014-10-24 09:04:58 -0700193 bool m_hasError;
194};
195
196} // namespace ndns
197} // namespace ndn
198
199int
200main(int argc, char* argv[])
201{
Shock Jiangae429122014-10-24 09:04:58 -0700202 using std::string;
203 using namespace ndn;
204 using namespace ndn::ndns;
205
Shock Jiangae429122014-10-24 09:04:58 -0700206 Name zone;
207 int ttl = 4;
208 Name rrLabel;
209 string rrType = "TXT";
Yumin Xiaa484ba72016-11-10 20:40:12 -0800210 string contentTypeStr = "resp";
Shock Jiangae429122014-10-24 09:04:58 -0700211 Name certName;
Shock Jiang06cd2142014-11-23 17:36:02 -0800212 std::vector<string> contents;
Shock Jiangae429122014-10-24 09:04:58 -0700213 string contentFile;
Shock Jiang43e59b42014-12-02 15:05:02 -0800214 shared_ptr<Data> update;
215
Shock Jiangae429122014-10-24 09:04:58 -0700216 try {
217 namespace po = boost::program_options;
218 po::variables_map vm;
219
220 po::options_description generic("Generic Options");
221 generic.add_options()("help,h", "print help message");
222
223 po::options_description config("Configuration");
224 config.add_options()
Shock Jiangae429122014-10-24 09:04:58 -0700225 ("ttl,T", po::value<int>(&ttl), "TTL of query. default: 4 sec")
226 ("rrtype,t", po::value<string>(&rrType), "set request RR Type. default: TXT")
Yumin Xiaa484ba72016-11-10 20:40:12 -0800227 ("contentType,n", po::value<string>(&contentTypeStr), "Set the contentType of the resource record. "
228 "Potential values are [blob|link|nack|auth|resp]. Default: resp")
Shock Jiangae429122014-10-24 09:04:58 -0700229 ("cert,c", po::value<Name>(&certName), "set the name of certificate to sign the update")
Shock Jiang06cd2142014-11-23 17:36:02 -0800230 ("content,o", po::value<std::vector<string>>(&contents)->multitoken(),
231 "set the content of the RR")
Shock Jiangae429122014-10-24 09:04:58 -0700232 ("contentFile,f", po::value<string>(&contentFile), "set the path of file which contain"
Shock Jiang43e59b42014-12-02 15:05:02 -0800233 " Response packet in base64 format")
Shock Jiangae429122014-10-24 09:04:58 -0700234 ;
235
236 po::options_description hidden("Hidden Options");
237 hidden.add_options()
238 ("zone,z", po::value<Name>(&zone), "zone the record is delegated")
239 ("rrlabel,l", po::value<Name>(&rrLabel), "set request RR Label")
240 ;
241 po::positional_options_description postion;
242 postion.add("zone", 1);
243 postion.add("rrlabel", 1);
244
245 po::options_description cmdline_options;
246 cmdline_options.add(generic).add(config).add(hidden);
247
248 po::options_description config_file_options;
249 config_file_options.add(config).add(hidden);
250
Shock Jiang43e59b42014-12-02 15:05:02 -0800251 po::options_description visible("Usage: ndns-update zone rrLabel [-t rrType] [-T TTL] "
Yumin Xiaa484ba72016-11-10 20:40:12 -0800252 "[-n NdnsContentType] [-c cert] "
Shock Jiang43e59b42014-12-02 15:05:02 -0800253 "[-f contentFile]|[-o content]\n"
254 "Allowed options");
255
Shock Jiangae429122014-10-24 09:04:58 -0700256 visible.add(generic).add(config);
257
258 po::parsed_options parsed =
259 po::command_line_parser(argc, argv).options(cmdline_options).positional(postion).run();
260
261 po::store(parsed, vm);
262 po::notify(vm);
263
264 if (vm.count("help")) {
Shock Jiangae429122014-10-24 09:04:58 -0700265 std::cout << visible << std::endl;
266 return 0;
267 }
268
Shock Jiang06cd2142014-11-23 17:36:02 -0800269
Shock Jiangae429122014-10-24 09:04:58 -0700270 if (vm.count("content") && vm.count("contentFile")) {
Shock Jiang43e59b42014-12-02 15:05:02 -0800271 std::cerr << "both -o content and -f contentFile are set. Only one is allowed" << std::endl;
272 return 1;
Shock Jiangae429122014-10-24 09:04:58 -0700273 }
274
Shock Jiang43e59b42014-12-02 15:05:02 -0800275 if (!vm.count("contentFile")) {
276 NDNS_LOG_TRACE("content option is set. try to figure out the certificate");
277 if (!vm.count("zone") || !vm.count("rrlabel")) {
278 std::cerr << "-o option must be set together with -z zone and -r rrLabel" << std::endl;
279 return 1;
280 }
281
282 KeyChain keyChain;
283 if (certName.empty()) {
284 Name name = Name().append(zone).append(rrLabel);
285 // choosing the longest match of the identity who also have default certificate
286 for (size_t i = name.size() + 1; i > 0; --i) { // i >=0 will present warnning
287 Name tmp = name.getPrefix(i - 1);
Yumin Xia2c509c22017-02-09 14:37:36 -0800288 if (CertHelper::doesIdentityExist(keyChain, tmp)) {
Shock Jiang43e59b42014-12-02 15:05:02 -0800289 try {
Yumin Xia2c509c22017-02-09 14:37:36 -0800290 certName = CertHelper::getDefaultCertificateNameOfIdentity(keyChain, tmp);
Shock Jiang43e59b42014-12-02 15:05:02 -0800291 break;
292 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800293 catch (const std::exception&) {
Shock Jiang43e59b42014-12-02 15:05:02 -0800294 // If it cannot get a default certificate from one identity,
295 // just ignore this one try next identity.
296 ;
297 }
298 }
299 } // for
300
301 if (certName.empty()) {
302 std::cerr << "cannot figure out the certificate automatically. "
303 << "please set it with -c CERT_NAEME" << std::endl;
304 return 1;
305 }
306 }
Shock Jiang43e59b42014-12-02 15:05:02 -0800307
Yumin Xiaa484ba72016-11-10 20:40:12 -0800308 NdnsContentType contentType = toNdnsContentType(contentTypeStr);
Shock Jiang43e59b42014-12-02 15:05:02 -0800309
Yumin Xiaa484ba72016-11-10 20:40:12 -0800310 if (contentType == ndns::NDNS_UNKNOWN) {
311 std::cerr << "unknown NdnsContentType: " << contentTypeStr << std::endl;
Shock Jiang43e59b42014-12-02 15:05:02 -0800312 return 1;
313 }
314
315 Response re;
316 re.setZone(zone);
317 re.setRrLabel(rrLabel);
Yumin Xia918343d2017-03-17 19:04:55 -0700318 name::Component qType = ndns::label::NDNS_ITERATIVE_QUERY;
Shock Jiang43e59b42014-12-02 15:05:02 -0800319
320 re.setQueryType(qType);
321 re.setRrType(name::Component(rrType));
Yumin Xiaa484ba72016-11-10 20:40:12 -0800322 re.setContentType(contentType);
Shock Jiang43e59b42014-12-02 15:05:02 -0800323
324 for (const auto& content : contents) {
Junxiao Shi767f35c2016-07-23 01:54:42 +0000325 re.addRr(makeBinaryBlock(ndns::tlv::RrData, content.c_str(), content.size()));
Shock Jiang43e59b42014-12-02 15:05:02 -0800326
327 // re.addRr(content);
328 }
329
330 update = re.toData();
Yumin Xia2c509c22017-02-09 14:37:36 -0800331 keyChain.sign(*update, security::signingByCertificate(certName));
Shock Jiang43e59b42014-12-02 15:05:02 -0800332 }
333 else {
334 try {
335 update = ndn::io::load<ndn::Data>(contentFile);
336 NDNS_LOG_TRACE("load data " << update->getName() << " from content file: " << contentFile);
337 }
338 catch (const std::exception& e) {
339 std::cerr << "Error: load Data packet from file: " << contentFile
340 << ". Due to: " << e.what() << std::endl;
341 return 1;
342 }
343
344 try {
345 // must check the Data is a legal Response with right name
Yumin Xia918343d2017-03-17 19:04:55 -0700346 shared_ptr<Regex> regex = make_shared<Regex>("(<>*)<NDNS>(<>+)<CERT><>*");
Shock Jiang43e59b42014-12-02 15:05:02 -0800347 shared_ptr<Regex> regex2 = make_shared<Regex>("(<>*)<NDNS>(<>+)");
348
349 Name zone2;
350 if (regex->match(update->getName())) {
351 zone2 = regex->expand("\\1");
352 }
353 else if (regex2->match(update->getName())) {
354 zone2 = regex2->expand("\\1");
355 }
356 else {
357 std::cerr << "The loaded Data packet cannot be stored in NDNS "
358 "since its does not have a proper name" << std::endl;
359 return 1;
360 }
361
362 if (vm.count("zone") && zone != zone2) {
363 std::cerr << "The loaded Data packet is supposed to be stored at zone: " << zone2
364 << " instead of zone: " << zone << std::endl;
365 return 1;
366 }
367 else {
368 zone = zone2;
369 }
370
371 Response re;
Yumin Xia6343c5b2016-10-20 15:45:50 -0700372 re.fromData(zone, *update);
Shock Jiang43e59b42014-12-02 15:05:02 -0800373
374 if (vm.count("rrlabel") && rrLabel != re.getRrLabel()) {
375 std::cerr << "The loaded Data packet is supposed to have rrLabel: " << re.getRrLabel()
376 << " instead of label: " << rrLabel << std::endl;
377 return 1;
378 }
379
380 if (vm.count("rrtype") && name::Component(rrType) != re.getRrType()) {
381 std::cerr << "The loaded Data packet is supposed to have rrType: " << re.getRrType()
382 << " instead of label: " << rrType << std::endl;
383 return 1;
384 }
385 }
386 catch (const std::exception& e) {
387 std::cerr << "Error: the loaded Data packet cannot parse to a Response stored at zone: "
388 << zone << std::endl;
389 return 1;
390 }
391
Shock Jiangae429122014-10-24 09:04:58 -0700392 }
Shock Jiangae429122014-10-24 09:04:58 -0700393 }
394 catch (const std::exception& ex) {
395 std::cerr << "Parameter Error: " << ex.what() << std::endl;
Shock Jiang43e59b42014-12-02 15:05:02 -0800396 return 1;
Shock Jiangae429122014-10-24 09:04:58 -0700397 }
398
399 Face face;
Shock Jiang43e59b42014-12-02 15:05:02 -0800400 try {
Yumin Xia6343c5b2016-10-20 15:45:50 -0700401 NdnsUpdate updater(zone, update, face);
Shock Jiang43e59b42014-12-02 15:05:02 -0800402 updater.setInterestLifetime(ndn::time::seconds(ttl));
Shock Jiangae429122014-10-24 09:04:58 -0700403
Shock Jiang43e59b42014-12-02 15:05:02 -0800404 updater.start();
405 face.processEvents();
406 if (updater.hasError())
407 return 1;
408 else
Shock Jiang06cd2142014-11-23 17:36:02 -0800409 return 0;
Shock Jiangae429122014-10-24 09:04:58 -0700410 }
Shock Jiang43e59b42014-12-02 15:05:02 -0800411 catch (const std::exception& e) {
412 std::cerr << "Error: " << e.what() << std::endl;
413 return 1;
414 }
Shock Jiangae429122014-10-24 09:04:58 -0700415}