blob: 605a355523acd9f21123cd33e58a5a2c64558076 [file] [log] [blame]
Jiewen Tan870b29b2014-11-17 19:09:49 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yumin Xia2c509c22017-02-09 14:37:36 -08002/*
Davide Pesaventod01c1a42019-01-21 21:42:45 -05003 * Copyright (c) 2014-2019, Regents of the University of California.
Jiewen Tan870b29b2014-11-17 19:09:49 -08004 *
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
Jiewen Tan870b29b2014-11-17 19:09:49 -080020#include "logger.hpp"
Davide Pesaventod01c1a42019-01-21 21:42:45 -050021#include "ndns-label.hpp"
Yumin Xia5dd9f2b2016-10-26 20:48:05 -070022#include "daemon/rrset-factory.hpp"
Davide Pesaventod01c1a42019-01-21 21:42:45 -050023#include "mgmt/management-tool.hpp"
24#include "util/util.hpp"
Jiewen Tan870b29b2014-11-17 19:09:49 -080025
26#include <boost/program_options.hpp>
27#include <boost/filesystem.hpp>
Yumin Xia5dd9f2b2016-10-26 20:48:05 -070028#include <boost/algorithm/string.hpp>
29#include <boost/lexical_cast.hpp>
Jiewen Tan870b29b2014-11-17 19:09:49 -080030
31#include <string>
32
Alexander Afanasyev08d18742018-03-15 16:31:28 -040033NDNS_LOG_INIT(AddRrTool);
Yumin Xiaacd21332016-11-28 22:54:48 -080034
Jiewen Tan870b29b2014-11-17 19:09:49 -080035int
36main(int argc, char* argv[])
37{
38 using std::string;
39 using namespace ndn;
Jiewen Tan870b29b2014-11-17 19:09:49 -080040
Jiewen Tan870b29b2014-11-17 19:09:49 -080041 int ttlInt = -1;
42 int versionInt = -1;
43 string zoneStr;
44 Name dsk;
Davide Pesaventod01c1a42019-01-21 21:42:45 -050045 string db = ndns::getDefaultDatabaseFile();
Jiewen Tan870b29b2014-11-17 19:09:49 -080046 string rrLabelStr;
47 string rrTypeStr;
Jiewen Tan870b29b2014-11-17 19:09:49 -080048 std::vector<std::string> content;
Yumin Xiac5ed63f2017-01-26 13:44:38 -080049 string file = "-";
50 string encoding = "base64";
51 bool setFile = false;
Yumin Xia95329332017-06-06 22:46:49 -070052 bool needResign = true;
Jiewen Tan870b29b2014-11-17 19:09:49 -080053 try {
54 namespace po = boost::program_options;
55 po::variables_map vm;
56
57 po::options_description options("Generic Options");
58 options.add_options()
Davide Pesaventod01c1a42019-01-21 21:42:45 -050059 ("help,h", "print this help message and exit")
60 ("db,b", po::value<std::string>(&db)->default_value(db), "path to NDNS database file")
Jiewen Tan870b29b2014-11-17 19:09:49 -080061 ;
62
63 po::options_description config("Record Options");
64 config.add_options()
Jiewen Tan870b29b2014-11-17 19:09:49 -080065 ("dsk,d", po::value<Name>(&dsk), "Set the name of DSK's certificate. "
Yumin Xiac5ed63f2017-01-26 13:44:38 -080066 "Default: use default DSK and its default certificate")
Jiewen Tan870b29b2014-11-17 19:09:49 -080067 ("content,c", po::value<std::vector<std::string>>(&content),
68 "Set the content of resource record. Default: empty string")
69 ("ttl,a", po::value<int>(&ttlInt), "Set ttl of the rrset. Default: 3600 seconds")
70 ("version,v", po::value<int>(&ttlInt), "Set version of the rrset. Default: Unix Timestamp")
Yumin Xiac5ed63f2017-01-26 13:44:38 -080071 ("file,f", po::value<string>(&file), "Set path to file containing a rrset. If set, label, "
72 "type, content-type, content, and version parameters will be ignored. Default is stdin(-)")
73 ("encoding,e", po::value<string>(&encoding),
74 "Set encoding format of input file. Default: base64")
Yumin Xia95329332017-06-06 22:46:49 -070075 ("resign,r", po::value<bool>(&needResign), "Resign the input with DSK. Default is true")
Jiewen Tan870b29b2014-11-17 19:09:49 -080076 ;
77
78 // add "Record Options" as a separate section
79 options.add(config);
80
81 po::options_description hidden("Hidden Options");
82 hidden.add_options()
83 ("zone", po::value<string>(&zoneStr), "host zone name")
84 ("label", po::value<string>(&rrLabelStr), "label of resource record.")
85 ("type", po::value<string>(&rrTypeStr), "Set the type of resource record.")
86 ;
87
88 po::positional_options_description positional;
89 positional.add("zone", 1);
90 positional.add("label", 1);
91 positional.add("type", 1);
92 positional.add("content", -1);
93
94 po::options_description cmdlineOptions;
95 cmdlineOptions.add(options).add(hidden);
96
97 // po::options_description configFileOptions;
98 // configFileOptions.add(config).add(hidden);
99
100 po::parsed_options parsed =
101 po::command_line_parser(argc, argv).options(cmdlineOptions).positional(positional).run();
102
103 po::store(parsed, vm);
104 po::notify(vm);
105
Jiewen Tan870b29b2014-11-17 19:09:49 -0800106 if (vm.count("help")) {
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800107 std::cout << "Usage: ndns-add-rr [options] zone label type [content ...]" << std::endl;
108 std::cout << " ndns-add-rr [options] zone [-f file] [-e raw|base64|hex]" << std::endl
Jiewen Tan870b29b2014-11-17 19:09:49 -0800109 << std::endl;
110 std::cout << options << std::endl;
111 return 0;
112 }
113
114 if (vm.count("zone") == 0) {
115 std::cerr << "Error: zone, label, and type must be specified" << std::endl;
116 return 1;
117 }
118
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800119 if (vm.count("file") == 0) {
120 if (vm.count("label") == 0) {
121 std::cerr << "Error: label and type must be specified" << std::endl;
122 return 1;
123 }
Jiewen Tan870b29b2014-11-17 19:09:49 -0800124
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800125 if (vm.count("type") == 0) {
126 std::cerr << "Error: type must be specified" << std::endl;
127 return 1;
128 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800129 }
130 else {
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800131 if (vm.count("resign")) {
132 needResign = true;
133 }
134 setFile = true;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800135 }
136 }
137 catch (const std::exception& ex) {
138 std::cerr << "Parameter Error: " << ex.what() << std::endl;
139 return 1;
140 }
141
142 try {
143 Name zoneName(zoneStr);
144 Name label(rrLabelStr);
145 name::Component type(rrTypeStr);
Yumin Xia2c509c22017-02-09 14:37:36 -0800146 KeyChain keyChain;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800147
Jiewen Tan870b29b2014-11-17 19:09:49 -0800148 time::seconds ttl;
149 if (ttlInt == -1)
150 ttl = ndns::DEFAULT_CACHE_TTL;
151 else
152 ttl = time::seconds(ttlInt);
153 uint64_t version = static_cast<uint64_t>(versionInt);
154
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800155 if (setFile) {
156 ndn::io::IoEncoding ioEncoding;
157 if (encoding == "raw") {
158 ioEncoding = ndn::io::NO_ENCODING;
159 }
160 else if (encoding == "hex") {
161 ioEncoding = ndn::io::HEX;
162 }
163 else if (encoding == "base64") {
164 ioEncoding = ndn::io::BASE64;
165 }
166 else {
167 std::cerr << "Error: not supported encoding format '" << encoding
168 << "' (valid options are: raw, hex, and base64)" << std::endl;
169 return 1;
170 }
171 ndn::ndns::ManagementTool tool(db, keyChain);
172 tool.addRrsetFromFile(zoneName, file, ttl, dsk, ioEncoding, needResign);
173 }
174 else {
Davide Pesaventod01c1a42019-01-21 21:42:45 -0500175 ndns::RrsetFactory rrsetFactory(db, zoneName, keyChain, dsk);
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800176 rrsetFactory.checkZoneKey();
Davide Pesaventod01c1a42019-01-21 21:42:45 -0500177 ndns::Rrset rrset;
Yumin Xia5dd9f2b2016-10-26 20:48:05 -0700178
Davide Pesaventod01c1a42019-01-21 21:42:45 -0500179 if (type == ndns::label::NS_RR_TYPE) {
Yumin Xia2c509c22017-02-09 14:37:36 -0800180 ndn::DelegationList delegations;
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800181 for (const auto& i : content) {
182 std::vector<string> data;
183 boost::split(data, i, boost::is_any_of(","));
Yumin Xia2c509c22017-02-09 14:37:36 -0800184 uint64_t priority = boost::lexical_cast<uint64_t>(data[0]);
185 delegations.insert(priority, Name(data[1]));
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800186 }
187
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700188 rrset = rrsetFactory.generateNsRrset(label,
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800189 version, ttl, delegations);
Yumin Xia2c509c22017-02-09 14:37:36 -0800190 }
Davide Pesaventod01c1a42019-01-21 21:42:45 -0500191 else if (type == ndns::label::TXT_RR_TYPE) {
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700192 rrset = rrsetFactory.generateTxtRrset(label,
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800193 version, ttl, content);
Yumin Xia5dd9f2b2016-10-26 20:48:05 -0700194 }
195
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800196 ndn::ndns::ManagementTool tool(db, keyChain);
Yumin Xia5dd9f2b2016-10-26 20:48:05 -0700197
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800198 if (label.size() > 1) {
199 NDNS_LOG_TRACE("add multi-level label Rrset, using the same TTL as the Rrset");
200 tool.addMultiLevelLabelRrset(rrset, rrsetFactory, ttl);
Yumin Xia2c509c22017-02-09 14:37:36 -0800201 }
202 else {
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800203 tool.addRrset(rrset);
204 }
Yumin Xiaacd21332016-11-28 22:54:48 -0800205 }
Jiewen Tan870b29b2014-11-17 19:09:49 -0800206
207 /// @todo Report success or failure
208 // May be also show the inserted record in ndns-list-zone format
209 }
210 catch (const std::exception& ex) {
211 std::cerr << "Error: " << ex.what() << std::endl;
212 return 1;
213 }
214}