Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 2 | /* |
Davide Pesavento | d01c1a4 | 2019-01-21 21:42:45 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, Regents of the University of California. |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 4 | * |
| 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 Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 20 | #include "logger.hpp" |
Davide Pesavento | d01c1a4 | 2019-01-21 21:42:45 -0500 | [diff] [blame] | 21 | #include "ndns-label.hpp" |
Yumin Xia | 5dd9f2b | 2016-10-26 20:48:05 -0700 | [diff] [blame] | 22 | #include "daemon/rrset-factory.hpp" |
Davide Pesavento | d01c1a4 | 2019-01-21 21:42:45 -0500 | [diff] [blame] | 23 | #include "mgmt/management-tool.hpp" |
| 24 | #include "util/util.hpp" |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 25 | |
| 26 | #include <boost/program_options.hpp> |
| 27 | #include <boost/filesystem.hpp> |
Yumin Xia | 5dd9f2b | 2016-10-26 20:48:05 -0700 | [diff] [blame] | 28 | #include <boost/algorithm/string.hpp> |
| 29 | #include <boost/lexical_cast.hpp> |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 30 | |
| 31 | #include <string> |
| 32 | |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 33 | NDNS_LOG_INIT(AddRrTool); |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 34 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 35 | int |
| 36 | main(int argc, char* argv[]) |
| 37 | { |
| 38 | using std::string; |
| 39 | using namespace ndn; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 40 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 41 | int ttlInt = -1; |
| 42 | int versionInt = -1; |
| 43 | string zoneStr; |
| 44 | Name dsk; |
Davide Pesavento | d01c1a4 | 2019-01-21 21:42:45 -0500 | [diff] [blame] | 45 | string db = ndns::getDefaultDatabaseFile(); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 46 | string rrLabelStr; |
| 47 | string rrTypeStr; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 48 | std::vector<std::string> content; |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 49 | string file = "-"; |
| 50 | string encoding = "base64"; |
| 51 | bool setFile = false; |
Yumin Xia | 9532933 | 2017-06-06 22:46:49 -0700 | [diff] [blame] | 52 | bool needResign = true; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 53 | 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 Pesavento | d01c1a4 | 2019-01-21 21:42:45 -0500 | [diff] [blame] | 59 | ("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 Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 61 | ; |
| 62 | |
| 63 | po::options_description config("Record Options"); |
| 64 | config.add_options() |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 65 | ("dsk,d", po::value<Name>(&dsk), "Set the name of DSK's certificate. " |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 66 | "Default: use default DSK and its default certificate") |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 67 | ("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 Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 71 | ("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 Xia | 9532933 | 2017-06-06 22:46:49 -0700 | [diff] [blame] | 75 | ("resign,r", po::value<bool>(&needResign), "Resign the input with DSK. Default is true") |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 76 | ; |
| 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 Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 106 | if (vm.count("help")) { |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 107 | 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 Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 109 | << 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 Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 119 | 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 Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 124 | |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 125 | if (vm.count("type") == 0) { |
| 126 | std::cerr << "Error: type must be specified" << std::endl; |
| 127 | return 1; |
| 128 | } |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 129 | } |
| 130 | else { |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 131 | if (vm.count("resign")) { |
| 132 | needResign = true; |
| 133 | } |
| 134 | setFile = true; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 135 | } |
| 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 Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 146 | KeyChain keyChain; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 147 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 148 | 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 Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 155 | 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 Pesavento | d01c1a4 | 2019-01-21 21:42:45 -0500 | [diff] [blame] | 175 | ndns::RrsetFactory rrsetFactory(db, zoneName, keyChain, dsk); |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 176 | rrsetFactory.checkZoneKey(); |
Davide Pesavento | d01c1a4 | 2019-01-21 21:42:45 -0500 | [diff] [blame] | 177 | ndns::Rrset rrset; |
Yumin Xia | 5dd9f2b | 2016-10-26 20:48:05 -0700 | [diff] [blame] | 178 | |
Davide Pesavento | d01c1a4 | 2019-01-21 21:42:45 -0500 | [diff] [blame] | 179 | if (type == ndns::label::NS_RR_TYPE) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 180 | ndn::DelegationList delegations; |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 181 | for (const auto& i : content) { |
| 182 | std::vector<string> data; |
| 183 | boost::split(data, i, boost::is_any_of(",")); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 184 | uint64_t priority = boost::lexical_cast<uint64_t>(data[0]); |
| 185 | delegations.insert(priority, Name(data[1])); |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 188 | rrset = rrsetFactory.generateNsRrset(label, |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 189 | version, ttl, delegations); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 190 | } |
Davide Pesavento | d01c1a4 | 2019-01-21 21:42:45 -0500 | [diff] [blame] | 191 | else if (type == ndns::label::TXT_RR_TYPE) { |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 192 | rrset = rrsetFactory.generateTxtRrset(label, |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 193 | version, ttl, content); |
Yumin Xia | 5dd9f2b | 2016-10-26 20:48:05 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 196 | ndn::ndns::ManagementTool tool(db, keyChain); |
Yumin Xia | 5dd9f2b | 2016-10-26 20:48:05 -0700 | [diff] [blame] | 197 | |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 198 | 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 Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 201 | } |
| 202 | else { |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 203 | tool.addRrset(rrset); |
| 204 | } |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 205 | } |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 206 | |
| 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 | } |