chiark / gitweb /
[PATCH] fix permission handling logic
authorgreg@kroah.com <greg@kroah.com>
Thu, 4 Dec 2003 02:33:58 +0000 (18:33 -0800)
committerGreg KH <gregkh@suse.de>
Wed, 27 Apr 2005 04:13:05 +0000 (21:13 -0700)
Now we can handle wildcards properly within the permission file.

namedev.c
namedev.h
namedev_parse.c
test/udev.permissions

index f4c263f..4e0476c 100644 (file)
--- a/namedev.c
+++ b/namedev.c
@@ -39,6 +39,7 @@
 #include "klibc_fixups.h"
 
 LIST_HEAD(config_device_list);
+LIST_HEAD(perm_device_list);
 
 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
 static int strcmp_pattern(const char *p, const char *s)
@@ -113,7 +114,6 @@ int add_config_dev(struct config_device *new_dev)
                if (strncmp(dev->bus, new_dev->bus, sizeof(dev->name)))
                        continue;
                copy_var(dev, new_dev, type);
-               copy_var(dev, new_dev, mode);
                copy_string(dev, new_dev, bus);
                copy_string(dev, new_dev, sysfs_file);
                copy_string(dev, new_dev, sysfs_value);
@@ -121,8 +121,6 @@ int add_config_dev(struct config_device *new_dev)
                copy_string(dev, new_dev, place);
                copy_string(dev, new_dev, kernel_name);
                copy_string(dev, new_dev, exec_program);
-               copy_string(dev, new_dev, owner);
-               copy_string(dev, new_dev, group);
                return 0;
        }
 
@@ -136,6 +134,46 @@ int add_config_dev(struct config_device *new_dev)
        return 0;
 }
 
+int add_perm_dev(struct perm_device *new_dev)
+{
+       struct list_head *tmp;
+       struct perm_device *tmp_dev;
+
+       /* update the values if we already have the device */
+       list_for_each(tmp, &perm_device_list) {
+               struct perm_device *dev = list_entry(tmp, struct perm_device, node);
+               if (strcmp_pattern(new_dev->name, dev->name))
+                       continue;
+               copy_var(dev, new_dev, mode);
+               copy_string(dev, new_dev, owner);
+               copy_string(dev, new_dev, group);
+               return 0;
+       }
+
+       /* not found, add new structure to the perm list */
+       tmp_dev = malloc(sizeof(*tmp_dev));
+       if (!tmp_dev)
+               return -ENOMEM;
+       memcpy(tmp_dev, new_dev, sizeof(*tmp_dev));
+       list_add_tail(&tmp_dev->node, &perm_device_list);
+       //dump_perm_dev(tmp_dev);
+       return 0;
+}
+
+static struct perm_device *find_perm(char *name)
+{
+       struct list_head *tmp;
+       struct perm_device *perm = NULL;
+
+       list_for_each(tmp, &perm_device_list) {
+               perm = list_entry(tmp, struct perm_device, node);
+               if (strcmp_pattern(perm->name, name))
+                       continue;
+               return perm;
+       }
+       return NULL;
+}
+
 static mode_t get_default_mode(struct sysfs_class_device *class_dev)
 {
        mode_t mode = 0600;     /* default to owner rw only */
@@ -328,15 +366,8 @@ static int do_callout(struct sysfs_class_device *class_dev, struct udevice *udev
                if (strcmp_pattern(dev->id, udev->callout_value) != 0)
                        continue;
                strfieldcpy(udev->name, dev->name);
-               if (dev->mode != 0) {
-                       udev->mode = dev->mode;
-                       strfieldcpy(udev->owner, dev->owner);
-                       strfieldcpy(udev->group, dev->group);
-               }
-               dbg("callout returned matching value '%s', '%s' becomes '%s'"
-                   " - owner='%s', group='%s', mode=%#o",
-                   dev->id, class_dev->name, udev->name,
-                   dev->owner, dev->group, dev->mode);
+               dbg("callout returned matching value '%s', '%s' becomes '%s'",
+                   dev->id, class_dev->name, udev->name);
                return 0;
        }
        return -ENODEV;
@@ -382,15 +413,8 @@ label_found:
                        continue;
 
                strfieldcpy(udev->name, dev->name);
-               if (dev->mode != 0) {
-                       udev->mode = dev->mode;
-                       strfieldcpy(udev->owner, dev->owner);
-                       strfieldcpy(udev->group, dev->group);
-               }
-               dbg("found matching attribute '%s', '%s' becomes '%s' "
-                         "- owner='%s', group='%s', mode=%#o",
-                         dev->sysfs_file, class_dev->name, udev->name,
-                         dev->owner, dev->group, dev->mode);
+               dbg("found matching attribute '%s', '%s' becomes '%s' ",
+                   dev->sysfs_file, class_dev->name, udev->name);
 
                return 0;
        }
