blob: 7003afc52c77636cf96e3a1d2fe82df9048c5423 [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_SET_DEFAULT_HPP
9#define NDNSEC_SET_DEFAULT_HPP
10
Yingdi Yub61f5402014-02-26 17:46:11 -080011int
12ndnsec_set_default(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080013{
14 using namespace ndn;
15 namespace po = boost::program_options;
16
17 std::string certFileName;
Yingdi Yub61f5402014-02-26 17:46:11 -080018 bool isSetDefaultId = true;
19 bool isSetDefaultKey = false;
20 bool isSetDefaultCert = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080021 std::string name;
22
Yingdi Yub61f5402014-02-26 17:46:11 -080023 po::options_description description("General Usage\n ndnsec set-default [-h] [-k|c] name\nGeneral options");
24 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080025 ("help,h", "produce help message")
Yingdi Yub61f5402014-02-26 17:46:11 -080026 ("default_key,k", "set default key of the identity")
27 ("default_cert,c", "set default certificate of the key")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080028 ("name,n", po::value<std::string>(&name), "the name to set")
29 ;
30
31 po::positional_options_description p;
32 p.add("name", 1);
33 po::variables_map vm;
Yingdi Yub61f5402014-02-26 17:46:11 -080034 try
Yingdi Yu8d7468f2014-02-21 14:49:45 -080035 {
Yingdi Yub61f5402014-02-26 17:46:11 -080036 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
37 vm);
38 po::notify(vm);
39 }
40 catch (const std::exception& e)
41 {
42 std::cerr << "ERROR: " << e.what() << std::endl;
43 std::cerr << description << std::endl;
44 return 1;
45 }
46
47 if (vm.count("help") != 0)
48 {
49 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080050 return 0;
51 }
52
Yingdi Yub61f5402014-02-26 17:46:11 -080053 if (vm.count("name") == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080054 {
Yingdi Yub61f5402014-02-26 17:46:11 -080055 std::cerr << "ERROR: name is required!" << std::endl;
56 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080057 return 1;
58 }
59
Yingdi Yub61f5402014-02-26 17:46:11 -080060 KeyChain keyChain;
61
62 if (vm.count("default_key") != 0)
63 {
64 isSetDefaultKey = true;
65 isSetDefaultId = false;
66 }
67 else if (vm.count("default_cert") != 0)
68 {
69 isSetDefaultCert = true;
70 isSetDefaultId = false;
71 }
72
73 if (isSetDefaultId)
74 {
75 Name idName(name);
76 keyChain.setDefaultIdentity(idName);
77 return 0;
78 }
79 if (isSetDefaultKey)
80 {
81 Name keyName(name);
82 keyChain.setDefaultKeyNameForIdentity(keyName);
83 return 0;
84 }
85
86 if (isSetDefaultCert)
87 {
88 keyChain.setDefaultCertificateNameForKey(name);
89 return 0;
90 }
91
92 return 1;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080093}
94#endif //NDNSEC_SET_DEFAULT_HPP