blob: 23f5b90a3437362692b04f30fe6568c0d33dcef0 [file] [log] [blame]
Jiewen Tan870b29b2014-11-17 19:09:49 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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 }
132 } else {
133 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 Xia5dd9f2b2016-10-26 20:48:05 -0700148 ndn::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) {
182 ndn::Link::DelegationSet delegations;
183 for (const auto& i : content) {
184 std::vector<string> data;
185 boost::split(data, i, boost::is_any_of(","));
186 uint32_t priority = boost::lexical_cast<uint32_t>(data[0]);
187 delegations.insert(std::make_pair(priority, data[1]));
188 }
189
190 rrset = rrsetFactory.generateNsRrset(label, type,
191 version, ttl, delegations);
192 } else if (type == label::TXT_RR_TYPE) {
193 rrset = rrsetFactory.generateTxtRrset(label, type,
194 version, ttl, content);
Yumin Xia5dd9f2b2016-10-26 20:48:05 -0700195 }
196
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800197 ndn::ndns::ManagementTool tool(db, keyChain);
Yumin Xia5dd9f2b2016-10-26 20:48:05 -0700198
Yumin Xiac5ed63f2017-01-26 13:44:38 -0800199 if (label.size() > 1) {
200 NDNS_LOG_TRACE("add multi-level label Rrset, using the same TTL as the Rrset");
201 tool.addMultiLevelLabelRrset(rrset, rrsetFactory, ttl);
202 } else {
203 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}