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