chiark / gitweb /
default rules: wait for 0:0:0:0 scsi devices only
[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] == '+') || (linepos[0] == '!') || (linepos[0] == ':'))
108                         if (linepos[1] == '=')
109                                 break;
110         }
111
112         /* remember end of key */
113         temp = linepos;
114
115         /* skip whitespace after key */
116         while (isspace(linepos[0]))
117                 linepos++;
118         if (linepos[0] == '\0')
119                 return -1;
120
121         /* get operation type */
122         if (linepos[0] == '=' && linepos[1] == '=') {
123                 *operation = KEY_OP_MATCH;
124                 linepos += 2;
125                 dbg("operator=match");
126         } else if (linepos[0] == '!' && linepos[1] == '=') {
127                 *operation = KEY_OP_NOMATCH;
128                 linepos += 2;
129                 dbg("operator=nomatch");
130         } else if (linepos[0] == '+' && linepos[1] == '=') {
131                 *operation = KEY_OP_ADD;
132                 linepos += 2;
133                 dbg("operator=add");
134         } else if (linepos[0] == '=') {
135                 *operation = KEY_OP_ASSIGN;
136                 linepos++;
137                 dbg("operator=assign");
138         } else if (linepos[0] == ':' && linepos[1] == '=') {
139                 *operation = KEY_OP_ASSIGN_FINAL;
140                 linepos += 2;
141                 dbg("operator=assign_final");
142         } else
143                 return -1;
144
145         /* terminate key */
146         temp[0] = '\0';
147         dbg("key='%s'", *key);
148
149         /* skip whitespace after operator */
150         while (isspace(linepos[0]))
151                 linepos++;
152         if (linepos[0] == '\0')
153                 return -1;
154
155         /* get the value*/
156         if (linepos[0] == '"')
157                 linepos++;
158         else
159                 return -1;
160         *value = linepos;
161
162         temp = strchr(linepos, '"');
163         if (!temp)
164                 return -1;
165         temp[0] = '\0';
166         temp++;
167         dbg("value='%s'", *value);
168
169         /* move line to next key */
170         *line = temp;
171
172         return 0;
173 }
174
175 /* extract possible KEY{attr} */
176 static char *get_key_attribute(char *str)
177 {
178         char *pos;
179         char *attr;
180
181         attr = strchr(str, '{');
182         if (attr != NULL) {
183                 attr++;
184                 pos = strchr(attr, '}');
185                 if (pos == NULL) {
186                         err("missing closing brace for format");
187                         return NULL;
188                 }
189                 pos[0] = '\0';
190                 dbg("attribute='%s'", attr);
191                 return attr;
192         }
193
194         return NULL;
195 }
196
197 static int add_rule_key(struct udev_rule *rule, struct key *key,
198                         enum key_operation operation, const char *value)
199 {
200         size_t val_len = strnlen(value, PATH_SIZE);
201
202         key->operation = operation;
203
204         key->val_off = rule->bufsize;
205         strlcpy(rule->buf + rule->bufsize, value, val_len+1);
206         rule->bufsize += val_len+1;
207
208         return 0;
209 }
210
211 static int add_rule_key_pair(struct udev_rule *rule, struct key_pairs *pairs,
212                              enum key_operation operation, const char *key, const char *value)
213 {
214         size_t key_len = strnlen(key, PATH_SIZE);
215
216         if (pairs->count >= PAIRS_MAX) {
217                 err("skip, too many keys of the same type in a single rule");
218                 return -1;
219         }
220
221         add_rule_key(rule, &pairs->keys[pairs->count].key, operation, value);
222
223         /* add the key-name of the pair */
224         pairs->keys[pairs->count].key_name_off = rule->bufsize;
225         strlcpy(rule->buf + rule->bufsize, key, key_len+1);
226         rule->bufsize += key_len+1;
227
228         pairs->count++;
229
230         return 0;
231 }
232
233 static int add_to_rules(struct udev_rules *rules, char *line, const char *filename, unsigned int lineno)
234 {
235         struct udev_rule *rule;
236         size_t rule_size;
237         int valid;
238         char *linepos;
239         char *attr;
240         size_t padding;
241         int physdev = 0;
242         int retval;
243
244         /* get all the keys */
245         rule = calloc(1, sizeof (struct udev_rule) + LINE_SIZE);
246         if (!rule) {
247                 err("malloc failed");
248                 return -1;
249         }
250         linepos = line;
251         valid = 0;
252
253         while (1) {
254                 char *key;
255                 char *value;
256                 enum key_operation operation = KEY_OP_UNSET;
257
258                 retval = get_key(&linepos, &key, &operation, &value);
259                 if (retval)
260                         break;
261
262                 if (strcasecmp(key, "ACTION") == 0) {
263                         if (operation != KEY_OP_MATCH &&
264                             operation != KEY_OP_NOMATCH) {
265                                 err("invalid ACTION operation");
266                                 goto invalid;
267                         }
268                         add_rule_key(rule, &rule->action, operation, value);
269                         valid = 1;
270                         continue;
271                 }
272
273                 if (strcasecmp(key, "DEVPATH") == 0) {
274                         if (operation != KEY_OP_MATCH &&
275                             operation != KEY_OP_NOMATCH) {
276                                 err("invalid DEVPATH operation");
277                                 goto invalid;
278                         }
279                         add_rule_key(rule, &rule->devpath, operation, value);
280                         valid = 1;
281                         continue;
282                 }
283
284                 if (strcasecmp(key, "KERNEL") == 0) {
285                         if (operation != KEY_OP_MATCH &&
286                             operation != KEY_OP_NOMATCH) {
287                                 err("invalid KERNEL operation");
288                                 goto invalid;
289                         }
290                         add_rule_key(rule, &rule->kernel, operation, value);
291                         valid = 1;
292                         continue;
293                 }
294
295                 if (strcasecmp(key, "SUBSYSTEM") == 0) {
296                         if (operation != KEY_OP_MATCH &&
297                             operation != KEY_OP_NOMATCH) {
298                                 err("invalid SUBSYSTEM operation");
299                                 goto invalid;
300                         }
301                         add_rule_key(rule, &rule->subsystem, operation, value);
302                         valid = 1;
303                         continue;
304                 }
305
306                 if (strcasecmp(key, "DRIVER") == 0) {
307                         if (operation != KEY_OP_MATCH &&
308                             operation != KEY_OP_NOMATCH) {
309                                 err("invalid DRIVER operation");
310                                 goto invalid;
311                         }
312                         add_rule_key(rule, &rule->driver, operation, value);
313                         valid = 1;
314                         continue;
315                 }
316
317                 if (strncasecmp(key, "ATTR{", sizeof("ATTR{")-1) == 0) {
318                         attr = get_key_attribute(key + sizeof("ATTR")-1);
319                         if (attr == NULL) {
320                                 err("error parsing ATTR attribute");
321                                 goto invalid;
322                         }
323                         if (add_rule_key_pair(rule, &rule->attr, operation, attr, value) != 0)
324                                 goto invalid;
325                         valid = 1;
326                         continue;
327                 }
328
329                 if (strcasecmp(key, "KERNELS") == 0 ||
330                     strcasecmp(key, "ID") == 0) {
331                         if (operation != KEY_OP_MATCH &&
332                             operation != KEY_OP_NOMATCH) {
333                                 err("invalid KERNELS operation");
334                                 goto invalid;
335                         }
336                         add_rule_key(rule, &rule->kernels, operation, value);
337                         valid = 1;
338                         continue;
339                 }
340
341                 if (strcasecmp(key, "SUBSYSTEMS") == 0 ||
342                     strcasecmp(key, "BUS") == 0) {
343                         if (operation != KEY_OP_MATCH &&
344                             operation != KEY_OP_NOMATCH) {
345                                 err("invalid SUBSYSTEMS operation");
346                                 goto invalid;
347                         }
348                         add_rule_key(rule, &rule->subsystems, operation, value);
349                         valid = 1;
350                         continue;
351                 }
352
353                 if (strcasecmp(key, "DRIVERS") == 0) {
354                         if (operation != KEY_OP_MATCH &&
355                             operation != KEY_OP_NOMATCH) {
356                                 err("invalid DRIVERS operation");
357                                 goto invalid;
358                         }
359                         add_rule_key(rule, &rule->drivers, operation, value);
360                         valid = 1;
361                         continue;
362                 }
363
364                 if (strncasecmp(key, "ATTRS{", sizeof("ATTRS{")-1) == 0 ||
365                     strncasecmp(key, "SYSFS{", sizeof("SYSFS{")-1) == 0) {
366                         if (operation != KEY_OP_MATCH &&
367                             operation != KEY_OP_NOMATCH) {
368                                 err("invalid ATTRS operation");
369                                 goto invalid;
370                         }
371                         attr = get_key_attribute(key + sizeof("ATTRS")-1);
372                         if (attr == NULL) {
373                                 err("error parsing ATTRS attribute");
374                                 goto invalid;
375                         }
376                         if (strncmp(attr, "device/", 7) == 0)
377                                 err("the 'device' link is deprecated and will be removed from a future kernel, "
378                                     "please fix it in %s:%u", filename, lineno);
379                         else if (strchr(attr, '/') != NULL)
380                                 err("do not reference parent sysfs directories directly, that may break with a future kernel, "
381                                     "please fix it in %s:%u", filename, lineno);
382                         if (add_rule_key_pair(rule, &rule->attrs, operation, attr, value) != 0)
383                                 goto invalid;
384                         valid = 1;
385                         continue;
386                 }
387
388                 if (strncasecmp(key, "ENV{", sizeof("ENV{")-1) == 0) {
389                         attr = get_key_attribute(key + sizeof("ENV")-1);
390                         if (attr == NULL) {
391                                 err("error parsing ENV attribute");
392                                 goto invalid;
393                         }
394                         if (strncmp(attr, "PHYSDEV", 7) == 0)
395                                 physdev = 1;
396                         if (add_rule_key_pair(rule, &rule->env, operation, attr, value) != 0)
397                                 goto invalid;
398                         valid = 1;
399                         continue;
400                 }
401
402                 if (strcasecmp(key, "PROGRAM") == 0) {
403                         add_rule_key(rule, &rule->program, operation, value);
404                         valid = 1;
405                         continue;
406                 }
407
408                 if (strcasecmp(key, "RESULT") == 0) {
409                         if (operation != KEY_OP_MATCH &&
410                             operation != KEY_OP_NOMATCH) {
411                                 err("invalid RESULT operation");
412                                 goto invalid;
413                         }
414                         add_rule_key(rule, &rule->result, operation, value);
415                         valid = 1;
416                         continue;
417                 }
418
419                 if (strncasecmp(key, "IMPORT", sizeof("IMPORT")-1) == 0) {
420                         attr = get_key_attribute(key + sizeof("IMPORT")-1);
421                         if (attr && strstr(attr, "program")) {
422                                 dbg("IMPORT will be executed");
423                                 rule->import_type  = IMPORT_PROGRAM;
424                         } else if (attr && strstr(attr, "file")) {
425                                 dbg("IMPORT will be included as file");
426                                 rule->import_type  = IMPORT_FILE;
427                         } else if (attr && strstr(attr, "parent")) {
428                                 dbg("IMPORT will include the parent values");
429                                 rule->import_type = IMPORT_PARENT;
430                         } else {
431                                 /* figure it out if it is executable */
432                                 char file[PATH_SIZE];
433                                 char *pos;
434                                 struct stat stats;
435
436                                 strlcpy(file, value, sizeof(file));
437                                 pos = strchr(file, ' ');
438                                 if (pos)
439                                         pos[0] = '\0';
440
441                                 /* allow programs in /lib/udev called without the path */
442                                 if (strchr(file, '/') == NULL) {
443                                         strlcpy(file, "/lib/udev/", sizeof(file));
444                                         strlcat(file, value, sizeof(file));
445                                         pos = strchr(file, ' ');
446                                         if (pos)
447                                                 pos[0] = '\0';
448                                 }
449
450                                 dbg("IMPORT auto mode for '%s'", file);
451                                 if (!lstat(file, &stats) && (stats.st_mode & S_IXUSR)) {
452                                         dbg("IMPORT is executable, will be executed (autotype)");
453                                         rule->import_type  = IMPORT_PROGRAM;
454                                 } else {
455                                         dbg("IMPORT is not executable, will be included as file (autotype)");
456                                         rule->import_type  = IMPORT_FILE;
457                                 }
458                         }
459                         add_rule_key(rule, &rule->import, operation, value);
460                         valid = 1;
461                         continue;
462                 }
463
464                 if (strcasecmp(key, "RUN") == 0) {
465                         add_rule_key(rule, &rule->run, operation, value);
466                         valid = 1;
467                         continue;
468                 }
469
470                 if (strcasecmp(key, "WAIT_FOR_SYSFS") == 0) {
471                         add_rule_key(rule, &rule->wait_for_sysfs, operation, value);
472                         valid = 1;
473                         continue;
474                 }
475
476                 if (strcasecmp(key, "LABEL") == 0) {
477                         add_rule_key(rule, &rule->label, operation, value);
478                         valid = 1;
479                         continue;
480                 }
481
482                 if (strcasecmp(key, "GOTO") == 0) {
483                         add_rule_key(rule, &rule->goto_label, operation, value);
484                         valid = 1;
485                         continue;
486                 }
487
488                 if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) {
489                         attr = get_key_attribute(key + sizeof("NAME")-1);
490                         if (attr != NULL) {
491                                 if (strstr(attr, "all_partitions") != NULL) {
492                                         dbg("creation of partition nodes requested");
493                                         rule->partitions = DEFAULT_PARTITIONS_COUNT;
494                                 }
495                                 if (strstr(attr, "ignore_remove") != NULL) {
496                                         dbg("remove event should be ignored");
497                                         rule->ignore_remove = 1;
498                                 }
499                         }
500                         if (value[0] == '\0')
501                                 dbg("name empty, node creation supressed");
502                         add_rule_key(rule, &rule->name, operation, value);
503                         continue;
504                 }
505
506                 if (strcasecmp(key, "SYMLINK") == 0) {
507                         add_rule_key(rule, &rule->symlink, operation, value);
508                         valid = 1;
509                         continue;
510                 }
511
512                 if (strcasecmp(key, "OWNER") == 0) {
513                         valid = 1;
514                         if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
515                                 char *endptr;
516                                 strtoul(value, &endptr, 10);
517                                 if (endptr[0] != '\0') {
518                                         char owner[32];
519                                         uid_t uid = lookup_user(value);
520                                         dbg("replacing username='%s' by id=%i", value, uid);
521                                         sprintf(owner, "%u", (unsigned int) uid);
522                                         add_rule_key(rule, &rule->owner, operation, owner);
523                                         continue;
524                                 }
525                         }
526
527                         add_rule_key(rule, &rule->owner, operation, value);
528                         continue;
529                 }
530
531                 if (strcasecmp(key, "GROUP") == 0) {
532                         valid = 1;
533                         if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
534                                 char *endptr;
535                                 strtoul(value, &endptr, 10);
536                                 if (endptr[0] != '\0') {
537                                         char group[32];
538                                         gid_t gid = lookup_group(value);
539                                         dbg("replacing groupname='%s' by id=%i", value, gid);
540                                         sprintf(group, "%u", (unsigned int) gid);
541                                         add_rule_key(rule, &rule->group, operation, group);
542                                         continue;
543                                 }
544                         }
545
546                         add_rule_key(rule, &rule->group, operation, value);
547                         continue;
548                 }
549
550                 if (strcasecmp(key, "MODE") == 0) {
551                         rule->mode = strtol(value, NULL, 8);
552                         rule->mode_operation = operation;
553                         valid = 1;
554                         continue;
555                 }
556
557                 if (strcasecmp(key, "OPTIONS") == 0) {
558                         if (strstr(value, "last_rule") != NULL) {
559                                 dbg("last rule to be applied");
560                                 rule->last_rule = 1;
561                         }
562                         if (strstr(value, "ignore_device") != NULL) {
563                                 dbg("device should be ignored");
564                                 rule->ignore_device = 1;
565                         }
566                         if (strstr(value, "ignore_remove") != NULL) {
567                                 dbg("remove event should be ignored");
568                                 rule->ignore_remove = 1;
569                         }
570                         if (strstr(value, "all_partitions") != NULL) {
571                                 dbg("creation of partition nodes requested");
572                                 rule->partitions = DEFAULT_PARTITIONS_COUNT;
573                         }
574                         valid = 1;
575                         continue;
576                 }
577
578                 err("unknown key '%s' in %s:%u", key, filename, lineno);
579         }
580
581         if (physdev && rule->wait_for_sysfs.operation == KEY_OP_UNSET)
582                 err("PHYSDEV* values are deprecated and will be removed from a future kernel, "
583                     "please fix it in %s:%u", filename, lineno);
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         info("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_dir, &stats) != 0)
686                 return -1;
687
688         if ((stats.st_mode & S_IFMT) != S_IFDIR) {
689                 dbg("parse single rules file '%s'", udev_rules_dir);
690                 retval = parse_file(rules, udev_rules_dir);
691         } else {
692                 struct name_entry *name_loop, *name_tmp;
693                 LIST_HEAD(name_list);
694
695                 dbg("parse rules directory '%s'", udev_rules_dir);
696                 retval = add_matching_files(&name_list, udev_rules_dir, RULESFILE_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 }