blob: 41f4f2a5bc6af4ae4edb3477f053c19ffa6dd576 [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 Afanasyevaf99f462015-01-19 21:43:09 -08003 * Copyright (c) 2013-2015 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
29
Yingdi Yub61f5402014-02-26 17:46:11 -080030int
31ndnsec_get_default(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080032{
33 using namespace ndn;
34 namespace po = boost::program_options;
35
Yingdi Yub61f5402014-02-26 17:46:11 -080036 bool isGetDefaultId = true;
37 bool isGetDefaultKey = false;
38 bool isGetDefaultCert = false;
39 bool isQuiet = false;
40 std::string identityString;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080041 std::string keyName;
42
Yingdi Yub61f5402014-02-26 17:46:11 -080043 po::options_description description("General Usage\n ndnsec get-default [-h] [-k|c] [-i identity|-K key] [-q]\nGeneral options");
44 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080045 ("help,h", "produce help message")
Yingdi Yub61f5402014-02-26 17:46:11 -080046 ("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")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080050 ("quiet,q", "don't output trailing newline")
51 ;
52
53 po::variables_map vm;
Yingdi Yub61f5402014-02-26 17:46:11 -080054 try
Yingdi Yu8d7468f2014-02-21 14:49:45 -080055 {
Yingdi Yub61f5402014-02-26 17:46:11 -080056 po::store(po::parse_command_line(argc, argv, description), vm);
57 po::notify(vm);
58 }
59 catch (const std::exception& e)
60 {
61 std::cerr << "ERROR: " << e.what() << std::endl;
62 std::cerr << description << std::endl;
63 return 1;
64 }
65
66 if (vm.count("help") != 0)
67 {
68 std::cerr << description << std::endl;;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080069 return 0;
70 }
71
Yingdi Yub61f5402014-02-26 17:46:11 -080072 if (vm.count("default_cert") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080073 {
Yingdi Yub61f5402014-02-26 17:46:11 -080074 isGetDefaultCert = true;
75 isGetDefaultId = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080076 }
Yingdi Yub61f5402014-02-26 17:46:11 -080077 else if (vm.count("default_key") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080078 {
Yingdi Yub61f5402014-02-26 17:46:11 -080079 isGetDefaultKey = true;
80 isGetDefaultId = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080081 }
82
Yingdi Yub61f5402014-02-26 17:46:11 -080083 if (vm.count("quiet") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080084 {
Yingdi Yub61f5402014-02-26 17:46:11 -080085 isQuiet = true;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080086 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080087
Yingdi Yub61f5402014-02-26 17:46:11 -080088 KeyChain keyChain;
89
90 if (vm.count("key") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080091 {
Yingdi Yub61f5402014-02-26 17:46:11 -080092 Name keyNdnName(keyName);
93 if (isGetDefaultCert)
94 {
95 std::cout << keyChain.getDefaultCertificateNameForKey(keyNdnName);
96 if (!isQuiet) std::cout << std::endl;
97 return 0;
98 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080099 return 1;
100 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800101 else if (vm.count("identity") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800102 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800103 Name identity(identityString);
104
105 if (isGetDefaultKey)
106 {
107 std::cout << keyChain.getDefaultKeyNameForIdentity(identity);
108 if (!isQuiet)
109 std::cout << std::endl;
110
111 return 0;
112 }
113 if (isGetDefaultCert)
114 {
115 std::cout << keyChain.getDefaultCertificateNameForIdentity(identity);
116 if (!isQuiet)
117 std::cout << std::endl;
118
119 return 0;
120 }
121 return 1;
122 }
123 else
124 {
125 Name identity = keyChain.getDefaultIdentity();
126 if (isGetDefaultId)
127 {
128 std::cout << identity;
129 if (!isQuiet) std::cout << std::endl;
130 return 0;
131 }
132 if (isGetDefaultKey)
133 {
134 std::cout << keyChain.getDefaultKeyNameForIdentity(identity);
135 if (!isQuiet) std::cout << std::endl;
136 return 0;
137 }
138 if (isGetDefaultCert)
139 {
140 std::cout << keyChain.getDefaultCertificateNameForIdentity(identity);
141 if (!isQuiet) std::cout << std::endl;
142 return 0;
143 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800144 return 1;
145 }
146}
147
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -0800148#endif // NDN_TOOLS_NDNSEC_GET_DEFAULT_HPP