return uid;
}
-
- string
- Connection::get_unix_username(unsigned long userid)
- {
- long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
- char* buf = (char*) malloc(bufsize);
- if (!buf)
- throw FatalException();
-
- struct passwd pwd;
- struct passwd *result;
- getpwuid_r(userid, &pwd, buf, bufsize, &result);
- if (!result)
- throw FatalException();
-
- string username(pwd.pw_name);
-
- free(buf);
-
- return username;
- }
-
}
#include <sys/types.h>
#include <pwd.h>
+#include <grp.h>
#include <boost/algorithm/string.hpp>
#include <snapper/Log.h>
#include "MetaSnapper.h"
-uid_t
-get_uid(const string& username)
+bool
+get_user_uid(const char* username, uid_t& uid)
{
- long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
- char* buf = (char*) malloc(bufsize);
- if (!buf)
- throw;
-
struct passwd pwd;
struct passwd* result;
- if (getpwnam_r(username.c_str(), &pwd, buf, bufsize, &result) != 0)
- throw;
+ long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
+ char buf[bufsize];
+
+ if (getpwnam_r(username, &pwd, buf, bufsize, &result) != 0 || result != &pwd)
+ {
+ y2war("couldn't find username '" << username << "'");
+ return false;
+ }
+
+ uid = pwd.pw_uid;
+
+ return true;
+}
+
+
+bool
+get_group_uids(const char* groupname, vector<uid_t>& uids)
+{
+ struct group grp;
+ struct group* result;
+
+ long bufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
+ char buf[bufsize];
- if (!result)
- throw;
+ if (getgrnam_r(groupname, &grp, buf, bufsize, &result) != 0 || result != &grp)
+ {
+ y2war("couldn't find groupname '" << groupname << "'");
+ return false;
+ }
- uid_t r = pwd.pw_uid;
+ uids.clear();
- free(buf);
+ for (char** p = grp.gr_mem; *p != NULL; ++p)
+ {
+ uid_t uid;
+ if (get_user_uid(*p, uid))
+ uids.push_back(uid);
+ }
- return r;
+ return true;
}
boost::split(users, tmp, boost::is_any_of(" \t"), boost::token_compress_on);
for (vector<string>::const_iterator it = users.begin(); it != users.end(); ++it)
{
- uid_t uid = get_uid(*it);
- uids.push_back(uid);
+ uid_t tmp;
+ if (get_user_uid(it->c_str(), tmp))
+ uids.push_back(tmp);
}
}
}
{
string tmp = pos2->second;
boost::trim(tmp, locale::classic());
-
- if (!tmp.empty())
- {
+ if (!tmp.empty())
+ {
vector<string> groups;
boost::split(groups, tmp, boost::is_any_of(" \t"), boost::token_compress_on);
for (vector<string>::const_iterator it = groups.begin(); it != groups.end(); ++it)
{
-
+ vector<uid_t> tmp;
+ if (get_group_uids(it->c_str(), tmp))
+ uids.insert(uids.end(), tmp.begin(), tmp.end());
}
}
}
+
+ sort(uids.begin(), uids.end());
+ uids.erase(unique(uids.begin(), uids.end()), uids.end());
}