udev-rules.c 109 KB
Newer Older
1
/*
Kay Sievers's avatar
Kay Sievers committed
2
 * Copyright (C) 2003-2010 Kay Sievers <kay.sievers@vrfy.org>
3
 * Copyright (C) 2008 Alan Jenkins <alan-jenkins@tuffmail.co.uk>
4
 *
Kay Sievers's avatar
Kay Sievers committed
5
6
7
8
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
9
 *
Kay Sievers's avatar
Kay Sievers committed
10
11
12
13
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14
 *
Kay Sievers's avatar
Kay Sievers committed
15
16
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
19
 */

#include <stddef.h>
20
#include <limits.h>
21
#include <stdlib.h>
22
#include <stdbool.h>
23
24
25
26
27
28
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <ctype.h>
#include <unistd.h>
#include <errno.h>
29
#include <dirent.h>
30
#include <fnmatch.h>
31
#include <time.h>
32
33

#include "udev.h"
Kay Sievers's avatar
Kay Sievers committed
34
#include "path-util.h"
Kay Sievers's avatar
Kay Sievers committed
35
#include "conf-files.h"
36

37
38
39
#define PREALLOC_TOKEN          2048
#define PREALLOC_STRBUF         32 * 1024
#define PREALLOC_TRIE           256
40

41
struct uid_gid {
42
43
        unsigned int name_off;
        union {
44
45
                uid_t uid;
                gid_t gid;
46
        };
47
48
};

49
struct trie_node {
50
51
52
53
54
55
56
57
58
        /* this node's first child */
        unsigned int child_idx;
        /* the next child of our parent node's child list */
        unsigned int next_child_idx;
        /* this node's last child (shortcut for append) */
        unsigned int last_child_idx;
        unsigned int value_off;
        unsigned short value_len;
        unsigned char key;
59
60
};

61
struct udev_rules {
62
        struct udev *udev;
63
64
        char **dirs;
        unsigned long long *dirs_ts_usec;
65
66
67
68
69
70
71
72
73
74
75
76
77
        int resolve_names;

        /* every key in the rules file becomes a token */
        struct token *tokens;
        unsigned int token_cur;
        unsigned int token_max;

        /* all key strings are copied to a single string buffer */
        char *buf;
        size_t buf_cur;
        size_t buf_max;
        unsigned int buf_count;

78
        /* during rule parsing, strings are indexed and de-duplicated */
79
80
81
82
83
84
85
86
87
88
89
        struct trie_node *trie_nodes;
        unsigned int trie_nodes_cur;
        unsigned int trie_nodes_max;

        /* during rule parsing, uid/gid lookup results are cached */
        struct uid_gid *uids;
        unsigned int uids_cur;
        unsigned int uids_max;
        struct uid_gid *gids;
        unsigned int gids_cur;
        unsigned int gids_max;
90
91
};

92
/* KEY=="", KEY!="", KEY+="", KEY="", KEY:="" */
93
enum operation_type {
94
        OP_UNSET,
95

96
97
98
        OP_MATCH,
        OP_NOMATCH,
        OP_MATCH_MAX,
99

100
101
102
        OP_ADD,
        OP_ASSIGN,
        OP_ASSIGN_FINAL,
103
104
};

105
enum string_glob_type {
106
        GL_UNSET,
107
        GL_PLAIN,                       /* no special chars */
108
        GL_GLOB,                        /* shell globs ?,*,[] */
109
110
111
        GL_SPLIT,                       /* multi-value A|B */
        GL_SPLIT_GLOB,                  /* multi-value with glob A*|B* */
        GL_SOMETHING,                   /* commonly used "?*" */
112
113
};

114
enum string_subst_type {
115
116
117
118
        SB_UNSET,
        SB_NONE,
        SB_FORMAT,
        SB_SUBSYS,
119
120
};

121
/* tokens of a rule are sorted/handled in this order */
122
enum token_type {
123
124
125
        TK_UNSET,
        TK_RULE,

126
127
128
129
130
131
132
133
134
135
136
        TK_M_ACTION,                    /* val */
        TK_M_DEVPATH,                   /* val */
        TK_M_KERNEL,                    /* val */
        TK_M_DEVLINK,                   /* val */
        TK_M_NAME,                      /* val */
        TK_M_ENV,                       /* val, attr */
        TK_M_TAG,                       /* val */
        TK_M_SUBSYSTEM,                 /* val */
        TK_M_DRIVER,                    /* val */
        TK_M_WAITFOR,                   /* val */
        TK_M_ATTR,                      /* val, attr */
137
138

        TK_M_PARENTS_MIN,
139
        TK_M_KERNELS,                   /* val */
140
        TK_M_SUBSYSTEMS,                /* val */
141
142
143
        TK_M_DRIVERS,                   /* val */
        TK_M_ATTRS,                     /* val, attr */
        TK_M_TAGS,                      /* val */
144
145
        TK_M_PARENTS_MAX,

146
147
148
149
150
151
152
153
154
155
        TK_M_TEST,                      /* val, mode_t */
        TK_M_EVENT_TIMEOUT,             /* int */
        TK_M_PROGRAM,                   /* val */
        TK_M_IMPORT_FILE,               /* val */
        TK_M_IMPORT_PROG,               /* val */
        TK_M_IMPORT_BUILTIN,            /* val */
        TK_M_IMPORT_DB,                 /* val */
        TK_M_IMPORT_CMDLINE,            /* val */
        TK_M_IMPORT_PARENT,             /* val */
        TK_M_RESULT,                    /* val */
156
157
158
159
160
        TK_M_MAX,

        TK_A_STRING_ESCAPE_NONE,
        TK_A_STRING_ESCAPE_REPLACE,
        TK_A_DB_PERSIST,
161
162
163
164
165
166
167
168
169
170
171
172
173
174
        TK_A_INOTIFY_WATCH,             /* int */
        TK_A_DEVLINK_PRIO,              /* int */
        TK_A_OWNER,                     /* val */
        TK_A_GROUP,                     /* val */
        TK_A_MODE,                      /* val */
        TK_A_OWNER_ID,                  /* uid_t */
        TK_A_GROUP_ID,                  /* gid_t */
        TK_A_MODE_ID,                   /* mode_t */
        TK_A_STATIC_NODE,               /* val */
        TK_A_ENV,                       /* val, attr */
        TK_A_TAG,                       /* val */
        TK_A_NAME,                      /* val */
        TK_A_DEVLINK,                   /* val */
        TK_A_ATTR,                      /* val, attr */
175
176
        TK_A_RUN_BUILTIN,               /* val, bool */
        TK_A_RUN_PROGRAM,               /* val, bool */
177
        TK_A_GOTO,                      /* size_t */
178
179

