scripts

misc scripts and tools
git clone git://git.2f30.org/scripts
Log | Files | Refs

cryptovol (2547B)


      1 #!/bin/sh
      2 # this scripts creates (or securely deletes) a file and uses it as an an encrypted softraid device.
      3 # init would create the file, attach would attach it to a mountpoint, detaching a mountpoint would free
      4 # the vnd and scsi devices and remove would securely wipe the file from the computer.
      5 
      6 init() {
      7 	if [ ${#} -ne 0 ]; then
      8 		usage; exit 1;
      9 	fi
     10 	echo -n "Enter volumefile name:"
     11 	read volfname
     12 	if test -e $volfname; then
     13 		echo "volume file:" $volfname " exists."
     14 		exit 1
     15 	fi
     16 
     17 	echo -n "Enter volumefile size (in Mb):"
     18 	read volsize
     19 	dd if=/dev/urandom of=$volfname bs=1m count=$volsize
     20 	/usr/bin/doas /sbin/vnconfig -c vnd0 $volfname
     21 	/usr/bin/doas fdisk -iy vnd0
     22 	printf "a\n\n\n\nRAID\nw\nq\n\n" | /usr/bin/doas disklabel -E vnd0
     23 	/usr/bin/doas /sbin/bioctl -c C -l vnd0a softraid0
     24 	cryptdev=`dmesg | tail -1 | grep sd | cut -f 1 -d :` # heuristic should get it since we just attached 
     25 	echo -n "the new device created is: " $cryptdev
     26 	echo -n  " shall we go ahead and erase it? [yn]:"
     27 	read yn
     28 	case $yn in
     29 	     [Yy]* ) fdisk -iy $cryptdev; printf "a\n\n\n\n4.2BSD\nw\nq\n\n" | /usr/bin/doas disklabel -E $cryptdev; doas newfs ${cryptdev}a
     30 	     ;;
     31 	     [Nn]* ) /usr/bin/doas /sbin/bioctl -d $cryptdev; doas /sbin/vnconfig -u vnd0 ;exit 0
     32 	     ;;
     33 	     * ) echo -n " yes or no";
     34 	     ;;
     35 	esac
     36 }
     37 
     38 attach() { # attach file dir
     39 	if [ ${#} -ne 2 ]; then
     40 		usage; exit 1;
     41 	fi
     42 	echo "attaching $1 to mountpoint $2"
     43 	/usr/bin/doas /sbin/vnconfig -c vnd0 $1
     44 	/usr/bin/doas /sbin/bioctl -c C -l vnd0a softraid0
     45 	cryptdev=`dmesg | tail -1 | grep sd | cut -f 1 -d :`
     46 	mount /dev/${cryptdev}a $2
     47 }
     48 
     49 detach() { #detach dir 
     50 	if [ ${#} -ne 1 ]; then
     51 		usage; exit 1;
     52 	fi
     53 	echo "unmounting $1"
     54 	/usr/bin/doas umount $1
     55 	cryptdev=`dmesg | tail -4 | grep -G "sd. at" | uniq |  cut -f 1 -d\ ` #heuristic 
     56 	/usr/bin/doas /sbin/bioctl -d $cryptdev
     57 	/usr/bin/doas /sbin/vnconfig -u vnd0
     58 	exit 0
     59 }
     60 
     61 remove() {
     62 	if [ ${#} -ne 1 ]; then
     63 		usage; exit 1;
     64 	fi
     65 	echo -n "remove file $1? [yn]:"
     66 	read yn
     67 	case $yn in
     68 		[Yy]* ) nblocks=`/usr/bin/stat -f "%b" $1`; /bin/dd if=/dev/zero of=$1 bs=$nblocks count=1; /bin/sync; /bin/rm $1; /bin/sync
     69 		;;
     70 		[Nn]* ) exit 0
     71 		;;
     72 		* ) echo -n " yes or no "
     73 		;;
     74 	esac
     75 }
     76 
     77 usage() {
     78 	cat << EOF 
     79 	cryptovol [command] [opts...]
     80 	 commands: 
     81                   init
     82 		  attach file mountpoint
     83                   detach mountpoint
     84                   remove file
     85 EOF
     86 }
     87 
     88 case $1 in
     89 	init ) shift; init $@
     90 	;;
     91 	attach ) shift; attach $@
     92 	;;
     93 	detach ) shift; detach $@
     94 	;;
     95 	remove ) shift; remove $@
     96 	;;
     97 	* ) usage
     98 	;;
     99 esac