Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | af99f46 | 2015-01-19 21:43:09 -0800 | [diff] [blame^] | 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 24 | #ifndef NDN_TOOLS_NDNSEC_GET_DEFAULT_HPP |
| 25 | #define NDN_TOOLS_NDNSEC_GET_DEFAULT_HPP |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 26 | |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 27 | #include "util.hpp" |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 28 | |
| 29 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 30 | int |
| 31 | ndnsec_get_default(int argc, char** argv) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 32 | { |
| 33 | using namespace ndn; |
| 34 | namespace po = boost::program_options; |
| 35 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 36 | bool isGetDefaultId = true; |
| 37 | bool isGetDefaultKey = false; |
| 38 | bool isGetDefaultCert = false; |
| 39 | bool isQuiet = false; |
| 40 | std::string identityString; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 41 | std::string keyName; |
| 42 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 43 | po::options_description description("General Usage\n ndnsec get-default [-h] [-k|c] [-i identity|-K key] [-q]\nGeneral options"); |
| 44 | description.add_options() |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 45 | ("help,h", "produce help message") |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 46 | ("default_key,k", "get default key") |
| 47 | ("default_cert,c", "get default certificate") |
| 48 | ("identity,i", po::value<std::string>(&identityString), "target identity") |
| 49 | ("key,K", po::value<std::string>(&keyName), "target key") |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 50 | ("quiet,q", "don't output trailing newline") |
| 51 | ; |
| 52 | |
| 53 | po::variables_map vm; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 54 | try |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 55 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 56 | po::store(po::parse_command_line(argc, argv, description), vm); |
| 57 | po::notify(vm); |
| 58 | } |
| 59 | catch (const std::exception& e) |
| 60 | { |
| 61 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 62 | std::cerr << description << std::endl; |
| 63 | return 1; |
| 64 | } |
| 65 | |
| 66 | if (vm.count("help") != 0) |
| 67 | { |
| 68 | std::cerr << description << std::endl;; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 69 | return 0; |
| 70 | } |
| 71 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 72 | if (vm.count("default_cert") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 73 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 74 | isGetDefaultCert = true; |
| 75 | isGetDefaultId = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 76 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 77 | else if (vm.count("default_key") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 78 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 79 | isGetDefaultKey = true; |
| 80 | isGetDefaultId = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 83 | if (vm.count("quiet") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 84 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 85 | isQuiet = true; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 86 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 87 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 88 | KeyChain keyChain; |
| 89 | |
| 90 | if (vm.count("key") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 91 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 92 | Name keyNdnName(keyName); |
| 93 | if (isGetDefaultCert) |
| 94 | { |
| 95 | std::cout << keyChain.getDefaultCertificateNameForKey(keyNdnName); |
| 96 | if (!isQuiet) std::cout << std::endl; |
| 97 | return 0; |
| 98 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 99 | return 1; |
| 100 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 101 | else if (vm.count("identity") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 102 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 103 | Name identity(identityString); |
| 104 | |
| 105 | if (isGetDefaultKey) |
| 106 | { |
| 107 | std::cout << keyChain.getDefaultKeyNameForIdentity(identity); |
| 108 | if (!isQuiet) |
| 109 | std::cout << std::endl; |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | if (isGetDefaultCert) |
| 114 | { |
| 115 | std::cout << keyChain.getDefaultCertificateNameForIdentity(identity); |
| 116 | if (!isQuiet) |
| 117 | std::cout << std::endl; |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | return 1; |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | Name identity = keyChain.getDefaultIdentity(); |
| 126 | if (isGetDefaultId) |
| 127 | { |
| 128 | std::cout << identity; |
| 129 | if (!isQuiet) std::cout << std::endl; |
| 130 | return 0; |
| 131 | } |
| 132 | if (isGetDefaultKey) |
| 133 | { |
| 134 | std::cout << keyChain.getDefaultKeyNameForIdentity(identity); |
| 135 | if (!isQuiet) std::cout << std::endl; |
| 136 | return 0; |
| 137 | } |
| 138 | if (isGetDefaultCert) |
| 139 | { |
| 140 | std::cout << keyChain.getDefaultCertificateNameForIdentity(identity); |
| 141 | if (!isQuiet) std::cout << std::endl; |
| 142 | return 0; |
| 143 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 144 | return 1; |
| 145 | } |
| 146 | } |
| 147 | |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 148 | #endif // NDN_TOOLS_NDNSEC_GET_DEFAULT_HPP |