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 | /* |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, 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 | |
| 20 | #include "mgmt/management-tool.hpp" |
| 21 | #include "ndns-label.hpp" |
| 22 | #include "logger.hpp" |
| 23 | #include "util/util.hpp" |
Yumin Xia | 5dd9f2b | 2016-10-26 20:48:05 -0700 | [diff] [blame] | 24 | #include "daemon/rrset-factory.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; |
| 40 | using namespace ndns; |
| 41 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 42 | int ttlInt = -1; |
| 43 | int versionInt = -1; |
| 44 | string zoneStr; |
| 45 | Name dsk; |
| 46 | string db; |
| 47 | string rrLabelStr; |
| 48 | string rrTypeStr; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 49 | std::vector<std::string> content; |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 50 | string file = "-"; |
| 51 | string encoding = "base64"; |
| 52 | bool setFile = false; |
Yumin Xia | 9532933 | 2017-06-06 22:46:49 -0700 | [diff] [blame] | 53 | bool needResign = true; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 54 | 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 Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 62 | "Default: " DEFAULT_DATABASE_PATH "/ndns.db") |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 63 | ; |
| 64 | |
| 65 | po::options_description config("Record Options"); |
| 66 | config.add_options() |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 67 | ("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] | 68 | "Default: use default DSK and its default certificate") |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 69 | ("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 Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 73 | ("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 Xia | 9532933 | 2017-06-06 22:46:49 -0700 | [diff] [blame] | 77 | ("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] | 78 | ; |
| 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 Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 108 | if (vm.count("help")) { |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 109 | 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 Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 111 | << 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 Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 121 | 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 Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 126 | |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 127 | if (vm.count("type") == 0) { |
| 128 | std::cerr << "Error: type must be specified" << std::endl; |
| 129 | return 1; |
| 130 | } |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 131 | } |
| 132 | else { |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 133 | if (vm.count("resign")) { |
| 134 | needResign = true; |
| 135 | } |
| 136 | setFile = true; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 137 | } |
| 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 Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 148 | KeyChain keyChain; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 149 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 150 | 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 Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 157 | 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 Xia | 5dd9f2b | 2016-10-26 20:48:05 -0700 | [diff] [blame] | 180 | |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 181 | if (type == label::NS_RR_TYPE) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 182 | ndn::DelegationList delegations; |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 183 | for (const auto& i : content) { |
| 184 | std::vector<string> data; |
| 185 | boost::split(data, i, boost::is_any_of(",")); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 186 | uint64_t priority = boost::lexical_cast<uint64_t>(data[0]); |
| 187 | delegations.insert(priority, Name(data[1])); |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 190 | rrset = rrsetFactory.generateNsRrset(label, |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 191 | version, ttl, delegations); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 192 | } |
| 193 | else if (type == label::TXT_RR_TYPE) { |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 194 | rrset = rrsetFactory.generateTxtRrset(label, |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 195 | version, ttl, content); |
Yumin Xia | 5dd9f2b | 2016-10-26 20:48:05 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 198 | ndn::ndns::ManagementTool tool(db, keyChain); |
Yumin Xia | 5dd9f2b | 2016-10-26 20:48:05 -0700 | [diff] [blame] | 199 | |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 200 | 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 Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 203 | } |
| 204 | else { |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 205 | tool.addRrset(rrset); |
| 206 | } |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 207 | } |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 208 | |
| 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 | } |