blob: ef7bcb892f078d46f398e179b4d63ea1426f458d [file] [log] [blame]
Yingdi Yu8d7468f2014-02-21 14:49:45 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * BSD license, See the LICENSE file for more information
5 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
6 */
7
8#ifndef NDNSEC_GET_DEFAULT_HPP
9#define NDNSEC_GET_DEFAULT_HPP
10
11#include "ndnsec-util.hpp"
12
13
Yingdi Yub61f5402014-02-26 17:46:11 -080014int
15ndnsec_get_default(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080016{
17 using namespace ndn;
18 namespace po = boost::program_options;
19
Yingdi Yub61f5402014-02-26 17:46:11 -080020 bool isGetDefaultId = true;
21 bool isGetDefaultKey = false;
22 bool isGetDefaultCert = false;
23 bool isQuiet = false;
24 std::string identityString;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080025 std::string keyName;
26
Yingdi Yub61f5402014-02-26 17:46:11 -080027 po::options_description description("General Usage\n ndnsec get-default [-h] [-k|c] [-i identity|-K key] [-q]\nGeneral options");
28 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080029 ("help,h", "produce help message")
Yingdi Yub61f5402014-02-26 17:46:11 -080030 ("default_key,k", "get default key")
31 ("default_cert,c", "get default certificate")
32 ("identity,i", po::value<std::string>(&identityString), "target identity")
33 ("key,K", po::value<std::string>(&keyName), "target key")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080034 ("quiet,q", "don't output trailing newline")
35 ;
36
37 po::variables_map vm;
Yingdi Yub61f5402014-02-26 17:46:11 -080038 try
Yingdi Yu8d7468f2014-02-21 14:49:45 -080039 {
Yingdi Yub61f5402014-02-26 17:46:11 -080040 po::store(po::parse_command_line(argc, argv, description), vm);
41 po::notify(vm);
42 }
43 catch (const std::exception& e)
44 {
45 std::cerr << "ERROR: " << e.what() << std::endl;
46 std::cerr << description << std::endl;
47 return 1;
48 }
49
50 if (vm.count("help") != 0)
51 {
52 std::cerr << description << std::endl;;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080053 return 0;
54 }
55
Yingdi Yub61f5402014-02-26 17:46:11 -080056 if (vm.count("default_cert") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080057 {
Yingdi Yub61f5402014-02-26 17:46:11 -080058 isGetDefaultCert = true;
59 isGetDefaultId = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080060 }
Yingdi Yub61f5402014-02-26 17:46:11 -080061 else if (vm.count("default_key") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080062 {
Yingdi Yub61f5402014-02-26 17:46:11 -080063 isGetDefaultKey = true;
64 isGetDefaultId = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080065 }
66
Yingdi Yub61f5402014-02-26 17:46:11 -080067 if (vm.count("quiet") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080068 {
Yingdi Yub61f5402014-02-26 17:46:11 -080069 isQuiet = true;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080070 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080071
Yingdi Yub61f5402014-02-26 17:46:11 -080072 KeyChain keyChain;
73
74 if (vm.count("key") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080075 {
Yingdi Yub61f5402014-02-26 17:46:11 -080076 Name keyNdnName(keyName);
77 if (isGetDefaultCert)
78 {
79 std::cout << keyChain.getDefaultCertificateNameForKey(keyNdnName);
80 if (!isQuiet) std::cout << std::endl;
81 return 0;
82 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080083 return 1;
84 }
Yingdi Yub61f5402014-02-26 17:46:11 -080085 else if (vm.count("identity") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080086 {
Yingdi Yub61f5402014-02-26 17:46:11 -080087 Name identity(identityString);
88
89 if (isGetDefaultKey)
90 {
91 std::cout << keyChain.getDefaultKeyNameForIdentity(identity);
92 if (!isQuiet)
93 std::cout << std::endl;
94
95 return 0;
96 }
97 if (isGetDefaultCert)
98 {
99 std::cout << keyChain.getDefaultCertificateNameForIdentity(identity);
100 if (!isQuiet)
101 std::cout << std::endl;
102
103 return 0;
104 }
105 return 1;
106 }
107 else
108 {
109 Name identity = keyChain.getDefaultIdentity();
110 if (isGetDefaultId)
111 {
112 std::cout << identity;
113 if (!isQuiet) std::cout << std::endl;
114 return 0;
115 }
116 if (isGetDefaultKey)
117 {
118 std::cout << keyChain.getDefaultKeyNameForIdentity(identity);
119 if (!isQuiet) std::cout << std::endl;
120 return 0;
121 }
122 if (isGetDefaultCert)
123 {
124 std::cout << keyChain.getDefaultCertificateNameForIdentity(identity);
125 if (!isQuiet) std::cout << std::endl;
126 return 0;
127 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800128 return 1;
129 }
130}
131
132#endif //NDNSEC_GET_DEFAULT_HPP