        TK_END,
180
181
};

182
/* we try to pack stuff in a way that we take only 12 bytes per token */
183
struct token {
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
        union {
                unsigned char type;                /* same in rule and key */
                struct {
                        enum token_type type:8;
                        bool can_set_name:1;
                        bool has_static_node:1;
                        unsigned int unused:6;
                        unsigned short token_count;
                        unsigned int label_off;
                        unsigned short filename_off;
                        unsigned short filename_line;
                } rule;
                struct {
                        enum token_type type:8;
                        enum operation_type op:8;
                        enum string_glob_type glob:8;
                        enum string_subst_type subst:4;
                        enum string_subst_type attrsubst:4;
                        unsigned int value_off;
                        union {
                                unsigned int attr_off;
                                int devlink_unique;
                                unsigned int rule_goto;
                                mode_t  mode;
                                uid_t uid;
                                gid_t gid;
                                int devlink_prio;
                                int event_timeout;
                                int watch;
                                enum udev_builtin_cmd builtin_cmd;
                        };
                } key;
        };
217
218
};

219
#define MAX_TK                64
220
struct rule_tmp {
221
222
223
224
        struct udev_rules *rules;
        struct token rule;
        struct token token[MAX_TK];
        unsigned int token_cur;
225
226
};

227
#ifdef DEBUG
228
229
static const char *operation_str(enum operation_type type)
{
230
        static const char *operation_strs[] = {
231
232
233
                [OP_UNSET] =            "UNSET",
                [OP_MATCH] =            "match",
                [OP_NOMATCH] =          "nomatch",
234
235
                [OP_MATCH_MAX] =        "MATCH_MAX",

236
237
238
                [OP_ADD] =              "add",
                [OP_ASSIGN] =           "assign",
                [OP_ASSIGN_FINAL] =     "assign-final",
239
240
241
}        ;

        return operation_strs[type];
242
}
243

244
245
static const char *string_glob_str(enum string_glob_type type)
{
246
        static const char *string_glob_strs[] = {
247
248
249
250
251
252
                [GL_UNSET] =            "UNSET",
                [GL_PLAIN] =            "plain",
                [GL_GLOB] =             "glob",
                [GL_SPLIT] =            "split",
                [GL_SPLIT_GLOB] =       "split-glob",
                [GL_SOMETHING] =        "split-glob",
253
254
255
        };

        return string_glob_strs[type];
256
257
258
259
}

static const char *token_str(enum token_type type)
{
260
        static const char *token_strs[] = {
261
262
                [TK_UNSET] =                    "UNSET",
                [TK_RULE] =                     "RULE",
263

264
                [TK_M_ACTION] =                 "M ACTION",
265
                [TK_M_DEVPATH] =                "M DEVPATH",
266
                [TK_M_KERNEL] =                 "M KERNEL",
267
                [TK_M_DEVLINK] =                "M DEVLINK",
268
269
270
271
272
                [TK_M_NAME] =                   "M NAME",
                [TK_M_ENV] =                    "M ENV",
                [TK_M_TAG] =                    "M TAG",
                [TK_M_SUBSYSTEM] =              "M SUBSYSTEM",
                [TK_M_DRIVER] =                 "M DRIVER",
273
                [TK_M_WAITFOR] =                "M WAITFOR",
274
                [TK_M_ATTR] =                   "M ATTR",
275

276
                [TK_M_PARENTS_MIN] =            "M PARENTS_MIN",
277
                [TK_M_KERNELS] =                "M KERNELS",
278
                [TK_M_SUBSYSTEMS] =             "M SUBSYSTEMS",
279
                [TK_M_DRIVERS] =                "M DRIVERS",
280
281
282
                [TK_M_ATTRS] =                  "M ATTRS",
                [TK_M_TAGS] =                   "M TAGS",
                [TK_M_PARENTS_MAX] =            "M PARENTS_MAX",
283

284
285
                [TK_M_TEST] =                   "M TEST",
                [TK_M_EVENT_TIMEOUT] =          "M EVENT_TIMEOUT",
286
                [TK_M_PROGRAM] =                "M PROGRAM",
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
                [TK_M_IMPORT_FILE] =            "M IMPORT_FILE",
                [TK_M_IMPORT_PROG] =            "M IMPORT_PROG",
                [TK_M_IMPORT_BUILTIN] =         "M IMPORT_BUILTIN",
                [TK_M_IMPORT_DB] =              "M IMPORT_DB",
                [TK_M_IMPORT_CMDLINE] =         "M IMPORT_CMDLINE",
                [TK_M_IMPORT_PARENT] =          "M IMPORT_PARENT",
                [TK_M_RESULT] =                 "M RESULT",
                [TK_M_MAX] =                    "M MAX",

                [TK_A_STRING_ESCAPE_NONE] =     "A STRING_ESCAPE_NONE",
                [TK_A_STRING_ESCAPE_REPLACE] =  "A STRING_ESCAPE_REPLACE",
                [TK_A_DB_PERSIST] =             "A DB_PERSIST",
                [TK_A_INOTIFY_WATCH] =          "A INOTIFY_WATCH",
                [TK_A_DEVLINK_PRIO] =           "A DEVLINK_PRIO",
                [TK_A_OWNER] =                  "A OWNER",
                [TK_A_GROUP] =                  "A GROUP",
                [TK_A_MODE] =                   "A MODE",
                [TK_A_OWNER_ID] =               "A OWNER_ID",
                [TK_A_GROUP_ID] =               "A GROUP_ID",
                [TK_A_STATIC_NODE] =            "A STATIC_NODE",
307
                [TK_A_MODE_ID] =                "A MODE_ID",
308
309
310
                [TK_A_ENV] =                    "A ENV",
                [TK_A_TAG] =                    "A ENV",
                [TK_A_NAME] =                   "A NAME",
311
                [TK_A_DEVLINK] =                "A DEVLINK",
312
                [TK_A_ATTR] =                   "A ATTR",
313
314
                [TK_A_RUN_BUILTIN] =            "A RUN_BUILTIN",
                [TK_A_RUN_PROGRAM] =            "A RUN_PROGRAM",
315
                [TK_A_GOTO] =                   "A GOTO",
316

317
                [TK_END] =                      "END",
318
319
320
        };

        return token_strs[type];
321
}
322

