blob: 92bd53da3f02b33ecb35bf0409736188bdd1a3c5 [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#ifndef NFD_CORE_PRIVILEGE_HELPER_HPP
26#define NFD_CORE_PRIVILEGE_HELPER_HPP
27
28#include "common.hpp"
29
30#include <unistd.h>
31
32namespace nfd {
33
34class PrivilegeHelper
35{
36public:
37
38 /// \brief PrivilegeHelper::Error represents a serious seteuid/gid failure and
39 /// should only be caught by main in as part of a graceful program termination.
40 class Error
41 {
42 public:
43 explicit
44 Error(const std::string& what)
45 : m_whatMessage(what)
46 {
47 }
48
49 const char*
50 what() const
51 {
52 return m_whatMessage.c_str();
53 }
54
55 private:
56 const std::string m_whatMessage;
57 };
58
59 static void
60 initialize(const std::string& userName, const std::string& groupName);
61
62 static void
63 drop();
64
65 static void
66 runElevated(function<void()> f);
67
68private:
69
70 static void
71 raise();
72
73private:
74
75 static uid_t s_normalUid;
76 static gid_t s_normalGid;
77
78 static uid_t s_privilegedUid;
79 static gid_t s_privilegedGid;
80};
81
82} // namespace nfd
83
84#endif // NFD_CORE_PRIVILEGE_HELPER_HPP