blob: e93153e4a410f01863ed8872638ba18e6fc41b4f [file] [log] [blame]
Yingdi Yu8d7468f2014-02-21 14:49:45 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
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 Yu8d7468f2014-02-21 14:49:45 -080013 */
14
15#ifndef NDNSEC_GET_DEFAULT_HPP
16#define NDNSEC_GET_DEFAULT_HPP
17
18#include "ndnsec-util.hpp"
19
20
Yingdi Yub61f5402014-02-26 17:46:11 -080021int
22ndnsec_get_default(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080023{
24 using namespace ndn;
25 namespace po = boost::program_options;
26
Yingdi Yub61f5402014-02-26 17:46:11 -080027 bool isGetDefaultId = true;
28 bool isGetDefaultKey = false;
29 bool isGetDefaultCert = false;
30 bool isQuiet = false;
31 std::string identityString;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080032 std::string keyName;
33
Yingdi Yub61f5402014-02-26 17:46:11 -080034 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 Yu8d7468f2014-02-21 14:49:45 -080036 ("help,h", "produce help message")
Yingdi Yub61f5402014-02-26 17:46:11 -080037 ("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 Yu8d7468f2014-02-21 14:49:45 -080041 ("quiet,q", "don't output trailing newline")
42 ;
43
44 po::variables_map vm;
Yingdi Yub61f5402014-02-26 17:46:11 -080045 try
Yingdi Yu8d7468f2014-02-21 14:49:45 -080046 {
Yingdi Yub61f5402014-02-26 17:46:11 -080047 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 Yu8d7468f2014-02-21 14:49:45 -080060 return 0;
61 }
62
Yingdi Yub61f5402014-02-26 17:46:11 -080063 if (vm.count("default_cert") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080064 {
Yingdi Yub61f5402014-02-26 17:46:11 -080065 isGetDefaultCert = true;
66 isGetDefaultId = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080067 }
Yingdi Yub61f5402014-02-26 17:46:11 -080068 else if (vm.count("default_key") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080069 {
Yingdi Yub61f5402014-02-26 17:46:11 -080070 isGetDefaultKey = true;
71 isGetDefaultId = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080072 }
73
Yingdi Yub61f5402014-02-26 17:46:11 -080074 if (vm.count("quiet") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080075 {
Yingdi Yub61f5402014-02-26 17:46:11 -080076 isQuiet = true;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080077 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080078
Yingdi Yub61f5402014-02-26 17:46:11 -080079 KeyChain keyChain;
80
81 if (vm.count("key") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080082 {
Yingdi Yub61f5402014-02-26 17:46:11 -080083 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 Yu8d7468f2014-02-21 14:49:45 -080090 return 1;
91 }
Yingdi Yub61f5402014-02-26 17:46:11 -080092 else if (vm.count("identity") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080093 {
Yingdi Yub61f5402014-02-26 17:46:11 -080094 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 Yu8d7468f2014-02-21 14:49:45 -0800135 return 1;
136 }
137}
138
139#endif //NDNSEC_GET_DEFAULT_HPP