]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-rules.c
exec-util,conf-files: skip non-executable files in execute_directories()
[thirdparty/systemd.git] / src / udev / udev-rules.c
1 /*
2 * Copyright (C) 2003-2012 Kay Sievers <kay@vrfy.org>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <ctype.h>
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <fnmatch.h>
22 #include <limits.h>
23 #include <stdbool.h>
24 #include <stddef.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
29 #include <unistd.h>
30
31 #include "alloc-util.h"
32 #include "conf-files.h"
33 #include "dirent-util.h"
34 #include "escape.h"
35 #include "fd-util.h"
36 #include "fs-util.h"
37 #include "glob-util.h"
38 #include "path-util.h"
39 #include "stat-util.h"
40 #include "stdio-util.h"
41 #include "strbuf.h"
42 #include "string-util.h"
43 #include "strv.h"
44 #include "sysctl-util.h"
45 #include "udev.h"
46 #include "user-util.h"
47 #include "util.h"
48
49 #define PREALLOC_TOKEN 2048
50
51 struct uid_gid {
52 unsigned int name_off;
53 union {
54 uid_t uid;
55 gid_t gid;
56 };
57 };
58
59 static const char* const rules_dirs[] = {
60 "/etc/udev/rules.d",
61 "/run/udev/rules.d",
62 UDEVLIBEXECDIR "/rules.d",
63 NULL
64 };
65
66 struct udev_rules {
67 struct udev *udev;
68 usec_t dirs_ts_usec;
69 int resolve_names;
70
71 /* every key in the rules file becomes a token */
72 struct token *tokens;
73 unsigned int token_cur;
74 unsigned int token_max;
75
76 /* all key strings are copied and de-duplicated in a single continuous string buffer */
77 struct strbuf *strbuf;
78
79 /* during rule parsing, uid/gid lookup results are cached */
80 struct uid_gid *uids;
81 unsigned int uids_cur;
82 unsigned int uids_max;
83 struct uid_gid *gids;
84 unsigned int gids_cur;
85 unsigned int gids_max;
86 };
87
88 static char *rules_str(struct udev_rules *rules, unsigned int off) {
89 return rules->strbuf->buf + off;
90 }
91
92 static unsigned int rules_add_string(struct udev_rules *rules, const char *s) {
93 return strbuf_add_string(rules->strbuf, s, strlen(s));
94 }
95
96 /* KEY=="", KEY!="", KEY+="", KEY-="", KEY="", KEY:="" */
97 enum operation_type {
98 OP_UNSET,
99
100 OP_MATCH,
101 OP_NOMATCH,
102 OP_MATCH_MAX,
103
104 OP_ADD,
105 OP_REMOVE,
106 OP_ASSIGN,
107 OP_ASSIGN_FINAL,
108 };
109
110 enum string_glob_type {
111 GL_UNSET,
112 GL_PLAIN, /* no special chars */
113 GL_GLOB, /* shell globs ?,*,[] */
114 GL_SPLIT, /* multi-value A|B */
115 GL_SPLIT_GLOB, /* multi-value with glob A*|B* */
116 GL_SOMETHING, /* commonly used "?*" */
117 };
118
119 enum string_subst_type {
120 SB_UNSET,
121 SB_NONE,
122 SB_FORMAT,
123 SB_SUBSYS,
124 };
125
126 /* tokens of a rule are sorted/handled in this order */
127 enum token_type {
128 TK_UNSET,
129 TK_RULE,
130
131 TK_M_ACTION, /* val */
132 TK_M_DEVPATH, /* val */
133 TK_M_KERNEL, /* val */
134 TK_M_DEVLINK, /* val */
135 TK_M_NAME, /* val */
136 TK_M_ENV, /* val, attr */
137 TK_M_TAG, /* val */
138 TK_M_SUBSYSTEM, /* val */
139 TK_M_DRIVER, /* val */
140 TK_M_WAITFOR, /* val */
141 TK_M_ATTR, /* val, attr */
142 TK_M_SYSCTL, /* val, attr */
143
144 TK_M_PARENTS_MIN,
145 TK_M_KERNELS, /* val */
146 TK_M_SUBSYSTEMS, /* val */
147 TK_M_DRIVERS, /* val */
148 TK_M_ATTRS, /* val, attr */
149 TK_M_TAGS, /* val */
150 TK_M_PARENTS_MAX,
151
152 TK_M_TEST, /* val, mode_t */
153 TK_M_PROGRAM, /* val */
154 TK_M_IMPORT_FILE, /* val */
155 TK_M_IMPORT_PROG, /* val */
156 TK_M_IMPORT_BUILTIN, /* val */
157 TK_M_IMPORT_DB, /* val */
158 TK_M_IMPORT_CMDLINE, /* val */
159 TK_M_IMPORT_PARENT, /* val */
160 TK_M_RESULT, /* val */
161 TK_M_MAX,
162
163 TK_A_STRING_ESCAPE_NONE,
164 TK_A_STRING_ESCAPE_REPLACE,
165 TK_A_DB_PERSIST,
166 TK_A_INOTIFY_WATCH, /* int */
167 TK_A_DEVLINK_PRIO, /* int */
168 TK_A_OWNER, /* val */
169 TK_A_GROUP, /* val */
170 TK_A_MODE, /* val */
171 TK_A_OWNER_ID, /* uid_t */
172 TK_A_GROUP_ID, /* gid_t */
173 TK_A_MODE_ID, /* mode_t */
174 TK_A_TAG, /* val */
175 TK_A_STATIC_NODE, /* val */
176 TK_A_SECLABEL, /* val, attr */
177 TK_A_ENV, /* val, attr */
178 TK_A_NAME, /* val */
179 TK_A_DEVLINK, /* val */
180 TK_A_ATTR, /* val, attr */
181 TK_A_SYSCTL, /* val, attr */
182 TK_A_RUN_BUILTIN, /* val, bool */
183 TK_A_RUN_PROGRAM, /* val, bool */
184 TK_A_GOTO, /* size_t */
185
186 TK_END,
187 };
188
189 /* we try to pack stuff in a way that we take only 12 bytes per token */
190 struct token {
191 union {
192 unsigned char type; /* same in rule and key */
193 struct {
194 enum token_type type:8;
195 bool can_set_name:1;
196 bool has_static_node:1;
197 unsigned int unused:6;
198 unsigned short token_count;
199 unsigned int label_off;
200 unsigned short filename_off;
201 unsigned short filename_line;
202 } rule;
203 struct {
204 enum token_type type:8;
205 enum operation_type op:8;
206 enum string_glob_type glob:8;
207 enum string_subst_type subst:4;
208 enum string_subst_type attrsubst:4;
209 unsigned int value_off;
210 union {
211 unsigned int attr_off;
212 unsigned int rule_goto;
213 mode_t mode;
214 uid_t uid;
215 gid_t gid;
216 int devlink_prio;
217 int watch;
218 enum udev_builtin_cmd builtin_cmd;
219 };
220 } key;
221 };
222 };
223
224 #define MAX_TK 64
225 struct rule_tmp {
226 struct udev_rules *rules;
227 struct token rule;
228 struct token token[MAX_TK];
229 unsigned int token_cur;
230 };
231
232 #ifdef DEBUG
233 static const char *operation_str(enum operation_type type) {
234 static const char *operation_strs[] = {
235 [OP_UNSET] = "UNSET",
236 [OP_MATCH] = "match",
237 [OP_NOMATCH] = "nomatch",
238 [OP_MATCH_MAX] = "MATCH_MAX",
239
240 [OP_ADD] = "add",
241 [OP_REMOVE] = "remove",
242 [OP_ASSIGN] = "assign",
243 [OP_ASSIGN_FINAL] = "assign-final",
244 } ;
245
246 return operation_strs[type];
247 }
248
249 static const char *string_glob_str(enum string_glob_type type) {
250 static const char *string_glob_strs[] = {
251 [GL_UNSET] = "UNSET",
252 [GL_PLAIN] = "plain",
253 [GL_GLOB] = "glob",
254 [GL_SPLIT] = "split",
255 [GL_SPLIT_GLOB] = "split-glob",
256 [GL_SOMETHING] = "split-glob",
257 };
258
259 return string_glob_strs[type];
260 }
261
262 static const char *token_str(enum token_type type) {
263 static const char *token_strs[] = {
264 [TK_UNSET] = "UNSET",
265 [TK_RULE] = "RULE",
266
267 [TK_M_ACTION] = "M ACTION",
268 [TK_M_DEVPATH] = "M DEVPATH",
269 [TK_M_KERNEL] = "M KERNEL",
270 [TK_M_DEVLINK] = "M DEVLINK",
271 [TK_M_NAME] = "M NAME",
272 [TK_M_ENV] = "M ENV",
273 [TK_M_TAG] = "M TAG",
274 [TK_M_SUBSYSTEM] = "M SUBSYSTEM",
275 [TK_M_DRIVER] = "M DRIVER",
276 [TK_M_WAITFOR] = "M WAITFOR",
277 [TK_M_ATTR] = "M ATTR",
278 [TK_M_SYSCTL] = "M SYSCTL",
279
280 [TK_M_PARENTS_MIN] = "M PARENTS_MIN",
281 [TK_M_KERNELS] = "M KERNELS",
282 [TK_M_SUBSYSTEMS] = "M SUBSYSTEMS",
283 [TK_M_DRIVERS] = "M DRIVERS",
284 [TK_M_ATTRS] = "M ATTRS",
285 [TK_M_TAGS] = "M TAGS",
286 [TK_M_PARENTS_MAX] = "M PARENTS_MAX",
287
288 [TK_M_TEST] = "M TEST",
289 [TK_M_PROGRAM] = "M PROGRAM",
290 [TK_M_IMPORT_FILE] = "M IMPORT_FILE",
291 [TK_M_IMPORT_PROG] = "M IMPORT_PROG",
292 [TK_M_IMPORT_BUILTIN] = "M IMPORT_BUILTIN",
293 [TK_M_IMPORT_DB] = "M IMPORT_DB",
294 [TK_M_IMPORT_CMDLINE] = "M IMPORT_CMDLINE",
295 [TK_M_IMPORT_PARENT] = "M IMPORT_PARENT",
296 [TK_M_RESULT] = "M RESULT",
297 [TK_M_MAX] = "M MAX",
298
299 [TK_A_STRING_ESCAPE_NONE] = "A STRING_ESCAPE_NONE",
300 [TK_A_STRING_ESCAPE_REPLACE] = "A STRING_ESCAPE_REPLACE",
301 [TK_A_DB_PERSIST] = "A DB_PERSIST",
302 [TK_A_INOTIFY_WATCH] = "A INOTIFY_WATCH",
303 [TK_A_DEVLINK_PRIO] = "A DEVLINK_PRIO",
304 [TK_A_OWNER] = "A OWNER",
305 [TK_A_GROUP] = "A GROUP",
306 [TK_A_MODE] = "A MODE",
307 [TK_A_OWNER_ID] = "A OWNER_ID",
308 [TK_A_GROUP_ID] = "A GROUP_ID",
309 [TK_A_STATIC_NODE] = "A STATIC_NODE",
310 [TK_A_SECLABEL] = "A SECLABEL",
311 [TK_A_MODE_ID] = "A MODE_ID",
312 [TK_A_ENV] = "A ENV",
313 [TK_A_TAG] = "A ENV",
314 [TK_A_NAME] = "A NAME",
315 [TK_A_DEVLINK] = "A DEVLINK",
316 [TK_A_ATTR] = "A ATTR",
317 [TK_A_SYSCTL] = "A SYSCTL",
318 [TK_A_RUN_BUILTIN] = "A RUN_BUILTIN",
319 [TK_A_RUN_PROGRAM] = "A RUN_PROGRAM",
320 [TK_A_GOTO] = "A GOTO",
321
322 [TK_END] = "END",
323 };
324
325 return token_strs[type];
326 }
327
328 static void dump_token(struct udev_rules *rules, struct token *token) {
329 enum token_type type = token->type;
330 enum operation_type op = token->key.op;
331 enum string_glob_type glob = token->key.glob;
332 const char *value = rules_str(rules, token->key.value_off);
333 const char *attr = &rules->strbuf->buf[token->key.attr_off];
334
335 switch (type) {
336 case TK_RULE:
337 {
338 const char *tks_ptr = (char *)rules->tokens;
339 const char *tk_ptr = (char *)token;
340 unsigned int idx = (tk_ptr - tks_ptr) / sizeof(struct token);
341
342 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'",
343 &rules->strbuf->buf[token->rule.filename_off], token->rule.filename_line,
344 idx, token->rule.token_count,
345 &rules->strbuf->buf[token->rule.label_off]);
346 break;
347 }
348 case TK_M_ACTION:
349 case TK_M_DEVPATH:
350 case TK_M_KERNEL:
351 case TK_M_SUBSYSTEM:
352 case TK_M_DRIVER:
353 case TK_M_WAITFOR:
354 case TK_M_DEVLINK:
355 case TK_M_NAME:
356 case TK_M_KERNELS:
357 case TK_M_SUBSYSTEMS:
358 case TK_M_DRIVERS:
359 case TK_M_TAGS:
360 case TK_M_PROGRAM:
361 case TK_M_IMPORT_FILE:
362 case TK_M_IMPORT_PROG:
363 case TK_M_IMPORT_DB:
364 case TK_M_IMPORT_CMDLINE:
365 case TK_M_IMPORT_PARENT:
366 case TK_M_RESULT:
367 case TK_A_NAME:
368 case TK_A_DEVLINK:
369 case TK_A_OWNER:
370 case TK_A_GROUP:
371 case TK_A_MODE:
372 case TK_A_RUN_BUILTIN:
373 case TK_A_RUN_PROGRAM:
374 log_debug("%s %s '%s'(%s)",
375 token_str(type), operation_str(op), value, string_glob_str(glob));
376 break;
377 case TK_M_IMPORT_BUILTIN:
378 log_debug("%s %i '%s'", token_str(type), token->key.builtin_cmd, value);
379 break;
380 case TK_M_ATTR:
381 case TK_M_SYSCTL:
382 case TK_M_ATTRS:
383 case TK_M_ENV:
384 case TK_A_ATTR:
385 case TK_A_SYSCTL:
386 case TK_A_ENV:
387 log_debug("%s %s '%s' '%s'(%s)",
388 token_str(type), operation_str(op), attr, value, string_glob_str(glob));
389 break;
390 case TK_M_TAG:
391 case TK_A_TAG:
392 log_debug("%s %s '%s'", token_str(type), operation_str(op), value);
393 break;
394 case TK_A_STRING_ESCAPE_NONE:
395 case TK_A_STRING_ESCAPE_REPLACE:
396 case TK_A_DB_PERSIST:
397 log_debug("%s", token_str(type));
398 break;
399 case TK_M_TEST:
400 log_debug("%s %s '%s'(%s) %#o",
401 token_str(type), operation_str(op), value, string_glob_str(glob), token->key.mode);
402 break;
403 case TK_A_INOTIFY_WATCH:
404 log_debug("%s %u", token_str(type), token->key.watch);
405 break;
406 case TK_A_DEVLINK_PRIO:
407 log_debug("%s %u", token_str(type), token->key.devlink_prio);
408 break;
409 case TK_A_OWNER_ID:
410 log_debug("%s %s %u", token_str(type), operation_str(op), token->key.uid);
411 break;
412 case TK_A_GROUP_ID:
413 log_debug("%s %s %u", token_str(type), operation_str(op), token->key.gid);
414 break;
415 case TK_A_MODE_ID:
416 log_debug("%s %s %#o", token_str(type), operation_str(op), token->key.mode);
417 break;
418 case TK_A_STATIC_NODE:
419 log_debug("%s '%s'", token_str(type), value);
420 break;
421 case TK_A_SECLABEL:
422 log_debug("%s %s '%s' '%s'", token_str(type), operation_str(op), attr, value);
423 break;
424 case TK_A_GOTO:
425 log_debug("%s '%s' %u", token_str(type), value, token->key.rule_goto);
426 break;
427 case TK_END:
428 log_debug("* %s", token_str(type));
429 break;
430 case TK_M_PARENTS_MIN:
431 case TK_M_PARENTS_MAX:
432 case TK_M_MAX:
433 case TK_UNSET:
434 log_debug("unknown type %u", type);
435 break;
436 }
437 }
438
439 static void dump_rules(struct udev_rules *rules) {
440 unsigned int i;
441
442 log_debug("dumping %u (%zu bytes) tokens, %zu (%zu bytes) strings",
443 rules->token_cur,
444 rules->token_cur * sizeof(struct token),
445 rules->strbuf->nodes_count,
446 rules->strbuf->len);
447 for (i = 0; i < rules->token_cur; i++)
448 dump_token(rules, &rules->tokens[i]);
449 }
450 #else
451 static inline void dump_token(struct udev_rules *rules, struct token *token) {}
452 static inline void dump_rules(struct udev_rules *rules) {}
453 #endif /* DEBUG */
454
455 static int add_token(struct udev_rules *rules, struct token *token) {
456 /* grow buffer if needed */
457 if (rules->token_cur+1 >= rules->token_max) {
458 struct token *tokens;
459 unsigned int add;
460
461 /* double the buffer size */
462 add = rules->token_max;
463 if (add < 8)
464 add = 8;
465
466 tokens = realloc(rules->tokens, (rules->token_max + add ) * sizeof(struct token));
467 if (tokens == NULL)
468 return -1;
469 rules->tokens = tokens;
470 rules->token_max += add;
471 }
472 memcpy(&rules->tokens[rules->token_cur], token, sizeof(struct token));
473 rules->token_cur++;
474 return 0;
475 }
476
477 static void log_unknown_owner(int error, const char *entity, const char *owner) {
478 if (IN_SET(abs(error), ENOENT, ESRCH))
479 log_error("Specified %s '%s' unknown", entity, owner);
480 else
481 log_error_errno(error, "Error resolving %s '%s': %m", entity, owner);
482 }
483
484 static uid_t add_uid(struct udev_rules *rules, const char *owner) {
485 unsigned int i;
486 uid_t uid = 0;
487 unsigned int off;
488 int r;
489
490 /* lookup, if we know it already */
491 for (i = 0; i < rules->uids_cur; i++) {
492 off = rules->uids[i].name_off;
493 if (streq(rules_str(rules, off), owner)) {
494 uid = rules->uids[i].uid;
495 return uid;
496 }
497 }
498 r = get_user_creds(&owner, &uid, NULL, NULL, NULL);
499 if (r < 0)
500 log_unknown_owner(r, "user", owner);
501
502 /* grow buffer if needed */
503 if (rules->uids_cur+1 >= rules->uids_max) {
504 struct uid_gid *uids;
505 unsigned int add;
506
507 /* double the buffer size */
508 add = rules->uids_max;
509 if (add < 1)
510 add = 8;
511
512 uids = realloc(rules->uids, (rules->uids_max + add ) * sizeof(struct uid_gid));
513 if (uids == NULL)
514 return uid;
515 rules->uids = uids;
516 rules->uids_max += add;
517 }
518 rules->uids[rules->uids_cur].uid = uid;
519 off = rules_add_string(rules, owner);
520 if (off <= 0)
521 return uid;
522 rules->uids[rules->uids_cur].name_off = off;
523 rules->uids_cur++;
524 return uid;
525 }
526
527 static gid_t add_gid(struct udev_rules *rules, const char *group) {
528 unsigned int i;
529 gid_t gid = 0;
530 unsigned int off;
531 int r;
532
533 /* lookup, if we know it already */
534 for (i = 0; i < rules->gids_cur; i++) {
535 off = rules->gids[i].name_off;
536 if (streq(rules_str(rules, off), group)) {
537 gid = rules->gids[i].gid;
538 return gid;
539 }
540 }
541 r = get_group_creds(&group, &gid);
542 if (r < 0)
543 log_unknown_owner(r, "group", group);
544
545 /* grow buffer if needed */
546 if (rules->gids_cur+1 >= rules->gids_max) {
547 struct uid_gid *gids;
548 unsigned int add;
549
550 /* double the buffer size */
551 add = rules->gids_max;
552 if (add < 1)
553 add = 8;
554
555 gids = realloc(rules->gids, (rules->gids_max + add ) * sizeof(struct uid_gid));
556 if (gids == NULL)
557 return gid;
558 rules->gids = gids;
559 rules->gids_max += add;
560 }
561 rules->gids[rules->gids_cur].gid = gid;
562 off = rules_add_string(rules, group);
563 if (off <= 0)
564 return gid;
565 rules->gids[rules->gids_cur].name_off = off;
566 rules->gids_cur++;
567 return gid;
568 }
569
570 static int import_property_from_string(struct udev_device *dev, char *line) {
571 char *key;
572 char *val;
573 size_t len;
574
575 /* find key */
576 key = line;
577 while (isspace(key[0]))
578 key++;
579
580 /* comment or empty line */
581 if (key[0] == '#' || key[0] == '\0')
582 return -1;
583
584 /* split key/value */
585 val = strchr(key, '=');
586 if (val == NULL)
587 return -1;
588 val[0] = '\0';
589 val++;
590
591 /* find value */
592 while (isspace(val[0]))
593 val++;
594
595 /* terminate key */
596 len = strlen(key);
597 if (len == 0)
598 return -1;
599 while (isspace(key[len-1]))
600 len--;
601 key[len] = '\0';
602
603 /* terminate value */
604 len = strlen(val);
605 if (len == 0)
606 return -1;
607 while (isspace(val[len-1]))
608 len--;
609 val[len] = '\0';
610
611 if (len == 0)
612 return -1;
613
614 /* unquote */
615 if (val[0] == '"' || val[0] == '\'') {
616 if (len == 1 || val[len-1] != val[0]) {
617 log_debug("inconsistent quoting: '%s', skip", line);
618 return -1;
619 }
620 val[len-1] = '\0';
621 val++;
622 }
623
624 udev_device_add_property(dev, key, val);
625
626 return 0;
627 }
628
629 static int import_file_into_properties(struct udev_device *dev, const char *filename) {
630 FILE *f;
631 char line[UTIL_LINE_SIZE];
632
633 f = fopen(filename, "re");
634 if (f == NULL)
635 return -1;
636 while (fgets(line, sizeof(line), f) != NULL)
637 import_property_from_string(dev, line);
638 fclose(f);
639 return 0;
640 }
641
642 static int import_program_into_properties(struct udev_event *event,
643 usec_t timeout_usec,
644 usec_t timeout_warn_usec,
645 const char *program) {
646 char result[UTIL_LINE_SIZE];
647 char *line;
648 int err;
649
650 err = udev_event_spawn(event, timeout_usec, timeout_warn_usec, true, program, result, sizeof(result));
651 if (err < 0)
652 return err;
653
654 line = result;
655 while (line != NULL) {
656 char *pos;
657
658 pos = strchr(line, '\n');
659 if (pos != NULL) {
660 pos[0] = '\0';
661 pos = &pos[1];
662 }
663 import_property_from_string(event->dev, line);
664 line = pos;
665 }
666 return 0;
667 }
668
669 static int import_parent_into_properties(struct udev_device *dev, const char *filter) {
670 struct udev_device *dev_parent;
671 struct udev_list_entry *list_entry;
672
673 assert(dev);
674 assert(filter);
675
676 dev_parent = udev_device_get_parent(dev);
677 if (dev_parent == NULL)
678 return -1;
679
680 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev_parent)) {
681 const char *key = udev_list_entry_get_name(list_entry);
682 const char *val = udev_list_entry_get_value(list_entry);
683
684 if (fnmatch(filter, key, 0) == 0)
685 udev_device_add_property(dev, key, val);
686 }
687 return 0;
688 }
689
690 static void attr_subst_subdir(char *attr, size_t len) {
691 const char *pos, *tail, *path;
692 _cleanup_closedir_ DIR *dir = NULL;
693 struct dirent *dent;
694
695 pos = strstr(attr, "/*/");
696 if (!pos)
697 return;
698
699 tail = pos + 2;
700 path = strndupa(attr, pos - attr + 1); /* include slash at end */
701 dir = opendir(path);
702 if (dir == NULL)
703 return;
704
705 FOREACH_DIRENT_ALL(dent, dir, break)
706 if (dent->d_name[0] != '.') {
707 char n[strlen(dent->d_name) + strlen(tail) + 1];
708
709 strscpyl(n, sizeof n, dent->d_name, tail, NULL);
710 if (faccessat(dirfd(dir), n, F_OK, 0) == 0) {
711 strscpyl(attr, len, path, n, NULL);
712 break;
713 }
714 }
715 }
716
717 static int get_key(struct udev *udev, char **line, char **key, enum operation_type *op, char **value) {
718 char *linepos;
719 char *temp;
720
721 linepos = *line;
722 if (linepos == NULL || linepos[0] == '\0')
723 return -1;
724
725 /* skip whitespace */
726 while (isspace(linepos[0]) || linepos[0] == ',')
727 linepos++;
728
729 /* get the key */
730 if (linepos[0] == '\0')
731 return -1;
732 *key = linepos;
733
734 for (;;) {
735 linepos++;
736 if (linepos[0] == '\0')
737 return -1;
738 if (isspace(linepos[0]))
739 break;
740 if (linepos[0] == '=')
741 break;
742 if ((linepos[0] == '+') || (linepos[0] == '-') || (linepos[0] == '!') || (linepos[0] == ':'))
743 if (linepos[1] == '=')
744 break;
745 }
746
747 /* remember end of key */
748 temp = linepos;
749
750 /* skip whitespace after key */
751 while (isspace(linepos[0]))
752 linepos++;
753 if (linepos[0] == '\0')
754 return -1;
755
756 /* get operation type */
757 if (linepos[0] == '=' && linepos[1] == '=') {
758 *op = OP_MATCH;
759 linepos += 2;
760 } else if (linepos[0] == '!' && linepos[1] == '=') {
761 *op = OP_NOMATCH;
762 linepos += 2;
763 } else if (linepos[0] == '+' && linepos[1] == '=') {
764 *op = OP_ADD;
765 linepos += 2;
766 } else if (linepos[0] == '-' && linepos[1] == '=') {
767 *op = OP_REMOVE;
768 linepos += 2;
769 } else if (linepos[0] == '=') {
770 *op = OP_ASSIGN;
771 linepos++;
772 } else if (linepos[0] == ':' && linepos[1] == '=') {
773 *op = OP_ASSIGN_FINAL;
774 linepos += 2;
775 } else
776 return -1;
777
778 /* terminate key */
779 temp[0] = '\0';
780
781 /* skip whitespace after operator */
782 while (isspace(linepos[0]))
783 linepos++;
784 if (linepos[0] == '\0')
785 return -1;
786
787 /* get the value */
788 if (linepos[0] == '"')
789 linepos++;
790 else
791 return -1;
792 *value = linepos;
793
794 /* terminate */
795 temp = strchr(linepos, '"');
796 if (!temp)
797 return -1;
798 temp[0] = '\0';
799 temp++;
800
801 /* move line to next key */
802 *line = temp;
803 return 0;
804 }
805
806 /* extract possible KEY{attr} */
807 static const char *get_key_attribute(struct udev *udev, char *str) {
808 char *pos;
809 char *attr;
810
811 attr = strchr(str, '{');
812 if (attr != NULL) {
813 attr++;
814 pos = strchr(attr, '}');
815 if (pos == NULL) {
816 log_error("Missing closing brace for format");
817 return NULL;
818 }
819 pos[0] = '\0';
820 return attr;
821 }
822 return NULL;
823 }
824
825 static void rule_add_key(struct rule_tmp *rule_tmp, enum token_type type,
826 enum operation_type op,
827 const char *value, const void *data) {
828 struct token *token = rule_tmp->token + rule_tmp->token_cur;
829 const char *attr = NULL;
830
831 assert(rule_tmp->token_cur < ELEMENTSOF(rule_tmp->token));
832 memzero(token, sizeof(struct token));
833
834 switch (type) {
835 case TK_M_ACTION:
836 case TK_M_DEVPATH:
837 case TK_M_KERNEL:
838 case TK_M_SUBSYSTEM:
839 case TK_M_DRIVER:
840 case TK_M_WAITFOR:
841 case TK_M_DEVLINK:
842 case TK_M_NAME:
843 case TK_M_KERNELS:
844 case TK_M_SUBSYSTEMS:
845 case TK_M_DRIVERS:
846 case TK_M_TAGS:
847 case TK_M_PROGRAM:
848 case TK_M_IMPORT_FILE:
849 case TK_M_IMPORT_PROG:
850 case TK_M_IMPORT_DB:
851 case TK_M_IMPORT_CMDLINE:
852 case TK_M_IMPORT_PARENT:
853 case TK_M_RESULT:
854 case TK_A_OWNER:
855 case TK_A_GROUP:
856 case TK_A_MODE:
857 case TK_A_DEVLINK:
858 case TK_A_NAME:
859 case TK_A_GOTO:
860 case TK_M_TAG:
861 case TK_A_TAG:
862 case TK_A_STATIC_NODE:
863 token->key.value_off = rules_add_string(rule_tmp->rules, value);
864 break;
865 case TK_M_IMPORT_BUILTIN:
866 token->key.value_off = rules_add_string(rule_tmp->rules, value);
867 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
868 break;
869 case TK_M_ENV:
870 case TK_M_ATTR:
871 case TK_M_SYSCTL:
872 case TK_M_ATTRS:
873 case TK_A_ATTR:
874 case TK_A_SYSCTL:
875 case TK_A_ENV:
876 case TK_A_SECLABEL:
877 attr = data;
878 token->key.value_off = rules_add_string(rule_tmp->rules, value);
879 token->key.attr_off = rules_add_string(rule_tmp->rules, attr);
880 break;
881 case TK_M_TEST:
882 token->key.value_off = rules_add_string(rule_tmp->rules, value);
883 if (data != NULL)
884 token->key.mode = *(mode_t *)data;
885 break;
886 case TK_A_STRING_ESCAPE_NONE:
887 case TK_A_STRING_ESCAPE_REPLACE:
888 case TK_A_DB_PERSIST:
889 break;
890 case TK_A_RUN_BUILTIN:
891 case TK_A_RUN_PROGRAM:
892 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
893 token->key.value_off = rules_add_string(rule_tmp->rules, value);
894 break;
895 case TK_A_INOTIFY_WATCH:
896 case TK_A_DEVLINK_PRIO:
897 token->key.devlink_prio = *(int *)data;
898 break;
899 case TK_A_OWNER_ID:
900 token->key.uid = *(uid_t *)data;
901 break;
902 case TK_A_GROUP_ID:
903 token->key.gid = *(gid_t *)data;
904 break;
905 case TK_A_MODE_ID:
906 token->key.mode = *(mode_t *)data;
907 break;
908 case TK_RULE:
909 case TK_M_PARENTS_MIN:
910 case TK_M_PARENTS_MAX:
911 case TK_M_MAX:
912 case TK_END:
913 case TK_UNSET:
914 assert_not_reached("wrong type");
915 }
916
917 if (value != NULL && type < TK_M_MAX) {
918 /* check if we need to split or call fnmatch() while matching rules */
919 enum string_glob_type glob;
920 int has_split;
921 int has_glob;
922
923 has_split = (strchr(value, '|') != NULL);
924 has_glob = string_is_glob(value);
925 if (has_split && has_glob) {
926 glob = GL_SPLIT_GLOB;
927 } else if (has_split) {
928 glob = GL_SPLIT;
929 } else if (has_glob) {
930 if (streq(value, "?*"))
931 glob = GL_SOMETHING;
932 else
933 glob = GL_GLOB;
934 } else {
935 glob = GL_PLAIN;
936 }
937 token->key.glob = glob;
938 }
939
940 if (value != NULL && type > TK_M_MAX) {
941 /* check if assigned value has substitution chars */
942 if (value[0] == '[')
943 token->key.subst = SB_SUBSYS;
944 else if (strchr(value, '%') != NULL || strchr(value, '$') != NULL)
945 token->key.subst = SB_FORMAT;
946 else
947 token->key.subst = SB_NONE;
948 }
949
950 if (attr != NULL) {
951 /* check if property/attribute name has substitution chars */
952 if (attr[0] == '[')
953 token->key.attrsubst = SB_SUBSYS;
954 else if (strchr(attr, '%') != NULL || strchr(attr, '$') != NULL)
955 token->key.attrsubst = SB_FORMAT;
956 else
957 token->key.attrsubst = SB_NONE;
958 }
959
960 token->key.type = type;
961 token->key.op = op;
962 rule_tmp->token_cur++;
963 }
964
965 static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp) {
966 unsigned int i;
967 unsigned int start = 0;
968 unsigned int end = rule_tmp->token_cur;
969
970 for (i = 0; i < rule_tmp->token_cur; i++) {
971 enum token_type next_val = TK_UNSET;
972 unsigned int next_idx = 0;
973 unsigned int j;
974
975 /* find smallest value */
976 for (j = start; j < end; j++) {
977 if (rule_tmp->token[j].type == TK_UNSET)
978 continue;
979 if (next_val == TK_UNSET || rule_tmp->token[j].type < next_val) {
980 next_val = rule_tmp->token[j].type;
981 next_idx = j;
982 }
983 }
984
985 /* add token and mark done */
986 if (add_token(rules, &rule_tmp->token[next_idx]) != 0)
987 return -1;
988 rule_tmp->token[next_idx].type = TK_UNSET;
989
990 /* shrink range */
991 if (next_idx == start)
992 start++;
993 if (next_idx+1 == end)
994 end--;
995 }
996 return 0;
997 }
998
999 #define LOG_RULE_ERROR(fmt, ...) log_error("Invalid rule %s:%u: " fmt, filename, lineno, ##__VA_ARGS__)
1000 #define LOG_RULE_WARNING(fmt, ...) log_warning("%s:%u: " fmt, filename, lineno, ##__VA_ARGS__)
1001 #define LOG_RULE_DEBUG(fmt, ...) log_debug("%s:%u: " fmt, filename, lineno, ##__VA_ARGS__)
1002 #define LOG_AND_RETURN(fmt, ...) { LOG_RULE_ERROR(fmt, __VA_ARGS__); return; }
1003
1004 static void add_rule(struct udev_rules *rules, char *line,
1005 const char *filename, unsigned int filename_off, unsigned int lineno) {
1006 char *linepos;
1007 const char *attr;
1008 struct rule_tmp rule_tmp = {
1009 .rules = rules,
1010 .rule.type = TK_RULE,
1011 };
1012
1013 /* the offset in the rule is limited to unsigned short */
1014 if (filename_off < USHRT_MAX)
1015 rule_tmp.rule.rule.filename_off = filename_off;
1016 rule_tmp.rule.rule.filename_line = lineno;
1017
1018 linepos = line;
1019 for (;;) {
1020 char *key;
1021 char *value;
1022 enum operation_type op;
1023
1024 if (get_key(rules->udev, &linepos, &key, &op, &value) != 0) {
1025 /* Avoid erroring on trailing whitespace. This is probably rare
1026 * so save the work for the error case instead of always trying
1027 * to strip the trailing whitespace with strstrip(). */
1028 while (isblank(*linepos))
1029 linepos++;
1030
1031 /* If we aren't at the end of the line, this is a parsing error.
1032 * Make a best effort to describe where the problem is. */
1033 if (!strchr(NEWLINE, *linepos)) {
1034 char buf[2] = {*linepos};
1035 _cleanup_free_ char *tmp;
1036
1037 tmp = cescape(buf);
1038 log_error("invalid key/value pair in file %s on line %u, starting at character %tu ('%s')",
1039 filename, lineno, linepos - line + 1, tmp);
1040 if (*linepos == '#')
1041 log_error("hint: comments can only start at beginning of line");
1042 }
1043 break;
1044 }
1045
1046 if (rule_tmp.token_cur >= ELEMENTSOF(rule_tmp.token))
1047 LOG_AND_RETURN("temporary rule array too small, aborting event processing with %u items", rule_tmp.token_cur);
1048
1049 if (streq(key, "ACTION")) {
1050 if (op > OP_MATCH_MAX)
1051 LOG_AND_RETURN("invalid %s operation", key);
1052
1053 rule_add_key(&rule_tmp, TK_M_ACTION, op, value, NULL);
1054
1055 } else if (streq(key, "DEVPATH")) {
1056 if (op > OP_MATCH_MAX)
1057 LOG_AND_RETURN("invalid %s operation", key);
1058
1059 rule_add_key(&rule_tmp, TK_M_DEVPATH, op, value, NULL);
1060
1061 } else if (streq(key, "KERNEL")) {
1062 if (op > OP_MATCH_MAX)
1063 LOG_AND_RETURN("invalid %s operation", key);
1064
1065 rule_add_key(&rule_tmp, TK_M_KERNEL, op, value, NULL);
1066
1067 } else if (streq(key, "SUBSYSTEM")) {
1068 if (op > OP_MATCH_MAX)
1069 LOG_AND_RETURN("invalid %s operation", key);
1070
1071 /* bus, class, subsystem events should all be the same */
1072 if (STR_IN_SET(value, "subsystem", "bus", "class")) {
1073 if (!streq(value, "subsystem"))
1074 LOG_RULE_WARNING("'%s' must be specified as 'subsystem'; please fix", value);
1075
1076 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, "subsystem|class|bus", NULL);
1077 } else
1078 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, value, NULL);
1079
1080 } else if (streq(key, "DRIVER")) {
1081 if (op > OP_MATCH_MAX)
1082 LOG_AND_RETURN("invalid %s operation", key);
1083
1084 rule_add_key(&rule_tmp, TK_M_DRIVER, op, value, NULL);
1085
1086 } else if (startswith(key, "ATTR{")) {
1087 attr = get_key_attribute(rules->udev, key + strlen("ATTR"));
1088 if (attr == NULL)
1089 LOG_AND_RETURN("error parsing %s attribute", "ATTR");
1090
1091 if (op == OP_REMOVE)
1092 LOG_AND_RETURN("invalid %s operation", "ATTR");
1093
1094 if (op < OP_MATCH_MAX)
1095 rule_add_key(&rule_tmp, TK_M_ATTR, op, value, attr);
1096 else
1097 rule_add_key(&rule_tmp, TK_A_ATTR, op, value, attr);
1098
1099 } else if (startswith(key, "SYSCTL{")) {
1100 attr = get_key_attribute(rules->udev, key + strlen("SYSCTL"));
1101 if (attr == NULL)
1102 LOG_AND_RETURN("error parsing %s attribute", "ATTR");
1103
1104 if (op == OP_REMOVE)
1105 LOG_AND_RETURN("invalid %s operation", "ATTR");
1106
1107 if (op < OP_MATCH_MAX)
1108 rule_add_key(&rule_tmp, TK_M_SYSCTL, op, value, attr);
1109 else
1110 rule_add_key(&rule_tmp, TK_A_SYSCTL, op, value, attr);
1111
1112 } else if (startswith(key, "SECLABEL{")) {
1113 attr = get_key_attribute(rules->udev, key + strlen("SECLABEL"));
1114 if (attr == NULL)
1115 LOG_AND_RETURN("error parsing %s attribute", "SECLABEL");
1116
1117 if (op == OP_REMOVE)
1118 LOG_AND_RETURN("invalid %s operation", "SECLABEL");
1119
1120 rule_add_key(&rule_tmp, TK_A_SECLABEL, op, value, attr);
1121
1122 } else if (streq(key, "KERNELS")) {
1123 if (op > OP_MATCH_MAX)
1124 LOG_AND_RETURN("invalid %s operation", key);
1125
1126 rule_add_key(&rule_tmp, TK_M_KERNELS, op, value, NULL);
1127
1128 } else if (streq(key, "SUBSYSTEMS")) {
1129 if (op > OP_MATCH_MAX)
1130 LOG_AND_RETURN("invalid %s operation", key);
1131
1132 rule_add_key(&rule_tmp, TK_M_SUBSYSTEMS, op, value, NULL);
1133
1134 } else if (streq(key, "DRIVERS")) {
1135 if (op > OP_MATCH_MAX)
1136 LOG_AND_RETURN("invalid %s operation", key);
1137
1138 rule_add_key(&rule_tmp, TK_M_DRIVERS, op, value, NULL);
1139
1140 } else if (startswith(key, "ATTRS{")) {
1141 if (op > OP_MATCH_MAX)
1142 LOG_AND_RETURN("invalid %s operation", "ATTRS");
1143
1144 attr = get_key_attribute(rules->udev, key + strlen("ATTRS"));
1145 if (attr == NULL)
1146 LOG_AND_RETURN("error parsing %s attribute", "ATTRS");
1147
1148 if (startswith(attr, "device/"))
1149 LOG_RULE_WARNING("'device' link may not be available in future kernels; please fix");
1150 if (strstr(attr, "../") != NULL)
1151 LOG_RULE_WARNING("direct reference to parent sysfs directory, may break in future kernels; please fix");
1152 rule_add_key(&rule_tmp, TK_M_ATTRS, op, value, attr);
1153
1154 } else if (streq(key, "TAGS")) {
1155 if (op > OP_MATCH_MAX)
1156 LOG_AND_RETURN("invalid %s operation", key);
1157
1158 rule_add_key(&rule_tmp, TK_M_TAGS, op, value, NULL);
1159
1160 } else if (startswith(key, "ENV{")) {
1161 attr = get_key_attribute(rules->udev, key + strlen("ENV"));
1162 if (attr == NULL)
1163 LOG_AND_RETURN("error parsing %s attribute", "ENV");
1164
1165 if (op == OP_REMOVE)
1166 LOG_AND_RETURN("invalid %s operation", "ENV");
1167
1168 if (op < OP_MATCH_MAX)
1169 rule_add_key(&rule_tmp, TK_M_ENV, op, value, attr);
1170 else {
1171 if (STR_IN_SET(attr,
1172 "ACTION",
1173 "SUBSYSTEM",
1174 "DEVTYPE",
1175 "MAJOR",
1176 "MINOR",
1177 "DRIVER",
1178 "IFINDEX",
1179 "DEVNAME",
1180 "DEVLINKS",
1181 "DEVPATH",
1182 "TAGS"))
1183 LOG_AND_RETURN("invalid ENV attribute, '%s' cannot be set", attr);
1184
1185 rule_add_key(&rule_tmp, TK_A_ENV, op, value, attr);
1186 }
1187
1188 } else if (streq(key, "TAG")) {
1189 if (op < OP_MATCH_MAX)
1190 rule_add_key(&rule_tmp, TK_M_TAG, op, value, NULL);
1191 else
1192 rule_add_key(&rule_tmp, TK_A_TAG, op, value, NULL);
1193
1194 } else if (streq(key, "PROGRAM")) {
1195 if (op == OP_REMOVE)
1196 LOG_AND_RETURN("invalid %s operation", key);
1197
1198 rule_add_key(&rule_tmp, TK_M_PROGRAM, op, value, NULL);
1199
1200 } else if (streq(key, "RESULT")) {
1201 if (op > OP_MATCH_MAX)
1202 LOG_AND_RETURN("invalid %s operation", key);
1203
1204 rule_add_key(&rule_tmp, TK_M_RESULT, op, value, NULL);
1205
1206 } else if (startswith(key, "IMPORT")) {
1207 attr = get_key_attribute(rules->udev, key + strlen("IMPORT"));
1208 if (attr == NULL) {
1209 LOG_RULE_WARNING("ignoring IMPORT{} with missing type");
1210 continue;
1211 }
1212 if (op == OP_REMOVE)
1213 LOG_AND_RETURN("invalid %s operation", "IMPORT");
1214
1215 if (streq(attr, "program")) {
1216 /* find known built-in command */
1217 if (value[0] != '/') {
1218 const enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1219
1220 if (cmd < UDEV_BUILTIN_MAX) {
1221 LOG_RULE_DEBUG("IMPORT found builtin '%s', replacing", value);
1222 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1223 continue;
1224 }
1225 }
1226 rule_add_key(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL);
1227 } else if (streq(attr, "builtin")) {
1228 const enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1229
1230 if (cmd >= UDEV_BUILTIN_MAX)
1231 LOG_RULE_WARNING("IMPORT{builtin} '%s' unknown", value);
1232 else
1233 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1234 } else if (streq(attr, "file"))
1235 rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL);
1236 else if (streq(attr, "db"))
1237 rule_add_key(&rule_tmp, TK_M_IMPORT_DB, op, value, NULL);
1238 else if (streq(attr, "cmdline"))
1239 rule_add_key(&rule_tmp, TK_M_IMPORT_CMDLINE, op, value, NULL);
1240 else if (streq(attr, "parent"))
1241 rule_add_key(&rule_tmp, TK_M_IMPORT_PARENT, op, value, NULL);
1242 else
1243 LOG_RULE_ERROR("ignoring unknown %s{} type '%s'", "IMPORT", attr);
1244
1245 } else if (startswith(key, "TEST")) {
1246 mode_t mode = 0;
1247
1248 if (op > OP_MATCH_MAX)
1249 LOG_AND_RETURN("invalid %s operation", "TEST");
1250
1251 attr = get_key_attribute(rules->udev, key + strlen("TEST"));
1252 if (attr != NULL) {
1253 mode = strtol(attr, NULL, 8);
1254 rule_add_key(&rule_tmp, TK_M_TEST, op, value, &mode);
1255 } else
1256 rule_add_key(&rule_tmp, TK_M_TEST, op, value, NULL);
1257
1258 } else if (startswith(key, "RUN")) {
1259 attr = get_key_attribute(rules->udev, key + strlen("RUN"));
1260 if (attr == NULL)
1261 attr = "program";
1262 if (op == OP_REMOVE)
1263 LOG_AND_RETURN("invalid %s operation", "RUN");
1264
1265 if (streq(attr, "builtin")) {
1266 const enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1267
1268 if (cmd < UDEV_BUILTIN_MAX)
1269 rule_add_key(&rule_tmp, TK_A_RUN_BUILTIN, op, value, &cmd);
1270 else
1271 LOG_RULE_ERROR("RUN{builtin}: '%s' unknown", value);
1272 } else if (streq(attr, "program")) {
1273 const enum udev_builtin_cmd cmd = UDEV_BUILTIN_MAX;
1274
1275 rule_add_key(&rule_tmp, TK_A_RUN_PROGRAM, op, value, &cmd);
1276 } else
1277 LOG_RULE_ERROR("ignoring unknown %s{} type '%s'", "RUN", attr);
1278
1279 } else if (streq(key, "LABEL")) {
1280 if (op == OP_REMOVE)
1281 LOG_AND_RETURN("invalid %s operation", key);
1282
1283 rule_tmp.rule.rule.label_off = rules_add_string(rules, value);
1284
1285 } else if (streq(key, "GOTO")) {
1286 if (op == OP_REMOVE)
1287 LOG_AND_RETURN("invalid %s operation", key);
1288
1289 rule_add_key(&rule_tmp, TK_A_GOTO, 0, value, NULL);
1290
1291 } else if (startswith(key, "NAME")) {
1292 if (op == OP_REMOVE)
1293 LOG_AND_RETURN("invalid %s operation", key);
1294
1295 if (op < OP_MATCH_MAX)
1296 rule_add_key(&rule_tmp, TK_M_NAME, op, value, NULL);
1297 else {
1298 if (streq(value, "%k")) {
1299 LOG_RULE_WARNING("NAME=\"%%k\" is ignored, because it breaks kernel supplied names; please remove");
1300 continue;
1301 }
1302 if (isempty(value)) {
1303 LOG_RULE_DEBUG("NAME=\"\" is ignored, because udev will not delete any device nodes; please remove");
1304 continue;
1305 }
1306 rule_add_key(&rule_tmp, TK_A_NAME, op, value, NULL);
1307 }
1308 rule_tmp.rule.rule.can_set_name = true;
1309
1310 } else if (streq(key, "SYMLINK")) {
1311 if (op == OP_REMOVE)
1312 LOG_AND_RETURN("invalid %s operation", key);
1313
1314 if (op < OP_MATCH_MAX)
1315 rule_add_key(&rule_tmp, TK_M_DEVLINK, op, value, NULL);
1316 else
1317 rule_add_key(&rule_tmp, TK_A_DEVLINK, op, value, NULL);
1318 rule_tmp.rule.rule.can_set_name = true;
1319
1320 } else if (streq(key, "OWNER")) {
1321 uid_t uid;
1322 char *endptr;
1323
1324 if (op == OP_REMOVE)
1325 LOG_AND_RETURN("invalid %s operation", key);
1326
1327 uid = strtoul(value, &endptr, 10);
1328 if (endptr[0] == '\0')
1329 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1330 else if (rules->resolve_names > 0 && strchr("$%", value[0]) == NULL) {
1331 uid = add_uid(rules, value);
1332 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1333 } else if (rules->resolve_names >= 0)
1334 rule_add_key(&rule_tmp, TK_A_OWNER, op, value, NULL);
1335
1336 rule_tmp.rule.rule.can_set_name = true;
1337
1338 } else if (streq(key, "GROUP")) {
1339 gid_t gid;
1340 char *endptr;
1341
1342 if (op == OP_REMOVE)
1343 LOG_AND_RETURN("invalid %s operation", key);
1344
1345 gid = strtoul(value, &endptr, 10);
1346 if (endptr[0] == '\0')
1347 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1348 else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1349 gid = add_gid(rules, value);
1350 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1351 } else if (rules->resolve_names >= 0)
1352 rule_add_key(&rule_tmp, TK_A_GROUP, op, value, NULL);
1353
1354 rule_tmp.rule.rule.can_set_name = true;
1355
1356 } else if (streq(key, "MODE")) {
1357 mode_t mode;
1358 char *endptr;
1359
1360 if (op == OP_REMOVE)
1361 LOG_AND_RETURN("invalid %s operation", key);
1362
1363 mode = strtol(value, &endptr, 8);
1364 if (endptr[0] == '\0')
1365 rule_add_key(&rule_tmp, TK_A_MODE_ID, op, NULL, &mode);
1366 else
1367 rule_add_key(&rule_tmp, TK_A_MODE, op, value, NULL);
1368 rule_tmp.rule.rule.can_set_name = true;
1369
1370 } else if (streq(key, "OPTIONS")) {
1371 const char *pos;
1372
1373 if (op == OP_REMOVE)
1374 LOG_AND_RETURN("invalid %s operation", key);
1375
1376 pos = strstr(value, "link_priority=");
1377 if (pos != NULL) {
1378 int prio = atoi(pos + strlen("link_priority="));
1379
1380 rule_add_key(&rule_tmp, TK_A_DEVLINK_PRIO, op, NULL, &prio);
1381 }
1382
1383 pos = strstr(value, "string_escape=");
1384 if (pos != NULL) {
1385 pos += strlen("string_escape=");
1386 if (startswith(pos, "none"))
1387 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_NONE, op, NULL, NULL);
1388 else if (startswith(pos, "replace"))
1389 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_REPLACE, op, NULL, NULL);
1390 }
1391
1392 pos = strstr(value, "db_persist");
1393 if (pos != NULL)
1394 rule_add_key(&rule_tmp, TK_A_DB_PERSIST, op, NULL, NULL);
1395
1396 pos = strstr(value, "nowatch");
1397 if (pos != NULL) {
1398 const int off = 0;
1399
1400 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &off);
1401 } else {
1402 pos = strstr(value, "watch");
1403 if (pos != NULL) {
1404 const int on = 1;
1405
1406 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &on);
1407 }
1408 }
1409
1410 pos = strstr(value, "static_node=");
1411 if (pos != NULL) {
1412 pos += strlen("static_node=");
1413 rule_add_key(&rule_tmp, TK_A_STATIC_NODE, op, pos, NULL);
1414 rule_tmp.rule.rule.has_static_node = true;
1415 }
1416
1417 } else
1418 LOG_AND_RETURN("unknown key '%s'", key);
1419 }
1420
1421 /* add rule token and sort tokens */
1422 rule_tmp.rule.rule.token_count = 1 + rule_tmp.token_cur;
1423 if (add_token(rules, &rule_tmp.rule) != 0 || sort_token(rules, &rule_tmp) != 0)
1424 LOG_RULE_ERROR("failed to add rule token");
1425 }
1426
1427 static int parse_file(struct udev_rules *rules, const char *filename) {
1428 _cleanup_fclose_ FILE *f = NULL;
1429 unsigned int first_token;
1430 unsigned int filename_off;
1431 char line[UTIL_LINE_SIZE];
1432 int line_nr = 0;
1433 unsigned int i;
1434
1435 f = fopen(filename, "re");
1436 if (!f) {
1437 if (errno == ENOENT)
1438 return 0;
1439 else
1440 return -errno;
1441 }
1442
1443 if (null_or_empty_fd(fileno(f))) {
1444 log_debug("Skipping empty file: %s", filename);
1445 return 0;
1446 } else
1447 log_debug("Reading rules file: %s", filename);
1448
1449 first_token = rules->token_cur;
1450 filename_off = rules_add_string(rules, filename);
1451
1452 while (fgets(line, sizeof(line), f) != NULL) {
1453 char *key;
1454 size_t len;
1455
1456 /* skip whitespace */
1457 line_nr++;
1458 key = line;
1459 while (isspace(key[0]))
1460 key++;
1461
1462 /* comment */
1463 if (key[0] == '#')
1464 continue;
1465
1466 len = strlen(line);
1467 if (len < 3)
1468 continue;
1469
1470 /* continue reading if backslash+newline is found */
1471 while (line[len-2] == '\\') {
1472 if (fgets(&line[len-2], (sizeof(line)-len)+2, f) == NULL)
1473 break;
1474 if (strlen(&line[len-2]) < 2)
1475 break;
1476 line_nr++;
1477 len = strlen(line);
1478 }
1479
1480 if (len+1 >= sizeof(line)) {
1481 log_error("line too long '%s':%u, ignored", filename, line_nr);
1482 continue;
1483 }
1484 add_rule(rules, key, filename, filename_off, line_nr);
1485 }
1486
1487 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1488 for (i = first_token+1; i < rules->token_cur; i++) {
1489 if (rules->tokens[i].type == TK_A_GOTO) {
1490 char *label = rules_str(rules, rules->tokens[i].key.value_off);
1491 unsigned int j;
1492
1493 for (j = i+1; j < rules->token_cur; j++) {
1494 if (rules->tokens[j].type != TK_RULE)
1495 continue;
1496 if (rules->tokens[j].rule.label_off == 0)
1497 continue;
1498 if (!streq(label, rules_str(rules, rules->tokens[j].rule.label_off)))
1499 continue;
1500 rules->tokens[i].key.rule_goto = j;
1501 break;
1502 }
1503 if (rules->tokens[i].key.rule_goto == 0)
1504 log_error("GOTO '%s' has no matching label in: '%s'", label, filename);
1505 }
1506 }
1507 return 0;
1508 }
1509
1510 struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names) {
1511 struct udev_rules *rules;
1512 struct udev_list file_list;
1513 struct token end_token;
1514 char **files, **f;
1515 int r;
1516
1517 rules = new0(struct udev_rules, 1);
1518 if (rules == NULL)
1519 return NULL;
1520 rules->udev = udev;
1521 rules->resolve_names = resolve_names;
1522 udev_list_init(udev, &file_list, true);
1523
1524 /* init token array and string buffer */
1525 rules->tokens = malloc(PREALLOC_TOKEN * sizeof(struct token));
1526 if (rules->tokens == NULL)
1527 return udev_rules_unref(rules);
1528 rules->token_max = PREALLOC_TOKEN;
1529
1530 rules->strbuf = strbuf_new();
1531 if (!rules->strbuf)
1532 return udev_rules_unref(rules);
1533
1534 udev_rules_check_timestamp(rules);
1535
1536 r = conf_files_list_strv(&files, ".rules", NULL, 0, rules_dirs);
1537 if (r < 0) {
1538 log_error_errno(r, "failed to enumerate rules files: %m");
1539 return udev_rules_unref(rules);
1540 }
1541
1542 /*
1543 * The offset value in the rules strct is limited; add all
1544 * rules file names to the beginning of the string buffer.
1545 */
1546 STRV_FOREACH(f, files)
1547 rules_add_string(rules, *f);
1548
1549 STRV_FOREACH(f, files)
1550 parse_file(rules, *f);
1551
1552 strv_free(files);
1553
1554 memzero(&end_token, sizeof(struct token));
1555 end_token.type = TK_END;
1556 add_token(rules, &end_token);
1557 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings",
1558 rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->strbuf->len);
1559
1560 /* cleanup temporary strbuf data */
1561 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used",
1562 rules->strbuf->in_count, rules->strbuf->in_len,
1563 rules->strbuf->dedup_count, rules->strbuf->dedup_len, rules->strbuf->nodes_count);
1564 strbuf_complete(rules->strbuf);
1565
1566 /* cleanup uid/gid cache */
1567 rules->uids = mfree(rules->uids);
1568 rules->uids_cur = 0;
1569 rules->uids_max = 0;
1570 rules->gids = mfree(rules->gids);
1571 rules->gids_cur = 0;
1572 rules->gids_max = 0;
1573
1574 dump_rules(rules);
1575 return rules;
1576 }
1577
1578 struct udev_rules *udev_rules_unref(struct udev_rules *rules) {
1579 if (rules == NULL)
1580 return NULL;
1581 free(rules->tokens);
1582 strbuf_cleanup(rules->strbuf);
1583 free(rules->uids);
1584 free(rules->gids);
1585 return mfree(rules);
1586 }
1587
1588 bool udev_rules_check_timestamp(struct udev_rules *rules) {
1589 if (!rules)
1590 return false;
1591
1592 return paths_check_timestamp(rules_dirs, &rules->dirs_ts_usec, true);
1593 }
1594
1595 static int match_key(struct udev_rules *rules, struct token *token, const char *val) {
1596 char *key_value = rules_str(rules, token->key.value_off);
1597 char *pos;
1598 bool match = false;
1599
1600 if (val == NULL)
1601 val = "";
1602
1603 switch (token->key.glob) {
1604 case GL_PLAIN:
1605 match = (streq(key_value, val));
1606 break;
1607 case GL_GLOB:
1608 match = (fnmatch(key_value, val, 0) == 0);
1609 break;
1610 case GL_SPLIT:
1611 {
1612 const char *s;
1613 size_t len;
1614
1615 s = rules_str(rules, token->key.value_off);
1616 len = strlen(val);
1617 for (;;) {
1618 const char *next;
1619
1620 next = strchr(s, '|');
1621 if (next != NULL) {
1622 size_t matchlen = (size_t)(next - s);
1623
1624 match = (matchlen == len && strneq(s, val, matchlen));
1625 if (match)
1626 break;
1627 } else {
1628 match = (streq(s, val));
1629 break;
1630 }
1631 s = &next[1];
1632 }
1633 break;
1634 }
1635 case GL_SPLIT_GLOB:
1636 {
1637 char value[UTIL_PATH_SIZE];
1638
1639 strscpy(value, sizeof(value), rules_str(rules, token->key.value_off));
1640 key_value = value;
1641 while (key_value != NULL) {
1642 pos = strchr(key_value, '|');
1643 if (pos != NULL) {
1644 pos[0] = '\0';
1645 pos = &pos[1];
1646 }
1647 match = (fnmatch(key_value, val, 0) == 0);
1648 if (match)
1649 break;
1650 key_value = pos;
1651 }
1652 break;
1653 }
1654 case GL_SOMETHING:
1655 match = (val[0] != '\0');
1656 break;
1657 case GL_UNSET:
1658 return -1;
1659 }
1660
1661 if (match && (token->key.op == OP_MATCH))
1662 return 0;
1663 if (!match && (token->key.op == OP_NOMATCH))
1664 return 0;
1665 return -1;
1666 }
1667
1668 static int match_attr(struct udev_rules *rules, struct udev_device *dev, struct udev_event *event, struct token *cur) {
1669 const char *name;
1670 char nbuf[UTIL_NAME_SIZE];
1671 const char *value;
1672 char vbuf[UTIL_NAME_SIZE];
1673 size_t len;
1674
1675 name = rules_str(rules, cur->key.attr_off);
1676 switch (cur->key.attrsubst) {
1677 case SB_FORMAT:
1678 udev_event_apply_format(event, name, nbuf, sizeof(nbuf), false);
1679 name = nbuf;
1680 /* fall through */
1681 case SB_NONE:
1682 value = udev_device_get_sysattr_value(dev, name);
1683 if (value == NULL)
1684 return -1;
1685 break;
1686 case SB_SUBSYS:
1687 if (util_resolve_subsys_kernel(event->udev, name, vbuf, sizeof(vbuf), 1) != 0)
1688 return -1;
1689 value = vbuf;
1690 break;
1691 default:
1692 return -1;
1693 }
1694
1695 /* remove trailing whitespace, if not asked to match for it */
1696 len = strlen(value);
1697 if (len > 0 && isspace(value[len-1])) {
1698 const char *key_value;
1699 size_t klen;
1700
1701 key_value = rules_str(rules, cur->key.value_off);
1702 klen = strlen(key_value);
1703 if (klen > 0 && !isspace(key_value[klen-1])) {
1704 if (value != vbuf) {
1705 strscpy(vbuf, sizeof(vbuf), value);
1706 value = vbuf;
1707 }
1708 while (len > 0 && isspace(vbuf[--len]))
1709 vbuf[len] = '\0';
1710 }
1711 }
1712
1713 return match_key(rules, cur, value);
1714 }
1715
1716 enum escape_type {
1717 ESCAPE_UNSET,
1718 ESCAPE_NONE,
1719 ESCAPE_REPLACE,
1720 };
1721
1722 void udev_rules_apply_to_event(struct udev_rules *rules,
1723 struct udev_event *event,
1724 usec_t timeout_usec,
1725 usec_t timeout_warn_usec,
1726 struct udev_list *properties_list) {
1727 struct token *cur;
1728 struct token *rule;
1729 enum escape_type esc = ESCAPE_UNSET;
1730 bool can_set_name;
1731
1732 if (rules->tokens == NULL)
1733 return;
1734
1735 can_set_name = ((!streq(udev_device_get_action(event->dev), "remove")) &&
1736 (major(udev_device_get_devnum(event->dev)) > 0 ||
1737 udev_device_get_ifindex(event->dev) > 0));
1738
1739 /* loop through token list, match, run actions or forward to next rule */
1740 cur = &rules->tokens[0];
1741 rule = cur;
1742 for (;;) {
1743 dump_token(rules, cur);
1744 switch (cur->type) {
1745 case TK_RULE:
1746 /* current rule */
1747 rule = cur;
1748 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1749 if (!can_set_name && rule->rule.can_set_name)
1750 goto nomatch;
1751 esc = ESCAPE_UNSET;
1752 break;
1753 case TK_M_ACTION:
1754 if (match_key(rules, cur, udev_device_get_action(event->dev)) != 0)
1755 goto nomatch;
1756 break;
1757 case TK_M_DEVPATH:
1758 if (match_key(rules, cur, udev_device_get_devpath(event->dev)) != 0)
1759 goto nomatch;
1760 break;
1761 case TK_M_KERNEL:
1762 if (match_key(rules, cur, udev_device_get_sysname(event->dev)) != 0)
1763 goto nomatch;
1764 break;
1765 case TK_M_DEVLINK: {
1766 struct udev_list_entry *list_entry;
1767 bool match = false;
1768
1769 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(event->dev)) {
1770 const char *devlink;
1771
1772 devlink = udev_list_entry_get_name(list_entry) + strlen("/dev/");
1773 if (match_key(rules, cur, devlink) == 0) {
1774 match = true;
1775 break;
1776 }
1777 }
1778 if (!match)
1779 goto nomatch;
1780 break;
1781 }
1782 case TK_M_NAME:
1783 if (match_key(rules, cur, event->name) != 0)
1784 goto nomatch;
1785 break;
1786 case TK_M_ENV: {
1787 const char *key_name = rules_str(rules, cur->key.attr_off);
1788 const char *value;
1789
1790 value = udev_device_get_property_value(event->dev, key_name);
1791
1792 /* check global properties */
1793 if (!value && properties_list) {
1794 struct udev_list_entry *list_entry;
1795
1796 list_entry = udev_list_get_entry(properties_list);
1797 list_entry = udev_list_entry_get_by_name(list_entry, key_name);
1798 if (list_entry != NULL)
1799 value = udev_list_entry_get_value(list_entry);
1800 }
1801
1802 if (!value)
1803 value = "";
1804 if (match_key(rules, cur, value))
1805 goto nomatch;
1806 break;
1807 }
1808 case TK_M_TAG: {
1809 struct udev_list_entry *list_entry;
1810 bool match = false;
1811
1812 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(event->dev)) {
1813 if (streq(rules_str(rules, cur->key.value_off), udev_list_entry_get_name(list_entry))) {
1814 match = true;
1815 break;
1816 }
1817 }
1818 if ((!match && (cur->key.op != OP_NOMATCH)) ||
1819 (match && (cur->key.op == OP_NOMATCH)))
1820 goto nomatch;
1821 break;
1822 }
1823 case TK_M_SUBSYSTEM:
1824 if (match_key(rules, cur, udev_device_get_subsystem(event->dev)) != 0)
1825 goto nomatch;
1826 break;
1827 case TK_M_DRIVER:
1828 if (match_key(rules, cur, udev_device_get_driver(event->dev)) != 0)
1829 goto nomatch;
1830 break;
1831 case TK_M_ATTR:
1832 if (match_attr(rules, event->dev, event, cur) != 0)
1833 goto nomatch;
1834 break;
1835 case TK_M_SYSCTL: {
1836 char filename[UTIL_PATH_SIZE];
1837 _cleanup_free_ char *value = NULL;
1838 size_t len;
1839
1840 udev_event_apply_format(event, rules_str(rules, cur->key.attr_off), filename, sizeof(filename), false);
1841 sysctl_normalize(filename);
1842 if (sysctl_read(filename, &value) < 0)
1843 goto nomatch;
1844
1845 len = strlen(value);
1846 while (len > 0 && isspace(value[--len]))
1847 value[len] = '\0';
1848 if (match_key(rules, cur, value) != 0)
1849 goto nomatch;
1850 break;
1851 }
1852 case TK_M_KERNELS:
1853 case TK_M_SUBSYSTEMS:
1854 case TK_M_DRIVERS:
1855 case TK_M_ATTRS:
1856 case TK_M_TAGS: {
1857 struct token *next;
1858
1859 /* get whole sequence of parent matches */
1860 next = cur;
1861 while (next->type > TK_M_PARENTS_MIN && next->type < TK_M_PARENTS_MAX)
1862 next++;
1863
1864 /* loop over parents */
1865 event->dev_parent = event->dev;
1866 for (;;) {
1867 struct token *key;
1868
1869 /* loop over sequence of parent match keys */
1870 for (key = cur; key < next; key++ ) {
1871 dump_token(rules, key);
1872 switch(key->type) {
1873 case TK_M_KERNELS:
1874 if (match_key(rules, key, udev_device_get_sysname(event->dev_parent)) != 0)
1875 goto try_parent;
1876 break;
1877 case TK_M_SUBSYSTEMS:
1878 if (match_key(rules, key, udev_device_get_subsystem(event->dev_parent)) != 0)
1879 goto try_parent;
1880 break;
1881 case TK_M_DRIVERS:
1882 if (match_key(rules, key, udev_device_get_driver(event->dev_parent)) != 0)
1883 goto try_parent;
1884 break;
1885 case TK_M_ATTRS:
1886 if (match_attr(rules, event->dev_parent, event, key) != 0)
1887 goto try_parent;
1888 break;
1889 case TK_M_TAGS: {
1890 bool match = udev_device_has_tag(event->dev_parent, rules_str(rules, cur->key.value_off));
1891
1892 if (match && key->key.op == OP_NOMATCH)
1893 goto try_parent;
1894 if (!match && key->key.op == OP_MATCH)
1895 goto try_parent;
1896 break;
1897 }
1898 default:
1899 goto nomatch;
1900 }
1901 }
1902 break;
1903
1904 try_parent:
1905 event->dev_parent = udev_device_get_parent(event->dev_parent);
1906 if (event->dev_parent == NULL)
1907 goto nomatch;
1908 }
1909 /* move behind our sequence of parent match keys */
1910 cur = next;
1911 continue;
1912 }
1913 case TK_M_TEST: {
1914 char filename[UTIL_PATH_SIZE];
1915 struct stat statbuf;
1916 int match;
1917
1918 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), filename, sizeof(filename), false);
1919 if (util_resolve_subsys_kernel(event->udev, filename, filename, sizeof(filename), 0) != 0) {
1920 if (filename[0] != '/') {
1921 char tmp[UTIL_PATH_SIZE];
1922
1923 strscpy(tmp, sizeof(tmp), filename);
1924 strscpyl(filename, sizeof(filename),
1925 udev_device_get_syspath(event->dev), "/", tmp, NULL);
1926 }
1927 }
1928 attr_subst_subdir(filename, sizeof(filename));
1929
1930 match = (stat(filename, &statbuf) == 0);
1931 if (match && cur->key.mode > 0)
1932 match = ((statbuf.st_mode & cur->key.mode) > 0);
1933 if (match && cur->key.op == OP_NOMATCH)
1934 goto nomatch;
1935 if (!match && cur->key.op == OP_MATCH)
1936 goto nomatch;
1937 break;
1938 }
1939 case TK_M_PROGRAM: {
1940 char program[UTIL_PATH_SIZE];
1941 char result[UTIL_LINE_SIZE];
1942
1943 event->program_result = mfree(event->program_result);
1944 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), program, sizeof(program), false);
1945 log_debug("PROGRAM '%s' %s:%u",
1946 program,
1947 rules_str(rules, rule->rule.filename_off),
1948 rule->rule.filename_line);
1949
1950 if (udev_event_spawn(event, timeout_usec, timeout_warn_usec, true, program, result, sizeof(result)) < 0) {
1951 if (cur->key.op != OP_NOMATCH)
1952 goto nomatch;
1953 } else {
1954 int count;
1955
1956 util_remove_trailing_chars(result, '\n');
1957 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
1958 count = util_replace_chars(result, UDEV_ALLOWED_CHARS_INPUT);
1959 if (count > 0)
1960 log_debug("%i character(s) replaced" , count);
1961 }
1962 event->program_result = strdup(result);
1963 if (cur->key.op == OP_NOMATCH)
1964 goto nomatch;
1965 }
1966 break;
1967 }
1968 case TK_M_IMPORT_FILE: {
1969 char import[UTIL_PATH_SIZE];
1970
1971 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import), false);
1972 if (import_file_into_properties(event->dev, import) != 0)
1973 if (cur->key.op != OP_NOMATCH)
1974 goto nomatch;
1975 break;
1976 }
1977 case TK_M_IMPORT_PROG: {
1978 char import[UTIL_PATH_SIZE];
1979
1980 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import), false);
1981 log_debug("IMPORT '%s' %s:%u",
1982 import,
1983 rules_str(rules, rule->rule.filename_off),
1984 rule->rule.filename_line);
1985
1986 if (import_program_into_properties(event, timeout_usec, timeout_warn_usec, import) != 0)
1987 if (cur->key.op != OP_NOMATCH)
1988 goto nomatch;
1989 break;
1990 }
1991 case TK_M_IMPORT_BUILTIN: {
1992 char command[UTIL_PATH_SIZE];
1993
1994 if (udev_builtin_run_once(cur->key.builtin_cmd)) {
1995 /* check if we ran already */
1996 if (event->builtin_run & (1 << cur->key.builtin_cmd)) {
1997 log_debug("IMPORT builtin skip '%s' %s:%u",
1998 udev_builtin_name(cur->key.builtin_cmd),
1999 rules_str(rules, rule->rule.filename_off),
2000 rule->rule.filename_line);
2001 /* return the result from earlier run */
2002 if (event->builtin_ret & (1 << cur->key.builtin_cmd))
2003 if (cur->key.op != OP_NOMATCH)
2004 goto nomatch;
2005 break;
2006 }
2007 /* mark as ran */
2008 event->builtin_run |= (1 << cur->key.builtin_cmd);
2009 }
2010
2011 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), command, sizeof(command), false);
2012 log_debug("IMPORT builtin '%s' %s:%u",
2013 udev_builtin_name(cur->key.builtin_cmd),
2014 rules_str(rules, rule->rule.filename_off),
2015 rule->rule.filename_line);
2016
2017 if (udev_builtin_run(event->dev, cur->key.builtin_cmd, command, false) != 0) {
2018 /* remember failure */
2019 log_debug("IMPORT builtin '%s' returned non-zero",
2020 udev_builtin_name(cur->key.builtin_cmd));
2021 event->builtin_ret |= (1 << cur->key.builtin_cmd);
2022 if (cur->key.op != OP_NOMATCH)
2023 goto nomatch;
2024 }
2025 break;
2026 }
2027 case TK_M_IMPORT_DB: {
2028 const char *key = rules_str(rules, cur->key.value_off);
2029 const char *value;
2030
2031 value = udev_device_get_property_value(event->dev_db, key);
2032 if (value != NULL)
2033 udev_device_add_property(event->dev, key, value);
2034 else {
2035 if (cur->key.op != OP_NOMATCH)
2036 goto nomatch;
2037 }
2038 break;
2039 }
2040 case TK_M_IMPORT_CMDLINE: {
2041 _cleanup_fclose_ FILE *f = NULL;
2042 bool imported = false;
2043
2044 f = fopen("/proc/cmdline", "re");
2045 if (f != NULL) {
2046 char cmdline[4096];
2047
2048 if (fgets(cmdline, sizeof(cmdline), f) != NULL) {
2049 const char *key = rules_str(rules, cur->key.value_off);
2050 char *pos;
2051
2052 pos = strstr(cmdline, key);
2053 if (pos != NULL) {
2054 imported = true;
2055 pos += strlen(key);
2056 if (pos[0] == '\0' || isspace(pos[0]))
2057 /* we import simple flags as 'FLAG=1' */
2058 udev_device_add_property(event->dev, key, "1");
2059 else if (pos[0] == '=') {
2060 const char *value;
2061
2062 pos++;
2063 value = pos;
2064 while (pos[0] != '\0' && !isspace(pos[0]))
2065 pos++;
2066 pos[0] = '\0';
2067 udev_device_add_property(event->dev, key, value);
2068 }
2069 }
2070 }
2071 }
2072 if (!imported && cur->key.op != OP_NOMATCH)
2073 goto nomatch;
2074 break;
2075 }
2076 case TK_M_IMPORT_PARENT: {
2077 char import[UTIL_PATH_SIZE];
2078
2079 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import), false);
2080 if (import_parent_into_properties(event->dev, import) != 0)
2081 if (cur->key.op != OP_NOMATCH)
2082 goto nomatch;
2083 break;
2084 }
2085 case TK_M_RESULT:
2086 if (match_key(rules, cur, event->program_result) != 0)
2087 goto nomatch;
2088 break;
2089 case TK_A_STRING_ESCAPE_NONE:
2090 esc = ESCAPE_NONE;
2091 break;
2092 case TK_A_STRING_ESCAPE_REPLACE:
2093 esc = ESCAPE_REPLACE;
2094 break;
2095 case TK_A_DB_PERSIST:
2096 udev_device_set_db_persist(event->dev);
2097 break;
2098 case TK_A_INOTIFY_WATCH:
2099 if (event->inotify_watch_final)
2100 break;
2101 if (cur->key.op == OP_ASSIGN_FINAL)
2102 event->inotify_watch_final = true;
2103 event->inotify_watch = cur->key.watch;
2104 break;
2105 case TK_A_DEVLINK_PRIO:
2106 udev_device_set_devlink_priority(event->dev, cur->key.devlink_prio);
2107 break;
2108 case TK_A_OWNER: {
2109 char owner[UTIL_NAME_SIZE];
2110 const char *ow = owner;
2111 int r;
2112
2113 if (event->owner_final)
2114 break;
2115 if (cur->key.op == OP_ASSIGN_FINAL)
2116 event->owner_final = true;
2117 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), owner, sizeof(owner), false);
2118 event->owner_set = true;
2119 r = get_user_creds(&ow, &event->uid, NULL, NULL, NULL);
2120 if (r < 0) {
2121 log_unknown_owner(r, "user", owner);
2122 event->uid = 0;
2123 }
2124 log_debug("OWNER %u %s:%u",
2125 event->uid,
2126 rules_str(rules, rule->rule.filename_off),
2127 rule->rule.filename_line);
2128 break;
2129 }
2130 case TK_A_GROUP: {
2131 char group[UTIL_NAME_SIZE];
2132 const char *gr = group;
2133 int r;
2134
2135 if (event->group_final)
2136 break;
2137 if (cur->key.op == OP_ASSIGN_FINAL)
2138 event->group_final = true;
2139 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), group, sizeof(group), false);
2140 event->group_set = true;
2141 r = get_group_creds(&gr, &event->gid);
2142 if (r < 0) {
2143 log_unknown_owner(r, "group", group);
2144 event->gid = 0;
2145 }
2146 log_debug("GROUP %u %s:%u",
2147 event->gid,
2148 rules_str(rules, rule->rule.filename_off),
2149 rule->rule.filename_line);
2150 break;
2151 }
2152 case TK_A_MODE: {
2153 char mode_str[UTIL_NAME_SIZE];
2154 mode_t mode;
2155 char *endptr;
2156
2157 if (event->mode_final)
2158 break;
2159 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), mode_str, sizeof(mode_str), false);
2160 mode = strtol(mode_str, &endptr, 8);
2161 if (endptr[0] != '\0') {
2162 log_error("ignoring invalid mode '%s'", mode_str);
2163 break;
2164 }
2165 if (cur->key.op == OP_ASSIGN_FINAL)
2166 event->mode_final = true;
2167 event->mode_set = true;
2168 event->mode = mode;
2169 log_debug("MODE %#o %s:%u",
2170 event->mode,
2171 rules_str(rules, rule->rule.filename_off),
2172 rule->rule.filename_line);
2173 break;
2174 }
2175 case TK_A_OWNER_ID:
2176 if (event->owner_final)
2177 break;
2178 if (cur->key.op == OP_ASSIGN_FINAL)
2179 event->owner_final = true;
2180 event->owner_set = true;
2181 event->uid = cur->key.uid;
2182 log_debug("OWNER %u %s:%u",
2183 event->uid,
2184 rules_str(rules, rule->rule.filename_off),
2185 rule->rule.filename_line);
2186 break;
2187 case TK_A_GROUP_ID:
2188 if (event->group_final)
2189 break;
2190 if (cur->key.op == OP_ASSIGN_FINAL)
2191 event->group_final = true;
2192 event->group_set = true;
2193 event->gid = cur->key.gid;
2194 log_debug("GROUP %u %s:%u",
2195 event->gid,
2196 rules_str(rules, rule->rule.filename_off),
2197 rule->rule.filename_line);
2198 break;
2199 case TK_A_MODE_ID:
2200 if (event->mode_final)
2201 break;
2202 if (cur->key.op == OP_ASSIGN_FINAL)
2203 event->mode_final = true;
2204 event->mode_set = true;
2205 event->mode = cur->key.mode;
2206 log_debug("MODE %#o %s:%u",
2207 event->mode,
2208 rules_str(rules, rule->rule.filename_off),
2209 rule->rule.filename_line);
2210 break;
2211 case TK_A_SECLABEL: {
2212 char label_str[UTIL_LINE_SIZE] = {};
2213 const char *name, *label;
2214
2215 name = rules_str(rules, cur->key.attr_off);
2216 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), label_str, sizeof(label_str), false);
2217 if (label_str[0] != '\0')
2218 label = label_str;
2219 else
2220 label = rules_str(rules, cur->key.value_off);
2221
2222 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2223 udev_list_cleanup(&event->seclabel_list);
2224 udev_list_entry_add(&event->seclabel_list, name, label);
2225 log_debug("SECLABEL{%s}='%s' %s:%u",
2226 name, label,
2227 rules_str(rules, rule->rule.filename_off),
2228 rule->rule.filename_line);
2229 break;
2230 }
2231 case TK_A_ENV: {
2232 const char *name = rules_str(rules, cur->key.attr_off);
2233 char *value = rules_str(rules, cur->key.value_off);
2234 char value_new[UTIL_NAME_SIZE];
2235 const char *value_old = NULL;
2236
2237 if (value[0] == '\0') {
2238 if (cur->key.op == OP_ADD)
2239 break;
2240 udev_device_add_property(event->dev, name, NULL);
2241 break;
2242 }
2243
2244 if (cur->key.op == OP_ADD)
2245 value_old = udev_device_get_property_value(event->dev, name);
2246 if (value_old) {
2247 char temp[UTIL_NAME_SIZE];
2248
2249 /* append value separated by space */
2250 udev_event_apply_format(event, value, temp, sizeof(temp), false);
2251 strscpyl(value_new, sizeof(value_new), value_old, " ", temp, NULL);
2252 } else
2253 udev_event_apply_format(event, value, value_new, sizeof(value_new), false);
2254
2255 udev_device_add_property(event->dev, name, value_new);
2256 break;
2257 }
2258 case TK_A_TAG: {
2259 char tag[UTIL_PATH_SIZE];
2260 const char *p;
2261
2262 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), tag, sizeof(tag), false);
2263 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2264 udev_device_cleanup_tags_list(event->dev);
2265 for (p = tag; *p != '\0'; p++) {
2266 if ((*p >= 'a' && *p <= 'z') ||
2267 (*p >= 'A' && *p <= 'Z') ||
2268 (*p >= '0' && *p <= '9') ||
2269 *p == '-' || *p == '_')
2270 continue;
2271 log_error("ignoring invalid tag name '%s'", tag);
2272 break;
2273 }
2274 if (cur->key.op == OP_REMOVE)
2275 udev_device_remove_tag(event->dev, tag);
2276 else
2277 udev_device_add_tag(event->dev, tag);
2278 break;
2279 }
2280 case TK_A_NAME: {
2281 const char *name = rules_str(rules, cur->key.value_off);
2282
2283 char name_str[UTIL_PATH_SIZE];
2284 int count;
2285
2286 if (event->name_final)
2287 break;
2288 if (cur->key.op == OP_ASSIGN_FINAL)
2289 event->name_final = true;
2290 udev_event_apply_format(event, name, name_str, sizeof(name_str), false);
2291 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2292 count = util_replace_chars(name_str, "/");
2293 if (count > 0)
2294 log_debug("%i character(s) replaced", count);
2295 }
2296 if (major(udev_device_get_devnum(event->dev)) &&
2297 !streq(name_str, udev_device_get_devnode(event->dev) + strlen("/dev/"))) {
2298 log_error("NAME=\"%s\" ignored, kernel device nodes cannot be renamed; please fix it in %s:%u\n",
2299 name,
2300 rules_str(rules, rule->rule.filename_off),
2301 rule->rule.filename_line);
2302 break;
2303 }
2304 if (free_and_strdup(&event->name, name_str) < 0) {
2305 log_oom();
2306 return;
2307 }
2308 log_debug("NAME '%s' %s:%u",
2309 event->name,
2310 rules_str(rules, rule->rule.filename_off),
2311 rule->rule.filename_line);
2312 break;
2313 }
2314 case TK_A_DEVLINK: {
2315 char temp[UTIL_PATH_SIZE];
2316 char filename[UTIL_PATH_SIZE];
2317 char *pos, *next;
2318 int count = 0;
2319
2320 if (event->devlink_final)
2321 break;
2322 if (major(udev_device_get_devnum(event->dev)) == 0)
2323 break;
2324 if (cur->key.op == OP_ASSIGN_FINAL)
2325 event->devlink_final = true;
2326 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2327 udev_device_cleanup_devlinks_list(event->dev);
2328
2329 /* allow multiple symlinks separated by spaces */
2330 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), temp, sizeof(temp), esc != ESCAPE_NONE);
2331 if (esc == ESCAPE_UNSET)
2332 count = util_replace_chars(temp, "/ ");
2333 else if (esc == ESCAPE_REPLACE)
2334 count = util_replace_chars(temp, "/");
2335 if (count > 0)
2336 log_debug("%i character(s) replaced" , count);
2337 pos = temp;
2338 while (isspace(pos[0]))
2339 pos++;
2340 next = strchr(pos, ' ');
2341 while (next != NULL) {
2342 next[0] = '\0';
2343 log_debug("LINK '%s' %s:%u", pos,
2344 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2345 strscpyl(filename, sizeof(filename), "/dev/", pos, NULL);
2346 udev_device_add_devlink(event->dev, filename);
2347 while (isspace(next[1]))
2348 next++;
2349 pos = &next[1];
2350 next = strchr(pos, ' ');
2351 }
2352 if (pos[0] != '\0') {
2353 log_debug("LINK '%s' %s:%u", pos,
2354 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2355 strscpyl(filename, sizeof(filename), "/dev/", pos, NULL);
2356 udev_device_add_devlink(event->dev, filename);
2357 }
2358 break;
2359 }
2360 case TK_A_ATTR: {
2361 const char *key_name = rules_str(rules, cur->key.attr_off);
2362 char attr[UTIL_PATH_SIZE];
2363 char value[UTIL_NAME_SIZE];
2364 _cleanup_fclose_ FILE *f = NULL;
2365
2366 if (util_resolve_subsys_kernel(event->udev, key_name, attr, sizeof(attr), 0) != 0)
2367 strscpyl(attr, sizeof(attr), udev_device_get_syspath(event->dev), "/", key_name, NULL);
2368 attr_subst_subdir(attr, sizeof(attr));
2369
2370 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), value, sizeof(value), false);
2371 log_debug("ATTR '%s' writing '%s' %s:%u", attr, value,
2372 rules_str(rules, rule->rule.filename_off),
2373 rule->rule.filename_line);
2374 f = fopen(attr, "we");
2375 if (f == NULL)
2376 log_error_errno(errno, "error opening ATTR{%s} for writing: %m", attr);
2377 else if (fprintf(f, "%s", value) <= 0)
2378 log_error_errno(errno, "error writing ATTR{%s}: %m", attr);
2379 break;
2380 }
2381 case TK_A_SYSCTL: {
2382 char filename[UTIL_PATH_SIZE];
2383 char value[UTIL_NAME_SIZE];
2384 int r;
2385
2386 udev_event_apply_format(event, rules_str(rules, cur->key.attr_off), filename, sizeof(filename), false);
2387 sysctl_normalize(filename);
2388 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), value, sizeof(value), false);
2389 log_debug("SYSCTL '%s' writing '%s' %s:%u", filename, value,
2390 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2391 r = sysctl_write(filename, value);
2392 if (r < 0)
2393 log_error_errno(r, "error writing SYSCTL{%s}='%s': %m", filename, value);
2394 break;
2395 }
2396 case TK_A_RUN_BUILTIN:
2397 case TK_A_RUN_PROGRAM: {
2398 struct udev_list_entry *entry;
2399
2400 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2401 udev_list_cleanup(&event->run_list);
2402 log_debug("RUN '%s' %s:%u",
2403 rules_str(rules, cur->key.value_off),
2404 rules_str(rules, rule->rule.filename_off),
2405 rule->rule.filename_line);
2406 entry = udev_list_entry_add(&event->run_list, rules_str(rules, cur->key.value_off), NULL);
2407 udev_list_entry_set_num(entry, cur->key.builtin_cmd);
2408 break;
2409 }
2410 case TK_A_GOTO:
2411 if (cur->key.rule_goto == 0)
2412 break;
2413 cur = &rules->tokens[cur->key.rule_goto];
2414 continue;
2415 case TK_END:
2416 return;
2417
2418 case TK_M_PARENTS_MIN:
2419 case TK_M_PARENTS_MAX:
2420 case TK_M_MAX:
2421 case TK_UNSET:
2422 log_error("wrong type %u", cur->type);
2423 goto nomatch;
2424 }
2425
2426 cur++;
2427 continue;
2428 nomatch:
2429 /* fast-forward to next rule */
2430 cur = rule + rule->rule.token_count;
2431 }
2432 }
2433
2434 int udev_rules_apply_static_dev_perms(struct udev_rules *rules) {
2435 struct token *cur;
2436 struct token *rule;
2437 uid_t uid = 0;
2438 gid_t gid = 0;
2439 mode_t mode = 0;
2440 _cleanup_strv_free_ char **tags = NULL;
2441 char **t;
2442 FILE *f = NULL;
2443 _cleanup_free_ char *path = NULL;
2444 int r;
2445
2446 if (rules->tokens == NULL)
2447 return 0;
2448
2449 cur = &rules->tokens[0];
2450 rule = cur;
2451 for (;;) {
2452 switch (cur->type) {
2453 case TK_RULE:
2454 /* current rule */
2455 rule = cur;
2456
2457 /* skip rules without a static_node tag */
2458 if (!rule->rule.has_static_node)
2459 goto next;
2460
2461 uid = 0;
2462 gid = 0;
2463 mode = 0;
2464 tags = strv_free(tags);
2465 break;
2466 case TK_A_OWNER_ID:
2467 uid = cur->key.uid;
2468 break;
2469 case TK_A_GROUP_ID:
2470 gid = cur->key.gid;
2471 break;
2472 case TK_A_MODE_ID:
2473 mode = cur->key.mode;
2474 break;
2475 case TK_A_TAG:
2476 r = strv_extend(&tags, rules_str(rules, cur->key.value_off));
2477 if (r < 0)
2478 goto finish;
2479
2480 break;
2481 case TK_A_STATIC_NODE: {
2482 char device_node[UTIL_PATH_SIZE];
2483 char tags_dir[UTIL_PATH_SIZE];
2484 char tag_symlink[UTIL_PATH_SIZE];
2485 struct stat stats;
2486
2487 /* we assure, that the permissions tokens are sorted before the static token */
2488
2489 if (mode == 0 && uid == 0 && gid == 0 && tags == NULL)
2490 goto next;
2491
2492 strscpyl(device_node, sizeof(device_node), "/dev/", rules_str(rules, cur->key.value_off), NULL);
2493 if (stat(device_node, &stats) != 0)
2494 break;
2495 if (!S_ISBLK(stats.st_mode) && !S_ISCHR(stats.st_mode))
2496 break;
2497
2498 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
2499 if (tags) {
2500 STRV_FOREACH(t, tags) {
2501 _cleanup_free_ char *unescaped_filename = NULL;
2502
2503 strscpyl(tags_dir, sizeof(tags_dir), "/run/udev/static_node-tags/", *t, "/", NULL);
2504 r = mkdir_p(tags_dir, 0755);
2505 if (r < 0)
2506 return log_error_errno(r, "failed to create %s: %m", tags_dir);
2507
2508 unescaped_filename = xescape(rules_str(rules, cur->key.value_off), "/.");
2509
2510 strscpyl(tag_symlink, sizeof(tag_symlink), tags_dir, unescaped_filename, NULL);
2511 r = symlink(device_node, tag_symlink);
2512 if (r < 0 && errno != EEXIST)
2513 return log_error_errno(errno, "failed to create symlink %s -> %s: %m",
2514 tag_symlink, device_node);
2515 }
2516 }
2517
2518 /* don't touch the permissions if only the tags were set */
2519 if (mode == 0 && uid == 0 && gid == 0)
2520 break;
2521
2522 if (mode == 0) {
2523 if (gid > 0)
2524 mode = 0660;
2525 else
2526 mode = 0600;
2527 }
2528 if (mode != (stats.st_mode & 01777)) {
2529 r = chmod(device_node, mode);
2530 if (r < 0)
2531 return log_error_errno(errno, "Failed to chmod '%s' %#o: %m",
2532 device_node, mode);
2533 else
2534 log_debug("chmod '%s' %#o", device_node, mode);
2535 }
2536
2537 if ((uid != 0 && uid != stats.st_uid) || (gid != 0 && gid != stats.st_gid)) {
2538 r = chown(device_node, uid, gid);
2539 if (r < 0)
2540 return log_error_errno(errno, "Failed to chown '%s' %u %u: %m",
2541 device_node, uid, gid);
2542 else
2543 log_debug("chown '%s' %u %u", device_node, uid, gid);
2544 }
2545
2546 utimensat(AT_FDCWD, device_node, NULL, 0);
2547 break;
2548 }
2549 case TK_END:
2550 goto finish;
2551 }
2552
2553 cur++;
2554 continue;
2555 next:
2556 /* fast-forward to next rule */
2557 cur = rule + rule->rule.token_count;
2558 continue;
2559 }
2560
2561 finish:
2562 if (f) {
2563 fflush(f);
2564 fchmod(fileno(f), 0644);
2565 if (ferror(f) || rename(path, "/run/udev/static_node-tags") < 0) {
2566 unlink_noerrno("/run/udev/static_node-tags");
2567 unlink_noerrno(path);
2568 return -errno;
2569 }
2570 }
2571
2572 return 0;
2573 }