Alexander Afanasyev | 2b57aeb | 2018-06-15 18:32:28 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 5181d89 | 2020-06-06 18:05:47 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, Regents of the University of California |
Alexander Afanasyev | 2b57aeb | 2018-06-15 18:32:28 -0400 | [diff] [blame] | 4 | * |
| 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 | |
| 23 | namespace ndn { |
| 24 | namespace nac { |
| 25 | |
| 26 | int |
| 27 | nac_add_member(int argc, char** argv) |
| 28 | { |
| 29 | namespace po = boost::program_options; |
| 30 | |
| 31 | Name identityName; |
| 32 | Name datasetName; |
| 33 | std::string output; |
| 34 | std::string member; |
| 35 | |
| 36 | po::options_description description("General Usage\n" |
| 37 | " ndn-nac add-member [-h] [-o output] [-d dataset] [-i] identity [-m] memberCert\n" |
| 38 | "General options"); |
| 39 | description.add_options() |
| 40 | ("help,h", "Produce help message") |
| 41 | ("output,o", po::value<std::string>(&output), "(Optional) output file, stdout if not specified") |
| 42 | ("identity,i", po::value<Name>(&identityName), "Data owner's namespace identity") |
| 43 | ("dataset,d", po::value<Name>(&datasetName), "Name of dataset to control") |
| 44 | ("member,m", po::value<std::string>(&member), "File with member's certificate, stdin if -") |
| 45 | ; |
| 46 | |
| 47 | po::positional_options_description p; |
| 48 | p.add("identity", 1); |
| 49 | p.add("member", 1); |
| 50 | |
| 51 | po::variables_map vm; |
| 52 | try { |
| 53 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm); |
| 54 | po::notify(vm); |
| 55 | } |
| 56 | catch (const std::exception& e) { |
| 57 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 58 | std::cerr << description << std::endl; |
| 59 | return 1; |
| 60 | } |
| 61 | |
| 62 | if (vm.count("help") != 0) { |
| 63 | std::cerr << description << std::endl; |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | if (vm.count("identity") == 0) { |
| 68 | std::cerr << "ERROR: identity must be specified" << std::endl; |
| 69 | std::cerr << description << std::endl; |
| 70 | return 1; |
| 71 | } |
| 72 | |
| 73 | if (vm.count("member") == 0) { |
| 74 | std::cerr << "ERROR: member must be specified" << std::endl; |
| 75 | std::cerr << description << std::endl; |
| 76 | return 1; |
| 77 | } |
| 78 | |
| 79 | if (vm.count("output") == 0) |
| 80 | output = "-"; |
| 81 | |
| 82 | try { |
Alexander Afanasyev | 5181d89 | 2020-06-06 18:05:47 -0400 | [diff] [blame] | 83 | KeyChain keyChain; |
| 84 | Identity id = keyChain.getPib().getIdentity(identityName); |
Alexander Afanasyev | 2b57aeb | 2018-06-15 18:32:28 -0400 | [diff] [blame] | 85 | |
| 86 | auto cert = loadCertificate(member); |
| 87 | |
| 88 | util::DummyClientFace face(keyChain); // to avoid any real IO |
| 89 | AccessManager manager(id, datasetName, keyChain, face); |
| 90 | |
| 91 | auto kdk = manager.addMember(cert); |
| 92 | |
| 93 | if (output == "-") |
| 94 | io::save(kdk, std::cout); |
| 95 | else |
| 96 | io::save(kdk, output); |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | catch (const std::runtime_error& e) { |
| 101 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 102 | return 1; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | } // namespace nac |
| 107 | } // namespace ndn |