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