blob: d477d4b5788547004a5df3e0b61cc24099de75e5 [file] [log] [blame]
Jiewen Tan870b29b2014-11-17 19:09:49 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yumin Xia2c509c22017-02-09 14:37:36 -08002/*
Alexander Afanasyev08d18742018-03-15 16:31:28 -04003 * Copyright (c) 2014-2018, 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 "config.hpp"
24
25#include <boost/program_options.hpp>
26#include <boost/filesystem.hpp>
27#include <string>
28
Alexander Afanasyev08d18742018-03-15 16:31:28 -040029NDNS_LOG_INIT(NdnsCreateZone);
Yumin Xia99c821a2017-04-07 11:01:08 -070030
Jiewen Tan870b29b2014-11-17 19:09:49 -080031int
32main(int argc, char* argv[])
33{
34 using std::string;
35 using namespace ndn;
Jiewen Tan870b29b2014-11-17 19:09:49 -080036 int cacheTtlInt = -1;
37 int certTtlInt = -1;
38 string zoneStr;
39 string parentStr;
40 string dskStr;
41 string kskStr;
Yumin Xia2c509c22017-02-09 14:37:36 -080042 string dkeyStr;
Jiewen Tan870b29b2014-11-17 19:09:49 -080043 string db;
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()
50 ("help,h", "print help message")
51 ("db,b", po::value<std::string>(&db), "Set the path of NDNS server database. "
52 "Default: " DEFAULT_DATABASE_PATH "/ndns.db")
53 ;
54
55 po::options_description config("Zone Options");
56 config.add_options()
57 ("cacheTtl,a", po::value<int>(&cacheTtlInt), "Set ttl of records of the zone and its "
Yumin Xia2c509c22017-02-09 14:37:36 -080058 "DSK CERT. Default: 3600 seconds")
Jiewen Tan870b29b2014-11-17 19:09:49 -080059 ("certTtl,e", po::value<int>(&certTtlInt), "Set ttl of DSK and KSK certificates. "
60 "Default: 365 days")
61 ("parent,p", po::value<std::string>(&parentStr), "Set the parent zone of the zone to be "
62 "created. Default: the zone's direct parent")
63 ("dsk,d", po::value<std::string>(&dskStr), "Set the name of DSK's certificate, "
64 "Default: generate new key and certificate")
65 ("ksk,k", po::value<std::string>(&kskStr), "Set the name of KSK's certificate, "
66 "Default: generate new key and certificate")
Yumin Xia2c509c22017-02-09 14:37:36 -080067 ("dkey,g", po::value<std::string>(&dkeyStr), "Set the name of DKEY's certificate, "
68 "Default: generate new key and certificate")
Jiewen Tan870b29b2014-11-17 19:09:49 -080069 ;
70
71 options.add(config);
72
73 po::options_description hidden("Hidden Options");
74 hidden.add_options()
75 ("zone", po::value<string>(&zoneStr), "name of the zone to be created")
76 ;
77 po::positional_options_description postion;
78 postion.add("zone", 1);
79
80 po::options_description cmdline_options;
81 cmdline_options.add(options).add(hidden);
82
83 // po::options_description config_file_options;
84 // config_file_options.add(config).add(hidden);
85
86 po::parsed_options parsed =
87 po::command_line_parser(argc, argv).options(cmdline_options).positional(postion).run();
88
89 po::store(parsed, vm);
90 po::notify(vm);
91
92 if (vm.count("help")) {
93 std::cout << "Usage: ndns-create-zone [-b db] zone [-a cacheTtl] [-e certTtl] [-p parent] "
Yumin Xia2c509c22017-02-09 14:37:36 -080094 "[-d dskCert] [-k kskCert] [-g dkeyCert]" << std::endl;
Jiewen Tan870b29b2014-11-17 19:09:49 -080095 std::cout << options << std::endl;
96 return 0;
97 }
98
99 if (vm.count("zone") == 0) {
100 std::cerr << "Error: zone must be specified" << std::endl;
101 return 1;
102 }
103 }
104 catch (const std::exception& ex) {
105 std::cerr << "Parameter Error: " << ex.what() << std::endl;
106 return 1;
107 }
108
109 try {
110 Name zone(zoneStr);
111 Name parent(parentStr);
112 if (!zone.empty() && parentStr.empty())
113 parent = zone.getPrefix(-1);
114
115 Name ksk(kskStr);
116 Name dsk(dskStr);
Yumin Xia2c509c22017-02-09 14:37:36 -0800117 Name dkey(dkeyStr);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800118
119 time::seconds cacheTtl;
120 time::seconds certTtl;
121 if (cacheTtlInt == -1)
122 cacheTtl = ndns::DEFAULT_CACHE_TTL;
123 else
124 cacheTtl = time::seconds(cacheTtlInt);
125
126 if (certTtlInt == -1)
127 certTtl = ndns::DEFAULT_CERT_TTL;
128 else
129 certTtl = time::seconds(certTtlInt);
130
Yumin Xia2c509c22017-02-09 14:37:36 -0800131 KeyChain keyChain;
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800132 ndn::ndns::ManagementTool tool(db, keyChain);
Yumin Xia2c509c22017-02-09 14:37:36 -0800133 ndn::ndns::Zone createdZone = tool.createZone(zone, parent, cacheTtl, certTtl, ksk, dsk, dkey);
134 ndn::security::v2::Certificate dkeyCert = tool.getZoneDkey(createdZone);
Yumin Xia99c821a2017-04-07 11:01:08 -0700135 NDNS_LOG_INFO("Generated DKEY " << dkeyCert.getName());
Yumin Xia2c509c22017-02-09 14:37:36 -0800136 ndn::io::save(dkeyCert, std::cout);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800137 }
138 catch (const std::exception& ex) {
139 std::cerr << "Error: " << ex.what() << std::endl;
140 return 1;
141 }
142}