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