From ec10c9c70c57bfe7d539b523426e9433a62e1bde Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Sun, 16 Jun 2013 11:12:23 +0200 Subject: [PATCH] udev: Use uaccess instead of plugdev (pwr/Solaar#66) Rules are taken from https://git.lekensteyn.nl/ltunify/. The "VX Nano" receivers (c51a and c526) have been removed as these are only used for Cordless mice and are not even enabled in Solaar. The installer is updated to check for the existence of a group and `mktemp` to create temporary files. (relying on `$TMPDIR` is a bad idea.) This installer is too fancy, most users will be fine with `install -m644 rules.d/42-logitech-unify-permissions.rules /etc/udev/rules.d/`. If you are lazy and want to make `solaar-cli` work over SSH, then this installer will help you. It won't add you to the `plugdev` group, instead it will edit the rule to use your user's primary group. Oh, and it does not change this group to "root" as that is pretty useless given that root is the default. --- rules.d/42-logitech-unify-permissions.rules | 27 ++++++++++++++++++++ rules.d/99-logitech-unifying-receiver.rules | 20 --------------- rules.d/install.sh | 28 ++++++++++++++------- 3 files changed, 46 insertions(+), 29 deletions(-) create mode 100644 rules.d/42-logitech-unify-permissions.rules delete mode 100644 rules.d/99-logitech-unifying-receiver.rules diff --git a/rules.d/42-logitech-unify-permissions.rules b/rules.d/42-logitech-unify-permissions.rules new file mode 100644 index 00000000..e315fb3d --- /dev/null +++ b/rules.d/42-logitech-unify-permissions.rules @@ -0,0 +1,27 @@ +# Allows non-root users to have raw access the Logitech Unifying USB Receiver +# device. For development purposes, allowing users to write to the receiver is +# potentially dangerous (e.g. perform firmware updates). + +# skip actual unified devices, only consider the receiver +DRIVERS=="logitech-djdevice", GOTO="not_unify_recv" + +# official Unifying receivers +SUBSYSTEM=="hidraw", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="c52b", GOTO="unify_dev" +SUBSYSTEM=="hidraw", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="c532", GOTO="unify_dev" + +# "Unifying Ready" Nano receiver +SUBSYSTEM=="hidraw", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="c52f", GOTO="unify_dev" + +GOTO="not_unify_recv" + +LABEL="unify_dev" +# Allow any seated user to access the receiver. +# uaccess: modern ACL-enabled udev +# udev-acl: for Ubuntu 12.10 and older +TAG+="uaccess", TAG+="udev-acl" + +# Grant members of the "plugdev" group access to receiver (useful for SSH users) +#MODE="0660", GROUP="plugdev" + +LABEL="not_unify_recv" +# vim: ft=udevrules diff --git a/rules.d/99-logitech-unifying-receiver.rules b/rules.d/99-logitech-unifying-receiver.rules deleted file mode 100644 index d0fdbb4c..00000000 --- a/rules.d/99-logitech-unifying-receiver.rules +++ /dev/null @@ -1,20 +0,0 @@ -# Initially copied from signal11/hidapi/udev, with modifications. - -# This is a sample udev file for HIDAPI devices which changes the permissions -# to 0660 and group to plugdev for a specified device on Linux systems. - -# Make sure the plugdev group exists on your system and your user is a member -# before applying these rules. - -# HIDAPI/hidraw for Logitech Unifying Receiver -ACTION=="add", KERNEL=="hidraw*", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="c52b", GROUP="plugdev", MODE="0660" -ACTION=="add", KERNEL=="hidraw*", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="c532", GROUP="plugdev", MODE="0660" - -# HIDAPI/hidraw for Logitech Nano Receiver, "Unifying ready" -ACTION=="add", KERNEL=="hidraw*", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="c52f", GROUP="plugdev", MODE="0660" - -# HIDAPI/hidraw for VX Nano receiver -#ACTION=="add", KERNEL=="hidraw*", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="c51a", GROUP="plugdev", MODE="0660" -#ACTION=="add", KERNEL=="hidraw*", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="c526", GROUP="plugdev", MODE="0660" - -# vim: ft=udevrules diff --git a/rules.d/install.sh b/rules.d/install.sh index 46e3692a..95e2cc9d 100755 --- a/rules.d/install.sh +++ b/rules.d/install.sh @@ -10,19 +10,30 @@ if ! test -d "$RULES_D"; then exit 1 fi -RULE=99-logitech-unifying-receiver.rules +RULE=42-logitech-unify-permissions.rules if test -n "$1"; then SOURCE="$1" else SOURCE="$(dirname "$Z")/$RULE" - if ! id -G -n | grep -q -F plugdev; then - GROUP="$(id -g -n)" - echo "User '$USER' does not belong to the 'plugdev' group, will use group '$GROUP' in the udev rule." - TEMP_RULE="${TMPDIR:-/tmp}/$$-$RULE" - cp -f "$SOURCE" "$TEMP_RULE" + REALUSER="${SUDO_USER-$USER}" + if [ -z "$REALUSER" -o "$REALUSER" = "root" ]; then + : # ignore unknown and root user + else + GROUP=plugdev + TEMP_RULE="$(mktemp --tmpdir "ltudev.XXXXXXXX")" + sed -e "/^#MODE.*\"plugdev\"/s/^#//" "$SOURCE" > "$TEMP_RULE" + if ! id -G -n "$REALUSER" | grep -q -F plugdev; then + GROUP="$(id -g -n "$REALUSER")" + if getent group plugdev >/dev/null; then + printf "User '%s' does not belong to the 'plugdev' group, " "$REALUSER" + else + printf "Group 'plugdev' does not exist, " + fi + echo "will use group '$GROUP' in the udev rule." + sed -i -e "s/\"plugdev\"/\"$GROUP\"/" "$TEMP_RULE" + fi SOURCE="$TEMP_RULE" - sed -i -e "s/GROUP=\"plugdev\"/GROUP=\"$GROUP\"/" "$SOURCE" fi fi @@ -36,7 +47,6 @@ if test "$(id -u)" != "0"; then fi echo "Installing $RULE." -cp "$SOURCE" "$RULES_D/$RULE" -chmod a+r "$RULES_D/$RULE" +install -m 644 "$SOURCE" "$RULES_D/$RULE" echo "Done. Now remove the Unfiying Receiver and plug it in again."