blob: ed23b6fdf048bb72b6f80a100b10460f820e006e [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_SET_DEFAULT_HPP
25#define NDN_TOOLS_NDNSEC_SET_DEFAULT_HPP
Yingdi Yu8d7468f2014-02-21 14:49:45 -080026
Yingdi Yub61f5402014-02-26 17:46:11 -080027int
28ndnsec_set_default(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080029{
30 using namespace ndn;
31 namespace po = boost::program_options;
32
33 std::string certFileName;
Yingdi Yub61f5402014-02-26 17:46:11 -080034 bool isSetDefaultId = true;
35 bool isSetDefaultKey = false;
36 bool isSetDefaultCert = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080037 std::string name;
38
Yingdi Yub61f5402014-02-26 17:46:11 -080039 po::options_description description("General Usage\n ndnsec set-default [-h] [-k|c] name\nGeneral options");
40 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080041 ("help,h", "produce help message")
Yingdi Yub61f5402014-02-26 17:46:11 -080042 ("default_key,k", "set default key of the identity")
43 ("default_cert,c", "set default certificate of the key")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080044 ("name,n", po::value<std::string>(&name), "the name to set")
45 ;
46
47 po::positional_options_description p;
48 p.add("name", 1);
49 po::variables_map vm;
Yingdi Yub61f5402014-02-26 17:46:11 -080050 try
Yingdi Yu8d7468f2014-02-21 14:49:45 -080051 {
Yingdi Yub61f5402014-02-26 17:46:11 -080052 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
53 vm);
54 po::notify(vm);
55 }
56 catch (const std::exception& e)
57 {
58 std::cerr << "ERROR: " << e.what() << std::endl;
59 std::cerr << description << std::endl;
60 return 1;
61 }
62
63 if (vm.count("help") != 0)
64 {
65 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080066 return 0;
67 }
68
Yingdi Yub61f5402014-02-26 17:46:11 -080069 if (vm.count("name") == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080070 {
Yingdi Yub61f5402014-02-26 17:46:11 -080071 std::cerr << "ERROR: name is required!" << std::endl;
72 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080073 return 1;
74 }
75
Yingdi Yub61f5402014-02-26 17:46:11 -080076 KeyChain keyChain;
77
78 if (vm.count("default_key") != 0)
79 {
80 isSetDefaultKey = true;
81 isSetDefaultId = false;
82 }
83 else if (vm.count("default_cert") != 0)
84 {
85 isSetDefaultCert = true;
86 isSetDefaultId = false;
87 }
88
89 if (isSetDefaultId)
90 {
91 Name idName(name);
92 keyChain.setDefaultIdentity(idName);
93 return 0;
94 }
95 if (isSetDefaultKey)
96 {
97 Name keyName(name);
98 keyChain.setDefaultKeyNameForIdentity(keyName);
99 return 0;
100 }
101
102 if (isSetDefaultCert)
103 {
104 keyChain.setDefaultCertificateNameForKey(name);
105 return 0;
106 }
107
108 return 1;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800109}
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -0800110#endif // NDN_TOOLS_NDNSEC_SET_DEFAULT_HPP