]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udev-rules.c
tree-wide: introduce mfree()
[thirdparty/systemd.git] / src / udev / udev-rules.c
CommitLineData
2232cac8 1/*
1298001e 2 * Copyright (C) 2003-2012 Kay Sievers <kay@vrfy.org>
2232cac8 3 *
55e9959b
KS
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
3d6d12c6 8 *
55e9959b
KS
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
2232cac8 13 *
55e9959b
KS
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
2232cac8
GKH
16 */
17
18#include <stddef.h>
1449a55d 19#include <limits.h>
2232cac8 20#include <stdlib.h>
6d87ee2e 21#include <stdbool.h>
2232cac8
GKH
22#include <string.h>
23#include <stdio.h>
24#include <fcntl.h>
25#include <ctype.h>
26#include <unistd.h>
27#include <errno.h>
5ee7ecfb 28#include <dirent.h>
cea61f5c 29#include <fnmatch.h>
959e8b5d 30#include <time.h>
2232cac8 31
2232cac8 32#include "udev.h"
9eb977db 33#include "path-util.h"
2c21044f 34#include "conf-files.h"
915bf0f6 35#include "strbuf.h"
84b6ad70 36#include "strv.h"
1291bc89 37#include "util.h"
f4cf2e5b 38#include "sysctl-util.h"
2232cac8 39
a1525d17 40#define PREALLOC_TOKEN 2048
c7521974 41
4052400f 42struct uid_gid {
912541b0
KS
43 unsigned int name_off;
44 union {
a1525d17
KS
45 uid_t uid;
46 gid_t gid;
912541b0 47 };
4052400f
KS
48};
49
2ad8416d
ZJS
50static const char* const rules_dirs[] = {
51 "/etc/udev/rules.d",
52 "/run/udev/rules.d",
53 UDEVLIBEXECDIR "/rules.d",
54 NULL};
55
4052400f 56struct udev_rules {
912541b0 57 struct udev *udev;
97f2d76d 58 usec_t dirs_ts_usec;
912541b0
KS
59 int resolve_names;
60
61 /* every key in the rules file becomes a token */
62 struct token *tokens;
63 unsigned int token_cur;
64 unsigned int token_max;
65
ab06eef8 66 /* all key strings are copied and de-duplicated in a single continuous string buffer */
915bf0f6 67 struct strbuf *strbuf;
912541b0
KS
68
69 /* during rule parsing, uid/gid lookup results are cached */
70 struct uid_gid *uids;
71 unsigned int uids_cur;
72 unsigned int uids_max;
73 struct uid_gid *gids;
74 unsigned int gids_cur;
75 unsigned int gids_max;
4052400f
KS
76};
77
915bf0f6
KS
78static char *rules_str(struct udev_rules *rules, unsigned int off) {
79 return rules->strbuf->buf + off;
80}
81
82static unsigned int rules_add_string(struct udev_rules *rules, const char *s) {
83 return strbuf_add_string(rules->strbuf, s, strlen(s));
84}
85
8e3ba377 86/* KEY=="", KEY!="", KEY+="", KEY-="", KEY="", KEY:="" */
b0f7409f 87enum operation_type {
912541b0 88 OP_UNSET,
b0f7409f 89
912541b0
KS
90 OP_MATCH,
91 OP_NOMATCH,
92 OP_MATCH_MAX,
b0f7409f 93
912541b0 94 OP_ADD,
8e3ba377 95 OP_REMOVE,
912541b0
KS
96 OP_ASSIGN,
97 OP_ASSIGN_FINAL,
c7521974
KS
98};
99
ac218d9c 100enum string_glob_type {
912541b0 101 GL_UNSET,
a1525d17 102 GL_PLAIN, /* no special chars */
912541b0 103 GL_GLOB, /* shell globs ?,*,[] */
a1525d17
KS
104 GL_SPLIT, /* multi-value A|B */
105 GL_SPLIT_GLOB, /* multi-value with glob A*|B* */
106 GL_SOMETHING, /* commonly used "?*" */
ac218d9c
KS
107};
108
32028733 109enum string_subst_type {
912541b0
KS
110 SB_UNSET,
111 SB_NONE,
112 SB_FORMAT,
113 SB_SUBSYS,
32028733
KS
114};
115
154a7b84 116/* tokens of a rule are sorted/handled in this order */
6880b25d 117enum token_type {
912541b0
KS
118 TK_UNSET,
119 TK_RULE,
120
a1525d17
KS
121 TK_M_ACTION, /* val */
122 TK_M_DEVPATH, /* val */
123 TK_M_KERNEL, /* val */
124 TK_M_DEVLINK, /* val */
125 TK_M_NAME, /* val */
126 TK_M_ENV, /* val, attr */
127 TK_M_TAG, /* val */
128 TK_M_SUBSYSTEM, /* val */
129 TK_M_DRIVER, /* val */
130 TK_M_WAITFOR, /* val */
131 TK_M_ATTR, /* val, attr */
f4cf2e5b 132 TK_M_SYSCTL, /* val, attr */
912541b0
KS
133
134 TK_M_PARENTS_MIN,
a1525d17 135 TK_M_KERNELS, /* val */
912541b0 136 TK_M_SUBSYSTEMS, /* val */
a1525d17
KS
137 TK_M_DRIVERS, /* val */
138 TK_M_ATTRS, /* val, attr */
139 TK_M_TAGS, /* val */
912541b0
KS
140 TK_M_PARENTS_MAX,
141
a1525d17 142 TK_M_TEST, /* val, mode_t */
a1525d17
KS
143 TK_M_PROGRAM, /* val */
144 TK_M_IMPORT_FILE, /* val */
145 TK_M_IMPORT_PROG, /* val */
146 TK_M_IMPORT_BUILTIN, /* val */
147 TK_M_IMPORT_DB, /* val */
148 TK_M_IMPORT_CMDLINE, /* val */
149 TK_M_IMPORT_PARENT, /* val */
150 TK_M_RESULT, /* val */
912541b0
KS
151 TK_M_MAX,
152
153 TK_A_STRING_ESCAPE_NONE,
154 TK_A_STRING_ESCAPE_REPLACE,
155 TK_A_DB_PERSIST,
a1525d17
KS
156 TK_A_INOTIFY_WATCH, /* int */
157 TK_A_DEVLINK_PRIO, /* int */
158 TK_A_OWNER, /* val */
159 TK_A_GROUP, /* val */
160 TK_A_MODE, /* val */
161 TK_A_OWNER_ID, /* uid_t */
162 TK_A_GROUP_ID, /* gid_t */
163 TK_A_MODE_ID, /* mode_t */
84b6ad70 164 TK_A_TAG, /* val */
a1525d17 165 TK_A_STATIC_NODE, /* val */
c26547d6 166 TK_A_SECLABEL, /* val, attr */
a1525d17 167 TK_A_ENV, /* val, attr */
a1525d17
KS
168 TK_A_NAME, /* val */
169 TK_A_DEVLINK, /* val */
170 TK_A_ATTR, /* val, attr */
f4cf2e5b 171 TK_A_SYSCTL, /* val, attr */
83cd6b75
KS
172 TK_A_RUN_BUILTIN, /* val, bool */
173 TK_A_RUN_PROGRAM, /* val, bool */
a1525d17 174 TK_A_GOTO, /* size_t */
912541b0
KS
175
176 TK_END,
c7521974
KS
177};
178
3f3aa9f5 179/* we try to pack stuff in a way that we take only 12 bytes per token */
4052400f 180struct token {
912541b0
KS
181 union {
182 unsigned char type; /* same in rule and key */
183 struct {
184 enum token_type type:8;
185 bool can_set_name:1;
186 bool has_static_node:1;
187 unsigned int unused:6;
188 unsigned short token_count;
189 unsigned int label_off;
190 unsigned short filename_off;
191 unsigned short filename_line;
192 } rule;
193 struct {
194 enum token_type type:8;
195 enum operation_type op:8;
196 enum string_glob_type glob:8;
197 enum string_subst_type subst:4;
198 enum string_subst_type attrsubst:4;
199 unsigned int value_off;
200 union {
201 unsigned int attr_off;
912541b0
KS
202 unsigned int rule_goto;
203 mode_t mode;
204 uid_t uid;
205 gid_t gid;
206 int devlink_prio;
912541b0
KS
207 int watch;
208 enum udev_builtin_cmd builtin_cmd;
209 };
210 } key;
211 };
4052400f
KS
212};
213
912541b0 214#define MAX_TK 64
4052400f 215struct rule_tmp {
912541b0
KS
216 struct udev_rules *rules;
217 struct token rule;
218 struct token token[MAX_TK];
219 unsigned int token_cur;
4052400f
KS
220};
221
baa30fbc 222#ifdef DEBUG
9ec6e95b 223static const char *operation_str(enum operation_type type) {
912541b0 224 static const char *operation_strs[] = {
a1525d17
KS
225 [OP_UNSET] = "UNSET",
226 [OP_MATCH] = "match",
227 [OP_NOMATCH] = "nomatch",
912541b0
KS
228 [OP_MATCH_MAX] = "MATCH_MAX",
229
a1525d17 230 [OP_ADD] = "add",
8e3ba377 231 [OP_REMOVE] = "remove",
a1525d17
KS
232 [OP_ASSIGN] = "assign",
233 [OP_ASSIGN_FINAL] = "assign-final",
912541b0
KS
234} ;
235
236 return operation_strs[type];
5d6a1fa6 237}
4052400f 238
9ec6e95b 239static const char *string_glob_str(enum string_glob_type type) {
912541b0 240 static const char *string_glob_strs[] = {
a1525d17
KS
241 [GL_UNSET] = "UNSET",
242 [GL_PLAIN] = "plain",
243 [GL_GLOB] = "glob",
244 [GL_SPLIT] = "split",
245 [GL_SPLIT_GLOB] = "split-glob",
246 [GL_SOMETHING] = "split-glob",
912541b0
KS
247 };
248
249 return string_glob_strs[type];
5d6a1fa6
KS
250}
251
9ec6e95b 252static const char *token_str(enum token_type type) {
912541b0 253 static const char *token_strs[] = {
a1525d17
KS
254 [TK_UNSET] = "UNSET",
255 [TK_RULE] = "RULE",
912541b0 256
a1525d17 257 [TK_M_ACTION] = "M ACTION",
912541b0 258 [TK_M_DEVPATH] = "M DEVPATH",
a1525d17 259 [TK_M_KERNEL] = "M KERNEL",
912541b0 260 [TK_M_DEVLINK] = "M DEVLINK",
a1525d17
KS
261 [TK_M_NAME] = "M NAME",
262 [TK_M_ENV] = "M ENV",
263 [TK_M_TAG] = "M TAG",
264 [TK_M_SUBSYSTEM] = "M SUBSYSTEM",
265 [TK_M_DRIVER] = "M DRIVER",
912541b0 266 [TK_M_WAITFOR] = "M WAITFOR",
a1525d17 267 [TK_M_ATTR] = "M ATTR",
f4cf2e5b 268 [TK_M_SYSCTL] = "M SYSCTL",
912541b0 269
a1525d17 270 [TK_M_PARENTS_MIN] = "M PARENTS_MIN",
912541b0 271 [TK_M_KERNELS] = "M KERNELS",
a1525d17 272 [TK_M_SUBSYSTEMS] = "M SUBSYSTEMS",
912541b0 273 [TK_M_DRIVERS] = "M DRIVERS",
a1525d17
KS
274 [TK_M_ATTRS] = "M ATTRS",
275 [TK_M_TAGS] = "M TAGS",
276 [TK_M_PARENTS_MAX] = "M PARENTS_MAX",
912541b0 277
a1525d17 278 [TK_M_TEST] = "M TEST",
912541b0 279 [TK_M_PROGRAM] = "M PROGRAM",
a1525d17
KS
280 [TK_M_IMPORT_FILE] = "M IMPORT_FILE",
281 [TK_M_IMPORT_PROG] = "M IMPORT_PROG",
282 [TK_M_IMPORT_BUILTIN] = "M IMPORT_BUILTIN",
283 [TK_M_IMPORT_DB] = "M IMPORT_DB",
284 [TK_M_IMPORT_CMDLINE] = "M IMPORT_CMDLINE",
285 [TK_M_IMPORT_PARENT] = "M IMPORT_PARENT",
286 [TK_M_RESULT] = "M RESULT",
287 [TK_M_MAX] = "M MAX",
288
289 [TK_A_STRING_ESCAPE_NONE] = "A STRING_ESCAPE_NONE",
290 [TK_A_STRING_ESCAPE_REPLACE] = "A STRING_ESCAPE_REPLACE",
291 [TK_A_DB_PERSIST] = "A DB_PERSIST",
292 [TK_A_INOTIFY_WATCH] = "A INOTIFY_WATCH",
293 [TK_A_DEVLINK_PRIO] = "A DEVLINK_PRIO",
294 [TK_A_OWNER] = "A OWNER",
295 [TK_A_GROUP] = "A GROUP",
296 [TK_A_MODE] = "A MODE",
297 [TK_A_OWNER_ID] = "A OWNER_ID",
298 [TK_A_GROUP_ID] = "A GROUP_ID",
299 [TK_A_STATIC_NODE] = "A STATIC_NODE",
c26547d6 300 [TK_A_SECLABEL] = "A SECLABEL",
912541b0 301 [TK_A_MODE_ID] = "A MODE_ID",
a1525d17
KS
302 [TK_A_ENV] = "A ENV",
303 [TK_A_TAG] = "A ENV",
304 [TK_A_NAME] = "A NAME",
912541b0 305 [TK_A_DEVLINK] = "A DEVLINK",
a1525d17 306 [TK_A_ATTR] = "A ATTR",
f4cf2e5b 307 [TK_A_SYSCTL] = "A SYSCTL",
83cd6b75
KS
308 [TK_A_RUN_BUILTIN] = "A RUN_BUILTIN",
309 [TK_A_RUN_PROGRAM] = "A RUN_PROGRAM",
a1525d17 310 [TK_A_GOTO] = "A GOTO",
912541b0 311
a1525d17 312 [TK_END] = "END",
912541b0
KS
313 };
314
315 return token_strs[type];
5d6a1fa6 316}
c7521974 317
9ec6e95b 318static void dump_token(struct udev_rules *rules, struct token *token) {
912541b0
KS
319 enum token_type type = token->type;
320 enum operation_type op = token->key.op;
321 enum string_glob_type glob = token->key.glob;
915bf0f6 322 const char *value = str(rules, token->key.value_off);
912541b0
KS
323 const char *attr = &rules->buf[token->key.attr_off];
324
325 switch (type) {
326 case TK_RULE:
327 {
328 const char *tks_ptr = (char *)rules->tokens;
329 const char *tk_ptr = (char *)token;
330 unsigned int idx = (tk_ptr - tks_ptr) / sizeof(struct token);
331
9f6445e3 332 log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'",
baa30fbc
KS
333 &rules->buf[token->rule.filename_off], token->rule.filename_line,
334 idx, token->rule.token_count,
335 &rules->buf[token->rule.label_off]);
912541b0
KS
336 break;
337 }
338 case TK_M_ACTION:
339 case TK_M_DEVPATH:
340 case TK_M_KERNEL:
341 case TK_M_SUBSYSTEM:
342 case TK_M_DRIVER:
343 case TK_M_WAITFOR:
344 case TK_M_DEVLINK:
345 case TK_M_NAME:
346 case TK_M_KERNELS:
347 case TK_M_SUBSYSTEMS:
348 case TK_M_DRIVERS:
349 case TK_M_TAGS:
350 case TK_M_PROGRAM:
351 case TK_M_IMPORT_FILE:
352 case TK_M_IMPORT_PROG:
353 case TK_M_IMPORT_DB:
354 case TK_M_IMPORT_CMDLINE:
355 case TK_M_IMPORT_PARENT:
356 case TK_M_RESULT:
357 case TK_A_NAME:
358 case TK_A_DEVLINK:
359 case TK_A_OWNER:
360 case TK_A_GROUP:
361 case TK_A_MODE:
83cd6b75
KS
362 case TK_A_RUN_BUILTIN:
363 case TK_A_RUN_PROGRAM:
9f6445e3 364 log_debug("%s %s '%s'(%s)",
baa30fbc 365 token_str(type), operation_str(op), value, string_glob_str(glob));
912541b0
KS
366 break;
367 case TK_M_IMPORT_BUILTIN:
9f6445e3 368 log_debug("%s %i '%s'", token_str(type), token->key.builtin_cmd, value);
912541b0
KS
369 break;
370 case TK_M_ATTR:
f4cf2e5b 371 case TK_M_SYSCTL:
912541b0
KS
372 case TK_M_ATTRS:
373 case TK_M_ENV:
374 case TK_A_ATTR:
f4cf2e5b 375 case TK_A_SYSCTL:
912541b0 376 case TK_A_ENV:
9f6445e3 377 log_debug("%s %s '%s' '%s'(%s)",
baa30fbc 378 token_str(type), operation_str(op), attr, value, string_glob_str(glob));
912541b0
KS
379 break;
380 case TK_M_TAG:
381 case TK_A_TAG:
9f6445e3 382 log_debug("%s %s '%s'", token_str(type), operation_str(op), value);
912541b0
KS
383 break;
384 case TK_A_STRING_ESCAPE_NONE:
385 case TK_A_STRING_ESCAPE_REPLACE:
386 case TK_A_DB_PERSIST:
9f6445e3 387 log_debug("%s", token_str(type));
912541b0
KS
388 break;
389 case TK_M_TEST:
9f6445e3 390 log_debug("%s %s '%s'(%s) %#o",
baa30fbc 391 token_str(type), operation_str(op), value, string_glob_str(glob), token->key.mode);
912541b0
KS
392 break;
393 case TK_A_INOTIFY_WATCH:
9f6445e3 394 log_debug("%s %u", token_str(type), token->key.watch);
912541b0
KS
395 break;
396 case TK_A_DEVLINK_PRIO:
9f6445e3 397 log_debug("%s %u", token_str(type), token->key.devlink_prio);
912541b0
KS
398 break;
399 case TK_A_OWNER_ID:
9f6445e3 400 log_debug("%s %s %u", token_str(type), operation_str(op), token->key.uid);
912541b0
KS
401 break;
402 case TK_A_GROUP_ID:
9f6445e3 403 log_debug("%s %s %u", token_str(type), operation_str(op), token->key.gid);
912541b0
KS
404 break;
405 case TK_A_MODE_ID:
9f6445e3 406 log_debug("%s %s %#o", token_str(type), operation_str(op), token->key.mode);
912541b0
KS
407 break;
408 case TK_A_STATIC_NODE:
9f6445e3 409 log_debug("%s '%s'", token_str(type), value);
912541b0 410 break;
c26547d6 411 case TK_A_SECLABEL:
9f6445e3 412 log_debug("%s %s '%s' '%s'", token_str(type), operation_str(op), attr, value);
c26547d6 413 break;
912541b0 414 case TK_A_GOTO:
9f6445e3 415 log_debug("%s '%s' %u", token_str(type), value, token->key.rule_goto);
912541b0
KS
416 break;
417 case TK_END:
9f6445e3 418 log_debug("* %s", token_str(type));
912541b0
KS
419 break;
420 case TK_M_PARENTS_MIN:
421 case TK_M_PARENTS_MAX:
422 case TK_M_MAX:
423 case TK_UNSET:
9f6445e3 424 log_debug("unknown type %u", type);
912541b0
KS
425 break;
426 }
4052400f 427}
154a7b84 428
9ec6e95b 429static void dump_rules(struct udev_rules *rules) {
912541b0
KS
430 unsigned int i;
431
9f6445e3 432 log_debug("dumping %u (%zu bytes) tokens, %u (%zu bytes) strings",
baa30fbc
KS
433 rules->token_cur,
434 rules->token_cur * sizeof(struct token),
435 rules->buf_count,
436 rules->buf_cur);
f168c273 437 for (i = 0; i < rules->token_cur; i++)
912541b0 438 dump_token(rules, &rules->tokens[i]);
4052400f
KS
439}
440#else
4052400f
KS
441static inline void dump_token(struct udev_rules *rules, struct token *token) {}
442static inline void dump_rules(struct udev_rules *rules) {}
baa30fbc 443#endif /* DEBUG */
c7521974 444
9ec6e95b 445static int add_token(struct udev_rules *rules, struct token *token) {
912541b0
KS
446 /* grow buffer if needed */
447 if (rules->token_cur+1 >= rules->token_max) {
448 struct token *tokens;
449 unsigned int add;
450
451 /* double the buffer size */
452 add = rules->token_max;
453 if (add < 8)
454 add = 8;
455
456 tokens = realloc(rules->tokens, (rules->token_max + add ) * sizeof(struct token));
457 if (tokens == NULL)
458 return -1;
912541b0
KS
459 rules->tokens = tokens;
460 rules->token_max += add;
461 }
462 memcpy(&rules->tokens[rules->token_cur], token, sizeof(struct token));
463 rules->token_cur++;
464 return 0;
c7521974 465}
a27cd06c 466
9ec6e95b 467static uid_t add_uid(struct udev_rules *rules, const char *owner) {
912541b0 468 unsigned int i;
23bf8dd7 469 uid_t uid = 0;
912541b0 470 unsigned int off;
23bf8dd7 471 int r;
912541b0
KS
472
473 /* lookup, if we know it already */
474 for (i = 0; i < rules->uids_cur; i++) {
475 off = rules->uids[i].name_off;
915bf0f6 476 if (streq(rules_str(rules, off), owner)) {
912541b0 477 uid = rules->uids[i].uid;
912541b0
KS
478 return uid;
479 }
480 }
23bf8dd7
TG
481 r = get_user_creds(&owner, &uid, NULL, NULL, NULL);
482 if (r < 0) {
483 if (r == -ENOENT || r == -ESRCH)
ff49bc32 484 log_error("specified user '%s' unknown", owner);
23bf8dd7 485 else
279d3c9c 486 log_error_errno(r, "error resolving user '%s': %m", owner);
23bf8dd7 487 }
912541b0
KS
488
489 /* grow buffer if needed */
490 if (rules->uids_cur+1 >= rules->uids_max) {
491 struct uid_gid *uids;
492 unsigned int add;
493
494 /* double the buffer size */
495 add = rules->uids_max;
496 if (add < 1)
497 add = 8;
498
499 uids = realloc(rules->uids, (rules->uids_max + add ) * sizeof(struct uid_gid));
500 if (uids == NULL)
501 return uid;
912541b0
KS
502 rules->uids = uids;
503 rules->uids_max += add;
504 }
505 rules->uids[rules->uids_cur].uid = uid;
915bf0f6 506 off = rules_add_string(rules, owner);
912541b0
KS
507 if (off <= 0)
508 return uid;
509 rules->uids[rules->uids_cur].name_off = off;
510 rules->uids_cur++;
511 return uid;
154a7b84
KS
512}
513
9ec6e95b 514static gid_t add_gid(struct udev_rules *rules, const char *group) {
912541b0 515 unsigned int i;
23bf8dd7 516 gid_t gid = 0;
912541b0 517 unsigned int off;
23bf8dd7 518 int r;
912541b0
KS
519
520 /* lookup, if we know it already */
521 for (i = 0; i < rules->gids_cur; i++) {
522 off = rules->gids[i].name_off;
915bf0f6 523 if (streq(rules_str(rules, off), group)) {
912541b0 524 gid = rules->gids[i].gid;
912541b0
KS
525 return gid;
526 }
527 }
23bf8dd7
TG
528 r = get_group_creds(&group, &gid);
529 if (r < 0) {
530 if (r == -ENOENT || r == -ESRCH)
ff49bc32 531 log_error("specified group '%s' unknown", group);
23bf8dd7 532 else
279d3c9c 533 log_error_errno(r, "error resolving group '%s': %m", group);
23bf8dd7 534 }
912541b0
KS
535
536 /* grow buffer if needed */
537 if (rules->gids_cur+1 >= rules->gids_max) {
538 struct uid_gid *gids;
539 unsigned int add;
540
541 /* double the buffer size */
542 add = rules->gids_max;
543 if (add < 1)
544 add = 8;
545
546 gids = realloc(rules->gids, (rules->gids_max + add ) * sizeof(struct uid_gid));
547 if (gids == NULL)
548 return gid;
912541b0
KS
549 rules->gids = gids;
550 rules->gids_max += add;
551 }
552 rules->gids[rules->gids_cur].gid = gid;
915bf0f6 553 off = rules_add_string(rules, group);
912541b0
KS
554 if (off <= 0)
555 return gid;
556 rules->gids[rules->gids_cur].name_off = off;
557 rules->gids_cur++;
558 return gid;
154a7b84
KS
559}
560
9ec6e95b 561static int import_property_from_string(struct udev_device *dev, char *line) {
912541b0
KS
562 char *key;
563 char *val;
564 size_t len;
565
566 /* find key */
567 key = line;
568 while (isspace(key[0]))
569 key++;
570
571 /* comment or empty line */
572 if (key[0] == '#' || key[0] == '\0')
573 return -1;
574
575 /* split key/value */
576 val = strchr(key, '=');
577 if (val == NULL)
578 return -1;
579 val[0] = '\0';
580 val++;
581
582 /* find value */
583 while (isspace(val[0]))
584 val++;
585
586 /* terminate key */
587 len = strlen(key);
588 if (len == 0)
589 return -1;
590 while (isspace(key[len-1]))
591 len--;
592 key[len] = '\0';
593
594 /* terminate value */
595 len = strlen(val);
596 if (len == 0)
597 return -1;
598 while (isspace(val[len-1]))
599 len--;
600 val[len] = '\0';
601
602 if (len == 0)
603 return -1;
604
605 /* unquote */
606 if (val[0] == '"' || val[0] == '\'') {
607 if (val[len-1] != val[0]) {
9f6445e3 608 log_debug("inconsistent quoting: '%s', skip", line);
912541b0
KS
609 return -1;
610 }
611 val[len-1] = '\0';
612 val++;
613 }
614
df546eb5 615 udev_device_add_property(dev, key, val);
0aa5b4d1 616
912541b0 617 return 0;
be4bedd1
KS
618}
619
9ec6e95b 620static int import_file_into_properties(struct udev_device *dev, const char *filename) {
912541b0
KS
621 FILE *f;
622 char line[UTIL_LINE_SIZE];
623
47ef94ac 624 f = fopen(filename, "re");
912541b0
KS
625 if (f == NULL)
626 return -1;
627 while (fgets(line, sizeof(line), f) != NULL)
628 import_property_from_string(dev, line);
629 fclose(f);
630 return 0;
bd0ed2ff
KS
631}
632
dd5eddd2
KS
633static int import_program_into_properties(struct udev_event *event,
634 usec_t timeout_usec,
67117413 635 usec_t timeout_warn_usec,
8314de1d 636 const char *program) {
912541b0
KS
637 char result[UTIL_LINE_SIZE];
638 char *line;
639 int err;
640
bbf35206 641 err = udev_event_spawn(event, timeout_usec, timeout_warn_usec, true, program, result, sizeof(result));
912541b0
KS
642 if (err < 0)
643 return err;
644
645 line = result;
646 while (line != NULL) {
647 char *pos;
648
649 pos = strchr(line, '\n');
650 if (pos != NULL) {
651 pos[0] = '\0';
652 pos = &pos[1];
653 }
bbf35206 654 import_property_from_string(event->dev, line);
912541b0
KS
655 line = pos;
656 }
657 return 0;
319c6700
KS
658}
659
9ec6e95b 660static int import_parent_into_properties(struct udev_device *dev, const char *filter) {
912541b0
KS
661 struct udev_device *dev_parent;
662 struct udev_list_entry *list_entry;
663
3b64e4d4
TG
664 assert(dev);
665 assert(filter);
666
912541b0
KS
667 dev_parent = udev_device_get_parent(dev);
668 if (dev_parent == NULL)
669 return -1;
670
912541b0
KS
671 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev_parent)) {
672 const char *key = udev_list_entry_get_name(list_entry);
673 const char *val = udev_list_entry_get_value(list_entry);
674
675 if (fnmatch(filter, key, 0) == 0) {
df546eb5 676 udev_device_add_property(dev, key, val);
912541b0
KS
677 }
678 }
679 return 0;
e2ecb34f
KS
680}
681
9ec6e95b 682static int attr_subst_subdir(char *attr, size_t len) {
912541b0
KS
683 bool found = false;
684
685 if (strstr(attr, "/*/")) {
686 char *pos;
687 char dirname[UTIL_PATH_SIZE];
688 const char *tail;
689 DIR *dir;
690
d5a89d7d 691 strscpy(dirname, sizeof(dirname), attr);
912541b0
KS
692 pos = strstr(dirname, "/*/");
693 if (pos == NULL)
694 return -1;
695 pos[0] = '\0';
696 tail = &pos[2];
697 dir = opendir(dirname);
698 if (dir != NULL) {
699 struct dirent *dent;
700
701 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
702 struct stat stats;
703
704 if (dent->d_name[0] == '.')
705 continue;
d5a89d7d 706 strscpyl(attr, len, dirname, "/", dent->d_name, tail, NULL);
912541b0
KS
707 if (stat(attr, &stats) == 0) {
708 found = true;
709 break;
710 }
711 }
712 closedir(dir);
713 }
714 }
715
716 return found;
0ea5e96e
KS
717}
718
9ec6e95b 719static int get_key(struct udev *udev, char **line, char **key, enum operation_type *op, char **value) {
912541b0
KS
720 char *linepos;
721 char *temp;
722
723 linepos = *line;
724 if (linepos == NULL || linepos[0] == '\0')
725 return -1;
726
727 /* skip whitespace */
728 while (isspace(linepos[0]) || linepos[0] == ',')
729 linepos++;
730
731 /* get the key */
732 if (linepos[0] == '\0')
733 return -1;
734 *key = linepos;
735
736 for (;;) {
737 linepos++;
738 if (linepos[0] == '\0')
739 return -1;
740 if (isspace(linepos[0]))
741 break;
742 if (linepos[0] == '=')
743 break;
8e3ba377 744 if ((linepos[0] == '+') || (linepos[0] == '-') || (linepos[0] == '!') || (linepos[0] == ':'))
912541b0
KS
745 if (linepos[1] == '=')
746 break;
747 }
748
749 /* remember end of key */
750 temp = linepos;
751
752 /* skip whitespace after key */
753 while (isspace(linepos[0]))
754 linepos++;
755 if (linepos[0] == '\0')
756 return -1;
757
758 /* get operation type */
759 if (linepos[0] == '=' && linepos[1] == '=') {
760 *op = OP_MATCH;
761 linepos += 2;
762 } else if (linepos[0] == '!' && linepos[1] == '=') {
763 *op = OP_NOMATCH;
764 linepos += 2;
765 } else if (linepos[0] == '+' && linepos[1] == '=') {
766 *op = OP_ADD;
767 linepos += 2;
8e3ba377
DH
768 } else if (linepos[0] == '-' && linepos[1] == '=') {
769 *op = OP_REMOVE;
770 linepos += 2;
912541b0
KS
771 } else if (linepos[0] == '=') {
772 *op = OP_ASSIGN;
773 linepos++;
774 } else if (linepos[0] == ':' && linepos[1] == '=') {
775 *op = OP_ASSIGN_FINAL;
776 linepos += 2;
777 } else
778 return -1;
779
780 /* terminate key */
781 temp[0] = '\0';
782
783 /* skip whitespace after operator */
784 while (isspace(linepos[0]))
785 linepos++;
786 if (linepos[0] == '\0')
787 return -1;
788
789 /* get the value */
790 if (linepos[0] == '"')
791 linepos++;
792 else
793 return -1;
794 *value = linepos;
795
796 /* terminate */
797 temp = strchr(linepos, '"');
798 if (!temp)
799 return -1;
800 temp[0] = '\0';
801 temp++;
912541b0
KS
802
803 /* move line to next key */
804 *line = temp;
805 return 0;
6880b25d 806}
95776dc6 807
6880b25d 808/* extract possible KEY{attr} */
9ec6e95b 809static const char *get_key_attribute(struct udev *udev, char *str) {
912541b0
KS
810 char *pos;
811 char *attr;
812
813 attr = strchr(str, '{');
814 if (attr != NULL) {
815 attr++;
816 pos = strchr(attr, '}');
817 if (pos == NULL) {
9f6445e3 818 log_error("missing closing brace for format");
912541b0
KS
819 return NULL;
820 }
821 pos[0] = '\0';
912541b0
KS
822 return attr;
823 }
824 return NULL;
6880b25d 825}
95776dc6 826
0ef254d5 827static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type,
912541b0 828 enum operation_type op,
9ec6e95b 829 const char *value, const void *data) {
912541b0
KS
830 struct token *token = &rule_tmp->token[rule_tmp->token_cur];
831 const char *attr = NULL;
832
29804cc1 833 memzero(token, sizeof(struct token));
912541b0
KS
834
835 switch (type) {
836 case TK_M_ACTION:
837 case TK_M_DEVPATH:
838 case TK_M_KERNEL:
839 case TK_M_SUBSYSTEM:
840 case TK_M_DRIVER:
841 case TK_M_WAITFOR:
842 case TK_M_DEVLINK:
843 case TK_M_NAME:
844 case TK_M_KERNELS:
845 case TK_M_SUBSYSTEMS:
846 case TK_M_DRIVERS:
847 case TK_M_TAGS:
848 case TK_M_PROGRAM:
849 case TK_M_IMPORT_FILE:
850 case TK_M_IMPORT_PROG:
851 case TK_M_IMPORT_DB:
852 case TK_M_IMPORT_CMDLINE:
853 case TK_M_IMPORT_PARENT:
854 case TK_M_RESULT:
855 case TK_A_OWNER:
856 case TK_A_GROUP:
857 case TK_A_MODE:
8a173387 858 case TK_A_DEVLINK:
912541b0
KS
859 case TK_A_NAME:
860 case TK_A_GOTO:
861 case TK_M_TAG:
862 case TK_A_TAG:
d6f116a7 863 case TK_A_STATIC_NODE:
915bf0f6 864 token->key.value_off = rules_add_string(rule_tmp->rules, value);
912541b0
KS
865 break;
866 case TK_M_IMPORT_BUILTIN:
915bf0f6 867 token->key.value_off = rules_add_string(rule_tmp->rules, value);
912541b0
KS
868 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
869 break;
870 case TK_M_ENV:
871 case TK_M_ATTR:
f4cf2e5b 872 case TK_M_SYSCTL:
912541b0
KS
873 case TK_M_ATTRS:
874 case TK_A_ATTR:
f4cf2e5b 875 case TK_A_SYSCTL:
912541b0 876 case TK_A_ENV:
c26547d6 877 case TK_A_SECLABEL:
912541b0 878 attr = data;
915bf0f6
KS
879 token->key.value_off = rules_add_string(rule_tmp->rules, value);
880 token->key.attr_off = rules_add_string(rule_tmp->rules, attr);
912541b0 881 break;
912541b0 882 case TK_M_TEST:
915bf0f6 883 token->key.value_off = rules_add_string(rule_tmp->rules, value);
912541b0
KS
884 if (data != NULL)
885 token->key.mode = *(mode_t *)data;
886 break;
887 case TK_A_STRING_ESCAPE_NONE:
888 case TK_A_STRING_ESCAPE_REPLACE:
889 case TK_A_DB_PERSIST:
890 break;
83cd6b75 891 case TK_A_RUN_BUILTIN:
83cd6b75 892 case TK_A_RUN_PROGRAM:
4590cfe4 893 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
915bf0f6 894 token->key.value_off = rules_add_string(rule_tmp->rules, value);
912541b0
KS
895 break;
896 case TK_A_INOTIFY_WATCH:
897 case TK_A_DEVLINK_PRIO:
898 token->key.devlink_prio = *(int *)data;
899 break;
900 case TK_A_OWNER_ID:
901 token->key.uid = *(uid_t *)data;
902 break;
903 case TK_A_GROUP_ID:
904 token->key.gid = *(gid_t *)data;
905 break;
906 case TK_A_MODE_ID:
907 token->key.mode = *(mode_t *)data;
908 break;
912541b0
KS
909 case TK_RULE:
910 case TK_M_PARENTS_MIN:
911 case TK_M_PARENTS_MAX:
912 case TK_M_MAX:
913 case TK_END:
914 case TK_UNSET:
9f6445e3 915 log_error("wrong type %u", type);
912541b0
KS
916 return -1;
917 }
918
919 if (value != NULL && type < TK_M_MAX) {
920 /* check if we need to split or call fnmatch() while matching rules */
921 enum string_glob_type glob;
922 int has_split;
923 int has_glob;
924
925 has_split = (strchr(value, '|') != NULL);
e3e0314b 926 has_glob = string_is_glob(value);
912541b0
KS
927 if (has_split && has_glob) {
928 glob = GL_SPLIT_GLOB;
929 } else if (has_split) {
930 glob = GL_SPLIT;
931 } else if (has_glob) {
33502ffe 932 if (streq(value, "?*"))
912541b0
KS
933 glob = GL_SOMETHING;
934 else
935 glob = GL_GLOB;
936 } else {
937 glob = GL_PLAIN;
938 }
939 token->key.glob = glob;
940 }
941
942 if (value != NULL && type > TK_M_MAX) {
943 /* check if assigned value has substitution chars */
944 if (value[0] == '[')
945 token->key.subst = SB_SUBSYS;
946 else if (strchr(value, '%') != NULL || strchr(value, '$') != NULL)
947 token->key.subst = SB_FORMAT;
948 else
949 token->key.subst = SB_NONE;
950 }
951
952 if (attr != NULL) {
d6f116a7 953 /* check if property/attribute name has substitution chars */
912541b0
KS
954 if (attr[0] == '[')
955 token->key.attrsubst = SB_SUBSYS;
956 else if (strchr(attr, '%') != NULL || strchr(attr, '$') != NULL)
957 token->key.attrsubst = SB_FORMAT;
958 else
959 token->key.attrsubst = SB_NONE;
960 }
961
962 token->key.type = type;
963 token->key.op = op;
964 rule_tmp->token_cur++;
8fef0ff2 965 if (rule_tmp->token_cur >= ELEMENTSOF(rule_tmp->token)) {
9f6445e3 966 log_error("temporary rule array too small");
912541b0
KS
967 return -1;
968 }
969 return 0;
6880b25d 970}
e57e7bc1 971
9ec6e95b 972static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp) {
912541b0
KS
973 unsigned int i;
974 unsigned int start = 0;
975 unsigned int end = rule_tmp->token_cur;
976
977 for (i = 0; i < rule_tmp->token_cur; i++) {
978 enum token_type next_val = TK_UNSET;
979 unsigned int next_idx = 0;
980 unsigned int j;
981
982 /* find smallest value */
983 for (j = start; j < end; j++) {
984 if (rule_tmp->token[j].type == TK_UNSET)
985 continue;
986 if (next_val == TK_UNSET || rule_tmp->token[j].type < next_val) {
987 next_val = rule_tmp->token[j].type;
988 next_idx = j;
989 }
990 }
991
992 /* add token and mark done */
993 if (add_token(rules, &rule_tmp->token[next_idx]) != 0)
994 return -1;
995 rule_tmp->token[next_idx].type = TK_UNSET;
996
997 /* shrink range */
998 if (next_idx == start)
999 start++;
1000 if (next_idx+1 == end)
1001 end--;
1002 }
1003 return 0;
724257d9
GKH
1004}
1005
6880b25d 1006static int add_rule(struct udev_rules *rules, char *line,
9ec6e95b 1007 const char *filename, unsigned int filename_off, unsigned int lineno) {
912541b0 1008 char *linepos;
04a9d3a0 1009 const char *attr;
84198c18
TG
1010 struct rule_tmp rule_tmp = {
1011 .rules = rules,
1012 .rule.type = TK_RULE,
1013 };
912541b0 1014
775f8b3c
KS
1015 /* the offset in the rule is limited to unsigned short */
1016 if (filename_off < USHRT_MAX)
1017 rule_tmp.rule.rule.filename_off = filename_off;
912541b0
KS
1018 rule_tmp.rule.rule.filename_line = lineno;
1019
1020 linepos = line;
1021 for (;;) {
1022 char *key;
1023 char *value;
1024 enum operation_type op;
1025
e736cf35 1026 if (get_key(rules->udev, &linepos, &key, &op, &value) != 0) {
3cf0f8f7
DR
1027 /* Avoid erroring on trailing whitespace. This is probably rare
1028 * so save the work for the error case instead of always trying
1029 * to strip the trailing whitespace with strstrip(). */
1030 while (isblank(*linepos))
1031 linepos++;
1032
e736cf35
DR
1033 /* If we aren't at the end of the line, this is a parsing error.
1034 * Make a best effort to describe where the problem is. */
2783fe06 1035 if (!strchr(NEWLINE, *linepos)) {
6501b52d 1036 char buf[2] = {*linepos};
1291bc89
ZJS
1037 _cleanup_free_ char *tmp;
1038
1039 tmp = cescape(buf);
ff49bc32 1040 log_error("invalid key/value pair in file %s on line %u, starting at character %tu ('%s')",
1291bc89 1041 filename, lineno, linepos - line + 1, tmp);
6501b52d 1042 if (*linepos == '#')
75716dd8 1043 log_error("hint: comments can only start at beginning of line");
1291bc89 1044 }
912541b0 1045 break;
e736cf35 1046 }
912541b0 1047
33502ffe 1048 if (streq(key, "ACTION")) {
912541b0 1049 if (op > OP_MATCH_MAX) {
9f6445e3 1050 log_error("invalid ACTION operation");
912541b0
KS
1051 goto invalid;
1052 }
1053 rule_add_key(&rule_tmp, TK_M_ACTION, op, value, NULL);
1054 continue;
1055 }
1056
33502ffe 1057 if (streq(key, "DEVPATH")) {
912541b0 1058 if (op > OP_MATCH_MAX) {
9f6445e3 1059 log_error("invalid DEVPATH operation");
912541b0
KS
1060 goto invalid;
1061 }
1062 rule_add_key(&rule_tmp, TK_M_DEVPATH, op, value, NULL);
1063 continue;
1064 }
1065
33502ffe 1066 if (streq(key, "KERNEL")) {
912541b0 1067 if (op > OP_MATCH_MAX) {
9f6445e3 1068 log_error("invalid KERNEL operation");
912541b0
KS
1069 goto invalid;
1070 }
1071 rule_add_key(&rule_tmp, TK_M_KERNEL, op, value, NULL);
1072 continue;
1073 }
1074
33502ffe 1075 if (streq(key, "SUBSYSTEM")) {
912541b0 1076 if (op > OP_MATCH_MAX) {
9f6445e3 1077 log_error("invalid SUBSYSTEM operation");
912541b0
KS
1078 goto invalid;
1079 }
1080 /* bus, class, subsystem events should all be the same */
33502ffe
KS
1081 if (streq(value, "subsystem") ||
1082 streq(value, "bus") ||
1083 streq(value, "class")) {
1084 if (streq(value, "bus") || streq(value, "class"))
9f6445e3 1085 log_error("'%s' must be specified as 'subsystem' "
912541b0
KS
1086 "please fix it in %s:%u", value, filename, lineno);
1087 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, "subsystem|class|bus", NULL);
1088 } else
1089 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, value, NULL);
1090 continue;
1091 }
1092
33502ffe 1093 if (streq(key, "DRIVER")) {
912541b0 1094 if (op > OP_MATCH_MAX) {
9f6445e3 1095 log_error("invalid DRIVER operation");
912541b0
KS
1096 goto invalid;
1097 }
1098 rule_add_key(&rule_tmp, TK_M_DRIVER, op, value, NULL);
1099 continue;
1100 }
1101
33502ffe 1102 if (startswith(key, "ATTR{")) {
f8294e41 1103 attr = get_key_attribute(rules->udev, key + strlen("ATTR"));
912541b0 1104 if (attr == NULL) {
9f6445e3 1105 log_error("error parsing ATTR attribute");
912541b0
KS
1106 goto invalid;
1107 }
8e3ba377
DH
1108 if (op == OP_REMOVE) {
1109 log_error("invalid ATTR operation");
1110 goto invalid;
1111 }
f4cf2e5b 1112 if (op < OP_MATCH_MAX)
912541b0 1113 rule_add_key(&rule_tmp, TK_M_ATTR, op, value, attr);
f4cf2e5b 1114 else
912541b0 1115 rule_add_key(&rule_tmp, TK_A_ATTR, op, value, attr);
f4cf2e5b
KS
1116 continue;
1117 }
1118
1119 if (startswith(key, "SYSCTL{")) {
1120 attr = get_key_attribute(rules->udev, key + strlen("SYSCTL"));
1121 if (attr == NULL) {
1122 log_error("error parsing SYSCTL attribute");
1123 goto invalid;
1124 }
1125 if (op == OP_REMOVE) {
1126 log_error("invalid SYSCTL operation");
1127 goto invalid;
912541b0 1128 }
f4cf2e5b
KS
1129 if (op < OP_MATCH_MAX)
1130 rule_add_key(&rule_tmp, TK_M_SYSCTL, op, value, attr);
1131 else
1132 rule_add_key(&rule_tmp, TK_A_SYSCTL, op, value, attr);
912541b0
KS
1133 continue;
1134 }
1135
c26547d6 1136 if (startswith(key, "SECLABEL{")) {
f8294e41 1137 attr = get_key_attribute(rules->udev, key + strlen("SECLABEL"));
c26547d6 1138 if (!attr) {
9f6445e3 1139 log_error("error parsing SECLABEL attribute");
c26547d6
KS
1140 goto invalid;
1141 }
8e3ba377
DH
1142 if (op == OP_REMOVE) {
1143 log_error("invalid SECLABEL operation");
1144 goto invalid;
1145 }
c26547d6
KS
1146
1147 rule_add_key(&rule_tmp, TK_A_SECLABEL, op, value, attr);
1148 continue;
1149 }
1150
33502ffe 1151 if (streq(key, "KERNELS")) {
912541b0 1152 if (op > OP_MATCH_MAX) {
9f6445e3 1153 log_error("invalid KERNELS operation");
912541b0
KS
1154 goto invalid;
1155 }
1156 rule_add_key(&rule_tmp, TK_M_KERNELS, op, value, NULL);
1157 continue;
1158 }
1159
33502ffe 1160 if (streq(key, "SUBSYSTEMS")) {
912541b0 1161 if (op > OP_MATCH_MAX) {
9f6445e3 1162 log_error("invalid SUBSYSTEMS operation");
912541b0
KS
1163 goto invalid;
1164 }
1165 rule_add_key(&rule_tmp, TK_M_SUBSYSTEMS, op, value, NULL);
1166 continue;
1167 }
1168
33502ffe 1169 if (streq(key, "DRIVERS")) {
912541b0 1170 if (op > OP_MATCH_MAX) {
9f6445e3 1171 log_error("invalid DRIVERS operation");
912541b0
KS
1172 goto invalid;
1173 }
1174 rule_add_key(&rule_tmp, TK_M_DRIVERS, op, value, NULL);
1175 continue;
1176 }
1177
33502ffe 1178 if (startswith(key, "ATTRS{")) {
912541b0 1179 if (op > OP_MATCH_MAX) {
9f6445e3 1180 log_error("invalid ATTRS operation");
912541b0
KS
1181 goto invalid;
1182 }
f8294e41 1183 attr = get_key_attribute(rules->udev, key + strlen("ATTRS"));
912541b0 1184 if (attr == NULL) {
9f6445e3 1185 log_error("error parsing ATTRS attribute");
912541b0
KS
1186 goto invalid;
1187 }
33502ffe 1188 if (startswith(attr, "device/"))
baa30fbc 1189 log_error("the 'device' link may not be available in a future kernel, "
912541b0
KS
1190 "please fix it in %s:%u", filename, lineno);
1191 else if (strstr(attr, "../") != NULL)
baa30fbc 1192 log_error("do not reference parent sysfs directories directly, "
912541b0
KS
1193 "it may break with a future kernel, please fix it in %s:%u", filename, lineno);
1194 rule_add_key(&rule_tmp, TK_M_ATTRS, op, value, attr);
1195 continue;
1196 }
1197
33502ffe 1198 if (streq(key, "TAGS")) {
912541b0 1199 if (op > OP_MATCH_MAX) {
9f6445e3 1200 log_error("invalid TAGS operation");
912541b0
KS
1201 goto invalid;
1202 }
1203 rule_add_key(&rule_tmp, TK_M_TAGS, op, value, NULL);
1204 continue;
1205 }
1206
33502ffe 1207 if (startswith(key, "ENV{")) {
f8294e41 1208 attr = get_key_attribute(rules->udev, key + strlen("ENV"));
912541b0 1209 if (attr == NULL) {
9f6445e3 1210 log_error("error parsing ENV attribute");
912541b0
KS
1211 goto invalid;
1212 }
8e3ba377
DH
1213 if (op == OP_REMOVE) {
1214 log_error("invalid ENV operation");
1215 goto invalid;
1216 }
912541b0
KS
1217 if (op < OP_MATCH_MAX) {
1218 if (rule_add_key(&rule_tmp, TK_M_ENV, op, value, attr) != 0)
1219 goto invalid;
1220 } else {
1221 static const char *blacklist[] = {
1222 "ACTION",
1223 "SUBSYSTEM",
1224 "DEVTYPE",
1225 "MAJOR",
1226 "MINOR",
1227 "DRIVER",
1228 "IFINDEX",
1229 "DEVNAME",
1230 "DEVLINKS",
1231 "DEVPATH",
1232 "TAGS",
1233 };
1234 unsigned int i;
1235
395ea978
KS
1236 for (i = 0; i < ELEMENTSOF(blacklist); i++) {
1237 if (!streq(attr, blacklist[i]))
912541b0 1238 continue;
9f6445e3 1239 log_error("invalid ENV attribute, '%s' can not be set %s:%u", attr, filename, lineno);
395ea978
KS
1240 goto invalid;
1241 }
912541b0
KS
1242 if (rule_add_key(&rule_tmp, TK_A_ENV, op, value, attr) != 0)
1243 goto invalid;
1244 }
1245 continue;
1246 }
1247
33502ffe 1248 if (streq(key, "TAG")) {
912541b0
KS
1249 if (op < OP_MATCH_MAX)
1250 rule_add_key(&rule_tmp, TK_M_TAG, op, value, NULL);
1251 else
1252 rule_add_key(&rule_tmp, TK_A_TAG, op, value, NULL);
1253 continue;
1254 }
1255
33502ffe 1256 if (streq(key, "PROGRAM")) {
8e3ba377
DH
1257 if (op == OP_REMOVE) {
1258 log_error("invalid PROGRAM operation");
1259 goto invalid;
1260 }
912541b0
KS
1261 rule_add_key(&rule_tmp, TK_M_PROGRAM, op, value, NULL);
1262 continue;
1263 }
1264
33502ffe 1265 if (streq(key, "RESULT")) {
912541b0 1266 if (op > OP_MATCH_MAX) {
9f6445e3 1267 log_error("invalid RESULT operation");
912541b0
KS
1268 goto invalid;
1269 }
1270 rule_add_key(&rule_tmp, TK_M_RESULT, op, value, NULL);
1271 continue;
1272 }
1273
33502ffe 1274 if (startswith(key, "IMPORT")) {
f8294e41 1275 attr = get_key_attribute(rules->udev, key + strlen("IMPORT"));
912541b0 1276 if (attr == NULL) {
9f6445e3 1277 log_error("IMPORT{} type missing, ignoring IMPORT %s:%u", filename, lineno);
912541b0
KS
1278 continue;
1279 }
8e3ba377
DH
1280 if (op == OP_REMOVE) {
1281 log_error("invalid IMPORT operation");
1282 goto invalid;
1283 }
33502ffe 1284 if (streq(attr, "program")) {
912541b0
KS
1285 /* find known built-in command */
1286 if (value[0] != '/') {
1287 enum udev_builtin_cmd cmd;
1288
1289 cmd = udev_builtin_lookup(value);
1290 if (cmd < UDEV_BUILTIN_MAX) {
9f6445e3 1291 log_debug("IMPORT found builtin '%s', replacing %s:%u",
baa30fbc 1292 value, filename, lineno);
912541b0
KS
1293 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1294 continue;
1295 }
1296 }
912541b0 1297 rule_add_key(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL);
33502ffe 1298 } else if (streq(attr, "builtin")) {
912541b0
KS
1299 enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1300
912541b0
KS
1301 if (cmd < UDEV_BUILTIN_MAX)
1302 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1303 else
9f6445e3 1304 log_error("IMPORT{builtin}: '%s' unknown %s:%u", value, filename, lineno);
33502ffe 1305 } else if (streq(attr, "file")) {
912541b0 1306 rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL);
33502ffe 1307 } else if (streq(attr, "db")) {
912541b0 1308 rule_add_key(&rule_tmp, TK_M_IMPORT_DB, op, value, NULL);
33502ffe 1309 } else if (streq(attr, "cmdline")) {
912541b0 1310 rule_add_key(&rule_tmp, TK_M_IMPORT_CMDLINE, op, value, NULL);
33502ffe 1311 } else if (streq(attr, "parent")) {
912541b0 1312 rule_add_key(&rule_tmp, TK_M_IMPORT_PARENT, op, value, NULL);
83cd6b75 1313 } else
9f6445e3 1314 log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u", filename, lineno);
912541b0
KS
1315 continue;
1316 }
1317
33502ffe 1318 if (startswith(key, "TEST")) {
912541b0
KS
1319 mode_t mode = 0;
1320
1321 if (op > OP_MATCH_MAX) {
9f6445e3 1322 log_error("invalid TEST operation");
912541b0
KS
1323 goto invalid;
1324 }
f8294e41 1325 attr = get_key_attribute(rules->udev, key + strlen("TEST"));
912541b0
KS
1326 if (attr != NULL) {
1327 mode = strtol(attr, NULL, 8);
1328 rule_add_key(&rule_tmp, TK_M_TEST, op, value, &mode);
1329 } else {
1330 rule_add_key(&rule_tmp, TK_M_TEST, op, value, NULL);
1331 }
1332 continue;
1333 }
1334
33502ffe 1335 if (startswith(key, "RUN")) {
f8294e41 1336 attr = get_key_attribute(rules->udev, key + strlen("RUN"));
83cd6b75
KS
1337 if (attr == NULL)
1338 attr = "program";
8e3ba377
DH
1339 if (op == OP_REMOVE) {
1340 log_error("invalid RUN operation");
1341 goto invalid;
1342 }
83cd6b75 1343
33502ffe 1344 if (streq(attr, "builtin")) {
83cd6b75
KS
1345 enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1346
1347 if (cmd < UDEV_BUILTIN_MAX)
1348 rule_add_key(&rule_tmp, TK_A_RUN_BUILTIN, op, value, &cmd);
1349 else
ec3281d3 1350 log_error("RUN{builtin}: '%s' unknown %s:%u", value, filename, lineno);
33502ffe 1351 } else if (streq(attr, "program")) {
83cd6b75
KS
1352 enum udev_builtin_cmd cmd = UDEV_BUILTIN_MAX;
1353
83cd6b75
KS
1354 rule_add_key(&rule_tmp, TK_A_RUN_PROGRAM, op, value, &cmd);
1355 } else {
9f6445e3 1356 log_error("RUN{} unknown type, ignoring RUN %s:%u", filename, lineno);
83cd6b75
KS
1357 }
1358
912541b0
KS
1359 continue;
1360 }
1361
33502ffe 1362 if (streq(key, "LABEL")) {
8e3ba377
DH
1363 if (op == OP_REMOVE) {
1364 log_error("invalid LABEL operation");
1365 goto invalid;
1366 }
915bf0f6 1367 rule_tmp.rule.rule.label_off = rules_add_string(rules, value);
912541b0
KS
1368 continue;
1369 }
1370
33502ffe 1371 if (streq(key, "GOTO")) {
8e3ba377
DH
1372 if (op == OP_REMOVE) {
1373 log_error("invalid GOTO operation");
1374 goto invalid;
1375 }
912541b0
KS
1376 rule_add_key(&rule_tmp, TK_A_GOTO, 0, value, NULL);
1377 continue;
1378 }
1379
33502ffe 1380 if (startswith(key, "NAME")) {
8e3ba377
DH
1381 if (op == OP_REMOVE) {
1382 log_error("invalid NAME operation");
1383 goto invalid;
1384 }
912541b0
KS
1385 if (op < OP_MATCH_MAX) {
1386 rule_add_key(&rule_tmp, TK_M_NAME, op, value, NULL);
1387 } else {
33502ffe 1388 if (streq(value, "%k")) {
baa30fbc 1389 log_error("NAME=\"%%k\" is ignored, because it breaks kernel supplied names, "
912541b0
KS
1390 "please remove it from %s:%u\n", filename, lineno);
1391 continue;
1392 }
1393 if (value[0] == '\0') {
baa30fbc
KS
1394 log_debug("NAME=\"\" is ignored, because udev will not delete any device nodes, "
1395 "please remove it from %s:%u\n", filename, lineno);
912541b0
KS
1396 continue;
1397 }
1398 rule_add_key(&rule_tmp, TK_A_NAME, op, value, NULL);
1399 }
1400 rule_tmp.rule.rule.can_set_name = true;
1401 continue;
1402 }
1403
8a173387 1404 if (streq(key, "SYMLINK")) {
8e3ba377
DH
1405 if (op == OP_REMOVE) {
1406 log_error("invalid SYMLINK operation");
1407 goto invalid;
1408 }
8a173387 1409 if (op < OP_MATCH_MAX)
912541b0 1410 rule_add_key(&rule_tmp, TK_M_DEVLINK, op, value, NULL);
8a173387
KS
1411 else
1412 rule_add_key(&rule_tmp, TK_A_DEVLINK, op, value, NULL);
912541b0
KS
1413 rule_tmp.rule.rule.can_set_name = true;
1414 continue;
1415 }
1416
33502ffe 1417 if (streq(key, "OWNER")) {
912541b0
KS
1418 uid_t uid;
1419 char *endptr;
1420
8e3ba377
DH
1421 if (op == OP_REMOVE) {
1422 log_error("invalid OWNER operation");
1423 goto invalid;
1424 }
1425
912541b0
KS
1426 uid = strtoul(value, &endptr, 10);
1427 if (endptr[0] == '\0') {
1428 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1429 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1430 uid = add_uid(rules, value);
1431 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1432 } else if (rules->resolve_names >= 0) {
1433 rule_add_key(&rule_tmp, TK_A_OWNER, op, value, NULL);
1434 }
1435 rule_tmp.rule.rule.can_set_name = true;
1436 continue;
1437 }
1438
33502ffe 1439 if (streq(key, "GROUP")) {
912541b0
KS
1440 gid_t gid;
1441 char *endptr;
1442
8e3ba377
DH
1443 if (op == OP_REMOVE) {
1444 log_error("invalid GROUP operation");
1445 goto invalid;
1446 }
1447
912541b0
KS
1448 gid = strtoul(value, &endptr, 10);
1449 if (endptr[0] == '\0') {
1450 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1451 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1452 gid = add_gid(rules, value);
1453 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1454 } else if (rules->resolve_names >= 0) {
1455 rule_add_key(&rule_tmp, TK_A_GROUP, op, value, NULL);
1456 }
1457 rule_tmp.rule.rule.can_set_name = true;
1458 continue;
1459 }
1460
33502ffe 1461 if (streq(key, "MODE")) {
912541b0
KS
1462 mode_t mode;
1463 char *endptr;
1464
8e3ba377
DH
1465 if (op == OP_REMOVE) {
1466 log_error("invalid MODE operation");
1467 goto invalid;
1468 }
1469
912541b0
KS
1470 mode = strtol(value, &endptr, 8);
1471 if (endptr[0] == '\0')
1472 rule_add_key(&rule_tmp, TK_A_MODE_ID, op, NULL, &mode);
1473 else
1474 rule_add_key(&rule_tmp, TK_A_MODE, op, value, NULL);
1475 rule_tmp.rule.rule.can_set_name = true;
1476 continue;
1477 }
1478
33502ffe 1479 if (streq(key, "OPTIONS")) {
912541b0
KS
1480 const char *pos;
1481
8e3ba377
DH
1482 if (op == OP_REMOVE) {
1483 log_error("invalid OPTIONS operation");
1484 goto invalid;
1485 }
1486
912541b0
KS
1487 pos = strstr(value, "link_priority=");
1488 if (pos != NULL) {
1489 int prio = atoi(&pos[strlen("link_priority=")]);
1490
1491 rule_add_key(&rule_tmp, TK_A_DEVLINK_PRIO, op, NULL, &prio);
912541b0
KS
1492 }
1493
9f20a8a3 1494 pos = strstr(value, "string_escape=");
912541b0
KS
1495 if (pos != NULL) {
1496 pos = &pos[strlen("string_escape=")];
33502ffe 1497 if (startswith(pos, "none"))
912541b0 1498 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_NONE, op, NULL, NULL);
33502ffe 1499 else if (startswith(pos, "replace"))
912541b0
KS
1500 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_REPLACE, op, NULL, NULL);
1501 }
1502
1503 pos = strstr(value, "db_persist");
1504 if (pos != NULL)
1505 rule_add_key(&rule_tmp, TK_A_DB_PERSIST, op, NULL, NULL);
1506
1507 pos = strstr(value, "nowatch");
1508 if (pos != NULL) {
1509 const int off = 0;
1510
1511 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &off);
912541b0
KS
1512 } else {
1513 pos = strstr(value, "watch");
1514 if (pos != NULL) {
1515 const int on = 1;
1516
1517 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &on);
912541b0
KS
1518 }
1519 }
1520
1521 pos = strstr(value, "static_node=");
1522 if (pos != NULL) {
1523 rule_add_key(&rule_tmp, TK_A_STATIC_NODE, op, &pos[strlen("static_node=")], NULL);
1524 rule_tmp.rule.rule.has_static_node = true;
1525 }
1526
1527 continue;
1528 }
1529
9f6445e3 1530 log_error("unknown key '%s' in %s:%u", key, filename, lineno);
912541b0
KS
1531 goto invalid;
1532 }
1533
1534 /* add rule token */
1535 rule_tmp.rule.rule.token_count = 1 + rule_tmp.token_cur;
1536 if (add_token(rules, &rule_tmp.rule) != 0)
1537 goto invalid;
1538
1539 /* add tokens to list, sorted by type */
1540 if (sort_token(rules, &rule_tmp) != 0)
1541 goto invalid;
1542
1543 return 0;
c7521974 1544invalid:
9f6445e3 1545 log_error("invalid rule '%s:%u'", filename, lineno);
912541b0 1546 return -1;
c7521974
KS
1547}
1548
9ec6e95b 1549static int parse_file(struct udev_rules *rules, const char *filename) {
6c8aaf0c 1550 _cleanup_fclose_ FILE *f = NULL;
912541b0 1551 unsigned int first_token;
775f8b3c 1552 unsigned int filename_off;
912541b0
KS
1553 char line[UTIL_LINE_SIZE];
1554 int line_nr = 0;
1555 unsigned int i;
1556
ed88bcfb
ZJS
1557 f = fopen(filename, "re");
1558 if (!f) {
1559 if (errno == ENOENT)
1560 return 0;
1561 else
1562 return -errno;
775f8b3c 1563 }
912541b0 1564
ed88bcfb
ZJS
1565 if (null_or_empty_fd(fileno(f))) {
1566 log_debug("Skipping empty file: %s", filename);
1567 return 0;
1568 } else
1569 log_debug("Reading rules file: %s", filename);
912541b0
KS
1570
1571 first_token = rules->token_cur;
915bf0f6 1572 filename_off = rules_add_string(rules, filename);
912541b0
KS
1573
1574 while (fgets(line, sizeof(line), f) != NULL) {
1575 char *key;
1576 size_t len;
1577
1578 /* skip whitespace */
1579 line_nr++;
1580 key = line;
1581 while (isspace(key[0]))
1582 key++;
1583
1584 /* comment */
1585 if (key[0] == '#')
1586 continue;
1587
1588 len = strlen(line);
1589 if (len < 3)
1590 continue;
1591
1592 /* continue reading if backslash+newline is found */
1593 while (line[len-2] == '\\') {
1594 if (fgets(&line[len-2], (sizeof(line)-len)+2, f) == NULL)
1595 break;
1596 if (strlen(&line[len-2]) < 2)
1597 break;
1598 line_nr++;
1599 len = strlen(line);
1600 }
1601
1602 if (len+1 >= sizeof(line)) {
9f6445e3 1603 log_error("line too long '%s':%u, ignored", filename, line_nr);
912541b0
KS
1604 continue;
1605 }
1606 add_rule(rules, key, filename, filename_off, line_nr);
1607 }
912541b0
KS
1608
1609 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1610 for (i = first_token+1; i < rules->token_cur; i++) {
1611 if (rules->tokens[i].type == TK_A_GOTO) {
915bf0f6 1612 char *label = rules_str(rules, rules->tokens[i].key.value_off);
912541b0
KS
1613 unsigned int j;
1614
1615 for (j = i+1; j < rules->token_cur; j++) {
1616 if (rules->tokens[j].type != TK_RULE)
1617 continue;
1618 if (rules->tokens[j].rule.label_off == 0)
1619 continue;
915bf0f6 1620 if (!streq(label, rules_str(rules, rules->tokens[j].rule.label_off)))
912541b0
KS
1621 continue;
1622 rules->tokens[i].key.rule_goto = j;
1623 break;
1624 }
1625 if (rules->tokens[i].key.rule_goto == 0)
9f6445e3 1626 log_error("GOTO '%s' has no matching label in: '%s'", label, filename);
912541b0
KS
1627 }
1628 }
1629 return 0;
c7521974
KS
1630}
1631
9ec6e95b 1632struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names) {
912541b0
KS
1633 struct udev_rules *rules;
1634 struct udev_list file_list;
912541b0 1635 struct token end_token;
775f8b3c
KS
1636 char **files, **f;
1637 int r;
912541b0 1638
955d98c9 1639 rules = new0(struct udev_rules, 1);
912541b0
KS
1640 if (rules == NULL)
1641 return NULL;
1642 rules->udev = udev;
1643 rules->resolve_names = resolve_names;
1644 udev_list_init(udev, &file_list, true);
1645
1646 /* init token array and string buffer */
1647 rules->tokens = malloc(PREALLOC_TOKEN * sizeof(struct token));
0820a4f0
VP
1648 if (rules->tokens == NULL)
1649 return udev_rules_unref(rules);
912541b0
KS
1650 rules->token_max = PREALLOC_TOKEN;
1651
915bf0f6
KS
1652 rules->strbuf = strbuf_new();
1653 if (!rules->strbuf)
0820a4f0
VP
1654 return udev_rules_unref(rules);
1655
3b8c1cb0
KS
1656 udev_rules_check_timestamp(rules);
1657
2ad8416d 1658 r = conf_files_list_strv(&files, ".rules", NULL, rules_dirs);
775f8b3c 1659 if (r < 0) {
da927ba9 1660 log_error_errno(r, "failed to enumerate rules files: %m");
0820a4f0 1661 return udev_rules_unref(rules);
912541b0
KS
1662 }
1663
775f8b3c
KS
1664 /*
1665 * The offset value in the rules strct is limited; add all
1666 * rules file names to the beginning of the string buffer.
1667 */
1668 STRV_FOREACH(f, files)
915bf0f6 1669 rules_add_string(rules, *f);
912541b0 1670
775f8b3c
KS
1671 STRV_FOREACH(f, files)
1672 parse_file(rules, *f);
1673
1674 strv_free(files);
912541b0 1675
29804cc1 1676 memzero(&end_token, sizeof(struct token));
912541b0
KS
1677 end_token.type = TK_END;
1678 add_token(rules, &end_token);
9f6445e3 1679 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings",
915bf0f6 1680 rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->strbuf->len);
912541b0 1681
915bf0f6 1682 /* cleanup temporary strbuf data */
9f6445e3 1683 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used",
915bf0f6
KS
1684 rules->strbuf->in_count, rules->strbuf->in_len,
1685 rules->strbuf->dedup_count, rules->strbuf->dedup_len, rules->strbuf->nodes_count);
1686 strbuf_complete(rules->strbuf);
912541b0
KS
1687
1688 /* cleanup uid/gid cache */
1689 free(rules->uids);
1690 rules->uids = NULL;
1691 rules->uids_cur = 0;
1692 rules->uids_max = 0;
1693 free(rules->gids);
1694 rules->gids = NULL;
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 }
1943 if (!match && (cur->key.op != OP_NOMATCH))
1944 goto nomatch;
1945 break;
1946 }
1947 case TK_M_SUBSYSTEM:
1948 if (match_key(rules, cur, udev_device_get_subsystem(event->dev)) != 0)
1949 goto nomatch;
1950 break;
1951 case TK_M_DRIVER:
1952 if (match_key(rules, cur, udev_device_get_driver(event->dev)) != 0)
1953 goto nomatch;
1954 break;
912541b0
KS
1955 case TK_M_ATTR:
1956 if (match_attr(rules, event->dev, event, cur) != 0)
1957 goto nomatch;
1958 break;
f4cf2e5b
KS
1959 case TK_M_SYSCTL: {
1960 char filename[UTIL_PATH_SIZE];
1961 _cleanup_free_ char *value = NULL;
1962 size_t len;
1963
1964 udev_event_apply_format(event, rules_str(rules, cur->key.attr_off), filename, sizeof(filename));
1965 sysctl_normalize(filename);
1966 if (sysctl_read(filename, &value) < 0)
1967 goto nomatch;
1968
1969 len = strlen(value);
1970 while (len > 0 && isspace(value[--len]))
1971 value[len] = '\0';
1972 if (match_key(rules, cur, value) != 0)
1973 goto nomatch;
1974 break;
1975 }
912541b0
KS
1976 case TK_M_KERNELS:
1977 case TK_M_SUBSYSTEMS:
1978 case TK_M_DRIVERS:
1979 case TK_M_ATTRS:
1980 case TK_M_TAGS: {
1981 struct token *next;
1982
1983 /* get whole sequence of parent matches */
1984 next = cur;
1985 while (next->type > TK_M_PARENTS_MIN && next->type < TK_M_PARENTS_MAX)
1986 next++;
1987
1988 /* loop over parents */
1989 event->dev_parent = event->dev;
1990 for (;;) {
1991 struct token *key;
1992
912541b0
KS
1993 /* loop over sequence of parent match keys */
1994 for (key = cur; key < next; key++ ) {
1995 dump_token(rules, key);
1996 switch(key->type) {
1997 case TK_M_KERNELS:
1998 if (match_key(rules, key, udev_device_get_sysname(event->dev_parent)) != 0)
1999 goto try_parent;
2000 break;
2001 case TK_M_SUBSYSTEMS:
2002 if (match_key(rules, key, udev_device_get_subsystem(event->dev_parent)) != 0)
2003 goto try_parent;
2004 break;
2005 case TK_M_DRIVERS:
2006 if (match_key(rules, key, udev_device_get_driver(event->dev_parent)) != 0)
2007 goto try_parent;
2008 break;
2009 case TK_M_ATTRS:
2010 if (match_attr(rules, event->dev_parent, event, key) != 0)
2011 goto try_parent;
2012 break;
2013 case TK_M_TAGS: {
915bf0f6 2014 bool match = udev_device_has_tag(event->dev_parent, rules_str(rules, cur->key.value_off));
912541b0
KS
2015
2016 if (match && key->key.op == OP_NOMATCH)
2017 goto try_parent;
2018 if (!match && key->key.op == OP_MATCH)
2019 goto try_parent;
2020 break;
2021 }
2022 default:
2023 goto nomatch;
2024 }
912541b0 2025 }
912541b0
KS
2026 break;
2027
2028 try_parent:
2029 event->dev_parent = udev_device_get_parent(event->dev_parent);
2030 if (event->dev_parent == NULL)
2031 goto nomatch;
2032 }
2033 /* move behind our sequence of parent match keys */
2034 cur = next;
2035 continue;
2036 }
2037 case TK_M_TEST: {
2038 char filename[UTIL_PATH_SIZE];
2039 struct stat statbuf;
2040 int match;
2041
915bf0f6 2042 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), filename, sizeof(filename));
912541b0
KS
2043 if (util_resolve_subsys_kernel(event->udev, filename, filename, sizeof(filename), 0) != 0) {
2044 if (filename[0] != '/') {
2045 char tmp[UTIL_PATH_SIZE];
2046
d5a89d7d
KS
2047 strscpy(tmp, sizeof(tmp), filename);
2048 strscpyl(filename, sizeof(filename),
912541b0
KS
2049 udev_device_get_syspath(event->dev), "/", tmp, NULL);
2050 }
2051 }
2052 attr_subst_subdir(filename, sizeof(filename));
2053
2054 match = (stat(filename, &statbuf) == 0);
baa30fbc 2055 if (match && cur->key.mode > 0)
912541b0 2056 match = ((statbuf.st_mode & cur->key.mode) > 0);
912541b0
KS
2057 if (match && cur->key.op == OP_NOMATCH)
2058 goto nomatch;
2059 if (!match && cur->key.op == OP_MATCH)
2060 goto nomatch;
2061 break;
2062 }
912541b0
KS
2063 case TK_M_PROGRAM: {
2064 char program[UTIL_PATH_SIZE];
209b031e 2065 char result[UTIL_LINE_SIZE];
912541b0
KS
2066
2067 free(event->program_result);
2068 event->program_result = NULL;
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))
2128 if (cur->key.op != OP_NOMATCH)
2129 goto nomatch;
2130 break;
2131 }
2132 /* mark as ran */
2133 event->builtin_run |= (1 << cur->key.builtin_cmd);
2134 }
2135
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)
2521 log_error("error writing SYSCTL{%s}='%s': %s", filename, value, strerror(-r));
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}