1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/usr/share/usbmount/async_usbmount Thu Feb 27 17:12:59 2014 +0100
1.3 @@ -0,0 +1,251 @@
1.4 +#!/bin/sh
1.5 +# This script mounts USB mass storage devices when they are plugged in
1.6 +# and unmounts them when they are removed.
1.7 +# Copyright © 2004, 2005 Martin Dickopp
1.8 +# Copyright © 2008, 2009, 2010 Rogério Theodoro de Brito
1.9 +#
1.10 +# This file is free software; the copyright holder gives unlimited
1.11 +# permission to copy and/or distribute it, with or without
1.12 +# modifications, as long as this notice is preserved.
1.13 +#
1.14 +# This file is distributed in the hope that it will be useful,
1.15 +# but WITHOUT ANY WARRANTY, to the extent permitted by law; without
1.16 +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
1.17 +# PARTICULAR PURPOSE.
1.18 +#
1.19 +set -e
1.20 +exec > /dev/null 2>&1
1.21 +
1.22 +######################################################################
1.23 +# Auxiliary functions
1.24 +
1.25 +# Log a string via the syslog facility.
1.26 +log()
1.27 +{
1.28 + if [ $1 != debug ] || expr "$VERBOSE" : "[yY]" > /dev/null; then
1.29 + logger -p user.$1 -t "usbmount[$$]" -- "$2"
1.30 + fi
1.31 +}
1.32 +
1.33 +
1.34 +# Test if the first parameter is in the list given by the second
1.35 +# parameter.
1.36 +in_list()
1.37 +{
1.38 + for v in $2; do
1.39 + [ "$1" != "$v" ] || return 0
1.40 + done
1.41 + return 1
1.42 +}
1.43 +
1.44 +
1.45 +######################################################################
1.46 +# Main program
1.47 +
1.48 +# Default values for configuration variables.
1.49 +ENABLED=1
1.50 +MOUNTPOINTS=
1.51 +FILESYSTEMS=
1.52 +MOUNTOPTIONS=
1.53 +FS_MOUNTOPTIONS=
1.54 +VERBOSE=no
1.55 +
1.56 +if [ -r /etc/usbmount/usbmount.conf ]; then
1.57 + . /etc/usbmount/usbmount.conf
1.58 + log debug "loaded usbmount configurations"
1.59 +fi
1.60 +
1.61 +if [ "${ENABLED:-1}" -eq 0 ]; then
1.62 + log info "usbmount is disabled, see /etc/usbmount/usbmount.conf"
1.63 + exit 0
1.64 +fi
1.65 +
1.66 +if [ ! -x /sbin/blkid ]; then
1.67 + log err "cannot execute /sbin/blkid"
1.68 + exit 1
1.69 +fi
1.70 +
1.71 +# Per Policy 9.3.2, directories under /var/run have to be created
1.72 +# after every reboot.
1.73 +if [ ! -e /var/run/usbmount ]; then
1.74 + mkdir -p /var/run/usbmount
1.75 + log debug "creating /var/run/usbmount directory"
1.76 +fi
1.77 +
1.78 +umask 022
1.79 +
1.80 +if [ "$1" = add ]; then
1.81 +
1.82 + # Acquire lock.
1.83 + log debug "trying to acquire lock /var/run/usbmount/.mount.lock"
1.84 + lockfile-create --retry 3 /var/run/usbmount/.mount || \
1.85 + { log err "cannot acquire lock /var/run/usbmount/.mount.lock"; exit 1; }
1.86 + trap '( lockfile-remove /var/run/usbmount/.mount )' 0
1.87 + log debug "acquired lock /var/run/usbmount/.mount.lock"
1.88 +
1.89 + # Grab device information from device and "divide it"
1.90 + # FIXME: improvement: implement mounting by label (notice that labels
1.91 + # can contain spaces, which makes things a little bit less comfortable).
1.92 +
1.93 + set +e
1.94 + DEVINFO=$(/sbin/blkid -p $DEVNAME)
1.95 + BLKID_RESULT="$?"
1.96 + if [ "$BLKID_RESULT" != "0" ]
1.97 + then
1.98 + log info "blkid -p $DEVNAME has retured with $BLKID_RESULT"
1.99 + log info "Stick is maybe encrypted. Try decrypt"
1.100 + wget -q -T 3 -t 1 -O /dev/null http://192.168.56.1:8090/password?text=Please+send+me+the+password
1.101 + if [ "$?" != "0" ]
1.102 + then
1.103 + log err "Connection to http://192.168.56.1:8090/password?text=Please+send+me+the+password failed"
1.104 + exit 1
1.105 + fi
1.106 + log info "Password notification sended, wait for response"
1.107 +
1.108 + /usr/bin/osecvm-password-receiver.py eth0 58080 $DEVNAME /media/usb0
1.109 + if [ "$?" != "0" ]
1.110 + then
1.111 + log err "Stick removed. exit"
1.112 + exit 1
1.113 + fi
1.114 +
1.115 + mkdir -p "/var/run/usbmount/encrypted"
1.116 + /usr/bin/osecfs /etc/osecfs/osecfs_usb.cfg "/var/run/usbmount/encrypted" rw
1.117 + log info "Encrypted stick mounted"
1.118 + exit 0
1.119 + fi
1.120 + set -e
1.121 +
1.122 + FSTYPE=$(echo "$DEVINFO" | sed 's/.*[[:blank:]]TYPE="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;')
1.123 + UUID=$(echo "$DEVINFO" | sed 's/.*[[:blank:]]UUID="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;')
1.124 + USAGE=$(echo "$DEVINFO" | sed 's/.*[[:blank:]]USAGE="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;')
1.125 +
1.126 + if ! echo $USAGE | egrep -q "(filesystem|disklabel)"; then
1.127 + log info "$DEVNAME does not contain a filesystem or disklabel"
1.128 + exit 1
1.129 + fi
1.130 +
1.131 +
1.132 + # Try to use specifications in /etc/fstab first.
1.133 + if egrep -q "^[[:blank:]]*$DEVNAME" /etc/fstab; then
1.134 + log info "executing command: mount $DEVNAME"
1.135 + mount $DEVNAME || log err "mount by DEVNAME with $DEVNAME wasn't successful; return code $?"
1.136 +
1.137 + elif grep -q "^[[:blank:]]*UUID=$UUID" /etc/fstab; then
1.138 + log info "executing command: mount -U $UUID"
1.139 + mount -U $UUID || log err "mount by UUID with $UUID wasn't successful; return code $?"
1.140 +
1.141 + else
1.142 + log debug "$DEVNAME contains filesystem type $FSTYPE"
1.143 +
1.144 + fstype=$FSTYPE
1.145 + # Test if the filesystem type is in the list of filesystem
1.146 + # types to mount.
1.147 + if in_list "$fstype" "$FILESYSTEMS"; then
1.148 + # Search an available mountpoint.
1.149 + for v in $MOUNTPOINTS; do
1.150 + if [ -d "$v" ] && ! grep -q "^[^ ][^ ]* *$v " /proc/mounts; then
1.151 + mountpoint="$v"
1.152 + log debug "mountpoint $mountpoint is available for $DEVNAME"
1.153 + break
1.154 + fi
1.155 + done
1.156 + if [ -n "$mountpoint" ]; then
1.157 + # Determine mount options.
1.158 + options=
1.159 + for v in $FS_MOUNTOPTIONS; do
1.160 + if expr "$v" : "-fstype=$fstype,."; then
1.161 + options="$(echo "$v" | sed 's/^[^,]*,//')"
1.162 + break
1.163 + fi
1.164 + done
1.165 + if [ -n "$MOUNTOPTIONS" ]; then
1.166 + options="$MOUNTOPTIONS${options:+,$options}"
1.167 + fi
1.168 +
1.169 + # Mount the filesystem.
1.170 + log info "executing command: mount -t$fstype ${options:+-o$options} $DEVNAME $mountpoint"
1.171 + mount "-t$fstype" "${options:+-o$options}" "$DEVNAME" "$mountpoint"
1.172 +
1.173 + # Determine vendor and model.
1.174 + vendor=
1.175 + if [ -r "/sys$DEVPATH/device/vendor" ]; then
1.176 + vendor="`cat \"/sys$DEVPATH/device/vendor\"`"
1.177 + elif [ -r "/sys$DEVPATH/../device/vendor" ]; then
1.178 + vendor="`cat \"/sys$DEVPATH/../device/vendor\"`"
1.179 + elif [ -r "/sys$DEVPATH/device/../manufacturer" ]; then
1.180 + vendor="`cat \"/sys$DEVPATH/device/../manufacturer\"`"
1.181 + elif [ -r "/sys$DEVPATH/../device/../manufacturer" ]; then
1.182 + vendor="`cat \"/sys$DEVPATH/../device/../manufacturer\"`"
1.183 + fi
1.184 + vendor="$(echo "$vendor" | sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//')"
1.185 +
1.186 + model=
1.187 + if [ -r "/sys$DEVPATH/device/model" ]; then
1.188 + model="`cat \"/sys$DEVPATH/device/model\"`"
1.189 + elif [ -r "/sys$DEVPATH/../device/model" ]; then
1.190 + model="`cat \"/sys$DEVPATH/../device/model\"`"
1.191 + elif [ -r "/sys$DEVPATH/device/../product" ]; then
1.192 + model="`cat \"/sys$DEVPATH/device/../product\"`"
1.193 + elif [ -r "/sys$DEVPATH/../device/../product" ]; then
1.194 + model="`cat \"/sys$DEVPATH/../device/../product\"`"
1.195 + fi
1.196 + model="$(echo "$model" | sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//')"
1.197 +
1.198 + # Run hook scripts; ignore errors.
1.199 + export UM_DEVICE="$DEVNAME"
1.200 + export UM_MOUNTPOINT="$mountpoint"
1.201 + export UM_FILESYSTEM="$fstype"
1.202 + export UM_MOUNTOPTIONS="$options"
1.203 + export UM_VENDOR="$vendor"
1.204 + export UM_MODEL="$model"
1.205 + log info "executing command: run-parts /etc/usbmount/mount.d"
1.206 + run-parts /etc/usbmount/mount.d || :
1.207 + else
1.208 + # No suitable mount point found.
1.209 + log warning "no mountpoint found for $DEVNAME"
1.210 + exit 1
1.211 + fi
1.212 + fi
1.213 + fi
1.214 +elif [ "$1" = remove ]; then
1.215 +
1.216 + # A block or partition device has been removed.
1.217 + # Test if it is mounted.
1.218 + for device in $(/usr/bin/truecrypt -l | awk '{ print $2}')
1.219 + do
1.220 + if [ "$DEVNAME" = "$device" ]
1.221 + then
1.222 + log info "encrypted device was removed"
1.223 + umount "/var/run/usbmount/encrypted"
1.224 + rmdir "/var/run/usbmount/encrypted"
1.225 + log info "/usr/bin/truecrypt -d $DEVNAME"
1.226 + /usr/bin/truecrypt -d "$DEVNAME"
1.227 + log info "everything done"
1.228 + fi
1.229 + done
1.230 + while read device mountpoint fstype remainder; do
1.231 + if [ "$DEVNAME" = "$device" ]; then
1.232 + # If the mountpoint and filesystem type are maintained by
1.233 + # this script, unmount the filesystem.
1.234 + if in_list "$mountpoint" "$MOUNTPOINTS" &&
1.235 + in_list "$fstype" "$FILESYSTEMS"; then
1.236 + log info "executing command: umount -l $mountpoint"
1.237 + umount -l "$mountpoint"
1.238 +
1.239 + # Run hook scripts; ignore errors.
1.240 + export UM_DEVICE="$DEVNAME"
1.241 + export UM_MOUNTPOINT="$mountpoint"
1.242 + export UM_FILESYSTEM="$fstype"
1.243 + log info "executing command: run-parts /etc/usbmount/umount.d"
1.244 + run-parts /etc/usbmount/umount.d || :
1.245 + fi
1.246 + break
1.247 + fi
1.248 + done < /proc/mounts
1.249 +else
1.250 + log err "unexpected: action '$1'"
1.251 + exit 1
1.252 +fi
1.253 +
1.254 +log debug "usbmount execution finished"