10 #include <pion/admin_rights.hpp>
13 #include <sys/types.h>
15 #include <sys/types.h>
16 #include <boost/regex.hpp>
17 #include <boost/tokenizer.hpp>
18 #include <boost/lexical_cast.hpp>
28 const boost::int16_t admin_rights::ADMIN_USER_ID = 0;
29 boost::mutex admin_rights::m_mutex;
37 : m_logger(PION_GET_LOGGER(
"pion.admin_rights")),
38 m_lock(m_mutex), m_user_id(-1), m_has_rights(false), m_use_log(use_log)
54 long admin_rights::find_system_id(
const std::string& name,
55 const std::string& file)
60 #else // NOT #ifdef _MSC_VER
63 : m_logger(PION_GET_LOGGER(
"pion.admin_rights")),
64 m_lock(m_mutex), m_user_id(-1), m_has_rights(false), m_use_log(use_log)
66 m_user_id = geteuid();
67 if ( seteuid(ADMIN_USER_ID) != 0 ) {
69 PION_LOG_ERROR(m_logger,
"Unable to upgrade to administrative rights");
75 PION_LOG_DEBUG(m_logger,
"Upgraded to administrative rights");
82 if ( seteuid(m_user_id) == 0 ) {
84 PION_LOG_DEBUG(m_logger,
"Released administrative rights");
87 PION_LOG_ERROR(m_logger,
"Unable to release administrative rights");
96 long user_id = find_system_id(user_name,
"/etc/passwd");
98 if ( seteuid(user_id) != 0 )
108 long group_id = find_system_id(group_name,
"/etc/group");
109 if (group_id != -1) {
110 if ( setegid(group_id) != 0 )
113 group_id = getegid();
118 long admin_rights::find_system_id(
const std::string& name,
119 const std::string& file)
122 const boost::regex just_numbers(
"\\d+");
123 if (boost::regex_match(name, just_numbers)) {
124 return boost::lexical_cast<boost::int32_t>(name);
128 std::ifstream system_file(file.c_str());
129 if (! system_file.is_open()) {
134 typedef boost::tokenizer<boost::char_separator<char> > Tok;
135 boost::char_separator<char> sep(
":");
137 boost::int32_t system_id = -1;
139 while (std::getline(system_file, line,
'\n')) {
140 Tok tokens(line, sep);
141 Tok::const_iterator token_it = tokens.begin();
142 if (token_it != tokens.end() && *token_it == name) {
144 if (++token_it != tokens.end() && ++token_it != tokens.end()
145 && boost::regex_match(*token_it, just_numbers))
148 system_id = boost::lexical_cast<boost::int32_t>(*token_it);
157 #endif // #ifdef _MSC_VER
void release(void)
releases administrative rights
static long run_as_group(const std::string &group_name)
calculates the group id based upon the group configuration parameter
admin_rights(bool use_log=true)
static long run_as_user(const std::string &user_name)
calculates the user id based upon the user configuration parameter