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 | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, 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" |
| 22 | #include "mgmt/management-tool.hpp" |
| 23 | #include "util/util.hpp" |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 24 | |
| 25 | #include <boost/program_options.hpp> |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 26 | |
| 27 | #include <iostream> |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 28 | |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 29 | NDNS_LOG_INIT(NdnsCreateZone); |
Yumin Xia | 99c821a | 2017-04-07 11:01:08 -0700 | [diff] [blame] | 30 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 31 | int |
| 32 | main(int argc, char* argv[]) |
| 33 | { |
| 34 | using std::string; |
| 35 | using namespace ndn; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 36 | int cacheTtlInt = -1; |
| 37 | int certTtlInt = -1; |
| 38 | string zoneStr; |
| 39 | string parentStr; |
| 40 | string dskStr; |
| 41 | string kskStr; |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 42 | string dkeyStr; |
Davide Pesavento | d01c1a4 | 2019-01-21 21:42:45 -0500 | [diff] [blame] | 43 | string db = ndns::getDefaultDatabaseFile(); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 44 | try { |
| 45 | namespace po = boost::program_options; |
| 46 | po::variables_map vm; |
| 47 | |
| 48 | po::options_description options("Generic Options"); |
| 49 | options.add_options() |
Davide Pesavento | d01c1a4 | 2019-01-21 21:42:45 -0500 | [diff] [blame] | 50 | ("help,h", "print this help message and exit") |
| 51 | ("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] | 52 | ; |
| 53 | |
| 54 | po::options_description config("Zone Options"); |
| 55 | config.add_options() |
| 56 | ("cacheTtl,a", po::value<int>(&cacheTtlInt), "Set ttl of records of the zone and its " |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 57 | "DSK CERT. Default: 3600 seconds") |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 58 | ("certTtl,e", po::value<int>(&certTtlInt), "Set ttl of DSK and KSK certificates. " |
| 59 | "Default: 365 days") |
| 60 | ("parent,p", po::value<std::string>(&parentStr), "Set the parent zone of the zone to be " |
| 61 | "created. Default: the zone's direct parent") |
| 62 | ("dsk,d", po::value<std::string>(&dskStr), "Set the name of DSK's certificate, " |
| 63 | "Default: generate new key and certificate") |
| 64 | ("ksk,k", po::value<std::string>(&kskStr), "Set the name of KSK's certificate, " |
| 65 | "Default: generate new key and certificate") |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 66 | ("dkey,g", po::value<std::string>(&dkeyStr), "Set the name of DKEY's certificate, " |
| 67 | "Default: generate new key and certificate") |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 68 | ; |
| 69 | |
| 70 | options.add(config); |
| 71 | |
| 72 | po::options_description hidden("Hidden Options"); |
| 73 | hidden.add_options() |
| 74 | ("zone", po::value<string>(&zoneStr), "name of the zone to be created") |
| 75 | ; |
| 76 | po::positional_options_description postion; |
| 77 | postion.add("zone", 1); |
| 78 | |
| 79 | po::options_description cmdline_options; |
| 80 | cmdline_options.add(options).add(hidden); |
| 81 | |
| 82 | // po::options_description config_file_options; |
| 83 | // config_file_options.add(config).add(hidden); |
| 84 | |
| 85 | po::parsed_options parsed = |
| 86 | po::command_line_parser(argc, argv).options(cmdline_options).positional(postion).run(); |
| 87 | |
| 88 | po::store(parsed, vm); |
| 89 | po::notify(vm); |
| 90 | |
| 91 | if (vm.count("help")) { |
| 92 | std::cout << "Usage: ndns-create-zone [-b db] zone [-a cacheTtl] [-e certTtl] [-p parent] " |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 93 | "[-d dskCert] [-k kskCert] [-g dkeyCert]" << std::endl; |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 94 | std::cout << options << std::endl; |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | if (vm.count("zone") == 0) { |
| 99 | std::cerr << "Error: zone must be specified" << std::endl; |
| 100 | return 1; |
| 101 | } |
| 102 | } |
| 103 | catch (const std::exception& ex) { |
| 104 | std::cerr << "Parameter Error: " << ex.what() << std::endl; |
| 105 | return 1; |
| 106 | } |
| 107 | |
| 108 | try { |
| 109 | Name zone(zoneStr); |
| 110 | Name parent(parentStr); |
| 111 | if (!zone.empty() && parentStr.empty()) |
| 112 | parent = zone.getPrefix(-1); |
| 113 | |
| 114 | Name ksk(kskStr); |
| 115 | Name dsk(dskStr); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 116 | Name dkey(dkeyStr); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 117 | |
| 118 | time::seconds cacheTtl; |
| 119 | time::seconds certTtl; |
| 120 | if (cacheTtlInt == -1) |
| 121 | cacheTtl = ndns::DEFAULT_CACHE_TTL; |
| 122 | else |
| 123 | cacheTtl = time::seconds(cacheTtlInt); |
| 124 | |
| 125 | if (certTtlInt == -1) |
| 126 | certTtl = ndns::DEFAULT_CERT_TTL; |
| 127 | else |
| 128 | certTtl = time::seconds(certTtlInt); |
| 129 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 130 | KeyChain keyChain; |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 131 | ndn::ndns::ManagementTool tool(db, keyChain); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 132 | ndn::ndns::Zone createdZone = tool.createZone(zone, parent, cacheTtl, certTtl, ksk, dsk, dkey); |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 133 | ndn::security::Certificate dkeyCert = tool.getZoneDkey(createdZone); |
Yumin Xia | 99c821a | 2017-04-07 11:01:08 -0700 | [diff] [blame] | 134 | NDNS_LOG_INFO("Generated DKEY " << dkeyCert.getName()); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 135 | ndn::io::save(dkeyCert, std::cout); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 136 | } |
| 137 | catch (const std::exception& ex) { |
| 138 | std::cerr << "Error: " << ex.what() << std::endl; |
| 139 | return 1; |
| 140 | } |
| 141 | } |