#!/bin/bashCONFIG="/etc/crypto/crypto.conf"NR="`grep NR_OF_DISKS $CONFIG |sed 's/NR_OF_DISKS=//'`"USBISIN="NO"dm=/sbin/dmsetupcr=/sbin/cryptsetup## Some fancy colorsRED='\e[1;31m'BLUE='\e[1;34m'GR='\e[0;32m'OT='\e[0;35m'NC='\e[0m'#### Check if the module are loaded both dm-mod and dm-crypt##if [[ `lsmod | grep -c dm_mod` = 0 ]] then modprobe dm-mod if [[ "$?" != 0 ]] then echo -e "$RED ERROR:: Cant load module dm-mod $NC" && exit 1 fifiif [[ `lsmod | grep -c dm_crypt` = 0 ]] then modprobe dm-crypt if [[ "$?" != 0 ]] then echo -e "$RED ERROR:: Cant load module dm-crypt $NC" && exit 1 fifi## Now we check if the crypto chiper for the disk(s) are loaded..for chipers in `grep chiper /etc/crypto/crypto.conf | sed 's/chiper=//'` ; do if [[ `lsmod |grep -c $chipers` == 0 ]] ; then modprobe "$chipers" if [[ "$?" != 0 ]] then echo -e "$RED ERROR:: Cant load chiper $chipers $NC" && exit 1 fi fidoneecho -e "\n$OT * Looking for the Usb-stick.. Please Wait $NC\n"for (( i=1 ; i <= "$NR" ; i++ )) do ## All the variables are in that config file :) export `sed -n "/<$i/,/>$i/p" "$CONFIG" |grep -v "<" | grep -v ">"` ## Check the mount points if [[ ! -d "$usbmountpoint" ]] then echo "$usbmountpoint does not exsit.. check your config file or make the mountpoint" && exit 1 fi if [[ ! -d "${hdmountpoint}" ]] then echo "${hdmountpoint} does not exsit.. check your config file or make the mountpoint" && exit 1 fi ## Check if the usb is pluged in.. if [[ "$USBISIN" = "NO" ]] then while (x=0) do let x++ sleep 1 ## We will wait no more than 25 sec.. if not pluged in by then, do exit.. if [[ "$x" == "25" ]] then echo -e "\n$RED * You better find that usbstick..$NC" && exit 1 fi ## when this returns 1 the kernel has read the device.. ## therfor this must be in the while loop so the variable ## usbdev can be updated on each loop.. ## this can |must be changed if you got a static device.. i dont not.. usbdev="`egrep "(sda1|sdb1|sdc1|sdd1)" /proc/partitions |awk '{print $4}'`" ## A little precheck.. check if the usb device is in mtab but not pluged in.. i can happen :) ## trust me just rip it out and its still be shown as mounted.. just try yourself.. look in df if [[ `df |grep -c "$usbmountpoint"` -eq "1" ]] && [[ $(lsusb | grep -c "${usb_vendor}") -eq "0" ]] ## just remove it and lets continue then umount -f ${usbmountpoint} ; break fi ## Lets see if the usb device is pluged in,, read by the kernel and mounted already.. if [ $usbdev > /dev/null ] && [ $(lsusb |grep -c "${usb_vendor}") == "1" ] && [[ "`df |grep -c "$usbmountpoint"`" -eq "1" ]] then USBISIN="YES" ; break fi ## Lets see if the usb device is pluged in,, read by the kernel and not mounted.. if [ $usbdev > /dev/null ] && [ $(lsusb |grep -c "${usb_vendor}") == "1" ] && [[ "`df |grep -c "$usbmountpoint"`" -eq "0" ]] then mount -t "$usbfs" "/dev/$usbdev" "$usbmountpoint" USBISIN="YES" ; break fi done fi ## Alright.. now we are ready to make the mappers.. if this is true if [[ "$USBISIN" == "YES" ]] && [[ -f "$usbmountpoint/$nameof_keyfile" ]] then if [[ ! -e ${mapperdev} ]] && [[ `df | grep -c "${mapperdev}"` == "0" ]] ## Make the mapper then echo -e "$BLUE making mapper $mapperdev $NC" $cr -c $chiper -d $usbmountpoint/$nameof_keyfile create $mappername $cryptohd && ## Check the disk(s) for errors.. fsck -t $fsocd -T -C -y "$mapperdev" if [[ "$?" != "0" ]] || [[ "$?" != "1" ]] then exit 1 ; fi mount -t "${fsocd}" "${mapperdev}" "${hdmountpoint}" ## If no mapper in dev but mounted ; bad stuff can happen elif [[ ! -e ${mapperdev} ]] && [[ `df |grep -c "${mapperdev}"` == "1" ]] then echo -e "$BLUE Umounting, making new mapper and mounting mapper $NC" umount -l -r -f ${mountpoint} || echo --e "$RED crap.. try lsof ${hdmountpoint} and kill all thouse pid's $NC" && exit 1; $cr -c $chiper -d $usbmountpoint/$nameof_keyfile create $mappername $cryptohd && ## Check the disk(s) for errors.. fsck -t $fsocd -T -C -y "$mapperdev" if [[ "$?" != "0" ]] || [[ "$?" != "1" ]] then exit 1 ; fi mount -t "${fsocd}" "${mapperdev}" "${hdmountpoint}" ## If mapper exsits but its not mounted.. remove it and make a new.. elif [[ -e "${mapperdev}" ]] && [[ `df |grep -c "${mapperdev}"` == "0" ]] then echo -e "$BLUE Removing the old mapper $mappername, making new $mappername and mounting $mapperdev $NC" ${dm} remove ${mappername} && $cr -c $chiper -d $usbmountpoint/$nameof_keyfile create $mappername $cryptohd && ## Check the disk(s) for errors.. fsck -t $fsocd -T -C -y "$mapperdev" if [[ "$?" != "0" ]] || [[ "$?" != "1" ]] then exit 1 ; fi mount -t "${fsocd}" "${mapperdev}" "${hdmountpoint}" ## if mounted and all is well just exit,, elif [[ -e "${mapperdev}" ]] && [[ `df |grep -c "${mapperdev}"` == "1" ]] then echo -e "$GR *** DEVICE $mapperdev Is already mounted $NC" fi fidoneexit 0