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