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