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