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 | |
| 14 | int |
| 15 | ndnsec_get_default(int argc, char** argv) |
| 16 | { |
| 17 | using namespace ndn; |
| 18 | namespace po = boost::program_options; |
| 19 | |
| 20 | bool getDefaultId = true; |
| 21 | bool getDefaultKey = false; |
| 22 | bool getDefaultCert = false; |
| 23 | bool quiet = false; |
| 24 | std::string idName; |
| 25 | std::string keyName; |
| 26 | |
| 27 | po::options_description desc("General Usage\n ndnsec get-default [-h] [-K|C] [-i identity|-k key] [-q]\nGeneral options"); |
| 28 | desc.add_options() |
| 29 | ("help,h", "produce help message") |
| 30 | ("default_key,K", "get default key") |
| 31 | ("default_cert,C", "get default certificate") |
| 32 | ("identity,i", po::value<std::string>(&idName), "target identity") |
| 33 | ("key,k", po::value<std::string>(&keyName), "target key") |
| 34 | ("quiet,q", "don't output trailing newline") |
| 35 | ; |
| 36 | |
| 37 | po::variables_map vm; |
| 38 | po::store(po::parse_command_line(argc, argv, desc), vm); |
| 39 | po::notify(vm); |
| 40 | |
| 41 | if (vm.count("help")) |
| 42 | { |
| 43 | std::cerr << desc << std::endl;; |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | if(vm.count("default_cert")) |
| 48 | { |
| 49 | getDefaultCert = true; |
| 50 | getDefaultId = false; |
| 51 | } |
| 52 | else if(vm.count("default_key")) |
| 53 | { |
| 54 | getDefaultKey = true; |
| 55 | getDefaultId = false; |
| 56 | } |
| 57 | |
| 58 | if(vm.count("quiet")) |
| 59 | { |
| 60 | quiet = true; |
| 61 | } |
| 62 | |
| 63 | try |
| 64 | { |
| 65 | KeyChain keyChain; |
| 66 | |
| 67 | if(vm.count("key")) |
| 68 | { |
| 69 | Name keyNdnName(keyName); |
| 70 | if(getDefaultCert) |
| 71 | { |
| 72 | std::cout << keyChain.getDefaultCertificateNameForKey(keyNdnName); |
| 73 | if (!quiet) std::cout << std::endl; |
| 74 | return 0; |
| 75 | } |
| 76 | return 1; |
| 77 | } |
| 78 | else if(vm.count("identity")) |
| 79 | { |
| 80 | Name idNdnName(idName); |
| 81 | |
| 82 | if(getDefaultKey) |
| 83 | { |
| 84 | std::cout << keyChain.getDefaultKeyNameForIdentity(idNdnName); |
| 85 | if (!quiet) std::cout << std::endl; |
| 86 | return 0; |
| 87 | } |
| 88 | if(getDefaultCert) |
| 89 | { |
| 90 | std::cout << keyChain.getDefaultCertificateNameForIdentity(idNdnName); |
| 91 | if (!quiet) std::cout << std::endl; |
| 92 | return 0; |
| 93 | } |
| 94 | return 1; |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | Name idNdnName = keyChain.getDefaultIdentity(); |
| 99 | if(getDefaultId) |
| 100 | { |
| 101 | std::cout << idNdnName; |
| 102 | if (!quiet) std::cout << std::endl; |
| 103 | return 0; |
| 104 | } |
| 105 | if(getDefaultKey) |
| 106 | { |
| 107 | std::cout << keyChain.getDefaultKeyNameForIdentity(idNdnName); |
| 108 | if (!quiet) std::cout << std::endl; |
| 109 | return 0; |
| 110 | } |
| 111 | if(getDefaultCert) |
| 112 | { |
| 113 | std::cout << keyChain.getDefaultCertificateNameForIdentity(idNdnName); |
| 114 | if (!quiet) std::cout << std::endl; |
| 115 | return 0; |
| 116 | } |
| 117 | return 1; |
| 118 | } |
| 119 | } |
| 120 | catch(SecPublicInfo::Error& e) |
| 121 | { |
| 122 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 123 | return 1; |
| 124 | } |
| 125 | catch(SecTpm::Error& e) |
| 126 | { |
| 127 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 128 | return 1; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | #endif //NDNSEC_GET_DEFAULT_HPP |