From: Arvin Schnell Date: Sat, 28 Jul 2012 16:06:22 +0000 (+0200) Subject: - work on dbus interface X-Git-Tag: v0.1.3~191 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=88d145c8ccef8113b5f9c1749bb421b5f856d085;p=thirdparty%2Fsnapper.git - work on dbus interface --- diff --git a/dbus/DBusConnection.cc b/dbus/DBusConnection.cc index 1d101c3b..572cd483 100644 --- a/dbus/DBusConnection.cc +++ b/dbus/DBusConnection.cc @@ -150,26 +150,4 @@ namespace DBus 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; - } - } diff --git a/dbus/DBusConnection.h b/dbus/DBusConnection.h index 80d6cac6..aa2b2f4c 100644 --- a/dbus/DBusConnection.h +++ b/dbus/DBusConnection.h @@ -55,7 +55,6 @@ namespace DBus void* user_data); unsigned long get_unix_userid(const Message& m); - static string get_unix_username(unsigned long userid); // TODO private: diff --git a/server/MetaSnapper.cc b/server/MetaSnapper.cc index 600ec7c8..71f28713 100644 --- a/server/MetaSnapper.cc +++ b/server/MetaSnapper.cc @@ -22,6 +22,7 @@ #include #include +#include #include #include @@ -29,28 +30,52 @@ #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& 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; } @@ -68,8 +93,9 @@ MetaSnapper::MetaSnapper(const ConfigInfo& config_info) boost::split(users, tmp, boost::is_any_of(" \t"), boost::token_compress_on); for (vector::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); } } } @@ -79,17 +105,21 @@ MetaSnapper::MetaSnapper(const ConfigInfo& config_info) { string tmp = pos2->second; boost::trim(tmp, locale::classic()); - - if (!tmp.empty()) - { + if (!tmp.empty()) + { vector groups; boost::split(groups, tmp, boost::is_any_of(" \t"), boost::token_compress_on); for (vector::const_iterator it = groups.begin(); it != groups.end(); ++it) { - + vector 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()); }