BlinkenArea - GitList
Repositories
Blog
Wiki
permissioner
Code
Commits
Branches
Tags
Search
Tree:
398a749
Branches
Tags
master
permissioner
libpermissioner
src
User.cpp
user/group lookup
Stefan Schuermans
commited
398a749
at 2020-08-23 09:21:48
User.cpp
Blame
History
Raw
#include <permissioner/User.h> #include <boost/optional.hpp> #include <cerrno> #include <cstring> #include <pwd.h> #include <sstream> #include <stdexcept> #include <string> #include <sys/types.h> #include <unistd.h> void User::parseUserName(std::string const &userNameStr) { if (userNameStr == "-") { userName = boost::none; uid = -1; return; } long size_max = sysconf(_SC_GETPW_R_SIZE_MAX); if (size_max <= 0) { std::stringstream msg; msg << "invalid maximum size of passwd entry structure " << size_max; throw std::runtime_error(msg.str()); } struct passwd pw_buf, *pw; char buf[size_max]; if (getpwnam_r(userNameStr.c_str(), &pw_buf, buf, sizeof(buf), &pw) != 0) { std::string err(strerror(errno)); std::stringstream msg; msg << "looking up user name \"" << userNameStr << "\" failed: " << err; throw std::runtime_error(msg.str()); } if (! pw) { std::stringstream msg; msg << "user name \"" << userNameStr << "\" not found"; throw std::runtime_error(msg.str()); } uid = pw->pw_uid; } boost::optional<std::string> const & User::getUserName() const { return userName; } int User::getUid() const { return uid; }