Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * BSD license, See the LICENSE file for more information |
| 5 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 6 | */ |
| 7 | |
| 8 | #ifndef NDNSEC_GET_DEFAULT_HPP |
| 9 | #define NDNSEC_GET_DEFAULT_HPP |
| 10 | |
| 11 | #include "ndnsec-util.hpp" |
| 12 | |
| 13 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 14 | int |
| 15 | ndnsec_get_default(int argc, char** argv) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 16 | { |
| 17 | using namespace ndn; |
| 18 | namespace po = boost::program_options; |
| 19 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 20 | bool isGetDefaultId = true; |
| 21 | bool isGetDefaultKey = false; |
| 22 | bool isGetDefaultCert = false; |
| 23 | bool isQuiet = false; |
| 24 | std::string identityString; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 25 | std::string keyName; |
| 26 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 27 | po::options_description description("General Usage\n ndnsec get-default [-h] [-k|c] [-i identity|-K key] [-q]\nGeneral options"); |
| 28 | description.add_options() |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 29 | ("help,h", "produce help message") |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 30 | ("default_key,k", "get default key") |
| 31 | ("default_cert,c", "get default certificate") |
| 32 | ("identity,i", po::value<std::string>(&identityString), "target identity") |
| 33 | ("key,K", po::value<std::string>(&keyName), "target key") |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 34 | ("quiet,q", "don't output trailing newline") |
| 35 | ; |
| 36 | |
| 37 | po::variables_map vm; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 38 | try |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 39 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 40 | po::store(po::parse_command_line(argc, argv, description), vm); |
| 41 | po::notify(vm); |
| 42 | } |
| 43 | catch (const std::exception& e) |
| 44 | { |
| 45 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 46 | std::cerr << description << std::endl; |
| 47 | return 1; |
| 48 | } |
| 49 | |
| 50 | if (vm.count("help") != 0) |
| 51 | { |
| 52 | std::cerr << description << std::endl;; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 53 | return 0; |
| 54 | } |
| 55 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 56 | if (vm.count("default_cert") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 57 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 58 | isGetDefaultCert = true; |
| 59 | isGetDefaultId = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 60 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 61 | else if (vm.count("default_key") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 62 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 63 | isGetDefaultKey = true; |
| 64 | isGetDefaultId = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 67 | if (vm.count("quiet") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 68 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 69 | isQuiet = true; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 70 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 71 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 72 | KeyChain keyChain; |
| 73 | |
| 74 | if (vm.count("key") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 75 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 76 | Name keyNdnName(keyName); |
| 77 | if (isGetDefaultCert) |
| 78 | { |
| 79 | std::cout << keyChain.getDefaultCertificateNameForKey(keyNdnName); |
| 80 | if (!isQuiet) std::cout << std::endl; |
| 81 | return 0; |
| 82 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 83 | return 1; |
| 84 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 85 | else if (vm.count("identity") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 86 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 87 | Name identity(identityString); |
| 88 | |
| 89 | if (isGetDefaultKey) |
| 90 | { |
| 91 | std::cout << keyChain.getDefaultKeyNameForIdentity(identity); |
| 92 | if (!isQuiet) |
| 93 | std::cout << std::endl; |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | if (isGetDefaultCert) |
| 98 | { |
| 99 | std::cout << keyChain.getDefaultCertificateNameForIdentity(identity); |
| 100 | if (!isQuiet) |
| 101 | std::cout << std::endl; |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | return 1; |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | Name identity = keyChain.getDefaultIdentity(); |
| 110 | if (isGetDefaultId) |
| 111 | { |
| 112 | std::cout << identity; |
| 113 | if (!isQuiet) std::cout << std::endl; |
| 114 | return 0; |
| 115 | } |
| 116 | if (isGetDefaultKey) |
| 117 | { |
| 118 | std::cout << keyChain.getDefaultKeyNameForIdentity(identity); |
| 119 | if (!isQuiet) std::cout << std::endl; |
| 120 | return 0; |
| 121 | } |
| 122 | if (isGetDefaultCert) |
| 123 | { |
| 124 | std::cout << keyChain.getDefaultCertificateNameForIdentity(identity); |
| 125 | if (!isQuiet) std::cout << std::endl; |
| 126 | return 0; |
| 127 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 128 | return 1; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | #endif //NDNSEC_GET_DEFAULT_HPP |