blob: ab045efd5e96fd4c389e29fa18b7051e693890d7 [file] [log] [blame]
Alexander Afanasyev82c359c2017-01-04 14:48:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2017 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * 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.
20 */
21
22#include "ndnsec.hpp"
23#include "util.hpp"
24
25namespace ndn {
26namespace ndnsec {
27
28int
29ndnsec_set_default(int argc, char** argv)
30{
31 using namespace ndn;
32 namespace po = boost::program_options;
33
34 std::string certFileName;
35 bool isSetDefaultId = true;
36 bool isSetDefaultKey = false;
37 bool isSetDefaultCert = false;
38 std::string name;
39
40 po::options_description description("General Usage\n ndnsec set-default [-h] [-k|c] name\nGeneral options");
41 description.add_options()
42 ("help,h", "produce help message")
43 ("default_key,k", "set default key of the identity")
44 ("default_cert,c", "set default certificate of the key")
45 ("name,n", po::value<std::string>(&name), "the name to set")
46 ;
47
48 po::positional_options_description p;
49 p.add("name", 1);
50 po::variables_map vm;
51 try {
52 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
53 po::notify(vm);
54 }
55 catch (const std::exception& e) {
56 std::cerr << "ERROR: " << e.what() << std::endl;
57 std::cerr << description << std::endl;
58 return 1;
59 }
60
61 if (vm.count("help") != 0) {
62 std::cerr << description << std::endl;
63 return 0;
64 }
65
66 if (vm.count("name") == 0) {
67 std::cerr << "ERROR: name is required!" << std::endl;
68 std::cerr << description << std::endl;
69 return 1;
70 }
71
72 security::v1::KeyChain keyChain;
73
74 if (vm.count("default_key") != 0) {
75 isSetDefaultKey = true;
76 isSetDefaultId = false;
77 }
78 else if (vm.count("default_cert") != 0) {
79 isSetDefaultCert = true;
80 isSetDefaultId = false;
81 }
82
83 if (isSetDefaultId) {
84 Name idName(name);
85 keyChain.setDefaultIdentity(idName);
86 return 0;
87 }
88 if (isSetDefaultKey) {
89 Name keyName(name);
90 keyChain.setDefaultKeyNameForIdentity(keyName);
91 return 0;
92 }
93
94 if (isSetDefaultCert) {
95 keyChain.setDefaultCertificateNameForKey(name);
96 return 0;
97 }
98
99 return 1;
100}
101
102} // namespace ndnsec
103} // namespace ndn