home *** CD-ROM | disk | FTP | other *** search
/ chilidog.highland.cc.ks.us / chilidog.highland.cc.ks.us.zip / chilidog.highland.cc.ks.us / backup / bradford.20120521.etc.tar.gz / bradford.20120521.etc.tar / etc / init.d / boot.crypto < prev    next >
Text File  |  2005-08-16  |  7KB  |  298 lines

  1. #!/bin/bash
  2. #
  3. # Copyright (c) 2001-2002 SuSE Linux AG, Nuernberg, Germany.
  4. # All rights reserved.
  5. #
  6. # Author:  Werner Fink <werner@suse.de>, 2001
  7. #
  8. # /etc/init.d/boot.crypto
  9. #
  10. ### BEGIN INIT INFO
  11. # Provides:          boot.crypto
  12. # Required-Start:    boot.localfs boot.klog
  13. # Should-Start: boot.md boot.lvm
  14. # Required-Stop:
  15. # Default-Start:     B
  16. # Default-Stop:
  17. # Description:       Enable crypto file systems before leaving boot phase
  18. ### END INIT INFO
  19.  
  20. . /etc/rc.status
  21.  
  22. trap "echo" SIGINT SIGSEGV
  23. set +e
  24.  
  25. # Redirect to real device (e.g. in case of boot logging)
  26. : ${CRYPTOTAB:=/etc/cryptotab}
  27. : ${TIMEOUT:=120}
  28. if test -z "$REDIRECT" ; then
  29.     if (echo -n > /dev/tty) 2>/dev/null ; then
  30.     REDIRECT=/dev/tty
  31.     else
  32.     REDIRECT=/dev/console
  33.     fi
  34. fi
  35. test -s $CRYPTOTAB || exit 0
  36. type -p losetup &> /dev/null || exit 0
  37.  
  38. splash=""
  39. redirect ()
  40. {
  41.     if test -e /proc/splash ; then
  42.     read splash  < /proc/splash
  43.     echo verbose > /proc/splash
  44.     fi
  45.     otty=$(stty -g)
  46.     stty $otty < $REDIRECT
  47.     stty -nl -ixon ignbrk -brkint < $REDIRECT
  48.     if test -x /etc/init.d/kbd -a -n "$RUNLEVEL" ; then
  49.     /etc/init.d/kbd start < $REDIRECT > $REDIRECT 2>&1
  50.     fi
  51. }
  52.  
  53. restore ()
  54. {
  55.     stty $otty < $REDIRECT;
  56.     [[ "$splash" =~ silent ]] && echo silent > /proc/splash
  57. }
  58.  
  59. ppid=0
  60. prmt=""
  61. setprompt ()
  62. {
  63.     if test -t 1 -a "$TERM" != "raw" -a "$TERM" != "dumb" && stty size <&1 > /dev/null 2>&1
  64.     then
  65.     (
  66.         trap "exit 0" SIGTERM
  67.         trap "echo" SIGINT SIGSEGV
  68.         usleep 10000
  69.         while test $TIMEOUT -gt 0 ; do
  70.         echo -en "\r${prmt}"
  71.         sleep 2
  72.         : $((TIMEOUT-=2))
  73.         done
  74.     ) & ppid=$!
  75.     else
  76.     usleep 10000
  77.     echo -en "\r${prmt}"
  78.     ppid=0
  79.     fi
  80. }
  81.  
  82. unsetprompt ()
  83. {
  84.     local ret=$?
  85.     test $ppid -gt 0 && kill -15 $ppid
  86.     ppid=0
  87.     return $ret
  88. }
  89.  
  90. rc_reset
  91. main_status=0
  92. case "$1" in
  93.     start|b)
  94.     redirect
  95.     echo "Activating crypto devices using $CRYPTOTAB ... "
  96.     while read loopdev physdev access filesys crypto mopt info rest ; do
  97.         case "$loopdev" in
  98.         \#*|"") continue ;;
  99.         esac
  100.         #
  101.         # Each loop is a new try
  102.         #
  103.         rc_status
  104.         if test $? -gt 0 ; then
  105.         main_status=1
  106.         fi
  107.         rc_reset
  108.         #
  109.         # Does the user want to skip current entry?
  110.         #
  111.         doskip=0
  112.         #
  113.         # Does the mount point exit?
  114.         #
  115.         test -d $access
  116.         rc_status
  117.         if test $? -ne 0 ; then
  118.         echo "${extd}${access}: No such directory${norm}"
  119.         continue
  120.         fi
  121.         #
  122.         # Seeking for crypto modules
  123.         #
  124.         case "$crypto" in
  125.         twofish)    modprobe loop_fish2 ;;
  126.         twofishSL92)    modprobe loop_fish2 ;;
  127.         twofish[0-9]*)    modprobe cryptoloop && modprobe twofish ;;
  128.         esac
  129.         rc_status
  130.         test $? -ne 0 && continue
  131.         #
  132.         #
  133.         #
  134.         while true; do
  135.         #
  136.         # Restore virgin state
  137.         #
  138.         losetup -d $loopdev &> /dev/null || true
  139.  
  140.         #
  141.         # Setting up loop device
  142.         #
  143.         if test -n "$info" ; then
  144.             prmt="${extd}Please enter passphrase for \"$info\" ($physdev): ${norm}"
  145.         else
  146.             prmt="${extd}Please enter passphrase for $physdev: ${norm}"
  147.         fi
  148.         setprompt
  149.         losetup -t $TIMEOUT -e $crypto $loopdev $physdev < $REDIRECT > $REDIRECT 2>&1
  150.         unsetprompt
  151.         rc_status
  152.         test $? -ne 0 && continue 2
  153.  
  154.         #
  155.         # Check if we've success
  156.         #
  157.         if   mount -t $filesys -n -o ro $loopdev $access &> /dev/null ; then
  158.             umount -n $access &> /dev/null || true
  159.             break
  160.         else
  161.             umount -n $access &> /dev/null || true
  162.             echo    "${warn}An error occured.  Maybe the wrong passphrase was"
  163.             echo    "entered or the file system on $physdev is corrupted.${norm}"
  164.             while true ; do
  165.             echo    "${extd}Do you want to retry entering the passphrase or${norm}"
  166.             echo -n "${extd}do you want to continue with a file system check?${norm}"
  167.             read -p " ([${extd}yes${norm}]/${extd}no${norm}/${extd}check${norm}/) " prolo < $REDIRECT
  168.             case "$prolo" in
  169.                 [yY][eE][sS]|[yY]|"")
  170.                 continue 2 ;;
  171.                 [nN][oO]|[nN])
  172.                 doskip=1
  173.                 break    2 ;;
  174.                 [Cc][hH][eE][Cc][kK]|[Cc])
  175.                 break    2 ;;
  176.             esac
  177.             done
  178.         fi
  179.         break
  180.         done
  181.         #
  182.         # Does the user have skipped this entry?
  183.         #
  184.         if test $doskip -gt 0 ; then
  185.         losetup -d $loopdev &> /dev/null || true
  186.         continue
  187.         fi
  188.         #
  189.         # Check for valid super blocks
  190.         #
  191.         case "$filesys" in
  192.         ext2)      tune2fs -l $loopdev     &> /dev/null ;;
  193.         reiserfs) debugreiserfs $loopdev &> /dev/null ;;
  194.         *)      true ;;
  195.         esac
  196.         rc_status
  197.         if test $? -gt 0 ; then
  198.         losetup -d $loopdev &> /dev/null || true
  199.         continue
  200.         fi
  201.         #
  202.         # Checking the structure on the loop device
  203.         #
  204.         fsck -a -t $filesys $loopdev
  205.         FSCK_RETURN=$?
  206.         test $FSCK_RETURN -lt 2
  207.         rc_status
  208.         if test $FSCK_RETURN -gt 1; then
  209.         echo "fsck of $loopdev failed.  Please repair manually."
  210.         echo "${warn}Warning: do never try to repair if you have entered the wrong passphrase.${norm}"
  211.         PS1="(repair filesystem) # "
  212.         /sbin/sulogin $REDIRECT < $REDIRECT > $REDIRECT 2>&1
  213.         sync
  214.         fi
  215.         #
  216.         # Mounting loop device to mount point WITHOUT entry in /etc/mtab
  217.         #
  218.         case "$mopt" in
  219.         default|"") mopt=""    ;;
  220.         esac
  221.         mount -t $filesys -n ${mopt:+-o $mopt} $loopdev $access
  222.         rc_status
  223.         if test $? -gt 0 ; then
  224.         losetup -d $loopdev &> /dev/null
  225.         else
  226.         #
  227.         # Generate entry in /etc/mtab (to enable umount to run losetup -d)
  228.         #
  229.         loopopt="loop=${loopdev},encryption=${crypto}"
  230.         case "$mopt" in
  231.             default|"") mopt="${loopopt}"        ;;
  232.             *)        mopt="${loopopt},${mopt}"    ;;
  233.         esac
  234.         mount -t $filesys -f -o $mopt $physdev $access
  235.         rc_status
  236.         fi
  237.     done < $CRYPTOTAB
  238.     test $main_status -gt 0 && rc_failed 1 || true
  239.     rc_status -v1
  240.     restore
  241.     ;;
  242.     stop)
  243.     reverse ()
  244.     {
  245.         local _line
  246.         while read -r _line ; do
  247.         case "$_line" in \#*|"") continue ;; esac
  248.         reverse
  249.         echo "$_line"
  250.         break
  251.         done
  252.     }
  253.     echo "Turning off crypto devices using $CRYPTOTAB ... "
  254.     while read loopdev physdev access filesys crypto mopt rest ; do
  255.         case "$loopdev" in
  256.         \#*|"") continue ;;
  257.         esac
  258.         #
  259.         # Each loop is a new try
  260.         #
  261.         rc_status
  262.         if test $? -gt 0 ; then
  263.         main_status=1
  264.         fi
  265.         rc_reset
  266.         #
  267.         # Umount loop device
  268.         #
  269.         umount $access
  270.         rc_status
  271.         #
  272.         # If /etc/mtab is linked to /proc/mounts
  273.         # we've to run losetup to detach loop device
  274.         #
  275.         if test -L /etc/mtab ; then
  276.         losetup -d $loopdev
  277.         rc_status
  278.         fi
  279.     done < <(reverse < $CRYPTOTAB)
  280.     test $main_status -gt 0 && rc_failed 1 || true
  281.     rc_status -v1
  282.     ;;
  283.     status)
  284.     rc_failed 4
  285.     rc_status -v
  286.     ;;
  287.     restart)
  288.     $0 stop
  289.     $0 start
  290.     rc_status
  291.     ;;
  292.     *)
  293.     echo "Usage: $0 {start|stop|status|restart}"
  294.     exit 1
  295.     ;;
  296. esac
  297. rc_exit
  298.