323
324
static void dump_token(struct udev_rules *rules, struct token *token)
{
325
326
327
328
329
330
331
332
333
334
335
336
337
        enum token_type type = token->type;
        enum operation_type op = token->key.op;
        enum string_glob_type glob = token->key.glob;
        const char *value = &rules->buf[token->key.value_off];
        const char *attr = &rules->buf[token->key.attr_off];

        switch (type) {
        case TK_RULE:
                {
                        const char *tks_ptr = (char *)rules->tokens;
                        const char *tk_ptr = (char *)token;
                        unsigned int idx = (tk_ptr - tks_ptr) / sizeof(struct token);

338
339
340
341
                        log_debug("* RULE %s:%u, token: %u, count: %u, label: '%s'\n",
                                  &rules->buf[token->rule.filename_off], token->rule.filename_line,
                                  idx, token->rule.token_count,
                                  &rules->buf[token->rule.label_off]);
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
                        break;
                }
        case TK_M_ACTION:
        case TK_M_DEVPATH:
        case TK_M_KERNEL:
        case TK_M_SUBSYSTEM:
        case TK_M_DRIVER:
        case TK_M_WAITFOR:
        case TK_M_DEVLINK:
        case TK_M_NAME:
        case TK_M_KERNELS:
        case TK_M_SUBSYSTEMS:
        case TK_M_DRIVERS:
        case TK_M_TAGS:
        case TK_M_PROGRAM:
        case TK_M_IMPORT_FILE:
        case TK_M_IMPORT_PROG:
        case TK_M_IMPORT_DB:
        case TK_M_IMPORT_CMDLINE:
        case TK_M_IMPORT_PARENT:
        case TK_M_RESULT:
        case TK_A_NAME:
        case TK_A_DEVLINK:
        case TK_A_OWNER:
        case TK_A_GROUP:
        case TK_A_MODE:
368
369
        case TK_A_RUN_BUILTIN:
        case TK_A_RUN_PROGRAM:
370
371
                log_debug("%s %s '%s'(%s)\n",
                          token_str(type), operation_str(op), value, string_glob_str(glob));
372
373
                break;
        case TK_M_IMPORT_BUILTIN:
374
                log_debug("%s %i '%s'\n", token_str(type), token->key.builtin_cmd, value);
375
376
377
378
379
380
                break;
        case TK_M_ATTR:
        case TK_M_ATTRS:
        case TK_M_ENV:
        case TK_A_ATTR:
        case TK_A_ENV:
381
382
                log_debug("%s %s '%s' '%s'(%s)\n",
                          token_str(type), operation_str(op), attr, value, string_glob_str(glob));
383
384
385
                break;
        case TK_M_TAG:
        case TK_A_TAG:
386
                log_debug("%s %s '%s'\n", token_str(type), operation_str(op), value);
387
388
389
390
                break;
        case TK_A_STRING_ESCAPE_NONE:
        case TK_A_STRING_ESCAPE_REPLACE:
        case TK_A_DB_PERSIST:
391
                log_debug("%s\n", token_str(type));
392
393
                break;
        case TK_M_TEST:
394
395
                log_debug("%s %s '%s'(%s) %#o\n",
                          token_str(type), operation_str(op), value, string_glob_str(glob), token->key.mode);
396
397
                break;
        case TK_A_INOTIFY_WATCH:
398
                log_debug("%s %u\n", token_str(type), token->key.watch);
399
400
                break;
        case TK_A_DEVLINK_PRIO:
401
                log_debug("%s %u\n", token_str(type), token->key.devlink_prio);
402
403
                break;
        case TK_A_OWNER_ID:
404
                log_debug("%s %s %u\n", token_str(type), operation_str(op), token->key.uid);
405
406
                break;
        case TK_A_GROUP_ID:
407
                log_debug("%s %s %u\n", token_str(type), operation_str(op), token->key.gid);
408
409
                break;
        case TK_A_MODE_ID:
410
                log_debug("%s %s %#o\n", token_str(type), operation_str(op), token->key.mode);
411
412
                break;
        case TK_A_STATIC_NODE:
413
                log_debug("%s '%s'\n", token_str(type), value);
414
415
                break;
        case TK_M_EVENT_TIMEOUT:
416
                log_debug("%s %u\n", token_str(type), token->key.event_timeout);
417
418
                break;
        case TK_A_GOTO:
419
                log_debug("%s '%s' %u\n", token_str(type), value, token->key.rule_goto);
420
421
                break;
        case TK_END:
422
                log_debug("* %s\n", token_str(type));
423
424
425
426
427
                break;
        case TK_M_PARENTS_MIN:
        case TK_M_PARENTS_MAX:
        case TK_M_MAX:
        case TK_UNSET:
428
                log_debug("unknown type %u\n", type);
429
430
                break;
        }
431
}
432

433
434
static void dump_rules(struct udev_rules *rules)
{
435
436
        unsigned int i;

437
438
439
440
441
        log_debug("dumping %u (%zu bytes) tokens, %u (%zu bytes) strings\n",
                  rules->token_cur,
                  rules->token_cur * sizeof(struct token),
                  rules->buf_count,
                  rules->buf_cur);
442
443
        for(i = 0; i < rules->token_cur; i++)
                dump_token(rules, &rules->tokens[i]);
444
445
}
#else
446
447
static inline const char *operation_str(enum operation_type type) { return NULL; }
static inline const char *token_str(enum token_type type) { return NULL; }
448
449
static inline void dump_token(struct udev_rules *rules, struct token *token) {}
static inline void dump_rules(struct udev_rules *rules) {}
450
#endif /* DEBUG */
451

