]>
git.proxmox.com Git - systemd.git/blob - src/test/test-chown-rec.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "alloc-util.h"
7 #include "chown-recursive.h"
10 #include "string-util.h"
12 #include "tmpfile-util.h"
14 static const uint8_t acl
[] = {
15 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x07, 0x00,
16 0xff, 0xff, 0xff, 0xff, 0x02, 0x00, 0x07, 0x00,
17 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x07, 0x00,
18 0xff, 0xff, 0xff, 0xff, 0x10, 0x00, 0x07, 0x00,
19 0xff, 0xff, 0xff, 0xff, 0x20, 0x00, 0x05, 0x00,
20 0xff, 0xff, 0xff, 0xff,
23 static const uint8_t default_acl
[] = {
24 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x07, 0x00,
25 0xff, 0xff, 0xff, 0xff, 0x04, 0x00, 0x07, 0x00,
26 0xff, 0xff, 0xff, 0xff, 0x08, 0x00, 0x07, 0x00,
27 0x04, 0x00, 0x00, 0x00, 0x10, 0x00, 0x07, 0x00,
28 0xff, 0xff, 0xff, 0xff, 0x20, 0x00, 0x05, 0x00,
29 0xff, 0xff, 0xff, 0xff,
32 static bool has_xattr(const char *p
) {
33 char buffer
[sizeof(acl
) * 4];
35 if (lgetxattr(p
, "system.posix_acl_access", buffer
, sizeof(buffer
)) < 0) {
36 if (IN_SET(errno
, EOPNOTSUPP
, ENOTTY
, ENODATA
, ENOSYS
))
43 static void test_chown_recursive(void) {
44 _cleanup_(rm_rf_physical_and_freep
) char *t
= NULL
;
47 const uid_t uid
= getuid();
48 const gid_t gid
= getgid();
51 assert_se(mkdtemp_malloc(NULL
, &t
) >= 0);
53 p
= strjoina(t
, "/dir");
54 assert_se(mkdir(p
, 0777) >= 0);
55 assert_se(lstat(p
, &st
) >= 0);
56 assert_se(S_ISDIR(st
.st_mode
));
57 assert_se((st
.st_mode
& 07777) == 0755);
58 assert_se(st
.st_uid
== uid
);
59 assert_se(st
.st_gid
== gid
);
60 assert_se(!has_xattr(p
));
62 p
= strjoina(t
, "/dir/symlink");
63 assert_se(symlink("../../", p
) >= 0);
64 assert_se(lstat(p
, &st
) >= 0);
65 assert_se(S_ISLNK(st
.st_mode
));
66 assert_se((st
.st_mode
& 07777) == 0777);
67 assert_se(st
.st_uid
== uid
);
68 assert_se(st
.st_gid
== gid
);
69 assert_se(!has_xattr(p
));
71 p
= strjoina(t
, "/dir/reg");
72 assert_se(mknod(p
, S_IFREG
|0777, 0) >= 0);
73 assert_se(lstat(p
, &st
) >= 0);
74 assert_se(S_ISREG(st
.st_mode
));
75 assert_se((st
.st_mode
& 07777) == 0755);
76 assert_se(st
.st_uid
== uid
);
77 assert_se(st
.st_gid
== gid
);
78 assert_se(!has_xattr(p
));
80 p
= strjoina(t
, "/dir/sock");
81 assert_se(mknod(p
, S_IFSOCK
|0777, 0) >= 0);
82 assert_se(lstat(p
, &st
) >= 0);
83 assert_se(S_ISSOCK(st
.st_mode
));
84 assert_se((st
.st_mode
& 07777) == 0755);
85 assert_se(st
.st_uid
== uid
);
86 assert_se(st
.st_gid
== gid
);
87 assert_se(!has_xattr(p
));
89 p
= strjoina(t
, "/dir/fifo");
90 assert_se(mknod(p
, S_IFIFO
|0777, 0) >= 0);
91 assert_se(lstat(p
, &st
) >= 0);
92 assert_se(S_ISFIFO(st
.st_mode
));
93 assert_se((st
.st_mode
& 07777) == 0755);
94 assert_se(st
.st_uid
== uid
);
95 assert_se(st
.st_gid
== gid
);
96 assert_se(!has_xattr(p
));
98 /* We now apply an xattr to the dir, and check it again */
99 p
= strjoina(t
, "/dir");
100 assert_se(setxattr(p
, "system.posix_acl_access", acl
, sizeof(acl
), 0) >= 0);
101 assert_se(setxattr(p
, "system.posix_acl_default", default_acl
, sizeof(default_acl
), 0) >= 0);
102 assert_se(lstat(p
, &st
) >= 0);
103 assert_se(S_ISDIR(st
.st_mode
));
104 assert_se((st
.st_mode
& 07777) == 0775); /* acl change changed the mode too */
105 assert_se(st
.st_uid
== uid
);
106 assert_se(st
.st_gid
== gid
);
107 assert_se(has_xattr(p
));
109 assert_se(path_chown_recursive(t
, 1, 2, 07777) >= 0);
111 p
= strjoina(t
, "/dir");
112 assert_se(lstat(p
, &st
) >= 0);
113 assert_se(S_ISDIR(st
.st_mode
));
114 assert_se((st
.st_mode
& 07777) == 0775);
115 assert_se(st
.st_uid
== 1);
116 assert_se(st
.st_gid
== 2);
117 assert_se(!has_xattr(p
));
119 p
= strjoina(t
, "/dir/symlink");
120 assert_se(lstat(p
, &st
) >= 0);
121 assert_se(S_ISLNK(st
.st_mode
));
122 assert_se((st
.st_mode
& 07777) == 0777);
123 assert_se(st
.st_uid
== 1);
124 assert_se(st
.st_gid
== 2);
125 assert_se(!has_xattr(p
));
127 p
= strjoina(t
, "/dir/reg");
128 assert_se(lstat(p
, &st
) >= 0);
129 assert_se(S_ISREG(st
.st_mode
));
130 assert_se((st
.st_mode
& 07777) == 0755);
131 assert_se(st
.st_uid
== 1);
132 assert_se(st
.st_gid
== 2);
133 assert_se(!has_xattr(p
));
135 p
= strjoina(t
, "/dir/sock");
136 assert_se(lstat(p
, &st
) >= 0);
137 assert_se(S_ISSOCK(st
.st_mode
));
138 assert_se((st
.st_mode
& 07777) == 0755);
139 assert_se(st
.st_uid
== 1);
140 assert_se(st
.st_gid
== 2);
141 assert_se(!has_xattr(p
));
143 p
= strjoina(t
, "/dir/fifo");
144 assert_se(lstat(p
, &st
) >= 0);
145 assert_se(S_ISFIFO(st
.st_mode
));
146 assert_se((st
.st_mode
& 07777) == 0755);
147 assert_se(st
.st_uid
== 1);
148 assert_se(st
.st_gid
== 2);
149 assert_se(!has_xattr(p
));
152 int main(int argc
, char *argv
[]) {
153 test_setup_logging(LOG_DEBUG
);
156 return log_tests_skipped("not running as root");
158 test_chown_recursive();