usr/share/usbmount/async_usbmount
author ft
Tue, 04 Nov 2014 14:58:00 +0100
changeset 2 ee0797f46473
parent 0 60bc07f3f415
permissions -rwxr-xr-x
changed some things
     1 #!/bin/sh
     2 # This script mounts USB mass storage devices when they are plugged in
     3 # and unmounts them when they are removed.
     4 # Copyright © 2004, 2005 Martin Dickopp
     5 # Copyright © 2008, 2009, 2010 Rogério Theodoro de Brito
     6 #
     7 # This file is free software; the copyright holder gives unlimited
     8 # permission to copy and/or distribute it, with or without
     9 # modifications, as long as this notice is preserved.
    10 #
    11 # This file is distributed in the hope that it will be useful,
    12 # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
    13 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
    14 # PARTICULAR PURPOSE.
    15 #
    16 set -e
    17 exec > /dev/null 2>&1
    18 
    19 ######################################################################
    20 # Auxiliary functions
    21 
    22 # Log a string via the syslog facility.
    23 log()
    24 {
    25     if [ $1 != debug ] || expr "$VERBOSE" : "[yY]" > /dev/null; then
    26 	logger -p user.$1 -t "usbmount[$$]" -- "$2"
    27     fi
    28 }
    29 
    30 
    31 # Test if the first parameter is in the list given by the second
    32 # parameter.
    33 in_list()
    34 {
    35     for v in $2; do
    36 	[ "$1" != "$v" ] || return 0
    37     done
    38     return 1
    39 }
    40 
    41 getRemoteIp ()
    42 {
    43         ip_address=$(ifconfig eth0 | grep "inet " | awk '{ print $2 }' | cut -d ":" -f 2)
    44         ip_netmask=$(ifconfig eth0 | grep "inet " | awk '{ print $4 }' | cut -d ":" -f 2)
    45         remote_ip=$(ipcalc $ip_address/$ip_netmask | grep HostMin | awk '{ print $2}')
    46 
    47         echo $remote_ip
    48 }
    49 
    50 ######################################################################
    51 # Main program
    52 
    53 # Default values for configuration variables.
    54 ENABLED=1
    55 MOUNTPOINTS=
    56 FILESYSTEMS=
    57 MOUNTOPTIONS=
    58 FS_MOUNTOPTIONS=
    59 VERBOSE=no
    60 
    61 if [ -r /etc/usbmount/usbmount.conf ]; then
    62     . /etc/usbmount/usbmount.conf
    63     log debug "loaded usbmount configurations"
    64 fi
    65 
    66 if [ "${ENABLED:-1}" -eq 0 ]; then
    67     log info "usbmount is disabled, see /etc/usbmount/usbmount.conf"
    68     exit 0
    69 fi
    70 
    71 if [ ! -x /sbin/blkid ]; then
    72     log err "cannot execute /sbin/blkid"
    73     exit 1
    74 fi
    75 
    76 # Per Policy 9.3.2, directories under /var/run have to be created
    77 # after every reboot.
    78 if [ ! -e /var/run/usbmount ]; then
    79     mkdir -p /var/run/usbmount
    80     log debug "creating /var/run/usbmount directory"
    81 fi
    82 
    83 if [ ! -e /tmp/usbmount ]; then
    84     mkdir -p /tmp/usbmount
    85     log debug "creating /tmp/usbmount directory"
    86 fi
    87 
    88 umask 022
    89 
    90 if [ "$1" = add ]; then
    91 
    92     # Acquire lock.
    93     log debug "trying to acquire lock /var/run/usbmount/.mount.lock"
    94     lockfile-create --retry 3 /var/run/usbmount/.mount || \
    95 	{ log err "cannot acquire lock /var/run/usbmount/.mount.lock"; exit 1; }
    96     trap '( lockfile-remove /var/run/usbmount/.mount )' 0
    97     log debug "acquired lock /var/run/usbmount/.mount.lock"
    98 
    99     # Grab device information from device and "divide it"
   100     #   FIXME: improvement: implement mounting by label (notice that labels
   101     #   can contain spaces, which makes things a little bit less comfortable).
   102 
   103     set +e
   104     DEVINFO=$(/sbin/blkid -p $DEVNAME)
   105     BLKID_RESULT="$?"
   106     if [ "$BLKID_RESULT" != "0" ]
   107     then
   108 	log info "blkid -p $DEVNAME has retured with $BLKID_RESULT"
   109 	log info "Stick is maybe encrypted. Try decrypt"
   110 	wget -q -T 3 -t 1 -O /dev/null http://$(getRemoteIp):8090/password?text=Please+enter+the+password
   111 	if [ "$?" != "0" ]
   112 	then
   113 		log err "Connection to \"http://$(getRemoteIp):8090/password?text=Please+enter+the+password\" failed"
   114 		exit 1
   115 	fi
   116 	log info "Password notification sended, wait for response"
   117 
   118 	/usr/bin/encryptionprovider.py -m eth0 58080 $DEVNAME /media/usb0
   119 	if [ "$?" != "0" ]
   120 	then
   121 		log err "Stick removed. exit"
   122 		exit 1
   123 	fi
   124 
   125 	chattr -i "/tmp/usbmount"
   126 	mkdir -p "/tmp/usbmount/encrypted"
   127 	/usr/bin/osecfs /etc/osecfs/osecfs_usb.cfg "/tmp/usbmount/encrypted" rw
   128 	log info "Encrypted stick mounted"
   129 
   130 	#run_initlistener.sh $DEVNAME &
   131 	encryptionprovider.py -i eth0 58081 "$DEVNAME" /media/usb0 &
   132 	chattr +i "/tmp/usbmount"
   133         exit 0
   134     fi
   135     set -e
   136 
   137     FSTYPE=$(echo "$DEVINFO" | sed 's/.*[[:blank:]]TYPE="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;')
   138     UUID=$(echo "$DEVINFO"   | sed 's/.*[[:blank:]]UUID="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;')
   139     USAGE=$(echo "$DEVINFO"  | sed 's/.*[[:blank:]]USAGE="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;')
   140 
   141     if ! echo $USAGE | egrep -q "(filesystem|disklabel)"; then
   142 	log info "$DEVNAME does not contain a filesystem or disklabel"
   143 	exit 1
   144     fi
   145 
   146 
   147     # Try to use specifications in /etc/fstab first.
   148     if egrep -q "^[[:blank:]]*$DEVNAME" /etc/fstab; then
   149 	log info "executing command: mount $DEVNAME"
   150 	mount $DEVNAME || log err "mount by DEVNAME with $DEVNAME wasn't successful; return code $?"
   151 
   152     elif grep -q "^[[:blank:]]*UUID=$UUID" /etc/fstab; then
   153         log info "executing command: mount -U $UUID"
   154 	mount -U $UUID || log err "mount by UUID with $UUID wasn't successful; return code $?"
   155 
   156     else
   157 	log debug "$DEVNAME contains filesystem type $FSTYPE"
   158 
   159 	fstype=$FSTYPE
   160 	# Test if the filesystem type is in the list of filesystem
   161 	# types to mount.
   162 	if in_list "$fstype" "$FILESYSTEMS"; then
   163 	    # Search an available mountpoint.
   164 	    for v in $MOUNTPOINTS; do
   165 		if [ -d "$v" ] && ! grep -q "^[^ ][^ ]*  *$v " /proc/mounts; then
   166 		    mountpoint="$v"
   167 		    log debug "mountpoint $mountpoint is available for $DEVNAME"
   168 		    break
   169 		fi
   170 	    done
   171 	    if [ -n "$mountpoint" ]; then
   172 		# Determine mount options.
   173 		options=
   174 		for v in $FS_MOUNTOPTIONS; do
   175 		    if expr "$v" : "-fstype=$fstype,."; then
   176 			options="$(echo "$v" | sed 's/^[^,]*,//')"
   177 			break
   178 		    fi
   179 		done
   180 		if [ -n "$MOUNTOPTIONS" ]; then
   181 		    options="$MOUNTOPTIONS${options:+,$options}"
   182 		fi
   183 
   184 		# Mount the filesystem.
   185 		log info "executing command: mount -t$fstype ${options:+-o$options} $DEVNAME $mountpoint"
   186 		mount "-t$fstype" "${options:+-o$options}" "$DEVNAME" "$mountpoint"
   187 
   188 		# Determine vendor and model.
   189 		vendor=
   190 		if [ -r "/sys$DEVPATH/device/vendor" ]; then
   191 		    vendor="`cat \"/sys$DEVPATH/device/vendor\"`"
   192 		elif [ -r "/sys$DEVPATH/../device/vendor" ]; then
   193 		    vendor="`cat \"/sys$DEVPATH/../device/vendor\"`"
   194 		elif [ -r "/sys$DEVPATH/device/../manufacturer" ]; then
   195 		    vendor="`cat \"/sys$DEVPATH/device/../manufacturer\"`"
   196 		elif [ -r "/sys$DEVPATH/../device/../manufacturer" ]; then
   197 		    vendor="`cat \"/sys$DEVPATH/../device/../manufacturer\"`"
   198 		fi
   199 		vendor="$(echo "$vendor" | sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//')"
   200 
   201 		model=
   202 		if [ -r "/sys$DEVPATH/device/model" ]; then
   203 		    model="`cat \"/sys$DEVPATH/device/model\"`"
   204 		elif [ -r "/sys$DEVPATH/../device/model" ]; then
   205 		    model="`cat \"/sys$DEVPATH/../device/model\"`"
   206 		elif [ -r "/sys$DEVPATH/device/../product" ]; then
   207 		    model="`cat \"/sys$DEVPATH/device/../product\"`"
   208 		elif [ -r "/sys$DEVPATH/../device/../product" ]; then
   209 		    model="`cat \"/sys$DEVPATH/../device/../product\"`"
   210 		fi
   211 		model="$(echo "$model" | sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//')"
   212 
   213 		# Run hook scripts; ignore errors.
   214 		export UM_DEVICE="$DEVNAME"
   215 		export UM_MOUNTPOINT="$mountpoint"
   216 		export UM_FILESYSTEM="$fstype"
   217 		export UM_MOUNTOPTIONS="$options"
   218 		export UM_VENDOR="$vendor"
   219 		export UM_MODEL="$model"
   220 		log info "executing command: run-parts /etc/usbmount/mount.d"
   221 		run-parts /etc/usbmount/mount.d || :
   222 	    else
   223 		# No suitable mount point found.
   224 		log warning "no mountpoint found for $DEVNAME"
   225 		exit 1
   226 	    fi
   227 	fi
   228     fi
   229 elif [ "$1" = remove ]; then
   230 
   231     # A block or partition device has been removed.
   232     # Test if it is mounted.
   233     for device in $(/usr/bin/encryptionprovider.py -g)
   234     do
   235 	if [ "$DEVNAME" = "$device" ]
   236 	then
   237 	    log info "umout encrypted device"
   238 	    chattr -i "/tmp/usbmount"
   239 	    umount "/tmp/usbmount/encrypted"
   240 	    rmdir "/tmp/usbmount/encrypted"
   241 	    log info "/usr/bin/encryptionprovider.py -u $DEVNAME"
   242 	    /usr/bin/encryptionprovider.py -u "$DEVNAME"
   243 	    log info "everything done"
   244 	fi
   245     done
   246     while read device mountpoint fstype remainder; do
   247 	if [ "$DEVNAME" = "$device" ]; then
   248 	    # If the mountpoint and filesystem type are maintained by
   249 	    # this script, unmount the filesystem.
   250 	    if in_list "$mountpoint" "$MOUNTPOINTS" &&
   251 		in_list "$fstype" "$FILESYSTEMS"; then
   252 		log info "executing command: umount -l $mountpoint"
   253 		umount -l "$mountpoint"
   254 
   255 		# Run hook scripts; ignore errors.
   256 		export UM_DEVICE="$DEVNAME"
   257 		export UM_MOUNTPOINT="$mountpoint"
   258 		export UM_FILESYSTEM="$fstype"
   259 		log info "executing command: run-parts /etc/usbmount/umount.d"
   260 		run-parts /etc/usbmount/umount.d || :
   261 	    fi
   262 	    break
   263 	fi
   264     done < /proc/mounts
   265 else
   266     log err "unexpected: action '$1'"
   267     exit 1
   268 fi
   269 
   270 log debug "usbmount execution finished"