blob: e4047e20c6434bf08038b179335046a58d9b02d2 [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/*
Yumin Xiaacd21332016-11-28 22:54:48 -08003 * Copyright (c) 2014-2017, 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
Yumin Xiaacd21332016-11-28 22:54:48 -080033NDNS_LOG_INIT("AddRrTool")
34
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
42 ndn::ndns::log::init();
43 int ttlInt = -1;
44 int versionInt = -1;
45 string zoneStr;
46 Name dsk;
47 string db;
48 string rrLabelStr;
49 string rrTypeStr;
Jiewen Tan870b29b2014-11-17 19:09:49 -080050 std::vector<std::string> content;
Yumin Xiac5ed63f2017-01-26 13:44:38 -080051 string file = "-";
52 string encoding = "base64";
53 bool setFile = false;
54 bool needResign = false;
Jiewen Tan870b29b2014-11-17 19:09:49 -080055 try {
56 namespace po = boost::program_options;
57 po::variables_map vm;
58
59 po::options_description options("Generic Options");
60 options.add_options()
61 ("help,h", "print help message")
62 ("db,b", po::value<std::string>(&db), "Set the path of NDNS server database. "
Yumin Xiac5ed63f2017-01-26 13:44:38 -080063 "Default: " DEFAULT_DATABASE_PATH "/ndns.db")
Jiewen Tan870b29b2014-11-17 19:09:49 -080064 ;
65
66 po::options_description config("Record Options");
67 config.add_options()
Jiewen Tan870b29b2014-11-17 19:09:49 -080068 ("dsk,d", po::value<Name>(&dsk), "Set the name of DSK's certificate. "
Yumin Xiac5ed63f2017-01-26 13:44:38 -080069 "Default: use default DSK and its default certificate")
Jiewen Tan870b29b2014-11-17 19:09:49 -080070 ("content,c", po::value<std::vector<std::string>>(&content),
71 "Set the content of resource record. Default: empty string")
72 ("ttl,a", po::value<int>(&ttlInt), "Set ttl of the rrset. Default: 3600 seconds")
73 ("version,v", po::value<int>(&ttlInt), "Set version of the rrset. Default: Unix Timestamp")
Yumin Xiac5ed63f2017-01-26 13:44:38 -080074 ("file,f", po::value<string>(&file), "Set path to file containing a rrset. If set, label, "
75 "type, content-type, content, and version parameters will be ignored. Default is stdin(-)")
76 ("encoding,e", po::value<string>(&encoding),
77 "Set encoding format of input file. Default: base64")
78 ("resign,r", po::value<bool>(&needResign), "Resign the input with DSK")
Jiewen Tan870b29b2014-11-17 19:09:49 -080079 ;
80
81 // add "Record Options" as a separate section
82 options.add(config);
83
84 po::options_description hidden("Hidden Options");
85 hidden.add_options()
86 ("zone", po::value<string>(&zoneStr), "host zone name")
87 ("label", po::value<string>(&rrLabelStr), "label of resource record.")
88 ("type", po::value<string>(&rrTypeStr), "Set the type of resource record.")
89 ;
90
91 po::positional_options_description positional;
92 positional.add("zone", 1);
93 positional.add("label", 1);
94 positional.add("type", 1);
95 positional.add("content", -1);
96
97 po::options_description cmdlineOptions;
98 cmdlineOptions.add(options).add(hidden);
99
100 // po::options_description configFileOptions;
101 // configFileOptions.add(config).add(hidden);
102
103 po::parsed_options parsed =
104 po::command_line_parser(argc, argv).options(cmdlineOptions).positional(positional).run();
105
106 po::store(parsed, vm);
107 po::notify(vm);
108
Jiewen Tan870b29b2014-11-17 19:09:49 -0800109 if (vm.count("help")) {
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800110 std::cout << "Usage: ndns-add-rr [options] zone label type [content ...]" << std::endl;
111 std::cout << " ndns-add-rr [options] zone [-f file] [-e raw|base64|hex]" << std::endl
Jiewen Tan870b29b2014-11-17 19:09:49 -0800112 << std::endl;
113 std::cout << options << std::endl;
114 return 0;
115 }
116
117 if (vm.count("zone") == 0) {
118 std::cerr << "Error: zone, label, and type must be specified" << std::endl;
119 return 1;
120 }
121
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800122 if (vm.count("file") == 0) {
123 if (vm.count("label") == 0) {
124 std::cerr << "Error: label and type must be specified" << std::endl;
125 return 1;
126 }
Jiewen Tan870b29b2014-11-17 19:09:49 -0800127
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800128 if (vm.count("type") == 0) {
129 std::cerr << "Error: type must be specified" << std::endl;
130 return 1;
131 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800132 }
133 else {
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800134 if (vm.count("resign")) {
135 needResign = true;
136 }
137 setFile = true;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800138 }
139 }
140 catch (const std::exception& ex) {
141 std::cerr << "Parameter Error: " << ex.what() << std::endl;
142 return 1;
143 }
144
145 try {
146 Name zoneName(zoneStr);
147 Name label(rrLabelStr);
148 name::Component type(rrTypeStr);
Yumin Xia2c509c22017-02-09 14:37:36 -0800149 KeyChain keyChain;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800150
Jiewen Tan870b29b2014-11-17 19:09:49 -0800151 time::seconds ttl;
152 if (ttlInt == -1)
153 ttl = ndns::DEFAULT_CACHE_TTL;
154 else
155 ttl = time::seconds(ttlInt);
156 uint64_t version = static_cast<uint64_t>(versionInt);
157
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800158 if (setFile) {
159 ndn::io::IoEncoding ioEncoding;
160 if (encoding == "raw") {
161 ioEncoding = ndn::io::NO_ENCODING;
162 }
163 else if (encoding == "hex") {
164 ioEncoding = ndn::io::HEX;
165 }
166 else if (encoding == "base64") {
167 ioEncoding = ndn::io::BASE64;
168 }
169 else {
170 std::cerr << "Error: not supported encoding format '" << encoding
171 << "' (valid options are: raw, hex, and base64)" << std::endl;
172 return 1;
173 }
174 ndn::ndns::ManagementTool tool(db, keyChain);
175 tool.addRrsetFromFile(zoneName, file, ttl, dsk, ioEncoding, needResign);
176 }
177 else {
178 RrsetFactory rrsetFactory(db, zoneName, keyChain, dsk);
179 rrsetFactory.checkZoneKey();
180 Rrset rrset;
Yumin Xia5dd9f2b2016-10-26 20:48:05 -0700181
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800182 if (type == label::NS_RR_TYPE) {
Yumin Xia2c509c22017-02-09 14:37:36 -0800183 ndn::DelegationList delegations;
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800184 for (const auto& i : content) {
185 std::vector<string> data;
186 boost::split(data, i, boost::is_any_of(","));
Yumin Xia2c509c22017-02-09 14:37:36 -0800187 uint64_t priority = boost::lexical_cast<uint64_t>(data[0]);
188 delegations.insert(priority, Name(data[1]));
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800189 }
190
191 rrset = rrsetFactory.generateNsRrset(label, type,
192 version, ttl, delegations);
Yumin Xia2c509c22017-02-09 14:37:36 -0800193 }
194 else if (type == label::TXT_RR_TYPE) {
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800195 rrset = rrsetFactory.generateTxtRrset(label, type,
196 version, ttl, content);
Yumin Xia5dd9f2b2016-10-26 20:48:05 -0700197 }
198
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800199 ndn::ndns::ManagementTool tool(db, keyChain);
Yumin Xia5dd9f2b2016-10-26 20:48:05 -0700200
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800201 if (label.size() > 1) {
202 NDNS_LOG_TRACE("add multi-level label Rrset, using the same TTL as the Rrset");
203 tool.addMultiLevelLabelRrset(rrset, rrsetFactory, ttl);
Yumin Xia2c509c22017-02-09 14:37:36 -0800204 }
205 else {
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800206 tool.addRrset(rrset);
207 }
Yumin Xiaacd21332016-11-28 22:54:48 -0800208 }
Jiewen Tan870b29b2014-11-17 19:09:49 -0800209
210 /// @todo Report success or failure
211 // May be also show the inserted record in ndns-list-zone format
212 }
213 catch (const std::exception& ex) {
214 std::cerr << "Error: " << ex.what() << std::endl;
215 return 1;
216 }
217}