blob: 96ee12e08f0e8730af4f4b1025b0d4fb12355c11 [file] [log] [blame]
Alexander Afanasyev82c359c2017-01-04 14:48:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2017 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
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.
20 */
21
22#include "ndnsec.hpp"
23#include "util.hpp"
24
25namespace ndn {
26namespace ndnsec {
27
28int
29ndnsec_get_default(int argc, char** argv)
30{
31 using namespace ndn;
32 namespace po = boost::program_options;
33
34 bool isGetDefaultId = true;
35 bool isGetDefaultKey = false;
36 bool isGetDefaultCert = false;
37 bool isQuiet = false;
38 std::string identityString;
39 std::string keyName;
40
41 po::options_description description("General Usage\n"
42 " ndnsec get-default [-h] [-k|c] [-i identity|-K key] [-q]\n"
43 "General options");
44 description.add_options()
45 ("help,h", "produce help message")
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")
50 ("quiet,q", "don't output trailing newline")
51 ;
52
53 po::variables_map vm;
54 try {
55 po::store(po::parse_command_line(argc, argv, description), vm);
56 po::notify(vm);
57 }
58 catch (const std::exception& e) {
59 std::cerr << "ERROR: " << e.what() << std::endl;
60 std::cerr << description << std::endl;
61 return 1;
62 }
63
64 if (vm.count("help") != 0) {
65 std::cerr << description << std::endl;
66 ;
67 return 0;
68 }
69
70 if (vm.count("default_cert") != 0) {
71 isGetDefaultCert = true;
72 isGetDefaultId = false;
73 }
74 else if (vm.count("default_key") != 0) {
75 isGetDefaultKey = true;
76 isGetDefaultId = false;
77 }
78
79 if (vm.count("quiet") != 0) {
80 isQuiet = true;
81 }
82
83 security::v1::KeyChain keyChain;
84
85 if (vm.count("key") != 0) {
86 Name keyNdnName(keyName);
87 if (isGetDefaultCert) {
88 std::cout << keyChain.getDefaultCertificateNameForKey(keyNdnName);
89 if (!isQuiet)
90 std::cout << std::endl;
91 return 0;
92 }
93 return 1;
94 }
95 else if (vm.count("identity") != 0) {
96 Name identity(identityString);
97
98 if (isGetDefaultKey) {
99 std::cout << keyChain.getDefaultKeyNameForIdentity(identity);
100 if (!isQuiet)
101 std::cout << std::endl;
102
103 return 0;
104 }
105 if (isGetDefaultCert) {
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 Name identity = keyChain.getDefaultIdentity();
116 if (isGetDefaultId) {
117 std::cout << identity;
118 if (!isQuiet)
119 std::cout << std::endl;
120 return 0;
121 }
122 if (isGetDefaultKey) {
123 std::cout << keyChain.getDefaultKeyNameForIdentity(identity);
124 if (!isQuiet)
125 std::cout << std::endl;
126 return 0;
127 }
128 if (isGetDefaultCert) {
129 std::cout << keyChain.getDefaultCertificateNameForIdentity(identity);
130 if (!isQuiet)
131 std::cout << std::endl;
132 return 0;
133 }
134 return 1;
135 }
136}
137
138} // namespace ndnsec
139} // namespace ndn