blob: 25eb98de5912dd671f81851b84a15b9b16dc1804 [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_SET_DEFAULT_HPP
16#define NDNSEC_SET_DEFAULT_HPP
17
Yingdi Yub61f5402014-02-26 17:46:11 -080018int
19ndnsec_set_default(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080020{
21 using namespace ndn;
22 namespace po = boost::program_options;
23
24 std::string certFileName;
Yingdi Yub61f5402014-02-26 17:46:11 -080025 bool isSetDefaultId = true;
26 bool isSetDefaultKey = false;
27 bool isSetDefaultCert = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080028 std::string name;
29
Yingdi Yub61f5402014-02-26 17:46:11 -080030 po::options_description description("General Usage\n ndnsec set-default [-h] [-k|c] name\nGeneral options");
31 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080032 ("help,h", "produce help message")
Yingdi Yub61f5402014-02-26 17:46:11 -080033 ("default_key,k", "set default key of the identity")
34 ("default_cert,c", "set default certificate of the key")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080035 ("name,n", po::value<std::string>(&name), "the name to set")
36 ;
37
38 po::positional_options_description p;
39 p.add("name", 1);
40 po::variables_map vm;
Yingdi Yub61f5402014-02-26 17:46:11 -080041 try
Yingdi Yu8d7468f2014-02-21 14:49:45 -080042 {
Yingdi Yub61f5402014-02-26 17:46:11 -080043 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
44 vm);
45 po::notify(vm);
46 }
47 catch (const std::exception& e)
48 {
49 std::cerr << "ERROR: " << e.what() << std::endl;
50 std::cerr << description << std::endl;
51 return 1;
52 }
53
54 if (vm.count("help") != 0)
55 {
56 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080057 return 0;
58 }
59
Yingdi Yub61f5402014-02-26 17:46:11 -080060 if (vm.count("name") == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080061 {
Yingdi Yub61f5402014-02-26 17:46:11 -080062 std::cerr << "ERROR: name is required!" << std::endl;
63 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080064 return 1;
65 }
66
Yingdi Yub61f5402014-02-26 17:46:11 -080067 KeyChain keyChain;
68
69 if (vm.count("default_key") != 0)
70 {
71 isSetDefaultKey = true;
72 isSetDefaultId = false;
73 }
74 else if (vm.count("default_cert") != 0)
75 {
76 isSetDefaultCert = true;
77 isSetDefaultId = false;
78 }
79
80 if (isSetDefaultId)
81 {
82 Name idName(name);
83 keyChain.setDefaultIdentity(idName);
84 return 0;
85 }
86 if (isSetDefaultKey)
87 {
88 Name keyName(name);
89 keyChain.setDefaultKeyNameForIdentity(keyName);
90 return 0;
91 }
92
93 if (isSetDefaultCert)
94 {
95 keyChain.setDefaultCertificateNameForKey(name);
96 return 0;
97 }
98
99 return 1;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800100}
101#endif //NDNSEC_SET_DEFAULT_HPP