]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
- handle case where _SC_GETPW_R_SIZE_MAX hint is too small 574/head
authorArvin Schnell <aschnell@suse.de>
Tue, 29 Sep 2020 07:42:17 +0000 (09:42 +0200)
committerArvin Schnell <aschnell@suse.de>
Tue, 29 Sep 2020 07:42:17 +0000 (09:42 +0200)
snapper/AppUtil.cc
snapper/AppUtil.h
snapper/Logger.cc
testsuite-real/ug-tests.cc

index 3c505a86e275cf0ab6555eb179abbc9562310789..8e80e7191ac929a099516e50f231c72abc032c62 100644 (file)
@@ -324,6 +324,29 @@ namespace snapper
     }
 
 
+    bool
+    get_uid_dir(uid_t uid, string& dir)
+    {
+       struct passwd pwd;
+       struct passwd* result;
+
+       vector<char> buf(sysconf(_SC_GETPW_R_SIZE_MAX, 1024));
+
+       int e;
+       while ((e = getpwuid_r(uid, &pwd, buf.data(), buf.size(), &result)) == ERANGE)
+           buf.resize(2 * buf.size());
+
+       if (e != 0 || result == NULL)
+           return false;
+
+       memset(pwd.pw_passwd, 0, strlen(pwd.pw_passwd));
+
+       dir = pwd.pw_dir;
+
+       return true;
+    }
+
+
     bool
     get_user_uid(const char* username, uid_t& uid)
     {
index b9858cfa50b6b71b37205c985e83872ecdc9df7f..0f070c44bdcc18a6a38c12cfa96db93b72808bbe 100644 (file)
@@ -90,9 +90,27 @@ namespace snapper
     string datetime(time_t time, bool utc, bool classic);
     time_t scan_datetime(const string& str, bool utc);
 
+    /**
+     * For the user with uid get the username (passwd.pw_name) and the gid
+     * (passwd.pw_gid).
+     */
     bool get_uid_username_gid(uid_t uid, string& username, gid_t& gid);
+
+    /**
+     * For the user with uid get the home directory (passwd.pw_dir).
+     */
+    bool get_uid_dir(uid_t uid, string& dir);
+
+    /*
+     * For the user with username get the uid (passwd.pw_uid).
+     */
     bool get_user_uid(const char* username, uid_t& uid);
+
+    /*
+     * For the group with groupname get the gid (group.gr_gid).
+     */
     bool get_group_gid(const char* groupname, gid_t& gid);
+
     vector<gid_t> getgrouplist(const char* username, gid_t gid);
 
 
index d778ebae2bb975ceaa56ce00ff1a157e76198140..a21d730366f8b1a0578be56c892a38784b7f5e88 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Copyright (c) [2004-2013] Novell, Inc.
- * Copyright (c) 2018 SUSE LLC
+ * Copyright (c) [2018-2020] SUSE LLC
  *
  * All Rights Reserved.
  *
@@ -158,17 +158,11 @@ namespace snapper
 
        if (geteuid())
        {
-           struct passwd pwd;
-           struct passwd* result;
+           string dir;
 
-           long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
-           char buf[bufsize];
-
-           if (getpwuid_r(geteuid(), &pwd, buf, bufsize, &result) == 0 && result == &pwd)
+           if (get_uid_dir(geteuid(), dir))
            {
-               memset(pwd.pw_passwd, 0, strlen(pwd.pw_passwd));
-
-               logger_data->filename = string(pwd.pw_dir) + "/.snapper.log";
+               logger_data->filename = dir + "/.snapper.log";
            }
        }
 
index f5af1b436ccb835f0b67168da824e24f074fd4e3..832dc558c0d3428bd93e77500b9cb28dc619458a 100644 (file)
@@ -23,6 +23,11 @@ test1()
     cout << "username:" << username << endl;
     cout << "gid:" << gid << endl;
 
+    string dir;
+    if (!get_uid_dir(uid, dir))
+       cerr << "failed to get dir" << endl;
+    cout << "dir:" << dir << endl;
+
     vector<gid_t> gids = getgrouplist(username.c_str(), gid);
     cout << "gids:";
     for (gid_t gid : gids)