| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# |
| 4 |
# vim:shiftwidth=2:tabstop=2:expandtab:textwidth=80:softtabstop=4:ai |
| 5 |
# |
| 6 |
# This script generates udev and/or policykit rules for Logitech Harmony |
| 7 |
# devices. |
| 8 |
# |
| 9 |
# This script is loosely-based on scripts by Douglas E. Warner |
| 10 |
# <[email protected]> |
| 11 |
# |
| 12 |
# GPLv2 |
| 13 |
# |
| 14 |
# Copyright 2009 Phil Dibowitz |
| 15 |
# |
| 16 |
|
| 17 |
# CONDITIONS |
| 18 |
# varios "conditions" to make udev rules |
| 19 |
|
| 20 |
# A partial template, used largely when generating one-line per device |
| 21 |
UDEV_CONDITION_PARTIAL_TEMPLATE='ATTR{idVendor}=="%s", ATTR{idProduct}=="%s"' |
| 22 |
# The above, but also with a USB specifier, for the one-line-for-all version |
| 23 |
UDEV_CONDITION_TEMPLATE="SUBSYSTEM==\"usb\", $UDEV_CONDITION_PARTIAL_TEMPLATE" |
| 24 |
# The older syntax for ancient udev. This one is also "partial" since you |
| 25 |
# have to specify each device ID separately on this version of udev. |
| 26 |
UDEV_CONDITION_OLD_TEMPLATE='SYSFS{idVendor}=="%s", SYSFS{idProduct}=="%s"' |
| 27 |
|
| 28 |
# ACTIONS |
| 29 |
# The "action" part of udev rules. |
| 30 |
|
| 31 |
# When using udev-acl, this instructs it to give access to the current |
| 32 |
# console user. |
| 33 |
UDEV_ACTION='ENV{ID_REMOTE_CONTROL}="1"' |
| 34 |
# This says give r/w to the dialout group |
| 35 |
UDEV_ACTION_GENERIC='MODE="0660", GROUP="dialout"' |
| 36 |
# This says make a symlink in /dev called "harmony-<id>" |
| 37 |
UDEV_ACTION_POLICYKIT='SYMLINK+="harmony-%%k"' |
| 38 |
|
| 39 |
# Stuff for HAL. Deprecated. |
| 40 |
HAL_PRE_TEMPLATE=' <match key="usb_device.vendor_id" int="0x%s">' |
| 41 |
HAL_RULE_TEMPLATE=' <match key="usb_device.product_id" int="0x%s"> |
| 42 |
<append key="info.capabilities" type="strlist">access_control</append> |
| 43 |
<merge key="access_control.file" |
| 44 |
type="copy_property">linux.device_file</merge> |
| 45 |
<merge key="access_control.type" type="string">libconcord</merge> |
| 46 |
</match>' |
| 47 |
HAL_POST=' </match>' |
| 48 |
|
| 49 |
NATIONAL_VID='0400' |
| 50 |
NATIONAL_PID='c359' |
| 51 |
LOGITECH_VID='046d' |
| 52 |
LOGITECH_MIN_PID='c110' |
| 53 |
LOGITECH_MAX_PID='c14f' |
| 54 |
LOGITECH_PID_GLOB='c1[1-4][0-9a-f]' |
| 55 |
|
| 56 |
UDEV_FILE='libconcord.rules' |
| 57 |
UDEV_USBNET_FILE='libconcord-usbnet.rules' |
| 58 |
HAL_POLICY_FILE='libconcord.fdi' |
| 59 |
POLICYKIT_FILE='org.freedesktop.hal.device-access.libconcord.policy' |
| 60 |
CONSOLEKIT_FILE='libconcord.perms' |
| 61 |
|
| 62 |
# |
| 63 |
# GENERAL FUNCTIONS |
| 64 |
# |
| 65 |
emit_for_all() { |
| 66 |
file="$1" |
| 67 |
template="$2" |
| 68 |
include_vid="$3" |
| 69 |
|
| 70 |
printf "$template\n" $NATIONAL_VID $NATIONAL_PID >>$file |
| 71 |
|
| 72 |
for pid in `seq 0x$LOGITECH_MIN_PID 0x$LOGITECH_MAX_PID`; do |
| 73 |
pid=`printf "%x" $pid` |
| 74 |
if [ "$include_vid" == 'yes' ] ; then |
| 75 |
printf "$template\n" $LOGITECH_VID $pid >>$file |
| 76 |
else |
| 77 |
printf "$template\n" $pid >>$file |
| 78 |
fi |
| 79 |
done |
| 80 |
} |
| 81 |
|
| 82 |
# |
| 83 |
# UDEV FUNCTIONS |
| 84 |
# |
| 85 |
emit_old_udev_header() { |
| 86 |
file="$1" |
| 87 |
cat >$file <<END |
| 88 |
# Neat trick so that non-harmony devices don't read through a million rules |
| 89 |
SUBSYSTEM=="usb_device", GOTO="harmony_usb_rules" |
| 90 |
SUBSYSTEM=="usb", GOTO="harmony_usb_rules" |
| 91 |
SUBSYSTEM!="usb", GOTO="harmony_rules_end" |
| 92 |
GOTO="harmony_rules_end" |
| 93 |
LABEL="harmony_usb_rules" |
| 94 |
END |
| 95 |
} |
| 96 |
|
| 97 |
emit_old_udev_footer() { |
| 98 |
file="$1" |
| 99 |
cat >>$file <<END |
| 100 |
GOTO="harmony_rules_end" |
| 101 |
LABEL="harmony_rules_end" |
| 102 |
END |
| 103 |
} |
| 104 |
|
| 105 |
emit_udev_rules() { |
| 106 |
file="$1" |
| 107 |
mode="$2" |
| 108 |
|
| 109 |
template="$UDEV_CONDITION_TEMPLATE" |
| 110 |
if [ "$mode" == 'generic_udev' ] ; then |
| 111 |
template="$template, $UDEV_ACTION_GENERIC" |
| 112 |
else |
| 113 |
template="$template, $UDEV_ACTION" |
| 114 |
fi |
| 115 |
|
| 116 |
printf "$template\n" $NATIONAL_VID $NATIONAL_PID >>$file |
| 117 |
printf "$template\n" $LOGITECH_VID $LOGITECH_PID_GLOB >>$file |
| 118 |
} |
| 119 |
|
| 120 |
# Rule for the usbnet family of remotes to start a DHCP daemon upon detection. |
| 121 |
emit_udev_usbnet_rules() { |
| 122 |
file="$1" |
| 123 |
cat >$file <<END |
| 124 |
SUBSYSTEM=="net", ENV{ID_VENDOR_ID}=="046d", ENV{ID_MODEL_ID}=="c11f", \ |
| 125 |
RUN+="start_concordance_dhcpd_wrapper.sh" |
| 126 |
END |
| 127 |
} |
| 128 |
|
| 129 |
emit_old_udev_rules() { |
| 130 |
file="$1" |
| 131 |
mode="$2" |
| 132 |
|
| 133 |
case "$mode" in |
| 134 |
policykit|consolekit) |
| 135 |
template="$UDEV_CONDITION_PARTIAL_TEMPLATE, $UDEV_ACTION_POLICYKIT" |
| 136 |
;; |
| 137 |
old_udev) |
| 138 |
template="$UDEV_CONDITION_OLD_TEMPLATE, $UDEV_ACTION_GENERIC" |
| 139 |
;; |
| 140 |
esac |
| 141 |
|
| 142 |
emit_for_all $file "$template" 'yes' |
| 143 |
} |
| 144 |
|
| 145 |
create_udev_files() { |
| 146 |
mode=$1 |
| 147 |
|
| 148 |
echo -n "Creating udev file: $UDEV_FILE ... " |
| 149 |
# delete wahtever used to be there |
| 150 |
rm -f $UDEV_FILE |
| 151 |
|
| 152 |
case "$mode" in |
| 153 |
policykit|consolekit|old_udev) |
| 154 |
emit_old_udev_header $UDEV_FILE |
| 155 |
emit_old_udev_rules $UDEV_FILE $mode |
| 156 |
emit_old_udev_footer $UDEV_FILE |
| 157 |
;; |
| 158 |
udev|generic_udev) |
| 159 |
emit_udev_rules $UDEV_FILE $mode |
| 160 |
;; |
| 161 |
*) |
| 162 |
echo "BAD MODE: $mode" |
| 163 |
exit 1 |
| 164 |
;; |
| 165 |
esac |
| 166 |
echo 'done' |
| 167 |
# USBNET rule needs to be in a separate file because it depends on |
| 168 |
# 75-net-description.rules. |
| 169 |
echo -n "Creating udev file: $UDEV_USBNET_FILE ... " |
| 170 |
emit_udev_usbnet_rules $UDEV_USBNET_FILE |
| 171 |
echo 'done' |
| 172 |
} |
| 173 |
|
| 174 |
# |
| 175 |
# HAL POLICY FUNCITONS |
| 176 |
# |
| 177 |
|
| 178 |
emit_hal_policy_header() { |
| 179 |
file="$1" |
| 180 |
cat >$file <<END |
| 181 |
<?xml version="1.0" encoding="UTF-8"?> |
| 182 |
<deviceinfo version="0.2"> |
| 183 |
<device> |
| 184 |
END |
| 185 |
printf "$HAL_PRE_TEMPLATE\n" $LOGITECH_VID >>$file |
| 186 |
} |
| 187 |
|
| 188 |
emit_hal_policy_footer() { |
| 189 |
printf "$HAL_POST\n" >>$file |
| 190 |
file="$1" |
| 191 |
cat >>$file <<END |
| 192 |
</device> |
| 193 |
</deviceinfo> |
| 194 |
END |
| 195 |
} |
| 196 |
|
| 197 |
emit_hal_policy_rules() { |
| 198 |
file="$1" |
| 199 |
|
| 200 |
emit_for_all $file "$HAL_RULE_TEMPLATE" |
| 201 |
} |
| 202 |
|
| 203 |
create_hal_policy_file() { |
| 204 |
echo -n "Creating hal policy file: $HAL_POLICY_FILE ... " |
| 205 |
emit_hal_policy_header $HAL_POLICY_FILE |
| 206 |
emit_hal_policy_rules $HAL_POLICY_FILE |
| 207 |
emit_hal_policy_footer $HAL_POLICY_FILE |
| 208 |
echo 'done' |
| 209 |
} |
| 210 |
|
| 211 |
# |
| 212 |
# POLICYKIT FUNCITONS |
| 213 |
# |
| 214 |
|
| 215 |
emit_policykit() { |
| 216 |
file="$1" |
| 217 |
cat >$file <<END |
| 218 |
<?xml version="1.0" encoding="UTF-8"?> |
| 219 |
<!DOCTYPE policyconfig PUBLIC |
| 220 |
"-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN" |
| 221 |
"http://www.freedesktop.org/standards/PolicyKit/1.0/policyconfig.dtd"> |
| 222 |
|
| 223 |
<policyconfig> |
| 224 |
<action id="org.freedesktop.hal.device-access.libconcord"> |
| 225 |
<description>Directly access the Logitech Harmony remote</description> |
| 226 |
<message>System policy prevents access to the Logitech Harmony remote</message> |
| 227 |
<defaults> |
| 228 |
<allow_inactive>no</allow_inactive> |
| 229 |
<allow_active>yes</allow_active> |
| 230 |
</defaults> |
| 231 |
</action> |
| 232 |
</policyconfig> |
| 233 |
END |
| 234 |
} |
| 235 |
|
| 236 |
create_policykit_file() { |
| 237 |
echo -n "Creating PolicyKit file: $POLICYKIT_FILE ..." |
| 238 |
emit_policykit $POLICYKIT_FILE |
| 239 |
echo ' done' |
| 240 |
} |
| 241 |
|
| 242 |
# |
| 243 |
# CONSOLEKIT |
| 244 |
# |
| 245 |
|
| 246 |
emit_perms() { |
| 247 |
file="$1" |
| 248 |
cat >$file <<END |
| 249 |
<harmony>=/dev/harmony* |
| 250 |
<console> 0600 <harmony> 0600 root |
| 251 |
END |
| 252 |
} |
| 253 |
|
| 254 |
create_perms_file() { |
| 255 |
echo -n "Creating perms file: $CONSOLEKIT_FILE ..." |
| 256 |
emit_perms $CONSOLEKIT_FILE |
| 257 |
echo ' done' |
| 258 |
} |
| 259 |
|
| 260 |
# |
| 261 |
# OTHER FUNCTIONS |
| 262 |
# |
| 263 |
|
| 264 |
usage() { |
| 265 |
echo "Usage: $0 <-u|-p|-c|-o>" |
| 266 |
} |
| 267 |
|
| 268 |
# |
| 269 |
# MAIN |
| 270 |
# |
| 271 |
|
| 272 |
while getopts cgopu opt; do |
| 273 |
case $opt in |
| 274 |
c) |
| 275 |
MODE='consolekit' |
| 276 |
;; |
| 277 |
g) |
| 278 |
MODE='generic_udev' |
| 279 |
;; |
| 280 |
o) |
| 281 |
MODE='old_udev' |
| 282 |
;; |
| 283 |
p) |
| 284 |
MODE='policykit' |
| 285 |
;; |
| 286 |
u) |
| 287 |
MODE='udev' |
| 288 |
;; |
| 289 |
*) |
| 290 |
usage |
| 291 |
exit 1 |
| 292 |
;; |
| 293 |
esac |
| 294 |
done |
| 295 |
|
| 296 |
if [ "$MODE" == '' ]; then |
| 297 |
usage |
| 298 |
exit 1 |
| 299 |
fi |
| 300 |
|
| 301 |
create_udev_files $MODE |
| 302 |
if [ "$MODE" == 'policykit' ]; then |
| 303 |
create_hal_policy_file |
| 304 |
create_policykit_file |
| 305 |
elif [ "$MODE" == 'consolekit' ]; then |
| 306 |
create_perms_file |
| 307 |
fi |
| 308 |
|