blob: afc8047020c98f756419ffbc2f227370a2ab596f [file] [log] [blame]
Alexander Afanasyev2b57aeb2018-06-15 18:32:28 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev5181d892020-06-06 18:05:47 -04003 * Copyright (c) 2014-2020, Regents of the University of California
Alexander Afanasyev2b57aeb2018-06-15 18:32:28 -04004 *
5 * NAC library is free software: you can redistribute it and/or modify it under the
6 * terms of the GNU Lesser General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option) any later version.
8 *
9 * NAC library is distributed in the hope that it will be useful, but WITHOUT ANY
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
11 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
12 *
13 * You should have received copies of the GNU General Public License and GNU Lesser
14 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
15 * <http://www.gnu.org/licenses/>.
16 *
17 * See AUTHORS.md for complete list of NAC library authors and contributors.
18 */
19
20#include "ndn-nac.hpp"
21#include "access-manager.hpp"
22
23namespace ndn {
24namespace nac {
25
26int
27nac_dump_kek(int argc, char** argv)
28{
29 namespace po = boost::program_options;
30
31 Name identityName;
32 Name datasetName;
33 std::string output;
34
35 po::options_description description("General Usage\n"
36 " ndn-nac dump-kek [-h] [-o output] [-d dataset] [-i] identity \n"
37 "General options");
38 description.add_options()
39 ("help,h", "Produce help message")
40 ("output,o", po::value<std::string>(&output), "(Optional) output file, stdout if not specified")
41 ("identity,i", po::value<Name>(&identityName), "Data owner's namespace identity")
42 ("dataset,d", po::value<Name>(&datasetName), "Name of dataset to control")
43 ;
44
45 po::positional_options_description p;
46 p.add("identity", 1);
47
48 po::variables_map vm;
49 try {
50 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
51 po::notify(vm);
52 }
53 catch (const std::exception& e) {
54 std::cerr << "ERROR: " << e.what() << std::endl;
55 std::cerr << description << std::endl;
56 return 1;
57 }
58
59 if (vm.count("help") != 0) {
60 std::cerr << description << std::endl;
61 return 0;
62 }
63
64 if (vm.count("identity") == 0) {
65 std::cerr << "ERROR: identity must be specified" << std::endl;
66 std::cerr << description << std::endl;
67 return 1;
68 }
69
70 if (vm.count("output") == 0)
71 output = "-";
72
73 try {
Alexander Afanasyev5181d892020-06-06 18:05:47 -040074 KeyChain keyChain;
75 Identity id = keyChain.getPib().getIdentity(identityName);
Alexander Afanasyev2b57aeb2018-06-15 18:32:28 -040076
77 util::DummyClientFace face(keyChain); // to avoid any real IO
78 AccessManager manager(id, datasetName, keyChain, face);
79
80 if (manager.size() != 1) {
81 std::cerr << "ERROR: Incorrect state of AccessManager instance (expect 1 KDK)" << std::endl;
82 return 2;
83 }
84
85 if (output == "-")
86 io::save(*manager.begin(), std::cout);
87 else
88 io::save(*manager.begin(), output);
89
90 return 0;
91 }
92 catch (const std::runtime_error& e) {
93 std::cerr << "ERROR: " << e.what() << std::endl;
94 return 1;
95 }
96}
97
98} // namespace nac
99} // namespace ndn