@@ -434,15 +458,8 @@ static int do_number(struct sysfs_class_device *class_dev, struct udevice *udev,
                if (!found)
                        continue;
                strfieldcpy(udev->name, dev->name);
-               if (dev->mode != 0) {
-                       udev->mode = dev->mode;
-                       strfieldcpy(udev->owner, dev->owner);
-                       strfieldcpy(udev->group, dev->group);
-               }
-               dbg("found matching id '%s', '%s' becomes '%s'"
-                   " - owner='%s', group ='%s', mode=%#o",
-                   dev->id, class_dev->name, udev->name,
-                   dev->owner, dev->group, dev->mode);
+               dbg("found matching id '%s', '%s' becomes '%s'",
+                   dev->id, class_dev->name, udev->name);
                return 0;
        }
        return -ENODEV;
@@ -486,15 +503,8 @@ static int do_topology(struct sysfs_class_device *class_dev, struct udevice *ude
                        continue;
 
                strfieldcpy(udev->name, dev->name);
-               if (dev->mode != 0) {
-                       udev->mode = dev->mode;
-                       strfieldcpy(udev->owner, dev->owner);
-                       strfieldcpy(udev->group, dev->group);
-               }
-               dbg("found matching place '%s', '%s' becomes '%s'"
-                   " - owner='%s', group ='%s', mode=%#o",
-                   dev->place, class_dev->name, udev->name,
-                   dev->owner, dev->group, dev->mode);
+               dbg("found matching place '%s', '%s' becomes '%s'",
+                   dev->place, class_dev->name, udev->name);
                return 0;
        }
        return -ENODEV;
@@ -515,15 +525,7 @@ static int do_replace(struct sysfs_class_device *class_dev, struct udevice *udev
                        continue;
 
                strfieldcpy(udev->name, dev->name);
-               if (dev->mode != 0) {
-                       udev->mode = dev->mode;
-                       strfieldcpy(udev->owner, dev->owner);
-                       strfieldcpy(udev->group, dev->group);
-               }
-               dbg("found name, '%s' becomes '%s'"
-                   " - owner='%s', group='%s', mode = %#o",
-                   dev->kernel_name, udev->name,
-                   dev->owner, dev->group, dev->mode);
+               dbg("found name, '%s' becomes '%s'", dev->kernel_name, udev->name);
                
                return 0;
        }
@@ -532,24 +534,8 @@ static int do_replace(struct sysfs_class_device *class_dev, struct udevice *udev
 
 static void do_kernelname(struct sysfs_class_device *class_dev, struct udevice *udev)
 {
-       struct config_device *dev;
-       struct list_head *tmp;
-       int len;
-
+       /* heh, this is pretty simple... */
        strfieldcpy(udev->name, class_dev->name);
-       /* look for permissions */
-       list_for_each(tmp, &config_device_list) {
-               dev = list_entry(tmp, struct config_device, node);
-               len = strlen(dev->name);
-               if (strcmp_pattern(dev->name, class_dev->name))
-                       continue;
-               if (dev->mode != 0) {
-                       dbg("found permissions for '%s'", class_dev->name);
-                       udev->mode = dev->mode;
-                       strfieldcpy(udev->owner, dev->owner);
-                       strfieldcpy(udev->group, dev->group);
-               }
-       }
 }
 
 int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *udev)
@@ -558,6 +544,7 @@ int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *ud
        struct sysfs_class_device *class_dev_parent = NULL;
        int retval = 0;
        char *temp = NULL;
+       struct perm_device *perm;
 
        udev->mode = 0;
 
@@ -632,12 +619,19 @@ found:
        apply_format(udev, udev->name);
 
 done:
-       /* mode was never set above */
-       if (!udev->mode) {
+       perm = find_perm(udev->name);
+       if (perm) {
+               udev->mode = perm->mode;
+               strfieldcpy(udev->owner, perm->owner);
+               strfieldcpy(udev->group, perm->group);
+       } else {
+               /* no matching perms found :( */
                udev->mode = get_default_mode(class_dev);
                udev->owner[0] = 0x00;
                udev->group[0] = 0x00;
        }
+       dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o",
+           udev->name, udev->owner, udev->group, udev->mode);
 
        if (class_dev_parent)
                sysfs_close_class_device(class_dev_parent);
@@ -658,5 +652,6 @@ int namedev_init(void)
                return retval;
 
        dump_config_dev_list();
+       dump_perm_dev_list();
        return retval;
 }
index 94fdf4a..f7b8a67 100644 (file)
--- a/namedev.h
+++ b/namedev.h
@@ -62,8 +62,12 @@ struct config_device {
        char place[PLACE_SIZE];
        char kernel_name[NAME_SIZE];
        char exec_program[FILE_SIZE];
+       char name[NAME_SIZE];
+};
+
+struct perm_device {
+       struct list_head node;
 
-       /* what to set the device to */
        char name[NAME_SIZE];
        char owner[OWNER_SIZE];
        char group[GROUP_SIZE];
@@ -71,6 +75,7 @@ struct config_device {
 };
 
 extern struct list_head config_device_list;
+extern struct list_head perm_device_list;
 
 extern int namedev_init(void);
 extern int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *dev);
