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