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