Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [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 | /** |
| 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #ifndef NDNSEC_SET_DEFAULT_HPP |
| 16 | #define NDNSEC_SET_DEFAULT_HPP |
| 17 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 18 | int |
| 19 | ndnsec_set_default(int argc, char** argv) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 20 | { |
| 21 | using namespace ndn; |
| 22 | namespace po = boost::program_options; |
| 23 | |
| 24 | std::string certFileName; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 25 | bool isSetDefaultId = true; |
| 26 | bool isSetDefaultKey = false; |
| 27 | bool isSetDefaultCert = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 28 | std::string name; |
| 29 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 30 | po::options_description description("General Usage\n ndnsec set-default [-h] [-k|c] name\nGeneral options"); |
| 31 | description.add_options() |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 32 | ("help,h", "produce help message") |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 33 | ("default_key,k", "set default key of the identity") |
| 34 | ("default_cert,c", "set default certificate of the key") |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 35 | ("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 Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 41 | try |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 42 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 43 | 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 60 | if (vm.count("name") == 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 61 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 62 | std::cerr << "ERROR: name is required!" << std::endl; |
| 63 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 64 | return 1; |
| 65 | } |
| 66 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 67 | 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 100 | } |
| 101 | #endif //NDNSEC_SET_DEFAULT_HPP |