| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <stdlib.h> |
| 5 |
#include <sys/stat.h> |
| 6 |
#include <unistd.h> |
| 7 |
|
| 8 |
#include "acl-util.h" |
| 9 |
#include "errno-util.h" |
| 10 |
#include "fd-util.h" |
| 11 |
#include "format-util.h" |
| 12 |
#include "path-util.h" |
| 13 |
#include "string-util.h" |
| 14 |
#include "strv.h" |
| 15 |
#include "tests.h" |
| 16 |
#include "tmpfile-util.h" |
| 17 |
#include "user-util.h" |
| 18 |
|
| 19 |
TEST_RET(add_acls_for_user) { |
| 20 |
_cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-empty.XXXXXX"; |
| 21 |
_cleanup_close_ int fd = -EBADF; |
| 22 |
char *cmd; |
| 23 |
uid_t uid; |
| 24 |
int r; |
| 25 |
|
| 26 |
FOREACH_STRING(s, "capsh", "getfacl", "ls") { |
| 27 |
r = find_executable(s, NULL); |
| 28 |
if (r < 0) |
| 29 |
return log_tests_skipped_errno(r, "Could not find %s binary: %m", s); |
| 30 |
} |
| 31 |
|
| 32 |
ASSERT_OK(fd = mkostemp_safe(fn)); |
| 33 |
|
| 34 |
/* Use the mode that user journal files use */ |
| 35 |
ASSERT_OK_ZERO_ERRNO(fchmod(fd, 0640)); |
| 36 |
|
| 37 |
cmd = strjoina("ls -l ", fn); |
| 38 |
ASSERT_OK_ZERO_ERRNO(system(cmd)); |
| 39 |
|
| 40 |
cmd = strjoina("getfacl -p ", fn); |
| 41 |
ASSERT_OK_ZERO_ERRNO(system(cmd)); |
| 42 |
|
| 43 |
if (getuid() == 0 && !userns_has_single_user()) { |
| 44 |
r = get_user_creds(NOBODY_USER_NAME, /* flags= */ 0, NULL, &uid, NULL, NULL, NULL); |
| 45 |
if (r < 0) |
| 46 |
uid = 0; |
| 47 |
} else |
| 48 |
uid = getuid(); |
| 49 |
|
| 50 |
r = fd_add_uid_acl_permission(fd, uid, ACL_READ); |
| 51 |
if (ERRNO_IS_NOT_SUPPORTED(r)) |
| 52 |
return log_tests_skipped("no ACL support on /tmp"); |
| 53 |
|
| 54 |
log_info_errno(r, "fd_add_uid_acl_permission(%i, "UID_FMT", ACL_READ): %m", fd, uid); |
| 55 |
assert_se(r >= 0); |
| 56 |
|
| 57 |
cmd = strjoina("ls -l ", fn); |
| 58 |
ASSERT_OK_ZERO_ERRNO(system(cmd)); |
| 59 |
|
| 60 |
cmd = strjoina("getfacl -p ", fn); |
| 61 |
ASSERT_OK_ZERO_ERRNO(system(cmd)); |
| 62 |
|
| 63 |
/* set the acls again */ |
| 64 |
ASSERT_OK(fd_add_uid_acl_permission(fd, uid, ACL_READ)); |
| 65 |
|
| 66 |
cmd = strjoina("ls -l ", fn); |
| 67 |
ASSERT_OK_ZERO_ERRNO(system(cmd)); |
| 68 |
|
| 69 |
cmd = strjoina("getfacl -p ", fn); |
| 70 |
ASSERT_OK_ZERO_ERRNO(system(cmd)); |
| 71 |
|
| 72 |
return 0; |
| 73 |
} |
| 74 |
|
| 75 |
TEST_RET(fd_acl_make_read_only) { |
| 76 |
_cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-empty.XXXXXX"; |
| 77 |
_cleanup_close_ int fd = -EBADF; |
| 78 |
const char *cmd; |
| 79 |
struct stat st; |
| 80 |
int r; |
| 81 |
|
| 82 |
FOREACH_STRING(s, "capsh", "getfacl", "ls", "stat") { |
| 83 |
r = find_executable(s, NULL); |
| 84 |
if (r < 0) |
| 85 |
return log_tests_skipped_errno(r, "Could not find %s binary: %m", s); |
| 86 |
} |
| 87 |
|
| 88 |
ASSERT_OK(fd = mkostemp_safe(fn)); |
| 89 |
|
| 90 |
/* make it more exciting */ |
| 91 |
(void) fd_add_uid_acl_permission(fd, 1, ACL_READ|ACL_WRITE|ACL_EXECUTE); |
| 92 |
|
| 93 |
ASSERT_OK_ERRNO(fstat(fd, &st)); |
| 94 |
ASSERT_TRUE(FLAGS_SET(st.st_mode, 0200)); |
| 95 |
|
| 96 |
cmd = strjoina("getfacl -p ", fn); |
| 97 |
ASSERT_OK_ZERO_ERRNO(system(cmd)); |
| 98 |
|
| 99 |
cmd = strjoina("stat ", fn); |
| 100 |
ASSERT_OK_ZERO_ERRNO(system(cmd)); |
| 101 |
|
| 102 |
log_info("read-only"); |
| 103 |
ASSERT_OK_POSITIVE(fd_acl_make_read_only(fd)); |
| 104 |
|
| 105 |
ASSERT_OK_ERRNO(fstat(fd, &st)); |
| 106 |
ASSERT_EQ(st.st_mode & 0222, 0000u); |
| 107 |
|
| 108 |
cmd = strjoina("getfacl -p ", fn); |
| 109 |
ASSERT_OK_ZERO_ERRNO(system(cmd)); |
| 110 |
|
| 111 |
cmd = strjoina("stat ", fn); |
| 112 |
ASSERT_OK_ZERO_ERRNO(system(cmd)); |
| 113 |
|
| 114 |
log_info("writable"); |
| 115 |
ASSERT_OK_POSITIVE(fd_acl_make_writable(fd)); |
| 116 |
|
| 117 |
ASSERT_OK_ERRNO(fstat(fd, &st)); |
| 118 |
ASSERT_EQ(st.st_mode & 0222, 0200u); |
| 119 |
|
| 120 |
cmd = strjoina("getfacl -p ", fn); |
| 121 |
ASSERT_OK_ZERO_ERRNO(system(cmd)); |
| 122 |
|
| 123 |
cmd = strjoina("stat ", fn); |
| 124 |
ASSERT_OK_ZERO_ERRNO(system(cmd)); |
| 125 |
|
| 126 |
log_info("read-only"); |
| 127 |
ASSERT_OK_POSITIVE(fd_acl_make_read_only(fd)); |
| 128 |
|
| 129 |
ASSERT_OK_ERRNO(fstat(fd, &st)); |
| 130 |
ASSERT_EQ(st.st_mode & 0222, 0000u); |
| 131 |
|
| 132 |
cmd = strjoina("getfacl -p ", fn); |
| 133 |
ASSERT_OK_ZERO_ERRNO(system(cmd)); |
| 134 |
|
| 135 |
cmd = strjoina("stat ", fn); |
| 136 |
ASSERT_OK_ZERO_ERRNO(system(cmd)); |
| 137 |
|
| 138 |
return 0; |
| 139 |
} |
| 140 |
|
| 141 |
DEFINE_TEST_MAIN(LOG_INFO); |