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