blob: 0e664b62134628c972dbb8cfaa707b00baf7513b [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/*
Alexander Afanasyev08d18742018-03-15 16:31:28 -04003 * Copyright (c) 2014-2018, 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
20#include "mgmt/management-tool.hpp"
21#include "ndns-label.hpp"
22#include "logger.hpp"
23#include "util/util.hpp"
Yumin Xia5dd9f2b2016-10-26 20:48:05 -070024#include "daemon/rrset-factory.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;
40 using namespace ndns;
41
Jiewen Tan870b29b2014-11-17 19:09:49 -080042 int ttlInt = -1;
43 int versionInt = -1;
44 string zoneStr;
45 Name dsk;
46 string db;
47 string rrLabelStr;
48 string rrTypeStr;
Jiewen Tan870b29b2014-11-17 19:09:49 -080049 std::vector<std::string> content;
Yumin Xiac5ed63f2017-01-26 13:44:38 -080050 string file = "-";
51 string encoding = "base64";
52 bool setFile = false;
Yumin Xia95329332017-06-06 22:46:49 -070053 bool needResign = true;
Jiewen Tan870b29b2014-11-17 19:09:49 -080054 try {
55 namespace po = boost::program_options;
56 po::variables_map vm;
57
58 po::options_description options("Generic Options");
59 options.add_options()
60 ("help,h", "print help message")
61 ("db,b", po::value<std::string>(&db), "Set the path of NDNS server database. "
Yumin Xiac5ed63f2017-01-26 13:44:38 -080062 "Default: " DEFAULT_DATABASE_PATH "/ndns.db")
Jiewen Tan870b29b2014-11-17 19:09:49 -080063 ;
64
65 po::options_description config("Record Options");
66 config.add_options()
Jiewen Tan870b29b2014-11-17 19:09:49 -080067 ("dsk,d", po::value<Name>(&dsk), "Set the name of DSK's certificate. "
Yumin Xiac5ed63f2017-01-26 13:44:38 -080068 "Default: use default DSK and its default certificate")
Jiewen Tan870b29b2014-11-17 19:09:49 -080069 ("content,c", po::value<std::vector<std::string>>(&content),
70 "Set the content of resource record. Default: empty string")
71 ("ttl,a", po::value<int>(&ttlInt), "Set ttl of the rrset. Default: 3600 seconds")
72 ("version,v", po::value<int>(&ttlInt), "Set version of the rrset. Default: Unix Timestamp")
Yumin Xiac5ed63f2017-01-26 13:44:38 -080073 ("file,f", po::value<string>(&file), "Set path to file containing a rrset. If set, label, "
74 "type, content-type, content, and version parameters will be ignored. Default is stdin(-)")
75 ("encoding,e", po::value<string>(&encoding),
76 "Set encoding format of input file. Default: base64")
Yumin Xia95329332017-06-06 22:46:49 -070077 ("resign,r", po::value<bool>(&needResign), "Resign the input with DSK. Default is true")
Jiewen Tan870b29b2014-11-17 19:09:49 -080078 ;
79
80 // add "Record Options" as a separate section
81 options.add(config);
82
83 po::options_description hidden("Hidden Options");
84 hidden.add_options()
85 ("zone", po::value<string>(&zoneStr), "host zone name")
86 ("label", po::value<string>(&rrLabelStr), "label of resource record.")
87 ("type", po::value<string>(&rrTypeStr), "Set the type of resource record.")
88 ;
89
90 po::positional_options_description positional;
91 positional.add("zone", 1);
92 positional.add("label", 1);
93 positional.add("type", 1);
94 positional.add("content", -1);
95
96 po::options_description cmdlineOptions;
97 cmdlineOptions.add(options).add(hidden);
98
99 // po::options_description configFileOptions;
100 // configFileOptions.add(config).add(hidden);
101
102 po::parsed_options parsed =
103 po::command_line_parser(argc, argv).options(cmdlineOptions).positional(positional).run();
104
105 po::store(parsed, vm);
106 po::notify(vm);
107
Jiewen Tan870b29b2014-11-17 19:09:49 -0800108 if (vm.count("help")) {
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800109 std::cout << "Usage: ndns-add-rr [options] zone label type [content ...]" << std::endl;
110 std::cout << " ndns-add-rr [options] zone [-f file] [-e raw|base64|hex]" << std::endl
Jiewen Tan870b29b2014-11-17 19:09:49 -0800111 << std::endl;
112 std::cout << options << std::endl;
113 return 0;
114 }
115
116 if (vm.count("zone") == 0) {
117 std::cerr << "Error: zone, label, and type must be specified" << std::endl;
118 return 1;
119 }
120
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800121 if (vm.count("file") == 0) {
122 if (vm.count("label") == 0) {
123 std::cerr << "Error: label and type must be specified" << std::endl;
124 return 1;
125 }
Jiewen Tan870b29b2014-11-17 19:09:49 -0800126
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800127 if (vm.count("type") == 0) {
128 std::cerr << "Error: type must be specified" << std::endl;
129 return 1;
130 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800131 }
132 else {
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800133 if (vm.count("resign")) {
134 needResign = true;
135 }
136 setFile = true;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800137 }
138 }
139 catch (const std::exception& ex) {
140 std::cerr << "Parameter Error: " << ex.what() << std::endl;
141 return 1;
142 }
143
144 try {
145 Name zoneName(zoneStr);
146 Name label(rrLabelStr);
147 name::Component type(rrTypeStr);
Yumin Xia2c509c22017-02-09 14:37:36 -0800148 KeyChain keyChain;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800149
Jiewen Tan870b29b2014-11-17 19:09:49 -0800150 time::seconds ttl;
151 if (ttlInt == -1)
152 ttl = ndns::DEFAULT_CACHE_TTL;
153 else
154 ttl = time::seconds(ttlInt);
155 uint64_t version = static_cast<uint64_t>(versionInt);
156
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800157 if (setFile) {
158 ndn::io::IoEncoding ioEncoding;
159 if (encoding == "raw") {
160 ioEncoding = ndn::io::NO_ENCODING;
161 }
162 else if (encoding == "hex") {
163 ioEncoding = ndn::io::HEX;
164 }
165 else if (encoding == "base64") {
166 ioEncoding = ndn::io::BASE64;
167 }
168 else {
169 std::cerr << "Error: not supported encoding format '" << encoding
170 << "' (valid options are: raw, hex, and base64)" << std::endl;
171 return 1;
172 }
173 ndn::ndns::ManagementTool tool(db, keyChain);
174 tool.addRrsetFromFile(zoneName, file, ttl, dsk, ioEncoding, needResign);
175 }
176 else {
177 RrsetFactory rrsetFactory(db, zoneName, keyChain, dsk);
178 rrsetFactory.checkZoneKey();
179 Rrset rrset;
Yumin Xia5dd9f2b2016-10-26 20:48:05 -0700180
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800181 if (type == label::NS_RR_TYPE) {
Yumin Xia2c509c22017-02-09 14:37:36 -0800182 ndn::DelegationList delegations;
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800183 for (const auto& i : content) {
184 std::vector<string> data;
185 boost::split(data, i, boost::is_any_of(","));
Yumin Xia2c509c22017-02-09 14:37:36 -0800186 uint64_t priority = boost::lexical_cast<uint64_t>(data[0]);
187 delegations.insert(priority, Name(data[1]));
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800188 }
189
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700190 rrset = rrsetFactory.generateNsRrset(label,
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800191 version, ttl, delegations);
Yumin Xia2c509c22017-02-09 14:37:36 -0800192 }
193 else if (type == label::TXT_RR_TYPE) {
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700194 rrset = rrsetFactory.generateTxtRrset(label,
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800195 version, ttl, content);
Yumin Xia5dd9f2b2016-10-26 20:48:05 -0700196 }
197
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800198 ndn::ndns::ManagementTool tool(db, keyChain);
Yumin Xia5dd9f2b2016-10-26 20:48:05 -0700199
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800200 if (label.size() > 1) {
201 NDNS_LOG_TRACE("add multi-level label Rrset, using the same TTL as the Rrset");
202 tool.addMultiLevelLabelRrset(rrset, rrsetFactory, ttl);
Yumin Xia2c509c22017-02-09 14:37:36 -0800203 }
204 else {
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800205 tool.addRrset(rrset);
206 }
Yumin Xiaacd21332016-11-28 22:54:48 -0800207 }
Jiewen Tan870b29b2014-11-17 19:09:49 -0800208
209 /// @todo Report success or failure
210 // May be also show the inserted record in ndns-list-zone format
211 }
212 catch (const std::exception& ex) {
213 std::cerr << "Error: " << ex.what() << std::endl;
214 return 1;
215 }
216}