452
static int add_new_string(struct udev_rules *rules, const char *str, size_t bytes)
453
{
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
        int off;

        /* grow buffer if needed */
        if (rules->buf_cur + bytes+1 >= rules->buf_max) {
                char *buf;
                unsigned int add;

                /* double the buffer size */
                add = rules->buf_max;
                if (add < bytes * 8)
                        add = bytes * 8;

                buf = realloc(rules->buf, rules->buf_max + add);
                if (buf == NULL)
                        return -1;
                rules->buf = buf;
                rules->buf_max += add;
        }
        off = rules->buf_cur;
        memcpy(&rules->buf[rules->buf_cur], str, bytes);
        rules->buf_cur += bytes;
        rules->buf_count++;
        return off;
477
478
}

479
static int add_string(struct udev_rules *rules, const char *str)
480
{
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
        unsigned int node_idx;
        struct trie_node *new_node;
        unsigned int new_node_idx;
        unsigned char key;
        unsigned short len;
        unsigned int depth;
        unsigned int off;
        struct trie_node *parent;

        /* walk trie, start from last character of str to find matching tails */
        len = strlen(str);
        key = str[len-1];
        node_idx = 0;
        for (depth = 0; depth <= len; depth++) {
                struct trie_node *node;
                unsigned int child_idx;

                node = &rules->trie_nodes[node_idx];
                off = node->value_off + node->value_len - len;

                /* match against current node */
                if (depth == len || (node->value_len >= len && memcmp(&rules->buf[off], str, len) == 0))
                        return off;

                /* lookup child node */
                key = str[len - 1 - depth];
                child_idx = node->child_idx;
                while (child_idx > 0) {
                        struct trie_node *child;

                        child = &rules->trie_nodes[child_idx];
                        if (child->key == key)
                                break;
                        child_idx = child->next_child_idx;
                }
                if (child_idx == 0)
                        break;
                node_idx = child_idx;
        }

        /* string not found, add it */
        off = add_new_string(rules, str, len + 1);

        /* grow trie nodes if needed */
        if (rules->trie_nodes_cur >= rules->trie_nodes_max) {
                struct trie_node *nodes;
                unsigned int add;

                /* double the buffer size */
                add = rules->trie_nodes_max;
                if (add < 8)
                        add = 8;

                nodes = realloc(rules->trie_nodes, (rules->trie_nodes_max + add) * sizeof(struct trie_node));
                if (nodes == NULL)
                        return -1;
                rules->trie_nodes = nodes;
                rules->trie_nodes_max += add;
        }

        /* get a new node */
        new_node_idx = rules->trie_nodes_cur;
        rules->trie_nodes_cur++;
        new_node = &rules->trie_nodes[new_node_idx];
        memset(new_node, 0x00, sizeof(struct trie_node));
        new_node->value_off = off;
        new_node->value_len = len;
        new_node->key = key;

        /* join the parent's child list */
        parent = &rules->trie_nodes[node_idx];
        if (parent->child_idx == 0) {
                parent->child_idx = new_node_idx;
        } else {
                struct trie_node *last_child;

                last_child = &rules->trie_nodes[parent->last_child_idx];
                last_child->next_child_idx = new_node_idx;
        }
        parent->last_child_idx = new_node_idx;
        return off;
562
563
564
565
}

static int add_token(struct udev_rules *rules, struct token *token)
{
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
        /* grow buffer if needed */
        if (rules->token_cur+1 >= rules->token_max) {
                struct token *tokens;
                unsigned int add;

                /* double the buffer size */
                add = rules->token_max;
                if (add < 8)
                        add = 8;

                tokens = realloc(rules->tokens, (rules->token_max + add ) * sizeof(struct token));
                if (tokens == NULL)
                        return -1;
                rules->tokens = tokens;
                rules->token_max += add;
        }
        memcpy(&rules->tokens[rules->token_cur], token, sizeof(struct token));
        rules->token_cur++;
        return 0;
585
}
586

587
588
static uid_t add_uid(struct udev_rules *rules, const char *owner)
{
589
590
591
592
593
594
595
        unsigned int i;
        uid_t uid;
        unsigned int off;

        /* lookup, if we know it already */
        for (i = 0; i < rules->uids_cur; i++) {
                off = rules->uids[i].name_off;
596
                if (streq(&rules->buf[off], owner)) {
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
                        uid = rules->uids[i].uid;
                        return uid;
                }
        }
        uid = util_lookup_user(rules->udev, owner);

        /* grow buffer if needed */
        if (rules->uids_cur+1 >= rules->uids_max) {
                struct uid_gid *uids;
                unsigned int add;

                /* double the buffer size */
                add = rules->uids_max;
                if (add < 1)
                        add = 8;

                uids = realloc(rules->uids, (rules->uids_max + add ) * sizeof(struct uid_gid));
                if (uids == NULL)
                        return uid;
                rules->uids = uids;
                rules->uids_max += add;
        }
        rules->uids[rules->uids_cur].uid = uid;
        off = add_string(rules, owner);
        if (off <= 0)
                return uid;
        rules->uids[rules->uids_cur].name_off = off;
        rules->uids_cur++;
        return uid;
626
627
628
629
}

static gid_t add_gid(struct udev_rules *rules, const char *group)
{
630
631
632
633
634
635
636
        unsigned int i;
        gid_t gid;
        unsigned int off;

        /* lookup, if we know it already */
        for (i = 0; i < rules->gids_cur; i++) {
                off = rules->gids[i].name_off;
637
                if (streq(&rules->buf[off], group)) {
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
                        gid = rules->gids[i].gid;
                        return gid;
                }
        }
        gid = util_lookup_group(rules->udev, group);

        /* grow buffer if needed */
        if (rules->gids_cur+1 >= rules->gids_max) {
                struct uid_gid *gids;
                unsigned int add;

                /* double the buffer size */
                add = rules->gids_max;
                if (add < 1)
                        add = 8;

                gids = realloc(rules->gids, (rules->gids_max + add ) * sizeof(struct uid_gid));
                if (gids == NULL)
                        return gid;
                rules->gids = gids;
                rules->gids_max += add;
        }
        rules->gids[rules->gids_cur].gid = gid;
        off = add_string(rules, group);
        if (off <= 0)
                return gid;
        rules->gids[rules->gids_cur].name_off = off;
        rules->gids_cur++;
        return gid;
667
668
}

