]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/udev-rules.c
Merge pull request #408 from ColdPie1/fix_resp
[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 struct udev_device *dev = event->dev;
638 char **envp;
639 char result[UTIL_LINE_SIZE];
640 char *line;
641 int err;
642
643 envp = udev_device_get_properties_envp(dev);
53318514 644 err = udev_event_spawn(event, timeout_usec, timeout_warn_usec, true, program, envp, result, sizeof(result));
912541b0
KS
645 if (err < 0)
646 return err;
647
648 line = result;
649 while (line != NULL) {
650 char *pos;
651
652 pos = strchr(line, '\n');
653 if (pos != NULL) {
654 pos[0] = '\0';
655 pos = &pos[1];
656 }
657 import_property_from_string(dev, line);
658 line = pos;
659 }
660 return 0;
319c6700
KS
661}
662
9ec6e95b 663static int import_parent_into_properties(struct udev_device *dev, const char *filter) {
912541b0
KS
664 struct udev_device *dev_parent;
665 struct udev_list_entry *list_entry;
666
3b64e4d4
TG
667 assert(dev);
668 assert(filter);
669
912541b0
KS
670 dev_parent = udev_device_get_parent(dev);
671 if (dev_parent == NULL)
672 return -1;
673
912541b0
KS
674 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev_parent)) {
675 const char *key = udev_list_entry_get_name(list_entry);
676 const char *val = udev_list_entry_get_value(list_entry);
677
678 if (fnmatch(filter, key, 0) == 0) {
df546eb5 679 udev_device_add_property(dev, key, val);
912541b0
KS
680 }
681 }
682 return 0;
e2ecb34f
KS
683}
684
912541b0 685#define WAIT_LOOP_PER_SECOND 50
9ec6e95b 686static int wait_for_file(struct udev_device *dev, const char *file, int timeout) {
912541b0
KS
687 char filepath[UTIL_PATH_SIZE];
688 char devicepath[UTIL_PATH_SIZE];
689 struct stat stats;
690 int loop = timeout * WAIT_LOOP_PER_SECOND;
691
692 /* a relative path is a device attribute */
693 devicepath[0] = '\0';
694 if (file[0] != '/') {
d5a89d7d
KS
695 strscpyl(devicepath, sizeof(devicepath), udev_device_get_syspath(dev), NULL);
696 strscpyl(filepath, sizeof(filepath), devicepath, "/", file, NULL);
912541b0
KS
697 file = filepath;
698 }
699
912541b0
KS
700 while (--loop) {
701 const struct timespec duration = { 0, 1000 * 1000 * 1000 / WAIT_LOOP_PER_SECOND };
702
703 /* lookup file */
704 if (stat(file, &stats) == 0) {
9f6445e3 705 log_debug("file '%s' appeared after %i loops", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
912541b0
KS
706 return 0;
707 }
708 /* make sure, the device did not disappear in the meantime */
709 if (devicepath[0] != '\0' && stat(devicepath, &stats) != 0) {
9f6445e3 710 log_debug("device disappeared while waiting for '%s'", file);
912541b0
KS
711 return -2;
712 }
9f6445e3 713 log_debug("wait for '%s' for %i mseconds", file, 1000 / WAIT_LOOP_PER_SECOND);
912541b0
KS
714 nanosleep(&duration, NULL);
715 }
9f6445e3 716 log_debug("waiting for '%s' failed", file);
912541b0 717 return -1;
b2fe4b9a
KS
718}
719
9ec6e95b 720static int attr_subst_subdir(char *attr, size_t len) {
912541b0
KS
721 bool found = false;
722
723 if (strstr(attr, "/*/")) {
724 char *pos;
725 char dirname[UTIL_PATH_SIZE];
726 const char *tail;
727 DIR *dir;
728
d5a89d7d 729 strscpy(dirname, sizeof(dirname), attr);
912541b0
KS
730 pos = strstr(dirname, "/*/");
731 if (pos == NULL)
732 return -1;
733 pos[0] = '\0';
734 tail = &pos[2];
735 dir = opendir(dirname);
736 if (dir != NULL) {
737 struct dirent *dent;
738
739 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
740 struct stat stats;
741
742 if (dent->d_name[0] == '.')
743 continue;
d5a89d7d 744 strscpyl(attr, len, dirname, "/", dent->d_name, tail, NULL);
912541b0
KS
745 if (stat(attr, &stats) == 0) {
746 found = true;
747 break;
748 }
749 }
750 closedir(dir);
751 }
752 }
753
754 return found;
0ea5e96e
KS
755}
756
9ec6e95b 757static int get_key(struct udev *udev, char **line, char **key, enum operation_type *op, char **value) {
912541b0
KS
758 char *linepos;
759 char *temp;
760
761 linepos = *line;
762 if (linepos == NULL || linepos[0] == '\0')
763 return -1;
764
765 /* skip whitespace */
766 while (isspace(linepos[0]) || linepos[0] == ',')
767 linepos++;
768
769 /* get the key */
770 if (linepos[0] == '\0')
771 return -1;
772 *key = linepos;
773
774 for (;;) {
775 linepos++;
776 if (linepos[0] == '\0')
777 return -1;
778 if (isspace(linepos[0]))
779 break;
780 if (linepos[0] == '=')
781 break;
8e3ba377 782 if ((linepos[0] == '+') || (linepos[0] == '-') || (linepos[0] == '!') || (linepos[0] == ':'))
912541b0
KS
783 if (linepos[1] == '=')
784 break;
785 }
786
787 /* remember end of key */
788 temp = linepos;
789
790 /* skip whitespace after key */
791 while (isspace(linepos[0]))
792 linepos++;
793 if (linepos[0] == '\0')
794 return -1;
795
796 /* get operation type */
797 if (linepos[0] == '=' && linepos[1] == '=') {
798 *op = OP_MATCH;
799 linepos += 2;
800 } else if (linepos[0] == '!' && linepos[1] == '=') {
801 *op = OP_NOMATCH;
802 linepos += 2;
803 } else if (linepos[0] == '+' && linepos[1] == '=') {
804 *op = OP_ADD;
805 linepos += 2;
8e3ba377
DH
806 } else if (linepos[0] == '-' && linepos[1] == '=') {
807 *op = OP_REMOVE;
808 linepos += 2;
912541b0
KS
809 } else if (linepos[0] == '=') {
810 *op = OP_ASSIGN;
811 linepos++;
812 } else if (linepos[0] == ':' && linepos[1] == '=') {
813 *op = OP_ASSIGN_FINAL;
814 linepos += 2;
815 } else
816 return -1;
817
818 /* terminate key */
819 temp[0] = '\0';
820
821 /* skip whitespace after operator */
822 while (isspace(linepos[0]))
823 linepos++;
824 if (linepos[0] == '\0')
825 return -1;
826
827 /* get the value */
828 if (linepos[0] == '"')
829 linepos++;
830 else
831 return -1;
832 *value = linepos;
833
834 /* terminate */
835 temp = strchr(linepos, '"');
836 if (!temp)
837 return -1;
838 temp[0] = '\0';
839 temp++;
912541b0
KS
840
841 /* move line to next key */
842 *line = temp;
843 return 0;
6880b25d 844}
95776dc6 845
6880b25d 846/* extract possible KEY{attr} */
9ec6e95b 847static const char *get_key_attribute(struct udev *udev, char *str) {
912541b0
KS
848 char *pos;
849 char *attr;
850
851 attr = strchr(str, '{');
852 if (attr != NULL) {
853 attr++;
854 pos = strchr(attr, '}');
855 if (pos == NULL) {
9f6445e3 856 log_error("missing closing brace for format");
912541b0
KS
857 return NULL;
858 }
859 pos[0] = '\0';
912541b0
KS
860 return attr;
861 }
862 return NULL;
6880b25d 863}
95776dc6 864
0ef254d5 865static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type,
912541b0 866 enum operation_type op,
9ec6e95b 867 const char *value, const void *data) {
912541b0
KS
868 struct token *token = &rule_tmp->token[rule_tmp->token_cur];
869 const char *attr = NULL;
870
29804cc1 871 memzero(token, sizeof(struct token));
912541b0
KS
872
873 switch (type) {
874 case TK_M_ACTION:
875 case TK_M_DEVPATH:
876 case TK_M_KERNEL:
877 case TK_M_SUBSYSTEM:
878 case TK_M_DRIVER:
879 case TK_M_WAITFOR:
880 case TK_M_DEVLINK:
881 case TK_M_NAME:
882 case TK_M_KERNELS:
883 case TK_M_SUBSYSTEMS:
884 case TK_M_DRIVERS:
885 case TK_M_TAGS:
886 case TK_M_PROGRAM:
887 case TK_M_IMPORT_FILE:
888 case TK_M_IMPORT_PROG:
889 case TK_M_IMPORT_DB:
890 case TK_M_IMPORT_CMDLINE:
891 case TK_M_IMPORT_PARENT:
892 case TK_M_RESULT:
893 case TK_A_OWNER:
894 case TK_A_GROUP:
895 case TK_A_MODE:
8a173387 896 case TK_A_DEVLINK:
912541b0
KS
897 case TK_A_NAME:
898 case TK_A_GOTO:
899 case TK_M_TAG:
900 case TK_A_TAG:
d6f116a7 901 case TK_A_STATIC_NODE:
915bf0f6 902 token->key.value_off = rules_add_string(rule_tmp->rules, value);
912541b0
KS
903 break;
904 case TK_M_IMPORT_BUILTIN:
915bf0f6 905 token->key.value_off = rules_add_string(rule_tmp->rules, value);
912541b0
KS
906 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
907 break;
908 case TK_M_ENV:
909 case TK_M_ATTR:
f4cf2e5b 910 case TK_M_SYSCTL:
912541b0
KS
911 case TK_M_ATTRS:
912 case TK_A_ATTR:
f4cf2e5b 913 case TK_A_SYSCTL:
912541b0 914 case TK_A_ENV:
c26547d6 915 case TK_A_SECLABEL:
912541b0 916 attr = data;
915bf0f6
KS
917 token->key.value_off = rules_add_string(rule_tmp->rules, value);
918 token->key.attr_off = rules_add_string(rule_tmp->rules, attr);
912541b0 919 break;
912541b0 920 case TK_M_TEST:
915bf0f6 921 token->key.value_off = rules_add_string(rule_tmp->rules, value);
912541b0
KS
922 if (data != NULL)
923 token->key.mode = *(mode_t *)data;
924 break;
925 case TK_A_STRING_ESCAPE_NONE:
926 case TK_A_STRING_ESCAPE_REPLACE:
927 case TK_A_DB_PERSIST:
928 break;
83cd6b75 929 case TK_A_RUN_BUILTIN:
83cd6b75 930 case TK_A_RUN_PROGRAM:
4590cfe4 931 token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
915bf0f6 932 token->key.value_off = rules_add_string(rule_tmp->rules, value);
912541b0
KS
933 break;
934 case TK_A_INOTIFY_WATCH:
935 case TK_A_DEVLINK_PRIO:
936 token->key.devlink_prio = *(int *)data;
937 break;
938 case TK_A_OWNER_ID:
939 token->key.uid = *(uid_t *)data;
940 break;
941 case TK_A_GROUP_ID:
942 token->key.gid = *(gid_t *)data;
943 break;
944 case TK_A_MODE_ID:
945 token->key.mode = *(mode_t *)data;
946 break;
912541b0
KS
947 case TK_RULE:
948 case TK_M_PARENTS_MIN:
949 case TK_M_PARENTS_MAX:
950 case TK_M_MAX:
951 case TK_END:
952 case TK_UNSET:
9f6445e3 953 log_error("wrong type %u", type);
912541b0
KS
954 return -1;
955 }
956
957 if (value != NULL && type < TK_M_MAX) {
958 /* check if we need to split or call fnmatch() while matching rules */
959 enum string_glob_type glob;
960 int has_split;
961 int has_glob;
962
963 has_split = (strchr(value, '|') != NULL);
e3e0314b 964 has_glob = string_is_glob(value);
912541b0
KS
965 if (has_split && has_glob) {
966 glob = GL_SPLIT_GLOB;
967 } else if (has_split) {
968 glob = GL_SPLIT;
969 } else if (has_glob) {
33502ffe 970 if (streq(value, "?*"))
912541b0
KS
971 glob = GL_SOMETHING;
972 else
973 glob = GL_GLOB;
974 } else {
975 glob = GL_PLAIN;
976 }
977 token->key.glob = glob;
978 }
979
980 if (value != NULL && type > TK_M_MAX) {
981 /* check if assigned value has substitution chars */
982 if (value[0] == '[')
983 token->key.subst = SB_SUBSYS;
984 else if (strchr(value, '%') != NULL || strchr(value, '$') != NULL)
985 token->key.subst = SB_FORMAT;
986 else
987 token->key.subst = SB_NONE;
988 }
989
990 if (attr != NULL) {
d6f116a7 991 /* check if property/attribute name has substitution chars */
912541b0
KS
992 if (attr[0] == '[')
993 token->key.attrsubst = SB_SUBSYS;
994 else if (strchr(attr, '%') != NULL || strchr(attr, '$') != NULL)
995 token->key.attrsubst = SB_FORMAT;
996 else
997 token->key.attrsubst = SB_NONE;
998 }
999
1000 token->key.type = type;
1001 token->key.op = op;
1002 rule_tmp->token_cur++;
8fef0ff2 1003 if (rule_tmp->token_cur >= ELEMENTSOF(rule_tmp->token)) {
9f6445e3 1004 log_error("temporary rule array too small");
912541b0
KS
1005 return -1;
1006 }
1007 return 0;
6880b25d 1008}
e57e7bc1 1009
9ec6e95b 1010static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp) {
912541b0
KS
1011 unsigned int i;
1012 unsigned int start = 0;
1013 unsigned int end = rule_tmp->token_cur;
1014
1015 for (i = 0; i < rule_tmp->token_cur; i++) {
1016 enum token_type next_val = TK_UNSET;
1017 unsigned int next_idx = 0;
1018 unsigned int j;
1019
1020 /* find smallest value */
1021 for (j = start; j < end; j++) {
1022 if (rule_tmp->token[j].type == TK_UNSET)
1023 continue;
1024 if (next_val == TK_UNSET || rule_tmp->token[j].type < next_val) {
1025 next_val = rule_tmp->token[j].type;
1026 next_idx = j;
1027 }
1028 }
1029
1030 /* add token and mark done */
1031 if (add_token(rules, &rule_tmp->token[next_idx]) != 0)
1032 return -1;
1033 rule_tmp->token[next_idx].type = TK_UNSET;
1034
1035 /* shrink range */
1036 if (next_idx == start)
1037 start++;
1038 if (next_idx+1 == end)
1039 end--;
1040 }
1041 return 0;
724257d9
GKH
1042}
1043
6880b25d 1044static int add_rule(struct udev_rules *rules, char *line,
9ec6e95b 1045 const char *filename, unsigned int filename_off, unsigned int lineno) {
912541b0 1046 char *linepos;
04a9d3a0 1047 const char *attr;
84198c18
TG
1048 struct rule_tmp rule_tmp = {
1049 .rules = rules,
1050 .rule.type = TK_RULE,
1051 };
912541b0 1052
775f8b3c
KS
1053 /* the offset in the rule is limited to unsigned short */
1054 if (filename_off < USHRT_MAX)
1055 rule_tmp.rule.rule.filename_off = filename_off;
912541b0
KS
1056 rule_tmp.rule.rule.filename_line = lineno;
1057
1058 linepos = line;
1059 for (;;) {
1060 char *key;
1061 char *value;
1062 enum operation_type op;
1063
e736cf35 1064 if (get_key(rules->udev, &linepos, &key, &op, &value) != 0) {
3cf0f8f7
DR
1065 /* Avoid erroring on trailing whitespace. This is probably rare
1066 * so save the work for the error case instead of always trying
1067 * to strip the trailing whitespace with strstrip(). */
1068 while (isblank(*linepos))
1069 linepos++;
1070
e736cf35
DR
1071 /* If we aren't at the end of the line, this is a parsing error.
1072 * Make a best effort to describe where the problem is. */
2783fe06 1073 if (!strchr(NEWLINE, *linepos)) {
6501b52d 1074 char buf[2] = {*linepos};
1291bc89
ZJS
1075 _cleanup_free_ char *tmp;
1076
1077 tmp = cescape(buf);
ff49bc32 1078 log_error("invalid key/value pair in file %s on line %u, starting at character %tu ('%s')",
1291bc89 1079 filename, lineno, linepos - line + 1, tmp);
6501b52d 1080 if (*linepos == '#')
75716dd8 1081 log_error("hint: comments can only start at beginning of line");
1291bc89 1082 }
912541b0 1083 break;
e736cf35 1084 }
912541b0 1085
33502ffe 1086 if (streq(key, "ACTION")) {
912541b0 1087 if (op > OP_MATCH_MAX) {
9f6445e3 1088 log_error("invalid ACTION operation");
912541b0
KS
1089 goto invalid;
1090 }
1091 rule_add_key(&rule_tmp, TK_M_ACTION, op, value, NULL);
1092 continue;
1093 }
1094
33502ffe 1095 if (streq(key, "DEVPATH")) {
912541b0 1096 if (op > OP_MATCH_MAX) {
9f6445e3 1097 log_error("invalid DEVPATH operation");
912541b0
KS
1098 goto invalid;
1099 }
1100 rule_add_key(&rule_tmp, TK_M_DEVPATH, op, value, NULL);
1101 continue;
1102 }
1103
33502ffe 1104 if (streq(key, "KERNEL")) {
912541b0 1105 if (op > OP_MATCH_MAX) {
9f6445e3 1106 log_error("invalid KERNEL operation");
912541b0
KS
1107 goto invalid;
1108 }
1109 rule_add_key(&rule_tmp, TK_M_KERNEL, op, value, NULL);
1110 continue;
1111 }
1112
33502ffe 1113 if (streq(key, "SUBSYSTEM")) {
912541b0 1114 if (op > OP_MATCH_MAX) {
9f6445e3 1115 log_error("invalid SUBSYSTEM operation");
912541b0
KS
1116 goto invalid;
1117 }
1118 /* bus, class, subsystem events should all be the same */
33502ffe
KS
1119 if (streq(value, "subsystem") ||
1120 streq(value, "bus") ||
1121 streq(value, "class")) {
1122 if (streq(value, "bus") || streq(value, "class"))
9f6445e3 1123 log_error("'%s' must be specified as 'subsystem' "
912541b0
KS
1124 "please fix it in %s:%u", value, filename, lineno);
1125 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, "subsystem|class|bus", NULL);
1126 } else
1127 rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, value, NULL);
1128 continue;
1129 }
1130
33502ffe 1131 if (streq(key, "DRIVER")) {
912541b0 1132 if (op > OP_MATCH_MAX) {
9f6445e3 1133 log_error("invalid DRIVER operation");
912541b0
KS
1134 goto invalid;
1135 }
1136 rule_add_key(&rule_tmp, TK_M_DRIVER, op, value, NULL);
1137 continue;
1138 }
1139
33502ffe 1140 if (startswith(key, "ATTR{")) {
f8294e41 1141 attr = get_key_attribute(rules->udev, key + strlen("ATTR"));
912541b0 1142 if (attr == NULL) {
9f6445e3 1143 log_error("error parsing ATTR attribute");
912541b0
KS
1144 goto invalid;
1145 }
8e3ba377
DH
1146 if (op == OP_REMOVE) {
1147 log_error("invalid ATTR operation");
1148 goto invalid;
1149 }
f4cf2e5b 1150 if (op < OP_MATCH_MAX)
912541b0 1151 rule_add_key(&rule_tmp, TK_M_ATTR, op, value, attr);
f4cf2e5b 1152 else
912541b0 1153 rule_add_key(&rule_tmp, TK_A_ATTR, op, value, attr);
f4cf2e5b
KS
1154 continue;
1155 }
1156
1157 if (startswith(key, "SYSCTL{")) {
1158 attr = get_key_attribute(rules->udev, key + strlen("SYSCTL"));
1159 if (attr == NULL) {
1160 log_error("error parsing SYSCTL attribute");
1161 goto invalid;
1162 }
1163 if (op == OP_REMOVE) {
1164 log_error("invalid SYSCTL operation");
1165 goto invalid;
912541b0 1166 }
f4cf2e5b
KS
1167 if (op < OP_MATCH_MAX)
1168 rule_add_key(&rule_tmp, TK_M_SYSCTL, op, value, attr);
1169 else
1170 rule_add_key(&rule_tmp, TK_A_SYSCTL, op, value, attr);
912541b0
KS
1171 continue;
1172 }
1173
c26547d6 1174 if (startswith(key, "SECLABEL{")) {
f8294e41 1175 attr = get_key_attribute(rules->udev, key + strlen("SECLABEL"));
c26547d6 1176 if (!attr) {
9f6445e3 1177 log_error("error parsing SECLABEL attribute");
c26547d6
KS
1178 goto invalid;
1179 }
8e3ba377
DH
1180 if (op == OP_REMOVE) {
1181 log_error("invalid SECLABEL operation");
1182 goto invalid;
1183 }
c26547d6
KS
1184
1185 rule_add_key(&rule_tmp, TK_A_SECLABEL, op, value, attr);
1186 continue;
1187 }
1188
33502ffe 1189 if (streq(key, "KERNELS")) {
912541b0 1190 if (op > OP_MATCH_MAX) {
9f6445e3 1191 log_error("invalid KERNELS operation");
912541b0
KS
1192 goto invalid;
1193 }
1194 rule_add_key(&rule_tmp, TK_M_KERNELS, op, value, NULL);
1195 continue;
1196 }
1197
33502ffe 1198 if (streq(key, "SUBSYSTEMS")) {
912541b0 1199 if (op > OP_MATCH_MAX) {
9f6445e3 1200 log_error("invalid SUBSYSTEMS operation");
912541b0
KS
1201 goto invalid;
1202 }
1203 rule_add_key(&rule_tmp, TK_M_SUBSYSTEMS, op, value, NULL);
1204 continue;
1205 }
1206
33502ffe 1207 if (streq(key, "DRIVERS")) {
912541b0 1208 if (op > OP_MATCH_MAX) {
9f6445e3 1209 log_error("invalid DRIVERS operation");
912541b0
KS
1210 goto invalid;
1211 }
1212 rule_add_key(&rule_tmp, TK_M_DRIVERS, op, value, NULL);
1213 continue;
1214 }
1215
33502ffe 1216 if (startswith(key, "ATTRS{")) {
912541b0 1217 if (op > OP_MATCH_MAX) {
9f6445e3 1218 log_error("invalid ATTRS operation");
912541b0
KS
1219 goto invalid;
1220 }
f8294e41 1221 attr = get_key_attribute(rules->udev, key + strlen("ATTRS"));
912541b0 1222 if (attr == NULL) {
9f6445e3 1223 log_error("error parsing ATTRS attribute");
912541b0
KS
1224 goto invalid;
1225 }
33502ffe 1226 if (startswith(attr, "device/"))
baa30fbc 1227 log_error("the 'device' link may not be available in a future kernel, "
912541b0
KS
1228 "please fix it in %s:%u", filename, lineno);
1229 else if (strstr(attr, "../") != NULL)
baa30fbc 1230 log_error("do not reference parent sysfs directories directly, "
912541b0
KS
1231 "it may break with a future kernel, please fix it in %s:%u", filename, lineno);
1232 rule_add_key(&rule_tmp, TK_M_ATTRS, op, value, attr);
1233 continue;
1234 }
1235
33502ffe 1236 if (streq(key, "TAGS")) {
912541b0 1237 if (op > OP_MATCH_MAX) {
9f6445e3 1238 log_error("invalid TAGS operation");
912541b0
KS
1239 goto invalid;
1240 }
1241 rule_add_key(&rule_tmp, TK_M_TAGS, op, value, NULL);
1242 continue;
1243 }
1244
33502ffe 1245 if (startswith(key, "ENV{")) {
f8294e41 1246 attr = get_key_attribute(rules->udev, key + strlen("ENV"));
912541b0 1247 if (attr == NULL) {
9f6445e3 1248 log_error("error parsing ENV attribute");
912541b0
KS
1249 goto invalid;
1250 }
8e3ba377
DH
1251 if (op == OP_REMOVE) {
1252 log_error("invalid ENV operation");
1253 goto invalid;
1254 }
912541b0
KS
1255 if (op < OP_MATCH_MAX) {
1256 if (rule_add_key(&rule_tmp, TK_M_ENV, op, value, attr) != 0)
1257 goto invalid;
1258 } else {
1259 static const char *blacklist[] = {
1260 "ACTION",
1261 "SUBSYSTEM",
1262 "DEVTYPE",
1263 "MAJOR",
1264 "MINOR",
1265 "DRIVER",
1266 "IFINDEX",
1267 "DEVNAME",
1268 "DEVLINKS",
1269 "DEVPATH",
1270 "TAGS",
1271 };
1272 unsigned int i;
1273
395ea978
KS
1274 for (i = 0; i < ELEMENTSOF(blacklist); i++) {
1275 if (!streq(attr, blacklist[i]))
912541b0 1276 continue;
9f6445e3 1277 log_error("invalid ENV attribute, '%s' can not be set %s:%u", attr, filename, lineno);
395ea978
KS
1278 goto invalid;
1279 }
912541b0
KS
1280 if (rule_add_key(&rule_tmp, TK_A_ENV, op, value, attr) != 0)
1281 goto invalid;
1282 }
1283 continue;
1284 }
1285
33502ffe 1286 if (streq(key, "TAG")) {
912541b0
KS
1287 if (op < OP_MATCH_MAX)
1288 rule_add_key(&rule_tmp, TK_M_TAG, op, value, NULL);
1289 else
1290 rule_add_key(&rule_tmp, TK_A_TAG, op, value, NULL);
1291 continue;
1292 }
1293
33502ffe 1294 if (streq(key, "PROGRAM")) {
8e3ba377
DH
1295 if (op == OP_REMOVE) {
1296 log_error("invalid PROGRAM operation");
1297 goto invalid;
1298 }
912541b0
KS
1299 rule_add_key(&rule_tmp, TK_M_PROGRAM, op, value, NULL);
1300 continue;
1301 }
1302
33502ffe 1303 if (streq(key, "RESULT")) {
912541b0 1304 if (op > OP_MATCH_MAX) {
9f6445e3 1305 log_error("invalid RESULT operation");
912541b0
KS
1306 goto invalid;
1307 }
1308 rule_add_key(&rule_tmp, TK_M_RESULT, op, value, NULL);
1309 continue;
1310 }
1311
33502ffe 1312 if (startswith(key, "IMPORT")) {
f8294e41 1313 attr = get_key_attribute(rules->udev, key + strlen("IMPORT"));
912541b0 1314 if (attr == NULL) {
9f6445e3 1315 log_error("IMPORT{} type missing, ignoring IMPORT %s:%u", filename, lineno);
912541b0
KS
1316 continue;
1317 }
8e3ba377
DH
1318 if (op == OP_REMOVE) {
1319 log_error("invalid IMPORT operation");
1320 goto invalid;
1321 }
33502ffe 1322 if (streq(attr, "program")) {
912541b0
KS
1323 /* find known built-in command */
1324 if (value[0] != '/') {
1325 enum udev_builtin_cmd cmd;
1326
1327 cmd = udev_builtin_lookup(value);
1328 if (cmd < UDEV_BUILTIN_MAX) {
9f6445e3 1329 log_debug("IMPORT found builtin '%s', replacing %s:%u",
baa30fbc 1330 value, filename, lineno);
912541b0
KS
1331 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1332 continue;
1333 }
1334 }
912541b0 1335 rule_add_key(&rule_tmp, TK_M_IMPORT_PROG, op, value, NULL);
33502ffe 1336 } else if (streq(attr, "builtin")) {
912541b0
KS
1337 enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1338
912541b0
KS
1339 if (cmd < UDEV_BUILTIN_MAX)
1340 rule_add_key(&rule_tmp, TK_M_IMPORT_BUILTIN, op, value, &cmd);
1341 else
9f6445e3 1342 log_error("IMPORT{builtin}: '%s' unknown %s:%u", value, filename, lineno);
33502ffe 1343 } else if (streq(attr, "file")) {
912541b0 1344 rule_add_key(&rule_tmp, TK_M_IMPORT_FILE, op, value, NULL);
33502ffe 1345 } else if (streq(attr, "db")) {
912541b0 1346 rule_add_key(&rule_tmp, TK_M_IMPORT_DB, op, value, NULL);
33502ffe 1347 } else if (streq(attr, "cmdline")) {
912541b0 1348 rule_add_key(&rule_tmp, TK_M_IMPORT_CMDLINE, op, value, NULL);
33502ffe 1349 } else if (streq(attr, "parent")) {
912541b0 1350 rule_add_key(&rule_tmp, TK_M_IMPORT_PARENT, op, value, NULL);
83cd6b75 1351 } else
9f6445e3 1352 log_error("IMPORT{} unknown type, ignoring IMPORT %s:%u", filename, lineno);
912541b0
KS
1353 continue;
1354 }
1355
33502ffe 1356 if (startswith(key, "TEST")) {
912541b0
KS
1357 mode_t mode = 0;
1358
1359 if (op > OP_MATCH_MAX) {
9f6445e3 1360 log_error("invalid TEST operation");
912541b0
KS
1361 goto invalid;
1362 }
f8294e41 1363 attr = get_key_attribute(rules->udev, key + strlen("TEST"));
912541b0
KS
1364 if (attr != NULL) {
1365 mode = strtol(attr, NULL, 8);
1366 rule_add_key(&rule_tmp, TK_M_TEST, op, value, &mode);
1367 } else {
1368 rule_add_key(&rule_tmp, TK_M_TEST, op, value, NULL);
1369 }
1370 continue;
1371 }
1372
33502ffe 1373 if (startswith(key, "RUN")) {
f8294e41 1374 attr = get_key_attribute(rules->udev, key + strlen("RUN"));
83cd6b75
KS
1375 if (attr == NULL)
1376 attr = "program";
8e3ba377
DH
1377 if (op == OP_REMOVE) {
1378 log_error("invalid RUN operation");
1379 goto invalid;
1380 }
83cd6b75 1381
33502ffe 1382 if (streq(attr, "builtin")) {
83cd6b75
KS
1383 enum udev_builtin_cmd cmd = udev_builtin_lookup(value);
1384
1385 if (cmd < UDEV_BUILTIN_MAX)
1386 rule_add_key(&rule_tmp, TK_A_RUN_BUILTIN, op, value, &cmd);
1387 else
ec3281d3 1388 log_error("RUN{builtin}: '%s' unknown %s:%u", value, filename, lineno);
33502ffe 1389 } else if (streq(attr, "program")) {
83cd6b75
KS
1390 enum udev_builtin_cmd cmd = UDEV_BUILTIN_MAX;
1391
83cd6b75
KS
1392 rule_add_key(&rule_tmp, TK_A_RUN_PROGRAM, op, value, &cmd);
1393 } else {
9f6445e3 1394 log_error("RUN{} unknown type, ignoring RUN %s:%u", filename, lineno);
83cd6b75
KS
1395 }
1396
912541b0
KS
1397 continue;
1398 }
1399
33502ffe 1400 if (streq(key, "WAIT_FOR") || streq(key, "WAIT_FOR_SYSFS")) {
8e3ba377
DH
1401 if (op == OP_REMOVE) {
1402 log_error("invalid WAIT_FOR/WAIT_FOR_SYSFS operation");
1403 goto invalid;
1404 }
912541b0
KS
1405 rule_add_key(&rule_tmp, TK_M_WAITFOR, 0, value, NULL);
1406 continue;
1407 }
1408
33502ffe 1409 if (streq(key, "LABEL")) {
8e3ba377
DH
1410 if (op == OP_REMOVE) {
1411 log_error("invalid LABEL operation");
1412 goto invalid;
1413 }
915bf0f6 1414 rule_tmp.rule.rule.label_off = rules_add_string(rules, value);
912541b0
KS
1415 continue;
1416 }
1417
33502ffe 1418 if (streq(key, "GOTO")) {
8e3ba377
DH
1419 if (op == OP_REMOVE) {
1420 log_error("invalid GOTO operation");
1421 goto invalid;
1422 }
912541b0
KS
1423 rule_add_key(&rule_tmp, TK_A_GOTO, 0, value, NULL);
1424 continue;
1425 }
1426
33502ffe 1427 if (startswith(key, "NAME")) {
8e3ba377
DH
1428 if (op == OP_REMOVE) {
1429 log_error("invalid NAME operation");
1430 goto invalid;
1431 }
912541b0
KS
1432 if (op < OP_MATCH_MAX) {
1433 rule_add_key(&rule_tmp, TK_M_NAME, op, value, NULL);
1434 } else {
33502ffe 1435 if (streq(value, "%k")) {
baa30fbc 1436 log_error("NAME=\"%%k\" is ignored, because it breaks kernel supplied names, "
912541b0
KS
1437 "please remove it from %s:%u\n", filename, lineno);
1438 continue;
1439 }
1440 if (value[0] == '\0') {
baa30fbc
KS
1441 log_debug("NAME=\"\" is ignored, because udev will not delete any device nodes, "
1442 "please remove it from %s:%u\n", filename, lineno);
912541b0
KS
1443 continue;
1444 }
1445 rule_add_key(&rule_tmp, TK_A_NAME, op, value, NULL);
1446 }
1447 rule_tmp.rule.rule.can_set_name = true;
1448 continue;
1449 }
1450
8a173387 1451 if (streq(key, "SYMLINK")) {
8e3ba377
DH
1452 if (op == OP_REMOVE) {
1453 log_error("invalid SYMLINK operation");
1454 goto invalid;
1455 }
8a173387 1456 if (op < OP_MATCH_MAX)
912541b0 1457 rule_add_key(&rule_tmp, TK_M_DEVLINK, op, value, NULL);
8a173387
KS
1458 else
1459 rule_add_key(&rule_tmp, TK_A_DEVLINK, op, value, NULL);
912541b0
KS
1460 rule_tmp.rule.rule.can_set_name = true;
1461 continue;
1462 }
1463
33502ffe 1464 if (streq(key, "OWNER")) {
912541b0
KS
1465 uid_t uid;
1466 char *endptr;
1467
8e3ba377
DH
1468 if (op == OP_REMOVE) {
1469 log_error("invalid OWNER operation");
1470 goto invalid;
1471 }
1472
912541b0
KS
1473 uid = strtoul(value, &endptr, 10);
1474 if (endptr[0] == '\0') {
1475 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1476 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1477 uid = add_uid(rules, value);
1478 rule_add_key(&rule_tmp, TK_A_OWNER_ID, op, NULL, &uid);
1479 } else if (rules->resolve_names >= 0) {
1480 rule_add_key(&rule_tmp, TK_A_OWNER, op, value, NULL);
1481 }
1482 rule_tmp.rule.rule.can_set_name = true;
1483 continue;
1484 }
1485
33502ffe 1486 if (streq(key, "GROUP")) {
912541b0
KS
1487 gid_t gid;
1488 char *endptr;
1489
8e3ba377
DH
1490 if (op == OP_REMOVE) {
1491 log_error("invalid GROUP operation");
1492 goto invalid;
1493 }
1494
912541b0
KS
1495 gid = strtoul(value, &endptr, 10);
1496 if (endptr[0] == '\0') {
1497 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1498 } else if ((rules->resolve_names > 0) && strchr("$%", value[0]) == NULL) {
1499 gid = add_gid(rules, value);
1500 rule_add_key(&rule_tmp, TK_A_GROUP_ID, op, NULL, &gid);
1501 } else if (rules->resolve_names >= 0) {
1502 rule_add_key(&rule_tmp, TK_A_GROUP, op, value, NULL);
1503 }
1504 rule_tmp.rule.rule.can_set_name = true;
1505 continue;
1506 }
1507
33502ffe 1508 if (streq(key, "MODE")) {
912541b0
KS
1509 mode_t mode;
1510 char *endptr;
1511
8e3ba377
DH
1512 if (op == OP_REMOVE) {
1513 log_error("invalid MODE operation");
1514 goto invalid;
1515 }
1516
912541b0
KS
1517 mode = strtol(value, &endptr, 8);
1518 if (endptr[0] == '\0')
1519 rule_add_key(&rule_tmp, TK_A_MODE_ID, op, NULL, &mode);
1520 else
1521 rule_add_key(&rule_tmp, TK_A_MODE, op, value, NULL);
1522 rule_tmp.rule.rule.can_set_name = true;
1523 continue;
1524 }
1525
33502ffe 1526 if (streq(key, "OPTIONS")) {
912541b0
KS
1527 const char *pos;
1528
8e3ba377
DH
1529 if (op == OP_REMOVE) {
1530 log_error("invalid OPTIONS operation");
1531 goto invalid;
1532 }
1533
912541b0
KS
1534 pos = strstr(value, "link_priority=");
1535 if (pos != NULL) {
1536 int prio = atoi(&pos[strlen("link_priority=")]);
1537
1538 rule_add_key(&rule_tmp, TK_A_DEVLINK_PRIO, op, NULL, &prio);
912541b0
KS
1539 }
1540
9f20a8a3 1541 pos = strstr(value, "string_escape=");
912541b0
KS
1542 if (pos != NULL) {
1543 pos = &pos[strlen("string_escape=")];
33502ffe 1544 if (startswith(pos, "none"))
912541b0 1545 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_NONE, op, NULL, NULL);
33502ffe 1546 else if (startswith(pos, "replace"))
912541b0
KS
1547 rule_add_key(&rule_tmp, TK_A_STRING_ESCAPE_REPLACE, op, NULL, NULL);
1548 }
1549
1550 pos = strstr(value, "db_persist");
1551 if (pos != NULL)
1552 rule_add_key(&rule_tmp, TK_A_DB_PERSIST, op, NULL, NULL);
1553
1554 pos = strstr(value, "nowatch");
1555 if (pos != NULL) {
1556 const int off = 0;
1557
1558 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &off);
912541b0
KS
1559 } else {
1560 pos = strstr(value, "watch");
1561 if (pos != NULL) {
1562 const int on = 1;
1563
1564 rule_add_key(&rule_tmp, TK_A_INOTIFY_WATCH, op, NULL, &on);
912541b0
KS
1565 }
1566 }
1567
1568 pos = strstr(value, "static_node=");
1569 if (pos != NULL) {
1570 rule_add_key(&rule_tmp, TK_A_STATIC_NODE, op, &pos[strlen("static_node=")], NULL);
1571 rule_tmp.rule.rule.has_static_node = true;
1572 }
1573
1574 continue;
1575 }
1576
9f6445e3 1577 log_error("unknown key '%s' in %s:%u", key, filename, lineno);
912541b0
KS
1578 goto invalid;
1579 }
1580
1581 /* add rule token */
1582 rule_tmp.rule.rule.token_count = 1 + rule_tmp.token_cur;
1583 if (add_token(rules, &rule_tmp.rule) != 0)
1584 goto invalid;
1585
1586 /* add tokens to list, sorted by type */
1587 if (sort_token(rules, &rule_tmp) != 0)
1588 goto invalid;
1589
1590 return 0;
c7521974 1591invalid:
9f6445e3 1592 log_error("invalid rule '%s:%u'", filename, lineno);
912541b0 1593 return -1;
c7521974
KS
1594}
1595
9ec6e95b 1596static int parse_file(struct udev_rules *rules, const char *filename) {
6c8aaf0c 1597 _cleanup_fclose_ FILE *f = NULL;
912541b0 1598 unsigned int first_token;
775f8b3c 1599 unsigned int filename_off;
912541b0
KS
1600 char line[UTIL_LINE_SIZE];
1601 int line_nr = 0;
1602 unsigned int i;
1603
ed88bcfb
ZJS
1604 f = fopen(filename, "re");
1605 if (!f) {
1606 if (errno == ENOENT)
1607 return 0;
1608 else
1609 return -errno;
775f8b3c 1610 }
912541b0 1611
ed88bcfb
ZJS
1612 if (null_or_empty_fd(fileno(f))) {
1613 log_debug("Skipping empty file: %s", filename);
1614 return 0;
1615 } else
1616 log_debug("Reading rules file: %s", filename);
912541b0
KS
1617
1618 first_token = rules->token_cur;
915bf0f6 1619 filename_off = rules_add_string(rules, filename);
912541b0
KS
1620
1621 while (fgets(line, sizeof(line), f) != NULL) {
1622 char *key;
1623 size_t len;
1624
1625 /* skip whitespace */
1626 line_nr++;
1627 key = line;
1628 while (isspace(key[0]))
1629 key++;
1630
1631 /* comment */
1632 if (key[0] == '#')
1633 continue;
1634
1635 len = strlen(line);
1636 if (len < 3)
1637 continue;
1638
1639 /* continue reading if backslash+newline is found */
1640 while (line[len-2] == '\\') {
1641 if (fgets(&line[len-2], (sizeof(line)-len)+2, f) == NULL)
1642 break;
1643 if (strlen(&line[len-2]) < 2)
1644 break;
1645 line_nr++;
1646 len = strlen(line);
1647 }
1648
1649 if (len+1 >= sizeof(line)) {
9f6445e3 1650 log_error("line too long '%s':%u, ignored", filename, line_nr);
912541b0
KS
1651 continue;
1652 }
1653 add_rule(rules, key, filename, filename_off, line_nr);
1654 }
912541b0
KS
1655
1656 /* link GOTOs to LABEL rules in this file to be able to fast-forward */
1657 for (i = first_token+1; i < rules->token_cur; i++) {
1658 if (rules->tokens[i].type == TK_A_GOTO) {
915bf0f6 1659 char *label = rules_str(rules, rules->tokens[i].key.value_off);
912541b0
KS
1660 unsigned int j;
1661
1662 for (j = i+1; j < rules->token_cur; j++) {
1663 if (rules->tokens[j].type != TK_RULE)
1664 continue;
1665 if (rules->tokens[j].rule.label_off == 0)
1666 continue;
915bf0f6 1667 if (!streq(label, rules_str(rules, rules->tokens[j].rule.label_off)))
912541b0
KS
1668 continue;
1669 rules->tokens[i].key.rule_goto = j;
1670 break;
1671 }
1672 if (rules->tokens[i].key.rule_goto == 0)
9f6445e3 1673 log_error("GOTO '%s' has no matching label in: '%s'", label, filename);
912541b0
KS
1674 }
1675 }
1676 return 0;
c7521974
KS
1677}
1678
9ec6e95b 1679struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names) {
912541b0
KS
1680 struct udev_rules *rules;
1681 struct udev_list file_list;
912541b0 1682 struct token end_token;
775f8b3c
KS
1683 char **files, **f;
1684 int r;
912541b0 1685
955d98c9 1686 rules = new0(struct udev_rules, 1);
912541b0
KS
1687 if (rules == NULL)
1688 return NULL;
1689 rules->udev = udev;
1690 rules->resolve_names = resolve_names;
1691 udev_list_init(udev, &file_list, true);
1692
1693 /* init token array and string buffer */
1694 rules->tokens = malloc(PREALLOC_TOKEN * sizeof(struct token));
0820a4f0
VP
1695 if (rules->tokens == NULL)
1696 return udev_rules_unref(rules);
912541b0
KS
1697 rules->token_max = PREALLOC_TOKEN;
1698
915bf0f6
KS
1699 rules->strbuf = strbuf_new();
1700 if (!rules->strbuf)
0820a4f0
VP
1701 return udev_rules_unref(rules);
1702
3b8c1cb0
KS
1703 udev_rules_check_timestamp(rules);
1704
2ad8416d 1705 r = conf_files_list_strv(&files, ".rules", NULL, rules_dirs);
775f8b3c 1706 if (r < 0) {
da927ba9 1707 log_error_errno(r, "failed to enumerate rules files: %m");
0820a4f0 1708 return udev_rules_unref(rules);
912541b0
KS
1709 }
1710
775f8b3c
KS
1711 /*
1712 * The offset value in the rules strct is limited; add all
1713 * rules file names to the beginning of the string buffer.
1714 */
1715 STRV_FOREACH(f, files)
915bf0f6 1716 rules_add_string(rules, *f);
912541b0 1717
775f8b3c
KS
1718 STRV_FOREACH(f, files)
1719 parse_file(rules, *f);
1720
1721 strv_free(files);
912541b0 1722
29804cc1 1723 memzero(&end_token, sizeof(struct token));
912541b0
KS
1724 end_token.type = TK_END;
1725 add_token(rules, &end_token);
9f6445e3 1726 log_debug("rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings",
915bf0f6 1727 rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->strbuf->len);
912541b0 1728
915bf0f6 1729 /* cleanup temporary strbuf data */
9f6445e3 1730 log_debug("%zu strings (%zu bytes), %zu de-duplicated (%zu bytes), %zu trie nodes used",
915bf0f6
KS
1731 rules->strbuf->in_count, rules->strbuf->in_len,
1732 rules->strbuf->dedup_count, rules->strbuf->dedup_len, rules->strbuf->nodes_count);
1733 strbuf_complete(rules->strbuf);
912541b0
KS
1734
1735 /* cleanup uid/gid cache */
1736 free(rules->uids);
1737 rules->uids = NULL;
1738 rules->uids_cur = 0;
1739 rules->uids_max = 0;
1740 free(rules->gids);
1741 rules->gids = NULL;
1742 rules->gids_cur = 0;
1743 rules->gids_max = 0;
1744
1745 dump_rules(rules);
1746 return rules;
c7521974
KS
1747}
1748
9ec6e95b 1749struct udev_rules *udev_rules_unref(struct udev_rules *rules) {
912541b0
KS
1750 if (rules == NULL)
1751 return NULL;
1752 free(rules->tokens);
915bf0f6 1753 strbuf_cleanup(rules->strbuf);
912541b0
KS
1754 free(rules->uids);
1755 free(rules->gids);
1756 free(rules);
1757 return NULL;
c7521974 1758}
6880b25d 1759
9ec6e95b 1760bool udev_rules_check_timestamp(struct udev_rules *rules) {
5c11fbe3
KS
1761 if (!rules)
1762 return false;
1763
2ad8416d 1764 return paths_check_timestamp(rules_dirs, &rules->dirs_ts_usec, true);
6ada823a
KS
1765}
1766
9ec6e95b 1767static int match_key(struct udev_rules *rules, struct token *token, const char *val) {
915bf0f6 1768 char *key_value = rules_str(rules, token->key.value_off);
912541b0
KS
1769 char *pos;
1770 bool match = false;
1771
1772 if (val == NULL)
1773 val = "";
1774
1775 switch (token->key.glob) {
1776 case GL_PLAIN:
33502ffe 1777 match = (streq(key_value, val));
912541b0
KS
1778 break;
1779 case GL_GLOB:
1780 match = (fnmatch(key_value, val, 0) == 0);
1781 break;
1782 case GL_SPLIT:
1783 {
33502ffe 1784 const char *s;
912541b0
KS
1785 size_t len;
1786
915bf0f6 1787 s = rules_str(rules, token->key.value_off);
912541b0
KS
1788 len = strlen(val);
1789 for (;;) {
1790 const char *next;
1791
33502ffe 1792 next = strchr(s, '|');
912541b0 1793 if (next != NULL) {
33502ffe 1794 size_t matchlen = (size_t)(next - s);
912541b0 1795
641906e9 1796 match = (matchlen == len && strneq(s, val, matchlen));
912541b0
KS
1797 if (match)
1798 break;
1799 } else {
33502ffe 1800 match = (streq(s, val));
912541b0
KS
1801 break;
1802 }
33502ffe 1803 s = &next[1];
912541b0
KS
1804 }
1805 break;
1806 }
1807 case GL_SPLIT_GLOB:
1808 {
1809 char value[UTIL_PATH_SIZE];
1810
d5a89d7d 1811 strscpy(value, sizeof(value), rules_str(rules, token->key.value_off));
912541b0
KS
1812 key_value = value;
1813 while (key_value != NULL) {
1814 pos = strchr(key_value, '|');
1815 if (pos != NULL) {
1816 pos[0] = '\0';
1817 pos = &pos[1];
1818 }
912541b0
KS
1819 match = (fnmatch(key_value, val, 0) == 0);
1820 if (match)
1821 break;
1822 key_value = pos;
1823 }
1824 break;
1825 }
1826 case GL_SOMETHING:
1827 match = (val[0] != '\0');
1828 break;
1829 case GL_UNSET:
1830 return -1;
1831 }
1832
baa30fbc 1833 if (match && (token->key.op == OP_MATCH))
912541b0 1834 return 0;
baa30fbc 1835 if (!match && (token->key.op == OP_NOMATCH))
912541b0 1836 return 0;
912541b0 1837 return -1;
6880b25d
KS
1838}
1839
9ec6e95b 1840static int match_attr(struct udev_rules *rules, struct udev_device *dev, struct udev_event *event, struct token *cur) {
912541b0
KS
1841 const char *name;
1842 char nbuf[UTIL_NAME_SIZE];
1843 const char *value;
1844 char vbuf[UTIL_NAME_SIZE];
1845 size_t len;
1846
915bf0f6 1847 name = rules_str(rules, cur->key.attr_off);
912541b0
KS
1848 switch (cur->key.attrsubst) {
1849 case SB_FORMAT:
1850 udev_event_apply_format(event, name, nbuf, sizeof(nbuf));
1851 name = nbuf;
1852 /* fall through */
1853 case SB_NONE:
1854 value = udev_device_get_sysattr_value(dev, name);
1855 if (value == NULL)
1856 return -1;
1857 break;
1858 case SB_SUBSYS:
1859 if (util_resolve_subsys_kernel(event->udev, name, vbuf, sizeof(vbuf), 1) != 0)
1860 return -1;
1861 value = vbuf;
1862 break;
1863 default:
1864 return -1;
1865 }
1866
1867 /* remove trailing whitespace, if not asked to match for it */
1868 len = strlen(value);
1869 if (len > 0 && isspace(value[len-1])) {
1870 const char *key_value;
1871 size_t klen;
1872
915bf0f6 1873 key_value = rules_str(rules, cur->key.value_off);
912541b0
KS
1874 klen = strlen(key_value);
1875 if (klen > 0 && !isspace(key_value[klen-1])) {
1876 if (value != vbuf) {
d5a89d7d 1877 strscpy(vbuf, sizeof(vbuf), value);
912541b0
KS
1878 value = vbuf;
1879 }
1880 while (len > 0 && isspace(vbuf[--len]))
1881 vbuf[len] = '\0';
912541b0
KS
1882 }
1883 }
1884
1885 return match_key(rules, cur, value);
6880b25d
KS
1886}
1887
1888enum escape_type {
912541b0
KS
1889 ESCAPE_UNSET,
1890 ESCAPE_NONE,
1891 ESCAPE_REPLACE,
6880b25d
KS
1892};
1893
dd5eddd2
KS
1894int udev_rules_apply_to_event(struct udev_rules *rules,
1895 struct udev_event *event,
1896 usec_t timeout_usec,
67117413 1897 usec_t timeout_warn_usec,
8314de1d 1898 struct udev_list *properties_list) {
912541b0
KS
1899 struct token *cur;
1900 struct token *rule;
1901 enum escape_type esc = ESCAPE_UNSET;
1902 bool can_set_name;
1903
1904 if (rules->tokens == NULL)
1905 return -1;
1906
33502ffe 1907 can_set_name = ((!streq(udev_device_get_action(event->dev), "remove")) &&
912541b0
KS
1908 (major(udev_device_get_devnum(event->dev)) > 0 ||
1909 udev_device_get_ifindex(event->dev) > 0));
1910
1911 /* loop through token list, match, run actions or forward to next rule */
1912 cur = &rules->tokens[0];
1913 rule = cur;
1914 for (;;) {
1915 dump_token(rules, cur);
1916 switch (cur->type) {
1917 case TK_RULE:
1918 /* current rule */
1919 rule = cur;
1920 /* possibly skip rules which want to set NAME, SYMLINK, OWNER, GROUP, MODE */
1921 if (!can_set_name && rule->rule.can_set_name)
1922 goto nomatch;
1923 esc = ESCAPE_UNSET;
1924 break;
1925 case TK_M_ACTION:
1926 if (match_key(rules, cur, udev_device_get_action(event->dev)) != 0)
1927 goto nomatch;
1928 break;
1929 case TK_M_DEVPATH:
1930 if (match_key(rules, cur, udev_device_get_devpath(event->dev)) != 0)
1931 goto nomatch;
1932 break;
1933 case TK_M_KERNEL:
1934 if (match_key(rules, cur, udev_device_get_sysname(event->dev)) != 0)
1935 goto nomatch;
1936 break;
1937 case TK_M_DEVLINK: {
912541b0
KS
1938 struct udev_list_entry *list_entry;
1939 bool match = false;
1940
1941 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(event->dev)) {
1942 const char *devlink;
1943
920b52e4 1944 devlink = udev_list_entry_get_name(list_entry) + strlen("/dev/");
912541b0
KS
1945 if (match_key(rules, cur, devlink) == 0) {
1946 match = true;
1947 break;
1948 }
1949 }
1950 if (!match)
1951 goto nomatch;
1952 break;
1953 }
1954 case TK_M_NAME:
1955 if (match_key(rules, cur, event->name) != 0)
1956 goto nomatch;
1957 break;
1958 case TK_M_ENV: {
915bf0f6 1959 const char *key_name = rules_str(rules, cur->key.attr_off);
912541b0
KS
1960 const char *value;
1961
1962 value = udev_device_get_property_value(event->dev, key_name);
adeba500
KS
1963
1964 /* check global properties */
1965 if (!value && properties_list) {
1966 struct udev_list_entry *list_entry;
1967
1968 list_entry = udev_list_get_entry(properties_list);
1969 list_entry = udev_list_entry_get_by_name(list_entry, key_name);
1970 if (list_entry != NULL)
1971 value = udev_list_entry_get_value(list_entry);
1972 }
1973
1974 if (!value)
912541b0 1975 value = "";
912541b0
KS
1976 if (match_key(rules, cur, value))
1977 goto nomatch;
1978 break;
1979 }
1980 case TK_M_TAG: {
1981 struct udev_list_entry *list_entry;
1982 bool match = false;
1983
1984 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(event->dev)) {
915bf0f6 1985 if (streq(rules_str(rules, cur->key.value_off), udev_list_entry_get_name(list_entry))) {
912541b0
KS
1986 match = true;
1987 break;
1988 }
1989 }
1990 if (!match && (cur->key.op != OP_NOMATCH))
1991 goto nomatch;
1992 break;
1993 }
1994 case TK_M_SUBSYSTEM:
1995 if (match_key(rules, cur, udev_device_get_subsystem(event->dev)) != 0)
1996 goto nomatch;
1997 break;
1998 case TK_M_DRIVER:
1999 if (match_key(rules, cur, udev_device_get_driver(event->dev)) != 0)
2000 goto nomatch;
2001 break;
2002 case TK_M_WAITFOR: {
2003 char filename[UTIL_PATH_SIZE];
2004 int found;
2005
915bf0f6 2006 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), filename, sizeof(filename));
912541b0
KS
2007 found = (wait_for_file(event->dev, filename, 10) == 0);
2008 if (!found && (cur->key.op != OP_NOMATCH))
2009 goto nomatch;
2010 break;
2011 }
2012 case TK_M_ATTR:
2013 if (match_attr(rules, event->dev, event, cur) != 0)
2014 goto nomatch;
2015 break;
f4cf2e5b
KS
2016 case TK_M_SYSCTL: {
2017 char filename[UTIL_PATH_SIZE];
2018 _cleanup_free_ char *value = NULL;
2019 size_t len;
2020
2021 udev_event_apply_format(event, rules_str(rules, cur->key.attr_off), filename, sizeof(filename));
2022 sysctl_normalize(filename);
2023 if (sysctl_read(filename, &value) < 0)
2024 goto nomatch;
2025
2026 len = strlen(value);
2027 while (len > 0 && isspace(value[--len]))
2028 value[len] = '\0';
2029 if (match_key(rules, cur, value) != 0)
2030 goto nomatch;
2031 break;
2032 }
912541b0
KS
2033 case TK_M_KERNELS:
2034 case TK_M_SUBSYSTEMS:
2035 case TK_M_DRIVERS:
2036 case TK_M_ATTRS:
2037 case TK_M_TAGS: {
2038 struct token *next;
2039
2040 /* get whole sequence of parent matches */
2041 next = cur;
2042 while (next->type > TK_M_PARENTS_MIN && next->type < TK_M_PARENTS_MAX)
2043 next++;
2044
2045 /* loop over parents */
2046 event->dev_parent = event->dev;
2047 for (;;) {
2048 struct token *key;
2049
912541b0
KS
2050 /* loop over sequence of parent match keys */
2051 for (key = cur; key < next; key++ ) {
2052 dump_token(rules, key);
2053 switch(key->type) {
2054 case TK_M_KERNELS:
2055 if (match_key(rules, key, udev_device_get_sysname(event->dev_parent)) != 0)
2056 goto try_parent;
2057 break;
2058 case TK_M_SUBSYSTEMS:
2059 if (match_key(rules, key, udev_device_get_subsystem(event->dev_parent)) != 0)
2060 goto try_parent;
2061 break;
2062 case TK_M_DRIVERS:
2063 if (match_key(rules, key, udev_device_get_driver(event->dev_parent)) != 0)
2064 goto try_parent;
2065 break;
2066 case TK_M_ATTRS:
2067 if (match_attr(rules, event->dev_parent, event, key) != 0)
2068 goto try_parent;
2069 break;
2070 case TK_M_TAGS: {
915bf0f6 2071 bool match = udev_device_has_tag(event->dev_parent, rules_str(rules, cur->key.value_off));
912541b0
KS
2072
2073 if (match && key->key.op == OP_NOMATCH)
2074 goto try_parent;
2075 if (!match && key->key.op == OP_MATCH)
2076 goto try_parent;
2077 break;
2078 }
2079 default:
2080 goto nomatch;
2081 }
912541b0 2082 }
912541b0
KS
2083 break;
2084
2085 try_parent:
2086 event->dev_parent = udev_device_get_parent(event->dev_parent);
2087 if (event->dev_parent == NULL)
2088 goto nomatch;
2089 }
2090 /* move behind our sequence of parent match keys */
2091 cur = next;
2092 continue;
2093 }
2094 case TK_M_TEST: {
2095 char filename[UTIL_PATH_SIZE];
2096 struct stat statbuf;
2097 int match;
2098
915bf0f6 2099 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), filename, sizeof(filename));
912541b0
KS
2100 if (util_resolve_subsys_kernel(event->udev, filename, filename, sizeof(filename), 0) != 0) {
2101 if (filename[0] != '/') {
2102 char tmp[UTIL_PATH_SIZE];
2103
d5a89d7d
KS
2104 strscpy(tmp, sizeof(tmp), filename);
2105 strscpyl(filename, sizeof(filename),
912541b0
KS
2106 udev_device_get_syspath(event->dev), "/", tmp, NULL);
2107 }
2108 }
2109 attr_subst_subdir(filename, sizeof(filename));
2110
2111 match = (stat(filename, &statbuf) == 0);
baa30fbc 2112 if (match && cur->key.mode > 0)
912541b0 2113 match = ((statbuf.st_mode & cur->key.mode) > 0);
912541b0
KS
2114 if (match && cur->key.op == OP_NOMATCH)
2115 goto nomatch;
2116 if (!match && cur->key.op == OP_MATCH)
2117 goto nomatch;
2118 break;
2119 }
912541b0
KS
2120 case TK_M_PROGRAM: {
2121 char program[UTIL_PATH_SIZE];
2122 char **envp;
209b031e 2123 char result[UTIL_LINE_SIZE];
912541b0
KS
2124
2125 free(event->program_result);
2126 event->program_result = NULL;
915bf0f6 2127 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), program, sizeof(program));
912541b0 2128 envp = udev_device_get_properties_envp(event->dev);
9f6445e3 2129 log_debug("PROGRAM '%s' %s:%u",
baa30fbc 2130 program,
915bf0f6 2131 rules_str(rules, rule->rule.filename_off),
baa30fbc 2132 rule->rule.filename_line);
912541b0 2133
53318514 2134 if (udev_event_spawn(event, timeout_usec, timeout_warn_usec, true, program, envp, result, sizeof(result)) < 0) {
912541b0
KS
2135 if (cur->key.op != OP_NOMATCH)
2136 goto nomatch;
2137 } else {
2138 int count;
2139
2140 util_remove_trailing_chars(result, '\n');
2141 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2142 count = util_replace_chars(result, UDEV_ALLOWED_CHARS_INPUT);
2143 if (count > 0)
9f6445e3 2144 log_debug("%i character(s) replaced" , count);
912541b0
KS
2145 }
2146 event->program_result = strdup(result);
912541b0
KS
2147 if (cur->key.op == OP_NOMATCH)
2148 goto nomatch;
2149 }
2150 break;
2151 }
2152 case TK_M_IMPORT_FILE: {
2153 char import[UTIL_PATH_SIZE];
2154
915bf0f6 2155 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
912541b0
KS
2156 if (import_file_into_properties(event->dev, import) != 0)
2157 if (cur->key.op != OP_NOMATCH)
2158 goto nomatch;
2159 break;
2160 }
2161 case TK_M_IMPORT_PROG: {
2162 char import[UTIL_PATH_SIZE];
2163
915bf0f6 2164 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
9f6445e3 2165 log_debug("IMPORT '%s' %s:%u",
baa30fbc 2166 import,
915bf0f6 2167 rules_str(rules, rule->rule.filename_off),
baa30fbc 2168 rule->rule.filename_line);
912541b0 2169
8314de1d 2170 if (import_program_into_properties(event, timeout_usec, timeout_warn_usec, import) != 0)
912541b0
KS
2171 if (cur->key.op != OP_NOMATCH)
2172 goto nomatch;
2173 break;
2174 }
2175 case TK_M_IMPORT_BUILTIN: {
2176 char command[UTIL_PATH_SIZE];
2177
2178 if (udev_builtin_run_once(cur->key.builtin_cmd)) {
2179 /* check if we ran already */
2180 if (event->builtin_run & (1 << cur->key.builtin_cmd)) {
9f6445e3 2181 log_debug("IMPORT builtin skip '%s' %s:%u",
baa30fbc 2182 udev_builtin_name(cur->key.builtin_cmd),
915bf0f6 2183 rules_str(rules, rule->rule.filename_off),
baa30fbc 2184 rule->rule.filename_line);
912541b0
KS
2185 /* return the result from earlier run */
2186 if (event->builtin_ret & (1 << cur->key.builtin_cmd))
2187 if (cur->key.op != OP_NOMATCH)
2188 goto nomatch;
2189 break;
2190 }
2191 /* mark as ran */
2192 event->builtin_run |= (1 << cur->key.builtin_cmd);
2193 }
2194
915bf0f6 2195 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), command, sizeof(command));
9f6445e3 2196 log_debug("IMPORT builtin '%s' %s:%u",
baa30fbc 2197 udev_builtin_name(cur->key.builtin_cmd),
915bf0f6 2198 rules_str(rules, rule->rule.filename_off),
baa30fbc 2199 rule->rule.filename_line);
912541b0
KS
2200
2201 if (udev_builtin_run(event->dev, cur->key.builtin_cmd, command, false) != 0) {
2202 /* remember failure */
9f6445e3 2203 log_debug("IMPORT builtin '%s' returned non-zero",
baa30fbc 2204 udev_builtin_name(cur->key.builtin_cmd));
912541b0
KS
2205 event->builtin_ret |= (1 << cur->key.builtin_cmd);
2206 if (cur->key.op != OP_NOMATCH)
2207 goto nomatch;
2208 }
2209 break;
2210 }
2211 case TK_M_IMPORT_DB: {
915bf0f6 2212 const char *key = rules_str(rules, cur->key.value_off);
912541b0
KS
2213 const char *value;
2214
2215 value = udev_device_get_property_value(event->dev_db, key);
df546eb5
TG
2216 if (value != NULL)
2217 udev_device_add_property(event->dev, key, value);
2218 else {
912541b0
KS
2219 if (cur->key.op != OP_NOMATCH)
2220 goto nomatch;
2221 }
2222 break;
2223 }
2224 case TK_M_IMPORT_CMDLINE: {
2225 FILE *f;
2226 bool imported = false;
2227
47ef94ac 2228 f = fopen("/proc/cmdline", "re");
912541b0
KS
2229 if (f != NULL) {
2230 char cmdline[4096];
2231
2232 if (fgets(cmdline, sizeof(cmdline), f) != NULL) {
915bf0f6 2233 const char *key = rules_str(rules, cur->key.value_off);
912541b0
KS
2234 char *pos;
2235
2236 pos = strstr(cmdline, key);
2237 if (pos != NULL) {
912541b0
KS
2238 pos += strlen(key);
2239 if (pos[0] == '\0' || isspace(pos[0])) {
2240 /* we import simple flags as 'FLAG=1' */
df546eb5 2241 udev_device_add_property(event->dev, key, "1");
912541b0
KS
2242 imported = true;
2243 } else if (pos[0] == '=') {
2244 const char *value;
2245
2246 pos++;
2247 value = pos;
2248 while (pos[0] != '\0' && !isspace(pos[0]))
2249 pos++;
2250 pos[0] = '\0';
df546eb5 2251 udev_device_add_property(event->dev, key, value);
912541b0
KS
2252 imported = true;
2253 }
2254 }
2255 }
2256 fclose(f);
2257 }
2258 if (!imported && cur->key.op != OP_NOMATCH)
2259 goto nomatch;
2260 break;
2261 }
2262 case TK_M_IMPORT_PARENT: {
2263 char import[UTIL_PATH_SIZE];
2264
915bf0f6 2265 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), import, sizeof(import));
912541b0
KS
2266 if (import_parent_into_properties(event->dev, import) != 0)
2267 if (cur->key.op != OP_NOMATCH)
2268 goto nomatch;
2269 break;
2270 }
2271 case TK_M_RESULT:
2272 if (match_key(rules, cur, event->program_result) != 0)
2273 goto nomatch;
2274 break;
2275 case TK_A_STRING_ESCAPE_NONE:
2276 esc = ESCAPE_NONE;
2277 break;
2278 case TK_A_STRING_ESCAPE_REPLACE:
2279 esc = ESCAPE_REPLACE;
2280 break;
2281 case TK_A_DB_PERSIST:
2282 udev_device_set_db_persist(event->dev);
2283 break;
2284 case TK_A_INOTIFY_WATCH:
2285 if (event->inotify_watch_final)
2286 break;
2287 if (cur->key.op == OP_ASSIGN_FINAL)
2288 event->inotify_watch_final = true;
2289 event->inotify_watch = cur->key.watch;
2290 break;
2291 case TK_A_DEVLINK_PRIO:
2292 udev_device_set_devlink_priority(event->dev, cur->key.devlink_prio);
2293 break;
2294 case TK_A_OWNER: {
2295 char owner[UTIL_NAME_SIZE];
23bf8dd7
TG
2296 const char *ow = owner;
2297 int r;
912541b0
KS
2298
2299 if (event->owner_final)
2300 break;
2301 if (cur->key.op == OP_ASSIGN_FINAL)
2302 event->owner_final = true;
915bf0f6 2303 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), owner, sizeof(owner));
1edefa4f 2304 event->owner_set = true;
23bf8dd7
TG
2305 r = get_user_creds(&ow, &event->uid, NULL, NULL, NULL);
2306 if (r < 0) {
2307 if (r == -ENOENT || r == -ESRCH)
ff49bc32 2308 log_error("specified user '%s' unknown", owner);
23bf8dd7 2309 else
279d3c9c 2310 log_error_errno(r, "error resolving user '%s': %m", owner);
23bf8dd7
TG
2311
2312 event->uid = 0;
2313 }
9f6445e3 2314 log_debug("OWNER %u %s:%u",
baa30fbc 2315 event->uid,
915bf0f6 2316 rules_str(rules, rule->rule.filename_off),
baa30fbc 2317 rule->rule.filename_line);
912541b0
KS
2318 break;
2319 }
2320 case TK_A_GROUP: {
2321 char group[UTIL_NAME_SIZE];
23bf8dd7
TG
2322 const char *gr = group;
2323 int r;
912541b0
KS
2324
2325 if (event->group_final)
2326 break;
2327 if (cur->key.op == OP_ASSIGN_FINAL)
2328 event->group_final = true;
915bf0f6 2329 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), group, sizeof(group));
1edefa4f 2330 event->group_set = true;
23bf8dd7
TG
2331 r = get_group_creds(&gr, &event->gid);
2332 if (r < 0) {
2333 if (r == -ENOENT || r == -ESRCH)
ff49bc32 2334 log_error("specified group '%s' unknown", group);
23bf8dd7 2335 else
279d3c9c 2336 log_error_errno(r, "error resolving group '%s': %m", group);
23bf8dd7
TG
2337
2338 event->gid = 0;
2339 }
9f6445e3 2340 log_debug("GROUP %u %s:%u",
baa30fbc 2341 event->gid,
915bf0f6 2342 rules_str(rules, rule->rule.filename_off),
baa30fbc 2343 rule->rule.filename_line);
912541b0
KS
2344 break;
2345 }
2346 case TK_A_MODE: {
2347 char mode_str[UTIL_NAME_SIZE];
2348 mode_t mode;
2349 char *endptr;
2350
2351 if (event->mode_final)
2352 break;
915bf0f6 2353 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), mode_str, sizeof(mode_str));
912541b0
KS
2354 mode = strtol(mode_str, &endptr, 8);
2355 if (endptr[0] != '\0') {
9f6445e3 2356 log_error("ignoring invalid mode '%s'", mode_str);
912541b0
KS
2357 break;
2358 }
2359 if (cur->key.op == OP_ASSIGN_FINAL)
2360 event->mode_final = true;
2361 event->mode_set = true;
2362 event->mode = mode;
9f6445e3 2363 log_debug("MODE %#o %s:%u",
baa30fbc 2364 event->mode,
915bf0f6 2365 rules_str(rules, rule->rule.filename_off),
baa30fbc 2366 rule->rule.filename_line);
912541b0
KS
2367 break;
2368 }
2369 case TK_A_OWNER_ID:
2370 if (event->owner_final)
2371 break;
2372 if (cur->key.op == OP_ASSIGN_FINAL)
2373 event->owner_final = true;
1edefa4f 2374 event->owner_set = true;
912541b0 2375 event->uid = cur->key.uid;
9f6445e3 2376 log_debug("OWNER %u %s:%u",
baa30fbc 2377 event->uid,
915bf0f6 2378 rules_str(rules, rule->rule.filename_off),
baa30fbc 2379 rule->rule.filename_line);
912541b0
KS
2380 break;
2381 case TK_A_GROUP_ID:
2382 if (event->group_final)
2383 break;
2384 if (cur->key.op == OP_ASSIGN_FINAL)
2385 event->group_final = true;
1edefa4f 2386 event->group_set = true;
912541b0 2387 event->gid = cur->key.gid;
9f6445e3 2388 log_debug("GROUP %u %s:%u",
baa30fbc 2389 event->gid,
915bf0f6 2390 rules_str(rules, rule->rule.filename_off),
baa30fbc 2391 rule->rule.filename_line);
912541b0
KS
2392 break;
2393 case TK_A_MODE_ID:
2394 if (event->mode_final)
2395 break;
2396 if (cur->key.op == OP_ASSIGN_FINAL)
2397 event->mode_final = true;
2398 event->mode_set = true;
2399 event->mode = cur->key.mode;
9f6445e3 2400 log_debug("MODE %#o %s:%u",
baa30fbc 2401 event->mode,
915bf0f6 2402 rules_str(rules, rule->rule.filename_off),
baa30fbc 2403 rule->rule.filename_line);
912541b0 2404 break;
c26547d6
KS
2405 case TK_A_SECLABEL: {
2406 const char *name, *label;
2407
2408 name = rules_str(rules, cur->key.attr_off);
2409 label = rules_str(rules, cur->key.value_off);
2410 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2411 udev_list_cleanup(&event->seclabel_list);
2412 udev_list_entry_add(&event->seclabel_list, name, label);
9f6445e3 2413 log_debug("SECLABEL{%s}='%s' %s:%u",
c26547d6
KS
2414 name, label,
2415 rules_str(rules, rule->rule.filename_off),
2416 rule->rule.filename_line);
2417 break;
2418 }
912541b0 2419 case TK_A_ENV: {
915bf0f6
KS
2420 const char *name = rules_str(rules, cur->key.attr_off);
2421 char *value = rules_str(rules, cur->key.value_off);
07845c14
KS
2422 char value_new[UTIL_NAME_SIZE];
2423 const char *value_old = NULL;
912541b0 2424
07845c14
KS
2425 if (value[0] == '\0') {
2426 if (cur->key.op == OP_ADD)
2427 break;
912541b0 2428 udev_device_add_property(event->dev, name, NULL);
07845c14 2429 break;
912541b0 2430 }
07845c14
KS
2431
2432 if (cur->key.op == OP_ADD)
2433 value_old = udev_device_get_property_value(event->dev, name);
2434 if (value_old) {
2435 char temp[UTIL_NAME_SIZE];
2436
2437 /* append value separated by space */
2438 udev_event_apply_format(event, value, temp, sizeof(temp));
d5a89d7d 2439 strscpyl(value_new, sizeof(value_new), value_old, " ", temp, NULL);
07845c14
KS
2440 } else
2441 udev_event_apply_format(event, value, value_new, sizeof(value_new));
2442
df546eb5 2443 udev_device_add_property(event->dev, name, value_new);
912541b0
KS
2444 break;
2445 }
2446 case TK_A_TAG: {
2447 char tag[UTIL_PATH_SIZE];
2448 const char *p;
2449
915bf0f6 2450 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), tag, sizeof(tag));
912541b0
KS
2451 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2452 udev_device_cleanup_tags_list(event->dev);
2453 for (p = tag; *p != '\0'; p++) {
2454 if ((*p >= 'a' && *p <= 'z') ||
2455 (*p >= 'A' && *p <= 'Z') ||
2456 (*p >= '0' && *p <= '9') ||
2457 *p == '-' || *p == '_')
2458 continue;
9f6445e3 2459 log_error("ignoring invalid tag name '%s'", tag);
912541b0
KS
2460 break;
2461 }
8e3ba377
DH
2462 if (cur->key.op == OP_REMOVE)
2463 udev_device_remove_tag(event->dev, tag);
2464 else
2465 udev_device_add_tag(event->dev, tag);
912541b0
KS
2466 break;
2467 }
2468 case TK_A_NAME: {
915bf0f6 2469 const char *name = rules_str(rules, cur->key.value_off);
0ecfcbd4 2470
912541b0
KS
2471 char name_str[UTIL_PATH_SIZE];
2472 int count;
2473
2474 if (event->name_final)
2475 break;
2476 if (cur->key.op == OP_ASSIGN_FINAL)
2477 event->name_final = true;
2478 udev_event_apply_format(event, name, name_str, sizeof(name_str));
2479 if (esc == ESCAPE_UNSET || esc == ESCAPE_REPLACE) {
2480 count = util_replace_chars(name_str, "/");
2481 if (count > 0)
9f6445e3 2482 log_debug("%i character(s) replaced", count);
912541b0 2483 }
6ada823a 2484 if (major(udev_device_get_devnum(event->dev)) &&
4cb72937 2485 (!streq(name_str, udev_device_get_devnode(event->dev) + strlen("/dev/")))) {
6ada823a
KS
2486 log_error("NAME=\"%s\" ignored, kernel device nodes "
2487 "can not be renamed; please fix it in %s:%u\n", name,
915bf0f6 2488 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
6ada823a 2489 break;
0ecfcbd4 2490 }
912541b0
KS
2491 free(event->name);
2492 event->name = strdup(name_str);
9f6445e3 2493 log_debug("NAME '%s' %s:%u",
baa30fbc 2494 event->name,
915bf0f6 2495 rules_str(rules, rule->rule.filename_off),
baa30fbc 2496 rule->rule.filename_line);
912541b0
KS
2497 break;
2498 }
2499 case TK_A_DEVLINK: {
2500 char temp[UTIL_PATH_SIZE];
2501 char filename[UTIL_PATH_SIZE];
2502 char *pos, *next;
2503 int count = 0;
2504
2505 if (event->devlink_final)
2506 break;
2507 if (major(udev_device_get_devnum(event->dev)) == 0)
2508 break;
2509 if (cur->key.op == OP_ASSIGN_FINAL)
2510 event->devlink_final = true;
2511 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2512 udev_device_cleanup_devlinks_list(event->dev);
2513
2514 /* allow multiple symlinks separated by spaces */
915bf0f6 2515 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), temp, sizeof(temp));
912541b0
KS
2516 if (esc == ESCAPE_UNSET)
2517 count = util_replace_chars(temp, "/ ");
2518 else if (esc == ESCAPE_REPLACE)
2519 count = util_replace_chars(temp, "/");
2520 if (count > 0)
9f6445e3 2521 log_debug("%i character(s) replaced" , count);
912541b0
KS
2522 pos = temp;
2523 while (isspace(pos[0]))
2524 pos++;
2525 next = strchr(pos, ' ');
2526 while (next != NULL) {
2527 next[0] = '\0';
9f6445e3 2528 log_debug("LINK '%s' %s:%u", pos,
915bf0f6 2529 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
d5a89d7d 2530 strscpyl(filename, sizeof(filename), "/dev/", pos, NULL);
8a173387 2531 udev_device_add_devlink(event->dev, filename);
912541b0
KS
2532 while (isspace(next[1]))
2533 next++;
2534 pos = &next[1];
2535 next = strchr(pos, ' ');
2536 }
2537 if (pos[0] != '\0') {
9f6445e3 2538 log_debug("LINK '%s' %s:%u", pos,
915bf0f6 2539 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
d5a89d7d 2540 strscpyl(filename, sizeof(filename), "/dev/", pos, NULL);
8a173387 2541 udev_device_add_devlink(event->dev, filename);
912541b0
KS
2542 }
2543 break;
2544 }
2545 case TK_A_ATTR: {
915bf0f6 2546 const char *key_name = rules_str(rules, cur->key.attr_off);
912541b0
KS
2547 char attr[UTIL_PATH_SIZE];
2548 char value[UTIL_NAME_SIZE];
2549 FILE *f;
2550
2551 if (util_resolve_subsys_kernel(event->udev, key_name, attr, sizeof(attr), 0) != 0)
d5a89d7d 2552 strscpyl(attr, sizeof(attr), udev_device_get_syspath(event->dev), "/", key_name, NULL);
912541b0
KS
2553 attr_subst_subdir(attr, sizeof(attr));
2554
915bf0f6 2555 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), value, sizeof(value));
9f6445e3 2556 log_debug("ATTR '%s' writing '%s' %s:%u", attr, value,
915bf0f6 2557 rules_str(rules, rule->rule.filename_off),
baa30fbc 2558 rule->rule.filename_line);
47ef94ac 2559 f = fopen(attr, "we");
912541b0
KS
2560 if (f != NULL) {
2561 if (fprintf(f, "%s", value) <= 0)
56f64d95 2562 log_error_errno(errno, "error writing ATTR{%s}: %m", attr);
912541b0
KS
2563 fclose(f);
2564 } else {
56f64d95 2565 log_error_errno(errno, "error opening ATTR{%s} for writing: %m", attr);
912541b0
KS
2566 }
2567 break;
2568 }
f4cf2e5b
KS
2569 case TK_A_SYSCTL: {
2570 char filename[UTIL_PATH_SIZE];
2571 char value[UTIL_NAME_SIZE];
2572 int r;
2573
2574 udev_event_apply_format(event, rules_str(rules, cur->key.attr_off), filename, sizeof(filename));
2575 sysctl_normalize(filename);
2576 udev_event_apply_format(event, rules_str(rules, cur->key.value_off), value, sizeof(value));
2577 log_debug("SYSCTL '%s' writing '%s' %s:%u", filename, value,
2578 rules_str(rules, rule->rule.filename_off), rule->rule.filename_line);
2579 r = sysctl_write(filename, value);
2580 if (r < 0)
2581 log_error("error writing SYSCTL{%s}='%s': %s", filename, value, strerror(-r));
2582 break;
2583 }
83cd6b75
KS
2584 case TK_A_RUN_BUILTIN:
2585 case TK_A_RUN_PROGRAM: {
2586 struct udev_list_entry *entry;
2587
912541b0
KS
2588 if (cur->key.op == OP_ASSIGN || cur->key.op == OP_ASSIGN_FINAL)
2589 udev_list_cleanup(&event->run_list);
9f6445e3 2590 log_debug("RUN '%s' %s:%u",
915bf0f6
KS
2591 rules_str(rules, cur->key.value_off),
2592 rules_str(rules, rule->rule.filename_off),
baa30fbc 2593 rule->rule.filename_line);
915bf0f6 2594 entry = udev_list_entry_add(&event->run_list, rules_str(rules, cur->key.value_off), NULL);
83cd6b75 2595 udev_list_entry_set_num(entry, cur->key.builtin_cmd);
912541b0
KS
2596 break;
2597 }
2598 case TK_A_GOTO:
2599 if (cur->key.rule_goto == 0)
2600 break;
2601 cur = &rules->tokens[cur->key.rule_goto];
2602 continue;
2603 case TK_END:
2604 return 0;
2605
2606 case TK_M_PARENTS_MIN:
2607 case TK_M_PARENTS_MAX:
2608 case TK_M_MAX:
2609 case TK_UNSET:
9f6445e3 2610 log_error("wrong type %u", cur->type);
912541b0
KS
2611 goto nomatch;
2612 }
2613
2614 cur++;
2615 continue;
2616 nomatch:
2617 /* fast-forward to next rule */
2618 cur = rule + rule->rule.token_count;
912541b0 2619 }
6880b25d 2620}
761dfddc 2621
9ec6e95b 2622int udev_rules_apply_static_dev_perms(struct udev_rules *rules) {
912541b0
KS
2623 struct token *cur;
2624 struct token *rule;
2625 uid_t uid = 0;
2626 gid_t gid = 0;
2627 mode_t mode = 0;
84b6ad70
TG
2628 _cleanup_strv_free_ char **tags = NULL;
2629 char **t;
2630 FILE *f = NULL;
2631 _cleanup_free_ char *path = NULL;
2632 int r = 0;
912541b0
KS
2633
2634 if (rules->tokens == NULL)
84b6ad70 2635 return 0;
912541b0
KS
2636
2637 cur = &rules->tokens[0];
2638 rule = cur;
2639 for (;;) {
2640 switch (cur->type) {
2641 case TK_RULE:
2642 /* current rule */
2643 rule = cur;
2644
2645 /* skip rules without a static_node tag */
2646 if (!rule->rule.has_static_node)
2647 goto next;
2648
2649 uid = 0;
2650 gid = 0;
2651 mode = 0;
84b6ad70
TG
2652 strv_free(tags);
2653 tags = NULL;
912541b0
KS
2654 break;
2655 case TK_A_OWNER_ID:
2656 uid = cur->key.uid;
2657 break;
2658 case TK_A_GROUP_ID:
2659 gid = cur->key.gid;
2660 break;
2661 case TK_A_MODE_ID:
2662 mode = cur->key.mode;
84b6ad70
TG
2663 break;
2664 case TK_A_TAG:
2665 r = strv_extend(&tags, rules_str(rules, cur->key.value_off));
2666 if (r < 0)
2667 goto finish;
2668
912541b0
KS
2669 break;
2670 case TK_A_STATIC_NODE: {
84b6ad70
TG
2671 char device_node[UTIL_PATH_SIZE];
2672 char tags_dir[UTIL_PATH_SIZE];
2673 char tag_symlink[UTIL_PATH_SIZE];
912541b0
KS
2674 struct stat stats;
2675
2676 /* we assure, that the permissions tokens are sorted before the static token */
ca2bb160 2677
84b6ad70 2678 if (mode == 0 && uid == 0 && gid == 0 && tags == NULL)
912541b0 2679 goto next;
d6f116a7 2680
84b6ad70 2681 strscpyl(device_node, sizeof(device_node), "/dev/", rules_str(rules, cur->key.value_off), NULL);
ca2bb160
KS
2682 if (stat(device_node, &stats) != 0)
2683 break;
2684 if (!S_ISBLK(stats.st_mode) && !S_ISCHR(stats.st_mode))
2685 break;
84b6ad70 2686
d6f116a7 2687 /* export the tags to a directory as symlinks, allowing otherwise dead nodes to be tagged */
84b6ad70 2688 if (tags) {
84b6ad70
TG
2689 STRV_FOREACH(t, tags) {
2690 _cleanup_free_ char *unescaped_filename = NULL;
2691
2692 strscpyl(tags_dir, sizeof(tags_dir), "/run/udev/static_node-tags/", *t, "/", NULL);
2693 r = mkdir_p(tags_dir, 0755);
f647962d
MS
2694 if (r < 0)
2695 return log_error_errno(r, "failed to create %s: %m", tags_dir);
84b6ad70
TG
2696
2697 unescaped_filename = xescape(rules_str(rules, cur->key.value_off), "/.");
2698
2699 strscpyl(tag_symlink, sizeof(tag_symlink), tags_dir, unescaped_filename, NULL);
2700 r = symlink(device_node, tag_symlink);
4a62c710
MS
2701 if (r < 0 && errno != EEXIST)
2702 return log_error_errno(errno, "failed to create symlink %s -> %s: %m",
2703 tag_symlink, device_node);
2704 else
84b6ad70
TG
2705 r = 0;
2706 }
2707 }
2708
15a72200
TG
2709 /* don't touch the permissions if only the tags were set */
2710 if (mode == 0 && uid == 0 && gid == 0)
d6f116a7
KS
2711 break;
2712
912541b0
KS
2713 if (mode == 0) {
2714 if (gid > 0)
2715 mode = 0660;
2716 else
2717 mode = 0600;
2718 }
2719 if (mode != (stats.st_mode & 01777)) {
490f0087
TG
2720 r = chmod(device_node, mode);
2721 if (r < 0) {
9f6445e3 2722 log_error("failed to chmod '%s' %#o", device_node, mode);
490f0087
TG
2723 return -errno;
2724 } else
9f6445e3 2725 log_debug("chmod '%s' %#o", device_node, mode);
912541b0
KS
2726 }
2727
2728 if ((uid != 0 && uid != stats.st_uid) || (gid != 0 && gid != stats.st_gid)) {
490f0087
TG
2729 r = chown(device_node, uid, gid);
2730 if (r < 0) {
9f6445e3 2731 log_error("failed to chown '%s' %u %u ", device_node, uid, gid);
490f0087
TG
2732 return -errno;
2733 } else
9f6445e3 2734 log_debug("chown '%s' %u %u", device_node, uid, gid);
912541b0
KS
2735 }
2736
84b6ad70 2737 utimensat(AT_FDCWD, device_node, NULL, 0);
912541b0
KS
2738 break;
2739 }
2740 case TK_END:
84b6ad70 2741 goto finish;
912541b0
KS
2742 }
2743
2744 cur++;
2745 continue;
761dfddc 2746next:
912541b0
KS
2747 /* fast-forward to next rule */
2748 cur = rule + rule->rule.token_count;
2749 continue;
2750 }
84b6ad70
TG
2751
2752finish:
2753 if (f) {
2754 fflush(f);
2755 fchmod(fileno(f), 0644);
2756 if (ferror(f) || rename(path, "/run/udev/static_node-tags") < 0) {
2757 r = -errno;
2758 unlink("/run/udev/static_node-tags");
2759 unlink(path);
2760 }
2761 fclose(f);
2762 }
2763
2764 return r;
761dfddc 2765}