blob: b701015dbe1ec45da706dbcd3b95f3680f497b52 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu8d7468f2014-02-21 14:49:45 -080022 */
23
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080024#ifndef NDN_TOOLS_NDNSEC_GET_DEFAULT_HPP
25#define NDN_TOOLS_NDNSEC_GET_DEFAULT_HPP
Yingdi Yu8d7468f2014-02-21 14:49:45 -080026
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080027#include "util.hpp"
Yingdi Yu8d7468f2014-02-21 14:49:45 -080028
Yingdi Yub61f5402014-02-26 17:46:11 -080029int
30ndnsec_get_default(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080031{
32 using namespace ndn;
33 namespace po = boost::program_options;
34
Yingdi Yub61f5402014-02-26 17:46:11 -080035 bool isGetDefaultId = true;
36 bool isGetDefaultKey = false;
37 bool isGetDefaultCert = false;
38 bool isQuiet = false;
39 std::string identityString;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080040 std::string keyName;
41
Yingdi Yub61f5402014-02-26 17:46:11 -080042 po::options_description description("General Usage\n ndnsec get-default [-h] [-k|c] [-i identity|-K key] [-q]\nGeneral options");
43 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080044 ("help,h", "produce help message")
Yingdi Yub61f5402014-02-26 17:46:11 -080045 ("default_key,k", "get default key")
46 ("default_cert,c", "get default certificate")
47 ("identity,i", po::value<std::string>(&identityString), "target identity")
48 ("key,K", po::value<std::string>(&keyName), "target key")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080049 ("quiet,q", "don't output trailing newline")
50 ;
51
52 po::variables_map vm;
Yingdi Yub61f5402014-02-26 17:46:11 -080053 try
Yingdi Yu8d7468f2014-02-21 14:49:45 -080054 {
Yingdi Yub61f5402014-02-26 17:46:11 -080055 po::store(po::parse_command_line(argc, argv, description), vm);
56 po::notify(vm);
57 }
58 catch (const std::exception& e)
59 {
60 std::cerr << "ERROR: " << e.what() << std::endl;
61 std::cerr << description << std::endl;
62 return 1;
63 }
64
65 if (vm.count("help") != 0)
66 {
67 std::cerr << description << std::endl;;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080068 return 0;
69 }
70
Yingdi Yub61f5402014-02-26 17:46:11 -080071 if (vm.count("default_cert") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080072 {
Yingdi Yub61f5402014-02-26 17:46:11 -080073 isGetDefaultCert = true;
74 isGetDefaultId = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080075 }
Yingdi Yub61f5402014-02-26 17:46:11 -080076 else if (vm.count("default_key") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080077 {
Yingdi Yub61f5402014-02-26 17:46:11 -080078 isGetDefaultKey = true;
79 isGetDefaultId = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080080 }
81
Yingdi Yub61f5402014-02-26 17:46:11 -080082 if (vm.count("quiet") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080083 {
Yingdi Yub61f5402014-02-26 17:46:11 -080084 isQuiet = true;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080085 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080086
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080087 ndn::security::v1::KeyChain keyChain;
Yingdi Yub61f5402014-02-26 17:46:11 -080088
89 if (vm.count("key") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080090 {
Yingdi Yub61f5402014-02-26 17:46:11 -080091 Name keyNdnName(keyName);
92 if (isGetDefaultCert)
93 {
94 std::cout << keyChain.getDefaultCertificateNameForKey(keyNdnName);
95 if (!isQuiet) std::cout << std::endl;
96 return 0;
97 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080098 return 1;
99 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800100 else if (vm.count("identity") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800101 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800102 Name identity(identityString);
103
104 if (isGetDefaultKey)
105 {
106 std::cout << keyChain.getDefaultKeyNameForIdentity(identity);
107 if (!isQuiet)
108 std::cout << std::endl;
109
110 return 0;
111 }
112 if (isGetDefaultCert)
113 {
114 std::cout << keyChain.getDefaultCertificateNameForIdentity(identity);
115 if (!isQuiet)
116 std::cout << std::endl;
117
118 return 0;
119 }
120 return 1;
121 }
122 else
123 {
124 Name identity = keyChain.getDefaultIdentity();
125 if (isGetDefaultId)
126 {
127 std::cout << identity;
128 if (!isQuiet) std::cout << std::endl;
129 return 0;
130 }
131 if (isGetDefaultKey)
132 {
133 std::cout << keyChain.getDefaultKeyNameForIdentity(identity);
134 if (!isQuiet) std::cout << std::endl;
135 return 0;
136 }
137 if (isGetDefaultCert)
138 {
139 std::cout << keyChain.getDefaultCertificateNameForIdentity(identity);
140 if (!isQuiet) std::cout << std::endl;
141 return 0;
142 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800143 return 1;
144 }
145}
146
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -0800147#endif // NDN_TOOLS_NDNSEC_GET_DEFAULT_HPP