blob: 4d82044100145effab1b9e38b0e6b480fa4d5435 [file] [log] [blame]
Alexander Afanasyev2b57aeb2018-06-15 18:32:28 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventobde084f2022-04-17 00:21:35 -04002/*
3 * Copyright (c) 2014-2022, 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
Davide Pesaventobde084f2022-04-17 00:21:35 -040023namespace ndn::nac {
Alexander Afanasyev2b57aeb2018-06-15 18:32:28 -040024
25int
26nac_add_member(int argc, char** argv)
27{
28 namespace po = boost::program_options;
29
30 Name identityName;
31 Name datasetName;
32 std::string output;
33 std::string member;
34
35 po::options_description description("General Usage\n"
36 " ndn-nac add-member [-h] [-o output] [-d dataset] [-i] identity [-m] memberCert\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 ("member,m", po::value<std::string>(&member), "File with member's certificate, stdin if -")
44 ;
45
46 po::positional_options_description p;
47 p.add("identity", 1);
48 p.add("member", 1);
49
50 po::variables_map vm;
51 try {
52 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
53 po::notify(vm);
54 }
55 catch (const std::exception& e) {
56 std::cerr << "ERROR: " << e.what() << std::endl;
57 std::cerr << description << std::endl;
58 return 1;
59 }
60
61 if (vm.count("help") != 0) {
62 std::cerr << description << std::endl;
63 return 0;
64 }
65
66 if (vm.count("identity") == 0) {
67 std::cerr << "ERROR: identity must be specified" << std::endl;
68 std::cerr << description << std::endl;
69 return 1;
70 }
71
72 if (vm.count("member") == 0) {
73 std::cerr << "ERROR: member must be specified" << std::endl;
74 std::cerr << description << std::endl;
75 return 1;
76 }
77
78 if (vm.count("output") == 0)
79 output = "-";
80
81 try {
Alexander Afanasyev5181d892020-06-06 18:05:47 -040082 KeyChain keyChain;
83 Identity id = keyChain.getPib().getIdentity(identityName);
Alexander Afanasyev2b57aeb2018-06-15 18:32:28 -040084
85 auto cert = loadCertificate(member);
86
87 util::DummyClientFace face(keyChain); // to avoid any real IO
88 AccessManager manager(id, datasetName, keyChain, face);
89
90 auto kdk = manager.addMember(cert);
91
92 if (output == "-")
93 io::save(kdk, std::cout);
94 else
95 io::save(kdk, output);
96
97 return 0;
98 }
99 catch (const std::runtime_error& e) {
100 std::cerr << "ERROR: " << e.what() << std::endl;
101 return 1;
102 }
103}
104
Davide Pesaventobde084f2022-04-17 00:21:35 -0400105} // namespace ndn::nac