chiark / gitweb /
update example rules
[elogind.git] / udev_rules_parse.c
1 /*
2  * Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
3  * Copyright (C) 2003-2006 Kay Sievers <kay.sievers@vrfy.org>
4  *
5  *      This program is free software; you can redistribute it and/or modify it
6  *      under the terms of the GNU General Public License as published by the
7  *      Free Software Foundation version 2 of the License.
8  * 
9  *      This program is distributed in the hope that it will be useful, but
10  *      WITHOUT ANY WARRANTY; without even the implied warranty of
11  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *      General Public License for more details.
13  * 
14  *      You should have received a copy of the GNU General Public License along
15  *      with this program; if not, write to the Free Software Foundation, Inc.,
16  *      51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  */
19
20 #include <stddef.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <ctype.h>
25 #include <unistd.h>
26 #include <sys/stat.h>
27 #include <errno.h>
28
29 #include "udev.h"
30 #include "udev_rules.h"
31
32
33 void udev_rules_iter_init(struct udev_rules *rules)
34 {
35         dbg("bufsize=%zi", rules->bufsize);
36         rules->current = 0;
37 }
38
39 struct udev_rule *udev_rules_iter_next(struct udev_rules *rules)
40 {
41         static struct udev_rule *rule;
42
43         if (!rules)
44                 return NULL;
45
46         dbg("current=%zi", rules->current);
47         if (rules->current >= rules->bufsize) {
48                 dbg("no more rules");
49                 return NULL;
50         }
51
52         /* get next rule */
53         rule = (struct udev_rule *) (rules->buf + rules->current);
54         rules->current += sizeof(struct udev_rule) + rule->bufsize;
55
56         return rule;
57 }
58
59 struct udev_rule *udev_rules_iter_label(struct udev_rules *rules, const char *label)
60 {
61         static struct udev_rule *rule;
62
63 next:
64         dbg("current=%zi", rules->current);
65         if (rules->current >= rules->bufsize) {
66                 dbg("no more rules");
67                 return NULL;
68         }
69         rule = (struct udev_rule *) (rules->buf + rules->current);
70
71         if (strcmp(&rule->buf[rule->label.val_off], label) != 0) {
72                 dbg("moving forward, looking for label '%s'", label);
73                 rules->current += sizeof(struct udev_rule) + rule->bufsize;
74                 goto next;
75         }
76
77         dbg("found label '%s'", label);
78         return rule;
79 }
80
81 static int get_key(char **line, char **key, enum key_operation *operation, char **value)
82 {
83         char *linepos;
84         char *temp;
85
86         linepos = *line;
87         if (linepos == NULL && linepos[0] == '\0')
88                 return -1;
89
90         /* skip whitespace */
91         while (isspace(linepos[0]) || linepos[0] == ',')
92                 linepos++;
93
94         /* get the key */
95         if (linepos[0] == '\0')
96                 return -1;
97         *key = linepos;
98
99         while (1) {
100                 linepos++;
101                 if (linepos[0] == '\0')
102                         return -1;
103                 if (isspace(linepos[0]))
104                         break;
105                 if (linepos[0] == '=')
106                         break;
107                 if (linepos[0] == '+')
108                         break;
109                 if (linepos[0] == '!')
110                         break;
111                 if (linepos[0] == ':')
112                         break;
113         }
114
115         /* remember end of key */
116         temp = linepos;
117
118         /* skip whitespace after key */
119         while (isspace(linepos[0]))
120                 linepos++;
121         if (linepos[0] == '\0')
122                 return -1;
123
124         /* get operation type */
125         if (linepos[0] == '=' && linepos[1] == '=') {
126                 *operation = KEY_OP_MATCH;
127                 linepos += 2;
128                 dbg("operator=match");
129         } else if (linepos[0] == '!' && linepos[1] == '=') {
130                 *operation = KEY_OP_NOMATCH;
131                 linepos += 2;
132                 dbg("operator=nomatch");
133         } else if (linepos[0] == '+' && linepos[1] == '=') {
134                 *operation = KEY_OP_ADD;
135                 linepos += 2;
136                 dbg("operator=add");
137         } else if (linepos[0] == '=') {
138                 *operation = KEY_OP_ASSIGN;
139                 linepos++;
140                 dbg("operator=assign");
141         } else if (linepos[0] == ':' && linepos[1] == '=') {
142                 *operation = KEY_OP_ASSIGN_FINAL;
143                 linepos += 2;
144                 dbg("operator=assign_final");
145         } else
146                 return -1;
147
148         /* terminate key */
149         temp[0] = '\0';
150         dbg("key='%s'", *key);
151
152         /* skip whitespace after operator */
153         while (isspace(linepos[0]))
154                 linepos++;
155         if (linepos[0] == '\0')
156                 return -1;
157
158         /* get the value*/
159         if (linepos[0] == '"')
160                 linepos++;
161         else
162                 return -1;
163         *value = linepos;
164
165         temp = strchr(linepos, '"');
166         if (!temp)
167                 return -1;
168         temp[0] = '\0';
169         temp++;
170         dbg("value='%s'", *value);
171
172         /* move line to next key */
173         *line = temp;
174
175         return 0;
176 }
177
178 /* extract possible KEY{attr} */
179 static char *get_key_attribute(char *str)
180 {
181         char *pos;
182         char *attr;
183
184         attr = strchr(str, '{');
185         if (attr != NULL) {
186                 attr++;
187                 pos = strchr(attr, '}');
188                 if (pos == NULL) {
189                         err("missing closing brace for format");
190                         return NULL;
191                 }
192                 pos[0] = '\0';
193                 dbg("attribute='%s'", attr);
194                 return attr;
195         }
196
197         return NULL;
198 }
199
200 static int add_rule_key(struct udev_rule *rule, struct key *key,
201                         enum key_operation operation, const char *value)
202 {
203         size_t val_len = strnlen(value, PATH_SIZE);
204
205         key->operation = operation;
206
207         key->val_off = rule->bufsize;
208         strlcpy(rule->buf + rule->bufsize, value, val_len+1);
209         rule->bufsize += val_len+1;
210
211         return 0;
212 }
213
214 static int add_rule_key_pair(struct udev_rule *rule, struct key_pairs *pairs,
215                              enum key_operation operation, const char *key, const char *value)
216 {
217         size_t key_len = strnlen(key, PATH_SIZE);
218
219         if (pairs->count >= PAIRS_MAX) {
220                 err("skip, too many keys of the same type in a single rule");
221                 return -1;
222         }
223
224         add_rule_key(rule, &pairs->keys[pairs->count].key, operation, value);
225
226         /* add the key-name of the pair */
227         pairs->keys[pairs->count].key_name_off = rule->bufsize;
228         strlcpy(rule->buf + rule->bufsize, key, key_len+1);
229         rule->bufsize += key_len+1;
230
231         pairs->count++;
232
233         return 0;
234 }
235
236 static int add_to_rules(struct udev_rules *rules, char *line, const char *filename, unsigned int lineno)
237 {
238         struct udev_rule *rule;
239         size_t rule_size;
240         int valid;
241         char *linepos;
242         char *attr;
243         size_t padding;
244         int physdev = 0;
245         int retval;
246
247         /* get all the keys */
248         rule = calloc(1, sizeof (struct udev_rule) + LINE_SIZE);
249         if (!rule) {
250                 err("malloc failed");
251                 return -1;
252         }
253         linepos = line;
254         valid = 0;
255
256         while (1) {
257                 char *key;
258                 char *value;
259                 enum key_operation operation = KEY_OP_UNSET;
260
261                 retval = get_key(&linepos, &key, &operation, &value);
262                 if (retval)
263                         break;
264
265                 if (strcasecmp(key, "ACTION") == 0) {
266                         if (operation != KEY_OP_MATCH &&
267                             operation != KEY_OP_NOMATCH) {
268                                 err("invalid ACTION operation");
269                                 goto invalid;
270                         }
271                         add_rule_key(rule, &rule->action, operation, value);
272                         valid = 1;
273                         continue;
274                 }
275
276                 if (strcasecmp(key, "DEVPATH") == 0) {
277                         if (operation != KEY_OP_MATCH &&
278                             operation != KEY_OP_NOMATCH) {
279                                 err("invalid DEVPATH operation");
280                                 goto invalid;
281                         }
282                         add_rule_key(rule, &rule->devpath, operation, value);
283                         valid = 1;
284                         continue;
285                 }
286
287                 if (strcasecmp(key, "KERNEL") == 0) {
288                         if (operation != KEY_OP_MATCH &&
289                             operation != KEY_OP_NOMATCH) {
290                                 err("invalid KERNEL operation");
291                                 goto invalid;
292                         }
293                         add_rule_key(rule, &rule->kernel, operation, value);
294                         valid = 1;
295                         continue;
296                 }
297
298                 if (strcasecmp(key, "SUBSYSTEM") == 0) {
299                         if (operation != KEY_OP_MATCH &&
300                             operation != KEY_OP_NOMATCH) {
301                                 err("invalid SUBSYSTEM operation");
302                                 goto invalid;
303                         }
304                         add_rule_key(rule, &rule->subsystem, operation, value);
305                         valid = 1;
306                         continue;
307                 }
308
309                 if (strcasecmp(key, "DRIVER") == 0) {
310                         add_rule_key(rule, &rule->driver, operation, value);
311                         valid = 1;
312                         continue;
313                 }
314
315                 if (strncasecmp(key, "ATTR{", sizeof("ATTR{")-1) == 0) {
316                         attr = get_key_attribute(key + sizeof("ATTR")-1);
317                         if (attr == NULL) {
318                                 err("error parsing ATTR attribute");
319                                 goto invalid;
320                         }
321                         if (add_rule_key_pair(rule, &rule->attr, operation, attr, value) != 0)
322                                 goto invalid;
323                         valid = 1;
324                         continue;
325                 }
326
327                 if (strcasecmp(key, "KERNELS") == 0 ||
328                     strcasecmp(key, "ID") == 0) {
329                         if (operation != KEY_OP_MATCH &&
330                             operation != KEY_OP_NOMATCH) {
331                                 err("invalid KERNELS operation");
332                                 goto invalid;
333                         }
334                         add_rule_key(rule, &rule->kernels, operation, value);
335                         valid = 1;
336                         continue;
337                 }
338
339                 if (strcasecmp(key, "SUBSYSTEMS") == 0 ||
340                     strcasecmp(key, "BUS") == 0) {
341                         if (operation != KEY_OP_MATCH &&
342                             operation != KEY_OP_NOMATCH) {
343                                 err("invalid SUBSYSTEMS operation");
344                                 goto invalid;
345                         }
346                         add_rule_key(rule, &rule->subsystems, operation, value);
347                         valid = 1;
348                         continue;
349                 }
350
351                 if (strcasecmp(key, "DRIVERS") == 0) {
352                         if (operation != KEY_OP_MATCH &&
353                             operation != KEY_OP_NOMATCH) {
354                                 err("invalid DRIVERS operation");
355                                 goto invalid;
356                         }
357                         add_rule_key(rule, &rule->drivers, operation, value);
358                         valid = 1;
359                         continue;
360                 }
361
362                 if (strncasecmp(key, "ATTRS{", sizeof("ATTRS{")-1) == 0 ||
363                     strncasecmp(key, "SYSFS{", sizeof("SYSFS{")-1) == 0) {
364                         if (operation != KEY_OP_MATCH &&
365                             operation != KEY_OP_NOMATCH) {
366                                 err("invalid ATTRS operation");
367                                 goto invalid;
368                         }
369                         attr = get_key_attribute(key + sizeof("ATTRS")-1);
370                         if (attr == NULL) {
371                                 err("error parsing ATTRS attribute");
372                                 goto invalid;
373                         }
374                         if (strncmp(attr, "device/", 7) == 0)
375                                 err("the 'device' link is deprecated and will be removed from a future kernel, "
376                                     "please fix it in %s:%u", filename, lineno);
377                         else if (strchr(attr, '/') != NULL)
378                                 err("do not reference parent sysfs directories directly, that may break with a future kernel, "
379                                     "please fix it in %s:%u", filename, lineno);
380                         if (add_rule_key_pair(rule, &rule->attrs, operation, attr, value) != 0)
381                                 goto invalid;
382                         valid = 1;
383                         continue;
384                 }
385
386                 if (strncasecmp(key, "ENV{", sizeof("ENV{")-1) == 0) {
387                         attr = get_key_attribute(key + sizeof("ENV")-1);
388                         if (attr == NULL) {
389                                 err("error parsing ENV attribute");
390                                 goto invalid;
391                         }
392                         if (strncmp(attr, "PHYSDEV", 7) == 0)
393                                 physdev = 1;
394                         if (add_rule_key_pair(rule, &rule->env, operation, attr, value) != 0)
395                                 goto invalid;
396                         valid = 1;
397                         continue;
398                 }
399
400                 if (strcasecmp(key, "PROGRAM") == 0) {
401                         add_rule_key(rule, &rule->program, operation, value);
402                         valid = 1;
403                         continue;
404                 }
405
406                 if (strcasecmp(key, "RESULT") == 0) {
407                         if (operation != KEY_OP_MATCH &&
408                             operation != KEY_OP_NOMATCH) {
409                                 err("invalid RESULT operation");
410                                 goto invalid;
411                         }
412                         add_rule_key(rule, &rule->result, operation, value);
413                         valid = 1;
414                         continue;
415                 }
416
417                 if (strncasecmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
418                         attr = get_key_attribute(key + sizeof("IMPORT")-1);
419                         if (attr && strstr(attr, "program")) {
420                                 dbg("IMPORT will be executed");
421                                 rule->import_type  = IMPORT_PROGRAM;
422                         } else if (attr && strstr(attr, "file")) {
423                                 dbg("IMPORT will be included as file");
424                                 rule->import_type  = IMPORT_FILE;
425                         } else if (attr && strstr(attr, "parent")) {
426                                 dbg("IMPORT will include the parent values");
427                                 rule->import_type = IMPORT_PARENT;
428                         } else {
429                                 /* figure it out if it is executable */
430                                 char file[PATH_SIZE];
431                                 char *pos;
432                                 struct stat stats;
433
434                                 strlcpy(file, value, sizeof(file));
435                                 pos = strchr(file, ' ');
436                                 if (pos)
437                                         pos[0] = '\0';
438
439                                 /* allow programs in /lib/udev called without the path */
440                                 if (strchr(file, '/') == NULL) {
441                                         strlcpy(file, "/lib/udev/", sizeof(file));
442                                         strlcat(file, value, sizeof(file));
443                                         pos = strchr(file, ' ');
444                                         if (pos)
445                                                 pos[0] = '\0';
446                                 }
447
448                                 dbg("IMPORT auto mode for '%s'", file);
449                                 if (!lstat(file, &stats) && (stats.st_mode & S_IXUSR)) {
450                                         dbg("IMPORT is executable, will be executed (autotype)");
451                                         rule->import_type  = IMPORT_PROGRAM;
452                                 } else {
453                                         dbg("IMPORT is not executable, will be included as file (autotype)");
454                                         rule->import_type  = IMPORT_FILE;
455                                 }
456                         }
457                         add_rule_key(rule, &rule->import, operation, value);
458                         valid = 1;
459                         continue;
460                 }
461
462                 if (strcasecmp(key, "RUN") == 0) {
463                         add_rule_key(rule, &rule->run, operation, value);
464                         valid = 1;
465                         continue;
466                 }
467
468                 if (strcasecmp(key, "WAIT_FOR_SYSFS") == 0) {
469                         add_rule_key(rule, &rule->wait_for_sysfs, operation, value);
470                         valid = 1;
471                         continue;
472                 }
473
474                 if (strcasecmp(key, "LABEL") == 0) {
475                         add_rule_key(rule, &rule->label, operation, value);
476                         valid = 1;
477                         continue;
478                 }
479
480                 if (strcasecmp(key, "GOTO") == 0) {
481                         add_rule_key(rule, &rule->goto_label, operation, value);
482                         valid = 1;
483                         continue;
484                 }
485
486                 if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) {
487                         attr = get_key_attribute(key + sizeof("NAME")-1);
488                         if (attr != NULL) {
489                                 if (strstr(attr, "all_partitions") != NULL) {
490                                         dbg("creation of partition nodes requested");
491                                         rule->partitions = DEFAULT_PARTITIONS_COUNT;
492                                 }
493                                 if (strstr(attr, "ignore_remove") != NULL) {
494                                         dbg("remove event should be ignored");
495                                         rule->ignore_remove = 1;
496                                 }
497                         }
498                         if (value[0] == '\0')
499                                 dbg("name empty, node creation supressed");
500                         add_rule_key(rule, &rule->name, operation, value);
501                         continue;
502                 }
503
504                 if (strcasecmp(key, "SYMLINK") == 0) {
505                         add_rule_key(rule, &rule->symlink, operation, value);
506                         valid = 1;
507                         continue;
508                 }
509
510                 if (strcasecmp(key, "OWNER") == 0) {
511                         valid = 1;
512                         if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
513                                 char *endptr;
514                                 strtoul(value, &endptr, 10);
515                                 if (endptr[0] != '\0') {
516                                         char owner[32];
517                                         uid_t uid = lookup_user(value);
518                                         dbg("replacing username='%s' by id=%i", value, uid);
519                                         sprintf(owner, "%u", (unsigned int) uid);
520                                         add_rule_key(rule, &rule->owner, operation, owner);
521                                         continue;
522                                 }
523                         }
524
525                         add_rule_key(rule, &rule->owner, operation, value);
526                         continue;
527                 }
528
529                 if (strcasecmp(key, "GROUP") == 0) {
530                         valid = 1;
531                         if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
532                                 char *endptr;
533                                 strtoul(value, &endptr, 10);
534                                 if (endptr[0] != '\0') {
535                                         char group[32];
536                                         gid_t gid = lookup_group(value);
537                                         dbg("replacing groupname='%s' by id=%i", value, gid);
538                                         sprintf(group, "%u", (unsigned int) gid);
539                                         add_rule_key(rule, &rule->group, operation, group);
540                                         continue;
541                                 }
542                         }
543
544                         add_rule_key(rule, &rule->group, operation, value);
545                         continue;
546                 }
547
548                 if (strcasecmp(key, "MODE") == 0) {
549                         rule->mode = strtol(value, NULL, 8);
550                         rule->mode_operation = operation;
551                         valid = 1;
552                         continue;
553                 }
554
555                 if (strcasecmp(key, "OPTIONS") == 0) {
556                         if (strstr(value, "last_rule") != NULL) {
557                                 dbg("last rule to be applied");
558                                 rule->last_rule = 1;
559                         }
560                         if (strstr(value, "ignore_device") != NULL) {
561                                 dbg("device should be ignored");
562                                 rule->ignore_device = 1;
563                         }
564                         if (strstr(value, "ignore_remove") != NULL) {
565                                 dbg("remove event should be ignored");
566                                 rule->ignore_remove = 1;
567                         }
568                         if (strstr(value, "all_partitions") != NULL) {
569                                 dbg("creation of partition nodes requested");
570                                 rule->partitions = DEFAULT_PARTITIONS_COUNT;
571                         }
572                         valid = 1;
573                         continue;
574                 }
575
576                 err("unknown key '%s' in %s:%u", key, filename, lineno);
577         }
578
579         if (physdev && rule->wait_for_sysfs.operation == KEY_OP_UNSET)
580                 err("PHYSDEV* values are deprecated and will be removed from a future kernel, "
581                     "please fix it in %s:%u", filename, lineno);
582
583         /* skip line if not any valid key was found */
584         if (!valid)
585                 goto invalid;
586
587         /* grow buffer and add rule */
588         rule_size = sizeof(struct udev_rule) + rule->bufsize;
589         padding = (sizeof(size_t) - rule_size % sizeof(size_t)) % sizeof(size_t);
590         dbg("add %zi padding bytes", padding);
591         rule_size += padding;
592         rule->bufsize += padding;
593
594         rules->buf = realloc(rules->buf, rules->bufsize + rule_size);
595         if (!rules->buf) {
596                 err("realloc failed");
597                 goto exit;
598         }
599         dbg("adding rule to offset %zi", rules->bufsize);
600         memcpy(rules->buf + rules->bufsize, rule, rule_size);
601         rules->bufsize += rule_size;
602 exit:
603         free(rule);
604         return 0;
605
606 invalid:
607         free(rule);
608         err("invalid rule '%s:%u'", filename, lineno);
609         return -1;
610 }
611
612 static int parse_file(struct udev_rules *rules, const char *filename)
613 {
614         char line[LINE_SIZE];
615         char *bufline;
616         unsigned int lineno;
617         char *buf;
618         size_t bufsize;
619         size_t cur;
620         size_t count;
621         int retval = 0;
622
623         if (file_map(filename, &buf, &bufsize) != 0) {
624                 err("can't open '%s' as rules file: %s", filename, strerror(errno));
625                 return -1;
626         }
627         info("reading '%s' as rules file", filename);
628
629         /* loop through the whole file */
630         cur = 0;
631         lineno = 0;
632         while (cur < bufsize) {
633                 unsigned int i, j;
634
635                 count = buf_get_line(buf, bufsize, cur);
636                 bufline = &buf[cur];
637                 cur += count+1;
638                 lineno++;
639
640                 /* eat the whitespace */
641                 while ((count > 0) && isspace(bufline[0])) {
642                         bufline++;
643                         count--;
644                 }
645                 if (count == 0)
646                         continue;
647
648                 /* see if this is a comment */
649                 if (bufline[0] == COMMENT_CHARACTER)
650                         continue;
651
652                 if (count >= sizeof(line)) {
653                         err("line too long, rule skipped '%s:%u'", filename, lineno);
654                         continue;
655                 }
656
657                 /* skip backslash and newline from multiline rules */
658                 for (i = j = 0; i < count; i++) {
659                         if (bufline[i] == '\\' && bufline[i+1] == '\n')
660                                 continue;
661
662                         line[j++] = bufline[i];
663                 }
664                 line[j] = '\0';
665
666                 dbg("read '%s'", line);
667                 add_to_rules(rules, line, filename, lineno);
668         }
669
670         file_unmap(buf, bufsize);
671         return retval;
672 }
673
674 int udev_rules_init(struct udev_rules *rules, int resolve_names)
675 {
676         struct stat stats;
677         int retval;
678
679         memset(rules, 0x00, sizeof(struct udev_rules));
680         rules->resolve_names = resolve_names;
681
682         /* parse rules file or all matching files in directory */
683         if (stat(udev_rules_dir, &stats) != 0)
684                 return -1;
685
686         if ((stats.st_mode & S_IFMT) != S_IFDIR) {
687                 dbg("parse single rules file '%s'", udev_rules_dir);
688                 retval = parse_file(rules, udev_rules_dir);
689         } else {
690                 struct name_entry *name_loop, *name_tmp;
691                 LIST_HEAD(name_list);
692
693                 dbg("parse rules directory '%s'", udev_rules_dir);
694                 retval = add_matching_files(&name_list, udev_rules_dir, RULESFILE_SUFFIX);
695
696                 list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
697                         if (stat(name_loop->name, &stats) == 0) {
698                                 if (stats.st_size)
699                                         parse_file(rules, name_loop->name);
700                                 else
701                                         dbg("empty rules file '%s'", name_loop->name);
702                         } else
703                                 err("could not read '%s': %s", name_loop->name, strerror(errno));
704                         list_del(&name_loop->node);
705                         free(name_loop);
706                 }
707         }
708
709         return retval;
710 }
711
712 void udev_rules_cleanup(struct udev_rules *rules)
713 {
714         if (rules->buf) {
715                 free(rules->buf);
716                 rules->buf = NULL;
717         }
718 }