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 | /* |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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 | |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 33 | NDNS_LOG_INIT("AddRrTool") |
| 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 | |
| 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 Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 50 | std::vector<std::string> content; |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 51 | string file = "-"; |
| 52 | string encoding = "base64"; |
| 53 | bool setFile = false; |
| 54 | bool needResign = false; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 55 | 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 Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 63 | "Default: " DEFAULT_DATABASE_PATH "/ndns.db") |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 64 | ; |
| 65 | |
| 66 | po::options_description config("Record Options"); |
| 67 | config.add_options() |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 68 | ("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] | 69 | "Default: use default DSK and its default certificate") |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 70 | ("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 Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 74 | ("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 Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 79 | ; |
| 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 Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 109 | if (vm.count("help")) { |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 110 | 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 Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 112 | << 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 Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 122 | 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 Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 127 | |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 128 | if (vm.count("type") == 0) { |
| 129 | std::cerr << "Error: type must be specified" << std::endl; |
| 130 | return 1; |
| 131 | } |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame^] | 132 | } |
| 133 | else { |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 134 | if (vm.count("resign")) { |
| 135 | needResign = true; |
| 136 | } |
| 137 | setFile = true; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 138 | } |
| 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 Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame^] | 149 | KeyChain keyChain; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 150 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 151 | 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 Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 158 | 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 Xia | 5dd9f2b | 2016-10-26 20:48:05 -0700 | [diff] [blame] | 181 | |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 182 | if (type == label::NS_RR_TYPE) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame^] | 183 | ndn::DelegationList delegations; |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 184 | for (const auto& i : content) { |
| 185 | std::vector<string> data; |
| 186 | boost::split(data, i, boost::is_any_of(",")); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame^] | 187 | uint64_t priority = boost::lexical_cast<uint64_t>(data[0]); |
| 188 | delegations.insert(priority, Name(data[1])); |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | rrset = rrsetFactory.generateNsRrset(label, type, |
| 192 | version, ttl, delegations); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame^] | 193 | } |
| 194 | else if (type == label::TXT_RR_TYPE) { |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 195 | rrset = rrsetFactory.generateTxtRrset(label, type, |
| 196 | version, ttl, content); |
Yumin Xia | 5dd9f2b | 2016-10-26 20:48:05 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 199 | ndn::ndns::ManagementTool tool(db, keyChain); |
Yumin Xia | 5dd9f2b | 2016-10-26 20:48:05 -0700 | [diff] [blame] | 200 | |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 201 | 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 Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame^] | 204 | } |
| 205 | else { |
Yumin Xia | c5ed63f | 2017-01-26 13:44:38 -0800 | [diff] [blame] | 206 | tool.addRrset(rrset); |
| 207 | } |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 208 | } |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 209 | |
| 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 | } |