Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 1 | /* -*- 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 Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame^] | 11 | int |
| 12 | ndnsec_set_default(int argc, char** argv) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 13 | { |
| 14 | using namespace ndn; |
| 15 | namespace po = boost::program_options; |
| 16 | |
| 17 | std::string certFileName; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame^] | 18 | bool isSetDefaultId = true; |
| 19 | bool isSetDefaultKey = false; |
| 20 | bool isSetDefaultCert = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 21 | std::string name; |
| 22 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame^] | 23 | po::options_description description("General Usage\n ndnsec set-default [-h] [-k|c] name\nGeneral options"); |
| 24 | description.add_options() |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 25 | ("help,h", "produce help message") |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame^] | 26 | ("default_key,k", "set default key of the identity") |
| 27 | ("default_cert,c", "set default certificate of the key") |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 28 | ("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 Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame^] | 34 | try |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 35 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame^] | 36 | 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 50 | return 0; |
| 51 | } |
| 52 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame^] | 53 | if (vm.count("name") == 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 54 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame^] | 55 | std::cerr << "ERROR: name is required!" << std::endl; |
| 56 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 57 | return 1; |
| 58 | } |
| 59 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame^] | 60 | 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 93 | } |
| 94 | #endif //NDNSEC_SET_DEFAULT_HPP |