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