chiark / gitweb /
fix typo in group assignment
[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_exec = 1;
315                         } else if (attr && strstr(attr, "file")) {
316                                 dbg("IMPORT will be included as file");
317                         } else {
318                                 /* figure it out if it is executable */
319                                 char file[PATH_SIZE];
320                                 char *pos;
321                                 struct stat stats;
322
323                                 strlcpy(file, value, sizeof(file));
324                                 pos = strchr(file, ' ');
325                                 if (pos)
326                                         pos[0] = '\0';
327                                 dbg("IMPORT auto mode for '%s'", file);
328                                 if (!lstat(file, &stats) && (stats.st_mode & S_IXUSR)) {
329                                                 dbg("IMPORT is executable, will be executed");
330                                                 rule->import_exec = 1;
331                                 }
332                         }
333                         add_rule_key(rule, &rule->import, operation, value);
334                         valid = 1;
335                         continue;
336                 }
337
338                 if (strcasecmp(key, "DRIVER") == 0) {
339                         add_rule_key(rule, &rule->driver, operation, value);
340                         valid = 1;
341                         continue;
342                 }
343
344                 if (strcasecmp(key, "RESULT") == 0) {
345                         add_rule_key(rule, &rule->result, operation, value);
346                         valid = 1;
347                         continue;
348                 }
349
350                 if (strcasecmp(key, "PROGRAM") == 0) {
351                         add_rule_key(rule, &rule->program, operation, value);
352                         valid = 1;
353                         continue;
354                 }
355
356                 if (strncasecmp(key, "NAME", sizeof("NAME")-1) == 0) {
357                         attr = get_key_attribute(key + sizeof("NAME")-1);
358                         if (attr != NULL) {
359                                 if (strstr(attr, "all_partitions") != NULL) {
360                                         dbg("creation of partition nodes requested");
361                                         rule->partitions = DEFAULT_PARTITIONS_COUNT;
362                                 }
363                                 if (strstr(attr, "ignore_remove") != NULL) {
364                                         dbg("remove event should be ignored");
365                                         rule->ignore_remove = 1;
366                                 }
367                         }
368                         if (value[0] == '\0') {
369                                 dbg("name empty device should be ignored");
370                                 rule->name.operation = operation;
371                                 rule->ignore_device = 1;
372                         } else
373                                 add_rule_key(rule, &rule->name, operation, value);
374                         continue;
375                 }
376
377                 if (strcasecmp(key, "SYMLINK") == 0) {
378                         add_rule_key(rule, &rule->symlink, operation, value);
379                         valid = 1;
380                         continue;
381                 }
382
383                 if (strcasecmp(key, "OWNER") == 0) {
384                         valid = 1;
385                         if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
386                                 char *endptr;
387                                 strtoul(value, &endptr, 10);
388                                 if (endptr[0] != '\0') {
389                                         char owner[32];
390                                         uid_t uid = lookup_user(value);
391                                         dbg("replacing username='%s' by id=%i", value, uid);
392                                         sprintf(owner, "%li", uid);
393                                         add_rule_key(rule, &rule->owner, operation, owner);
394                                         continue;
395                                 }
396                         }
397
398                         add_rule_key(rule, &rule->owner, operation, value);
399                         continue;
400                 }
401
402                 if (strcasecmp(key, "GROUP") == 0) {
403                         valid = 1;
404                         if (rules->resolve_names && (!strchr(value, '$') && !strchr(value, '%'))) {
405                                 char *endptr;
406                                 strtoul(value, &endptr, 10);
407                                 if (endptr[0] != '\0') {
408                                         char group[32];
409                                         gid_t gid = lookup_group(value);
410                                         dbg("replacing groupname='%s' by id=%i", value, gid);
411                                         sprintf(group, "%li", gid);
412                                         add_rule_key(rule, &rule->group, operation, group);
413                                         continue;
414                                 }
415                         }
416
417                         add_rule_key(rule, &rule->group, operation, value);
418                         continue;
419                 }
420
421                 if (strcasecmp(key, "MODE") == 0) {
422                         rule->mode = strtol(value, NULL, 8);
423                         rule->mode_operation = operation;
424                         valid = 1;
425                         continue;
426                 }
427
428                 if (strcasecmp(key, "RUN") == 0) {
429                         add_rule_key(rule, &rule->run, operation, value);
430                         valid = 1;
431                         continue;
432                 }
433
434                 if (strcasecmp(key, "OPTIONS") == 0) {
435                         if (strstr(value, "last_rule") != NULL) {
436                                 dbg("last rule to be applied");
437                                 rule->last_rule = 1;
438                         }
439                         if (strstr(value, "ignore_device") != NULL) {
440                                 dbg("device should be ignored");
441                                 rule->ignore_device = 1;
442                         }
443                         if (strstr(value, "ignore_remove") != NULL) {
444                                 dbg("remove event should be ignored");
445                                 rule->ignore_remove = 1;
446                         }
447                         if (strstr(value, "all_partitions") != NULL) {
448                                 dbg("creation of partition nodes requested");
449                                 rule->partitions = DEFAULT_PARTITIONS_COUNT;
450                         }
451                         valid = 1;
452                         continue;
453                 }
454
455                 err("unknown key '%s', in '%s'", key, line);
456         }
457
458         /* skip line if not any valid key was found */
459         if (!valid) {
460                 err("invalid rule '%s'", line);
461                 goto exit;
462         }
463
464         /* grow buffer and add rule */
465         rule_size = sizeof(struct udev_rule) + rule->bufsize;
466         rules->buf = realloc(rules->buf, rules->bufsize + rule_size);
467         if (!rules->buf) {
468                 err("realloc failed");
469                 goto exit;
470         }
471         memcpy(rules->buf + rules->bufsize, rule, rule_size);
472         rules->bufsize += rule_size;
473 exit:
474         free(rule);
475         return 0;
476 }
477
478 static int parse_file(struct udev_rules *rules, const char *filename)
479 {
480         char line[LINE_SIZE];
481         char *bufline;
482         int lineno;
483         char *buf;
484         size_t bufsize;
485         size_t cur;
486         size_t count;
487         int retval = 0;
488
489         if (file_map(filename, &buf, &bufsize) != 0) {
490                 err("can't open '%s' as rules file", filename);
491                 return -1;
492         }
493         dbg("reading '%s' as rules file", filename);
494
495         /* loop through the whole file */
496         cur = 0;
497         lineno = 0;
498         while (cur < bufsize) {
499                 unsigned int i, j;
500
501                 count = buf_get_line(buf, bufsize, cur);
502                 bufline = &buf[cur];
503                 cur += count+1;
504                 lineno++;
505
506                 if (count >= sizeof(line)) {
507                         info("line too long, rule skipped %s, line %d", filename, lineno);
508                         continue;
509                 }
510
511                 /* eat the whitespace */
512                 while ((count > 0) && isspace(bufline[0])) {
513                         bufline++;
514                         count--;
515                 }
516                 if (count == 0)
517                         continue;
518
519                 /* see if this is a comment */
520                 if (bufline[0] == COMMENT_CHARACTER)
521                         continue;
522
523                 /* skip backslash and newline from multi line rules */
524                 for (i = j = 0; i < count; i++) {
525                         if (bufline[i] == '\\' && bufline[i+1] == '\n')
526                                 continue;
527
528                         line[j++] = bufline[i];
529                 }
530                 line[j] = '\0';
531
532                 dbg("read '%s'", line);
533                 add_to_rules(rules, line);
534         }
535
536         file_unmap(buf, bufsize);
537         return retval;
538 }
539
540 static int rules_map(struct udev_rules *rules, const char *filename)
541 {
542         if (file_map(filename, &rules->buf, &rules->bufsize)) {
543                 rules->buf = NULL;
544                 return -1;
545         }
546         if (rules->bufsize == 0) {
547                 file_unmap(rules->buf, rules->bufsize);
548                 rules->buf = NULL;
549                 return -1;
550         }
551
552         return 0;
553 }
554
555 int udev_rules_init(struct udev_rules *rules, int resolve_names)
556 {
557         char comp[PATH_SIZE];
558         struct stat stats;
559         int retval;
560
561         memset(rules, 0x00, sizeof(struct udev_rules));
562         rules->resolve_names = resolve_names;
563
564         /* check for precompiled rules */
565         strlcpy(comp, udev_rules_filename, sizeof(comp));
566         strlcat(comp, ".compiled", sizeof(comp));
567         if (stat(comp, &stats) == 0) {
568                 dbg("map compiled rules '%s'", comp);
569                 if (rules_map(rules, comp) == 0)
570                         return 0;
571         }
572
573         if (stat(udev_rules_filename, &stats) != 0)
574                 return -1;
575
576         if ((stats.st_mode & S_IFMT) != S_IFDIR) {
577                 dbg("parse single rules file '%s'", udev_rules_filename);
578                 retval = parse_file(rules, udev_rules_filename);
579         } else {
580                 struct name_entry *name_loop, *name_tmp;
581                 LIST_HEAD(name_list);
582
583                 dbg("parse rules directory '%s'", udev_rules_filename);
584                 retval = add_matching_files(&name_list, udev_rules_filename, RULEFILE_SUFFIX);
585
586                 list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
587                         parse_file(rules, name_loop->name);
588                         list_del(&name_loop->node);
589                 }
590         }
591
592         return retval;
593 }
594
595 void udev_rules_close(struct udev_rules *rules)
596 {
597         if (rules->mapped)
598                 file_unmap(rules->buf, rules->bufsize);
599         else
600                 free(rules->buf);
601
602         rules->buf = NULL;
603 }