chiark / gitweb /
IMPORT allow to import program returned keys into the env
[elogind.git] / udev_rules.c
1 /*
2  * udev_rules.c
3  *
4  * Userspace devfs
5  *
6  * Copyright (C) 2003 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 <fcntl.h>
30 #include <ctype.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <sys/wait.h>
34
35 #include "libsysfs/sysfs/libsysfs.h"
36 #include "list.h"
37 #include "udev_libc_wrapper.h"
38 #include "udev.h"
39 #include "udev_utils.h"
40 #include "udev_version.h"
41 #include "logging.h"
42 #include "udev_rules.h"
43 #include "udev_db.h"
44
45
46 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
47 static int strcmp_pattern(const char *p, const char *s)
48 {
49         if (s[0] == '\0') {
50                 while (p[0] == '*')
51                         p++;
52                 return (p[0] != '\0');
53         }
54         switch (p[0]) {
55         case '[':
56                 {
57                         int not = 0;
58                         p++;
59                         if (p[0] == '!') {
60                                 not = 1;
61                                 p++;
62                         }
63                         while ((p[0] != '\0') && (p[0] != ']')) {
64                                 int match = 0;
65                                 if (p[1] == '-') {
66                                         if ((s[0] >= p[0]) && (s[0] <= p[2]))
67                                                 match = 1;
68                                         p += 3;
69                                 } else {
70                                         match = (p[0] == s[0]);
71                                         p++;
72                                 }
73                                 if (match ^ not) {
74                                         while ((p[0] != '\0') && (p[0] != ']'))
75                                                 p++;
76                                         if (p[0] == ']')
77                                                 return strcmp_pattern(p+1, s+1);
78                                 }
79                         }
80                 }
81                 break;
82         case '*':
83                 if (strcmp_pattern(p, s+1))
84                         return strcmp_pattern(p+1, s);
85                 return 0;
86         case '\0':
87                 if (s[0] == '\0') {
88                         return 0;
89                 }
90                 break;
91         default:
92                 if ((p[0] == s[0]) || (p[0] == '?'))
93                         return strcmp_pattern(p+1, s+1);
94                 break;
95         }
96         return 1;
97 }
98
99 /* extract possible {attr} and move str behind it */
100 static char *get_format_attribute(char **str)
101 {
102         char *pos;
103         char *attr = NULL;
104
105         if (*str[0] == '{') {
106                 pos = strchr(*str, '}');
107                 if (pos == NULL) {
108                         err("missing closing brace for format");
109                         return NULL;
110                 }
111                 pos[0] = '\0';
112                 attr = *str+1;
113                 *str = pos+1;
114                 dbg("attribute='%s', str='%s'", attr, *str);
115         }
116         return attr;
117 }
118
119 /* extract possible format length and move str behind it*/
120 static int get_format_len(char **str)
121 {
122         int num;
123         char *tail;
124
125         if (isdigit(*str[0])) {
126                 num = (int) strtoul(*str, &tail, 10);
127                 if (num > 0) {
128                         *str = tail;
129                         dbg("format length=%i", num);
130                         return num;
131                 } else {
132                         err("format parsing error '%s'", *str);
133                 }
134         }
135         return -1;
136 }
137
138 static int get_key(char **line, char **key, char **value)
139 {
140         char *linepos;
141         char *temp;
142
143         linepos = *line;
144         if (!linepos)
145                 return -1;
146
147         if (strchr(linepos, '\\')) {
148                 dbg("escaped characters are not supported, skip");
149                 return -1;
150         }
151
152         /* skip whitespace */
153         while (isspace(linepos[0]))
154                 linepos++;
155
156         /* get the key */
157         *key = linepos;
158         while (1) {
159                 linepos++;
160                 if (linepos[0] == '\0')
161                         return -1;
162                 if (isspace(linepos[0]))
163                         break;
164                 if (linepos[0] == '=')
165                         break;
166         }
167
168         /* terminate key */
169         linepos[0] = '\0';
170         linepos++;
171
172         /* skip whitespace */
173         while (isspace(linepos[0]))
174                 linepos++;
175
176         /* get the value*/
177         if (linepos[0] == '\0')
178                 return -1;
179
180         if (linepos[0] == '"') {
181                 linepos++;
182                 temp = strchr(linepos, '"');
183                 if (!temp)
184                         return -1;
185                 temp[0] = '\0';
186         } else if (linepos[0] == '\'') {
187                 linepos++;
188                 temp = strchr(linepos, '\'');
189                 if (!temp)
190                         return -1;
191                 temp[0] = '\0';
192         } else {
193                 temp = linepos;
194                 while (temp[0] && !isspace(temp[0]))
195                         temp++;
196                 temp[0] = '\0';
197         }
198         *value = linepos;
199
200         return 0;
201 }
202
203 static int import_keys_into_env(const char *buf, size_t bufsize)
204 {
205         char line[LINE_SIZE];
206         const char *bufline;
207         char *linepos;
208         char *variable;
209         char *value;
210         size_t cur;
211         size_t count;
212         int lineno;
213
214         /* loop through the whole file */
215         lineno = 0;
216         cur = 0;
217         while (cur < bufsize) {
218                 count = buf_get_line(buf, bufsize, cur);
219                 bufline = &buf[cur];
220                 cur += count+1;
221                 lineno++;
222
223                 if (count >= sizeof(line)) {
224                         err("line too long, conf line skipped %s, line %d", udev_config_filename, lineno);
225                         continue;
226                 }
227
228                 /* eat the whitespace */
229                 while ((count > 0) && isspace(bufline[0])) {
230                         bufline++;
231                         count--;
232                 }
233                 if (count == 0)
234                         continue;
235
236                 /* see if this is a comment */
237                 if (bufline[0] == COMMENT_CHARACTER)
238                         continue;
239
240                 strlcpy(line, bufline, count+1);
241
242                 linepos = line;
243                 if (get_key(&linepos, &variable, &value) == 0) {
244                         dbg("import '%s=%s'", variable, value);
245                         setenv(variable, value, 1);
246                 }
247         }
248
249         return 0;
250 }
251
252 static int import_file_into_env(const char *filename)
253 {
254         char *buf;
255         size_t bufsize;
256
257         if (file_map(filename, &buf, &bufsize) != 0) {
258                 err("can't open '%s'", filename);
259                 return -1;
260         }
261         import_keys_into_env(buf, bufsize);
262         file_unmap(buf, bufsize);
263
264         return 0;
265 }
266
267 static int import_program_into_env(struct udevice *udev, const char *program)
268 {
269         char result[1024];
270         size_t reslen;
271
272         if (execute_program(program, udev->subsystem, result, sizeof(result), &reslen) != 0)
273                 return -1;
274         return import_keys_into_env(result, reslen);
275 }
276
277 /* finds the lowest positive N such that <name>N isn't present in the udevdb
278  * if <name> doesn't exist, 0 is returned, N otherwise
279  */
280 static int find_free_number(struct udevice *udev, const char *name)
281 {
282         char devpath[PATH_SIZE];
283         char filename[PATH_SIZE];
284         int num = 0;
285
286         strlcpy(filename, name, sizeof(filename));
287         while (1) {
288                 dbg("look for existing node '%s'", filename);
289                 if (udev_db_search_name(devpath, sizeof(devpath), filename) != 0) {
290                         dbg("free num=%d", num);
291                         return num;
292                 }
293
294                 num++;
295                 if (num > 1000) {
296                         info("find_free_number gone crazy (num=%d), aborted", num);
297                         return -1;
298                 }
299                 snprintf(filename, sizeof(filename), "%s%d", name, num);
300                 filename[sizeof(filename)-1] = '\0';
301         }
302 }
303
304 static int find_sysfs_attribute(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device,
305                                 const char *name, char *value, size_t len)
306 {
307         struct sysfs_attribute *tmpattr;
308
309         dbg("look for device attribute '%s'", name);
310         if (class_dev) {
311                 dbg("look for class attribute '%s/%s'", class_dev->path, name);
312                 tmpattr = sysfs_get_classdev_attr(class_dev, name);
313                 if (tmpattr)
314                         goto attr_found;
315         }
316         if (sysfs_device) {
317                 dbg("look for devices attribute '%s/%s'", sysfs_device->path, name);
318                 tmpattr = sysfs_get_device_attr(sysfs_device, name);
319                 if (tmpattr)
320                         goto attr_found;
321         }
322         return -1;
323
324 attr_found:
325         strlcpy(value, tmpattr->value, len);
326         remove_trailing_char(value, '\n');
327
328         dbg("found attribute '%s'", tmpattr->path);
329         return 0;
330 }
331
332 static void apply_format(struct udevice *udev, char *string, size_t maxsize,
333                          struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
334 {
335         char temp[PATH_SIZE];
336         char temp2[PATH_SIZE];
337         char *head, *tail, *pos, *cpos, *attr, *rest;
338         int len;
339         int i;
340         unsigned int next_free_number;
341         struct sysfs_class_device *class_dev_parent;
342         enum subst_type {
343                 SUBST_UNKNOWN,
344                 SUBST_DEVPATH,
345                 SUBST_ID,
346                 SUBST_KERNEL_NUMBER,
347                 SUBST_KERNEL_NAME,
348                 SUBST_MAJOR,
349                 SUBST_MINOR,
350                 SUBST_RESULT,
351                 SUBST_SYSFS,
352                 SUBST_ENUM,
353                 SUBST_PARENT,
354                 SUBST_TEMP_NODE,
355                 SUBST_ROOT,
356                 SUBST_MODALIAS,
357                 SUBST_ENV,
358         };
359         static const struct subst_map {
360                 char *name;
361                 char fmt;
362                 enum subst_type type;
363         } map[] = {
364                 { .name = "devpath",            .fmt = 'p',     .type = SUBST_DEVPATH },
365                 { .name = "id",                 .fmt = 'b',     .type = SUBST_ID },
366                 { .name = "number",             .fmt = 'n',     .type = SUBST_KERNEL_NUMBER },
367                 { .name = "kernel",             .fmt = 'k',     .type = SUBST_KERNEL_NAME },
368                 { .name = "major",              .fmt = 'M',     .type = SUBST_MAJOR },
369                 { .name = "minor",              .fmt = 'm',     .type = SUBST_MINOR },
370                 { .name = "result",             .fmt = 'c',     .type = SUBST_RESULT },
371                 { .name = "sysfs",              .fmt = 's',     .type = SUBST_SYSFS },
372                 { .name = "enum",               .fmt = 'e',     .type = SUBST_ENUM },
373                 { .name = "parent",             .fmt = 'P',     .type = SUBST_PARENT },
374                 { .name = "tempnode",           .fmt = 'N',     .type = SUBST_TEMP_NODE },
375                 { .name = "root",               .fmt = 'r',     .type = SUBST_ROOT },
376                 { .name = "modalias",           .fmt = 'A',     .type = SUBST_MODALIAS },
377                 { .name = "env",                .fmt = 'E',     .type = SUBST_ENV },
378                 {}
379         };
380         enum subst_type type;
381         const struct subst_map *subst;
382
383         head = string;
384         while (1) {
385                 len = -1;
386                 while (head[0] != '\0') {
387                         if (head[0] == '$') {
388                                 /* substitute named variable */
389                                 if (head[1] == '\0')
390                                         break;
391                                 if (head[1] == '$') {
392                                         strlcpy(temp, head+2, sizeof(temp));
393                                         strlcpy(head+1, temp, maxsize);
394                                         head++;
395                                         continue;
396                                 }
397                                 head[0] = '\0';
398                                 for (subst = map; subst->name; subst++) {
399                                         if (strncasecmp(&head[1], subst->name, strlen(subst->name)) == 0) {
400                                                 type = subst->type;
401                                                 tail = head + strlen(subst->name)+1;
402                                                 dbg("will substitute format name '%s'", subst->name);
403                                                 goto found;
404                                         }
405                                 }
406                         }
407                         else if (head[0] == '%') {
408                                 /* substitute format char */
409                                 if (head[1] == '\0')
410                                         break;
411                                 if (head[1] == '%') {
412                                         strlcpy(temp, head+2, sizeof(temp));
413                                         strlcpy(head+1, temp, maxsize);
414                                         head++;
415                                         continue;
416                                 }
417                                 head[0] = '\0';
418                                 tail = head+1;
419                                 len = get_format_len(&tail);
420                                 for (subst = map; subst->name; subst++) {
421                                         if (tail[0] == subst->fmt) {
422                                                 type = subst->type;
423                                                 tail++;
424                                                 dbg("will substitute format char '%c'", subst->fmt);
425                                                 goto found;
426                                         }
427                                 }
428                         }
429                         head++;
430                 }
431                 break;
432 found:
433                 attr = get_format_attribute(&tail);
434                 strlcpy(temp, tail, sizeof(temp));
435                 dbg("format=%i, string='%s', tail='%s', class_dev=%p, sysfs_dev=%p",
436                     type ,string, tail, class_dev, sysfs_device);
437
438                 switch (type) {
439                 case SUBST_DEVPATH:
440                         strlcat(string, udev->devpath, maxsize);
441                         dbg("substitute devpath '%s'", udev->devpath);
442                         break;
443                 case SUBST_ID:
444                         strlcat(string, udev->bus_id, maxsize);
445                         dbg("substitute bus_id '%s'", udev->bus_id);
446                         break;
447                 case SUBST_KERNEL_NAME:
448                         strlcat(string, udev->kernel_name, maxsize);
449                         dbg("substitute kernel name '%s'", udev->kernel_name);
450                         break;
451                 case SUBST_KERNEL_NUMBER:
452                         strlcat(string, udev->kernel_number, maxsize);
453                         dbg("substitute kernel number '%s'", udev->kernel_number);
454                         break;
455                 case SUBST_MAJOR:
456                         sprintf(temp2, "%d", major(udev->devt));
457                         strlcat(string, temp2, maxsize);
458                         dbg("substitute major number '%s'", temp2);
459                         break;
460                 case SUBST_MINOR:
461                         sprintf(temp2, "%d", minor(udev->devt));
462                         strlcat(string, temp2, maxsize);
463                         dbg("substitute minor number '%s'", temp2);
464                         break;
465                 case SUBST_RESULT:
466                         if (udev->program_result[0] == '\0')
467                                 break;
468                         /* get part part of the result string */
469                         i = 0;
470                         if (attr != NULL)
471                                 i = strtoul(attr, &rest, 10);
472                         if (i > 0) {
473                                 dbg("request part #%d of result string", i);
474                                 cpos = udev->program_result;
475                                 while (--i) {
476                                         while (cpos[0] != '\0' && !isspace(cpos[0]))
477                                                 cpos++;
478                                         while (isspace(cpos[0]))
479                                                 cpos++;
480                                 }
481                                 if (i > 0) {
482                                         err("requested part of result string not found");
483                                         break;
484                                 }
485                                 strlcpy(temp2, cpos, sizeof(temp2));
486                                 /* %{2+}c copies the whole string from the second part on */
487                                 if (rest[0] != '+') {
488                                         cpos = strchr(temp2, ' ');
489                                         if (cpos)
490                                                 cpos[0] = '\0';
491                                 }
492                                 strlcat(string, temp2, maxsize);
493                                 dbg("substitute part of result string '%s'", temp2);
494                         } else {
495                                 strlcat(string, udev->program_result, maxsize);
496                                 dbg("substitute result string '%s'", udev->program_result);
497                         }
498                         break;
499                 case SUBST_SYSFS:
500                         if (attr == NULL) {
501                                 dbg("missing attribute");
502                                 break;
503                         }
504                         if (find_sysfs_attribute(class_dev, sysfs_device, attr, temp2, sizeof(temp2)) != 0) {
505                                 struct sysfs_device *parent_device;
506
507                                 dbg("sysfs attribute '%s' not found, walk up the physical devices", attr);
508                                 parent_device = sysfs_get_device_parent(sysfs_device);
509                                 while (parent_device) {
510                                         dbg("looking at '%s'", parent_device->path);
511                                         if (find_sysfs_attribute(NULL, parent_device, attr, temp2, sizeof(temp2)) == 0)
512                                                 break;
513                                         parent_device = sysfs_get_device_parent(parent_device);
514                                 }
515                                 if (!parent_device)
516                                         break;
517                         }
518                         /* strip trailing whitespace of sysfs value */
519                         i = strlen(temp2);
520                         while (i > 0 && isspace(temp2[i-1]))
521                                 temp2[--i] = '\0';
522                         replace_untrusted_chars(temp2);
523                         strlcat(string, temp2, maxsize);
524                         dbg("substitute sysfs value '%s'", temp2);
525                         break;
526                 case SUBST_ENUM:
527                         next_free_number = find_free_number(udev, string);
528                         if (next_free_number > 0) {
529                                 sprintf(temp2, "%d", next_free_number);
530                                 strlcat(string, temp2, maxsize);
531                         }
532                         break;
533                 case SUBST_PARENT:
534                         if (!class_dev)
535                                 break;
536                         class_dev_parent = sysfs_get_classdev_parent(class_dev);
537                         if (class_dev_parent != NULL) {
538                                 struct udevice udev_parent;
539
540                                 dbg("found parent '%s', get the node name", class_dev_parent->path);
541                                 udev_init_device(&udev_parent, NULL, NULL, NULL);
542                                 /* lookup the name in the udev_db with the DEVPATH of the parent */
543                                 if (udev_db_get_device(&udev_parent, &class_dev_parent->path[strlen(sysfs_path)]) == 0) {
544                                         strlcat(string, udev_parent.name, maxsize);
545                                         dbg("substitute parent node name'%s'", udev_parent.name);
546                                 } else
547                                         dbg("parent not found in database");
548                                 udev_cleanup_device(&udev_parent);
549                         }
550                         break;
551                 case SUBST_TEMP_NODE:
552                         if (udev->tmp_node[0] == '\0') {
553                                 dbg("create temporary device node for callout");
554                                 snprintf(udev->tmp_node, sizeof(udev->tmp_node), "%s/.tmp-%u-%u",
555                                          udev_root, major(udev->devt), minor(udev->devt));
556                                 udev->tmp_node[sizeof(udev->tmp_node)-1] = '\0';
557                                 udev_make_node(udev, udev->tmp_node, udev->devt, 0600, 0, 0);
558                         }
559                         strlcat(string, udev->tmp_node, maxsize);
560                         dbg("substitute temporary device node name '%s'", udev->tmp_node);
561                         break;
562                 case SUBST_ROOT:
563                         strlcat(string, udev_root, maxsize);
564                         dbg("substitute udev_root '%s'", udev_root);
565                         break;
566                 case SUBST_MODALIAS:
567                         if (find_sysfs_attribute(NULL, sysfs_device, "modalias", temp2, sizeof(temp2)) != 0)
568                                 break;
569                         strlcat(string, temp2, maxsize);
570                         dbg("substitute MODALIAS '%s'", temp2);
571                         break;
572                 case SUBST_ENV:
573                         if (attr == NULL) {
574                                 dbg("missing attribute");
575                                 break;
576                         }
577                         pos = getenv(attr);
578                         if (pos == NULL)
579                                 break;
580                         strlcat(string, pos, maxsize);
581                         dbg("substitute env '%s=%s'", attr, pos);
582                         break;
583                 default:
584                         err("unknown substitution type=%i", type);
585                         break;
586                 }
587                 /* possibly truncate to format-char specified length */
588                 if (len != -1) {
589                         head[len] = '\0';
590                         dbg("truncate to %i chars, subtitution string becomes '%s'", len, head);
591                 }
592                 strlcat(string, temp, maxsize);
593         }
594 }
595
596 static int match_rule(struct udevice *udev, struct udev_rule *rule,
597                       struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device)
598 {
599         struct sysfs_device *parent_device = sysfs_device;
600
601         if (rule->action_operation != KEY_OP_UNSET) {
602                 dbg("check for " KEY_ACTION " rule->action='%s' udev->action='%s'",
603                     rule->action, udev->action);
604                 if (strcmp_pattern(rule->action, udev->action) != 0) {
605                         dbg(KEY_ACTION " is not matching");
606                         if (rule->action_operation != KEY_OP_NOMATCH)
607                                 goto exit;
608                 } else {
609                         dbg(KEY_ACTION " matches");
610                         if (rule->action_operation == KEY_OP_NOMATCH)
611                                 goto exit;
612                 }
613                 dbg(KEY_ACTION " key is true");
614         }
615
616         if (rule->kernel_operation != KEY_OP_UNSET) {
617                 dbg("check for " KEY_KERNEL " rule->kernel='%s' udev_kernel_name='%s'",
618                     rule->kernel, udev->kernel_name);
619                 if (strcmp_pattern(rule->kernel, udev->kernel_name) != 0) {
620                         dbg(KEY_KERNEL " is not matching");
621                         if (rule->kernel_operation != KEY_OP_NOMATCH)
622                                 goto exit;
623                 } else {
624                         dbg(KEY_KERNEL " matches");
625                         if (rule->kernel_operation == KEY_OP_NOMATCH)
626                                 goto exit;
627                 }
628                 dbg(KEY_KERNEL " key is true");
629         }
630
631         if (rule->subsystem_operation != KEY_OP_UNSET) {
632                 dbg("check for " KEY_SUBSYSTEM " rule->subsystem='%s' udev->subsystem='%s'",
633                     rule->subsystem, udev->subsystem);
634                 if (strcmp_pattern(rule->subsystem, udev->subsystem) != 0) {
635                         dbg(KEY_SUBSYSTEM " is not matching");
636                         if (rule->subsystem_operation != KEY_OP_NOMATCH)
637                                 goto exit;
638                 } else {
639                         dbg(KEY_SUBSYSTEM " matches");
640                         if (rule->subsystem_operation == KEY_OP_NOMATCH)
641                                 goto exit;
642                 }
643                 dbg(KEY_SUBSYSTEM " key is true");
644         }
645
646         if (rule->devpath_operation != KEY_OP_UNSET) {
647                 dbg("check for " KEY_DEVPATH " rule->devpath='%s' udev->devpath='%s'",
648                     rule->devpath, udev->devpath);
649                 if (strcmp_pattern(rule->devpath, udev->devpath) != 0) {
650                         dbg(KEY_DEVPATH " is not matching");
651                         if (rule->devpath_operation != KEY_OP_NOMATCH)
652                                 goto exit;
653                 } else {
654                         dbg(KEY_DEVPATH " matches");
655                         if (rule->devpath_operation == KEY_OP_NOMATCH)
656                                 goto exit;
657                 }
658                 dbg(KEY_DEVPATH " key is true");
659         }
660
661         if (rule->modalias_operation != KEY_OP_UNSET) {
662                 char value[NAME_SIZE];
663
664                 if (find_sysfs_attribute(NULL, sysfs_device, "modalias", value, sizeof(value)) != 0) {
665                         dbg(KEY_MODALIAS " value not found");
666                         goto exit;
667                 }
668                 dbg("check for " KEY_MODALIAS " rule->modalias='%s' modalias='%s'",
669                     rule->modalias, value);
670                 if (strcmp_pattern(rule->modalias, value) != 0) {
671                         dbg(KEY_MODALIAS " is not matching");
672                         if (rule->modalias_operation != KEY_OP_NOMATCH)
673                                 goto exit;
674                 } else {
675                         dbg(KEY_MODALIAS " matches");
676                         if (rule->modalias_operation == KEY_OP_NOMATCH)
677                                 goto exit;
678                 }
679                 dbg(KEY_MODALIAS " key is true");
680         }
681
682         if (rule->env_pair_count) {
683                 int i;
684
685                 dbg("check for " KEY_ENV " pairs");
686                 for (i = 0; i < rule->env_pair_count; i++) {
687                         struct key_pair *pair;
688                         const char *value;
689
690                         pair = &rule->env_pair[i];
691                         value = getenv(pair->name);
692                         if (!value) {
693                                 dbg(KEY_ENV "{'%s'} is not found", pair->name);
694                                 goto exit;
695                         }
696                         if (strcmp_pattern(pair->value, value) != 0) {
697                                 dbg(KEY_ENV "{'%s'} is not matching", pair->name);
698                                 if (pair->operation != KEY_OP_NOMATCH)
699                                         goto exit;
700                         } else {
701                                 dbg(KEY_ENV "{'%s'} matches", pair->name);
702                                 if (pair->operation == KEY_OP_NOMATCH)
703                                         goto exit;
704                         }
705                 }
706                 dbg(KEY_ENV " key is true");
707         }
708
709         /* walk up the chain of physical devices and find a match */
710         while (1) {
711                 /* check for matching driver */
712                 if (rule->driver_operation != KEY_OP_UNSET) {
713                         if (parent_device == NULL) {
714                                 dbg("device has no sysfs_device");
715                                 goto exit;
716                         }
717                         dbg("check for " KEY_DRIVER " rule->driver='%s' sysfs_device->driver_name='%s'",
718                             rule->driver, parent_device->driver_name);
719                         if (strcmp_pattern(rule->driver, parent_device->driver_name) != 0) {
720                                 dbg(KEY_DRIVER " is not matching");
721                                 if (rule->driver_operation != KEY_OP_NOMATCH)
722                                         goto try_parent;
723                         } else {
724                                 dbg(KEY_DRIVER " matches");
725                                 if (rule->driver_operation == KEY_OP_NOMATCH)
726                                         goto try_parent;
727                         }
728                         dbg(KEY_DRIVER " key is true");
729                 }
730
731                 /* check for matching bus value */
732                 if (rule->bus_operation != KEY_OP_UNSET) {
733                         if (parent_device == NULL) {
734                                 dbg("device has no sysfs_device");
735                                 goto exit;
736                         }
737                         dbg("check for " KEY_BUS " rule->bus='%s' sysfs_device->bus='%s'",
738                             rule->bus, parent_device->bus);
739                         if (strcmp_pattern(rule->bus, parent_device->bus) != 0) {
740                                 dbg(KEY_BUS " is not matching");
741                                 if (rule->bus_operation != KEY_OP_NOMATCH)
742                                         goto try_parent;
743                         } else {
744                                 dbg(KEY_BUS " matches");
745                                 if (rule->bus_operation == KEY_OP_NOMATCH)
746                                         goto try_parent;
747                         }
748                         dbg(KEY_BUS " key is true");
749                 }
750
751                 /* check for matching bus id */
752                 if (rule->id_operation != KEY_OP_UNSET) {
753                         if (parent_device == NULL) {
754                                 dbg("device has no sysfs_device");
755                                 goto exit;
756                         }
757                         dbg("check " KEY_ID);
758                         if (strcmp_pattern(rule->id, parent_device->bus_id) != 0) {
759                                 dbg(KEY_ID " is not matching");
760                                 if (rule->id_operation != KEY_OP_NOMATCH)
761                                         goto try_parent;
762                         } else {
763                                 dbg(KEY_ID " matches");
764                                 if (rule->id_operation == KEY_OP_NOMATCH)
765                                         goto try_parent;
766                         }
767                         dbg(KEY_ID " key is true");
768                 }
769
770                 /* check for matching sysfs pairs */
771                 if (rule->sysfs_pair_count) {
772                         int i;
773
774                         dbg("check " KEY_SYSFS " pairs");
775                         for (i = 0; i < rule->sysfs_pair_count; i++) {
776                                 struct key_pair *pair;
777                                 char value[VALUE_SIZE];
778                                 size_t len;
779
780                                 pair = &rule->sysfs_pair[i];
781                                 if (find_sysfs_attribute(class_dev, parent_device, pair->name, value, sizeof(value)) != 0)
782                                         goto try_parent;
783
784                                 /* strip trailing whitespace of value, if not asked to match for it */
785                                 len = strlen(pair->value);
786                                 if (len && !isspace(pair->value[len-1])) {
787                                         len = strlen(value);
788                                         while (len > 0 && isspace(value[len-1]))
789                                                 value[--len] = '\0';
790                                         dbg("removed %i trailing whitespace chars from '%s'", strlen(value)-len, value);
791                                 }
792
793                                 dbg("compare attribute '%s' value '%s' with '%s'", pair->name, value, pair->value);
794                                 if (strcmp_pattern(pair->value, value) != 0) {
795                                         dbg(KEY_SYSFS "{'%s'} is not matching", pair->name);
796                                         if (pair->operation != KEY_OP_NOMATCH)
797                                                 goto try_parent;
798                                 } else {
799                                         dbg(KEY_SYSFS "{'%s'} matches", pair->name);
800                                         if (pair->operation == KEY_OP_NOMATCH)
801                                                 goto try_parent;
802                                 }
803                         }
804                         dbg(KEY_SYSFS " keys are true");
805                 }
806
807                 /* found matching physical device  */
808                 break;
809 try_parent:
810                 dbg("try parent sysfs device");
811                 parent_device = sysfs_get_device_parent(parent_device);
812                 if (parent_device == NULL)
813                         goto exit;
814                 dbg("look at sysfs_device->path='%s'", parent_device->path);
815                 dbg("look at sysfs_device->bus_id='%s'", parent_device->bus_id);
816         }
817
818         /* import variables from file into environment */
819         if (rule->import_operation != KEY_OP_UNSET) {
820                 char import[PATH_SIZE];
821                 int rc = -1;
822
823                 strlcpy(import, rule->import, sizeof(import));
824                 apply_format(udev, import, sizeof(import), class_dev, sysfs_device);
825                 dbg("check for " KEY_IMPORT " import='%s'", import);
826                 if (rule->import_exec) {
827                         dbg("run executable file import='%s'", import);
828                         rc = import_program_into_env(udev, import);
829                 } else {
830                         dbg("import file import='%s'", import);
831                         rc = import_file_into_env(import);
832                 }
833                 if (rc) {
834                         dbg(KEY_IMPORT " failed");
835                         if (rule->import_operation != KEY_OP_NOMATCH)
836                                 goto exit;
837                 } else
838                         dbg(KEY_IMPORT " '%s' imported", rule->import);
839                 dbg(KEY_IMPORT " key is true");
840         }
841
842         /* execute external program */
843         if (rule->program_operation != KEY_OP_UNSET) {
844                 char program[PATH_SIZE];
845                 char result[PATH_SIZE];
846
847                 strlcpy(program, rule->program, sizeof(program));
848                 apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
849                 dbg("check for " KEY_PROGRAM " program='%s", program);
850                 if (execute_program(program, udev->subsystem, result, sizeof(result), NULL) != 0) {
851                         dbg(KEY_PROGRAM " is not matching");
852                         if (rule->program_operation != KEY_OP_NOMATCH)
853                                 goto exit;
854                 } else {
855                         dbg(KEY_PROGRAM " matches");
856                         remove_trailing_char(result, '\n');
857                         replace_untrusted_chars(result);
858                         dbg("result is '%s'", result);
859                         strlcpy(udev->program_result, result, sizeof(udev->program_result));
860                         dbg(KEY_PROGRAM " returned successful");
861                         if (rule->program_operation == KEY_OP_NOMATCH)
862                                 goto exit;
863                 }
864                 dbg(KEY_PROGRAM " key is true");
865         }
866
867         /* check for matching result of external program */
868         if (rule->result_operation != KEY_OP_UNSET) {
869                 dbg("check for " KEY_RESULT " rule->result='%s', udev->program_result='%s'",
870                    rule->result, udev->program_result);
871                 if (strcmp_pattern(rule->result, udev->program_result) != 0) {
872                         dbg(KEY_RESULT " is not matching");
873                         if (rule->result_operation != KEY_OP_NOMATCH)
874                                 goto exit;
875                 } else {
876                         dbg(KEY_RESULT " matches");
877                         if (rule->result_operation == KEY_OP_NOMATCH)
878                                 goto exit;
879                 }
880                 dbg(KEY_RESULT " key is true");
881         }
882
883         /* rule matches */
884         return 0;
885
886 exit:
887         return -1;
888 }
889
890 int udev_rules_get_name(struct udevice *udev, struct sysfs_class_device *class_dev)
891 {
892         struct sysfs_class_device *class_dev_parent;
893         struct sysfs_device *sysfs_device = NULL;
894         struct udev_rule *rule;
895
896         dbg("class_dev->name='%s'", class_dev->name);
897
898         /* Figure out where the "device"-symlink is at.  For char devices this will
899          * always be in the class_dev->path.  On block devices, only the main block
900          * device will have the device symlink in it's path. All partition devices
901          * need to look at the symlink in its parent directory.
902          */
903         class_dev_parent = sysfs_get_classdev_parent(class_dev);
904         if (class_dev_parent != NULL) {
905                 dbg("given class device has a parent, use this instead");
906                 sysfs_device = sysfs_get_classdev_device(class_dev_parent);
907         } else {
908                 sysfs_device = sysfs_get_classdev_device(class_dev);
909         }
910
911         if (sysfs_device) {
912                 dbg("found devices device: path='%s', bus_id='%s', bus='%s'",
913                     sysfs_device->path, sysfs_device->bus_id, sysfs_device->bus);
914                 strlcpy(udev->bus_id, sysfs_device->bus_id, sizeof(udev->bus_id));
915         }
916
917         dbg("udev->kernel_name='%s'", udev->kernel_name);
918
919         /* look for a matching rule to apply */
920         udev_rules_iter_init();
921         while (1) {
922                 rule = udev_rules_iter_next();
923                 if (rule == NULL)
924                         break;
925
926                 if (udev->name_set && rule->name_operation != KEY_OP_UNSET) {
927                         dbg("node name already set, rule ignored");
928                         continue;
929                 }
930
931                 dbg("process rule");
932                 if (match_rule(udev, rule, class_dev, sysfs_device) == 0) {
933                         /* apply options */
934                         if (rule->ignore_device) {
935                                 info("configured rule in '%s[%i]' applied, '%s' is ignored",
936                                      rule->config_file, rule->config_line, udev->kernel_name);
937                                 udev->ignore_device = 1;
938                                 return 0;
939                         }
940                         if (rule->ignore_remove) {
941                                 udev->ignore_remove = 1;
942                                 dbg("remove event should be ignored");
943                         }
944                         /* apply all_partitions option only at a main block device */
945                         if (rule->partitions && udev->type == DEV_BLOCK && udev->kernel_number[0] == '\0') {
946                                 udev->partitions = rule->partitions;
947                                 dbg("creation of partition nodes requested");
948                         }
949
950                         /* apply permissions */
951                         if (!udev->mode_final && rule->mode != 0000) {
952                                 if (rule->mode_operation == KEY_OP_ASSIGN_FINAL)
953                                         udev->mode_final = 1;
954                                 udev->mode = rule->mode;
955                                 dbg("applied mode=%#o to '%s'", udev->mode, udev->kernel_name);
956                         }
957                         if (!udev->owner_final && rule->owner[0] != '\0') {
958                                 if (rule->owner_operation == KEY_OP_ASSIGN_FINAL)
959                                         udev->owner_final = 1;
960                                 strlcpy(udev->owner, rule->owner, sizeof(udev->owner));
961                                 apply_format(udev, udev->owner, sizeof(udev->owner), class_dev, sysfs_device);
962                                 dbg("applied owner='%s' to '%s'", udev->owner, udev->kernel_name);
963                         }
964                         if (!udev->group_final && rule->group[0] != '\0') {
965                                 if (rule->group_operation == KEY_OP_ASSIGN_FINAL)
966                                         udev->group_final = 1;
967                                 strlcpy(udev->group, rule->group, sizeof(udev->group));
968                                 apply_format(udev, udev->group, sizeof(udev->group), class_dev, sysfs_device);
969                                 dbg("applied group='%s' to '%s'", udev->group, udev->kernel_name);
970                         }
971
972                         /* collect symlinks */
973                         if (!udev->symlink_final && rule->symlink_operation != KEY_OP_UNSET) {
974                                 char temp[PATH_SIZE];
975                                 char *pos, *next;
976
977                                 if (rule->symlink_operation == KEY_OP_ASSIGN_FINAL)
978                                         udev->symlink_final = 1;
979                                 if (rule->symlink_operation == KEY_OP_ASSIGN || rule->symlink_operation == KEY_OP_ASSIGN_FINAL) {
980                                         struct name_entry *name_loop;
981                                         struct name_entry *temp_loop;
982
983                                         info("reset symlink list");
984                                         list_for_each_entry_safe(name_loop, temp_loop, &udev->symlink_list, node) {
985                                                 list_del(&name_loop->node);
986                                                 free(name_loop);
987                                         }
988                                 }
989                                 if (rule->symlink[0] != '\0') {
990                                         info("configured rule in '%s[%i]' applied, added symlink '%s'",
991                                              rule->config_file, rule->config_line, rule->symlink);
992                                         strlcpy(temp, rule->symlink, sizeof(temp));
993                                         apply_format(udev, temp, sizeof(temp), class_dev, sysfs_device);
994
995                                         /* add multiple symlinks separated by spaces */
996                                         pos = temp;
997                                         next = strchr(temp, ' ');
998                                         while (next) {
999                                                 next[0] = '\0';
1000                                                 info("add symlink '%s'", pos);
1001                                                 name_list_add(&udev->symlink_list, pos, 0);
1002                                                 pos = &next[1];
1003                                                 next = strchr(pos, ' ');
1004                                         }
1005                                         info("add symlink '%s'", pos);
1006                                         name_list_add(&udev->symlink_list, pos, 0);
1007                                 }
1008                         }
1009
1010                         /* set name, later rules with name set will be ignored */
1011                         if (rule->name_operation != KEY_OP_UNSET) {
1012                                 udev->name_set = 1;
1013                                 if (rule->name[0] == '\0') {
1014                                         info("configured rule in '%s[%i]' applied, node handling for '%s' supressed",
1015                                              rule->config_file, rule->config_line, udev->kernel_name);
1016                                 } else {
1017                                         strlcpy(udev->name, rule->name, sizeof(udev->name));
1018                                         apply_format(udev, udev->name, sizeof(udev->name), class_dev, sysfs_device);
1019                                         strlcpy(udev->config_file, rule->config_file, sizeof(udev->config_file));
1020                                         udev->config_line = rule->config_line;
1021
1022                                         info("configured rule in '%s:%i' applied, '%s' becomes '%s'",
1023                                              rule->config_file, rule->config_line, udev->kernel_name, rule->name);
1024                                         if (udev->type != DEV_NET)
1025                                                 dbg("name, '%s' is going to have owner='%s', group='%s', mode=%#o partitions=%i",
1026                                                     udev->name, udev->owner, udev->group, udev->mode, udev->partitions);
1027                                 }
1028                         }
1029
1030                         if (!udev->run_final && rule->run_operation != KEY_OP_UNSET) {
1031                                 char program[PATH_SIZE];
1032
1033                                 if (rule->run_operation == KEY_OP_ASSIGN_FINAL)
1034                                         udev->run_final = 1;
1035                                 if (rule->run_operation == KEY_OP_ASSIGN || rule->run_operation == KEY_OP_ASSIGN_FINAL) {
1036                                         struct name_entry *name_loop;
1037                                         struct name_entry *temp_loop;
1038
1039                                         info("reset run list");
1040                                         list_for_each_entry_safe(name_loop, temp_loop, &udev->run_list, node) {
1041                                                 list_del(&name_loop->node);
1042                                                 free(name_loop);
1043                                         }
1044                                 }
1045                                 if (rule->run[0] != '\0') {
1046                                         strlcpy(program, rule->run, sizeof(program));
1047                                         apply_format(udev, program, sizeof(program), class_dev, sysfs_device);
1048                                         dbg("add run '%s'", program);
1049                                         name_list_add(&udev->run_list, program, 0);
1050                                 }
1051                         }
1052
1053                         if (rule->last_rule) {
1054                                 dbg("last rule to be applied");
1055                                 break;
1056                         }
1057                 }
1058         }
1059
1060         if (udev->name[0] == '\0') {
1061                 strlcpy(udev->name, udev->kernel_name, sizeof(udev->name));
1062                 info("no rule found, use kernel name '%s'", udev->name);
1063         }
1064
1065         if (udev->tmp_node[0] != '\0') {
1066                 dbg("removing temporary device node");
1067                 unlink_secure(udev->tmp_node);
1068                 udev->tmp_node[0] = '\0';
1069         }
1070
1071         return 0;
1072 }
1073
1074 int udev_rules_get_run(struct udevice *udev, struct sysfs_device *sysfs_device)
1075 {
1076         struct udev_rule *rule;
1077
1078         /* look for a matching rule to apply */
1079         udev_rules_iter_init();
1080         while (1) {
1081                 rule = udev_rules_iter_next();
1082                 if (rule == NULL)
1083                         break;
1084
1085                 dbg("process rule");
1086                 if (rule->run_operation == KEY_OP_UNSET)
1087                         continue;
1088
1089                 if (rule->name_operation != KEY_OP_UNSET || rule->symlink_operation != KEY_OP_UNSET ||
1090                     rule->mode != 0000 || rule->owner[0] != '\0' || rule->group[0] != '\0') {
1091                         dbg("skip rule that names a device");
1092                         continue;
1093                 }
1094
1095                 if (match_rule(udev, rule, NULL, sysfs_device) == 0) {
1096                         if (rule->ignore_device) {
1097                                 info("configured rule in '%s[%i]' applied, '%s' is ignored",
1098                                      rule->config_file, rule->config_line, udev->kernel_name);
1099                                 udev->ignore_device = 1;
1100                                 return 0;
1101                         }
1102
1103                         if (!udev->run_final && rule->run_operation != KEY_OP_UNSET) {
1104                                 char program[PATH_SIZE];
1105
1106                                 if (rule->run_operation == KEY_OP_ASSIGN || rule->run_operation == KEY_OP_ASSIGN_FINAL) {
1107                                         struct name_entry *name_loop;
1108                                         struct name_entry *temp_loop;
1109
1110                                         info("reset run list");
1111                                         list_for_each_entry_safe(name_loop, temp_loop, &udev->run_list, node) {
1112                                                 list_del(&name_loop->node);
1113                                                 free(name_loop);
1114                                         }
1115                                 }
1116                                 if (rule->run[0] != '\0') {
1117                                         strlcpy(program, rule->run, sizeof(program));
1118                                         apply_format(udev, program, sizeof(program), NULL, sysfs_device);
1119                                         dbg("add run '%s'", program);
1120                                         name_list_add(&udev->run_list, program, 0);
1121                                 }
1122                                 if (rule->run_operation == KEY_OP_ASSIGN_FINAL)
1123                                         break;
1124                         }
1125
1126                         if (rule->last_rule) {
1127                                 dbg("last rule to be applied");
1128                                 break;
1129                         }
1130                 }
1131         }
1132
1133         return 0;
1134 }