]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-acl-util.c
license: LGPL-2.1+ -> LGPL-2.1-or-later
[thirdparty/systemd.git] / src / test / test-acl-util.c
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 "string-util.h"
13 #include "tests.h"
14 #include "tmpfile-util.h"
15 #include "user-util.h"
16
17 static int test_add_acls_for_user(void) {
18 char fn[] = "/tmp/test-empty.XXXXXX";
19 _cleanup_close_ int fd = -1;
20 char *cmd;
21 uid_t uid;
22 int r;
23
24 log_info("/* %s */", __func__);
25
26 fd = mkostemp_safe(fn);
27 assert_se(fd >= 0);
28
29 /* Use the mode that user journal files use */
30 assert_se(fchmod(fd, 0640) == 0);
31
32 cmd = strjoina("ls -l ", fn);
33 assert_se(system(cmd) == 0);
34
35 cmd = strjoina("getfacl -p ", fn);
36 assert_se(system(cmd) == 0);
37
38 if (getuid() == 0) {
39 const char *nobody = NOBODY_USER_NAME;
40 r = get_user_creds(&nobody, &uid, NULL, NULL, NULL, 0);
41 if (r < 0)
42 uid = 0;
43 } else
44 uid = getuid();
45
46 r = fd_add_uid_acl_permission(fd, uid, ACL_READ);
47 if (ERRNO_IS_NOT_SUPPORTED(r))
48 return log_tests_skipped("no ACL support on /tmp");
49
50 log_info_errno(r, "fd_add_uid_acl_permission(%i, "UID_FMT", ACL_READ): %m", fd, uid);
51 assert_se(r >= 0);
52
53 cmd = strjoina("ls -l ", fn);
54 assert_se(system(cmd) == 0);
55
56 cmd = strjoina("getfacl -p ", fn);
57 assert_se(system(cmd) == 0);
58
59 /* set the acls again */
60
61 r = fd_add_uid_acl_permission(fd, uid, ACL_READ);
62 assert_se(r >= 0);
63
64 cmd = strjoina("ls -l ", fn);
65 assert_se(system(cmd) == 0);
66
67 cmd = strjoina("getfacl -p ", fn);
68 assert_se(system(cmd) == 0);
69
70 (void) unlink(fn);
71 return 0;
72 }
73
74 int main(int argc, char **argv) {
75 return test_add_acls_for_user();
76 }