truecrypt_scripts/truecrypt_init.sh
author ft
Tue, 09 Sep 2014 09:19:17 +0200
changeset 25 25581dcb9e62
parent 17 980ca72ff1f1
child 27 a8c8d86b8501
permissions -rwxr-xr-x
changed information notifications (no popup anymore)
     1 #!/bin/sh
     2 
     3 BASEDIR="$(dirname $0)"
     4 DEVICE="$1"
     5 MOUNTPOINT="$2"
     6 PASSWORD="$3"
     7 KEYFILE="$4"
     8 
     9 
    10 getRemoteIp ()
    11 {
    12 	ip_address=$(ifconfig eth0 | grep "inet " | awk '{ print $2 }' | cut -d ":" -f 2)
    13 	ip_netmask=$(ifconfig eth0 | grep "inet " | awk '{ print $4 }' | cut -d ":" -f 2)
    14 	remote_ip=$(ipcalc $ip_address/$ip_netmask | grep HostMin | awk '{ print $2}')
    15 
    16 	echo $remote_ip
    17 }
    18 
    19 sendInfoNotification ()
    20 {
    21 	MESSAGE="$1"
    22 	wget -q -T 3 -t 1 -O /dev/null "http://$(getRemoteIp):8090/message?msgtype=information&text=$MESSAGE"
    23 }
    24 
    25 sendErrorNotification ()
    26 {
    27 	MESSAGE="$1"
    28 	wget -q -T 3 -t 1 -O /dev/null "http://$(getRemoteIp):8090/notification?msgtype=critical&text=$MESSAGE"
    29 }
    30 
    31 
    32 if [ -r "$BASEDIR/truecrypt_config.cfg" ]
    33 then
    34 	. "$BASEDIR/truecrypt_config.cfg"
    35 else
    36 	echo "truecrypt_config.cfg not found" >&2
    37 	exit 1
    38 fi
    39 
    40 # make sure to have "/dev/sdb" (not "/dev/sdb1")
    41 #DEVICE="${DEVICE:0:8}" the bash way does not work in dash -.-
    42 DEVICE="$(echo "$DEVICE" | awk '{print substr($1,0,9)}')"
    43 
    44 sendInfoNotification "Encrypt device"
    45 if [ -z "$KEYFILE" ]
    46 then
    47 	message="$($tc_cmd -c --non-interactive --quick --filesystem=none --encryption=AES --hash=RIPEMD-160 -p "$PASSWORD" "$DEVICE")"
    48 	result="$?"
    49 else
    50 	message="$($tc_cmd -c --non-interactive --quick --filesystem=none --encryption=AES --hash=RIPEMD-160 -p "$PASSWORD" -k "$KEYFILE" "$DEVICE")"
    51 	result="$?"
    52 fi
    53 
    54 	
    55 if [ "$result" != "0" ]
    56 then
    57 	sendErrorNotification "Encryption failed"
    58 	exit 1
    59 fi
    60 
    61 sendInfoNotification "Device encrypted"
    62 
    63 
    64 
    65 if [ -z "$KEYFILE" ]
    66 then
    67 	message="$message\n$($tc_cmd --non-interactive --filesystem=none -p "$PASSWORD" "$DEVICE")"
    68 	result="$?"
    69 else
    70 	message="$message\n$($tc_cmd --non-interactive --filesystem=none -p "$PASSWORD" -k "$KEYFILE" "$DEVICE")"
    71 	result="$?"
    72 fi
    73 	
    74 if [ "$result" != "0" ]
    75 then
    76 	exit 1
    77 fi
    78 
    79 
    80 
    81 sendInfoNotification "Create NTFS filesystem on encrypted device"
    82 
    83 TC_DEVICE=$(truecrypt -l | awk '{print $3}')
    84 
    85 message="$message\n$(mkfs.ntfs --quick "$TC_DEVICE")"
    86 result="$?"
    87 
    88 if [ "$result" != "0" ]
    89 then
    90 	sendErrorNotification "Filesystem creation failed"
    91 	exit 1
    92 fi
    93 
    94 sendInfoNotification "Filesystem successfully created"
    95 
    96 mount "$TC_DEVICE" "$MOUNTPOINT" 
    97 
    98 sendInfoNotification "Stick is now initialized"
    99 
   100 echo "$message"
   101 exit 0