blob: 99c70f72cdbdcd514e161417391a3f82ba028e2c [file] [log] [blame]
Jiewen Tan870b29b2014-11-17 19:09:49 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California.
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 "config.hpp"
24
25#include <boost/program_options.hpp>
26#include <boost/filesystem.hpp>
27#include <string>
28
29int
30main(int argc, char* argv[])
31{
32 using std::string;
33 using namespace ndn;
34 ndn::ndns::log::init();
35 int cacheTtlInt = -1;
36 int certTtlInt = -1;
37 string zoneStr;
38 string parentStr;
39 string dskStr;
40 string kskStr;
41 string db;
42 try {
43 namespace po = boost::program_options;
44 po::variables_map vm;
45
46 po::options_description options("Generic Options");
47 options.add_options()
48 ("help,h", "print help message")
49 ("db,b", po::value<std::string>(&db), "Set the path of NDNS server database. "
50 "Default: " DEFAULT_DATABASE_PATH "/ndns.db")
51 ;
52
53 po::options_description config("Zone Options");
54 config.add_options()
55 ("cacheTtl,a", po::value<int>(&cacheTtlInt), "Set ttl of records of the zone and its "
56 "DSK ID-CERT. Default: 3600 seconds")
57 ("certTtl,e", po::value<int>(&certTtlInt), "Set ttl of DSK and KSK certificates. "
58 "Default: 365 days")
59 ("parent,p", po::value<std::string>(&parentStr), "Set the parent zone of the zone to be "
60 "created. Default: the zone's direct parent")
61 ("dsk,d", po::value<std::string>(&dskStr), "Set the name of DSK's certificate, "
62 "Default: generate new key and certificate")
63 ("ksk,k", po::value<std::string>(&kskStr), "Set the name of KSK's certificate, "
64 "Default: generate new key and certificate")
65 ;
66
67 options.add(config);
68
69 po::options_description hidden("Hidden Options");
70 hidden.add_options()
71 ("zone", po::value<string>(&zoneStr), "name of the zone to be created")
72 ;
73 po::positional_options_description postion;
74 postion.add("zone", 1);
75
76 po::options_description cmdline_options;
77 cmdline_options.add(options).add(hidden);
78
79 // po::options_description config_file_options;
80 // config_file_options.add(config).add(hidden);
81
82 po::parsed_options parsed =
83 po::command_line_parser(argc, argv).options(cmdline_options).positional(postion).run();
84
85 po::store(parsed, vm);
86 po::notify(vm);
87
88 if (vm.count("help")) {
89 std::cout << "Usage: ndns-create-zone [-b db] zone [-a cacheTtl] [-e certTtl] [-p parent] "
90 "[-d dskCert] [-k kskCert]" << std::endl;
91 std::cout << options << std::endl;
92 return 0;
93 }
94
95 if (vm.count("zone") == 0) {
96 std::cerr << "Error: zone must be specified" << std::endl;
97 return 1;
98 }
99 }
100 catch (const std::exception& ex) {
101 std::cerr << "Parameter Error: " << ex.what() << std::endl;
102 return 1;
103 }
104
105 try {
106 Name zone(zoneStr);
107 Name parent(parentStr);
108 if (!zone.empty() && parentStr.empty())
109 parent = zone.getPrefix(-1);
110
111 Name ksk(kskStr);
112 Name dsk(dskStr);
113
114 time::seconds cacheTtl;
115 time::seconds certTtl;
116 if (cacheTtlInt == -1)
117 cacheTtl = ndns::DEFAULT_CACHE_TTL;
118 else
119 cacheTtl = time::seconds(cacheTtlInt);
120
121 if (certTtlInt == -1)
122 certTtl = ndns::DEFAULT_CERT_TTL;
123 else
124 certTtl = time::seconds(certTtlInt);
125
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800126 ndn::KeyChain keyChain;
127 ndn::ndns::ManagementTool tool(db, keyChain);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800128 tool.createZone(zone, parent, cacheTtl, certTtl, ksk, dsk);
129 }
130 catch (const std::exception& ex) {
131 std::cerr << "Error: " << ex.what() << std::endl;
132 return 1;
133 }
134}