669
static int import_property_from_string(struct udev_device *dev, char *line)
670
{
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
        char *key;
        char *val;
        size_t len;

        /* find key */
        key = line;
        while (isspace(key[0]))
                key++;

        /* comment or empty line */
        if (key[0] == '#' || key[0] == '\0')
                return -1;

        /* split key/value */
        val = strchr(key, '=');
        if (val == NULL)
                return -1;
        val[0] = '\0';
        val++;

        /* find value */
        while (isspace(val[0]))
                val++;

        /* terminate key */
        len = strlen(key);
        if (len == 0)
                return -1;
        while (isspace(key[len-1]))
                len--;
        key[len] = '\0';

        /* terminate value */
        len = strlen(val);
        if (len == 0)
                return -1;
        while (isspace(val[len-1]))
                len--;
        val[len] = '\0';

        if (len == 0)
                return -1;

        /* unquote */
        if (val[0] == '"' || val[0] == '\'') {
                if (val[len-1] != val[0]) {
717
                        log_debug("inconsistent quoting: '%s', skip\n", line);
718
719
720
721
722
723
724
                        return -1;
                }
                val[len-1] = '\0';
                val++;
        }

        /* handle device, renamed by external tool, returning new path */
725
        if (streq(key, "DEVPATH")) {
726
727
                char syspath[UTIL_PATH_SIZE];

728
729
                log_debug("updating devpath from '%s' to '%s'\n",
                          udev_device_get_devpath(dev), val);
730
                util_strscpyl(syspath, sizeof(syspath), "/sys", val, NULL);
731
732
733
734
735
736
737
738
739
740
                udev_device_set_syspath(dev, syspath);
        } else {
                struct udev_list_entry *entry;

                entry = udev_device_add_property(dev, key, val);
                /* store in db, skip private keys */
                if (key[0] != '.')
                        udev_list_entry_set_num(entry, true);
        }
        return 0;
741
742
}

743
static int import_file_into_properties(struct udev_device *dev, const char *filename)
744
{
745
746
747
        FILE *f;
        char line[UTIL_LINE_SIZE];

Kay Sievers's avatar
Kay Sievers committed
748
        f = fopen(filename, "re");
749
750
751
752
753
754
        if (f == NULL)
                return -1;
        while (fgets(line, sizeof(line), f) != NULL)
                import_property_from_string(dev, line);
        fclose(f);
        return 0;
755
756
}

Kay Sievers's avatar
Kay Sievers committed
757
static int import_program_into_properties(struct udev_event *event, const char *program, const sigset_t *sigmask)
758
{
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
        struct udev_device *dev = event->dev;
        char **envp;
        char result[UTIL_LINE_SIZE];
        char *line;
        int err;

        envp = udev_device_get_properties_envp(dev);
        err = udev_event_spawn(event, program, envp, sigmask, result, sizeof(result));
        if (err < 0)
                return err;

        line = result;
        while (line != NULL) {
                char *pos;

                pos = strchr(line, '\n');
                if (pos != NULL) {
                        pos[0] = '\0';
                        pos = &pos[1];
                }
                import_property_from_string(dev, line);
                line = pos;
        }
        return 0;
783
784
}

785
static int import_parent_into_properties(struct udev_device *dev, const char *filter)
786
{
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
        struct udev_device *dev_parent;
        struct udev_list_entry *list_entry;

        dev_parent = udev_device_get_parent(dev);
        if (dev_parent == NULL)
                return -1;

        udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(dev_parent)) {
                const char *key = udev_list_entry_get_name(list_entry);
                const char *val = udev_list_entry_get_value(list_entry);

                if (fnmatch(filter, key, 0) == 0) {
                        struct udev_list_entry *entry;

                        entry = udev_device_add_property(dev, key, val);
                        /* store in db, skip private keys */
                        if (key[0] != '.')
                                udev_list_entry_set_num(entry, true);
                }
        }
        return 0;
808
809
}

810
#define WAIT_LOOP_PER_SECOND                50
811
static int wait_for_file(struct udev_device *dev, const char *file, int timeout)
812
{
813
814
815
816
817
818
819
820
        char filepath[UTIL_PATH_SIZE];
        char devicepath[UTIL_PATH_SIZE];
        struct stat stats;
        int loop = timeout * WAIT_LOOP_PER_SECOND;

        /* a relative path is a device attribute */
        devicepath[0] = '\0';
        if (file[0] != '/') {
821
                util_strscpyl(devicepath, sizeof(devicepath), udev_device_get_syspath(dev), NULL);
822
823
824
825
826
827
828
829
830
                util_strscpyl(filepath, sizeof(filepath), devicepath, "/", file, NULL);
                file = filepath;
        }

        while (--loop) {
                const struct timespec duration = { 0, 1000 * 1000 * 1000 / WAIT_LOOP_PER_SECOND };

                /* lookup file */
                if (stat(file, &stats) == 0) {
831
                        log_debug("file '%s' appeared after %i loops\n", file, (timeout * WAIT_LOOP_PER_SECOND) - loop-1);
832
833
834
835
                        return 0;
                }
                /* make sure, the device did not disappear in the meantime */
                if (devicepath[0] != '\0' && stat(devicepath, &stats) != 0) {
836
                        log_debug("device disappeared while waiting for '%s'\n", file);
837
838
                        return -2;
                }
839
                log_debug("wait for '%s' for %i mseconds\n", file, 1000 / WAIT_LOOP_PER_SECOND);
840
841
                nanosleep(&duration, NULL);
        }
842
        log_debug("waiting for '%s' failed\n", file);
843
        return -1;
844
845
}

846
847
static int attr_subst_subdir(char *attr, size_t len)
{
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
        bool found = false;

        if (strstr(attr, "/*/")) {
                char *pos;
                char dirname[UTIL_PATH_SIZE];
                const char *tail;
                DIR *dir;

                util_strscpy(dirname, sizeof(dirname), attr);
                pos = strstr(dirname, "/*/");
                if (pos == NULL)
                        return -1;
                pos[0] = '\0';
                tail = &pos[2];
                dir = opendir(dirname);
                if (dir != NULL) {
                        struct dirent *dent;

                        for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
                                struct stat stats;

                                if (dent->d_name[0] == '.')
                                        continue;
                                util_strscpyl(attr, len, dirname, "/", dent->d_name, tail, NULL);
                                if (stat(attr, &stats) == 0) {
                                        found = true;
                                        break;
                                }
                        }
                        closedir(dir);
                }
        }

        return found;
