Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -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 | * Yingdi Yu |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 9 | */ |
| 10 | |
| 11 | #include <iostream> |
| 12 | #include <fstream> |
| 13 | |
| 14 | #include <boost/program_options/options_description.hpp> |
| 15 | #include <boost/program_options/variables_map.hpp> |
| 16 | #include <boost/program_options/parsers.hpp> |
| 17 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 18 | #include <cryptopp/base64.h> |
| 19 | |
| 20 | #include "security/key-chain.hpp" |
| 21 | |
| 22 | using namespace ndn; |
| 23 | namespace po = boost::program_options; |
| 24 | |
| 25 | int main(int argc, char** argv) |
| 26 | { |
| 27 | bool getDefaultId = true; |
| 28 | bool getDefaultKey = false; |
| 29 | bool getDefaultCert = false; |
| 30 | bool quiet = false; |
| 31 | std::string idName; |
| 32 | std::string keyName; |
| 33 | |
| 34 | po::options_description desc("General Usage\n ndn-get-default [-h] [-K|C] [-i identity|-k key]\nGeneral options"); |
| 35 | desc.add_options() |
| 36 | ("help,h", "produce help message") |
| 37 | ("default_key,K", "get default key") |
| 38 | ("default_cert,C", "get default certificate") |
| 39 | ("identity,i", po::value<std::string>(&idName), "target identity") |
| 40 | ("key,k", po::value<std::string>(&keyName), "target key") |
| 41 | ("quiet,q", "don't output trailing newline") |
| 42 | ; |
| 43 | |
| 44 | po::variables_map vm; |
| 45 | po::store(po::parse_command_line(argc, argv, desc), vm); |
| 46 | po::notify(vm); |
| 47 | |
| 48 | if (vm.count("help")) |
| 49 | { |
| 50 | std::cerr << desc << std::endl;; |
| 51 | return 1; |
| 52 | } |
| 53 | |
| 54 | if(vm.count("default_cert")) |
| 55 | { |
| 56 | getDefaultCert = true; |
| 57 | getDefaultId = false; |
| 58 | } |
| 59 | else if(vm.count("default_key")) |
| 60 | { |
| 61 | getDefaultKey = true; |
| 62 | getDefaultId = false; |
| 63 | } |
| 64 | |
| 65 | if(vm.count("quiet")) |
| 66 | { |
| 67 | quiet = true; |
| 68 | } |
| 69 | |
| 70 | KeyChain keyChain; |
| 71 | bool ok = false; |
| 72 | |
| 73 | if(vm.count("key")) |
| 74 | { |
| 75 | Name keyNdnName(keyName); |
| 76 | if(getDefaultCert) |
| 77 | { |
| 78 | std::cout << keyChain.getDefaultCertificateNameForKey(keyNdnName); |
| 79 | if (!quiet) std::cout << std::endl; |
| 80 | return 0; |
| 81 | } |
| 82 | return 1; |
| 83 | } |
| 84 | else if(vm.count("identity")) |
| 85 | { |
| 86 | Name idNdnName(idName); |
| 87 | |
| 88 | if(getDefaultKey) |
| 89 | { |
| 90 | std::cout << keyChain.getDefaultKeyNameForIdentity(idNdnName); |
| 91 | if (!quiet) std::cout << std::endl; |
| 92 | return 0; |
| 93 | } |
| 94 | if(getDefaultCert) |
| 95 | { |
| 96 | std::cout << keyChain.getDefaultCertificateNameForIdentity(idNdnName); |
| 97 | if (!quiet) std::cout << std::endl; |
| 98 | return 0; |
| 99 | } |
| 100 | return 1; |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | Name idNdnName = keyChain.getDefaultIdentity(); |
| 105 | if(getDefaultId) |
| 106 | { |
| 107 | std::cout << idNdnName; |
| 108 | if (!quiet) std::cout << std::endl; |
| 109 | return 0; |
| 110 | } |
| 111 | if(getDefaultKey) |
| 112 | { |
| 113 | std::cout << keyChain.getDefaultKeyNameForIdentity(idNdnName); |
| 114 | if (!quiet) std::cout << std::endl; |
| 115 | return 0; |
| 116 | } |
| 117 | if(getDefaultCert) |
| 118 | { |
| 119 | std::cout << keyChain.getDefaultCertificateNameForIdentity(idNdnName); |
| 120 | if (!quiet) std::cout << std::endl; |
| 121 | return 0; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | |
| 126 | } |