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