882
883
}

884
static int get_key(struct udev *udev, char **line, char **key, enum operation_type *op, char **value)
Kay Sievers's avatar
Kay Sievers committed
885
{
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
        char *linepos;
        char *temp;

        linepos = *line;
        if (linepos == NULL || linepos[0] == '\0')
                return -1;

        /* skip whitespace */
        while (isspace(linepos[0]) || linepos[0] == ',')
                linepos++;

        /* get the key */
        if (linepos[0] == '\0')
                return -1;
        *key = linepos;

        for (;;) {
                linepos++;
                if (linepos[0] == '\0')
                        return -1;
                if (isspace(linepos[0]))
                        break;
                if (linepos[0] == '=')
                        break;
                if ((linepos[0] == '+') || (linepos[0] == '!') || (linepos[0] == ':'))
                        if (linepos[1] == '=')
                                break;
        }

        /* remember end of key */
        temp = linepos;

        /* skip whitespace after key */
        while (isspace(linepos[0]))
                linepos++;
        if (linepos[0] == '\0')
                return -1;

        /* get operation type */
        if (linepos[0] == '=' && linepos[1] == '=') {
                *op = OP_MATCH;
                linepos += 2;
        } else if (linepos[0] == '!' && linepos[1] == '=') {
                *op = OP_NOMATCH;
                linepos += 2;
        } else if (linepos[0] == '+' && linepos[1] == '=') {
                *op = OP_ADD;
                linepos += 2;
        } else if (linepos[0] == '=') {
                *op = OP_ASSIGN;
                linepos++;
        } else if (linepos[0] == ':' && linepos[1] == '=') {
                *op = OP_ASSIGN_FINAL;
                linepos += 2;
        } else
                return -1;

        /* terminate key */
        temp[0] = '\0';

        /* skip whitespace after operator */
        while (isspace(linepos[0]))
                linepos++;
        if (linepos[0] == '\0')
                return -1;

        /* get the value */
        if (linepos[0] == '"')
                linepos++;
        else
                return -1;
        *value = linepos;

        /* terminate */
        temp = strchr(linepos, '"');
        if (!temp)
                return -1;
        temp[0] = '\0';
        temp++;

        /* move line to next key */
        *line = temp;
        return 0;
969
}
970

971
/* extract possible KEY{attr} */
972
static const char *get_key_attribute(struct udev *udev, char *str)
973
{
974
975
976
977
978
979
980
981
        char *pos;
        char *attr;

        attr = strchr(str, '{');
        if (attr != NULL) {
                attr++;
                pos = strchr(attr, '}');
                if (pos == NULL) {
982
                        log_error("missing closing brace for format\n");
983
984
985
986
987
988
                        return NULL;
                }
                pos[0] = '\0';
                return attr;
        }
        return NULL;
989
}
990

991
static int rule_add_key(struct rule_tmp *rule_tmp, enum token_type type,
992
993
                        enum operation_type op,
                        const char *value, const void *data)
994
{
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
        struct token *token = &rule_tmp->token[rule_tmp->token_cur];
        const char *attr = NULL;

        memset(token, 0x00, sizeof(struct token));

        switch (type) {
        case TK_M_ACTION:
        case TK_M_DEVPATH:
        case TK_M_KERNEL:
        case TK_M_SUBSYSTEM:
        case TK_M_DRIVER:
        case TK_M_WAITFOR:
        case TK_M_DEVLINK:
        case TK_M_NAME:
        case TK_M_KERNELS:
        case TK_M_SUBSYSTEMS:
        case TK_M_DRIVERS:
        case TK_M_TAGS:
        case TK_M_PROGRAM:
        case TK_M_IMPORT_FILE:
        case TK_M_IMPORT_PROG:
        case TK_M_IMPORT_DB:
        case TK_M_IMPORT_CMDLINE:
        case TK_M_IMPORT_PARENT:
        case TK_M_RESULT:
        case TK_A_OWNER:
        case TK_A_GROUP:
        case TK_A_MODE:
        case TK_A_NAME:
        case TK_A_GOTO:
        case TK_M_TAG:
        case TK_A_TAG:
                token->key.value_off = add_string(rule_tmp->rules, value);
                break;
        case TK_M_IMPORT_BUILTIN:
                token->key.value_off = add_string(rule_tmp->rules, value);
                token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
                break;
        case TK_M_ENV:
        case TK_M_ATTR:
        case TK_M_ATTRS:
        case TK_A_ATTR:
        case TK_A_ENV:
                attr = data;
                token->key.value_off = add_string(rule_tmp->rules, value);
                token->key.attr_off = add_string(rule_tmp->rules, attr);
                break;
        case TK_A_DEVLINK:
                token->key.value_off = add_string(rule_tmp->rules, value);
                token->key.devlink_unique = *(int *)data;
                break;
        case TK_M_TEST:
                token->key.value_off = add_string(rule_tmp->rules, value);
                if (data != NULL)
                        token->key.mode = *(mode_t *)data;
                break;
        case TK_A_STRING_ESCAPE_NONE:
        case TK_A_STRING_ESCAPE_REPLACE:
        case TK_A_DB_PERSIST:
                break;
1055
1056
        case TK_A_RUN_BUILTIN:
        case TK_A_RUN_PROGRAM:
1057
                token->key.builtin_cmd = *(enum udev_builtin_cmd *)data;
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
                token->key.value_off = add_string(rule_tmp->rules, value);
                break;
        case TK_A_INOTIFY_WATCH:
        case TK_A_DEVLINK_PRIO:
                token->key.devlink_prio = *(int *)data;
                break;
        case TK_A_OWNER_ID:
                token->key.uid = *(uid_t *)data;
                break;
        case TK_A_GROUP_ID:
                token->key.gid = *(gid_t *)data;
                break;
        case TK_A_MODE_ID:
                token->key.mode = *(mode_t *)data;
                break;
        case TK_A_STATIC_NODE:
                token->key.value_off = add_string(rule_tmp->rules, value);
                break;
        case TK_M_EVENT_TIMEOUT:
                token->key.event_timeout = *(int *)data;
                break;
        case TK_RULE:
        case TK_M_PARENTS_MIN:
        case TK_M_PARENTS_MAX:
        case TK_M_MAX:
        case TK_END:
        case TK_UNSET:
1085
                log_error("wrong type %u\n", type);
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
                return -1;
        }

        if (value != NULL && type < TK_M_MAX) {
                /* check if we need to split or call fnmatch() while matching rules */
                enum string_glob_type glob;
                int has_split;
                int has_glob;

                has_split = (strchr(value, '|') != NULL);
                has_glob = (strchr(value, '*') != NULL || strchr(value, '?') != NULL || strchr(value, '[') != NULL);
                if (has_split && has_glob) {
                        glob = GL_SPLIT_GLOB;
                } else if (has_split) {
                        glob = GL_SPLIT;
                } else if (has_glob) {
1102
                        if (streq(value, "?*"))
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
                                glob = GL_SOMETHING;
                        else
                                glob = GL_GLOB;
                } else {
                        glob = GL_PLAIN;
                }
                token->key.glob = glob;
        }

        if (value != NULL && type > TK_M_MAX) {
                /* check if assigned value has substitution chars */
                if (value[0] == '[')
                        token->key.subst = SB_SUBSYS;
                else if (strchr(value, '%') != NULL || strchr(value, '$') != NULL)
                        token->key.subst = SB_FORMAT;
                else
                        token->key.subst = SB_NONE;
        }

        if (attr != NULL) {
                /* check if property/attribut name has substitution chars */
                if (attr[0] == '[')
                        token->key.attrsubst = SB_SUBSYS;
                else if (strchr(attr, '%') != NULL || strchr(attr, '$') != NULL)
                        token->key.attrsubst = SB_FORMAT;
                else
                        token->key.attrsubst = SB_NONE;
        }

        token->key.type = type;
        token->key.op = op;
        rule_tmp->token_cur++;
1135
        if (rule_tmp->token_cur >= ELEMENTSOF(rule_tmp->token)) {
1136
                log_error("temporary rule array too small\n");
1137
1138
1139
                return -1;
        }
        return 0;
1140
}
1141

