pgbackrest.git / summary / log / commit / refs
commit f4f21d0df7217bcb583189d0fc4ecaac3faed146
Author: David Steele <david@pgmasters.net>
Date: Tue Sep 10 17:02:05 2019 +0000
Add groupIdFromName() and userIdFromName() to user module.
Update StorageWritePosix to use the new functions.
A side effect is that storageWritePosixOpen() will no longer error when the user/group name does not exist. It will simply retain the original user/group, i.e. the user that executed the restore.
In general this is a feature since completing a restore is more important than setting permissions exactly from the source host. However, some notification of this omission to the user would be beneficial.
src/Makefile.in | 2 +-
src/common/user.c | 38 +++++++++++++++++++++++++++++++++++++
src/common/user.h | 6 ++++++
src/storage/posix/write.c | 28 +++++++++------------------
test/src/module/common/userTest.c | 6 ++++++
test/src/module/storage/posixTest.c | 6 ------
6 files changed, 60 insertions(+), 26 deletions(-)
diff --git a/src/Makefile.in b/src/Makefile.in
index adf93de9d..6d395a217 100644
--- a/src/Makefile.in
+++ b/src/Makefile.in
@@ -570,7 +570,7 @@ storage/posix/read.o: storage/posix/read.c build.auto.h common/assert.h common/d
storage/posix/storage.o: storage/posix/storage.c build.auto.h common/assert.h common/debug.h common/error.auto.h common/error.h common/io/filter/filter.h common/io/filter/group.h common/io/read.h common/io/read.intern.h common/io/write.h common/io/write.intern.h common/log.h common/logLevel.h common/macro.h common/memContext.h common/object.h common/regExp.h common/stackTrace.h common/time.h common/type/buffer.h common/type/convert.h common/type/keyValue.h common/type/string.h common/type/stringList.h common/type/variant.h common/type/variantList.h common/user.h storage/info.h storage/posix/read.h storage/posix/storage.h storage/posix/storage.intern.h storage/posix/write.h storage/read.h storage/read.intern.h storage/storage.h storage/storage.intern.h storage/write.h storage/write.intern.h version.h
$(CC) $(CPPFLAGS) $(CFLAGS) $(CMAKE) -c storage/posix/storage.c -o storage/posix/storage.o
-storage/posix/write.o: storage/posix/write.c build.auto.h common/assert.h common/debug.h common/error.auto.h common/error.h common/io/filter/filter.h common/io/filter/group.h common/io/read.h common/io/read.intern.h common/io/write.h common/io/write.intern.h common/log.h common/logLevel.h common/macro.h common/memContext.h common/object.h common/stackTrace.h common/time.h common/type/buffer.h common/type/convert.h common/type/keyValue.h common/type/string.h common/type/stringList.h common/type/variant.h common/type/variantList.h storage/info.h storage/posix/storage.h storage/posix/storage.intern.h storage/posix/write.h storage/read.h storage/read.intern.h storage/storage.h storage/storage.intern.h storage/write.h storage/write.intern.h version.h
+storage/posix/write.o: storage/posix/write.c build.auto.h common/assert.h common/debug.h common/error.auto.h common/error.h common/io/filter/filter.h common/io/filter/group.h common/io/read.h common/io/read.intern.h common/io/write.h common/io/write.intern.h common/log.h common/logLevel.h common/macro.h common/memContext.h common/object.h common/stackTrace.h common/time.h common/type/buffer.h common/type/convert.h common/type/keyValue.h common/type/string.h common/type/stringList.h common/type/variant.h common/type/variantList.h common/user.h storage/info.h storage/posix/storage.h storage/posix/storage.intern.h storage/posix/write.h storage/read.h storage/read.intern.h storage/storage.h storage/storage.intern.h storage/write.h storage/write.intern.h version.h
$(CC) $(CPPFLAGS) $(CFLAGS) $(CMAKE) -c storage/posix/write.c -o storage/posix/write.o
storage/read.o: storage/read.c build.auto.h common/assert.h common/debug.h common/error.auto.h common/error.h common/io/filter/filter.h common/io/filter/group.h common/io/read.h common/io/read.intern.h common/log.h common/logLevel.h common/macro.h common/memContext.h common/object.h common/stackTrace.h common/type/buffer.h common/type/convert.h common/type/keyValue.h common/type/string.h common/type/variant.h common/type/variantList.h storage/read.h storage/read.intern.h
diff --git a/src/common/user.c b/src/common/user.c
index 8e02cb880..580123820 100644
--- a/src/common/user.c
+++ b/src/common/user.c
@@ -71,6 +71,25 @@ groupId(void)
FUNCTION_TEST_RETURN(userLocalData.groupId);
}
+/**********************************************************************************************************************************/
+gid_t
+groupIdFromName(const String *groupName)
+{
+ FUNCTION_TEST_BEGIN();
+ FUNCTION_TEST_PARAM(STRING, groupName);
+ FUNCTION_TEST_END();
+
+ if (groupName != NULL)
+ {
+ struct group *groupData = getgrnam(strPtr(groupName));
+
+ if (groupData != NULL)
+ FUNCTION_TEST_RETURN(groupData->gr_gid);
+ }
+
+ FUNCTION_TEST_RETURN((gid_t)-1);
+}
+
/**********************************************************************************************************************************/
const String *
groupName(void)
@@ -103,6 +122,25 @@ userId(void)
FUNCTION_TEST_RETURN(userLocalData.userId);
}
+/**********************************************************************************************************************************/
+uid_t
+userIdFromName(const String *userName)
+{
+ FUNCTION_TEST_BEGIN();
+ FUNCTION_TEST_PARAM(STRING, userName);
+ FUNCTION_TEST_END();
+
+ if (userName != NULL)
+ {
+ struct passwd *userData = getpwnam(strPtr(userName));
+
+ if (userData != NULL)
+ FUNCTION_TEST_RETURN(userData->pw_uid);
+ }
+
+ FUNCTION_TEST_RETURN((uid_t)-1);
+}
+
/**********************************************************************************************************************************/
const String *
userName(void)
diff --git a/src/common/user.h b/src/common/user.h
index 0c371f2e0..aa1436cfe 100644
--- a/src/common/user.h
+++ b/src/common/user.h
@@ -15,6 +15,9 @@ void userInit(void);
// Get the primary group id of the current user
gid_t groupId(void);
+// Get the id of the specified group. Returns (gid_t)-1 if not found.
+gid_t groupIdFromName(const String *groupName);
+
// Get the primary group name of the current user. Returns NULL if there is no mapping.
const String *groupName(void);
@@ -24,6 +27,9 @@ String *groupNameFromId(gid_t groupId);
// Get the id of the current user
uid_t userId(void);
+// Get the id of the specified user. Returns (uid_t)-1 if not found.
+uid_t userIdFromName(const String *userName);
+
// Get the name of the current user. Returns NULL if there is no mapping.
const String *userName(void);
diff --git a/src/storage/posix/write.c b/src/storage/posix/write.c
index 78328e165..f89d01f6d 100644
--- a/src/storage/posix/write.c
+++ b/src/storage/posix/write.c
@@ -4,8 +4,6 @@ Posix Storage File write
#include "build.auto.h"
#include <fcntl.h>
-#include <grp.h>
-#include <pwd.h>
#include <stdio.h>
#include <unistd.h>
#include <utime.h>
@@ -15,6 +13,7 @@ Posix Storage File write
#include "common/log.h"
#include "common/memContext.h"
#include "common/object.h"
+#include "common/user.h"
#include "storage/posix/storage.intern.h"
#include "storage/posix/write.h"
#include "storage/write.intern.h"
@@ -104,27 +103,18 @@ storageWritePosixOpen(THIS_VOID)
// Update user/group owner
if (this->interface.user != NULL || this->interface.group != NULL)
{
- struct passwd *userData = NULL;
- struct group *groupData = NULL;
+ uid_t updateUserId = userIdFromName(this->interface.user);
- if (this->interface.user != NULL)
- {
- THROW_ON_SYS_ERROR_FMT(
- (userData = getpwnam(strPtr(this->interface.user))) == NULL, UserMissingError, "unable to find user '%s'",
- strPtr(this->interface.user));
- }
+ if (updateUserId == userId())
+ updateUserId = (uid_t)-1;
- if (this->interface.group != NULL)
- {
- THROW_ON_SYS_ERROR_FMT(
- (groupData = getgrnam(strPtr(this->interface.group))) == NULL, GroupMissingError, "unable to find group '%s'",
- strPtr(this->interface.group));
- }
+ gid_t updateGroupId = groupIdFromName(this->interface.group);
+
+ if (updateGroupId == groupId())
+ updateGroupId = (gid_t)-1;
THROW_ON_SYS_ERROR_FMT(
- chown(
- strPtr(this->nameTmp), userData != NULL ? userData->pw_uid : (uid_t)-1,
- groupData != NULL ? groupData->gr_gid : (gid_t)-1) == -1,
+ chown(strPtr(this->nameTmp), updateUserId, updateGroupId) == -1,
FileOwnerError, "unable to set ownership for '%s'", strPtr(this->nameTmp));
}
diff --git a/test/src/module/common/userTest.c b/test/src/module/common/userTest.c
index d13dfd38b..7f0f90fcb 100644
--- a/test/src/module/common/userTest.c
+++ b/test/src/module/common/userTest.c
@@ -17,11 +17,17 @@ testRun(void)
TEST_RESULT_VOID(userInit(), "initialize info again");
TEST_RESULT_UINT(userId(), getuid(), "check user id");
+ TEST_RESULT_UINT(userIdFromName(userName()), userId(), "get user id");
+ TEST_RESULT_UINT(userIdFromName(NULL), (uid_t)-1, "get null user id");
+ TEST_RESULT_UINT(userIdFromName(STRDEF("bogus")), (uid_t)-1, "get bogus user id");
TEST_RESULT_STR_Z(userName(), testUser(), "check user name");
TEST_RESULT_STR_Z(userNameFromId(77777), NULL, "invalid user name by id");
TEST_RESULT_BOOL(userRoot(), false, "check user is root");
TEST_RESULT_UINT(groupId(), getgid(), "check group id");
+ TEST_RESULT_UINT(groupIdFromName(groupName()), groupId(), "get group id");
+ TEST_RESULT_UINT(groupIdFromName(NULL), (gid_t)-1, "get null group id");
+ TEST_RESULT_UINT(groupIdFromName(STRDEF("bogus")), (uid_t)-1, "get bogus group id");
TEST_RESULT_STR_Z(groupName(), testGroup(), "check name name");
TEST_RESULT_STR_Z(groupNameFromId(77777), NULL, "invalid group name by id");
}
diff --git a/test/src/module/storage/posixTest.c b/test/src/module/storage/posixTest.c
index c190cb34c..54a2889b5 100644
--- a/test/src/module/storage/posixTest.c
+++ b/test/src/module/storage/posixTest.c
@@ -639,12 +639,6 @@ testRun(void)
ioWriteOpen(storageWriteIo(file)), FileOpenError, STORAGE_ERROR_WRITE_OPEN ": [13] Permission denied",
strPtr(fileNoPerm));
- TEST_ASSIGN(file, storageNewWriteP(storageTest, fileName, .user = strNew("bogus")), "new write file (bogus user)");
- TEST_ERROR(ioWriteOpen(storageWriteIo(file)), UserMissingError, "unable to find user 'bogus'");
-
- TEST_ASSIGN(file, storageNewWriteP(storageTest, fileName, .group = strNew("bogus")), "new write file (bogus group)");
- TEST_ERROR(ioWriteOpen(storageWriteIo(file)), GroupMissingError, "unable to find group 'bogus'");
-
// -------------------------------------------------------------------------------------------------------------------------
TEST_ASSIGN(
file,
[parent: f8d057475934]