36 #ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE 37 uid_t PrivilegeHelper::s_normalUid = ::geteuid();
38 gid_t PrivilegeHelper::s_normalGid = ::getegid();
40 uid_t PrivilegeHelper::s_privilegedUid = ::geteuid();
41 gid_t PrivilegeHelper::s_privilegedGid = ::getegid();
42 #endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE 47 #ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE 48 static const size_t MAX_GROUP_BUFFER_SIZE = 16384;
49 static const size_t MAX_PASSWD_BUFFER_SIZE = 16384;
51 static const size_t FALLBACK_GROUP_BUFFER_SIZE = 1024;
52 static const size_t FALLBACK_PASSWD_BUFFER_SIZE = 1024;
54 NFD_LOG_TRACE(
"initializing privilege helper with user \"" << userName <<
"\"" 55 <<
" group \"" << groupName <<
"\"");
59 if (!groupName.empty()) {
60 static long groupSize = ::sysconf(_SC_GETGR_R_SIZE_MAX);
63 groupSize = FALLBACK_GROUP_BUFFER_SIZE;
65 std::vector<char> groupBuffer(groupSize);
67 struct group* groupResult =
nullptr;
69 int errorCode = getgrnam_r(groupName.data(), &group,
70 &groupBuffer[0], groupBuffer.size(), &groupResult);
72 while (errorCode == ERANGE) {
73 if (groupBuffer.size() * 2 > MAX_GROUP_BUFFER_SIZE)
74 throw Error(
"Cannot allocate large enough buffer for struct group");
76 groupBuffer.resize(groupBuffer.size() * 2);
78 errorCode = getgrnam_r(groupName.data(), &group,
79 &groupBuffer[0], groupBuffer.size(), &groupResult);
82 if (errorCode != 0 || !groupResult)
83 throw Error(
"Failed to get gid for \"" + groupName +
"\"");
85 s_normalGid = group.gr_gid;
88 if (!userName.empty()) {
89 static long passwdSize = ::sysconf(_SC_GETPW_R_SIZE_MAX);
92 passwdSize = FALLBACK_PASSWD_BUFFER_SIZE;
94 std::vector<char> passwdBuffer(passwdSize);
96 struct passwd* passwdResult =
nullptr;
98 int errorCode = getpwnam_r(userName.data(), &passwd,
99 &passwdBuffer[0], passwdBuffer.size(), &passwdResult);
101 while (errorCode == ERANGE) {
102 if (passwdBuffer.size() * 2 > MAX_PASSWD_BUFFER_SIZE)
103 throw Error(
"Cannot allocate large enough buffer for struct passwd");
105 passwdBuffer.resize(passwdBuffer.size() * 2);
107 errorCode = getpwnam_r(userName.data(), &passwd,
108 &passwdBuffer[0], passwdBuffer.size(), &passwdResult);
111 if (errorCode != 0 || !passwdResult)
112 throw Error(
"Failed to get uid for \"" + userName +
"\"");
114 s_normalUid = passwd.pw_uid;
117 if (!userName.empty() || !groupName.empty()) {
118 throw Error(
"Dropping and raising privileges is not supported on this platform");
120 #endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE 126 #ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE 128 if (::setegid(s_normalGid) != 0)
129 throw Error(
"Failed to drop to effective gid=" +
to_string(s_normalGid));
132 if (::seteuid(s_normalUid) != 0)
133 throw Error(
"Failed to drop to effective uid=" +
to_string(s_normalUid));
135 NFD_LOG_INFO(
"dropped to effective uid=" << ::geteuid() <<
" gid=" << ::getegid());
137 NFD_LOG_WARN(
"Dropping privileges is not supported on this platform");
138 #endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE 142 PrivilegeHelper::raise()
144 #ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE 145 NFD_LOG_TRACE(
"elevating to effective uid=" << s_privilegedUid);
146 if (::seteuid(s_privilegedUid) != 0)
147 throw Error(
"Failed to elevate to effective uid=" +
to_string(s_privilegedUid));
149 NFD_LOG_TRACE(
"elevating to effective gid=" << s_privilegedGid);
150 if (::setegid(s_privilegedGid) != 0)
151 throw Error(
"Failed to elevate to effective gid=" +
to_string(s_privilegedGid));
153 NFD_LOG_INFO(
"elevated to effective uid=" << ::geteuid() <<
" gid=" << ::getegid());
155 NFD_LOG_WARN(
"Elevating privileges is not supported on this platform");
156 #endif // HAVE_PRIVILEGE_DROP_AND_ELEVATE
represents a serious seteuid/gid failure
#define NFD_LOG_INFO(expression)
#define NFD_LOG_TRACE(expression)
static void initialize(const std::string &userName, const std::string &groupName)
Copyright (c) 2011-2015 Regents of the University of California.
#define NFD_LOG_WARN(expression)
std::string to_string(const V &v)
#define NFD_LOG_INIT(name)