home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / hal / scripts / hal-system-storage-mount < prev    next >
Encoding:
Text File  |  2006-08-30  |  6.8 KB  |  237 lines

  1. #!/bin/bash
  2.  
  3. # Copyright (C) 2005, Kay Sievers <kay.sievers@vrfy.org>
  4. # Copyright (C) 2006, David Zeuthen <david@fubar.dk>
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License version 2.
  8.  
  9. MOUNT_ROOT="/media"
  10.  
  11. # Check for environment variables
  12. if [ "$HAL_PROP_BLOCK_DEVICE" == "" ] || [ "$HAL_PROP_INFO_UDI" == "" ]; then
  13.     echo "Missing or empty environment variable(s)." >&2
  14.     echo "This script should be started by hald." >&2
  15.     exit 1
  16. fi
  17.  
  18. # check validity of user id
  19. if [ "$HAL_METHOD_INVOKED_BY_UID" = "0" ]; then
  20.     echo "Script was called with HAL_METHOD_INVOKED_BY_UID=0, aborting." >&2
  21.     exit 1
  22. fi
  23. PWD=`getent passwd "$HAL_METHOD_INVOKED_BY_UID"` || {
  24.     echo "User id $HAL_METHOD_INVOKED_BY_UID does not exist." >&2
  25.     exit 1
  26. }
  27. USER="`echo "$PWD" | cut -f 1 -d:`"
  28.  
  29. su -c "pmount-hal \"$HAL_PROP_BLOCK_DEVICE\"" "$USER"
  30. exit $?
  31.  
  32. # check if device is already mounted
  33. if [ "$HAL_PROP_VOLUME_IS_MOUNTED" = "true" ]; then
  34.     echo "org.freedesktop.Hal.Device.Volume.AlreadyMounted" >&2
  35.     echo "Device is already mounted." >&2
  36.     exit 1
  37. fi
  38.  
  39. # check if device should be ignored
  40. if [ "$HAL_PROP_VOLUME_IGNORE" = "true" ]; then
  41.     echo "org.freedesktop.Hal.Device.Volume.PermissionDenied" >&2
  42.     echo "Device has volume.ignore set to TRUE. Refusing to mount." >&2
  43.     exit 1
  44. fi
  45.  
  46.  
  47. check_fstab()
  48. {
  49.     awk -v dev="$1" \
  50.     '
  51.     BEGIN {
  52.         sub(/^\/dev\//,"", dev)
  53.         cmd = "/usr/bin/udevinfo -q symlink -n " dev
  54.         if (( cmd | getline) < 0 ) {
  55.             print "org.freedesktop.Hal.Device.Volume.PermissionDenied" >/dev/stderr
  56.             exit 1
  57.         }
  58.  
  59.         for(i=1; i <= NF; i++) {
  60.             a["/dev/" $i] = 1
  61.         }
  62.         a["/dev/" dev] = 1
  63.     }
  64.     {
  65.         if ($1 in a) { 
  66.             print "org.freedesktop.Hal.Device.Volume.PermissionDenied" >"/dev/stderr"
  67.             print $1 " found in /etc/fstab" >"/dev/stderr"
  68.             exit 1
  69.         }
  70.     }
  71.     ' /etc/fstab
  72. }
  73.  
  74. check_fstab "$HAL_PROP_BLOCK_DEVICE" || exit 1
  75.  
  76. # read parameters
  77. # "MyDisk\n"
  78. # "fuse\n"
  79. # "ro\tsync\n"
  80. #
  81. # only allow ^a-zA-Z0-9_= in the string because otherwise someone may
  82. # pass e.g. umask=0600,suid,dev or umask=`/bin/evil`
  83.  
  84. # Used to get :alnum: to be somewhat useful
  85. LANG=en_US.UTF-8
  86.  
  87. read GIVEN_MOUNTPOINT
  88. GIVEN_MOUNTPOINT=${GIVEN_MOUNTPOINT//[^[:alnum:]_=[:space:]:.,+-]/@}
  89. read GIVEN_MOUNTTYPE
  90. GIVEN_MOUNTTYPE=${GIVEN_MOUNTTYPE//[^a-zA-Z0-9_=]/_}
  91. read GIVEN_MOUNTOPTIONS
  92. GIVEN_MOUNTOPTIONS=${GIVEN_MOUNTOPTIONS//[^a-zA-Z0-9_=-[:space:]]/_}
  93.  
  94. # if no mountpoint is requested, get the mountpoint from the filesystem
  95. # label if it does not contain invalid chars or starts with a "."
  96. if [ "$GIVEN_MOUNTPOINT" == "" ]; then
  97.     case "$HAL_PROP_VOLUME_LABEL" in
  98.     *[!A-Za-z0-9_:.+-]*)
  99.         ;;
  100.     .*)
  101.         ;;
  102.     *)
  103.         GIVEN_MOUNTPOINT="$HAL_PROP_VOLUME_LABEL"
  104.     esac
  105. fi
  106.  
  107. # if no mountpoint is given, use default name
  108. if [ "$GIVEN_MOUNTPOINT" == "" ]; then
  109.     if [ "$HAL_PROP_STORAGE_MEDIA_CHECK_ENABLED" == "false" ]; then
  110.     GIVEN_MOUNTPOINT="$HAL_PROP_STORAGE_DRIVE_TYPE"
  111.     else
  112.     GIVEN_MOUNTPOINT="disk"
  113.     fi
  114. fi
  115.  
  116. # last check, we've replaced invalid characters in requested mountpoint
  117. # with '@' and don't allow a specified mountpoint starting with "."
  118. case "$GIVEN_MOUNTPOINT" in
  119.     .*|*@*)
  120.     echo "org.freedesktop.Hal.Device.Volume.InvalidMountpoint" >&2
  121.     echo "The mountpoint is invalid." >&2
  122.     exit 1
  123.     ;;
  124. esac
  125. MOUNTPOINT="$GIVEN_MOUNTPOINT"
  126.  
  127. # pass only whitelisted types; allow anything if it's on a non-pollable drive
  128. if [ "$GIVEN_MOUNTTYPE" != "" ]; then
  129.     if [ "$HAL_PROP_STORAGE_MEDIA_CHECK_ENABLED" == "false" ]; then
  130.     MOUNTTYPE=$GIVEN_MOUNTTYPE
  131.     else
  132.     case "$GIVEN_MOUNTTYPE" in
  133.         *)
  134.         echo "org.freedesktop.Hal.Device.Volume.InvalidFilesystemType" >&2
  135.         echo "Invalid filesystem type." >&2
  136.         exit 1
  137.     esac
  138.     fi
  139. fi
  140.  
  141. # if no type is given, use default name
  142. if [ "$MOUNTTYPE" == "" ]; then
  143.     MOUNTTYPE=$HAL_PROP_VOLUME_FSTYPE
  144. fi
  145.  
  146. # retrieve white-list from device properties (see fdi/policy/osvendor/20-storage-methods.fdi)
  147. LEGAL_MOUNT_OPTIONS="$HAL_PROP_VOLUME_MOUNT_VALID_OPTIONS "
  148. # pass only whitelisted mount options, bail out on anything not in the whitelist
  149. if [ "$GIVEN_MOUNTOPTIONS" != "" ]; then
  150.     for OPTION in $GIVEN_MOUNTOPTIONS; do
  151.     OPTION_WAS_OK="0"
  152.     for LEGAL_OPTION in $LEGAL_MOUNT_OPTIONS; do
  153.         if [ "$OPTION" == "$LEGAL_OPTION" ]; then
  154.         MOUNTOPTIONS="$MOUNTOPTIONS,$OPTION"
  155.         OPTION_WAS_OK="1"
  156.         elif [ "${LEGAL_OPTION:${#LEGAL_OPTION}-1}" == "=" ]; then
  157.         # support for LEGAL_OPTION="umask=", e.g. support any $OPTION that starts with "umask="
  158.         if [ "${OPTION:0:${#LEGAL_OPTION}}" == "$LEGAL_OPTION" ]; then            
  159.  
  160.             # special handling for uid; only allow uid=$HAL_METHOD_INVOKED_BY_UID expect if
  161.             # $HAL_METHOD_INVOKED_BY_UID is 0
  162.             if [ "$LEGAL_OPTION" == "uid=" ]; then
  163.             if [ "$HAL_METHOD_INVOKED_BY_UID" != "0" ]; then
  164.                 if [ "uid=$HAL_METHOD_INVOKED_BY_UID" != "$OPTION" ]; then
  165.                 echo "org.freedesktop.Hal.Device.Volume.InvalidMountOption" >&2
  166.                 echo "The option '$OPTION' is not allowed for uid=$HAL_METHOD_INVOKED_BY_UID" >&2
  167.                 exit 1
  168.                 fi
  169.             fi
  170.             fi
  171.             MOUNTOPTIONS="$MOUNTOPTIONS,$OPTION"
  172.             OPTION_WAS_OK="1"
  173.         fi
  174.         fi
  175.     done
  176.     if [ "$OPTION_WAS_OK" != "1" ]; then
  177.         echo "org.freedesktop.Hal.Device.Volume.InvalidMountOption" >&2
  178.         echo "The option '$OPTION' is not allowed" >&2
  179.         exit 1
  180.     fi
  181.     done
  182. fi
  183.  
  184. # echo "options = '$MOUNTOPTIONS'"
  185.  
  186. # append number to mountpoint if it already exists
  187. if [ -e "$MOUNT_ROOT/$MOUNTPOINT" ]; then
  188.     NUM=1;
  189.     while [ -e "$MOUNT_ROOT/$MOUNTPOINT-$NUM" ]; do
  190.     NUM=$(($NUM + 1))
  191.     done
  192.     MOUNTPOINT="$MOUNTPOINT-$NUM"
  193. fi
  194.  
  195. # create directory and mark it for cleanup with an extended attribute
  196. if [ ! -e "$MOUNT_ROOT/$MOUNTPOINT" ]; then
  197.     MOUNTPOINT_CREATED=1
  198.     mkdir "$MOUNT_ROOT/$MOUNTPOINT"
  199.     touch "$MOUNT_ROOT/$MOUNTPOINT/.created-by-hal"
  200. fi
  201.  
  202. if [ ! -e "$MOUNT_ROOT/$MOUNTPOINT" ]; then
  203.     echo "org.freedesktop.Hal.Device.Volume.FailedToCreateMountpoint" >&2
  204.     echo "Failed to create moutpoint $MOUNT_ROOT/$MOUNTPOINT." >&2
  205.     exit 1
  206. fi
  207.  
  208. if [ "$MOUNTTYPE" != "" ]; then
  209.     MOUNTTYPE_EXPANDED="-t $MOUNTTYPE"
  210. else
  211.     MOUNTTYPE_EXPANDED=""
  212. fi
  213.  
  214. # mount and return status
  215. RESULT=$(mount -o "noexec,nosuid,nodev$MOUNTOPTIONS" $MOUNTTYPE_EXPANDED "$HAL_PROP_BLOCK_DEVICE" "$MOUNT_ROOT/$MOUNTPOINT" 2>&1)
  216. if [ $? -ne 0 ]; then
  217.     case "$RESULT" in
  218.     *"unknown filesystem"*)
  219.         echo "org.freedesktop.Hal.Device.Volume.UnknownFilesystemType" >&2
  220.         echo "Invalid filesystem type." >&2
  221.         ;;
  222.     *)
  223.         echo "org.freedesktop.Hal.Device.Volume.UnknownFailure" >&2
  224.         echo "Failed to mount device." >&2
  225.     esac
  226.     if [ -n "$MOUNTPOINT_CREATED" ]; then
  227.     rm -f "$MOUNT_ROOT/$MOUNTPOINT/.created-by-hal"
  228.     rmdir "$MOUNT_ROOT/$MOUNTPOINT"
  229.     fi
  230.     exit 1
  231. fi
  232.  
  233. hal-set-property --udi $UDI --key info.hal_mount.created_mount_point --string "$MOUNT_ROOT/$MOUNTPOINT" > /dev/null 2>&1
  234. hal-set-property --udi $UDI --key info.hal_mount.mounted_by_uid --int "$HAL_METHOD_INVOKED_BY_UID" > /dev/null 2>&1
  235.  
  236. exit 0
  237.