Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #ifndef NDNSEC_SET_DEFAULT_HPP |
| 25 | #define NDNSEC_SET_DEFAULT_HPP |
| 26 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 27 | int |
| 28 | ndnsec_set_default(int argc, char** argv) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 29 | { |
| 30 | using namespace ndn; |
| 31 | namespace po = boost::program_options; |
| 32 | |
| 33 | std::string certFileName; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 34 | bool isSetDefaultId = true; |
| 35 | bool isSetDefaultKey = false; |
| 36 | bool isSetDefaultCert = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 37 | std::string name; |
| 38 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 39 | po::options_description description("General Usage\n ndnsec set-default [-h] [-k|c] name\nGeneral options"); |
| 40 | description.add_options() |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 41 | ("help,h", "produce help message") |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 42 | ("default_key,k", "set default key of the identity") |
| 43 | ("default_cert,c", "set default certificate of the key") |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 44 | ("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 Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 50 | try |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 51 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 52 | 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 66 | return 0; |
| 67 | } |
| 68 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 69 | if (vm.count("name") == 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 70 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 71 | std::cerr << "ERROR: name is required!" << std::endl; |
| 72 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 73 | return 1; |
| 74 | } |
| 75 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 76 | 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 109 | } |
| 110 | #endif //NDNSEC_SET_DEFAULT_HPP |