blob: ed6f99e86defc4b3dcea6df87d86d66f83cee28f [file] [log] [blame]
Steve DiBenedetto24b9a642014-04-07 15:45:39 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014 Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology
9 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
24
25#include "general-config-section.hpp"
26
27#include "common.hpp"
28#include "core/logger.hpp"
29#include "core/privilege-helper.hpp"
30#include "core/config-file.hpp"
31
32namespace nfd {
33
34namespace general {
35
36NFD_LOG_INIT("GeneralConfigSection");
37
38static void
39onConfig(const ConfigSection& configSection,
40 bool isDryRun,
41 const std::string& filename)
42{
43 // general
44 // {
45 // ; user "ndn-user"
46 // ; group "ndn-user"
47 // }
48
49 std::string user;
50 std::string group;
51
52 for (ConfigSection::const_iterator i = configSection.begin();
53 i != configSection.end();
54 ++i)
55 {
56 if (i->first == "user")
57 {
58 try
59 {
60 user = i->second.get_value<std::string>("user");
61
62 if (user.empty())
63 {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -070064 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for \"user\""
65 " in \"general\" section"));
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060066 }
67 }
68 catch (const boost::property_tree::ptree_error& error)
69 {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -070070 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for \"user\""
71 " in \"general\" section"));
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060072 }
73 }
74 else if (i->first == "group")
75 {
76 try
77 {
78 group = i->second.get_value<std::string>("group");
79
80 if (group.empty())
81 {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -070082 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for \"group\""
83 " in \"general\" section"));
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060084 }
85 }
86 catch (const boost::property_tree::ptree_error& error)
87 {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -070088 BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for \"group\""
89 " in \"general\" section"));
Steve DiBenedetto24b9a642014-04-07 15:45:39 -060090 }
91 }
92 }
93 NFD_LOG_TRACE("using user \"" << user << "\" group \"" << group << "\"");
94
95 PrivilegeHelper::initialize(user, group);
96}
97
98void
99setConfigFile(ConfigFile& configFile)
100{
101 configFile.addSectionHandler("general", &onConfig);
102}
103
Steve DiBenedetto24b9a642014-04-07 15:45:39 -0600104} // namespace general
105
106} // namespace nfd