]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
5eda94dd | 2 | |
5eda94dd | 3 | #include <errno.h> |
5eda94dd | 4 | |
4f209af7 YW |
5 | #include "sd-device.h" |
6 | ||
79c07722 | 7 | #include "acl-util.h" |
b5efdb8a | 8 | #include "alloc-util.h" |
8437c059 | 9 | #include "device-util.h" |
0f46548d | 10 | #include "devnode-acl.h" |
a0956174 | 11 | #include "dirent-util.h" |
3ffd4af2 | 12 | #include "fd-util.h" |
f97b34a6 | 13 | #include "format-util.h" |
e992fd18 | 14 | #include "fs-util.h" |
e2341b6b | 15 | #include "glyph-util.h" |
6b78df0a | 16 | #include "set.h" |
07630cea | 17 | #include "string-util.h" |
5eda94dd LP |
18 | |
19 | static int flush_acl(acl_t acl) { | |
20 | acl_entry_t i; | |
21 | int found; | |
22 | bool changed = false; | |
23 | ||
24 | assert(acl); | |
25 | ||
26 | for (found = acl_get_entry(acl, ACL_FIRST_ENTRY, &i); | |
27 | found > 0; | |
28 | found = acl_get_entry(acl, ACL_NEXT_ENTRY, &i)) { | |
29 | ||
30 | acl_tag_t tag; | |
31 | ||
32 | if (acl_get_tag_type(i, &tag) < 0) | |
33 | return -errno; | |
34 | ||
35 | if (tag != ACL_USER) | |
36 | continue; | |
37 | ||
38 | if (acl_delete_entry(acl, i) < 0) | |
39 | return -errno; | |
40 | ||
41 | changed = true; | |
42 | } | |
43 | ||
44 | if (found < 0) | |
45 | return -errno; | |
46 | ||
47 | return changed; | |
48 | } | |
49 | ||
50 | int devnode_acl(const char *path, | |
51 | bool flush, | |
52 | bool del, uid_t old_uid, | |
53 | bool add, uid_t new_uid) { | |
54 | ||
203ea2c8 DT |
55 | _cleanup_(acl_freep) acl_t acl = NULL; |
56 | int r; | |
5eda94dd LP |
57 | bool changed = false; |
58 | ||
59 | assert(path); | |
60 | ||
61 | acl = acl_get_file(path, ACL_TYPE_ACCESS); | |
62 | if (!acl) | |
63 | return -errno; | |
64 | ||
65 | if (flush) { | |
66 | ||
67 | r = flush_acl(acl); | |
68 | if (r < 0) | |
203ea2c8 | 69 | return r; |
5eda94dd LP |
70 | if (r > 0) |
71 | changed = true; | |
72 | ||
73 | } else if (del && old_uid > 0) { | |
74 | acl_entry_t entry; | |
75 | ||
f4b47811 | 76 | r = acl_find_uid(acl, old_uid, &entry); |
5eda94dd | 77 | if (r < 0) |
203ea2c8 | 78 | return r; |
5eda94dd LP |
79 | |
80 | if (r > 0) { | |
203ea2c8 DT |
81 | if (acl_delete_entry(acl, entry) < 0) |
82 | return -errno; | |
5eda94dd LP |
83 | |
84 | changed = true; | |
85 | } | |
86 | } | |
87 | ||
88 | if (add && new_uid > 0) { | |
89 | acl_entry_t entry; | |
90 | acl_permset_t permset; | |
91 | int rd, wt; | |
92 | ||
f4b47811 | 93 | r = acl_find_uid(acl, new_uid, &entry); |
5eda94dd | 94 | if (r < 0) |
203ea2c8 | 95 | return r; |
5eda94dd LP |
96 | |
97 | if (r == 0) { | |
203ea2c8 DT |
98 | if (acl_create_entry(&acl, &entry) < 0) |
99 | return -errno; | |
5eda94dd LP |
100 | |
101 | if (acl_set_tag_type(entry, ACL_USER) < 0 || | |
203ea2c8 DT |
102 | acl_set_qualifier(entry, &new_uid) < 0) |
103 | return -errno; | |
5eda94dd LP |
104 | } |
105 | ||
203ea2c8 DT |
106 | if (acl_get_permset(entry, &permset) < 0) |
107 | return -errno; | |
5eda94dd LP |
108 | |
109 | rd = acl_get_perm(permset, ACL_READ); | |
203ea2c8 DT |
110 | if (rd < 0) |
111 | return -errno; | |
5eda94dd LP |
112 | |
113 | wt = acl_get_perm(permset, ACL_WRITE); | |
203ea2c8 DT |
114 | if (wt < 0) |
115 | return -errno; | |
5eda94dd LP |
116 | |
117 | if (!rd || !wt) { | |
118 | ||
203ea2c8 DT |
119 | if (acl_add_perm(permset, ACL_READ|ACL_WRITE) < 0) |
120 | return -errno; | |
5eda94dd LP |
121 | |
122 | changed = true; | |
123 | } | |
124 | } | |
125 | ||
126 | if (!changed) | |
203ea2c8 | 127 | return 0; |
5eda94dd | 128 | |
203ea2c8 DT |
129 | if (acl_calc_mask(&acl) < 0) |
130 | return -errno; | |
5eda94dd | 131 | |
203ea2c8 DT |
132 | if (acl_set_file(path, ACL_TYPE_ACCESS, acl) < 0) |
133 | return -errno; | |
5eda94dd | 134 | |
203ea2c8 | 135 | return 0; |
5eda94dd LP |
136 | } |
137 | ||
4f209af7 | 138 | int devnode_acl_all(const char *seat, |
5eda94dd LP |
139 | bool flush, |
140 | bool del, uid_t old_uid, | |
141 | bool add, uid_t new_uid) { | |
142 | ||
4f209af7 | 143 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
c07eb49a | 144 | _cleanup_set_free_ Set *nodes = NULL; |
db0c1e3b | 145 | _cleanup_closedir_ DIR *dir = NULL; |
bf5332d2 | 146 | char *n; |
5eda94dd LP |
147 | int r; |
148 | ||
4f209af7 YW |
149 | r = sd_device_enumerator_new(&e); |
150 | if (r < 0) | |
151 | return r; | |
6b78df0a TG |
152 | |
153 | if (isempty(seat)) | |
154 | seat = "seat0"; | |
5eda94dd | 155 | |
7b3afbac LP |
156 | /* We can only match by one tag in libudev. We choose |
157 | * "uaccess" for that. If we could match for two tags here we | |
158 | * could add the seat name as second match tag, but this would | |
159 | * be hardly optimizable in libudev, and hence checking the | |
160 | * second tag manually in our loop is a good solution. */ | |
4f209af7 | 161 | r = sd_device_enumerator_add_match_tag(e, "uaccess"); |
5eda94dd | 162 | if (r < 0) |
bf5332d2 | 163 | return r; |
5eda94dd | 164 | |
8437c059 | 165 | FOREACH_DEVICE(e, d) { |
5eda94dd LP |
166 | const char *node, *sn; |
167 | ||
fccb48b2 LP |
168 | /* Make sure the tag is still in place */ |
169 | if (sd_device_has_current_tag(d, "uaccess") <= 0) | |
170 | continue; | |
171 | ||
4f209af7 | 172 | if (sd_device_get_property_value(d, "ID_SEAT", &sn) < 0 || isempty(sn)) |
5eda94dd LP |
173 | sn = "seat0"; |
174 | ||
bf5332d2 | 175 | if (!streq(seat, sn)) |
5eda94dd | 176 | continue; |
5eda94dd | 177 | |
bf5332d2 | 178 | /* In case people mistag devices with nodes, we need to ignore this */ |
4f209af7 | 179 | if (sd_device_get_devname(d, &node) < 0) |
d2d4b038 | 180 | continue; |
5eda94dd | 181 | |
76386309 | 182 | log_device_debug(d, "Found udev node %s for seat %s", node, seat); |
c07eb49a | 183 | r = set_put_strdup_full(&nodes, &path_hash_ops_free, node); |
5eda94dd | 184 | if (r < 0) |
bf5332d2 | 185 | return r; |
5eda94dd LP |
186 | } |
187 | ||
6b78df0a TG |
188 | /* udev exports "dead" device nodes to allow module on-demand loading, |
189 | * these devices are not known to the kernel at this moment */ | |
190 | dir = opendir("/run/udev/static_node-tags/uaccess"); | |
191 | if (dir) { | |
af3b864d | 192 | FOREACH_DIRENT(de, dir, return -errno) { |
e992fd18 YW |
193 | r = readlinkat_malloc(dirfd(dir), de->d_name, &n); |
194 | if (r == -ENOENT) | |
195 | continue; | |
196 | if (r < 0) { | |
197 | log_debug_errno(r, | |
198 | "Unable to read symlink '/run/udev/static_node-tags/uaccess/%s', ignoring: %m", | |
199 | de->d_name); | |
200 | continue; | |
201 | } | |
6b78df0a TG |
202 | |
203 | log_debug("Found static node %s for seat %s", n, seat); | |
c07eb49a | 204 | r = set_ensure_consume(&nodes, &path_hash_ops_free, n); |
bf5332d2 LP |
205 | if (r < 0) |
206 | return r; | |
6b78df0a | 207 | } |
6b78df0a TG |
208 | } |
209 | ||
bf5332d2 | 210 | r = 0; |
90e74a66 | 211 | SET_FOREACH(n, nodes) { |
bf5332d2 LP |
212 | int k; |
213 | ||
e2341b6b DT |
214 | log_debug("Changing ACLs at %s for seat %s (uid "UID_FMT"%s"UID_FMT"%s%s)", |
215 | n, seat, old_uid, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), new_uid, | |
6b9732b2 ZJS |
216 | del ? " del" : "", add ? " add" : ""); |
217 | ||
bf5332d2 | 218 | k = devnode_acl(n, flush, del, old_uid, add, new_uid); |
8016b904 DH |
219 | if (k == -ENOENT) |
220 | log_debug("Device %s disappeared while setting ACLs", n); | |
f7a5bb28 | 221 | else if (k < 0 && r == 0) |
bf5332d2 | 222 | r = k; |
6b78df0a TG |
223 | } |
224 | ||
5eda94dd LP |
225 | return r; |
226 | } |