Makefile for basic installation added
[truecrypt-extra.git] / usr.bin.truecrypt-mount
1 #!/bin/sh 
2
3 ### TrueCrypt Mount wrapper
4 ### This file is a part of `truecrypt-extra`
5 ###
6 ### (c) Andrew Sichevoi, http://thekondor.net
7 ### Distributed in the terms of GNU GPL v3.0+ license
8 ###
9 ### The latest version is available on http://git.thekondor.net
10
11 if [ -f /etc/default/truecrypt ]; then
12   . /etc/default/truecrypt
13 fi
14
15 run_textual_truecrypt()
16 {
17   ${TRUECRYPT_BINARY} --text --non-interactive $@ 2>/dev/null
18 }
19
20 run_regular_truecrypt()
21 {
22   ${TRUECRYPT_BINARY} $@
23 }
24
25 setup_defaults_if_not_available()
26 {
27   DEFAULT_USER_MOUNT_ROOT=/home/%username%/.mount/encrypted
28
29   if [ -z "${USER_MOUNT_ROOT}" ]; then
30     USER_MOUNT_ROOT=${DEFAULT_USER_MOUNT_ROOT}
31   fi
32
33   DEFAULT_TRUECRYPT_BINARY=/usr/bin/truecrypt
34   if [ -z "${TRUECRYPT_BINARY}" ]; then
35     TRUECRYPT_BINARY=${DEFAULT_TRUECRYPT_BINARY}
36   fi
37
38   DEFAULT_EXPLORE_MOUNTED_VOLUME=true
39   if [ -z "${EXPLORE_MOUNTED_VOLUME}" ]; then
40     EXPLORE_MOUNTED_VOLUME=${DEFAULT_EXPLORE_MOUNTED_VOLUME}
41   fi
42
43   DEFAULT_ERROR_REPORTING_CHANNEL='/bin/echo %error%'
44   if [ -z "${ERROR_REPORTING_CHANNEL}" ]; then
45     ERROR_REPORTING_CHANNEL=${DEFAULT_ERROR_REPORTING_CHANNEL}
46   fi
47 }
48
49 check_if_mount_point_already_used()
50 {
51   _MOUNT_POINT_DIR=$1
52   _IS_LISTED_IN_TRUECRYPT=$(run_textual_truecrypt --list | grep "${_MOUNT_POINT_DIR}" -c)
53   if [ 0 -eq $? -a ! 0 -eq ${_IS_LISTED_IN_TRUECRYPT} ]; then
54     raise_error "Error: mount point '${_MOUNT_POINT_DIR}' is already used by truecrypt. It cannot be used right now."
55   fi
56 }
57
58 check_if_truecrypt_available()
59 {
60   if [ ! -x "${TRUECRYPT_BINARY}" ]; then
61     raise_error "Error: Truecrypt (${TRUECRYPT_BINARY}) is not available"
62   fi
63 }
64
65 raise_error()
66 {
67   _ERROR_MESSAGE=$(/bin/echo -n "$@" | sed -e 's/\\/\\\\/g' | sed -e "s/\//::/g" | tr \" \')
68   _ERROR_CMD=$(/bin/echo ${ERROR_REPORTING_CHANNEL} | sed -e "s/%error%/${_ERROR_MESSAGE}/g")
69   eval "${_ERROR_CMD}"
70   exit 1
71 }
72
73 check_passed_arguments()
74 {
75   if [ 1 != $# ]; then
76     raise_error "Usage: ${0} <truecrypt-volume>"
77   fi
78 }
79
80 escape_mount_dir_name()
81 {
82   /bin/echo $@ | /usr/bin/tr "[:cntrl:][:space:][:punct:]" "_"
83 }
84
85 generate_mount_point_dir_name()
86 {
87   escape_mount_dir_name $1
88 }
89
90 generate_mount_point()
91 {
92   _MOUNT_POINT_DIR=$(/bin/echo ${USER_MOUNT_ROOT}/$(generate_mount_point_dir_name $1))
93   _USERNAME_VAR=$(whoami)
94   _MOUNT_POINT_DIR=$(/bin/echo ${_MOUNT_POINT_DIR} | /bin/sed -e s/%username%/${_USERNAME_VAR}/g)
95   
96   /bin/echo ${_MOUNT_POINT_DIR}
97 }
98
99 create_mount_point()
100 {
101   _MOUNT_POINT_DIR=$1
102   if [ ! -d "${_MOUNT_POINT_DIR}" ]; then
103     /bin/mkdir -p ${_MOUNT_POINT_DIR}
104     if [ ! 0 -eq $? ]; then
105       raise_error "Error: failed to create ${_MOUNT_POINT_DIR}"
106     fi
107   fi
108 }
109
110 explore_mount_point()
111 {
112   if [ ${EXPLORE_MOUNTED_VOLUME} ]; then
113     _MOUNT_POINT_DIR=${1}
114     /usr/bin/xdg-open ${_MOUNT_POINT_DIR}
115   fi
116 }
117
118 mount_tc_volume()
119 {
120   _TC_VOLUME=${1}
121   _MOUNT_POINT_DIR=$(generate_mount_point ${_TC_VOLUME})
122
123   check_if_mount_point_already_used ${_MOUNT_POINT_DIR}
124   create_mount_point ${_MOUNT_POINT_DIR}
125
126   run_regular_truecrypt ${_TC_VOLUME} ${_MOUNT_POINT_DIR}
127   if [ ! 0 -eq $? ]; then
128     raise_error "Error: truecrypt execution error, code = ${?}"
129   fi
130
131   explore_mount_point ${_MOUNT_POINT_DIR}
132 }
133
134
135 check_passed_arguments $@
136 setup_defaults_if_not_available
137 mount_tc_volume $1