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 | |
| 11 | int |
| 12 | ndnsec_set_default(int argc, char** argv) |
| 13 | { |
| 14 | using namespace ndn; |
| 15 | namespace po = boost::program_options; |
| 16 | |
| 17 | std::string certFileName; |
| 18 | bool setDefaultId = true; |
| 19 | bool setDefaultKey = false; |
| 20 | bool setDefaultCert = false; |
| 21 | std::string name; |
| 22 | |
| 23 | po::options_description desc("General Usage\n ndnsec set-default [-h] [-K|C] name\nGeneral options"); |
| 24 | desc.add_options() |
| 25 | ("help,h", "produce help message") |
| 26 | ("default_key,K", "set default key of the identity") |
| 27 | ("default_cert,C", "set default certificate of the key") |
| 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; |
| 34 | po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm); |
| 35 | |
| 36 | po::notify(vm); |
| 37 | |
| 38 | if (vm.count("help")) |
| 39 | { |
| 40 | std::cerr << desc << std::endl; |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | try |
| 45 | { |
| 46 | KeyChain keyChain; |
| 47 | |
| 48 | if (vm.count("default_key")) |
| 49 | { |
| 50 | setDefaultKey = true; |
| 51 | setDefaultId = false; |
| 52 | } |
| 53 | else if(vm.count("default_cert")) |
| 54 | { |
| 55 | setDefaultCert = true; |
| 56 | setDefaultId = false; |
| 57 | } |
| 58 | |
| 59 | if (setDefaultId) |
| 60 | { |
| 61 | Name idName(name); |
| 62 | keyChain.setDefaultIdentity(idName); |
| 63 | return 0; |
| 64 | } |
| 65 | if (setDefaultKey) |
| 66 | { |
| 67 | Name keyName(name); |
| 68 | keyChain.setDefaultKeyNameForIdentity(keyName); |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | if (setDefaultCert) |
| 73 | { |
| 74 | keyChain.setDefaultCertificateNameForKey(name); |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | return 1; |
| 79 | |
| 80 | } |
| 81 | catch(SecPublicInfo::Error& e) |
| 82 | { |
| 83 | std::cerr << e.what() << std::endl; |
| 84 | return 1; |
| 85 | } |
| 86 | catch(SecTpm::Error& e) |
| 87 | { |
| 88 | std::cerr << e.what() << std::endl; |
| 89 | return 1; |
| 90 | } |
| 91 | |
| 92 | } |
| 93 | #endif //NDNSEC_SET_DEFAULT_HPP |