@@ -78,8 +83,11 @@ extern int namedev_init_permissions(void);
 extern int namedev_init_rules(void);
 
 extern int add_config_dev(struct config_device *new_dev);
+extern int add_perm_dev(struct perm_device *new_dev);
 extern void dump_config_dev(struct config_device *dev);
 extern void dump_config_dev_list(void);
+extern void dump_perm_dev(struct perm_device *dev);
+extern void dump_perm_dev_list(void);
 
 extern int get_pair(char **orig_string, char **left, char **right);
 
index a6a7d4b..5cb3a3e 100644 (file)
@@ -88,39 +88,27 @@ void dump_config_dev(struct config_device *dev)
 {
        switch (dev->type) {
        case KERNEL_NAME:
-               dbg_parse("KERNEL name='%s' ,"
-                         "owner='%s', group='%s', mode=%#o",
-                         dev->name, dev->owner, dev->group, dev->mode);
+               dbg_parse("KERNEL name='%s'", dev->name);
                break;
        case LABEL:
-               dbg_parse("LABEL name='%s', bus='%s', sysfs_file='%s', sysfs_value='%s', "
-                         "owner='%s', group='%s', mode=%#o",
-                         dev->name, dev->bus, dev->sysfs_file, dev->sysfs_value,
-                         dev->owner, dev->group, dev->mode);
+               dbg_parse("LABEL name='%s', bus='%s', sysfs_file='%s', sysfs_value='%s'",
+                         dev->name, dev->bus, dev->sysfs_file, dev->sysfs_value);
                break;
        case NUMBER:
-               dbg_parse("NUMBER name='%s', bus='%s', id='%s', "
-                         "owner='%s', group='%s', mode=%#o",
-                         dev->name, dev->bus, dev->id,
-                         dev->owner, dev->group, dev->mode);
+               dbg_parse("NUMBER name='%s', bus='%s', id='%s'",
+                         dev->name, dev->bus, dev->id);
                break;
        case TOPOLOGY:
-               dbg_parse("TOPOLOGY name='%s', bus='%s', place='%s', "
-                         "owner='%s', group='%s', mode=%#o",
-                         dev->name, dev->bus, dev->place,
-                         dev->owner, dev->group, dev->mode);
+               dbg_parse("TOPOLOGY name='%s', bus='%s', place='%s'",
+                         dev->name, dev->bus, dev->place);
                break;
        case REPLACE:
-               dbg_parse("REPLACE name=%s, kernel_name=%s, "
-                         "owner='%s', group='%s', mode=%#o",
-                         dev->name, dev->kernel_name,
-                         dev->owner, dev->group, dev->mode);
+               dbg_parse("REPLACE name=%s, kernel_name=%s",
+                         dev->name, dev->kernel_name);
                break;
        case CALLOUT:
-               dbg_parse("CALLOUT name='%s', bus='%s', program='%s', id='%s', "
-                         "owner='%s', group='%s', mode=%#o",
-                         dev->name, dev->bus, dev->exec_program, dev->id,
-                         dev->owner, dev->group, dev->mode);
+               dbg_parse("CALLOUT name='%s', bus='%s', program='%s', id='%s'",
+                         dev->name, dev->bus, dev->exec_program, dev->id);
                break;
        default:
                dbg_parse("unknown type of method");
@@ -136,7 +124,24 @@ void dump_config_dev_list(void)
                dump_config_dev(dev);
        }
 }
-       
+
+void dump_perm_dev(struct perm_device *dev)
+{
+       dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
+                 dev->name, dev->owner, dev->group, dev->mode);
+}
+
+void dump_perm_dev_list(void)
+{
+       struct list_head *tmp;
+
+       list_for_each(tmp, &perm_device_list) {
+               struct perm_device *dev = list_entry(tmp, struct perm_device, node);
+               dump_perm_dev(dev);
+       }
+}
+
+
 int namedev_init_rules(void)
 {
        char line[255];
@@ -344,7 +349,7 @@ int namedev_init_permissions(void)
        char *temp2;
        FILE *fd;
        int retval = 0;
-       struct config_device dev;
+       struct perm_device dev;
 
        fd = fopen(udev_permissions_filename, "r");
        if (fd != NULL) {
@@ -407,7 +412,7 @@ int namedev_init_permissions(void)
                dbg_parse("name='%s', owner='%s', group='%s', mode=%#o",
                          dev.name, dev.owner, dev.group,
                          dev.mode);
-               retval = add_config_dev(&dev);
+               retval = add_perm_dev(&dev);
                if (retval) {
                        dbg("add_config_dev returned with error %d", retval);
                        goto exit;
index d14082a..44ef3d4 100644 (file)
@@ -3,4 +3,4 @@ ttyUSB0:root:uucp:0660
 ttyUSB1:root:uucp:0666
 visor:500:500:0666
 dsp1:::0666
-
+boot_disk?:::0666