]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
- work on dbus interface
authorArvin Schnell <aschnell@suse.de>
Sat, 28 Jul 2012 16:06:22 +0000 (18:06 +0200)
committerArvin Schnell <aschnell@suse.de>
Sat, 28 Jul 2012 16:06:22 +0000 (18:06 +0200)
dbus/DBusConnection.cc
dbus/DBusConnection.h
server/MetaSnapper.cc

index 1d101c3bc744f35dcdad9438f595b5a89af37fa2..572cd483b1d9e74e22269999d29d52dd34591dc2 100644 (file)
@@ -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;
-    }
-
 }
index 80d6cac6b302530718543561ac58ef4369efc73f..aa2b2f4cd2c8f00f18059efee3cebbba1e9843a7 100644 (file)
@@ -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:
 
index 600ec7c83a087157d1841fb5f847e7eb001a4416..71f28713ea250abd36c1ddb45916646947d57b30 100644 (file)
@@ -22,6 +22,7 @@
 
 #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;
 }
 
 
@@ -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<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);
            }
        }
     }
@@ -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<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());
 }