]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udev-rules.c
util-lib: split our string related calls from util.[ch] into its own file string...
[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
2232cac8 18#include <ctype.h>
5ee7ecfb 19#include <dirent.h>
4f5dd394
LP
20#include <errno.h>
21#include <fcntl.h>
cea61f5c 22#include <fnmatch.h>
4f5dd394
LP
23#include <limits.h>
24#include <stdbool.h>
25#include <stddef.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
959e8b5d 29#include <time.h>
4f5dd394 30#include <unistd.h>
2232cac8 31
2c21044f 32#include "conf-files.h"
4f5dd394
LP
33#include "escape.h"
34#include "path-util.h"
915bf0f6 35#include "strbuf.h"
07630cea 36#include "string-util.h"
84b6ad70 37#include "strv.h"
f4cf2e5b 38#include "sysctl-util.h"
4f5dd394 39#include "udev.h"
07630cea 40#include "util.h"
2232cac8 41
a1525d17 42#define PREALLOC_TOKEN 2048
c7521974 43
4052400f 44struct uid_gid {
912541b0
KS
45 unsigned int name_off;
46 union {
a1525d17
KS
47 uid_t uid;
48 gid_t gid;
912541b0 49 };
4052400f
KS
50};
51
2ad8416d
ZJS
52static const char* const rules_dirs[] = {
53 "/etc/udev/rules.d",
54 "/run/udev/rules.d",
55 UDEVLIBEXECDIR "/rules.d",
4f5dd394
LP
56 NULL
57};
2ad8416d 58
4052400f 59struct udev_rules {
912541b0 60 struct udev *udev;
97f2d76d 61 usec_t dirs_ts_usec;
912541b0
KS
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
ab06eef8 69 /* all key strings are copied and de-duplicated in a single continuous string buffer */
915bf0f6 70 struct strbuf *strbuf;
912541b0
KS
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;
4052400f
KS
79};
80
915bf0f6
KS
81static char *rules_str(struct udev_rules *rules, unsigned int off) {
82 return rules->strbuf->buf + off;
83}
84
85static unsigned int rules_add_string(struct udev_rules *rules, const char *s) {
86 return strbuf_add_string(rules->strbuf, s, strlen(s));
87}
88
8e3ba377 89/* KEY=="", KEY!="", KEY+="", KEY-="", KEY="", KEY:="" */
b0f7409f 90enum operation_type {
912541b0 91 OP_UNSET,
b0f7409f 92
912541b0
KS
93 OP_MATCH,
94 OP_NOMATCH,
95 OP_MATCH_MAX,
b0f7409f 96
912541b0 97 OP_ADD,
8e3ba377 98 OP_REMOVE,
912541b0
KS
99 OP_ASSIGN,
100 OP_ASSIGN_FINAL,
c7521974
KS
101};
102
ac218d9c 103enum string_glob_type {
912541b0 104 GL_UNSET,
a1525d17 105 GL_PLAIN, /* no special chars */
912541b0 106 GL_GLOB, /* shell globs ?,*,[] */
a1525d17
KS
107 GL_SPLIT, /* multi-value A|B */
108 GL_SPLIT_GLOB, /* multi-value with glob A*|B* */
109 GL_SOMETHING, /* commonly used "?*" */
ac218d9c
KS
110};
111
32028733 112enum string_subst_type {
912541b0
KS
113 SB_UNSET,
114 SB_NONE,
115 SB_FORMAT,
116 SB_SUBSYS,
32028733
KS
117};
118
154a7b84 119/* tokens of a rule are sorted/handled in this order */
6880b25d 120enum token_type {
912541b0
KS
121 TK_UNSET,
122 TK_RULE,
123
a1525d17
KS
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 */
f4cf2e5b 135 TK_M_SYSCTL, /* val, attr */
912541b0
KS
136
137 TK_M_PARENTS_MIN,
a1525d17 138 TK_M_KERNELS, /* val */
912541b0 139 TK_M_SUBSYSTEMS, /* val */
a1525d17
KS
140 TK_M_DRIVERS, /* val */
141 TK_M_ATTRS, /* val, attr */
142 TK_M_TAGS, /* val */
912541b0
KS
143 TK_M_PARENTS_MAX,
144
a1525d17 145 TK_M_TEST, /* val, mode_t */
a1525d17
KS
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 */
912541b0
KS
154 TK_M_MAX,
155
156 TK_A_STRING_ESCAPE_NONE,
157 TK_A_STRING_ESCAPE_REPLACE,
158 TK_A_DB_PERSIST,
a1525d17
KS
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 */
84b6ad70 167 TK_A_TAG, /* val */
a1525d17 168 TK_A_STATIC_NODE, /* val */
c26547d6 169 TK_A_SECLABEL, /* val, attr */
a1525d17 170 TK_A_ENV, /* val, attr */
a1525d17
KS
171 TK_A_NAME, /* val */
172 TK_A_DEVLINK, /* val */
173 TK_A_ATTR, /* val, attr */
f4cf2e5b 174 TK_A_SYSCTL, /* val, attr */
83cd6b75
KS
175 TK_A_RUN_BUILTIN, /* val, bool */
176 TK_A_RUN_PROGRAM, /* val, bool */
a1525d17 177 TK_A_GOTO, /* size_t */
912541b0
KS
178
179 TK_END,
c7521974
KS
180};
181
3f3aa9f5 182/* we try to pack stuff in a way that we take only 12 bytes per token */
4052400f 183struct token {
912541b0
KS
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;
912541b0
KS
205 unsigned int rule_goto;
206 mode_t mode;
207 uid_t uid;
208 gid_t gid;
209 int devlink_prio;
912541b0
KS
210 int watch;
211 enum udev_builtin_cmd builtin_cmd;
212 };
213 } key;
214 };
4052400f
KS
215};
216
912541b0 217#define MAX_TK 64
4052400f 218struct rule_tmp {
912541b0
KS
219 struct udev_rules *rules;
220 struct token rule;
221 struct token token[MAX_TK];
222 unsigned int token_cur;
4052400f
KS
223};
224
baa30fbc 225#ifdef DEBUG
9ec6e95b 226static const char *operation_str(enum operation_type type) {
912541b0 227 static const char *operation_strs[] = {
a1525d17
KS
228 [OP_UNSET] = "UNSET",
229 [OP_MATCH] = "match",
230 [OP_NOMATCH] = "nomatch",
912541b0
KS
231 [OP_MATCH_MAX] = "MATCH_MAX",
232
a1525d17 233 [OP_ADD] = "add",
8e3ba377 234 [OP_REMOVE] = "remove",
a1525d17
KS
235 [OP_ASSIGN] = "assign",
236 [OP_ASSIGN_FINAL] = "assign-final",
912541b0
KS
237} ;
238
239 return operation_strs[type];
5d6a1fa6 240}
4052400f 241
9ec6e95b 242static const char *string_glob_str(enum string_glob_type type) {
912541b0 243 static const char *string_glob_strs[] = {
a1525d17
KS
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",
912541b0
KS
250 };
251
252 return string_glob_strs[type];
5d6a1fa6
KS
253}
254
9ec6e95b 255static const char *token_str(enum token_type type) {
912541b0 256 static const char *token_strs[] = {
a1525d17
KS
257 [TK_UNSET] = "UNSET",
258 [TK_RULE] = "RULE",
912541b0 259
a1525d17 260 [TK_M_ACTION] = "M ACTION",
912541b0 261 [TK_M_DEVPATH] = "M DEVPATH",
a1525d17 262 [TK_M_KERNEL] = "M KERNEL",
912541b0 263 [TK_M_DEVLINK] = "M DEVLINK",
a1525d17
KS
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",
912541b0 269 [TK_M_WAITFOR] = "M WAITFOR",
a1525d17 270 [TK_M_ATTR] = "M ATTR",
f4cf2e5b 271 [TK_M_SYSCTL] = "M SYSCTL",
912541b0 272
a1525d17 273 [TK_M_PARENTS_MIN] = "M PARENTS_MIN",
912541b0 274 [TK_M_KERNELS] = "M KERNELS",
a1525d17 275 [TK_M_SUBSYSTEMS] = "M SUBSYSTEMS",
912541b0 276 [TK_M_DRIVERS] = "M DRIVERS",
a1525d17
KS
277 [TK_M_ATTRS] = "M ATTRS",
278 [TK_M_TAGS] = "M TAGS",
279 [TK_M_PARENTS_MAX] = "M PARENTS_MAX",
912541b0 280
a1525d17 281 [TK_M_TEST] = "M TEST",
912541b0 282 [TK_M_PROGRAM] = "M PROGRAM",
a1525d17
KS
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",
c26547d6 303 [TK_A_SECLABEL] = "A SECLABEL",
912541b0 304 [TK_A_MODE_ID] = "A MODE_ID",
a1525d17
KS
305 [TK_A_ENV] = "A ENV",
306 [TK_A_TAG] = "A ENV",
307 [TK_A_NAME] = "A NAME",
912541b0 308 [TK_A_DEVLINK] = "A DEVLINK",
a1525d17 309 [TK_A_ATTR] = "A ATTR",
f4cf2e5b 310 [TK_A_SYSCTL] = "A SYSCTL",
83cd6b75
KS
311 [TK_A_RUN_BUILTIN] = "A RUN_BUILTIN",
312 [TK_A_RUN_PROGRAM] = "A RUN_PROGRAM",
a1525d17 313 [TK_A_GOTO] = "A GOTO",
912541b0 314
a1525d17 315 [TK_END] = "END",
912541b0
KS
316 };
317
318 return token_strs[type];
5d6a1fa6 319}
c7521974 320
9ec6e95b 321static void dump_token(struct udev_rules *rules, struct token *token) {
912541b0
KS
322 enum token_type type = token->type;
323 enum operation_type op = token->key.op;
324 enum string_glob_type glob = token->key.glob;
915bf0f6 325 const char *value = str(rules, token->key.value_off);
912541b0
KS
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
9f6445e3 335 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'",
baa30fbc
KS
336 &rules->buf[token->rule.filename_off], token->rule.filename_line,
337 idx, token->rule.token_count,
338 &rules->buf[token->rule.label_off]);
912541b0
KS
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:
83cd6b75
KS
365 case TK_A_RUN_BUILTIN:
366 case TK_A_RUN_PROGRAM:
9f6445e3 367 log_debug("%s %s '%s'(%s)",
baa30fbc 368 token_str(type), operation_str(op), value, string_glob_str(glob));
912541b0
KS
369 break;
370 case TK_M_IMPORT_BUILTIN:
9f6445e3 371 log_debug("%s %i '%s'", token_str(type), token->key.builtin_cmd, value);
912541b0
KS
372 break;
373 case TK_M_ATTR:
f4cf2e5b 374 case TK_M_SYSCTL:
912541b0
KS
375 case TK_M_ATTRS:
376 case TK_M_ENV:
377 case TK_A_ATTR:
f4cf2e5b 378 case TK_A_SYSCTL:
912541b0 379 case TK_A_ENV:
9f6445e3 380 log_debug("%s %s '%s' '%s'(%s)",
baa30fbc 381 token_str(type), operation_str(op), attr, value, string_glob_str(glob));
912541b0
KS
382 break;
383 case TK_M_TAG:
384 case TK_A_TAG:
9f6445e3 385 log_debug("%s %s '%s'", token_str(type), operation_str(op), value);
912541b0
KS
386 break;
387 case TK_A_STRING_ESCAPE_NONE:
388 case TK_A_STRING_ESCAPE_REPLACE:
389 case TK_A_DB_PERSIST:
9f6445e3 390 log_debug("%s", token_str(type));
912541b0
KS
391 break;
392 case TK_M_TEST:
9f6445e3 393 log_debug("%s %s '%s'(%s) %#o",
baa30fbc 394 token_str(type), operation_str(op), value, string_glob_str(glob), token->key.mode);
912541b0
KS
395 break;
396 case TK_A_INOTIFY_WATCH:
9f6445e3 397 log_debug("%s %u", token_str(type), token->key.watch);
912541b0
KS
398 break;
399 case TK_A_DEVLINK_PRIO:
9f6445e3 400 log_debug("%s %u", token_str(type), token->key.devlink_prio);
912541b0
KS
401 break;
402 case TK_A_OWNER_ID:
9f6445e3 403 log_debug("%s %s %u", token_str(type), operation_str(op), token->key.uid);
912541b0
KS
404 break;
405 case TK_A_GROUP_ID:
9f6445e3 406 log_debug("%s %s %u", token_str(type), operation_str(op), token->key.gid);
912541b0
KS
407 break;
408 case TK_A_MODE_ID:
9f6445e3 409 log_debug("%s %s %#o", token_str(type), operation_str(op), token->key.mode);
912541b0
KS
410 break;
411 case TK_A_STATIC_NODE:
9f6445e3 412 log_debug("%s '%s'", token_str(type), value);
912541b0 413 break;
c26547d6 414 case TK_A_SECLABEL:
9f6445e3 415 log_debug("%s %s '%s' '%s'", token_str(type), operation_str(op), attr, value);
c26547d6 416 break;
912541b0 417 case TK_A_GOTO:
9f6445e3 418 log_debug("%s '%s' %u", token_str(type), value, token->key.rule_goto);
912541b0
KS
419 break;
420 case TK_END:
9f6445e3 421 log_debug("* %s", token_str(type));
912541b0
KS
422 break;
423 case TK_M_PARENTS_MIN:
424 case TK_M_PARENTS_MAX:
425 case TK_M_MAX:
426 case TK_UNSET:
9f6445e3 427 log_debug("unknown type %u", type);
912541b0
KS
428 break;
429 }
4052400f 430}
154a7b84 431
9ec6e95b 432static void dump_rules(struct udev_rules *rules) {
912541b0
KS
433 unsigned int i;
434
9f6445e3 435 log_debug("dumping %u (%zu bytes) tokens, %u (%zu bytes) strings",
baa30fbc
KS
436 rules->token_cur,
437 rules->token_cur * sizeof(struct token),
438 rules->buf_count,
439 rules->buf_cur);
f168c273 440 for (i = 0; i < rules->token_cur; i++)
912541b0 441 dump_token(rules, &rules->tokens[i]);
4052400f
KS
442}
443#else
4052400f
KS
444static inline void dump_token(struct udev_rules *rules, struct token *token) {}
445static inline void dump_rules(struct udev_rules *rules) {}
baa30fbc 446#endif /* DEBUG */
c7521974 447
9ec6e95b 448static int add_token(struct udev_rules *rules, struct token *token) {
912541b0
KS
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;
912541b0
KS
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;
c7521974 468}
a27cd06c 469
9ec6e95b 470static uid_t add_uid(struct udev_rules *rules, const char *owner) {
912541b0 471 unsigned int i;
23bf8dd7 472 uid_t uid = 0;
912541b0 473 unsigned int off;
23bf8dd7 474 int r;
912541b0
KS
475
476 /* lookup, if we know it already */
477 for (i = 0; i < rules->uids_cur; i++) {
478 off = rules->uids[i].name_off;
915bf0f6 479 if (streq(rules_str(rules, off), owner)) {
912541b0 480 uid = rules->uids[i].uid;
912541b0
KS
481 return uid;
482 }
483 }
23bf8dd7
TG
484 r = get_user_creds(&owner, &uid, NULL, NULL, NULL);
485 if (r < 0) {
486 if (r == -ENOENT || r == -ESRCH)
ff49bc32 487 log_error("specified user '%s' unknown", owner);
23bf8dd7 488 else
279d3c9c 489 log_error_errno(r, "error resolving user '%s': %m", owner);
23bf8dd7 490 }
912541b0
KS
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;
912541b0
KS
505 rules->uids = uids;
506 rules->uids_max += add;
507 }
508 rules->uids[rules->uids_cur].uid = uid;
915bf0f6 509 off = rules_add_string(rules, owner);
912541b0
KS
510 if (off <= 0)
511 return uid;
512 rules->uids[rules->uids_cur].name_off = off;
513 rules->uids_cur++;
514 return uid;
154a7b84
KS
515}
516
9ec6e95b 517static gid_t add_gid(struct udev_rules *rules, const char *group) {
912541b0 518 unsigned int i;
23bf8dd7 519 gid_t gid = 0;
912541b0 520 unsigned int off;
23bf8dd7 521 int r;
912541b0
KS
522
523 /* lookup, if we know it already */
524 for (i = 0; i < rules->gids_cur; i++) {
525 off = rules->gids[i].name_off;
915bf0f6 526 if (streq(rules_str(rules, off), group)) {
912541b0 527 gid = rules->gids[i].gid;
912541b0
KS
528 return gid;
529 }
530 }
23bf8dd7
TG
531 r = get_group_creds(&group, &gid);
532 if (r < 0) {
533 if (r == -ENOENT || r == -ESRCH)
ff49bc32 534 log_error("specified group '%s' unknown", group);
23bf8dd7 535 else
279d3c9c 536 log_error_errno(r, "error resolving group '%s': %m", group);
23bf8dd7 537 }
912541b0
KS
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;
912541b0
KS
552 rules->gids = gids;
553 rules->gids_max += add;
554 }
555 rules->gids[rules->gids_cur].gid = gid;
915bf0f6 556 off = rules_add_string(rules, group);
912541b0
KS
557 if (off <= 0)
558 return gid;
559 rules->gids[rules->gids_cur].name_off = off;
560 rules->gids_cur++;
561 return gid;
154a7b84
KS
562}
563
9ec6e95b 564static int import_property_from_string(struct udev_device *dev, char *line) {
912541b0
KS
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]) {
9f6445e3 611 log_debug("inconsistent quoting: '%s', skip", line);
912541b0
KS
612 return -1;
613 }
614 val[len-1] = '\0';
615 val++;
616 }
617
df546eb5 618 udev_device_add_property(dev, key, val);
0aa5b4d1 619
912541b0 620 return 0;
be4bedd1
KS
621}
622
9ec6e95b 623static int import_file_into_properties(struct udev_device *dev, const char *filename) {
912541b0
KS
624 FILE *f;
625 char line[UTIL_LINE_SIZE];
626
47ef94ac 627 f = fopen(filename, "re");
912541b0
KS
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;
bd0ed2ff
KS
634}
635
dd5eddd2
KS
636static int import_program_into_properties(struct udev_event *event,
637 usec_t timeout_usec,
67117413 638 usec_t timeout_warn_usec,
8314de1d 639 const char *program) {
912541b0
KS
640 char result[UTIL_LINE_SIZE];
641 char *line;
642 int err;
643
bbf35206 644 err = udev_event_spawn(event, timeout_usec, timeout_warn_usec, true, program, result, sizeof(result));
912541b0
KS
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 }
bbf35206 657 import_property_from_string(event->dev, line);
912541b0
KS
658 line = pos;
659 }
660 return 0;
319c6700
KS
661}
662
9ec6e95b 663static int import_parent_into_properties(struct udev_device *dev, const char *filter) {
912541b0
KS
664 struct udev_device *dev_parent;
665 struct udev_list_entry *list_entry;
666
3b64e4d4
TG
667 assert(dev);
668 assert(filter);
669
912541b0
KS
670 dev_parent = udev_device_get_parent(dev);
671 if (dev_parent == NULL)
672 return -1;
673
912541b0
KS
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
ece174c5 678 if (fnmatch(filter, key, 0) == 0)
df546eb5 679 udev_device_add_property(dev, key, val);
912541b0
KS
680 }
681 return 0;
e2ecb34f
KS
682}
683
9ec6e95b 684static int attr_subst_subdir(char *attr, size_t len) {
912541b0
KS
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
d5a89d7d 693 strscpy(dirname, sizeof(dirname), attr);
912541b0
KS
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;
d5a89d7d 708 strscpyl(attr, len, dirname, "/", dent->d_name, tail, NULL);
912541b0
KS
709 if (stat(attr, &stats) == 0) {
710 found = true;
711 break;
712 }
713 }
714 closedir(dir);
715 }
716 }
717
718 return found;
0ea5e96e
KS
719}
720
9ec6e95b 721static int get_key(struct udev *udev, char **line, char **key, enum operation_type *op, char **value) {
912541b0
KS
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;
8e3ba377 746 if ((linepos[0] == '+') || (linepos[0] == '-') || (linepos[0] == '!') || (linepos[0] == ':'))
912541b0
KS
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;
8e3ba377
DH
770 } else if (linepos[0] == '-' && linepos[1] == '=') {
771 *op = OP_REMOVE;
772 linepos += 2;
912541b0
KS
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++;
912541b0
KS
804
805 /* move line to next key */
806 *line = temp;
807 return 0;
6880b25d 808}
95776dc6 809
6880b25d 810/* extract possible KEY{attr} */
9ec6e95b 811static const char *get_key_attribute(struct udev *udev, char *str) {
912541b0
KS
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) {
9f6445e3 820 log_error("missing closing brace for format");
912541b0
KS
821 return NULL;
822 }
823 pos[0] = '\0';
912541b0
KS
824 return attr;
825 }
826 return NULL;
6880b25d 827}
95776dc6 828
0ef254d5 829static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type,
912541b0 830 enum operation_type op,
9ec6e95b 831 const char *value, const void *data) {
912541b0
KS
832 struct token *token = &rule_tmp->token[rule_tmp->token_cur];
833 const char *attr = NULL;
834
29804cc1 835 memzero(token, sizeof(struct token));
912541b0
KS
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:
8a173387 860 case TK_A_DEVLINK:
912541b0
KS
861 case TK_A_NAME:
862 case TK_A_GOTO:
863 case TK_M_TAG:
864 case TK_A_TAG:
d6f116a7 865 case TK_A_STATIC_NODE:
915bf0f6 866 token->key.value_off = rules_add_string(rule_tmp->rules, value);
912541b0
KS
867 break;
868 case TK_M_IMPORT_BUILTIN:
915bf0f6 869 token->key.value_off = rules_add_string(rule_tmp->rules, value);
912541b0
KS
870 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
871 break;
872 case TK_M_ENV:
873 case TK_M_ATTR:
f4cf2e5b 874 case TK_M_SYSCTL:
912541b0
KS
875 case TK_M_ATTRS:
876 case TK_A_ATTR:
f4cf2e5b 877 case TK_A_SYSCTL:
912541b0 878 case TK_A_ENV:
c26547d6 879 case TK_A_SECLABEL:
912541b0 880 attr = data;
915bf0f6
KS
881 token->key.value_off = rules_add_string(rule_tmp->rules, value);
882 token->key.attr_off = rules_add_string(rule_tmp->rules, attr);
912541b0 883 break;
912541b0 884 case TK_M_TEST:
915bf0f6 885 token->key.value_off = rules_add_string(rule_tmp->rules, value);
912541b0
KS
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;
83cd6b75 893 case TK_A_RUN_BUILTIN:
83cd6b75 894 case TK_A_RUN_PROGRAM:
4590cfe4 895 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
915bf0f6 896 token->key.value_off = rules_add_string(rule_tmp->rules, value);
912541b0
KS
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;
912541b0
KS
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:
9f6445e3 917 log_error("wrong type %u", type);
912541b0
KS
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);
e3e0314b 928 has_glob = string_is_glob(value);
912541b0
KS
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) {
33502ffe 934 if (streq(value, "?*"))
912541b0
KS
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) {
d6f116a7 955 /* check if property/attribute name has substitution chars */
912541b0
KS
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++;
8fef0ff2 967 if (rule_tmp->token_cur >= ELEMENTSOF(rule_tmp->token)) {
9f6445e3 968 log_error("temporary rule array too small");
912541b0
KS
969 return -1;
970 }
971 return 0;
6880b25d 972}
e57e7bc1 973
9ec6e95b 974static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp) {
912541b0
KS
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;
724257d9
GKH
1006}
1007
6880b25d 1008static int add_rule(struct udev_rules *rules, char *line,
9ec6e95b 1009 const char *filename, unsigned int filename_off, unsigned int lineno) {
912541b0 1010 char *linepos;
04a9d3a0 1011 const char *attr;
84198c18
TG
1012 struct rule_tmp rule_tmp = {
1013 .rules = rules,
1014 .rule.type = TK_RULE,
1015 };
912541b0 1016
775f8b3c
KS
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;
912541b0
KS
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
e736cf35 1028 if (get_key(rules->udev, &linepos, &key, &op, &value) != 0) {
3cf0f8f7
DR
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
e736cf35
DR
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. */
2783fe06 1037 if (!strchr(NEWLINE, *linepos)) {
6501b52d 1038 char buf[2] = {*linepos};
1291bc89
ZJS
1039 _cleanup_free_ char *tmp;
1040
1041 tmp = cescape(buf);
ff49bc32 1042 log_error("invalid key/value pair in file %s on line %u, starting at character %tu ('%s')",
1291bc89 1043 filename, lineno, linepos - line + 1, tmp);
6501b52d 1044 if (*linepos == '#')
75716dd8 1045 log_error("hint: comments can only start at beginning of line");
1291bc89 1046 }
912541b0 1047 break;
e736cf35 1048 }
912541b0 1049
33502ffe 1050 if (streq(key, "ACTION")) {
912541b0 1051 if (op > OP_MATCH_MAX) {
9f6445e3 1052 log_error("invalid ACTION operation");
912541b0
KS
1053 goto invalid;
1054 }
1055 rule_add_key(&rule_tmp, TK_M_ACTION, op, value, NULL);
1056 continue;
1057 }
1058
33502ffe 1059 if (streq(key, "DEVPATH")) {
912541b0 1060 if (op > OP_MATCH_MAX) {
9f6445e3 1061 log_error("invalid DEVPATH operation");
912541b0
KS
1062 goto invalid;
1063 }
1064 rule_add_key(&rule_tmp, TK_M_DEVPATH, op, value, NULL);
1065 continue;
1066 }
1067
33502ffe 1068 if (streq(key, "KERNEL")) {
912541b0 1069 if (op > OP_MATCH_MAX) {
9f6445e3 1070 log_error("invalid KERNEL operation");
912541b0
KS
1071 goto invalid;
1072 }
1073 rule_add_key(&rule_tmp, TK_M_KERNEL, op, value, NULL);
1074 continue;
1075 }
1076
33502ffe 1077 if (streq(key, "SUBSYSTEM")) {
912541b0 1078 if (op > OP_MATCH_MAX) {
9f6445e3 1079 log_error("invalid SUBSYSTEM operation");
912541b0
KS
1080 goto invalid;
1081 }
1082 /* bus, class, subsystem events should all be the same */
33502ffe
KS
1083 if (streq(value, "subsystem") ||
1084 streq(value, "bus") ||
1085 streq(value, "class")) {
1086 if (streq(value, "bus") || streq(value, "class"))
9f6445e3 1087 log_error("'%s' must be specified as 'subsystem' "
912541b0
KS
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
33502ffe 1095 if (streq(key, "DRIVER")) {
912541b0 1096 if (op > OP_MATCH_MAX) {
9f6445e3 1097 log_error("invalid DRIVER operation");
912541b0
KS
1098 goto invalid;
1099 }
1100 rule_add_key(&rule_tmp, TK_M_DRIVER, op, value, NULL);
1101 continue;
1102 }
1103
33502ffe 1104 if (startswith(key, "ATTR{")) {
f8294e41 1105 attr = get_key_attribute(rules->udev, key + strlen("ATTR"));
912541b0 1106 if (attr == NULL) {
9f6445e3 1107 log_error("error parsing ATTR attribute");
912541b0
KS
1108 goto invalid;
1109 }
8e3ba377
DH
1110 if (op == OP_REMOVE) {
1111 log_error("invalid ATTR operation");
1112 goto invalid;
1113 }
f4cf2e5b 1114 if (op < OP_MATCH_MAX)
912541b0 1115 rule_add_key(&rule_tmp, TK_M_ATTR, op, value, attr);
f4cf2e5b 1116 else
912541b0 1117 rule_add_key(&rule_tmp, TK_A_ATTR, op, value, attr);
f4cf2e5b
KS
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;
912541b0 1130 }
f4cf2e5b
KS
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);
912541b0
KS
1135 continue;
1136 }
1137
c26547d6 1138 if (startswith(key, "SECLABEL{")) {
f8294e41 1139 attr = get_key_attribute(rules->udev, key + strlen("SECLABEL"));
c26547d6 1140 if (!attr) {
9f6445e3 1141 log_error("error parsing SECLABEL attribute");
c26547d6
KS
1142 goto invalid;
1143 }
8e3ba377
DH
1144 if (op == OP_REMOVE) {
1145 log_error("invalid SECLABEL operation");
1146 goto invalid;
1147 }
c26547d6
KS
1148
1149 rule_add_key(&rule_tmp, TK_A_SECLABEL, op, value, attr);
1150 continue;
1151 }
1152
33502ffe 1153 if (streq(key, "KERNELS")) {
912541b0 1154 if (op > OP_MATCH_MAX) {
9f6445e3 1155 log_error("invalid KERNELS operation");
912541b0
KS
1156 goto invalid;
1157 }
1158 rule_add_key(&rule_tmp, TK_M_KERNELS, op, value, NULL);
1159 continue;
1160 }
1161
33502ffe 1162 if (streq(key, "SUBSYSTEMS")) {
912541b0 1163 if (op > OP_MATCH_MAX) {
9f6445e3 1164 log_error("invalid SUBSYSTEMS operation");
912541b0
KS
1165 goto invalid;
1166 }
1167 rule_add_key(&rule_tmp, TK_M_SUBSYSTEMS, op, value, NULL);
1168 continue;
1169 }
1170
33502ffe 1171 if (streq(key, "DRIVERS")) {
912541b0 1172 if (op > OP_MATCH_MAX) {
9f6445e3 1173 log_error("invalid DRIVERS operation");
912541b0
KS
1174 goto invalid;
1175 }
1176 rule_add_key(&rule_tmp, TK_M_DRIVERS, op, value, NULL);
1177 continue;
1178 }
1179
33502ffe 1180 if (startswith(key, "ATTRS{")) {
912541b0 1181 if (op > OP_MATCH_MAX) {
9f6445e3 1182 log_error("invalid ATTRS operation");
912541b0
KS
1183 goto invalid;
1184 }
f8294e41 1185 attr = get_key_attribute(rules->udev, key + strlen("ATTRS"));
912541b0 1186 if (attr == NULL) {
9f6445e3 1187 log_error("error parsing ATTRS attribute");
912541b0
KS
1188 goto invalid;
1189 }
33502ffe 1190 if (startswith(attr, "device/"))
baa30fbc 1191 log_error("the 'device' link may not be available in a future kernel, "
912541b0
KS
1192 "please fix it in %s:%u", filename, lineno);
1193 else if (strstr(attr, "../") != NULL)
baa30fbc 1194 log_error("do not reference parent sysfs directories directly, "
912541b0
KS
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
33502ffe 1200 if (streq(key, "TAGS")) {
912541b0 1201 if (op > OP_MATCH_MAX) {
9f6445e3 1202 log_error("invalid TAGS operation");
912541b0
KS
1203 goto invalid;
1204 }
1205 rule_add_key(&rule_tmp, TK_M_TAGS, op, value, NULL);
1206 continue;
1207 }
1208
33502ffe 1209 if (startswith(key, "ENV{")) {
f8294e41 1210 attr = get_key_attribute(rules->udev, key + strlen("ENV"));
912541b0 1211 if (attr == NULL) {
9f6445e3 1212 log_error("error parsing ENV attribute");
912541b0
KS
1213 goto invalid;
1214 }
8e3ba377
DH
1215 if (op == OP_REMOVE) {
1216 log_error("invalid ENV operation");
1217 goto invalid;
1218 }
912541b0
KS
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
395ea978
KS
1238 for (i = 0; i < ELEMENTSOF(blacklist); i++) {
1239 if (!streq(attr, blacklist[i]))
912541b0 1240 continue;
9f6445e3 1241 log_error("invalid ENV attribute, '%s' can not be set %s:%u", attr, filename, lineno);
395ea978
KS
1242 goto invalid;
1243 }
912541b0
KS
1244 if (rule_add_key(&rule_tmp, TK_A_ENV, op, value, attr) != 0)
1245 goto invalid;
1246 }
1247 continue;
1248 }
1249
33502ffe 1250 if (streq(key, "TAG")) {
912541b0
KS
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
33502ffe 1258 if (streq(key, "PROGRAM")) {
8e3ba377
DH
1259 if (op == OP_REMOVE) {
1260 log_error("invalid PROGRAM operation");
1261 goto invalid;
1262 }
912541b0
KS
1263 rule_add_key(&rule_tmp, TK_M_PROGRAM, op, value, NULL);
1264 continue;
1265 }
1266
33502ffe 1267 if (streq(key, "RESULT")) {
912541b0 1268 if (op > OP_MATCH_MAX) {
9f6445e3 1269 log_error("invalid RESULT operation");
912541b0
KS
1270 goto invalid;
1271 }
1272 rule_add_key(&rule_tmp, TK_M_RESULT, op, value, NULL);
1273 continue;
1274 }
1275
33502ffe 1276 if (startswith(key, "IMPORT")) {
f8294e41 1277 attr = get_key_attribute(rules->udev, key + strlen("IMPORT"));
912541b0 1278 if (attr == NULL) {
9f6445e3 1279 log_error("IMPORT{} type missing, ignoring IMPORT %s:%u", filename, lineno);
912541b0
KS
1280 continue;
1281 }
8e3ba377
DH
1282 if (op == OP_REMOVE) {
1283 log_error("invalid IMPORT operation");
1284 goto invalid;
1285 }
33502ffe 1286 if (streq(attr, "program")) {
912541b0
KS
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) {
9f6445e3 1293 log_debug("IMPORT found builtin '%s', replacing %s:%u",
baa30fbc 1294 value, filename, lineno);
912541b0
KS
1295 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1296 continue;
1297 }
1298 }
912541b0 1299 rule_add_key(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL);
33502ffe 1300 } else if (streq(attr, "builtin")) {
912541b0
KS
1301 enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1302
912541b0
KS
1303 if (cmd < UDEV_BUILTIN_MAX)
1304 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1305 else
9f6445e3 1306 log_error("IMPORT{builtin}: '%s' unknown %s:%u", value, filename, lineno);
33502ffe 1307 } else if (streq(attr, "file")) {
912541b0 1308 rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL);
33502ffe 1309 } else if (streq(attr, "db")) {
912541b0 1310 rule_add_key(&rule_tmp, TK_M_IMPORT_DB, op, value, NULL);
33502ffe 1311 } else if (streq(attr, "cmdline")) {
912541b0 1312 rule_add_key(&rule_tmp, TK_M_IMPORT_CMDLINE, op, value, NULL);
33502ffe 1313 } else if (streq(attr, "parent")) {
912541b0 1314 rule_add_key(&rule_tmp, TK_M_IMPORT_PARENT, op, value, NULL);
83cd6b75 1315 } else
9f6445e3 1316 log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u", filename, lineno);
912541b0
KS
1317 continue;
1318 }
1319
33502ffe 1320 if (startswith(key, "TEST")) {
912541b0
KS
1321 mode_t mode = 0;
1322
1323 if (op > OP_MATCH_MAX) {
9f6445e3 1324 log_error("invalid TEST operation");
912541b0
KS
1325 goto invalid;
1326 }
f8294e41 1327 attr = get_key_attribute(rules->udev, key + strlen("TEST"));
912541b0
KS
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
33502ffe 1337 if (startswith(key, "RUN")) {
f8294e41 1338 attr = get_key_attribute(rules->udev, key + strlen("RUN"));
83cd6b75
KS
1339 if (attr == NULL)
1340 attr = "program";
8e3ba377
DH
1341 if (op == OP_REMOVE) {
1342 log_error("invalid RUN operation");
1343 goto invalid;
1344 }
83cd6b75 1345
33502ffe 1346 if (streq(attr, "builtin")) {
83cd6b75
KS
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
ec3281d3 1352 log_error("RUN{builtin}: '%s' unknown %s:%u", value, filename, lineno);
33502ffe 1353 } else if (streq(attr, "program")) {
83cd6b75
KS
1354 enum udev_builtin_cmd cmd = UDEV_BUILTIN_MAX;
1355
83cd6b75
KS
1356 rule_add_key(&rule_tmp, TK_A_RUN_PROGRAM, op, value, &cmd);
1357 } else {
9f6445e3 1358 log_error("RUN{} unknown type, ignoring RUN %s:%u", filename, lineno);
83cd6b75
KS
1359 }
1360
912541b0
KS
1361 continue;
1362 }
1363
33502ffe 1364 if (streq(key, "LABEL")) {
8e3ba377
DH
1365 if (op == OP_REMOVE) {
1366 log_error("invalid LABEL operation");
1367 goto invalid;
1368 }
915bf0f6 1369 rule_tmp.rule.rule.label_off = rules_add_string(rules, value);
912541b0
KS
1370 continue;
1371 }
1372
33502ffe 1373 if (streq(key, "GOTO")) {
8e3ba377
DH
1374 if (op == OP_REMOVE) {
1375 log_error("invalid GOTO operation");
1376 goto invalid;
1377 }
912541b0
KS
1378 rule_add_key(&rule_tmp, TK_A_GOTO, 0, value, NULL);
1379 continue;
1380 }
1381
33502ffe 1382 if (startswith(key, "NAME")) {
8e3ba377
DH
1383 if (op == OP_REMOVE) {
1384 log_error("invalid NAME operation");
1385 goto invalid;
1386 }
912541b0
KS
1387 if (op < OP_MATCH_MAX) {
1388 rule_add_key(&rule_tmp, TK_M_NAME, op, value, NULL);
1389 } else {
33502ffe 1390 if (streq(value, "%k")) {
baa30fbc 1391 log_error("NAME=\"%%k\" is ignored, because it breaks kernel supplied names, "
912541b0
KS
1392 "please remove it from %s:%u\n", filename, lineno);
1393 continue;
1394 }
1395 if (value[0] == '\0') {
baa30fbc
KS
1396 log_debug("NAME=\"\" is ignored, because udev will not delete any device nodes, "
1397 "please remove it from %s:%u\n", filename, lineno);
912541b0
KS
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
8a173387 1406 if (streq(key, "SYMLINK")) {
8e3ba377
DH
1407 if (op == OP_REMOVE) {
1408 log_error("invalid SYMLINK operation");
1409 goto invalid;
1410 }
8a173387 1411 if (op < OP_MATCH_MAX)
912541b0 1412 rule_add_key(&rule_tmp, TK_M_DEVLINK, op, value, NULL);
8a173387
KS
1413 else
1414 rule_add_key(&rule_tmp, TK_A_DEVLINK, op, value, NULL);
912541b0
KS
1415 rule_tmp.rule.rule.can_set_name = true;
1416 continue;
1417 }
1418
33502ffe 1419 if (streq(key, "OWNER")) {
912541b0
KS
1420 uid_t uid;
1421 char *endptr;
1422
8e3ba377
DH
1423 if (op == OP_REMOVE) {
1424 log_error("invalid OWNER operation");
1425 goto invalid;
1426 }
1427
912541b0
KS
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);
1f6b4113 1434 } else if (rules->resolve_names >= 0)
912541b0 1435 rule_add_key(&rule_tmp, TK_A_OWNER, op, value, NULL);
1f6b4113 1436
912541b0
KS
1437 rule_tmp.rule.rule.can_set_name = true;
1438 continue;
1439 }
1440
33502ffe 1441 if (streq(key, "GROUP")) {
912541b0
KS
1442 gid_t gid;
1443 char *endptr;
1444
8e3ba377
DH
1445 if (op == OP_REMOVE) {
1446 log_error("invalid GROUP operation");
1447 goto invalid;
1448 }
1449
912541b0
KS
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);
1f6b4113 1456 } else if (rules->resolve_names >= 0)
912541b0 1457 rule_add_key(&rule_tmp, TK_A_GROUP, op, value, NULL);
1f6b4113 1458
912541b0
KS
1459 rule_tmp.rule.rule.can_set_name = true;
1460 continue;
1461 }
1462
33502ffe 1463 if (streq(key, "MODE")) {
912541b0
KS
1464 mode_t mode;
1465 char *endptr;
1466
8e3ba377
DH
1467 if (op == OP_REMOVE) {
1468 log_error("invalid MODE operation");
1469 goto invalid;
1470 }
1471
912541b0
KS
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
33502ffe 1481 if (streq(key, "OPTIONS")) {
912541b0
KS
1482 const char *pos;
1483
8e3ba377
DH
1484 if (op == OP_REMOVE) {
1485 log_error("invalid OPTIONS operation");
1486 goto invalid;
1487 }
1488
912541b0
KS
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);
912541b0
KS
1494 }
1495
9f20a8a3 1496 pos = strstr(value, "string_escape=");
912541b0
KS
1497 if (pos != NULL) {
1498 pos = &pos[strlen("string_escape=")];
33502ffe 1499 if (startswith(pos, "none"))
912541b0 1500 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_NONE, op, NULL, NULL);
33502ffe 1501 else if (startswith(pos, "replace"))
912541b0
KS
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);
912541b0
KS
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);
912541b0
KS
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
9f6445e3 1532 log_error("unknown key '%s' in %s:%u", key, filename, lineno);
912541b0
KS
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;
c7521974 1546invalid:
9f6445e3 1547 log_error("invalid rule '%s:%u'", filename, lineno);
912541b0 1548 return -1;
c7521974
KS
1549}
1550
9ec6e95b 1551static int parse_file(struct udev_rules *rules, const char *filename) {
6c8aaf0c 1552 _cleanup_fclose_ FILE *f = NULL;
912541b0 1553 unsigned int first_token;
775f8b3c 1554 unsigned int filename_off;
912541b0
KS
1555 char line[UTIL_LINE_SIZE];
1556 int line_nr = 0;
1557 unsigned int i;
1558
ed88bcfb
ZJS
1559 f = fopen(filename, "re");
1560 if (!f) {
1561 if (errno == ENOENT)
1562 return 0;
1563 else
1564 return -errno;
775f8b3c 1565 }
912541b0 1566
ed88bcfb
ZJS
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);
912541b0
KS
1572
1573 first_token = rules->token_cur;
915bf0f6 1574 filename_off = rules_add_string(rules, filename);
912541b0
KS
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)) {
9f6445e3 1605 log_error("line too long '%s':%u, ignored", filename, line_nr);
912541b0
KS
1606 continue;
1607 }
1608 add_rule(rules, key, filename, filename_off, line_nr);
1609 }
912541b0
KS
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) {
915bf0f6 1614 char *label = rules_str(rules, rules->tokens[i].key.value_off);
912541b0
KS
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;
915bf0f6 1622 if (!streq(label, rules_str(rules, rules->tokens[j].rule.label_off)))
912541b0
KS
1623 continue;
1624 rules->tokens[i].key.rule_goto = j;
1625 break;
1626 }
1627 if (rules->tokens[i].key.rule_goto == 0)
9f6445e3 1628 log_error("GOTO '%s' has no matching label in: '%s'", label, filename);
912541b0
KS
1629 }
1630 }
1631 return 0;
c7521974
KS
1632}
1633
9ec6e95b 1634struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names) {
912541b0
KS
1635 struct udev_rules *rules;
1636 struct udev_list file_list;
912541b0 1637 struct token end_token;
775f8b3c
KS
1638 char **files, **f;
1639 int r;
912541b0 1640
955d98c9 1641 rules = new0(struct udev_rules, 1);
912541b0
KS
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));
0820a4f0
VP
1650 if (rules->tokens == NULL)
1651 return udev_rules_unref(rules);
912541b0
KS
1652 rules->token_max = PREALLOC_TOKEN;
1653
915bf0f6
KS
1654 rules->strbuf = strbuf_new();
1655 if (!rules->strbuf)
0820a4f0
VP
1656 return udev_rules_unref(rules);
1657
3b8c1cb0
KS
1658 udev_rules_check_timestamp(rules);
1659
2ad8416d 1660 r = conf_files_list_strv(&files, ".rules", NULL, rules_dirs);
775f8b3c 1661 if (r < 0) {
da927ba9 1662 log_error_errno(r, "failed to enumerate rules files: %m");
0820a4f0 1663 return udev_rules_unref(rules);
912541b0
KS
1664 }
1665
775f8b3c
KS
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)
915bf0f6 1671 rules_add_string(rules, *f);
912541b0 1672
775f8b3c
KS
1673 STRV_FOREACH(f, files)
1674 parse_file(rules, *f);
1675
1676 strv_free(files);
912541b0 1677
29804cc1 1678 memzero(&end_token, sizeof(struct token));
912541b0
KS
1679 end_token.type = TK_END;
1680 add_token(rules, &end_token);
9f6445e3 1681 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings",
915bf0f6 1682 rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->strbuf->len);
912541b0 1683
915bf0f6 1684 /* cleanup temporary strbuf data */
9f6445e3 1685 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used",
915bf0f6
KS
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);
912541b0
KS
1689
1690 /* cleanup uid/gid cache */
a1e58e8e 1691 rules->uids = mfree(rules->uids);
912541b0
KS
1692 rules->uids_cur = 0;
1693 rules->uids_max = 0;
a1e58e8e 1694 rules->gids = mfree(rules->gids);
912541b0
KS
1695 rules->gids_cur = 0;
1696 rules->gids_max = 0;
1697
1698 dump_rules(rules);
1699 return rules;
c7521974
KS
1700}
1701
9ec6e95b 1702struct udev_rules *udev_rules_unref(struct udev_rules *rules) {
912541b0
KS
1703 if (rules == NULL)
1704 return NULL;
1705 free(rules->tokens);
915bf0f6 1706 strbuf_cleanup(rules->strbuf);
912541b0
KS
1707 free(rules->uids);
1708 free(rules->gids);
1709 free(rules);
1710 return NULL;
c7521974 1711}
6880b25d 1712
9ec6e95b 1713bool udev_rules_check_timestamp(struct udev_rules *rules) {
5c11fbe3
KS
1714 if (!rules)
1715 return false;
1716
2ad8416d 1717 return paths_check_timestamp(rules_dirs, &rules->dirs_ts_usec, true);
6ada823a
KS
1718}
1719
9ec6e95b 1720static int match_key(struct udev_rules *rules, struct token *token, const char *val) {
915bf0f6 1721 char *key_value = rules_str(rules, token->key.value_off);
912541b0
KS
1722 char *pos;
1723 bool match = false;
1724
1725 if (val == NULL)
1726 val = "";
1727
1728 switch (token->key.glob) {
1729 case GL_PLAIN:
33502ffe 1730 match = (streq(key_value, val));
912541b0
KS
1731 break;
1732 case GL_GLOB:
1733 match = (fnmatch(key_value, val, 0) == 0);
1734 break;
1735 case GL_SPLIT:
1736 {
33502ffe 1737 const char *s;
912541b0
KS
1738 size_t len;
1739
915bf0f6 1740 s = rules_str(rules, token->key.value_off);
912541b0
KS
1741 len = strlen(val);
1742 for (;;) {
1743 const char *next;
1744
33502ffe 1745 next = strchr(s, '|');
912541b0 1746 if (next != NULL) {
33502ffe 1747 size_t matchlen = (size_t)(next - s);
912541b0 1748
641906e9 1749 match = (matchlen == len && strneq(s, val, matchlen));
912541b0
KS
1750 if (match)
1751 break;
1752 } else {
33502ffe 1753 match = (streq(s, val));
912541b0
KS
1754 break;
1755 }
33502ffe 1756 s = &next[1];
912541b0
KS
1757 }
1758 break;
1759 }
1760 case GL_SPLIT_GLOB:
1761 {
1762 char value[UTIL_PATH_SIZE];
1763
d5a89d7d 1764 strscpy(value, sizeof(value), rules_str(rules, token->key.value_off));
912541b0
KS
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 }
912541b0
KS
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
baa30fbc 1786 if (match && (token->key.op == OP_MATCH))
912541b0 1787 return 0;
baa30fbc 1788 if (!match && (token->key.op == OP_NOMATCH))
912541b0 1789 return 0;
912541b0 1790 return -1;
6880b25d
KS
1791}
1792
9ec6e95b 1793static int match_attr(struct udev_rules *rules, struct udev_device *dev, struct udev_event *event, struct token *cur) {
912541b0
KS
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
915bf0f6 1800 name = rules_str(rules, cur->key.attr_off);
912541b0
KS
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
915bf0f6 1826 key_value = rules_str(rules, cur->key.value_off);
912541b0
KS
1827 klen = strlen(key_value);
1828 if (klen > 0 && !isspace(key_value[klen-1])) {
1829 if (value != vbuf) {
d5a89d7d 1830 strscpy(vbuf, sizeof(vbuf), value);
912541b0
KS
1831 value = vbuf;
1832 }
1833 while (len > 0 && isspace(vbuf[--len]))
1834 vbuf[len] = '\0';
912541b0
KS
1835 }
1836 }
1837
1838 return match_key(rules, cur, value);
6880b25d
KS
1839}
1840
1841enum escape_type {
912541b0
KS
1842 ESCAPE_UNSET,
1843 ESCAPE_NONE,
1844 ESCAPE_REPLACE,
6880b25d
KS
1845};
1846
dd5eddd2
KS
1847int udev_rules_apply_to_event(struct udev_rules *rules,
1848 struct udev_event *event,
1849 usec_t timeout_usec,
67117413 1850 usec_t timeout_warn_usec,
8314de1d 1851 struct udev_list *properties_list) {
912541b0
KS
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
33502ffe 1860 can_set_name = ((!streq(udev_device_get_action(event->dev), "remove")) &&
912541b0
KS
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: {
912541b0
KS
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
920b52e4 1897 devlink = udev_list_entry_get_name(list_entry) + strlen("/dev/");
912541b0
KS
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: {
915bf0f6 1912 const char *key_name = rules_str(rules, cur->key.attr_off);
912541b0
KS
1913 const char *value;
1914
1915 value = udev_device_get_property_value(event->dev, key_name);
adeba500
KS
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)
912541b0 1928 value = "";
912541b0
KS
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)) {
915bf0f6 1938 if (streq(rules_str(rules, cur->key.value_off), udev_list_entry_get_name(list_entry))) {
912541b0
KS
1939 match = true;
1940 break;
1941 }
1942 }
4302857b
FF
1943 if ((!match && (cur->key.op != OP_NOMATCH)) ||
1944 (match && (cur->key.op == OP_NOMATCH)))
912541b0
KS
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;
912541b0
KS
1956 case TK_M_ATTR:
1957 if (match_attr(rules, event->dev, event, cur) != 0)
1958 goto nomatch;
1959 break;
f4cf2e5b
KS
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 }
912541b0
KS
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
912541b0
KS
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: {
915bf0f6 2015 bool match = udev_device_has_tag(event->dev_parent, rules_str(rules, cur->key.value_off));
912541b0
KS
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 }
912541b0 2026 }
912541b0
KS
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
915bf0f6 2043 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), filename, sizeof(filename));
912541b0
KS
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
d5a89d7d
KS
2048 strscpy(tmp, sizeof(tmp), filename);
2049 strscpyl(filename, sizeof(filename),
912541b0
KS
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);
baa30fbc 2056 if (match && cur->key.mode > 0)
912541b0 2057 match = ((statbuf.st_mode & cur->key.mode) > 0);
912541b0
KS
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 }
912541b0
KS
2064 case TK_M_PROGRAM: {
2065 char program[UTIL_PATH_SIZE];
209b031e 2066 char result[UTIL_LINE_SIZE];
912541b0 2067
a1e58e8e 2068 event->program_result = mfree(event->program_result);
915bf0f6 2069 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), program, sizeof(program));
9f6445e3 2070 log_debug("PROGRAM '%s' %s:%u",
baa30fbc 2071 program,
915bf0f6 2072 rules_str(rules, rule->rule.filename_off),
baa30fbc 2073 rule->rule.filename_line);
912541b0 2074
bbf35206 2075 if (udev_event_spawn(event, timeout_usec, timeout_warn_usec, true, program, result, sizeof(result)) < 0) {
912541b0
KS
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)
9f6445e3 2085 log_debug("%i character(s) replaced" , count);
912541b0
KS
2086 }
2087 event->program_result = strdup(result);
912541b0
KS
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
915bf0f6 2096 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
912541b0
KS
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
915bf0f6 2105 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
9f6445e3 2106 log_debug("IMPORT '%s' %s:%u",
baa30fbc 2107 import,
915bf0f6 2108 rules_str(rules, rule->rule.filename_off),
baa30fbc 2109 rule->rule.filename_line);
912541b0 2110
8314de1d 2111 if (import_program_into_properties(event, timeout_usec, timeout_warn_usec, import) != 0)
912541b0
KS
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)) {
9f6445e3 2122 log_debug("IMPORT builtin skip '%s' %s:%u",
baa30fbc 2123 udev_builtin_name(cur->key.builtin_cmd),
915bf0f6 2124 rules_str(rules, rule->rule.filename_off),
baa30fbc 2125 rule->rule.filename_line);
912541b0
KS
2126 /* return the result from earlier run */
2127 if (event->builtin_ret & (1 << cur->key.builtin_cmd))
7d6884b6 2128 if (cur->key.op != OP_NOMATCH)
912541b0
KS
2129 goto nomatch;
2130 break;
2131 }
2132 /* mark as ran */
2133 event->builtin_run |= (1 << cur->key.builtin_cmd);
2134 }
2135
915bf0f6 2136 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), command, sizeof(command));
9f6445e3 2137 log_debug("IMPORT builtin '%s' %s:%u",
baa30fbc 2138 udev_builtin_name(cur->key.builtin_cmd),
915bf0f6 2139 rules_str(rules, rule->rule.filename_off),
baa30fbc 2140 rule->rule.filename_line);
912541b0
KS
2141
2142 if (udev_builtin_run(event->dev, cur->key.builtin_cmd, command, false) != 0) {
2143 /* remember failure */
9f6445e3 2144 log_debug("IMPORT builtin '%s' returned non-zero",
baa30fbc 2145 udev_builtin_name(cur->key.builtin_cmd));
912541b0
KS
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: {
915bf0f6 2153 const char *key = rules_str(rules, cur->key.value_off);
912541b0
KS
2154 const char *value;
2155
2156 value = udev_device_get_property_value(event->dev_db, key);
df546eb5
TG
2157 if (value != NULL)
2158 udev_device_add_property(event->dev, key, value);
2159 else {
912541b0
KS
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
47ef94ac 2169 f = fopen("/proc/cmdline", "re");
912541b0
KS
2170 if (f != NULL) {
2171 char cmdline[4096];
2172
2173 if (fgets(cmdline, sizeof(cmdline), f) != NULL) {
915bf0f6 2174 const char *key = rules_str(rules, cur->key.value_off);
912541b0
KS
2175 char *pos;
2176
2177 pos = strstr(cmdline, key);
2178 if (pos != NULL) {
912541b0
KS
2179 pos += strlen(key);
2180 if (pos[0] == '\0' || isspace(pos[0])) {
2181 /* we import simple flags as 'FLAG=1' */
df546eb5 2182 udev_device_add_property(event->dev, key, "1");
912541b0
KS
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';
df546eb5 2192 udev_device_add_property(event->dev, key, value);
912541b0
KS
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
915bf0f6 2206 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
912541b0
KS
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];
23bf8dd7
TG
2237 const char *ow = owner;
2238 int r;
912541b0
KS
2239
2240 if (event->owner_final)
2241 break;
2242 if (cur->key.op == OP_ASSIGN_FINAL)
2243 event->owner_final = true;
915bf0f6 2244 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), owner, sizeof(owner));
1edefa4f 2245 event->owner_set = true;
23bf8dd7
TG
2246 r = get_user_creds(&ow, &event->uid, NULL, NULL, NULL);
2247 if (r < 0) {
2248 if (r == -ENOENT || r == -ESRCH)
ff49bc32 2249 log_error("specified user '%s' unknown", owner);
23bf8dd7 2250 else
279d3c9c 2251 log_error_errno(r, "error resolving user '%s': %m", owner);
23bf8dd7
TG
2252
2253 event->uid = 0;
2254 }
9f6445e3 2255 log_debug("OWNER %u %s:%u",
baa30fbc 2256 event->uid,
915bf0f6 2257 rules_str(rules, rule->rule.filename_off),
baa30fbc 2258 rule->rule.filename_line);
912541b0
KS
2259 break;
2260 }
2261 case TK_A_GROUP: {
2262 char group[UTIL_NAME_SIZE];
23bf8dd7
TG
2263 const char *gr = group;
2264 int r;
912541b0
KS
2265
2266 if (event->group_final)
2267 break;
2268 if (cur->key.op == OP_ASSIGN_FINAL)
2269 event->group_final = true;
915bf0f6 2270 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), group, sizeof(group));
1edefa4f 2271 event->group_set = true;
23bf8dd7
TG
2272 r = get_group_creds(&gr, &event->gid);
2273 if (r < 0) {
2274 if (r == -ENOENT || r == -ESRCH)
ff49bc32 2275 log_error("specified group '%s' unknown", group);
23bf8dd7 2276 else
279d3c9c 2277 log_error_errno(r, "error resolving group '%s': %m", group);
23bf8dd7
TG
2278
2279 event->gid = 0;
2280 }
9f6445e3 2281 log_debug("GROUP %u %s:%u",
baa30fbc 2282 event->gid,
915bf0f6 2283 rules_str(rules, rule->rule.filename_off),
baa30fbc 2284 rule->rule.filename_line);
912541b0
KS
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;
915bf0f6 2294 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), mode_str, sizeof(mode_str));
912541b0
KS
2295 mode = strtol(mode_str, &endptr, 8);
2296 if (endptr[0] != '\0') {
9f6445e3 2297 log_error("ignoring invalid mode '%s'", mode_str);
912541b0
KS
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;
9f6445e3 2304 log_debug("MODE %#o %s:%u",
baa30fbc 2305 event->mode,
915bf0f6 2306 rules_str(rules, rule->rule.filename_off),
baa30fbc 2307 rule->rule.filename_line);
912541b0
KS
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;
1edefa4f 2315 event->owner_set = true;
912541b0 2316 event->uid = cur->key.uid;
9f6445e3 2317 log_debug("OWNER %u %s:%u",
baa30fbc 2318 event->uid,
915bf0f6 2319 rules_str(rules, rule->rule.filename_off),
baa30fbc 2320 rule->rule.filename_line);
912541b0
KS
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;
1edefa4f 2327 event->group_set = true;
912541b0 2328 event->gid = cur->key.gid;
9f6445e3 2329 log_debug("GROUP %u %s:%u",
baa30fbc 2330 event->gid,
915bf0f6 2331 rules_str(rules, rule->rule.filename_off),
baa30fbc 2332 rule->rule.filename_line);
912541b0
KS
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;
9f6445e3 2341 log_debug("MODE %#o %s:%u",
baa30fbc 2342 event->mode,
915bf0f6 2343 rules_str(rules, rule->rule.filename_off),
baa30fbc 2344 rule->rule.filename_line);
912541b0 2345 break;
c26547d6
KS
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);
9f6445e3 2354 log_debug("SECLABEL{%s}='%s' %s:%u",
c26547d6
KS
2355 name, label,
2356 rules_str(rules, rule->rule.filename_off),
2357 rule->rule.filename_line);
2358 break;
2359 }
912541b0 2360 case TK_A_ENV: {
915bf0f6
KS
2361 const char *name = rules_str(rules, cur->key.attr_off);
2362 char *value = rules_str(rules, cur->key.value_off);
07845c14
KS
2363 char value_new[UTIL_NAME_SIZE];
2364 const char *value_old = NULL;
912541b0 2365
07845c14
KS
2366 if (value[0] == '\0') {
2367 if (cur->key.op == OP_ADD)
2368 break;
912541b0 2369 udev_device_add_property(event->dev, name, NULL);
07845c14 2370 break;
912541b0 2371 }
07845c14
KS
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));
d5a89d7d 2380 strscpyl(value_new, sizeof(value_new), value_old, " ", temp, NULL);
07845c14
KS
2381 } else
2382 udev_event_apply_format(event, value, value_new, sizeof(value_new));
2383
df546eb5 2384 udev_device_add_property(event->dev, name, value_new);
912541b0
KS
2385 break;
2386 }
2387 case TK_A_TAG: {
2388 char tag[UTIL_PATH_SIZE];
2389 const char *p;
2390
915bf0f6 2391 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), tag, sizeof(tag));
912541b0
KS
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;
9f6445e3 2400 log_error("ignoring invalid tag name '%s'", tag);
912541b0
KS
2401 break;
2402 }
8e3ba377
DH
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);
912541b0
KS
2407 break;
2408 }
2409 case TK_A_NAME: {
915bf0f6 2410 const char *name = rules_str(rules, cur->key.value_off);
0ecfcbd4 2411
912541b0
KS
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)
9f6445e3 2423 log_debug("%i character(s) replaced", count);
912541b0 2424 }
6ada823a 2425 if (major(udev_device_get_devnum(event->dev)) &&
4cb72937 2426 (!streq(name_str, udev_device_get_devnode(event->dev) + strlen("/dev/")))) {
6ada823a
KS
2427 log_error("NAME=\"%s\" ignored, kernel device nodes "
2428 "can not be renamed; please fix it in %s:%u\n", name,
915bf0f6 2429 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
6ada823a 2430 break;
0ecfcbd4 2431 }
2fc09a9c 2432 free_and_strdup(&event->name, name_str);
9f6445e3 2433 log_debug("NAME '%s' %s:%u",
baa30fbc 2434 event->name,
915bf0f6 2435 rules_str(rules, rule->rule.filename_off),
baa30fbc 2436 rule->rule.filename_line);
912541b0
KS
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 */
915bf0f6 2455 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), temp, sizeof(temp));
912541b0
KS
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)
9f6445e3 2461 log_debug("%i character(s) replaced" , count);
912541b0
KS
2462 pos = temp;
2463 while (isspace(pos[0]))
2464 pos++;
2465 next = strchr(pos, ' ');
2466 while (next != NULL) {
2467 next[0] = '\0';
9f6445e3 2468 log_debug("LINK '%s' %s:%u", pos,
915bf0f6 2469 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
d5a89d7d 2470 strscpyl(filename, sizeof(filename), "/dev/", pos, NULL);
8a173387 2471 udev_device_add_devlink(event->dev, filename);
912541b0
KS
2472 while (isspace(next[1]))
2473 next++;
2474 pos = &next[1];
2475 next = strchr(pos, ' ');
2476 }
2477 if (pos[0] != '\0') {
9f6445e3 2478 log_debug("LINK '%s' %s:%u", pos,
915bf0f6 2479 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
d5a89d7d 2480 strscpyl(filename, sizeof(filename), "/dev/", pos, NULL);
8a173387 2481 udev_device_add_devlink(event->dev, filename);
912541b0
KS
2482 }
2483 break;
2484 }
2485 case TK_A_ATTR: {
915bf0f6 2486 const char *key_name = rules_str(rules, cur->key.attr_off);
912541b0
KS
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)
d5a89d7d 2492 strscpyl(attr, sizeof(attr), udev_device_get_syspath(event->dev), "/", key_name, NULL);
912541b0
KS
2493 attr_subst_subdir(attr, sizeof(attr));
2494
915bf0f6 2495 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), value, sizeof(value));
9f6445e3 2496 log_debug("ATTR '%s' writing '%s' %s:%u", attr, value,
915bf0f6 2497 rules_str(rules, rule->rule.filename_off),
baa30fbc 2498 rule->rule.filename_line);
47ef94ac 2499 f = fopen(attr, "we");
912541b0
KS
2500 if (f != NULL) {
2501 if (fprintf(f, "%s", value) <= 0)
56f64d95 2502 log_error_errno(errno, "error writing ATTR{%s}: %m", attr);
912541b0
KS
2503 fclose(f);
2504 } else {
56f64d95 2505 log_error_errno(errno, "error opening ATTR{%s} for writing: %m", attr);
912541b0
KS
2506 }
2507 break;
2508 }
f4cf2e5b
KS
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)
46b57b6e 2521 log_error_errno(r, "error writing SYSCTL{%s}='%s': %m", filename, value);
f4cf2e5b
KS
2522 break;
2523 }
83cd6b75
KS
2524 case TK_A_RUN_BUILTIN:
2525 case TK_A_RUN_PROGRAM: {
2526 struct udev_list_entry *entry;
2527
912541b0
KS
2528 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2529 udev_list_cleanup(&event->run_list);
9f6445e3 2530 log_debug("RUN '%s' %s:%u",
915bf0f6
KS
2531 rules_str(rules, cur->key.value_off),
2532 rules_str(rules, rule->rule.filename_off),
baa30fbc 2533 rule->rule.filename_line);
915bf0f6 2534 entry = udev_list_entry_add(&event->run_list, rules_str(rules, cur->key.value_off), NULL);
83cd6b75 2535 udev_list_entry_set_num(entry, cur->key.builtin_cmd);
912541b0
KS
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:
9f6445e3 2550 log_error("wrong type %u", cur->type);
912541b0
KS
2551 goto nomatch;
2552 }
2553
2554 cur++;
2555 continue;
2556 nomatch:
2557 /* fast-forward to next rule */
2558 cur = rule + rule->rule.token_count;
912541b0 2559 }
6880b25d 2560}
761dfddc 2561
9ec6e95b 2562int udev_rules_apply_static_dev_perms(struct udev_rules *rules) {
912541b0
KS
2563 struct token *cur;
2564 struct token *rule;
2565 uid_t uid = 0;
2566 gid_t gid = 0;
2567 mode_t mode = 0;
84b6ad70
TG
2568 _cleanup_strv_free_ char **tags = NULL;
2569 char **t;
2570 FILE *f = NULL;
2571 _cleanup_free_ char *path = NULL;
2572 int r = 0;
912541b0
KS
2573
2574 if (rules->tokens == NULL)
84b6ad70 2575 return 0;
912541b0
KS
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;
97b11eed 2592 tags = strv_free(tags);
912541b0
KS
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;
84b6ad70
TG
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
912541b0
KS
2608 break;
2609 case TK_A_STATIC_NODE: {
84b6ad70
TG
2610 char device_node[UTIL_PATH_SIZE];
2611 char tags_dir[UTIL_PATH_SIZE];
2612 char tag_symlink[UTIL_PATH_SIZE];
912541b0
KS
2613 struct stat stats;
2614
2615 /* we assure, that the permissions tokens are sorted before the static token */
ca2bb160 2616
84b6ad70 2617 if (mode == 0 && uid == 0 && gid == 0 && tags == NULL)
912541b0 2618 goto next;
d6f116a7 2619
84b6ad70 2620 strscpyl(device_node, sizeof(device_node), "/dev/", rules_str(rules, cur->key.value_off), NULL);
ca2bb160
KS
2621 if (stat(device_node, &stats) != 0)
2622 break;
2623 if (!S_ISBLK(stats.st_mode) && !S_ISCHR(stats.st_mode))
2624 break;
84b6ad70 2625
d6f116a7 2626 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
84b6ad70 2627 if (tags) {
84b6ad70
TG
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);
f647962d
MS
2633 if (r < 0)
2634 return log_error_errno(r, "failed to create %s: %m", tags_dir);
84b6ad70
TG
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);
4a62c710
MS
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
84b6ad70
TG
2644 r = 0;
2645 }
2646 }
2647
15a72200
TG
2648 /* don't touch the permissions if only the tags were set */
2649 if (mode == 0 && uid == 0 && gid == 0)
d6f116a7
KS
2650 break;
2651
912541b0
KS
2652 if (mode == 0) {
2653 if (gid > 0)
2654 mode = 0660;
2655 else
2656 mode = 0600;
2657 }
2658 if (mode != (stats.st_mode & 01777)) {
490f0087
TG
2659 r = chmod(device_node, mode);
2660 if (r < 0) {
9f6445e3 2661 log_error("failed to chmod '%s' %#o", device_node, mode);
490f0087
TG
2662 return -errno;
2663 } else
9f6445e3 2664 log_debug("chmod '%s' %#o", device_node, mode);
912541b0
KS
2665 }
2666
2667 if ((uid != 0 && uid != stats.st_uid) || (gid != 0 && gid != stats.st_gid)) {
490f0087
TG
2668 r = chown(device_node, uid, gid);
2669 if (r < 0) {
9f6445e3 2670 log_error("failed to chown '%s' %u %u ", device_node, uid, gid);
490f0087
TG
2671 return -errno;
2672 } else
9f6445e3 2673 log_debug("chown '%s' %u %u", device_node, uid, gid);
912541b0
KS
2674 }
2675
84b6ad70 2676 utimensat(AT_FDCWD, device_node, NULL, 0);
912541b0
KS
2677 break;
2678 }
2679 case TK_END:
84b6ad70 2680 goto finish;
912541b0
KS
2681 }
2682
2683 cur++;
2684 continue;
761dfddc 2685next:
912541b0
KS
2686 /* fast-forward to next rule */
2687 cur = rule + rule->rule.token_count;
2688 continue;
2689 }
84b6ad70
TG
2690
2691finish:
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;
761dfddc 2704}