2d4d15040014e6fbdd4e66c77fcd87b2534a3d8f
Stefan Schuermans user/group lookup

Stefan Schuermans authored 4 years ago

1) #include <permissioner/User.h>
2) 
3) #include <boost/optional.hpp>
4) #include <cerrno>
5) #include <cstring>
6) #include <pwd.h>
7) #include <sstream>
8) #include <stdexcept>
9) #include <string>
10) #include <sys/types.h>
11) #include <unistd.h>
12) 
13) void User::parseUserName(std::string const &userNameStr) {
14)   if (userNameStr == "-") {
15)     userName = boost::none;
16)     uid = -1;
17)     return;
18)   }
19) 
20)   long size_max = sysconf(_SC_GETPW_R_SIZE_MAX);
21)   if (size_max <= 0) {
22)     std::stringstream msg;
23)     msg << "invalid maximum size of passwd entry structure " << size_max;
24)     throw std::runtime_error(msg.str());
25)   }
26) 
27)   struct passwd pw_buf, *pw;
28)   char buf[size_max];
29)   if (getpwnam_r(userNameStr.c_str(), &pw_buf, buf, sizeof(buf), &pw) != 0) {
30)     std::string err(strerror(errno));
31)     std::stringstream msg;
32)     msg << "looking up user name \"" << userNameStr << "\" failed: " << err;
33)     throw std::runtime_error(msg.str());
34)   }
35)   if (! pw) {
36)     std::stringstream msg;
37)     msg << "user name \"" << userNameStr << "\" not found";
38)     throw std::runtime_error(msg.str());
39)   }
40) 
Stefan Schuermans complete config text, fix c...

Stefan Schuermans authored 4 years ago

41)   userName = pw->pw_name;