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