1142
1143
static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp)
{
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
        unsigned int i;
        unsigned int start = 0;
        unsigned int end = rule_tmp->token_cur;

        for (i = 0; i < rule_tmp->token_cur; i++) {
                enum token_type next_val = TK_UNSET;
                unsigned int next_idx = 0;
                unsigned int j;

                /* find smallest value */
                for (j = start; j < end; j++) {
                        if (rule_tmp->token[j].type == TK_UNSET)
                                continue;
                        if (next_val == TK_UNSET || rule_tmp->token[j].type < next_val) {
                                next_val = rule_tmp->token[j].type;
                                next_idx = j;
                        }
                }

                /* add token and mark done */
                if (add_token(rules, &rule_tmp->token[next_idx]) != 0)
                        return -1;
                rule_tmp->token[next_idx].type = TK_UNSET;

                /* shrink range */
                if (next_idx == start)
                        start++;
                if (next_idx+1 == end)
                        end--;
        }
        return 0;
1175
1176
}

1177
static int add_rule(struct udev_rules *rules, char *line,
1178
                    const char *filename, unsigned int filename_off, unsigned int lineno)
1179
{
1180
        char *linepos;
1181
        const char *attr;
1182
1183
1184
1185
1186
        struct rule_tmp rule_tmp;

        memset(&rule_tmp, 0x00, sizeof(struct rule_tmp));
        rule_tmp.rules = rules;
        rule_tmp.rule.type = TK_RULE;
1187
1188
1189
        /* the offset in the rule is limited to unsigned short */
        if (filename_off < USHRT_MAX)
                rule_tmp.rule.rule.filename_off = filename_off;
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
        rule_tmp.rule.rule.filename_line = lineno;

        linepos = line;
        for (;;) {
                char *key;
                char *value;
                enum operation_type op;

                if (get_key(rules->udev, &linepos, &key, &op, &value) != 0)
                        break;

1201
                if (streq(key, "ACTION")) {
1202
                        if (op > OP_MATCH_MAX) {
1203
                                log_error("invalid ACTION operation\n");
1204
1205
1206
1207
1208
1209
                                goto invalid;
                        }
                        rule_add_key(&rule_tmp, TK_M_ACTION, op, value, NULL);
                        continue;
                }

1210
                if (streq(key, "DEVPATH")) {
1211
                        if (op > OP_MATCH_MAX) {
1212
                                log_error("invalid DEVPATH operation\n");
1213
1214
1215
1216
1217
1218
                                goto invalid;
                        }
                        rule_add_key(&rule_tmp, TK_M_DEVPATH, op, value, NULL);
                        continue;
                }

1219
                if (streq(key, "KERNEL")) {
1220
                        if (op > OP_MATCH_MAX) {
1221
                                log_error("invalid KERNEL operation\n");
1222
1223
1224
1225
1226
1227
                                goto invalid;
                        }
                        rule_add_key(&rule_tmp, TK_M_KERNEL, op, value, NULL);
                        continue;
                }

1228
                if (streq(key, "SUBSYSTEM")) {
1229
                        if (op > OP_MATCH_MAX) {
1230
                                log_error("invalid SUBSYSTEM operation\n");
1231
1232
1233
                                goto invalid;
                        }
                        /* bus, class, subsystem events should all be the same */
1234
1235
1236
1237
                        if (streq(value, "subsystem") ||
                            streq(value, "bus") ||
                            streq(value, "class")) {
                                if (streq(value, "bus") || streq(value, "class"))
1238
                                        log_error("'%s' must be specified as 'subsystem' \n"
1239
1240
1241
1242
1243
1244
1245
                                            "please fix it in %s:%u", value, filename, lineno);
                                rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, "subsystem|class|bus", NULL);
                        } else
                                rule_add_key(&rule_tmp, TK_M_SUBSYSTEM, op, value, NULL);
                        continue;
                }

1246
                if (streq(key, "DRIVER")) {
1247
                        if (op > OP_MATCH_MAX) {
1248
                                log_error("invalid DRIVER operation\n");
1249
1250
1251
1252
1253
1254
                                goto invalid;
                        }
                        rule_add_key(&rule_tmp, TK_M_DRIVER, op, value, NULL);
                        continue;
                }

1255
                if (startswith(key, "ATTR{")) {
1256
1257
                        attr = get_key_attribute(rules->udev, key + sizeof("ATTR")-1);
                        if (attr == NULL) {
1258
                                log_error("error parsing ATTR attribute\n");
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
                                goto invalid;
                        }
                        if (op < OP_MATCH_MAX) {
                                rule_add_key(&rule_tmp, TK_M_ATTR, op, value, attr);
                        } else {
                                rule_add_key(&rule_tmp, TK_A_ATTR, op, value, attr);
                        }
                        continue;
                }

1269
                if (streq(key, "KERNELS")) {
1270
                        if (op > OP_MATCH_MAX) {
1271
                                log_error("invalid KERNELS operation\n");
1272
1273
1274
1275
1276
1277
                                goto invalid;
                        }
                        rule_add_key(&rule_tmp, TK_M_KERNELS, op, value, NULL);
                        continue;
                }

1278
                if (streq(key, "SUBSYSTEMS")) {
1279
                        if (op > OP_MATCH_MAX) {
1280
                                log_error("invalid SUBSYSTEMS operation\n");
1281
1282
1283
1284
1285
1286
                                goto invalid;
                        }
                        rule_add_key(&rule_tmp, TK_M_SUBSYSTEMS, op, value, NULL);
                        continue;
                }

1287
                if (streq(key, "DRIVERS")) {
1288
                        if (op > OP_MATCH_MAX) {
1289
                                log_error("invalid DRIVERS operation\n");
1290
1291
1292
1293
1294
1295
                                goto invalid;
                        }
                        rule_add_key(&rule_tmp, TK_M_DRIVERS, op, value, NULL);
                        continue;
                }

1296
                if (startswith(key, "ATTRS{")) {
1297
                        if (op > OP_MATCH_MAX) {
1298
                                log_error("invalid ATTRS operation\n");
1299
1300
1301
1302
                                goto invalid;
                        }
                        attr = get_key_attribute(rules->udev, key + sizeof("ATTRS")-1);
                        if (attr == NULL) {
1303
                                log_error("error parsing ATTRS attribute\n");
1304
1305
                                goto invalid;
                        }
1306
                        if (startswith(attr, "device/"))
1307
                                log_error("the 'device' link may not be available in a future kernel, "
1308
1309
                                    "please fix it in %s:%u", filename, lineno);
                        else if (strstr(attr, "../") != NULL)
1310
                                log_error("do not reference parent sysfs directories directly, "
1311
1312
1313
1314
1315
                                    "it may break with a future kernel, please fix it in %s:%u", filename, lineno);
                        rule_add_key(&rule_tmp, TK_M_ATTRS, op, value, attr);
                        continue;
                }

1316
                if (streq(key, "TAGS")) {
1317
                        if (op > OP_MATCH_MAX) {
1318
                                log_error("invalid TAGS operation\n");
1319
1320
1321
1322
1323
1324
                                goto invalid;
                        }
                        rule_add_key(&rule_tmp, TK_M_TAGS, op, value, NULL);
                        continue;
                }

1325
                if (startswith(key, "ENV{")) {
1326
1327
                        attr = get_key_attribute(rules->udev, key + sizeof("ENV")-1);
                        if (attr == NULL) {
1328
                                log_error("error parsing ENV attribute\n");
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
                                goto invalid;
                        }
                        if (op < OP_MATCH_MAX) {
                                if (rule_add_key(&rule_tmp, TK_M_ENV, op, value, attr) != 0)
                                        goto invalid;
                        } else {
                                static const char *blacklist[] = {
                                        "ACTION",
                                        "SUBSYSTEM",
                                        "DEVTYPE",
                                        "MAJOR",
                                        "MINOR",
                                        "DRIVER",
                                        "IFINDEX",
                                        "DEVNAME",
                                        "DEVLINKS",
                                        "DEVPATH",
                                        "TAGS",
                                };
                                unsigned int i;

1350
1351
                                for (i = 0; i < ELEMENTSOF(blacklist); i++) {
                                        if (!streq(attr, blacklist[i]))
1352
                                                continue;
1353
1354
1355
                                        log_error("invalid ENV attribute, '%s' can not be set %s:%u\n", attr, filename, lineno);
                                        goto invalid;
                                }
1356
1357
1358
1359
1360
1361
                                if (rule_add_key(&rule_tmp, TK_A_ENV, op, value, attr) != 0)
                                        goto invalid;
                        }
                        continue;
                }

1362
                if (streq(key, "TAG")) {
1363
1364
1365
1366
1367
1368
1369
                        if (op < OP_MATCH_MAX)
                                rule_add_key(&rule_tmp, TK_M_TAG, op, value, NULL);
                        else
                                rule_add_key(&rule_tmp, TK_A_TAG, op, value, NULL);
                        continue;
                }

1370
                if (streq(key, "PROGRAM")) {
1371
1372
1373
1374
                        rule_add_key(&rule_tmp, TK_M_PROGRAM, op, value, NULL);
                        continue;
                }

1375
                if (streq(key, "RESULT")) {
1376
                        if (op > OP_MATCH_MAX) {
1377
                                log_error("invalid RESULT operation\n");
1378
1379
1380
1381
1382
1383
                                goto invalid;
                        }
                        rule_add_key(&rule_tmp, TK_M_RESULT, op, value, NULL);
                        continue;
                }

1384
                if (startswith(key, "IMPORT")) {
1385
1386
                        attr = get_key_attribute(rules->udev, key + sizeof("IMPORT")-1);
                        if (attr == NULL) {
1387
                                log_error("IMPORT{} type missing, ignoring IMPORT %s:%u\n", filename, lineno);
1388
1389
                                continue;
                        }
1390
                        if (streq(attr, "program")) {
1391
1392
1393
1394
1395
1396
                                /* find known built-in command */
                                if (value[0] != '/') {
                                        enum udev_builtin_cmd cmd;

                                        cmd = udev_builtin_lookup(value);
                                        if