chiark / gitweb /
update SUSE rules
[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, not creation supressed");
418                         add_rule_key(rule, &rule->name, operation, value);
419                         continue;
420                 }
421
422                 if (strcasecmp(key, "SYMLINK") == 0) {
423                         add_rule_key(rule, &rule->symlink, operation, value);
424                         valid = 1;
425                         continue;
426                 }
427
428                 if (strcasecmp(key, "OWNER") == 0) {
429                         valid = 1;
430                         if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
431                                 char *endptr;
432                                 strtoul(value, &endptr, 10);
433                                 if (endptr[0] != '\0') {
434                                         char owner[32];
435                                         uid_t uid = lookup_user(value);
436                                         dbg("replacing username='%s' by id=%i", value, uid);
437                                         sprintf(owner, "%u", (unsigned int) uid);
438                                         add_rule_key(rule, &rule->owner, operation, owner);
439                                         continue;
440                                 }
441                         }
442
443                         add_rule_key(rule, &rule->owner, operation, value);
444                         continue;
445                 }
446
447                 if (strcasecmp(key, "GROUP") == 0) {
448                         valid = 1;
449                         if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
450                                 char *endptr;
451                                 strtoul(value, &endptr, 10);
452                                 if (endptr[0] != '\0') {
453                                         char group[32];
454                                         gid_t gid = lookup_group(value);
455                                         dbg("replacing groupname='%s' by id=%i", value, gid);
456                                         sprintf(group, "%u", (unsigned int) gid);
457                                         add_rule_key(rule, &rule->group, operation, group);
458                                         continue;
459                                 }
460                         }
461
462                         add_rule_key(rule, &rule->group, operation, value);
463                         continue;
464                 }
465
466                 if (strcasecmp(key, "MODE") == 0) {
467                         rule->mode = strtol(value, NULL, 8);
468                         rule->mode_operation = operation;
469                         valid = 1;
470                         continue;
471                 }
472
473                 if (strcasecmp(key, "RUN") == 0) {
474                         add_rule_key(rule, &rule->run, operation, value);
475                         valid = 1;
476                         continue;
477                 }
478
479                 if (strcasecmp(key, "OPTIONS") == 0) {
480                         if (strstr(value, "last_rule") != NULL) {
481                                 dbg("last rule to be applied");
482                                 rule->last_rule = 1;
483                         }
484                         if (strstr(value, "ignore_device") != NULL) {
485                                 dbg("device should be ignored");
486                                 rule->ignore_device = 1;
487                         }
488                         if (strstr(value, "ignore_remove") != NULL) {
489                                 dbg("remove event should be ignored");
490                                 rule->ignore_remove = 1;
491                         }
492                         if (strstr(value, "all_partitions") != NULL) {
493                                 dbg("creation of partition nodes requested");
494                                 rule->partitions = DEFAULT_PARTITIONS_COUNT;
495                         }
496                         valid = 1;
497                         continue;
498                 }
499
500                 err("unknown key '%s', in '%s'", key, line);
501         }
502
503         /* skip line if not any valid key was found */
504         if (!valid) {
505                 err("invalid rule '%s'", line);
506                 goto exit;
507         }
508
509         /* grow buffer and add rule */
510         rule_size = sizeof(struct udev_rule) + rule->bufsize;
511         padding = (sizeof(size_t) - rule_size % sizeof(size_t)) % sizeof(size_t);
512         dbg("add %zi padding bytes", padding);
513         rule_size += padding;
514         rule->bufsize += padding;
515
516         rules->buf = realloc(rules->buf, rules->bufsize + rule_size);
517         if (!rules->buf) {
518                 err("realloc failed");
519                 goto exit;
520         }
521         dbg("adding rule to offset %zi", rules->bufsize);
522         memcpy(rules->buf + rules->bufsize, rule, rule_size);
523         rules->bufsize += rule_size;
524 exit:
525         free(rule);
526         return 0;
527 }
528
529 static int parse_file(struct udev_rules *rules, const char *filename)
530 {
531         char line[LINE_SIZE];
532         char *bufline;
533         int lineno;
534         char *buf;
535         size_t bufsize;
536         size_t cur;
537         size_t count;
538         int retval = 0;
539
540         if (file_map(filename, &buf, &bufsize) != 0) {
541                 err("can't open '%s' as rules file: %s", filename, strerror(errno));
542                 return -1;
543         }
544         dbg("reading '%s' as rules file", filename);
545
546         /* loop through the whole file */
547         cur = 0;
548         lineno = 0;
549         while (cur < bufsize) {
550                 unsigned int i, j;
551
552                 count = buf_get_line(buf, bufsize, cur);
553                 bufline = &buf[cur];
554                 cur += count+1;
555                 lineno++;
556
557                 if (count >= sizeof(line)) {
558                         info("line too long, rule skipped %s, line %d", filename, lineno);
559                         continue;
560                 }
561
562                 /* eat the whitespace */
563                 while ((count > 0) && isspace(bufline[0])) {
564                         bufline++;
565                         count--;
566                 }
567                 if (count == 0)
568                         continue;
569
570                 /* see if this is a comment */
571                 if (bufline[0] == COMMENT_CHARACTER)
572                         continue;
573
574                 /* skip backslash and newline from multi line rules */
575                 for (i = j = 0; i < count; i++) {
576                         if (bufline[i] == '\\' && bufline[i+1] == '\n')
577                                 continue;
578
579                         line[j++] = bufline[i];
580                 }
581                 line[j] = '\0';
582
583                 dbg("read '%s'", line);
584                 add_to_rules(rules, line);
585         }
586
587         file_unmap(buf, bufsize);
588         return retval;
589 }
590
591 int udev_rules_init(struct udev_rules *rules, int resolve_names)
592 {
593         struct stat stats;
594         int retval;
595
596         memset(rules, 0x00, sizeof(struct udev_rules));
597         rules->resolve_names = resolve_names;
598
599         /* parse rules file or all matching files in directory */
600         if (stat(udev_rules_filename, &stats) != 0)
601                 return -1;
602
603         if ((stats.st_mode & S_IFMT) != S_IFDIR) {
604                 dbg("parse single rules file '%s'", udev_rules_filename);
605                 retval = parse_file(rules, udev_rules_filename);
606         } else {
607                 struct name_entry *name_loop, *name_tmp;
608                 LIST_HEAD(name_list);
609
610                 dbg("parse rules directory '%s'", udev_rules_filename);
611                 retval = add_matching_files(&name_list, udev_rules_filename, RULEFILE_SUFFIX);
612
613                 list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
614                         if (stat(name_loop->name, &stats) == 0) {
615                                 if (stats.st_size)
616                                         parse_file(rules, name_loop->name);
617                                 else
618                                         dbg("empty rules file '%s'", name_loop->name);
619                         } else
620                                 err("could not read '%s': %s", name_loop->name, strerror(errno));
621                         list_del(&name_loop->node);
622                         free(name_loop);
623                 }
624         }
625
626         return retval;
627 }
628
629 void udev_rules_close(struct udev_rules *rules)
630 {
631         if (rules->buf) {
632                 free(rules->buf);
633                 rules->buf = NULL;
634         }
635 }