]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ | |
2 | ||
3 | #include <ctype.h> | |
4 | ||
5 | #include "alloc-util.h" | |
6 | #include "architecture.h" | |
7 | #include "conf-files.h" | |
8 | #include "conf-parser.h" | |
9 | #include "confidential-virt.h" | |
10 | #include "constants.h" | |
11 | #include "device-private.h" | |
12 | #include "device-util.h" | |
13 | #include "dirent-util.h" | |
14 | #include "escape.h" | |
15 | #include "fd-util.h" | |
16 | #include "fileio.h" | |
17 | #include "format-util.h" | |
18 | #include "fs-util.h" | |
19 | #include "glob-util.h" | |
20 | #include "list.h" | |
21 | #include "mkdir.h" | |
22 | #include "netif-naming-scheme.h" | |
23 | #include "nulstr-util.h" | |
24 | #include "parse-util.h" | |
25 | #include "path-util.h" | |
26 | #include "proc-cmdline.h" | |
27 | #include "socket-util.h" | |
28 | #include "stat-util.h" | |
29 | #include "string-table.h" | |
30 | #include "strv.h" | |
31 | #include "strxcpyx.h" | |
32 | #include "sysctl-util.h" | |
33 | #include "syslog-util.h" | |
34 | #include "udev-builtin.h" | |
35 | #include "udev-event.h" | |
36 | #include "udev-format.h" | |
37 | #include "udev-node.h" | |
38 | #include "udev-rules.h" | |
39 | #include "udev-spawn.h" | |
40 | #include "udev-trace.h" | |
41 | #include "udev-util.h" | |
42 | #include "user-util.h" | |
43 | #include "virt.h" | |
44 | ||
45 | #define RULES_DIRS ((const char* const*) CONF_PATHS_STRV("udev/rules.d")) | |
46 | ||
47 | typedef enum { | |
48 | OP_MATCH, /* == */ | |
49 | OP_NOMATCH, /* != */ | |
50 | OP_ADD, /* += */ | |
51 | OP_REMOVE, /* -= */ | |
52 | OP_ASSIGN, /* = */ | |
53 | OP_ASSIGN_FINAL, /* := */ | |
54 | _OP_TYPE_MAX, | |
55 | _OP_TYPE_INVALID = -EINVAL, | |
56 | } UdevRuleOperatorType; | |
57 | ||
58 | typedef enum { | |
59 | MATCH_TYPE_EMPTY, /* empty string */ | |
60 | MATCH_TYPE_PLAIN, /* no special characters */ | |
61 | MATCH_TYPE_PLAIN_WITH_EMPTY, /* no special characters with empty string, e.g., "|foo" */ | |
62 | MATCH_TYPE_GLOB, /* shell globs ?,*,[] */ | |
63 | MATCH_TYPE_GLOB_WITH_EMPTY, /* shell globs ?,*,[] with empty string, e.g., "|foo*" */ | |
64 | MATCH_TYPE_SUBSYSTEM, /* "subsystem", "bus", or "class" */ | |
65 | _MATCH_TYPE_MAX, | |
66 | _MATCH_TYPE_INVALID = -EINVAL, | |
67 | } UdevRuleMatchType; | |
68 | ||
69 | typedef enum { | |
70 | SUBST_TYPE_PLAIN, /* no substitution */ | |
71 | SUBST_TYPE_FORMAT, /* % or $ */ | |
72 | SUBST_TYPE_SUBSYS, /* "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */ | |
73 | _SUBST_TYPE_MAX, | |
74 | _SUBST_TYPE_INVALID = -EINVAL, | |
75 | } UdevRuleSubstituteType; | |
76 | ||
77 | typedef enum { | |
78 | /* lvalues which take match or nomatch operator */ | |
79 | TK_M_ACTION, /* string, device_get_action() */ | |
80 | TK_M_DEVPATH, /* path, sd_device_get_devpath() */ | |
81 | TK_M_KERNEL, /* string, sd_device_get_sysname() */ | |
82 | TK_M_DEVLINK, /* strv, sd_device_get_devlink_first(), sd_device_get_devlink_next() */ | |
83 | TK_M_NAME, /* string, name of network interface */ | |
84 | TK_M_ENV, /* string, device property, takes key through attribute */ | |
85 | TK_M_CONST, /* string, system-specific hard-coded constant */ | |
86 | TK_M_TAG, /* strv, sd_device_get_tag_first(), sd_device_get_tag_next() */ | |
87 | TK_M_SUBSYSTEM, /* string, sd_device_get_subsystem() */ | |
88 | TK_M_DRIVER, /* string, sd_device_get_driver() */ | |
89 | TK_M_ATTR, /* string, takes filename through attribute, sd_device_get_sysattr_value(), udev_resolve_subsys_kernel(), etc. */ | |
90 | TK_M_SYSCTL, /* string, takes kernel parameter through attribute */ | |
91 | ||
92 | /* matches parent parameters */ | |
93 | TK_M_PARENTS_KERNEL, /* string */ | |
94 | TK_M_PARENTS_SUBSYSTEM, /* string */ | |
95 | TK_M_PARENTS_DRIVER, /* string */ | |
96 | TK_M_PARENTS_ATTR, /* string */ | |
97 | TK_M_PARENTS_TAG, /* strv */ | |
98 | ||
99 | TK_M_TEST, /* path, optionally mode_t can be specified by attribute, test the existence of a file */ | |
100 | TK_M_PROGRAM, /* string, execute a program */ | |
101 | TK_M_IMPORT_FILE, /* path */ | |
102 | TK_M_IMPORT_PROGRAM, /* string, import properties from the result of program */ | |
103 | TK_M_IMPORT_BUILTIN, /* string, import properties from the result of built-in command */ | |
104 | TK_M_IMPORT_DB, /* string, import properties from database */ | |
105 | TK_M_IMPORT_CMDLINE, /* string, kernel command line */ | |
106 | TK_M_IMPORT_PARENT, /* string, parent property */ | |
107 | TK_M_RESULT, /* string, result of TK_M_PROGRAM */ | |
108 | ||
109 | #define _TK_M_MAX (TK_M_RESULT + 1) | |
110 | #define _TK_A_MIN _TK_M_MAX | |
111 | ||
112 | /* lvalues which take one of assign operators */ | |
113 | TK_A_OPTIONS_STRING_ESCAPE_NONE, /* no argument */ | |
114 | TK_A_OPTIONS_STRING_ESCAPE_REPLACE, /* no argument */ | |
115 | TK_A_OPTIONS_DB_PERSIST, /* no argument */ | |
116 | TK_A_OPTIONS_INOTIFY_WATCH, /* boolean */ | |
117 | TK_A_OPTIONS_DEVLINK_PRIORITY, /* int */ | |
118 | TK_A_OPTIONS_LOG_LEVEL, /* string of log level or "reset" */ | |
119 | TK_A_OWNER, /* user name */ | |
120 | TK_A_GROUP, /* group name */ | |
121 | TK_A_MODE, /* mode string */ | |
122 | TK_A_OWNER_ID, /* uid_t */ | |
123 | TK_A_GROUP_ID, /* gid_t */ | |
124 | TK_A_MODE_ID, /* mode_t */ | |
125 | TK_A_TAG, /* string */ | |
126 | TK_A_OPTIONS_STATIC_NODE, /* device path, /dev/... */ | |
127 | TK_A_SECLABEL, /* string with attribute */ | |
128 | TK_A_ENV, /* string with attribute */ | |
129 | TK_A_NAME, /* ifname */ | |
130 | TK_A_DEVLINK, /* string */ | |
131 | TK_A_ATTR, /* string with attribute */ | |
132 | TK_A_SYSCTL, /* string with attribute */ | |
133 | TK_A_RUN_BUILTIN, /* string */ | |
134 | TK_A_RUN_PROGRAM, /* string */ | |
135 | ||
136 | _TK_TYPE_MAX, | |
137 | _TK_TYPE_INVALID = -EINVAL, | |
138 | } UdevRuleTokenType; | |
139 | ||
140 | typedef enum { | |
141 | LINE_HAS_NAME = 1 << 0, /* has NAME= */ | |
142 | LINE_HAS_DEVLINK = 1 << 1, /* has SYMLINK=, OWNER=, GROUP= or MODE= */ | |
143 | LINE_HAS_STATIC_NODE = 1 << 2, /* has OPTIONS=static_node */ | |
144 | LINE_HAS_GOTO = 1 << 3, /* has GOTO= */ | |
145 | LINE_HAS_LABEL = 1 << 4, /* has LABEL= */ | |
146 | LINE_UPDATE_SOMETHING = 1 << 5, /* has other TK_A_* or TK_M_IMPORT tokens */ | |
147 | LINE_IS_REFERENCED = 1 << 6, /* is referenced by GOTO */ | |
148 | } UdevRuleLineType; | |
149 | ||
150 | typedef struct UdevRuleFile UdevRuleFile; | |
151 | typedef struct UdevRuleLine UdevRuleLine; | |
152 | typedef struct UdevRuleToken UdevRuleToken; | |
153 | ||
154 | struct UdevRuleToken { | |
155 | UdevRuleTokenType type:8; | |
156 | UdevRuleOperatorType op:8; | |
157 | UdevRuleMatchType match_type:8; | |
158 | UdevRuleSubstituteType attr_subst_type:7; | |
159 | bool attr_match_remove_trailing_whitespace:1; | |
160 | const char *value; | |
161 | void *data; | |
162 | ||
163 | UdevRuleLine *rule_line; | |
164 | LIST_FIELDS(UdevRuleToken, tokens); | |
165 | }; | |
166 | ||
167 | struct UdevRuleLine { | |
168 | char *line; | |
169 | unsigned line_number; | |
170 | UdevRuleLineType type; | |
171 | ||
172 | const char *label; | |
173 | const char *goto_label; | |
174 | UdevRuleLine *goto_line; | |
175 | ||
176 | UdevRuleFile *rule_file; | |
177 | LIST_HEAD(UdevRuleToken, tokens); | |
178 | LIST_FIELDS(UdevRuleLine, rule_lines); | |
179 | }; | |
180 | ||
181 | struct UdevRuleFile { | |
182 | char *filename; | |
183 | unsigned issues; /* used by "udevadm verify" */ | |
184 | ||
185 | UdevRules *rules; | |
186 | LIST_HEAD(UdevRuleLine, rule_lines); | |
187 | LIST_FIELDS(UdevRuleFile, rule_files); | |
188 | }; | |
189 | ||
190 | struct UdevRules { | |
191 | ResolveNameTiming resolve_name_timing; | |
192 | Hashmap *known_users; | |
193 | Hashmap *known_groups; | |
194 | Hashmap *stats_by_path; | |
195 | LIST_HEAD(UdevRuleFile, rule_files); | |
196 | }; | |
197 | ||
198 | #define LINE_GET_RULES(line) \ | |
199 | ASSERT_PTR(ASSERT_PTR(ASSERT_PTR(line)->rule_file)->rules) | |
200 | ||
201 | /*** Logging helpers ***/ | |
202 | ||
203 | #define log_udev_rule_internal(device, file, line_nr, level, error, fmt, ...) \ | |
204 | ({ \ | |
205 | int _lv = (level); \ | |
206 | sd_device *_dev = (device); \ | |
207 | UdevRuleFile *_f = (file); \ | |
208 | const char *_n = _f ? _f->filename : NULL; \ | |
209 | \ | |
210 | if (!_dev && _f) \ | |
211 | _f->issues |= (1U << _lv); \ | |
212 | \ | |
213 | log_device_full_errno_zerook( \ | |
214 | _dev, _lv, error, "%s:%u " fmt, \ | |
215 | strna(_n), line_nr, \ | |
216 | ##__VA_ARGS__); \ | |
217 | }) | |
218 | ||
219 | /* Mainly used when applying tokens to the event device. */ | |
220 | #define log_event_full_errno_zerook(device, token, ...) \ | |
221 | ({ \ | |
222 | UdevRuleToken *_t = (token); \ | |
223 | UdevRuleLine *_l = _t ? _t->rule_line : NULL; \ | |
224 | \ | |
225 | log_udev_rule_internal( \ | |
226 | device, \ | |
227 | _l ? _l->rule_file : NULL, \ | |
228 | _l ? _l->line_number : 0, \ | |
229 | __VA_ARGS__); \ | |
230 | }) | |
231 | ||
232 | #define log_event_full_errno(device, token, level, error, ...) \ | |
233 | ({ \ | |
234 | int _error = (error); \ | |
235 | ASSERT_NON_ZERO(_error); \ | |
236 | log_event_full_errno_zerook( \ | |
237 | device, token, level, _error, ##__VA_ARGS__); \ | |
238 | }) | |
239 | ||
240 | #define log_event_full(device, token, level, ...) (void) log_event_full_errno_zerook(device, token, level, 0, __VA_ARGS__) | |
241 | ||
242 | #define log_event_debug(device, token, ...) log_event_full(device, token, LOG_DEBUG, __VA_ARGS__) | |
243 | #define log_event_info(device, token, ...) log_event_full(device, token, LOG_INFO, __VA_ARGS__) | |
244 | #define log_event_notice(device, token, ...) log_event_full(device, token, LOG_NOTICE, __VA_ARGS__) | |
245 | #define log_event_warning(device, token, ...) log_event_full(device, token, LOG_WARNING, __VA_ARGS__) | |
246 | #define log_event_error(device, token, ...) log_event_full(device, token, LOG_ERR, __VA_ARGS__) | |
247 | ||
248 | #define log_event_debug_errno(device, token, error, ...) log_event_full_errno(device, token, LOG_DEBUG, error, __VA_ARGS__) | |
249 | #define log_event_info_errno(device, token, error, ...) log_event_full_errno(device, token, LOG_INFO, error, __VA_ARGS__) | |
250 | #define log_event_notice_errno(device, token, error, ...) log_event_full_errno(device, token, LOG_NOTICE, error, __VA_ARGS__) | |
251 | #define log_event_warning_errno(device, token, error, ...) log_event_full_errno(device, token, LOG_WARNING, error, __VA_ARGS__) | |
252 | #define log_event_error_errno(device, token, error, ...) log_event_full_errno(device, token, LOG_ERR, error, __VA_ARGS__) | |
253 | ||
254 | /* Mainly used when parsing .rules files. */ | |
255 | #define log_file_full_errno_zerook(...) \ | |
256 | log_udev_rule_internal(NULL, __VA_ARGS__) | |
257 | ||
258 | #define log_file_error(file, line_nr, ...) \ | |
259 | log_file_full_errno_zerook(file, line_nr, LOG_ERR, 0, __VA_ARGS__) | |
260 | ||
261 | #define log_line_full_errno_zerook(line, ...) \ | |
262 | ({ \ | |
263 | UdevRuleLine *_l = (line); \ | |
264 | log_file_full_errno_zerook( \ | |
265 | _l ? _l->rule_file : NULL, \ | |
266 | _l ? _l->line_number : 0, \ | |
267 | __VA_ARGS__); \ | |
268 | }) | |
269 | ||
270 | #define log_line_full_errno(line, level, error, ...) \ | |
271 | ({ \ | |
272 | int _error = (error); \ | |
273 | ASSERT_NON_ZERO(_error); \ | |
274 | log_line_full_errno_zerook( \ | |
275 | line, level, _error, ##__VA_ARGS__); \ | |
276 | }) | |
277 | ||
278 | #define log_line_full(line, level, ...) (void) log_line_full_errno_zerook(line, level, 0, __VA_ARGS__) | |
279 | ||
280 | #define log_line_debug(line, ...) log_line_full(line, LOG_DEBUG, __VA_ARGS__) | |
281 | #define log_line_info(line, ...) log_line_full(line, LOG_INFO, __VA_ARGS__) | |
282 | #define log_line_notice(line, ...) log_line_full(line, LOG_NOTICE, __VA_ARGS__) | |
283 | #define log_line_warning(line, ...) log_line_full(line, LOG_WARNING, __VA_ARGS__) | |
284 | #define log_line_error(line, ...) log_line_full(line, LOG_ERR, __VA_ARGS__) | |
285 | ||
286 | #define log_line_debug_errno(line, error, ...) log_line_full_errno(line, LOG_DEBUG, error, __VA_ARGS__) | |
287 | #define log_line_info_errno(line, error, ...) log_line_full_errno(line, LOG_INFO, error, __VA_ARGS__) | |
288 | #define log_line_notice_errno(line, error, ...) log_line_full_errno(line, LOG_NOTICE, error, __VA_ARGS__) | |
289 | #define log_line_warning_errno(line, error, ...) log_line_full_errno(line, LOG_WARNING, error, __VA_ARGS__) | |
290 | #define log_line_error_errno(line, error, ...) log_line_full_errno(line, LOG_ERR, error, __VA_ARGS__) | |
291 | ||
292 | #define _log_line_invalid_token(line, key, type) \ | |
293 | log_line_error_errno(line, SYNTHETIC_ERRNO(EINVAL), \ | |
294 | "Invalid %s for %s.", type, key) | |
295 | ||
296 | #define log_line_invalid_op(line, key) _log_line_invalid_token(line, key, "operator") | |
297 | #define log_line_invalid_attr(line, key) _log_line_invalid_token(line, key, "attribute") | |
298 | ||
299 | #define log_line_invalid_attr_format(line, key, attr, offset, hint) \ | |
300 | log_line_error_errno(line, SYNTHETIC_ERRNO(EINVAL), \ | |
301 | "Invalid attribute \"%s\" for %s (char %zu: %s), ignoring.", \ | |
302 | attr, key, offset, hint) | |
303 | #define log_line_invalid_value(line, key, value, offset, hint) \ | |
304 | log_line_error_errno(line, SYNTHETIC_ERRNO(EINVAL), \ | |
305 | "Invalid value \"%s\" for %s (char %zu: %s), ignoring.", \ | |
306 | value, key, offset, hint) | |
307 | ||
308 | static void log_unknown_owner(sd_device *dev, UdevRuleLine *line, int error, const char *entity, const char *name) { | |
309 | assert(line); | |
310 | ASSERT_NON_ZERO(error); | |
311 | ||
312 | if (IN_SET(abs(error), ENOENT, ESRCH)) | |
313 | log_udev_rule_internal(dev, line->rule_file, line->line_number, LOG_ERR, error, | |
314 | "Unknown %s '%s', ignoring.", entity, name); | |
315 | else | |
316 | log_udev_rule_internal(dev, line->rule_file, line->line_number, LOG_ERR, error, | |
317 | "Failed to resolve %s '%s', ignoring: %m", entity, name); | |
318 | } | |
319 | ||
320 | static void log_event_truncated( | |
321 | sd_device *dev, | |
322 | UdevRuleToken *token, | |
323 | const char *what, | |
324 | const char *format, | |
325 | const char *key, | |
326 | bool is_match) { | |
327 | ||
328 | if (is_match) | |
329 | log_event_debug(dev, token, | |
330 | "The %s is truncated while substituting into '%s', " | |
331 | "assuming the %s key does not match.", | |
332 | what, format, key); | |
333 | else | |
334 | log_event_warning(dev, token, | |
335 | "The %s is truncated while substituting into '%s', " | |
336 | "refusing to apply the %s key.", | |
337 | what, format, key); | |
338 | } | |
339 | ||
340 | /*** Other functions ***/ | |
341 | ||
342 | static UdevRuleToken *udev_rule_token_free(UdevRuleToken *token) { | |
343 | if (!token) | |
344 | return NULL; | |
345 | ||
346 | if (token->rule_line) | |
347 | LIST_REMOVE(tokens, token->rule_line->tokens, token); | |
348 | ||
349 | return mfree(token); | |
350 | } | |
351 | ||
352 | DEFINE_TRIVIAL_CLEANUP_FUNC(UdevRuleToken*, udev_rule_token_free); | |
353 | ||
354 | static void udev_rule_line_clear_tokens(UdevRuleLine *rule_line) { | |
355 | assert(rule_line); | |
356 | ||
357 | LIST_FOREACH(tokens, i, rule_line->tokens) | |
358 | udev_rule_token_free(i); | |
359 | } | |
360 | ||
361 | static UdevRuleLine *udev_rule_line_free(UdevRuleLine *rule_line) { | |
362 | if (!rule_line) | |
363 | return NULL; | |
364 | ||
365 | udev_rule_line_clear_tokens(rule_line); | |
366 | ||
367 | if (rule_line->rule_file) | |
368 | LIST_REMOVE(rule_lines, rule_line->rule_file->rule_lines, rule_line); | |
369 | ||
370 | free(rule_line->line); | |
371 | return mfree(rule_line); | |
372 | } | |
373 | ||
374 | DEFINE_TRIVIAL_CLEANUP_FUNC(UdevRuleLine*, udev_rule_line_free); | |
375 | ||
376 | static UdevRuleFile *udev_rule_file_free(UdevRuleFile *rule_file) { | |
377 | if (!rule_file) | |
378 | return NULL; | |
379 | ||
380 | LIST_FOREACH(rule_lines, i, rule_file->rule_lines) | |
381 | udev_rule_line_free(i); | |
382 | ||
383 | if (rule_file->rules) | |
384 | LIST_REMOVE(rule_files, rule_file->rules->rule_files, rule_file); | |
385 | ||
386 | free(rule_file->filename); | |
387 | return mfree(rule_file); | |
388 | } | |
389 | ||
390 | DEFINE_TRIVIAL_CLEANUP_FUNC(UdevRuleFile*, udev_rule_file_free); | |
391 | ||
392 | UdevRules *udev_rules_free(UdevRules *rules) { | |
393 | if (!rules) | |
394 | return NULL; | |
395 | ||
396 | LIST_FOREACH(rule_files, i, rules->rule_files) | |
397 | udev_rule_file_free(i); | |
398 | ||
399 | hashmap_free_free_key(rules->known_users); | |
400 | hashmap_free_free_key(rules->known_groups); | |
401 | hashmap_free(rules->stats_by_path); | |
402 | return mfree(rules); | |
403 | } | |
404 | ||
405 | static int rule_resolve_user(UdevRuleLine *rule_line, const char *name, uid_t *ret) { | |
406 | Hashmap **known_users = &LINE_GET_RULES(rule_line)->known_users; | |
407 | _cleanup_free_ char *n = NULL; | |
408 | uid_t uid; | |
409 | void *val; | |
410 | int r; | |
411 | ||
412 | assert(name); | |
413 | assert(ret); | |
414 | ||
415 | val = hashmap_get(*known_users, name); | |
416 | if (val) { | |
417 | *ret = PTR_TO_UID(val); | |
418 | return 0; | |
419 | } | |
420 | ||
421 | r = get_user_creds(&name, &uid, NULL, NULL, NULL, USER_CREDS_ALLOW_MISSING); | |
422 | if (r < 0) { | |
423 | log_unknown_owner(NULL, rule_line, r, "user", name); | |
424 | *ret = UID_INVALID; | |
425 | return 0; | |
426 | } | |
427 | ||
428 | n = strdup(name); | |
429 | if (!n) | |
430 | return -ENOMEM; | |
431 | ||
432 | r = hashmap_ensure_put(known_users, &string_hash_ops, n, UID_TO_PTR(uid)); | |
433 | if (r < 0) | |
434 | return r; | |
435 | ||
436 | TAKE_PTR(n); | |
437 | *ret = uid; | |
438 | return 0; | |
439 | } | |
440 | ||
441 | static int rule_resolve_group(UdevRuleLine *rule_line, const char *name, gid_t *ret) { | |
442 | Hashmap **known_groups = &LINE_GET_RULES(rule_line)->known_groups; | |
443 | _cleanup_free_ char *n = NULL; | |
444 | gid_t gid; | |
445 | void *val; | |
446 | int r; | |
447 | ||
448 | assert(name); | |
449 | assert(ret); | |
450 | ||
451 | val = hashmap_get(*known_groups, name); | |
452 | if (val) { | |
453 | *ret = PTR_TO_GID(val); | |
454 | return 0; | |
455 | } | |
456 | ||
457 | r = get_group_creds(&name, &gid, USER_CREDS_ALLOW_MISSING); | |
458 | if (r < 0) { | |
459 | log_unknown_owner(NULL, rule_line, r, "group", name); | |
460 | *ret = GID_INVALID; | |
461 | return 0; | |
462 | } | |
463 | ||
464 | n = strdup(name); | |
465 | if (!n) | |
466 | return -ENOMEM; | |
467 | ||
468 | r = hashmap_ensure_put(known_groups, &string_hash_ops, n, GID_TO_PTR(gid)); | |
469 | if (r < 0) | |
470 | return r; | |
471 | ||
472 | TAKE_PTR(n); | |
473 | *ret = gid; | |
474 | return 0; | |
475 | } | |
476 | ||
477 | static UdevRuleSubstituteType rule_get_substitution_type(const char *str) { | |
478 | assert(str); | |
479 | ||
480 | if (str[0] == '[') | |
481 | return SUBST_TYPE_SUBSYS; | |
482 | if (strchr(str, '%') || strchr(str, '$')) | |
483 | return SUBST_TYPE_FORMAT; | |
484 | return SUBST_TYPE_PLAIN; | |
485 | } | |
486 | ||
487 | static bool type_has_nulstr_value(UdevRuleTokenType type) { | |
488 | return type < TK_M_TEST || type == TK_M_RESULT; | |
489 | } | |
490 | ||
491 | static int rule_line_add_token(UdevRuleLine *rule_line, UdevRuleTokenType type, UdevRuleOperatorType op, char *value, void *data) { | |
492 | _cleanup_(udev_rule_token_freep) UdevRuleToken *token = NULL; | |
493 | UdevRuleMatchType match_type = _MATCH_TYPE_INVALID; | |
494 | UdevRuleSubstituteType subst_type = _SUBST_TYPE_INVALID; | |
495 | bool remove_trailing_whitespace = false; | |
496 | size_t len; | |
497 | ||
498 | assert(rule_line); | |
499 | assert(type >= 0 && type < _TK_TYPE_MAX); | |
500 | assert(op >= 0 && op < _OP_TYPE_MAX); | |
501 | ||
502 | if (type < _TK_M_MAX) { | |
503 | assert(value); | |
504 | assert(IN_SET(op, OP_MATCH, OP_NOMATCH)); | |
505 | ||
506 | if (type == TK_M_SUBSYSTEM && STR_IN_SET(value, "subsystem", "bus", "class")) | |
507 | match_type = MATCH_TYPE_SUBSYSTEM; | |
508 | else if (isempty(value)) | |
509 | match_type = MATCH_TYPE_EMPTY; | |
510 | else if (streq(value, "?*")) { | |
511 | /* Convert KEY=="?*" -> KEY!="" */ | |
512 | match_type = MATCH_TYPE_EMPTY; | |
513 | op = op == OP_MATCH ? OP_NOMATCH : OP_MATCH; | |
514 | } else if (string_is_glob(value)) | |
515 | match_type = MATCH_TYPE_GLOB; | |
516 | else | |
517 | match_type = MATCH_TYPE_PLAIN; | |
518 | ||
519 | if (type_has_nulstr_value(type)) { | |
520 | /* Convert value string to nulstr. */ | |
521 | bool bar = true, empty = false; | |
522 | char *a, *b; | |
523 | ||
524 | for (a = b = value; *a != '\0'; a++) { | |
525 | if (*a != '|') { | |
526 | *b++ = *a; | |
527 | bar = false; | |
528 | } else { | |
529 | if (bar) | |
530 | empty = true; | |
531 | else | |
532 | *b++ = '\0'; | |
533 | bar = true; | |
534 | } | |
535 | } | |
536 | *b = '\0'; | |
537 | ||
538 | /* Make sure the value is end, so NULSTR_FOREACH can read correct match */ | |
539 | if (b < a) | |
540 | b[1] = '\0'; | |
541 | ||
542 | if (bar) | |
543 | empty = true; | |
544 | ||
545 | if (empty) { | |
546 | if (match_type == MATCH_TYPE_GLOB) | |
547 | match_type = MATCH_TYPE_GLOB_WITH_EMPTY; | |
548 | if (match_type == MATCH_TYPE_PLAIN) | |
549 | match_type = MATCH_TYPE_PLAIN_WITH_EMPTY; | |
550 | } | |
551 | } | |
552 | } | |
553 | ||
554 | if (IN_SET(type, TK_M_ATTR, TK_M_PARENTS_ATTR)) { | |
555 | assert(value); | |
556 | assert(data); | |
557 | ||
558 | len = strlen(value); | |
559 | if (len > 0 && !isspace(value[len - 1])) | |
560 | remove_trailing_whitespace = true; | |
561 | ||
562 | subst_type = rule_get_substitution_type(data); | |
563 | } | |
564 | ||
565 | token = new(UdevRuleToken, 1); | |
566 | if (!token) | |
567 | return -ENOMEM; | |
568 | ||
569 | *token = (UdevRuleToken) { | |
570 | .type = type, | |
571 | .op = op, | |
572 | .value = value, | |
573 | .data = data, | |
574 | .match_type = match_type, | |
575 | .attr_subst_type = subst_type, | |
576 | .attr_match_remove_trailing_whitespace = remove_trailing_whitespace, | |
577 | .rule_line = rule_line, | |
578 | }; | |
579 | ||
580 | LIST_APPEND(tokens, rule_line->tokens, token); | |
581 | ||
582 | if (token->type == TK_A_NAME) | |
583 | SET_FLAG(rule_line->type, LINE_HAS_NAME, true); | |
584 | ||
585 | else if (IN_SET(token->type, TK_A_DEVLINK, | |
586 | TK_A_OWNER, TK_A_GROUP, TK_A_MODE, | |
587 | TK_A_OWNER_ID, TK_A_GROUP_ID, TK_A_MODE_ID)) | |
588 | SET_FLAG(rule_line->type, LINE_HAS_DEVLINK, true); | |
589 | ||
590 | else if (token->type == TK_A_OPTIONS_STATIC_NODE) | |
591 | SET_FLAG(rule_line->type, LINE_HAS_STATIC_NODE, true); | |
592 | ||
593 | else if (token->type >= _TK_A_MIN || | |
594 | IN_SET(token->type, TK_M_PROGRAM, | |
595 | TK_M_IMPORT_FILE, TK_M_IMPORT_PROGRAM, TK_M_IMPORT_BUILTIN, | |
596 | TK_M_IMPORT_DB, TK_M_IMPORT_CMDLINE, TK_M_IMPORT_PARENT)) | |
597 | SET_FLAG(rule_line->type, LINE_UPDATE_SOMETHING, true); | |
598 | ||
599 | TAKE_PTR(token); | |
600 | return 0; | |
601 | } | |
602 | ||
603 | static void check_value_format_and_warn(UdevRuleLine *line, const char *key, const char *value, bool nonempty) { | |
604 | size_t offset; | |
605 | const char *hint; | |
606 | ||
607 | if (nonempty && isempty(value)) | |
608 | log_line_invalid_value(line, key, value, (size_t) 0, "empty value"); | |
609 | else if (udev_check_format(value, &offset, &hint) < 0) | |
610 | log_line_invalid_value(line, key, value, offset + 1, hint); | |
611 | } | |
612 | ||
613 | static int check_attr_format_and_warn(UdevRuleLine *line, const char *key, const char *value) { | |
614 | size_t offset; | |
615 | const char *hint; | |
616 | ||
617 | if (isempty(value)) | |
618 | return log_line_invalid_attr(line, key); | |
619 | if (udev_check_format(value, &offset, &hint) < 0) | |
620 | log_line_invalid_attr_format(line, key, value, offset + 1, hint); | |
621 | return 0; | |
622 | } | |
623 | ||
624 | static int parse_token(UdevRuleLine *rule_line, const char *key, char *attr, UdevRuleOperatorType op, char *value) { | |
625 | ResolveNameTiming resolve_name_timing = LINE_GET_RULES(rule_line)->resolve_name_timing; | |
626 | bool is_match = IN_SET(op, OP_MATCH, OP_NOMATCH); | |
627 | int r; | |
628 | ||
629 | assert(key); | |
630 | assert(value); | |
631 | ||
632 | if (streq(key, "ACTION")) { | |
633 | if (attr) | |
634 | return log_line_invalid_attr(rule_line, key); | |
635 | if (!is_match) | |
636 | return log_line_invalid_op(rule_line, key); | |
637 | ||
638 | r = rule_line_add_token(rule_line, TK_M_ACTION, op, value, NULL); | |
639 | } else if (streq(key, "DEVPATH")) { | |
640 | if (attr) | |
641 | return log_line_invalid_attr(rule_line, key); | |
642 | if (!is_match) | |
643 | return log_line_invalid_op(rule_line, key); | |
644 | ||
645 | r = rule_line_add_token(rule_line, TK_M_DEVPATH, op, value, NULL); | |
646 | } else if (streq(key, "KERNEL")) { | |
647 | if (attr) | |
648 | return log_line_invalid_attr(rule_line, key); | |
649 | if (!is_match) | |
650 | return log_line_invalid_op(rule_line, key); | |
651 | ||
652 | r = rule_line_add_token(rule_line, TK_M_KERNEL, op, value, NULL); | |
653 | } else if (streq(key, "SYMLINK")) { | |
654 | if (attr) | |
655 | return log_line_invalid_attr(rule_line, key); | |
656 | if (!is_match) { | |
657 | check_value_format_and_warn(rule_line, key, value, false); | |
658 | r = rule_line_add_token(rule_line, TK_A_DEVLINK, op, value, NULL); | |
659 | } else | |
660 | r = rule_line_add_token(rule_line, TK_M_DEVLINK, op, value, NULL); | |
661 | } else if (streq(key, "NAME")) { | |
662 | if (attr) | |
663 | return log_line_invalid_attr(rule_line, key); | |
664 | if (op == OP_REMOVE) | |
665 | return log_line_invalid_op(rule_line, key); | |
666 | if (op == OP_ADD) { | |
667 | log_line_warning(rule_line, "%s key takes '==', '!=', '=', or ':=' operator, assuming '='.", key); | |
668 | op = OP_ASSIGN; | |
669 | } | |
670 | ||
671 | if (!is_match) { | |
672 | if (streq(value, "%k")) | |
673 | return log_line_error_errno(rule_line, SYNTHETIC_ERRNO(EINVAL), | |
674 | "Ignoring NAME=\"%%k\", as it will take no effect."); | |
675 | if (isempty(value)) | |
676 | return log_line_error_errno(rule_line, SYNTHETIC_ERRNO(EINVAL), | |
677 | "Ignoring NAME=\"\", as udev will not delete any network interfaces."); | |
678 | check_value_format_and_warn(rule_line, key, value, false); | |
679 | ||
680 | r = rule_line_add_token(rule_line, TK_A_NAME, op, value, NULL); | |
681 | } else | |
682 | r = rule_line_add_token(rule_line, TK_M_NAME, op, value, NULL); | |
683 | } else if (streq(key, "ENV")) { | |
684 | if (isempty(attr)) | |
685 | return log_line_invalid_attr(rule_line, key); | |
686 | if (op == OP_REMOVE) | |
687 | return log_line_invalid_op(rule_line, key); | |
688 | if (op == OP_ASSIGN_FINAL) { | |
689 | log_line_warning(rule_line, "%s key takes '==', '!=', '=', or '+=' operator, assuming '='.", key); | |
690 | op = OP_ASSIGN; | |
691 | } | |
692 | ||
693 | if (!is_match) { | |
694 | if (!device_property_can_set(attr)) | |
695 | return log_line_error_errno(rule_line, SYNTHETIC_ERRNO(EINVAL), | |
696 | "Invalid ENV attribute. '%s' cannot be set.", attr); | |
697 | ||
698 | check_value_format_and_warn(rule_line, key, value, false); | |
699 | ||
700 | r = rule_line_add_token(rule_line, TK_A_ENV, op, value, attr); | |
701 | } else | |
702 | r = rule_line_add_token(rule_line, TK_M_ENV, op, value, attr); | |
703 | } else if (streq(key, "CONST")) { | |
704 | if (isempty(attr) || !STR_IN_SET(attr, "arch", "virt")) | |
705 | return log_line_invalid_attr(rule_line, key); | |
706 | if (!is_match) | |
707 | return log_line_invalid_op(rule_line, key); | |
708 | r = rule_line_add_token(rule_line, TK_M_CONST, op, value, attr); | |
709 | } else if (streq(key, "TAG")) { | |
710 | if (attr) | |
711 | return log_line_invalid_attr(rule_line, key); | |
712 | if (op == OP_ASSIGN_FINAL) { | |
713 | log_line_warning(rule_line, "%s key takes '==', '!=', '=', or '+=' operator, assuming '='.", key); | |
714 | op = OP_ASSIGN; | |
715 | } | |
716 | ||
717 | if (!is_match) { | |
718 | check_value_format_and_warn(rule_line, key, value, true); | |
719 | ||
720 | r = rule_line_add_token(rule_line, TK_A_TAG, op, value, NULL); | |
721 | } else | |
722 | r = rule_line_add_token(rule_line, TK_M_TAG, op, value, NULL); | |
723 | } else if (streq(key, "SUBSYSTEM")) { | |
724 | if (attr) | |
725 | return log_line_invalid_attr(rule_line, key); | |
726 | if (!is_match) | |
727 | return log_line_invalid_op(rule_line, key); | |
728 | ||
729 | if (STR_IN_SET(value, "bus", "class")) | |
730 | log_line_warning(rule_line, "\"%s\" must be specified as \"subsystem\".", value); | |
731 | ||
732 | r = rule_line_add_token(rule_line, TK_M_SUBSYSTEM, op, value, NULL); | |
733 | } else if (streq(key, "DRIVER")) { | |
734 | if (attr) | |
735 | return log_line_invalid_attr(rule_line, key); | |
736 | if (!is_match) | |
737 | return log_line_invalid_op(rule_line, key); | |
738 | ||
739 | r = rule_line_add_token(rule_line, TK_M_DRIVER, op, value, NULL); | |
740 | } else if (streq(key, "ATTR")) { | |
741 | r = check_attr_format_and_warn(rule_line, key, attr); | |
742 | if (r < 0) | |
743 | return r; | |
744 | if (op == OP_REMOVE) | |
745 | return log_line_invalid_op(rule_line, key); | |
746 | if (IN_SET(op, OP_ADD, OP_ASSIGN_FINAL)) { | |
747 | log_line_warning(rule_line, "%s key takes '==', '!=', or '=' operator, assuming '='.", key); | |
748 | op = OP_ASSIGN; | |
749 | } | |
750 | ||
751 | if (!is_match) { | |
752 | check_value_format_and_warn(rule_line, key, value, false); | |
753 | r = rule_line_add_token(rule_line, TK_A_ATTR, op, value, attr); | |
754 | } else | |
755 | r = rule_line_add_token(rule_line, TK_M_ATTR, op, value, attr); | |
756 | } else if (streq(key, "SYSCTL")) { | |
757 | r = check_attr_format_and_warn(rule_line, key, attr); | |
758 | if (r < 0) | |
759 | return r; | |
760 | if (op == OP_REMOVE) | |
761 | return log_line_invalid_op(rule_line, key); | |
762 | if (IN_SET(op, OP_ADD, OP_ASSIGN_FINAL)) { | |
763 | log_line_warning(rule_line, "%s key takes '==', '!=', or '=' operator, assuming '='.", key); | |
764 | op = OP_ASSIGN; | |
765 | } | |
766 | ||
767 | if (!is_match) { | |
768 | check_value_format_and_warn(rule_line, key, value, false); | |
769 | r = rule_line_add_token(rule_line, TK_A_SYSCTL, op, value, attr); | |
770 | } else | |
771 | r = rule_line_add_token(rule_line, TK_M_SYSCTL, op, value, attr); | |
772 | } else if (streq(key, "KERNELS")) { | |
773 | if (attr) | |
774 | return log_line_invalid_attr(rule_line, key); | |
775 | if (!is_match) | |
776 | return log_line_invalid_op(rule_line, key); | |
777 | ||
778 | r = rule_line_add_token(rule_line, TK_M_PARENTS_KERNEL, op, value, NULL); | |
779 | } else if (streq(key, "SUBSYSTEMS")) { | |
780 | if (attr) | |
781 | return log_line_invalid_attr(rule_line, key); | |
782 | if (!is_match) | |
783 | return log_line_invalid_op(rule_line, key); | |
784 | ||
785 | r = rule_line_add_token(rule_line, TK_M_PARENTS_SUBSYSTEM, op, value, NULL); | |
786 | } else if (streq(key, "DRIVERS")) { | |
787 | if (attr) | |
788 | return log_line_invalid_attr(rule_line, key); | |
789 | if (!is_match) | |
790 | return log_line_invalid_op(rule_line, key); | |
791 | ||
792 | r = rule_line_add_token(rule_line, TK_M_PARENTS_DRIVER, op, value, NULL); | |
793 | } else if (streq(key, "ATTRS")) { | |
794 | r = check_attr_format_and_warn(rule_line, key, attr); | |
795 | if (r < 0) | |
796 | return r; | |
797 | if (!is_match) | |
798 | return log_line_invalid_op(rule_line, key); | |
799 | ||
800 | if (startswith(attr, "device/")) | |
801 | log_line_warning(rule_line, "'device' link may not be available in future kernels."); | |
802 | if (strstr(attr, "../")) | |
803 | log_line_warning(rule_line, "Direct reference to parent sysfs directory, may break in future kernels."); | |
804 | ||
805 | r = rule_line_add_token(rule_line, TK_M_PARENTS_ATTR, op, value, attr); | |
806 | } else if (streq(key, "TAGS")) { | |
807 | if (attr) | |
808 | return log_line_invalid_attr(rule_line, key); | |
809 | if (!is_match) | |
810 | return log_line_invalid_op(rule_line, key); | |
811 | ||
812 | r = rule_line_add_token(rule_line, TK_M_PARENTS_TAG, op, value, NULL); | |
813 | } else if (streq(key, "TEST")) { | |
814 | mode_t mode = MODE_INVALID; | |
815 | ||
816 | if (!isempty(attr)) { | |
817 | r = parse_mode(attr, &mode); | |
818 | if (r < 0) | |
819 | return log_line_error_errno(rule_line, r, "Failed to parse mode '%s': %m", attr); | |
820 | } | |
821 | check_value_format_and_warn(rule_line, key, value, true); | |
822 | if (!is_match) | |
823 | return log_line_invalid_op(rule_line, key); | |
824 | ||
825 | r = rule_line_add_token(rule_line, TK_M_TEST, op, value, MODE_TO_PTR(mode)); | |
826 | } else if (streq(key, "PROGRAM")) { | |
827 | if (attr) | |
828 | return log_line_invalid_attr(rule_line, key); | |
829 | check_value_format_and_warn(rule_line, key, value, true); | |
830 | if (op == OP_REMOVE) | |
831 | return log_line_invalid_op(rule_line, key); | |
832 | if (!is_match) | |
833 | op = OP_MATCH; | |
834 | ||
835 | r = rule_line_add_token(rule_line, TK_M_PROGRAM, op, value, NULL); | |
836 | } else if (streq(key, "IMPORT")) { | |
837 | if (isempty(attr)) | |
838 | return log_line_invalid_attr(rule_line, key); | |
839 | check_value_format_and_warn(rule_line, key, value, true); | |
840 | if (op == OP_REMOVE) | |
841 | return log_line_invalid_op(rule_line, key); | |
842 | if (!is_match) | |
843 | op = OP_MATCH; | |
844 | ||
845 | if (streq(attr, "file")) | |
846 | r = rule_line_add_token(rule_line, TK_M_IMPORT_FILE, op, value, NULL); | |
847 | else if (streq(attr, "program")) { | |
848 | UdevBuiltinCommand cmd; | |
849 | ||
850 | cmd = udev_builtin_lookup(value); | |
851 | if (cmd >= 0) { | |
852 | log_line_debug(rule_line, "Found builtin command '%s' for %s, replacing attribute.", value, key); | |
853 | r = rule_line_add_token(rule_line, TK_M_IMPORT_BUILTIN, op, value, UDEV_BUILTIN_CMD_TO_PTR(cmd)); | |
854 | } else | |
855 | r = rule_line_add_token(rule_line, TK_M_IMPORT_PROGRAM, op, value, NULL); | |
856 | } else if (streq(attr, "builtin")) { | |
857 | UdevBuiltinCommand cmd; | |
858 | ||
859 | cmd = udev_builtin_lookup(value); | |
860 | if (cmd < 0) | |
861 | return log_line_error_errno(rule_line, SYNTHETIC_ERRNO(EINVAL), | |
862 | "Unknown builtin command: %s", value); | |
863 | r = rule_line_add_token(rule_line, TK_M_IMPORT_BUILTIN, op, value, UDEV_BUILTIN_CMD_TO_PTR(cmd)); | |
864 | } else if (streq(attr, "db")) | |
865 | r = rule_line_add_token(rule_line, TK_M_IMPORT_DB, op, value, NULL); | |
866 | else if (streq(attr, "cmdline")) | |
867 | r = rule_line_add_token(rule_line, TK_M_IMPORT_CMDLINE, op, value, NULL); | |
868 | else if (streq(attr, "parent")) | |
869 | r = rule_line_add_token(rule_line, TK_M_IMPORT_PARENT, op, value, NULL); | |
870 | else | |
871 | return log_line_invalid_attr(rule_line, key); | |
872 | } else if (streq(key, "RESULT")) { | |
873 | if (attr) | |
874 | return log_line_invalid_attr(rule_line, key); | |
875 | if (!is_match) | |
876 | return log_line_invalid_op(rule_line, key); | |
877 | ||
878 | r = rule_line_add_token(rule_line, TK_M_RESULT, op, value, NULL); | |
879 | } else if (streq(key, "OPTIONS")) { | |
880 | char *tmp; | |
881 | ||
882 | if (attr) | |
883 | return log_line_invalid_attr(rule_line, key); | |
884 | if (is_match || op == OP_REMOVE) | |
885 | return log_line_invalid_op(rule_line, key); | |
886 | if (op == OP_ADD) | |
887 | op = OP_ASSIGN; | |
888 | ||
889 | if (streq(value, "string_escape=none")) | |
890 | r = rule_line_add_token(rule_line, TK_A_OPTIONS_STRING_ESCAPE_NONE, op, NULL, NULL); | |
891 | else if (streq(value, "string_escape=replace")) | |
892 | r = rule_line_add_token(rule_line, TK_A_OPTIONS_STRING_ESCAPE_REPLACE, op, NULL, NULL); | |
893 | else if (streq(value, "db_persist")) | |
894 | r = rule_line_add_token(rule_line, TK_A_OPTIONS_DB_PERSIST, op, NULL, NULL); | |
895 | else if (streq(value, "watch")) | |
896 | r = rule_line_add_token(rule_line, TK_A_OPTIONS_INOTIFY_WATCH, op, NULL, INT_TO_PTR(1)); | |
897 | else if (streq(value, "nowatch")) | |
898 | r = rule_line_add_token(rule_line, TK_A_OPTIONS_INOTIFY_WATCH, op, NULL, INT_TO_PTR(0)); | |
899 | else if ((tmp = startswith(value, "static_node="))) | |
900 | r = rule_line_add_token(rule_line, TK_A_OPTIONS_STATIC_NODE, op, tmp, NULL); | |
901 | else if ((tmp = startswith(value, "link_priority="))) { | |
902 | int prio; | |
903 | ||
904 | r = safe_atoi(tmp, &prio); | |
905 | if (r < 0) | |
906 | return log_line_error_errno(rule_line, r, "Failed to parse link priority '%s': %m", tmp); | |
907 | r = rule_line_add_token(rule_line, TK_A_OPTIONS_DEVLINK_PRIORITY, op, NULL, INT_TO_PTR(prio)); | |
908 | } else if ((tmp = startswith(value, "log_level="))) { | |
909 | int level; | |
910 | ||
911 | if (streq(tmp, "reset")) | |
912 | level = -1; | |
913 | else { | |
914 | level = log_level_from_string(tmp); | |
915 | if (level < 0) | |
916 | return log_line_error_errno(rule_line, level, "Failed to parse log level '%s': %m", tmp); | |
917 | } | |
918 | r = rule_line_add_token(rule_line, TK_A_OPTIONS_LOG_LEVEL, op, NULL, INT_TO_PTR(level)); | |
919 | } else { | |
920 | log_line_warning(rule_line, "Invalid value for OPTIONS key, ignoring: '%s'", value); | |
921 | return 0; | |
922 | } | |
923 | } else if (streq(key, "OWNER")) { | |
924 | uid_t uid; | |
925 | ||
926 | if (attr) | |
927 | return log_line_invalid_attr(rule_line, key); | |
928 | if (is_match || op == OP_REMOVE) | |
929 | return log_line_invalid_op(rule_line, key); | |
930 | if (op == OP_ADD) { | |
931 | log_line_warning(rule_line, "%s key takes '=' or ':=' operator, assuming '='.", key); | |
932 | op = OP_ASSIGN; | |
933 | } | |
934 | ||
935 | if (parse_uid(value, &uid) >= 0) | |
936 | r = rule_line_add_token(rule_line, TK_A_OWNER_ID, op, NULL, UID_TO_PTR(uid)); | |
937 | else if (resolve_name_timing == RESOLVE_NAME_EARLY && | |
938 | rule_get_substitution_type(value) == SUBST_TYPE_PLAIN) { | |
939 | r = rule_resolve_user(rule_line, value, &uid); | |
940 | if (r < 0) | |
941 | return log_line_error_errno(rule_line, r, "Failed to resolve user name '%s': %m", value); | |
942 | ||
943 | r = rule_line_add_token(rule_line, TK_A_OWNER_ID, op, NULL, UID_TO_PTR(uid)); | |
944 | } else if (resolve_name_timing != RESOLVE_NAME_NEVER) { | |
945 | check_value_format_and_warn(rule_line, key, value, true); | |
946 | r = rule_line_add_token(rule_line, TK_A_OWNER, op, value, NULL); | |
947 | } else { | |
948 | log_line_debug(rule_line, "User name resolution is disabled, ignoring %s=\"%s\".", key, value); | |
949 | return 0; | |
950 | } | |
951 | } else if (streq(key, "GROUP")) { | |
952 | gid_t gid; | |
953 | ||
954 | if (attr) | |
955 | return log_line_invalid_attr(rule_line, key); | |
956 | if (is_match || op == OP_REMOVE) | |
957 | return log_line_invalid_op(rule_line, key); | |
958 | if (op == OP_ADD) { | |
959 | log_line_warning(rule_line, "%s key takes '=' or ':=' operator, assuming '='.", key); | |
960 | op = OP_ASSIGN; | |
961 | } | |
962 | ||
963 | if (parse_gid(value, &gid) >= 0) | |
964 | r = rule_line_add_token(rule_line, TK_A_GROUP_ID, op, NULL, GID_TO_PTR(gid)); | |
965 | else if (resolve_name_timing == RESOLVE_NAME_EARLY && | |
966 | rule_get_substitution_type(value) == SUBST_TYPE_PLAIN) { | |
967 | r = rule_resolve_group(rule_line, value, &gid); | |
968 | if (r < 0) | |
969 | return log_line_error_errno(rule_line, r, "Failed to resolve group name '%s': %m", value); | |
970 | ||
971 | r = rule_line_add_token(rule_line, TK_A_GROUP_ID, op, NULL, GID_TO_PTR(gid)); | |
972 | } else if (resolve_name_timing != RESOLVE_NAME_NEVER) { | |
973 | check_value_format_and_warn(rule_line, key, value, true); | |
974 | r = rule_line_add_token(rule_line, TK_A_GROUP, op, value, NULL); | |
975 | } else { | |
976 | log_line_debug(rule_line, "Resolving group name is disabled, ignoring GROUP=\"%s\".", value); | |
977 | return 0; | |
978 | } | |
979 | } else if (streq(key, "MODE")) { | |
980 | mode_t mode; | |
981 | ||
982 | if (attr) | |
983 | return log_line_invalid_attr(rule_line, key); | |
984 | if (is_match || op == OP_REMOVE) | |
985 | return log_line_invalid_op(rule_line, key); | |
986 | if (op == OP_ADD) { | |
987 | log_line_warning(rule_line, "%s key takes '=' or ':=' operator, assuming '='.", key); | |
988 | op = OP_ASSIGN; | |
989 | } | |
990 | ||
991 | if (parse_mode(value, &mode) >= 0) | |
992 | r = rule_line_add_token(rule_line, TK_A_MODE_ID, op, NULL, MODE_TO_PTR(mode)); | |
993 | else { | |
994 | check_value_format_and_warn(rule_line, key, value, true); | |
995 | r = rule_line_add_token(rule_line, TK_A_MODE, op, value, NULL); | |
996 | } | |
997 | } else if (streq(key, "SECLABEL")) { | |
998 | if (isempty(attr)) | |
999 | return log_line_invalid_attr(rule_line, key); | |
1000 | check_value_format_and_warn(rule_line, key, value, true); | |
1001 | if (is_match || op == OP_REMOVE) | |
1002 | return log_line_invalid_op(rule_line, key); | |
1003 | if (op == OP_ASSIGN_FINAL) { | |
1004 | log_line_warning(rule_line, "%s key takes '=' or '+=' operator, assuming '='.", key); | |
1005 | op = OP_ASSIGN; | |
1006 | } | |
1007 | ||
1008 | r = rule_line_add_token(rule_line, TK_A_SECLABEL, op, value, attr); | |
1009 | } else if (streq(key, "RUN")) { | |
1010 | if (is_match || op == OP_REMOVE) | |
1011 | return log_line_invalid_op(rule_line, key); | |
1012 | check_value_format_and_warn(rule_line, key, value, true); | |
1013 | if (!attr || streq(attr, "program")) | |
1014 | r = rule_line_add_token(rule_line, TK_A_RUN_PROGRAM, op, value, NULL); | |
1015 | else if (streq(attr, "builtin")) { | |
1016 | UdevBuiltinCommand cmd; | |
1017 | ||
1018 | cmd = udev_builtin_lookup(value); | |
1019 | if (cmd < 0) | |
1020 | return log_line_error_errno(rule_line, SYNTHETIC_ERRNO(EINVAL), | |
1021 | "Unknown builtin command '%s', ignoring.", value); | |
1022 | r = rule_line_add_token(rule_line, TK_A_RUN_BUILTIN, op, value, UDEV_BUILTIN_CMD_TO_PTR(cmd)); | |
1023 | } else | |
1024 | return log_line_invalid_attr(rule_line, key); | |
1025 | } else if (streq(key, "GOTO")) { | |
1026 | if (attr) | |
1027 | return log_line_invalid_attr(rule_line, key); | |
1028 | if (op != OP_ASSIGN) | |
1029 | return log_line_invalid_op(rule_line, key); | |
1030 | if (FLAGS_SET(rule_line->type, LINE_HAS_GOTO)) { | |
1031 | log_line_warning(rule_line, "Contains multiple GOTO keys, ignoring GOTO=\"%s\".", value); | |
1032 | return 0; | |
1033 | } | |
1034 | ||
1035 | rule_line->goto_label = value; | |
1036 | SET_FLAG(rule_line->type, LINE_HAS_GOTO, true); | |
1037 | return 1; | |
1038 | } else if (streq(key, "LABEL")) { | |
1039 | if (attr) | |
1040 | return log_line_invalid_attr(rule_line, key); | |
1041 | if (op != OP_ASSIGN) | |
1042 | return log_line_invalid_op(rule_line, key); | |
1043 | if (FLAGS_SET(rule_line->type, LINE_HAS_LABEL)) | |
1044 | log_line_warning(rule_line, "Contains multiple LABEL keys, ignoring LABEL=\"%s\".", | |
1045 | rule_line->label); | |
1046 | ||
1047 | rule_line->label = value; | |
1048 | SET_FLAG(rule_line->type, LINE_HAS_LABEL, true); | |
1049 | return 1; | |
1050 | } else | |
1051 | return log_line_error_errno(rule_line, SYNTHETIC_ERRNO(EINVAL), "Invalid key '%s'.", key); | |
1052 | if (r < 0) | |
1053 | return log_oom(); | |
1054 | ||
1055 | return 1; | |
1056 | } | |
1057 | ||
1058 | static UdevRuleOperatorType parse_operator(const char *op) { | |
1059 | assert(op); | |
1060 | ||
1061 | if (startswith(op, "==")) | |
1062 | return OP_MATCH; | |
1063 | if (startswith(op, "!=")) | |
1064 | return OP_NOMATCH; | |
1065 | if (startswith(op, "+=")) | |
1066 | return OP_ADD; | |
1067 | if (startswith(op, "-=")) | |
1068 | return OP_REMOVE; | |
1069 | if (startswith(op, "=")) | |
1070 | return OP_ASSIGN; | |
1071 | if (startswith(op, ":=")) | |
1072 | return OP_ASSIGN_FINAL; | |
1073 | ||
1074 | return _OP_TYPE_INVALID; | |
1075 | } | |
1076 | ||
1077 | static void check_token_delimiters(UdevRuleLine *rule_line, const char *line) { | |
1078 | assert(rule_line); | |
1079 | ||
1080 | size_t n_comma = 0; | |
1081 | bool ws_before_comma = false, ws_after_comma = false; | |
1082 | const char *p; | |
1083 | ||
1084 | for (p = line; !isempty(p); ++p) { | |
1085 | if (*p == ',') | |
1086 | ++n_comma; | |
1087 | else if (strchr(WHITESPACE, *p)) { | |
1088 | if (n_comma > 0) | |
1089 | ws_after_comma = true; | |
1090 | else | |
1091 | ws_before_comma = true; | |
1092 | } else | |
1093 | break; | |
1094 | } | |
1095 | ||
1096 | if (line == rule_line->line) { | |
1097 | /* this is the first token of the rule */ | |
1098 | if (n_comma > 0) | |
1099 | log_line_notice(rule_line, "style: stray leading comma."); | |
1100 | } else if (isempty(p)) { | |
1101 | /* there are no more tokens in the rule */ | |
1102 | if (n_comma > 0) | |
1103 | log_line_notice(rule_line, "style: stray trailing comma."); | |
1104 | } else { | |
1105 | /* single comma is expected */ | |
1106 | if (n_comma == 0) | |
1107 | log_line_notice(rule_line, "style: a comma between tokens is expected."); | |
1108 | else if (n_comma > 1) | |
1109 | log_line_notice(rule_line, "style: more than one comma between tokens."); | |
1110 | ||
1111 | /* whitespace after comma is expected */ | |
1112 | if (n_comma > 0) { | |
1113 | if (ws_before_comma) | |
1114 | log_line_notice(rule_line, "style: stray whitespace before comma."); | |
1115 | if (!ws_after_comma) | |
1116 | log_line_notice(rule_line, "style: whitespace after comma is expected."); | |
1117 | } else if (!ws_before_comma && !ws_after_comma) | |
1118 | log_line_notice(rule_line, "style: whitespace between tokens is expected."); | |
1119 | } | |
1120 | } | |
1121 | ||
1122 | int udev_rule_parse_value(char *str, char **ret_value, char **ret_endpos) { | |
1123 | char *i, *j; | |
1124 | bool is_escaped; | |
1125 | ||
1126 | /* value must be double quotated */ | |
1127 | is_escaped = str[0] == 'e'; | |
1128 | str += is_escaped; | |
1129 | if (str[0] != '"') | |
1130 | return -EINVAL; | |
1131 | ||
1132 | if (!is_escaped) { | |
1133 | /* unescape double quotation '\"'->'"' */ | |
1134 | for (j = str, i = str + 1; *i != '"'; i++, j++) { | |
1135 | if (*i == '\0') | |
1136 | return -EINVAL; | |
1137 | if (i[0] == '\\' && i[1] == '"') | |
1138 | i++; | |
1139 | *j = *i; | |
1140 | } | |
1141 | j[0] = '\0'; | |
1142 | /* | |
1143 | * The return value must be terminated by two subsequent NULs | |
1144 | * so it could be safely interpreted as nulstr. | |
1145 | */ | |
1146 | j[1] = '\0'; | |
1147 | } else { | |
1148 | _cleanup_free_ char *unescaped = NULL; | |
1149 | ssize_t l; | |
1150 | ||
1151 | /* find the end position of value */ | |
1152 | for (i = str + 1; *i != '"'; i++) { | |
1153 | if (i[0] == '\\') | |
1154 | i++; | |
1155 | if (*i == '\0') | |
1156 | return -EINVAL; | |
1157 | } | |
1158 | i[0] = '\0'; | |
1159 | ||
1160 | l = cunescape_length(str + 1, i - (str + 1), 0, &unescaped); | |
1161 | if (l < 0) | |
1162 | return l; | |
1163 | ||
1164 | assert(l <= i - (str + 1)); | |
1165 | memcpy(str, unescaped, l + 1); | |
1166 | /* | |
1167 | * The return value must be terminated by two subsequent NULs | |
1168 | * so it could be safely interpreted as nulstr. | |
1169 | */ | |
1170 | str[l + 1] = '\0'; | |
1171 | } | |
1172 | ||
1173 | *ret_value = str; | |
1174 | *ret_endpos = i + 1; | |
1175 | return 0; | |
1176 | } | |
1177 | ||
1178 | static int parse_line(char **line, char **ret_key, char **ret_attr, UdevRuleOperatorType *ret_op, char **ret_value) { | |
1179 | char *key_begin, *key_end, *attr, *tmp; | |
1180 | UdevRuleOperatorType op; | |
1181 | int r; | |
1182 | ||
1183 | assert(line); | |
1184 | assert(*line); | |
1185 | assert(ret_key); | |
1186 | assert(ret_op); | |
1187 | assert(ret_value); | |
1188 | ||
1189 | key_begin = skip_leading_chars(*line, WHITESPACE ","); | |
1190 | ||
1191 | if (isempty(key_begin)) | |
1192 | return 0; | |
1193 | ||
1194 | for (key_end = key_begin; ; key_end++) { | |
1195 | if (key_end[0] == '\0') | |
1196 | return -EINVAL; | |
1197 | if (strchr(WHITESPACE "={", key_end[0])) | |
1198 | break; | |
1199 | if (strchr("+-!:", key_end[0]) && key_end[1] == '=') | |
1200 | break; | |
1201 | } | |
1202 | if (key_end[0] == '{') { | |
1203 | attr = key_end + 1; | |
1204 | tmp = strchr(attr, '}'); | |
1205 | if (!tmp) | |
1206 | return -EINVAL; | |
1207 | *tmp++ = '\0'; | |
1208 | } else { | |
1209 | attr = NULL; | |
1210 | tmp = key_end; | |
1211 | } | |
1212 | ||
1213 | tmp = skip_leading_chars(tmp, NULL); | |
1214 | op = parse_operator(tmp); | |
1215 | if (op < 0) | |
1216 | return -EINVAL; | |
1217 | ||
1218 | key_end[0] = '\0'; | |
1219 | ||
1220 | tmp += op == OP_ASSIGN ? 1 : 2; | |
1221 | tmp = skip_leading_chars(tmp, NULL); | |
1222 | r = udev_rule_parse_value(tmp, ret_value, line); | |
1223 | if (r < 0) | |
1224 | return r; | |
1225 | ||
1226 | *ret_key = key_begin; | |
1227 | *ret_attr = attr; | |
1228 | *ret_op = op; | |
1229 | return 1; | |
1230 | } | |
1231 | ||
1232 | static void check_tokens_order(UdevRuleLine *rule_line) { | |
1233 | bool has_result = false; | |
1234 | ||
1235 | assert(rule_line); | |
1236 | ||
1237 | LIST_FOREACH(tokens, t, rule_line->tokens) | |
1238 | if (t->type == TK_M_RESULT) | |
1239 | has_result = true; | |
1240 | else if (has_result && t->type == TK_M_PROGRAM) { | |
1241 | log_line_warning(rule_line, "Reordering RESULT check after PROGRAM assignment."); | |
1242 | break; | |
1243 | } | |
1244 | } | |
1245 | ||
1246 | static void sort_tokens(UdevRuleLine *rule_line) { | |
1247 | assert(rule_line); | |
1248 | ||
1249 | UdevRuleToken *old_tokens = TAKE_PTR(rule_line->tokens); | |
1250 | ||
1251 | while (old_tokens) { | |
1252 | UdevRuleToken *min_token = NULL; | |
1253 | ||
1254 | LIST_FOREACH(tokens, t, old_tokens) | |
1255 | if (!min_token || min_token->type > t->type) | |
1256 | min_token = t; | |
1257 | ||
1258 | LIST_REMOVE(tokens, old_tokens, min_token); | |
1259 | LIST_APPEND(tokens, rule_line->tokens, min_token); | |
1260 | } | |
1261 | } | |
1262 | ||
1263 | static int rule_add_line(UdevRuleFile *rule_file, const char *line_str, unsigned line_nr, bool extra_checks) { | |
1264 | _cleanup_(udev_rule_line_freep) UdevRuleLine *rule_line = NULL; | |
1265 | _cleanup_free_ char *line = NULL; | |
1266 | char *p; | |
1267 | int r; | |
1268 | ||
1269 | assert(rule_file); | |
1270 | assert(line_str); | |
1271 | ||
1272 | if (isempty(line_str)) | |
1273 | return 0; | |
1274 | ||
1275 | line = strdup(line_str); | |
1276 | if (!line) | |
1277 | return log_oom(); | |
1278 | ||
1279 | rule_line = new(UdevRuleLine, 1); | |
1280 | if (!rule_line) | |
1281 | return log_oom(); | |
1282 | ||
1283 | *rule_line = (UdevRuleLine) { | |
1284 | .line = TAKE_PTR(line), | |
1285 | .line_number = line_nr, | |
1286 | .rule_file = rule_file, | |
1287 | }; | |
1288 | ||
1289 | LIST_APPEND(rule_lines, rule_file->rule_lines, rule_line); | |
1290 | ||
1291 | for (p = rule_line->line; !isempty(p); ) { | |
1292 | char *key, *attr, *value; | |
1293 | UdevRuleOperatorType op; | |
1294 | ||
1295 | if (extra_checks) | |
1296 | check_token_delimiters(rule_line, p); | |
1297 | ||
1298 | r = parse_line(&p, &key, &attr, &op, &value); | |
1299 | if (r < 0) | |
1300 | return log_line_error_errno(rule_line, r, "Invalid key/value pair, ignoring."); | |
1301 | if (r == 0) | |
1302 | break; | |
1303 | ||
1304 | r = parse_token(rule_line, key, attr, op, value); | |
1305 | if (r < 0) | |
1306 | return r; | |
1307 | } | |
1308 | ||
1309 | if (rule_line->type == 0) { | |
1310 | log_line_warning(rule_line, "The line has no effect, ignoring."); | |
1311 | return 0; | |
1312 | } | |
1313 | ||
1314 | if (extra_checks) | |
1315 | check_tokens_order(rule_line); | |
1316 | ||
1317 | sort_tokens(rule_line); | |
1318 | TAKE_PTR(rule_line); | |
1319 | return 0; | |
1320 | } | |
1321 | ||
1322 | static void rule_resolve_goto(UdevRuleFile *rule_file) { | |
1323 | assert(rule_file); | |
1324 | ||
1325 | /* link GOTOs to LABEL rules in this file to be able to fast-forward */ | |
1326 | LIST_FOREACH(rule_lines, line, rule_file->rule_lines) { | |
1327 | if (!FLAGS_SET(line->type, LINE_HAS_GOTO)) | |
1328 | continue; | |
1329 | ||
1330 | LIST_FOREACH(rule_lines, i, line->rule_lines_next) | |
1331 | if (streq_ptr(i->label, line->goto_label)) { | |
1332 | line->goto_line = i; | |
1333 | SET_FLAG(i->type, LINE_IS_REFERENCED, true); | |
1334 | break; | |
1335 | } | |
1336 | ||
1337 | if (!line->goto_line) { | |
1338 | log_line_error(line, "GOTO=\"%s\" has no matching label, ignoring.", | |
1339 | line->goto_label); | |
1340 | ||
1341 | SET_FLAG(line->type, LINE_HAS_GOTO, false); | |
1342 | line->goto_label = NULL; | |
1343 | ||
1344 | if ((line->type & ~(LINE_HAS_LABEL|LINE_IS_REFERENCED)) == 0) { | |
1345 | log_line_warning(line, "The line has no effect any more, dropping."); | |
1346 | /* LINE_IS_REFERENCED implies LINE_HAS_LABEL */ | |
1347 | if (line->type & LINE_HAS_LABEL) | |
1348 | udev_rule_line_clear_tokens(line); | |
1349 | else | |
1350 | udev_rule_line_free(line); | |
1351 | } | |
1352 | } | |
1353 | } | |
1354 | } | |
1355 | ||
1356 | static bool token_data_is_string(UdevRuleTokenType type) { | |
1357 | return IN_SET(type, TK_M_ENV, | |
1358 | TK_M_CONST, | |
1359 | TK_M_ATTR, | |
1360 | TK_M_SYSCTL, | |
1361 | TK_M_PARENTS_ATTR, | |
1362 | TK_A_SECLABEL, | |
1363 | TK_A_ENV, | |
1364 | TK_A_ATTR, | |
1365 | TK_A_SYSCTL); | |
1366 | } | |
1367 | ||
1368 | static bool token_type_and_data_eq(const UdevRuleToken *a, const UdevRuleToken *b) { | |
1369 | assert(a); | |
1370 | assert(b); | |
1371 | ||
1372 | return a->type == b->type && | |
1373 | (token_data_is_string(a->type) ? streq_ptr(a->data, b->data) : (a->data == b->data)); | |
1374 | } | |
1375 | ||
1376 | static bool nulstr_eq(const char *a, const char *b) { | |
1377 | NULSTR_FOREACH(i, a) | |
1378 | if (!nulstr_contains(b, i)) | |
1379 | return false; | |
1380 | ||
1381 | NULSTR_FOREACH(i, b) | |
1382 | if (!nulstr_contains(a, i)) | |
1383 | return false; | |
1384 | ||
1385 | return true; | |
1386 | } | |
1387 | ||
1388 | static bool token_type_and_value_eq(const UdevRuleToken *a, const UdevRuleToken *b) { | |
1389 | assert(a); | |
1390 | assert(b); | |
1391 | ||
1392 | if (a->type != b->type || | |
1393 | a->match_type != b->match_type) | |
1394 | return false; | |
1395 | ||
1396 | /* token value is ignored for certain match types */ | |
1397 | if (IN_SET(a->match_type, MATCH_TYPE_EMPTY, MATCH_TYPE_SUBSYSTEM)) | |
1398 | return true; | |
1399 | ||
1400 | return type_has_nulstr_value(a->type) ? nulstr_eq(a->value, b->value) : | |
1401 | streq_ptr(a->value, b->value); | |
1402 | } | |
1403 | ||
1404 | static bool conflicting_op(UdevRuleOperatorType a, UdevRuleOperatorType b) { | |
1405 | return (a == OP_MATCH && b == OP_NOMATCH) || | |
1406 | (a == OP_NOMATCH && b == OP_MATCH); | |
1407 | } | |
1408 | ||
1409 | /* test whether all fields besides UdevRuleOperatorType of two tokens match */ | |
1410 | static bool tokens_eq(const UdevRuleToken *a, const UdevRuleToken *b) { | |
1411 | assert(a); | |
1412 | assert(b); | |
1413 | ||
1414 | return a->attr_subst_type == b->attr_subst_type && | |
1415 | a->attr_match_remove_trailing_whitespace == b->attr_match_remove_trailing_whitespace && | |
1416 | token_type_and_value_eq(a, b) && | |
1417 | token_type_and_data_eq(a, b); | |
1418 | } | |
1419 | ||
1420 | static bool nulstr_tokens_conflict(const UdevRuleToken *a, const UdevRuleToken *b) { | |
1421 | assert(a); | |
1422 | assert(b); | |
1423 | ||
1424 | if (!(a->type == b->type && | |
1425 | type_has_nulstr_value(a->type) && | |
1426 | a->op == b->op && | |
1427 | a->op == OP_MATCH && | |
1428 | a->match_type == b->match_type && | |
1429 | a->attr_subst_type == b->attr_subst_type && | |
1430 | a->attr_match_remove_trailing_whitespace == b->attr_match_remove_trailing_whitespace && | |
1431 | token_type_and_data_eq(a, b))) | |
1432 | return false; | |
1433 | ||
1434 | if (a->match_type == MATCH_TYPE_PLAIN) { | |
1435 | NULSTR_FOREACH(i, a->value) | |
1436 | if (nulstr_contains(b->value, i)) | |
1437 | return false; | |
1438 | return true; | |
1439 | } | |
1440 | ||
1441 | if (a->match_type == MATCH_TYPE_GLOB) { | |
1442 | NULSTR_FOREACH(i, a->value) { | |
1443 | size_t i_n = strcspn(i, GLOB_CHARS); | |
1444 | if (i_n == 0) | |
1445 | return false; | |
1446 | NULSTR_FOREACH(j, b->value) { | |
1447 | size_t j_n = strcspn(j, GLOB_CHARS); | |
1448 | if (j_n == 0 || strneq(i, j, MIN(i_n, j_n))) | |
1449 | return false; | |
1450 | } | |
1451 | ||
1452 | } | |
1453 | return true; | |
1454 | } | |
1455 | ||
1456 | return false; | |
1457 | } | |
1458 | ||
1459 | static void udev_check_unused_labels(UdevRuleLine *line) { | |
1460 | assert(line); | |
1461 | ||
1462 | if (FLAGS_SET(line->type, LINE_HAS_LABEL) && | |
1463 | !FLAGS_SET(line->type, LINE_IS_REFERENCED)) | |
1464 | log_line_notice(line, "style: LABEL=\"%s\" is unused.", line->label); | |
1465 | } | |
1466 | ||
1467 | static void udev_check_conflicts_duplicates(UdevRuleLine *line) { | |
1468 | assert(line); | |
1469 | ||
1470 | bool conflicts = false, duplicates = false; | |
1471 | ||
1472 | LIST_FOREACH(tokens, token, line->tokens) | |
1473 | LIST_FOREACH(tokens, i, token->tokens_next) { | |
1474 | bool new_conflicts = false, new_duplicates = false; | |
1475 | ||
1476 | if (tokens_eq(token, i)) { | |
1477 | if (!duplicates && token->op == i->op) | |
1478 | new_duplicates = true; | |
1479 | if (!conflicts && conflicting_op(token->op, i->op)) | |
1480 | new_conflicts = true; | |
1481 | } else if (!conflicts && nulstr_tokens_conflict(token, i)) | |
1482 | new_conflicts = true; | |
1483 | else | |
1484 | continue; | |
1485 | ||
1486 | if (new_duplicates) { | |
1487 | duplicates = new_duplicates; | |
1488 | log_line_warning(line, "duplicate expressions."); | |
1489 | } | |
1490 | if (new_conflicts) { | |
1491 | conflicts = new_conflicts; | |
1492 | log_line_error(line, "conflicting match expressions, the line has no effect."); | |
1493 | } | |
1494 | if (conflicts && duplicates) | |
1495 | return; | |
1496 | } | |
1497 | } | |
1498 | ||
1499 | static void udev_check_rule_line(UdevRuleLine *line) { | |
1500 | udev_check_unused_labels(line); | |
1501 | udev_check_conflicts_duplicates(line); | |
1502 | } | |
1503 | ||
1504 | int udev_rules_parse_file(UdevRules *rules, const char *filename, bool extra_checks, UdevRuleFile **ret) { | |
1505 | _cleanup_(udev_rule_file_freep) UdevRuleFile *rule_file = NULL; | |
1506 | _cleanup_free_ char *continuation = NULL, *name = NULL; | |
1507 | _cleanup_fclose_ FILE *f = NULL; | |
1508 | bool ignore_line = false; | |
1509 | unsigned line_nr = 0; | |
1510 | struct stat st; | |
1511 | int r; | |
1512 | ||
1513 | assert(rules); | |
1514 | assert(filename); | |
1515 | ||
1516 | f = fopen(filename, "re"); | |
1517 | if (!f) { | |
1518 | if (extra_checks) | |
1519 | return -errno; | |
1520 | ||
1521 | if (errno == ENOENT) | |
1522 | return 0; | |
1523 | ||
1524 | return log_warning_errno(errno, "Failed to open %s, ignoring: %m", filename); | |
1525 | } | |
1526 | ||
1527 | if (fstat(fileno(f), &st) < 0) | |
1528 | return log_warning_errno(errno, "Failed to stat %s, ignoring: %m", filename); | |
1529 | ||
1530 | if (null_or_empty(&st)) { | |
1531 | log_debug("Skipping empty file: %s", filename); | |
1532 | if (ret) | |
1533 | *ret = NULL; | |
1534 | return 0; | |
1535 | } | |
1536 | ||
1537 | r = hashmap_put_stats_by_path(&rules->stats_by_path, filename, &st); | |
1538 | if (r < 0) | |
1539 | return log_warning_errno(r, "Failed to save stat for %s, ignoring: %m", filename); | |
1540 | ||
1541 | (void) fd_warn_permissions(filename, fileno(f)); | |
1542 | ||
1543 | log_debug("Reading rules file: %s", filename); | |
1544 | ||
1545 | name = strdup(filename); | |
1546 | if (!name) | |
1547 | return log_oom(); | |
1548 | ||
1549 | rule_file = new(UdevRuleFile, 1); | |
1550 | if (!rule_file) | |
1551 | return log_oom(); | |
1552 | ||
1553 | *rule_file = (UdevRuleFile) { | |
1554 | .filename = TAKE_PTR(name), | |
1555 | .rules = rules, | |
1556 | }; | |
1557 | ||
1558 | LIST_APPEND(rule_files, rules->rule_files, rule_file); | |
1559 | ||
1560 | for (;;) { | |
1561 | _cleanup_free_ char *buf = NULL; | |
1562 | size_t len; | |
1563 | char *line; | |
1564 | ||
1565 | r = read_line(f, UDEV_LINE_SIZE, &buf); | |
1566 | if (r < 0) | |
1567 | return r; | |
1568 | if (r == 0) | |
1569 | break; | |
1570 | ||
1571 | line_nr++; | |
1572 | line = skip_leading_chars(buf, NULL); | |
1573 | ||
1574 | /* Lines beginning with '#' are ignored regardless of line continuation. */ | |
1575 | if (line[0] == '#') | |
1576 | continue; | |
1577 | ||
1578 | len = strlen(line); | |
1579 | ||
1580 | if (continuation && !ignore_line) { | |
1581 | if (strlen(continuation) + len >= UDEV_LINE_SIZE) | |
1582 | ignore_line = true; | |
1583 | ||
1584 | if (!strextend(&continuation, line)) | |
1585 | return log_oom(); | |
1586 | ||
1587 | if (!ignore_line) { | |
1588 | line = continuation; | |
1589 | len = strlen(line); | |
1590 | } | |
1591 | } | |
1592 | ||
1593 | if (len > 0 && line[len - 1] == '\\') { | |
1594 | if (ignore_line) | |
1595 | continue; | |
1596 | ||
1597 | line[len - 1] = '\0'; | |
1598 | if (!continuation) { | |
1599 | continuation = strdup(line); | |
1600 | if (!continuation) | |
1601 | return log_oom(); | |
1602 | } | |
1603 | ||
1604 | continue; | |
1605 | } | |
1606 | ||
1607 | if (ignore_line) | |
1608 | log_file_error(rule_file, line_nr, "Line is too long, ignored."); | |
1609 | else if (len > 0) | |
1610 | (void) rule_add_line(rule_file, line, line_nr, extra_checks); | |
1611 | ||
1612 | continuation = mfree(continuation); | |
1613 | ignore_line = false; | |
1614 | } | |
1615 | ||
1616 | if (continuation) | |
1617 | log_file_error(rule_file, line_nr, | |
1618 | "Unexpected EOF after line continuation, line ignored."); | |
1619 | ||
1620 | rule_resolve_goto(rule_file); | |
1621 | ||
1622 | if (extra_checks) | |
1623 | LIST_FOREACH(rule_lines, line, rule_file->rule_lines) | |
1624 | udev_check_rule_line(line); | |
1625 | ||
1626 | if (ret) | |
1627 | *ret = rule_file; | |
1628 | ||
1629 | TAKE_PTR(rule_file); | |
1630 | return 1; | |
1631 | } | |
1632 | ||
1633 | unsigned udev_rule_file_get_issues(UdevRuleFile *rule_file) { | |
1634 | assert(rule_file); | |
1635 | ||
1636 | return rule_file->issues; | |
1637 | } | |
1638 | ||
1639 | UdevRules* udev_rules_new(ResolveNameTiming resolve_name_timing) { | |
1640 | assert(resolve_name_timing >= 0 && resolve_name_timing < _RESOLVE_NAME_TIMING_MAX); | |
1641 | ||
1642 | UdevRules *rules = new(UdevRules, 1); | |
1643 | if (!rules) | |
1644 | return NULL; | |
1645 | ||
1646 | *rules = (UdevRules) { | |
1647 | .resolve_name_timing = resolve_name_timing, | |
1648 | }; | |
1649 | ||
1650 | return rules; | |
1651 | } | |
1652 | ||
1653 | int udev_rules_load(UdevRules **ret_rules, ResolveNameTiming resolve_name_timing) { | |
1654 | _cleanup_(udev_rules_freep) UdevRules *rules = NULL; | |
1655 | _cleanup_strv_free_ char **files = NULL; | |
1656 | int r; | |
1657 | ||
1658 | rules = udev_rules_new(resolve_name_timing); | |
1659 | if (!rules) | |
1660 | return -ENOMEM; | |
1661 | ||
1662 | r = conf_files_list_strv(&files, ".rules", NULL, 0, RULES_DIRS); | |
1663 | if (r < 0) | |
1664 | return log_debug_errno(r, "Failed to enumerate rules files: %m"); | |
1665 | ||
1666 | STRV_FOREACH(f, files) { | |
1667 | r = udev_rules_parse_file(rules, *f, /* extra_checks = */ false, NULL); | |
1668 | if (r < 0) | |
1669 | log_debug_errno(r, "Failed to read rules file %s, ignoring: %m", *f); | |
1670 | } | |
1671 | ||
1672 | *ret_rules = TAKE_PTR(rules); | |
1673 | return 0; | |
1674 | } | |
1675 | ||
1676 | bool udev_rules_should_reload(UdevRules *rules) { | |
1677 | _cleanup_hashmap_free_ Hashmap *stats_by_path = NULL; | |
1678 | int r; | |
1679 | ||
1680 | if (!rules) | |
1681 | return true; | |
1682 | ||
1683 | r = config_get_stats_by_path(".rules", NULL, 0, RULES_DIRS, /* check_dropins = */ false, &stats_by_path); | |
1684 | if (r < 0) { | |
1685 | log_warning_errno(r, "Failed to get stats of udev rules, ignoring: %m"); | |
1686 | return true; | |
1687 | } | |
1688 | ||
1689 | if (!stats_by_path_equal(rules->stats_by_path, stats_by_path)) { | |
1690 | log_debug("Udev rules need reloading"); | |
1691 | return true; | |
1692 | } | |
1693 | ||
1694 | return false; | |
1695 | } | |
1696 | ||
1697 | static bool token_match_string(UdevRuleToken *token, const char *str) { | |
1698 | const char *value; | |
1699 | bool match = false; | |
1700 | ||
1701 | assert(token); | |
1702 | assert(token->value); | |
1703 | assert(token->type < _TK_M_MAX); | |
1704 | ||
1705 | str = strempty(str); | |
1706 | value = token->value; | |
1707 | ||
1708 | switch (token->match_type) { | |
1709 | case MATCH_TYPE_EMPTY: | |
1710 | match = isempty(str); | |
1711 | break; | |
1712 | case MATCH_TYPE_SUBSYSTEM: | |
1713 | match = STR_IN_SET(str, "subsystem", "class", "bus"); | |
1714 | break; | |
1715 | case MATCH_TYPE_PLAIN_WITH_EMPTY: | |
1716 | if (isempty(str)) { | |
1717 | match = true; | |
1718 | break; | |
1719 | } | |
1720 | _fallthrough_; | |
1721 | case MATCH_TYPE_PLAIN: | |
1722 | NULSTR_FOREACH(i, value) | |
1723 | if (streq(i, str)) { | |
1724 | match = true; | |
1725 | break; | |
1726 | } | |
1727 | break; | |
1728 | case MATCH_TYPE_GLOB_WITH_EMPTY: | |
1729 | if (isempty(str)) { | |
1730 | match = true; | |
1731 | break; | |
1732 | } | |
1733 | _fallthrough_; | |
1734 | case MATCH_TYPE_GLOB: | |
1735 | NULSTR_FOREACH(i, value) | |
1736 | if ((fnmatch(i, str, 0) == 0)) { | |
1737 | match = true; | |
1738 | break; | |
1739 | } | |
1740 | break; | |
1741 | default: | |
1742 | assert_not_reached(); | |
1743 | } | |
1744 | ||
1745 | return token->op == (match ? OP_MATCH : OP_NOMATCH); | |
1746 | } | |
1747 | ||
1748 | static bool token_match_attr(UdevRuleToken *token, sd_device *dev, UdevEvent *event) { | |
1749 | char nbuf[UDEV_NAME_SIZE], vbuf[UDEV_NAME_SIZE]; | |
1750 | const char *name, *value; | |
1751 | bool truncated; | |
1752 | ||
1753 | assert(token); | |
1754 | assert(IN_SET(token->type, TK_M_ATTR, TK_M_PARENTS_ATTR)); | |
1755 | assert(dev); | |
1756 | assert(event); | |
1757 | ||
1758 | name = token->data; | |
1759 | ||
1760 | switch (token->attr_subst_type) { | |
1761 | case SUBST_TYPE_FORMAT: | |
1762 | (void) udev_event_apply_format(event, name, nbuf, sizeof(nbuf), false, &truncated); | |
1763 | if (truncated) { | |
1764 | log_event_truncated(dev, token, "sysfs attribute name", name, | |
1765 | token->type == TK_M_ATTR ? "ATTR" : "ATTRS", /* is_match = */ true); | |
1766 | return false; | |
1767 | } | |
1768 | ||
1769 | name = nbuf; | |
1770 | _fallthrough_; | |
1771 | case SUBST_TYPE_PLAIN: | |
1772 | if (sd_device_get_sysattr_value(dev, name, &value) < 0) | |
1773 | return false; | |
1774 | break; | |
1775 | case SUBST_TYPE_SUBSYS: | |
1776 | if (udev_resolve_subsys_kernel(name, vbuf, sizeof(vbuf), true) < 0) | |
1777 | return false; | |
1778 | value = vbuf; | |
1779 | break; | |
1780 | default: | |
1781 | assert_not_reached(); | |
1782 | } | |
1783 | ||
1784 | /* remove trailing whitespace, if not asked to match for it */ | |
1785 | if (token->attr_match_remove_trailing_whitespace) { | |
1786 | if (value != vbuf) { | |
1787 | strscpy(vbuf, sizeof(vbuf), value); | |
1788 | value = vbuf; | |
1789 | } | |
1790 | ||
1791 | delete_trailing_chars(vbuf, NULL); | |
1792 | } | |
1793 | ||
1794 | return token_match_string(token, value); | |
1795 | } | |
1796 | ||
1797 | static int get_property_from_string(char *line, char **ret_key, char **ret_value) { | |
1798 | char *key, *val; | |
1799 | size_t len; | |
1800 | ||
1801 | assert(line); | |
1802 | assert(ret_key); | |
1803 | assert(ret_value); | |
1804 | ||
1805 | /* find key */ | |
1806 | key = skip_leading_chars(line, NULL); | |
1807 | ||
1808 | /* comment or empty line */ | |
1809 | if (IN_SET(key[0], '#', '\0')) { | |
1810 | *ret_key = *ret_value = NULL; | |
1811 | return 0; | |
1812 | } | |
1813 | ||
1814 | /* split key/value */ | |
1815 | val = strchr(key, '='); | |
1816 | if (!val) | |
1817 | return -EINVAL; | |
1818 | *val++ = '\0'; | |
1819 | ||
1820 | key = strstrip(key); | |
1821 | if (isempty(key)) | |
1822 | return -EINVAL; | |
1823 | ||
1824 | val = strstrip(val); | |
1825 | if (isempty(val)) | |
1826 | return -EINVAL; | |
1827 | ||
1828 | /* unquote */ | |
1829 | if (IN_SET(val[0], '"', '\'')) { | |
1830 | len = strlen(val); | |
1831 | if (len == 1 || val[len-1] != val[0]) | |
1832 | return -EINVAL; | |
1833 | val[len-1] = '\0'; | |
1834 | val++; | |
1835 | } | |
1836 | ||
1837 | *ret_key = key; | |
1838 | *ret_value = val; | |
1839 | return 1; | |
1840 | } | |
1841 | ||
1842 | static int import_parent_into_properties(sd_device *dev, const char *filter) { | |
1843 | sd_device *parent; | |
1844 | int r; | |
1845 | ||
1846 | assert(dev); | |
1847 | assert(filter); | |
1848 | ||
1849 | r = sd_device_get_parent(dev, &parent); | |
1850 | if (r == -ENOENT) | |
1851 | return 0; | |
1852 | if (r < 0) | |
1853 | return r; | |
1854 | ||
1855 | FOREACH_DEVICE_PROPERTY(parent, key, val) { | |
1856 | if (fnmatch(filter, key, 0) != 0) | |
1857 | continue; | |
1858 | r = device_add_property(dev, key, val); | |
1859 | if (r < 0) | |
1860 | return r; | |
1861 | } | |
1862 | ||
1863 | return 1; | |
1864 | } | |
1865 | ||
1866 | static int attr_subst_subdir(char attr[static UDEV_PATH_SIZE]) { | |
1867 | _cleanup_closedir_ DIR *dir = NULL; | |
1868 | char buf[UDEV_PATH_SIZE], *p; | |
1869 | const char *tail; | |
1870 | size_t len, size; | |
1871 | bool truncated; | |
1872 | ||
1873 | assert(attr); | |
1874 | ||
1875 | tail = strstr(attr, "/*/"); | |
1876 | if (!tail) | |
1877 | return 0; | |
1878 | ||
1879 | len = tail - attr + 1; /* include slash at the end */ | |
1880 | tail += 2; /* include slash at the beginning */ | |
1881 | ||
1882 | p = buf; | |
1883 | size = sizeof(buf); | |
1884 | size -= strnpcpy_full(&p, size, attr, len, &truncated); | |
1885 | if (truncated) | |
1886 | return -ENOENT; | |
1887 | ||
1888 | dir = opendir(buf); | |
1889 | if (!dir) | |
1890 | return -errno; | |
1891 | ||
1892 | FOREACH_DIRENT_ALL(de, dir, break) { | |
1893 | if (de->d_name[0] == '.') | |
1894 | continue; | |
1895 | ||
1896 | strscpyl_full(p, size, &truncated, de->d_name, tail, NULL); | |
1897 | if (truncated) | |
1898 | continue; | |
1899 | ||
1900 | if (faccessat(dirfd(dir), p, F_OK, 0) < 0) | |
1901 | continue; | |
1902 | ||
1903 | strcpy(attr, buf); | |
1904 | return 0; | |
1905 | } | |
1906 | ||
1907 | return -ENOENT; | |
1908 | } | |
1909 | ||
1910 | static size_t udev_replace_ifname(char *str) { | |
1911 | size_t replaced = 0; | |
1912 | ||
1913 | assert(str); | |
1914 | ||
1915 | /* See ifname_valid_full(). */ | |
1916 | ||
1917 | for (char *p = str; *p != '\0'; p++) | |
1918 | if (!ifname_valid_char(*p)) { | |
1919 | *p = '_'; | |
1920 | replaced++; | |
1921 | } | |
1922 | ||
1923 | return replaced; | |
1924 | } | |
1925 | ||
1926 | static int udev_rule_apply_token_to_event( | |
1927 | UdevRuleToken *token, | |
1928 | sd_device *dev, | |
1929 | UdevEvent *event) { | |
1930 | ||
1931 | int r; | |
1932 | ||
1933 | assert(token); | |
1934 | assert(dev); | |
1935 | assert(event); | |
1936 | ||
1937 | /* This returns the following values: | |
1938 | * 0 on the current token does not match the event, | |
1939 | * 1 on the current token matches the event, and | |
1940 | * negative errno on some critical errors. */ | |
1941 | ||
1942 | switch (token->type) { | |
1943 | case TK_M_ACTION: { | |
1944 | sd_device_action_t a; | |
1945 | ||
1946 | r = sd_device_get_action(dev, &a); | |
1947 | if (r < 0) | |
1948 | return log_event_error_errno(dev, token, r, "Failed to get uevent action type: %m"); | |
1949 | ||
1950 | return token_match_string(token, device_action_to_string(a)); | |
1951 | } | |
1952 | case TK_M_DEVPATH: { | |
1953 | const char *val; | |
1954 | ||
1955 | r = sd_device_get_devpath(dev, &val); | |
1956 | if (r < 0) | |
1957 | return log_event_error_errno(dev, token, r, "Failed to get devpath: %m"); | |
1958 | ||
1959 | return token_match_string(token, val); | |
1960 | } | |
1961 | case TK_M_KERNEL: | |
1962 | case TK_M_PARENTS_KERNEL: { | |
1963 | const char *val; | |
1964 | ||
1965 | r = sd_device_get_sysname(dev, &val); | |
1966 | if (r < 0) | |
1967 | return log_event_error_errno(dev, token, r, "Failed to get sysname: %m"); | |
1968 | ||
1969 | return token_match_string(token, val); | |
1970 | } | |
1971 | case TK_M_DEVLINK: | |
1972 | FOREACH_DEVICE_DEVLINK(dev, val) | |
1973 | if (token_match_string(token, strempty(startswith(val, "/dev/"))) == (token->op == OP_MATCH)) | |
1974 | return token->op == OP_MATCH; | |
1975 | return token->op == OP_NOMATCH; | |
1976 | case TK_M_NAME: | |
1977 | return token_match_string(token, event->name); | |
1978 | case TK_M_ENV: { | |
1979 | const char *val = NULL; | |
1980 | ||
1981 | (void) device_get_property_value_with_fallback(dev, token->data, event->worker ? event->worker->properties : NULL, &val); | |
1982 | ||
1983 | return token_match_string(token, val); | |
1984 | } | |
1985 | case TK_M_CONST: { | |
1986 | const char *val, *k = token->data; | |
1987 | ||
1988 | if (streq(k, "arch")) | |
1989 | val = architecture_to_string(uname_architecture()); | |
1990 | else if (streq(k, "virt")) | |
1991 | val = virtualization_to_string(detect_virtualization()); | |
1992 | else if (streq(k, "cvm")) | |
1993 | val = confidential_virtualization_to_string(detect_confidential_virtualization()); | |
1994 | else | |
1995 | assert_not_reached(); | |
1996 | return token_match_string(token, val); | |
1997 | } | |
1998 | case TK_M_TAG: | |
1999 | case TK_M_PARENTS_TAG: | |
2000 | FOREACH_DEVICE_CURRENT_TAG(dev, val) | |
2001 | if (token_match_string(token, val) == (token->op == OP_MATCH)) | |
2002 | return token->op == OP_MATCH; | |
2003 | return token->op == OP_NOMATCH; | |
2004 | case TK_M_SUBSYSTEM: | |
2005 | case TK_M_PARENTS_SUBSYSTEM: { | |
2006 | const char *val; | |
2007 | ||
2008 | r = sd_device_get_subsystem(dev, &val); | |
2009 | if (r == -ENOENT) | |
2010 | val = NULL; | |
2011 | else if (r < 0) | |
2012 | return log_event_error_errno(dev, token, r, "Failed to get subsystem: %m"); | |
2013 | ||
2014 | return token_match_string(token, val); | |
2015 | } | |
2016 | case TK_M_DRIVER: | |
2017 | case TK_M_PARENTS_DRIVER: { | |
2018 | const char *val; | |
2019 | ||
2020 | r = sd_device_get_driver(dev, &val); | |
2021 | if (r == -ENOENT) | |
2022 | val = NULL; | |
2023 | else if (r < 0) | |
2024 | return log_event_error_errno(dev, token, r, "Failed to get driver: %m"); | |
2025 | ||
2026 | return token_match_string(token, val); | |
2027 | } | |
2028 | case TK_M_ATTR: | |
2029 | case TK_M_PARENTS_ATTR: | |
2030 | return token_match_attr(token, dev, event); | |
2031 | case TK_M_SYSCTL: { | |
2032 | _cleanup_free_ char *value = NULL; | |
2033 | char buf[UDEV_PATH_SIZE]; | |
2034 | bool truncated; | |
2035 | ||
2036 | (void) udev_event_apply_format(event, token->data, buf, sizeof(buf), false, &truncated); | |
2037 | if (truncated) { | |
2038 | log_event_truncated(dev, token, "sysctl entry name", token->data, "SYSCTL", /* is_match = */ true); | |
2039 | return false; | |
2040 | } | |
2041 | ||
2042 | r = sysctl_read(sysctl_normalize(buf), &value); | |
2043 | if (r < 0 && r != -ENOENT) | |
2044 | return log_event_error_errno(dev, token, r, "Failed to read sysctl '%s': %m", buf); | |
2045 | ||
2046 | return token_match_string(token, strstrip(value)); | |
2047 | } | |
2048 | case TK_M_TEST: { | |
2049 | mode_t mode = PTR_TO_MODE(token->data); | |
2050 | char buf[UDEV_PATH_SIZE]; | |
2051 | struct stat statbuf; | |
2052 | bool match, truncated; | |
2053 | ||
2054 | (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false, &truncated); | |
2055 | if (truncated) { | |
2056 | log_event_truncated(dev, token, "file name", token->value, "TEST", /* is_match = */ true); | |
2057 | return false; | |
2058 | } | |
2059 | ||
2060 | if (!path_is_absolute(buf) && | |
2061 | udev_resolve_subsys_kernel(buf, buf, sizeof(buf), false) < 0) { | |
2062 | char tmp[UDEV_PATH_SIZE]; | |
2063 | const char *val; | |
2064 | ||
2065 | r = sd_device_get_syspath(dev, &val); | |
2066 | if (r < 0) | |
2067 | return log_event_error_errno(dev, token, r, "Failed to get syspath: %m"); | |
2068 | ||
2069 | strscpy_full(tmp, sizeof(tmp), buf, &truncated); | |
2070 | assert(!truncated); | |
2071 | strscpyl_full(buf, sizeof(buf), &truncated, val, "/", tmp, NULL); | |
2072 | if (truncated) | |
2073 | return false; | |
2074 | } | |
2075 | ||
2076 | r = attr_subst_subdir(buf); | |
2077 | if (r == -ENOENT) | |
2078 | return token->op == OP_NOMATCH; | |
2079 | if (r < 0) | |
2080 | return log_event_error_errno(dev, token, r, "Failed to test for the existence of '%s': %m", buf); | |
2081 | ||
2082 | if (stat(buf, &statbuf) < 0) | |
2083 | return token->op == OP_NOMATCH; | |
2084 | ||
2085 | if (mode == MODE_INVALID) | |
2086 | return token->op == OP_MATCH; | |
2087 | ||
2088 | match = (statbuf.st_mode & mode) > 0; | |
2089 | return token->op == (match ? OP_MATCH : OP_NOMATCH); | |
2090 | } | |
2091 | case TK_M_PROGRAM: { | |
2092 | char buf[UDEV_LINE_SIZE], result[UDEV_LINE_SIZE]; | |
2093 | bool truncated; | |
2094 | size_t count; | |
2095 | ||
2096 | event->program_result = mfree(event->program_result); | |
2097 | (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false, &truncated); | |
2098 | if (truncated) { | |
2099 | log_event_truncated(dev, token, "command", token->value, "PROGRAM", /* is_match = */ true); | |
2100 | return false; | |
2101 | } | |
2102 | ||
2103 | log_event_debug(dev, token, "Running PROGRAM=\"%s\"", buf); | |
2104 | ||
2105 | r = udev_event_spawn(event, /* accept_failure = */ true, buf, result, sizeof(result), NULL); | |
2106 | if (r != 0) { | |
2107 | if (r < 0) | |
2108 | log_event_warning_errno(dev, token, r, "Failed to execute \"%s\": %m", buf); | |
2109 | else /* returned value is positive when program fails */ | |
2110 | log_event_debug(dev, token, "Command \"%s\" returned %d (error)", buf, r); | |
2111 | return token->op == OP_NOMATCH; | |
2112 | } | |
2113 | ||
2114 | delete_trailing_chars(result, "\n"); | |
2115 | count = udev_replace_chars(result, UDEV_ALLOWED_CHARS_INPUT); | |
2116 | if (count > 0) | |
2117 | log_event_debug(dev, token, | |
2118 | "Replaced %zu character(s) in result of \"%s\"", | |
2119 | count, buf); | |
2120 | ||
2121 | event->program_result = strdup(result); | |
2122 | return token->op == OP_MATCH; | |
2123 | } | |
2124 | case TK_M_IMPORT_FILE: { | |
2125 | _cleanup_fclose_ FILE *f = NULL; | |
2126 | char buf[UDEV_PATH_SIZE]; | |
2127 | bool truncated; | |
2128 | ||
2129 | (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false, &truncated); | |
2130 | if (truncated) { | |
2131 | log_event_truncated(dev, token, "file name to be imported", token->value, "IMPORT", /* is_match = */ true); | |
2132 | return false; | |
2133 | } | |
2134 | ||
2135 | log_event_debug(dev, token, "Importing properties from '%s'", buf); | |
2136 | ||
2137 | f = fopen(buf, "re"); | |
2138 | if (!f) { | |
2139 | if (errno != ENOENT) | |
2140 | return log_event_error_errno(dev, token, errno, "Failed to open '%s': %m", buf); | |
2141 | return token->op == OP_NOMATCH; | |
2142 | } | |
2143 | ||
2144 | for (;;) { | |
2145 | _cleanup_free_ char *line = NULL; | |
2146 | char *key, *value; | |
2147 | ||
2148 | r = read_line(f, LONG_LINE_MAX, &line); | |
2149 | if (r < 0) { | |
2150 | log_event_debug_errno(dev, token, r, "Failed to read '%s', ignoring: %m", buf); | |
2151 | return token->op == OP_NOMATCH; | |
2152 | } | |
2153 | if (r == 0) | |
2154 | break; | |
2155 | ||
2156 | r = get_property_from_string(line, &key, &value); | |
2157 | if (r < 0) { | |
2158 | log_event_debug_errno(dev, token, r, | |
2159 | "Failed to parse key and value from '%s', ignoring: %m", | |
2160 | line); | |
2161 | continue; | |
2162 | } | |
2163 | if (r == 0) | |
2164 | continue; | |
2165 | ||
2166 | r = device_add_property(dev, key, value); | |
2167 | if (r < 0) | |
2168 | return log_event_error_errno(dev, token, r, | |
2169 | "Failed to add property %s=%s: %m", | |
2170 | key, value); | |
2171 | } | |
2172 | ||
2173 | return token->op == OP_MATCH; | |
2174 | } | |
2175 | case TK_M_IMPORT_PROGRAM: { | |
2176 | _cleanup_strv_free_ char **lines = NULL; | |
2177 | char buf[UDEV_LINE_SIZE], result[UDEV_LINE_SIZE]; | |
2178 | bool truncated; | |
2179 | ||
2180 | (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false, &truncated); | |
2181 | if (truncated) { | |
2182 | log_event_truncated(dev, token, "command", token->value, "IMPORT", /* is_match = */ true); | |
2183 | return false; | |
2184 | } | |
2185 | ||
2186 | log_event_debug(dev, token, "Importing properties from results of '%s'", buf); | |
2187 | ||
2188 | r = udev_event_spawn(event, /* accept_failure = */ true, buf, result, sizeof result, &truncated); | |
2189 | if (r != 0) { | |
2190 | if (r < 0) | |
2191 | log_event_warning_errno(dev, token, r, "Failed to execute '%s', ignoring: %m", buf); | |
2192 | else /* returned value is positive when program fails */ | |
2193 | log_event_debug(dev, token, "Command \"%s\" returned %d (error), ignoring", buf, r); | |
2194 | return token->op == OP_NOMATCH; | |
2195 | } | |
2196 | ||
2197 | if (truncated) { | |
2198 | bool found = false; | |
2199 | ||
2200 | /* Drop the last line. */ | |
2201 | for (char *p = PTR_SUB1(buf + strlen(buf), buf); p; p = PTR_SUB1(p, buf)) | |
2202 | if (strchr(NEWLINE, *p)) { | |
2203 | *p = '\0'; | |
2204 | found = true; | |
2205 | } else if (found) | |
2206 | break; | |
2207 | } | |
2208 | ||
2209 | r = strv_split_newlines_full(&lines, result, EXTRACT_RETAIN_ESCAPE); | |
2210 | if (r == -ENOMEM) | |
2211 | return log_oom(); | |
2212 | if (r < 0) { | |
2213 | log_event_warning_errno(dev, token, r, | |
2214 | "Failed to extract lines from result of command \"%s\", ignoring: %m", buf); | |
2215 | return false; | |
2216 | } | |
2217 | ||
2218 | STRV_FOREACH(line, lines) { | |
2219 | char *key, *value; | |
2220 | ||
2221 | r = get_property_from_string(*line, &key, &value); | |
2222 | if (r < 0) { | |
2223 | log_event_debug_errno(dev, token, r, | |
2224 | "Failed to parse key and value from '%s', ignoring: %m", | |
2225 | *line); | |
2226 | continue; | |
2227 | } | |
2228 | if (r == 0) | |
2229 | continue; | |
2230 | ||
2231 | r = device_add_property(dev, key, value); | |
2232 | if (r < 0) | |
2233 | return log_event_error_errno(dev, token, r, | |
2234 | "Failed to add property %s=%s: %m", | |
2235 | key, value); | |
2236 | } | |
2237 | ||
2238 | return token->op == OP_MATCH; | |
2239 | } | |
2240 | case TK_M_IMPORT_BUILTIN: { | |
2241 | UdevBuiltinCommand cmd = PTR_TO_UDEV_BUILTIN_CMD(token->data); | |
2242 | assert(cmd >= 0 && cmd < _UDEV_BUILTIN_MAX); | |
2243 | unsigned mask = 1U << (int) cmd; | |
2244 | char buf[UDEV_LINE_SIZE]; | |
2245 | bool truncated; | |
2246 | ||
2247 | if (udev_builtin_run_once(cmd)) { | |
2248 | /* check if we ran already */ | |
2249 | if (event->builtin_run & mask) { | |
2250 | log_event_debug(dev, token, "Skipping builtin '%s' in IMPORT key", | |
2251 | udev_builtin_name(cmd)); | |
2252 | /* return the result from earlier run */ | |
2253 | return token->op == (event->builtin_ret & mask ? OP_NOMATCH : OP_MATCH); | |
2254 | } | |
2255 | /* mark as ran */ | |
2256 | event->builtin_run |= mask; | |
2257 | } | |
2258 | ||
2259 | (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false, &truncated); | |
2260 | if (truncated) { | |
2261 | log_event_truncated(dev, token, "builtin command", token->value, "IMPORT", /* is_match = */ true); | |
2262 | return false; | |
2263 | } | |
2264 | ||
2265 | log_event_debug(dev, token, "Importing properties from results of builtin command '%s'", buf); | |
2266 | ||
2267 | r = udev_builtin_run(event, cmd, buf); | |
2268 | if (r < 0) { | |
2269 | /* remember failure */ | |
2270 | log_event_debug_errno(dev, token, r, "Failed to run builtin '%s': %m", buf); | |
2271 | event->builtin_ret |= mask; | |
2272 | } | |
2273 | return token->op == (r >= 0 ? OP_MATCH : OP_NOMATCH); | |
2274 | } | |
2275 | case TK_M_IMPORT_DB: { | |
2276 | const char *val; | |
2277 | ||
2278 | if (!event->dev_db_clone) | |
2279 | return token->op == OP_NOMATCH; | |
2280 | r = sd_device_get_property_value(event->dev_db_clone, token->value, &val); | |
2281 | if (r == -ENOENT) | |
2282 | return token->op == OP_NOMATCH; | |
2283 | if (r < 0) | |
2284 | return log_event_error_errno(dev, token, r, | |
2285 | "Failed to get property '%s' from database: %m", | |
2286 | token->value); | |
2287 | ||
2288 | r = device_add_property(dev, token->value, val); | |
2289 | if (r < 0) | |
2290 | return log_event_error_errno(dev, token, r, "Failed to add property '%s=%s': %m", | |
2291 | token->value, val); | |
2292 | return token->op == OP_MATCH; | |
2293 | } | |
2294 | case TK_M_IMPORT_CMDLINE: { | |
2295 | _cleanup_free_ char *value = NULL; | |
2296 | ||
2297 | r = proc_cmdline_get_key(token->value, PROC_CMDLINE_VALUE_OPTIONAL|PROC_CMDLINE_IGNORE_EFI_OPTIONS, &value); | |
2298 | if (r < 0) | |
2299 | return log_event_error_errno(dev, token, r, | |
2300 | "Failed to read '%s' option from /proc/cmdline: %m", | |
2301 | token->value); | |
2302 | if (r == 0) | |
2303 | return token->op == OP_NOMATCH; | |
2304 | ||
2305 | r = device_add_property(dev, token->value, value ?: "1"); | |
2306 | if (r < 0) | |
2307 | return log_event_error_errno(dev, token, r, "Failed to add property '%s=%s': %m", | |
2308 | token->value, value ?: "1"); | |
2309 | return token->op == OP_MATCH; | |
2310 | } | |
2311 | case TK_M_IMPORT_PARENT: { | |
2312 | char buf[UDEV_PATH_SIZE]; | |
2313 | bool truncated; | |
2314 | ||
2315 | (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false, &truncated); | |
2316 | if (truncated) { | |
2317 | log_event_truncated(dev, token, "property name", token->value, "IMPORT", /* is_match = */ true); | |
2318 | return false; | |
2319 | } | |
2320 | ||
2321 | r = import_parent_into_properties(dev, buf); | |
2322 | if (r < 0) | |
2323 | return log_event_error_errno(dev, token, r, | |
2324 | "Failed to import properties '%s' from parent: %m", | |
2325 | buf); | |
2326 | return token->op == (r > 0 ? OP_MATCH : OP_NOMATCH); | |
2327 | } | |
2328 | case TK_M_RESULT: | |
2329 | return token_match_string(token, event->program_result); | |
2330 | case TK_A_OPTIONS_STRING_ESCAPE_NONE: | |
2331 | event->esc = ESCAPE_NONE; | |
2332 | break; | |
2333 | case TK_A_OPTIONS_STRING_ESCAPE_REPLACE: | |
2334 | event->esc = ESCAPE_REPLACE; | |
2335 | break; | |
2336 | case TK_A_OPTIONS_DB_PERSIST: | |
2337 | device_set_db_persist(dev); | |
2338 | break; | |
2339 | case TK_A_OPTIONS_INOTIFY_WATCH: | |
2340 | if (event->inotify_watch_final) | |
2341 | break; | |
2342 | if (token->op == OP_ASSIGN_FINAL) | |
2343 | event->inotify_watch_final = true; | |
2344 | ||
2345 | event->inotify_watch = token->data; | |
2346 | break; | |
2347 | case TK_A_OPTIONS_DEVLINK_PRIORITY: | |
2348 | device_set_devlink_priority(dev, PTR_TO_INT(token->data)); | |
2349 | break; | |
2350 | case TK_A_OPTIONS_LOG_LEVEL: { | |
2351 | int level = PTR_TO_INT(token->data); | |
2352 | ||
2353 | if (level < 0) | |
2354 | level = event->default_log_level; | |
2355 | ||
2356 | log_set_max_level(level); | |
2357 | ||
2358 | if (level == LOG_DEBUG && !event->log_level_was_debug) { | |
2359 | /* The log level becomes LOG_DEBUG at first time. Let's log basic information. */ | |
2360 | log_device_uevent(dev, "The log level is changed to 'debug' while processing device"); | |
2361 | event->log_level_was_debug = true; | |
2362 | } | |
2363 | ||
2364 | break; | |
2365 | } | |
2366 | case TK_A_OWNER: { | |
2367 | char owner[UDEV_NAME_SIZE]; | |
2368 | const char *ow = owner; | |
2369 | bool truncated; | |
2370 | ||
2371 | if (event->owner_final) | |
2372 | break; | |
2373 | if (token->op == OP_ASSIGN_FINAL) | |
2374 | event->owner_final = true; | |
2375 | ||
2376 | (void) udev_event_apply_format(event, token->value, owner, sizeof(owner), false, &truncated); | |
2377 | if (truncated) { | |
2378 | log_event_truncated(dev, token, "user name", token->value, "OWNER", /* is_match = */ false); | |
2379 | break; | |
2380 | } | |
2381 | ||
2382 | r = get_user_creds(&ow, &event->uid, NULL, NULL, NULL, USER_CREDS_ALLOW_MISSING); | |
2383 | if (r < 0) | |
2384 | log_unknown_owner(dev, token->rule_line, r, "user", owner); | |
2385 | else | |
2386 | log_event_debug(dev, token, "OWNER %s(%u)", owner, event->uid); | |
2387 | break; | |
2388 | } | |
2389 | case TK_A_GROUP: { | |
2390 | char group[UDEV_NAME_SIZE]; | |
2391 | const char *gr = group; | |
2392 | bool truncated; | |
2393 | ||
2394 | if (event->group_final) | |
2395 | break; | |
2396 | if (token->op == OP_ASSIGN_FINAL) | |
2397 | event->group_final = true; | |
2398 | ||
2399 | (void) udev_event_apply_format(event, token->value, group, sizeof(group), false, &truncated); | |
2400 | if (truncated) { | |
2401 | log_event_truncated(dev, token, "group name", token->value, "GROUP", /* is_match = */ false); | |
2402 | break; | |
2403 | } | |
2404 | ||
2405 | r = get_group_creds(&gr, &event->gid, USER_CREDS_ALLOW_MISSING); | |
2406 | if (r < 0) | |
2407 | log_unknown_owner(dev, token->rule_line, r, "group", group); | |
2408 | else | |
2409 | log_event_debug(dev, token, "GROUP %s(%u)", group, event->gid); | |
2410 | break; | |
2411 | } | |
2412 | case TK_A_MODE: { | |
2413 | char mode_str[UDEV_NAME_SIZE]; | |
2414 | bool truncated; | |
2415 | ||
2416 | if (event->mode_final) | |
2417 | break; | |
2418 | if (token->op == OP_ASSIGN_FINAL) | |
2419 | event->mode_final = true; | |
2420 | ||
2421 | (void) udev_event_apply_format(event, token->value, mode_str, sizeof(mode_str), false, &truncated); | |
2422 | if (truncated) { | |
2423 | log_event_truncated(dev, token, "mode", token->value, "MODE", /* is_match = */ false); | |
2424 | break; | |
2425 | } | |
2426 | ||
2427 | r = parse_mode(mode_str, &event->mode); | |
2428 | if (r < 0) | |
2429 | log_event_error_errno(dev, token, r, "Failed to parse mode '%s', ignoring: %m", mode_str); | |
2430 | else | |
2431 | log_event_debug(dev, token, "MODE %#o", event->mode); | |
2432 | break; | |
2433 | } | |
2434 | case TK_A_OWNER_ID: | |
2435 | if (event->owner_final) | |
2436 | break; | |
2437 | if (token->op == OP_ASSIGN_FINAL) | |
2438 | event->owner_final = true; | |
2439 | if (!token->data) | |
2440 | break; | |
2441 | event->uid = PTR_TO_UID(token->data); | |
2442 | log_event_debug(dev, token, "OWNER %u", event->uid); | |
2443 | break; | |
2444 | case TK_A_GROUP_ID: | |
2445 | if (event->group_final) | |
2446 | break; | |
2447 | if (token->op == OP_ASSIGN_FINAL) | |
2448 | event->group_final = true; | |
2449 | if (!token->data) | |
2450 | break; | |
2451 | event->gid = PTR_TO_GID(token->data); | |
2452 | log_event_debug(dev, token, "GROUP %u", event->gid); | |
2453 | break; | |
2454 | case TK_A_MODE_ID: | |
2455 | if (event->mode_final) | |
2456 | break; | |
2457 | if (token->op == OP_ASSIGN_FINAL) | |
2458 | event->mode_final = true; | |
2459 | if (!token->data) | |
2460 | break; | |
2461 | event->mode = PTR_TO_MODE(token->data); | |
2462 | log_event_debug(dev, token, "MODE %#o", event->mode); | |
2463 | break; | |
2464 | case TK_A_SECLABEL: { | |
2465 | _cleanup_free_ char *name = NULL, *label = NULL; | |
2466 | char label_str[UDEV_LINE_SIZE] = {}; | |
2467 | bool truncated; | |
2468 | ||
2469 | name = strdup(token->data); | |
2470 | if (!name) | |
2471 | return log_oom(); | |
2472 | ||
2473 | (void) udev_event_apply_format(event, token->value, label_str, sizeof(label_str), false, &truncated); | |
2474 | if (truncated) { | |
2475 | log_event_truncated(dev, token, "security label", token->value, "SECLABEL", /* is_match = */ false); | |
2476 | break; | |
2477 | } | |
2478 | ||
2479 | if (!isempty(label_str)) | |
2480 | label = strdup(label_str); | |
2481 | else | |
2482 | label = strdup(token->value); | |
2483 | if (!label) | |
2484 | return log_oom(); | |
2485 | ||
2486 | if (token->op == OP_ASSIGN) | |
2487 | ordered_hashmap_clear_free_free(event->seclabel_list); | |
2488 | ||
2489 | r = ordered_hashmap_ensure_put(&event->seclabel_list, NULL, name, label); | |
2490 | if (r == -ENOMEM) | |
2491 | return log_oom(); | |
2492 | if (r < 0) | |
2493 | return log_event_error_errno(dev, token, r, "Failed to store SECLABEL{%s}='%s': %m", name, label); | |
2494 | ||
2495 | log_event_debug(dev, token, "SECLABEL{%s}='%s'", name, label); | |
2496 | ||
2497 | TAKE_PTR(name); | |
2498 | TAKE_PTR(label); | |
2499 | break; | |
2500 | } | |
2501 | case TK_A_ENV: { | |
2502 | const char *val, *name = token->data; | |
2503 | char value_new[UDEV_NAME_SIZE], *p = value_new; | |
2504 | size_t count, l = sizeof(value_new); | |
2505 | bool truncated; | |
2506 | ||
2507 | if (isempty(token->value)) { | |
2508 | if (token->op == OP_ADD) | |
2509 | break; | |
2510 | r = device_add_property(dev, name, NULL); | |
2511 | if (r < 0) | |
2512 | return log_event_error_errno(dev, token, r, "Failed to remove property '%s': %m", name); | |
2513 | break; | |
2514 | } | |
2515 | ||
2516 | if (token->op == OP_ADD && | |
2517 | device_get_property_value_with_fallback(dev, name, event->worker ? event->worker->properties : NULL, &val) >= 0) { | |
2518 | l = strpcpyl_full(&p, l, &truncated, val, " ", NULL); | |
2519 | if (truncated) { | |
2520 | log_event_warning(dev, token, | |
2521 | "The buffer for the property '%s' is full, " | |
2522 | "refusing to append the new value '%s'.", name, token->value); | |
2523 | break; | |
2524 | } | |
2525 | } | |
2526 | ||
2527 | (void) udev_event_apply_format(event, token->value, p, l, false, &truncated); | |
2528 | if (truncated) { | |
2529 | _cleanup_free_ char *key_with_name = strjoin("ENV{", name, "}"); | |
2530 | log_event_truncated(dev, token, "property value", token->value, | |
2531 | key_with_name ?: "ENV", /* is_match = */ false); | |
2532 | break; | |
2533 | } | |
2534 | ||
2535 | if (event->esc == ESCAPE_REPLACE) { | |
2536 | count = udev_replace_chars(p, NULL); | |
2537 | if (count > 0) | |
2538 | log_event_debug(dev, token, | |
2539 | "Replaced %zu slash(es) from result of ENV{%s}%s=\"%s\"", | |
2540 | count, name, token->op == OP_ADD ? "+" : "", token->value); | |
2541 | } | |
2542 | ||
2543 | r = device_add_property(dev, name, value_new); | |
2544 | if (r < 0) | |
2545 | return log_event_error_errno(dev, token, r, "Failed to add property '%s=%s': %m", name, value_new); | |
2546 | break; | |
2547 | } | |
2548 | case TK_A_TAG: { | |
2549 | char buf[UDEV_PATH_SIZE]; | |
2550 | bool truncated; | |
2551 | ||
2552 | (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false, &truncated); | |
2553 | if (truncated) { | |
2554 | log_event_truncated(dev, token, "tag name", token->value, "TAG", /* is_match = */ false); | |
2555 | break; | |
2556 | } | |
2557 | ||
2558 | if (token->op == OP_ASSIGN) | |
2559 | device_cleanup_tags(dev); | |
2560 | ||
2561 | if (token->op == OP_REMOVE) | |
2562 | device_remove_tag(dev, buf); | |
2563 | else { | |
2564 | r = device_add_tag(dev, buf, true); | |
2565 | if (r == -ENOMEM) | |
2566 | return log_oom(); | |
2567 | if (r < 0) | |
2568 | log_event_warning_errno(dev, token, r, "Failed to add tag '%s', ignoring: %m", buf); | |
2569 | } | |
2570 | break; | |
2571 | } | |
2572 | case TK_A_NAME: { | |
2573 | char buf[UDEV_PATH_SIZE]; | |
2574 | bool truncated; | |
2575 | size_t count; | |
2576 | ||
2577 | if (event->name_final) | |
2578 | break; | |
2579 | if (token->op == OP_ASSIGN_FINAL) | |
2580 | event->name_final = true; | |
2581 | ||
2582 | if (sd_device_get_ifindex(dev, NULL) < 0) { | |
2583 | log_event_error(dev, token, | |
2584 | "Only network interfaces can be renamed, ignoring NAME=\"%s\".", | |
2585 | token->value); | |
2586 | break; | |
2587 | } | |
2588 | ||
2589 | (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false, &truncated); | |
2590 | if (truncated) { | |
2591 | log_event_truncated(dev, token, "network interface name", token->value, "NAME", /* is_match = */ false); | |
2592 | break; | |
2593 | } | |
2594 | ||
2595 | if (IN_SET(event->esc, ESCAPE_UNSET, ESCAPE_REPLACE)) { | |
2596 | if (naming_scheme_has(NAMING_REPLACE_STRICTLY)) | |
2597 | count = udev_replace_ifname(buf); | |
2598 | else | |
2599 | count = udev_replace_chars(buf, "/"); | |
2600 | if (count > 0) | |
2601 | log_event_debug(dev, token, | |
2602 | "Replaced %zu character(s) from result of NAME=\"%s\"", | |
2603 | count, token->value); | |
2604 | } | |
2605 | r = free_and_strdup_warn(&event->name, buf); | |
2606 | if (r < 0) | |
2607 | return r; | |
2608 | ||
2609 | log_event_debug(dev, token, "NAME '%s'", event->name); | |
2610 | break; | |
2611 | } | |
2612 | case TK_A_DEVLINK: { | |
2613 | char buf[UDEV_PATH_SIZE]; | |
2614 | bool truncated; | |
2615 | size_t count; | |
2616 | ||
2617 | if (event->devlink_final) | |
2618 | break; | |
2619 | if (sd_device_get_devnum(dev, NULL) < 0) | |
2620 | break; | |
2621 | if (token->op == OP_ASSIGN_FINAL) | |
2622 | event->devlink_final = true; | |
2623 | if (IN_SET(token->op, OP_ASSIGN, OP_ASSIGN_FINAL)) | |
2624 | device_cleanup_devlinks(dev); | |
2625 | ||
2626 | (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), | |
2627 | /* replace_whitespace = */ event->esc != ESCAPE_NONE, &truncated); | |
2628 | if (truncated) { | |
2629 | log_event_truncated(dev, token, "symbolic link path", token->value, "SYMLINK", /* is_match = */ false); | |
2630 | break; | |
2631 | } | |
2632 | ||
2633 | /* By default or string_escape=none, allow multiple symlinks separated by spaces. */ | |
2634 | if (event->esc == ESCAPE_UNSET) | |
2635 | count = udev_replace_chars(buf, /* allow = */ "/ "); | |
2636 | else if (event->esc == ESCAPE_REPLACE) | |
2637 | count = udev_replace_chars(buf, /* allow = */ "/"); | |
2638 | else | |
2639 | count = 0; | |
2640 | if (count > 0) | |
2641 | log_event_debug(dev, token, | |
2642 | "Replaced %zu character(s) from result of SYMLINK=\"%s\"", | |
2643 | count, token->value); | |
2644 | ||
2645 | for (const char *p = buf;;) { | |
2646 | _cleanup_free_ char *path = NULL; | |
2647 | ||
2648 | r = extract_first_word(&p, &path, NULL, EXTRACT_RETAIN_ESCAPE); | |
2649 | if (r == -ENOMEM) | |
2650 | return log_oom(); | |
2651 | if (r < 0) { | |
2652 | log_warning_errno(r, "Failed to extract first path in SYMLINK=, ignoring: %m"); | |
2653 | break; | |
2654 | } | |
2655 | if (r == 0) | |
2656 | break; | |
2657 | ||
2658 | if (token->op == OP_REMOVE) { | |
2659 | r = device_remove_devlink(dev, path); | |
2660 | if (r == -ENOMEM) | |
2661 | return log_oom(); | |
2662 | if (r < 0) | |
2663 | log_event_warning_errno(dev, token, r, "Failed to remove devlink '%s', ignoring: %m", path); | |
2664 | else if (r > 0) | |
2665 | log_event_debug(dev, token, "Dropped SYMLINK '%s'", path); | |
2666 | } else { | |
2667 | r = device_add_devlink(dev, path); | |
2668 | if (r == -ENOMEM) | |
2669 | return log_oom(); | |
2670 | if (r < 0) | |
2671 | log_event_warning_errno(dev, token, r, "Failed to add devlink '%s', ignoring: %m", path); | |
2672 | else if (r > 0) | |
2673 | log_event_debug(dev, token, "Added SYMLINK '%s'", path); | |
2674 | } | |
2675 | } | |
2676 | break; | |
2677 | } | |
2678 | case TK_A_ATTR: { | |
2679 | char buf[UDEV_PATH_SIZE], value[UDEV_NAME_SIZE]; | |
2680 | const char *val, *key_name = token->data; | |
2681 | bool truncated; | |
2682 | ||
2683 | if (udev_resolve_subsys_kernel(key_name, buf, sizeof(buf), false) < 0 && | |
2684 | sd_device_get_syspath(dev, &val) >= 0) { | |
2685 | strscpyl_full(buf, sizeof(buf), &truncated, val, "/", key_name, NULL); | |
2686 | if (truncated) { | |
2687 | log_event_warning(dev, token, | |
2688 | "The path to the attribute '%s/%s' is too long, refusing to set the attribute.", | |
2689 | val, key_name); | |
2690 | break; | |
2691 | } | |
2692 | } | |
2693 | ||
2694 | r = attr_subst_subdir(buf); | |
2695 | if (r < 0) { | |
2696 | log_event_error_errno(dev, token, r, "Could not find file matches '%s', ignoring: %m", buf); | |
2697 | break; | |
2698 | } | |
2699 | (void) udev_event_apply_format(event, token->value, value, sizeof(value), false, &truncated); | |
2700 | if (truncated) { | |
2701 | log_event_truncated(dev, token, "attribute value", token->value, "ATTR", /* is_match = */ false); | |
2702 | break; | |
2703 | } | |
2704 | ||
2705 | if (EVENT_MODE_DESTRUCTIVE(event)) { | |
2706 | log_event_debug(dev, token, "Writing ATTR{'%s'}=\"%s\".", buf, value); | |
2707 | r = write_string_file(buf, value, | |
2708 | WRITE_STRING_FILE_VERIFY_ON_FAILURE | | |
2709 | WRITE_STRING_FILE_DISABLE_BUFFER | | |
2710 | WRITE_STRING_FILE_AVOID_NEWLINE | | |
2711 | WRITE_STRING_FILE_VERIFY_IGNORE_NEWLINE); | |
2712 | if (r < 0) | |
2713 | log_event_error_errno(dev, token, r, "Failed to write ATTR{%s}=\"%s\", ignoring: %m", buf, value); | |
2714 | } else | |
2715 | log_event_debug(dev, token, "Running in test mode, skipping writing ATTR{%s}=\"%s\".", buf, value); | |
2716 | ||
2717 | break; | |
2718 | } | |
2719 | case TK_A_SYSCTL: { | |
2720 | char buf[UDEV_PATH_SIZE], value[UDEV_NAME_SIZE]; | |
2721 | bool truncated; | |
2722 | ||
2723 | (void) udev_event_apply_format(event, token->data, buf, sizeof(buf), false, &truncated); | |
2724 | if (truncated) { | |
2725 | log_event_truncated(dev, token, "sysctl entry name", token->data, "SYSCTL", /* is_match = */ false); | |
2726 | break; | |
2727 | } | |
2728 | ||
2729 | (void) udev_event_apply_format(event, token->value, value, sizeof(value), false, &truncated); | |
2730 | if (truncated) { | |
2731 | _cleanup_free_ char *key_with_name = strjoin("SYSCTL{", buf, "}"); | |
2732 | log_event_truncated(dev, token, "sysctl value", token->value, | |
2733 | key_with_name ?: "SYSCTL", /* is_match = */ false); | |
2734 | break; | |
2735 | } | |
2736 | ||
2737 | sysctl_normalize(buf); | |
2738 | ||
2739 | if (EVENT_MODE_DESTRUCTIVE(event)) { | |
2740 | log_event_debug(dev, token, "Writing SYSCTL{%s}=\"%s\".", buf, value); | |
2741 | r = sysctl_write(buf, value); | |
2742 | if (r < 0) | |
2743 | log_event_error_errno(dev, token, r, "Failed to write SYSCTL{%s}=\"%s\", ignoring: %m", buf, value); | |
2744 | } else | |
2745 | log_event_debug(dev, token, "Running in test mode, skipping writing SYSCTL{%s}=\"%s\".", buf, value); | |
2746 | ||
2747 | break; | |
2748 | } | |
2749 | case TK_A_RUN_BUILTIN: | |
2750 | case TK_A_RUN_PROGRAM: { | |
2751 | _cleanup_free_ char *cmd = NULL; | |
2752 | char buf[UDEV_LINE_SIZE]; | |
2753 | bool truncated; | |
2754 | ||
2755 | if (event->run_final) | |
2756 | break; | |
2757 | if (token->op == OP_ASSIGN_FINAL) | |
2758 | event->run_final = true; | |
2759 | ||
2760 | if (IN_SET(token->op, OP_ASSIGN, OP_ASSIGN_FINAL)) | |
2761 | ordered_hashmap_clear_free_key(event->run_list); | |
2762 | ||
2763 | (void) udev_event_apply_format(event, token->value, buf, sizeof(buf), false, &truncated); | |
2764 | if (truncated) { | |
2765 | log_event_truncated(dev, token, "command", token->value, | |
2766 | token->type == TK_A_RUN_BUILTIN ? "RUN{builtin}" : "RUN{program}", | |
2767 | /* is_match = */ false); | |
2768 | break; | |
2769 | } | |
2770 | ||
2771 | cmd = strdup(buf); | |
2772 | if (!cmd) | |
2773 | return log_oom(); | |
2774 | ||
2775 | r = ordered_hashmap_ensure_put(&event->run_list, NULL, cmd, token->data); | |
2776 | if (r == -ENOMEM) | |
2777 | return log_oom(); | |
2778 | if (r < 0) | |
2779 | return log_event_error_errno(dev, token, r, "Failed to store command '%s': %m", cmd); | |
2780 | ||
2781 | TAKE_PTR(cmd); | |
2782 | ||
2783 | log_event_debug(dev, token, "RUN '%s'", token->value); | |
2784 | break; | |
2785 | } | |
2786 | case TK_A_OPTIONS_STATIC_NODE: | |
2787 | /* do nothing for events. */ | |
2788 | break; | |
2789 | default: | |
2790 | assert_not_reached(); | |
2791 | } | |
2792 | ||
2793 | return true; | |
2794 | } | |
2795 | ||
2796 | static bool token_is_for_parents(UdevRuleToken *token) { | |
2797 | return token->type >= TK_M_PARENTS_KERNEL && token->type <= TK_M_PARENTS_TAG; | |
2798 | } | |
2799 | ||
2800 | static int udev_rule_apply_parent_token_to_event(UdevRuleToken *head_token, UdevEvent *event) { | |
2801 | int r; | |
2802 | ||
2803 | assert(head_token); | |
2804 | assert(event); | |
2805 | ||
2806 | event->dev_parent = ASSERT_PTR(event->dev); | |
2807 | ||
2808 | for (;;) { | |
2809 | LIST_FOREACH(tokens, token, head_token) { | |
2810 | if (!token_is_for_parents(token)) | |
2811 | return true; /* All parent tokens match. */ | |
2812 | ||
2813 | r = udev_rule_apply_token_to_event(token, event->dev_parent, event); | |
2814 | if (r < 0) | |
2815 | return r; | |
2816 | if (r == 0) | |
2817 | break; | |
2818 | } | |
2819 | if (r > 0) | |
2820 | /* All parent tokens match, and no more token (except for GOTO) in the line. */ | |
2821 | return true; | |
2822 | ||
2823 | if (sd_device_get_parent(event->dev_parent, &event->dev_parent) < 0) { | |
2824 | event->dev_parent = NULL; | |
2825 | return false; | |
2826 | } | |
2827 | } | |
2828 | } | |
2829 | ||
2830 | static int udev_rule_apply_line_to_event( | |
2831 | UdevRuleLine *line, | |
2832 | UdevEvent *event, | |
2833 | UdevRuleLine **next_line) { | |
2834 | ||
2835 | UdevRuleLineType mask = LINE_HAS_GOTO | LINE_UPDATE_SOMETHING; | |
2836 | bool parents_done = false; | |
2837 | sd_device_action_t action; | |
2838 | int r; | |
2839 | ||
2840 | assert(line); | |
2841 | assert(event); | |
2842 | assert(next_line); | |
2843 | ||
2844 | r = sd_device_get_action(event->dev, &action); | |
2845 | if (r < 0) | |
2846 | return r; | |
2847 | ||
2848 | if (action != SD_DEVICE_REMOVE) { | |
2849 | if (sd_device_get_devnum(event->dev, NULL) >= 0) | |
2850 | mask |= LINE_HAS_DEVLINK; | |
2851 | ||
2852 | if (sd_device_get_ifindex(event->dev, NULL) >= 0) | |
2853 | mask |= LINE_HAS_NAME; | |
2854 | } | |
2855 | ||
2856 | if ((line->type & mask) == 0) | |
2857 | return 0; | |
2858 | ||
2859 | event->esc = ESCAPE_UNSET; | |
2860 | ||
2861 | DEVICE_TRACE_POINT(rules_apply_line, event->dev, line->rule_file->filename, line->line_number); | |
2862 | ||
2863 | LIST_FOREACH(tokens, token, line->tokens) { | |
2864 | if (token_is_for_parents(token)) { | |
2865 | if (parents_done) | |
2866 | continue; | |
2867 | ||
2868 | r = udev_rule_apply_parent_token_to_event(token, event); | |
2869 | if (r <= 0) | |
2870 | return r; | |
2871 | ||
2872 | parents_done = true; | |
2873 | continue; | |
2874 | } | |
2875 | ||
2876 | r = udev_rule_apply_token_to_event(token, event->dev, event); | |
2877 | if (r <= 0) | |
2878 | return r; | |
2879 | } | |
2880 | ||
2881 | if (line->goto_line) | |
2882 | *next_line = line->goto_line; /* update next_line only when the line has GOTO token. */ | |
2883 | ||
2884 | return 0; | |
2885 | } | |
2886 | ||
2887 | int udev_rules_apply_to_event(UdevRules *rules, UdevEvent *event) { | |
2888 | int r; | |
2889 | ||
2890 | assert(rules); | |
2891 | assert(event); | |
2892 | ||
2893 | LIST_FOREACH(rule_files, file, rules->rule_files) | |
2894 | LIST_FOREACH_WITH_NEXT(rule_lines, line, next_line, file->rule_lines) { | |
2895 | r = udev_rule_apply_line_to_event(line, event, &next_line); | |
2896 | if (r < 0) | |
2897 | return r; | |
2898 | } | |
2899 | ||
2900 | return 0; | |
2901 | } | |
2902 | ||
2903 | static int udev_rule_line_apply_static_dev_perms(UdevRuleLine *rule_line) { | |
2904 | _cleanup_strv_free_ char **tags = NULL; | |
2905 | uid_t uid = UID_INVALID; | |
2906 | gid_t gid = GID_INVALID; | |
2907 | mode_t mode = MODE_INVALID; | |
2908 | int r; | |
2909 | ||
2910 | assert(rule_line); | |
2911 | ||
2912 | if (!FLAGS_SET(rule_line->type, LINE_HAS_STATIC_NODE)) | |
2913 | return 0; | |
2914 | ||
2915 | LIST_FOREACH(tokens, token, rule_line->tokens) | |
2916 | if (token->type == TK_A_OWNER_ID) | |
2917 | uid = PTR_TO_UID(token->data); | |
2918 | else if (token->type == TK_A_GROUP_ID) | |
2919 | gid = PTR_TO_GID(token->data); | |
2920 | else if (token->type == TK_A_MODE_ID) | |
2921 | mode = PTR_TO_MODE(token->data); | |
2922 | else if (token->type == TK_A_TAG) { | |
2923 | r = strv_extend(&tags, token->value); | |
2924 | if (r < 0) | |
2925 | return log_oom(); | |
2926 | } else if (token->type == TK_A_OPTIONS_STATIC_NODE) { | |
2927 | r = static_node_apply_permissions(token->value, mode, uid, gid, tags); | |
2928 | if (r < 0) | |
2929 | return r; | |
2930 | } | |
2931 | ||
2932 | return 0; | |
2933 | } | |
2934 | ||
2935 | int udev_rules_apply_static_dev_perms(UdevRules *rules) { | |
2936 | int r; | |
2937 | ||
2938 | assert(rules); | |
2939 | ||
2940 | LIST_FOREACH(rule_files, file, rules->rule_files) | |
2941 | LIST_FOREACH(rule_lines, line, file->rule_lines) { | |
2942 | r = udev_rule_line_apply_static_dev_perms(line); | |
2943 | if (r < 0) | |
2944 | return r; | |
2945 | } | |
2946 | ||
2947 | return 0; | |
2948 | } | |
2949 | ||
2950 | static const char* const resolve_name_timing_table[_RESOLVE_NAME_TIMING_MAX] = { | |
2951 | [RESOLVE_NAME_NEVER] = "never", | |
2952 | [RESOLVE_NAME_LATE] = "late", | |
2953 | [RESOLVE_NAME_EARLY] = "early", | |
2954 | }; | |
2955 | ||
2956 | DEFINE_STRING_TABLE_LOOKUP(resolve_name_timing, ResolveNameTiming); |