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