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