File: mount.crypt

package info (click to toggle)
libpam-mount 0.9.22-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,260 kB
  • ctags: 602
  • sloc: sh: 12,388; ansic: 6,399; makefile: 114; perl: 23
file content (162 lines) | stat: -rwxr-xr-x 3,608 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/bin/sh

#   FILE: mount.crypt -- mount a dm-crypt encrypted volume
# AUTHOR: W. Michael Petullo <mike@flyn.org>
#   DATE: 18 April 2004
# 
# Copyright (C) 2004 W. Michael Petullo <mike@flyn.org>
# All rights reserved.
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

LOSETUP=/sbin/losetup
CRYPTSETUP=/sbin/cryptsetup
MOUNT=/bin/mount

OPTIONS=""

USAGE="[-o options] dev dir

  -o options  set the mount options  [ $OPTIONS ]"

_losetup() {
	DEVICE=$1
	LOOPDEV=0
	while [ $LOOPDEV -le 15 ]; do
		if $LOSETUP /dev/loop${LOOPDEV} $DEVICE 2> /dev/null; then
			echo /dev/loop${LOOPDEV}
			return 0
		fi
		LOOPDEV=$(($LOOPDEV + 1))
	done
	echo "${0##*/}: error setting up loop device for $DEVICE" >&2
	exit 1
}

while :;
	do case "$1" in
		-h | "-?" )	
			echo -e usage: ${0##*/} "$USAGE" >&2
			exit 1 ;;
		-o )
			OPTIONS=$2
			shift ;;
		-?* )
			echo "${0##*/}: unrecognised option: $1" >&2
			exit 1 ;;
		* )
			break ;;
	esac
	shift
done

if [ -z "$1" ]; then
	echo "${0##*/}: device to mount not specified" >&2
fi

if [ ! -f "$1" ] && [ ! -b "$1" ]; then
        echo "${0##*/}: $1 is not a block device or file" >&2
	exit 1
fi

if [ ! -d "$2" ]; then
        echo "${0##*/}: $2 is not a directory" >&2
	exit 1
fi

CIPHER=""
KEYSIZE=""
HASH=""
LOOP=false
MOUNTOPTIONS=""
IFS=","
for opt in $OPTIONS; do
	KEY=`echo $opt | awk -F = '{ print $1 }'`
	VAL=`echo $opt | awk -F = '{ print $2 }'`
	case $KEY in
		# FIXME: use cipher instead of encryption to avoid conflicting
		# with mount's built-in (cryptoloop) encryption argument.
		cipher )
			CIPHER="$VAL"
			;;
		keysize )
			KEYSIZE="$VAL"
			;;
		hash )
			HASH="$VAL"
			;;
		loop )
			LOOP=true
			;;
		* )
			if [ -z "$MOUNTOPTIONS" ]; then
				MOUNTOPTIONS="$opt"
			else
				IFS=""
				MOUNTOPTIONS="$MOUNTOPTIONS,$opt"
			fi
			;;
	esac
done

# FIXME: blind replacement of / with _ may be a bad idea.
if [ x"${LOOP}" = xtrue ]; then
	DEVICE=`_losetup $1`
	DMDEVICE=`echo $DEVICE | sed 's/\//_/g'`
else
	DEVICE=$1
	DMDEVICE=`echo $DEVICE | sed 's/\//_/g'`
fi

CIPHEROPT="aes"
if [ -n "$CIPHER" ]; then
	CIPHEROPT="-c $CIPHER"
fi

HASHOPT="ripemd160"
if [ -n "$HASH" ]; then
	HASHOPT="-h $HASH"
fi

KEYSIZEOPT="256"
if [ -n "$KEYSIZE" ]; then
       KEYSIZEOPT="$KEYSIZE"
fi

$CRYPTSETUP -c $CIPHEROPT -h $HASHOPT -s $KEYSIZEOPT create $DMDEVICE $DEVICE
if [ $? != 0 ]; then
	echo "${0##*/}: error creating $DMDEVICE" >&2
	[ x"$LOOP" = xtrue ] && $LOSETUP -d $DEVICE
	exit 1
fi

if [ -z "$MOUNTOPTIONS" ]; then
	# $2 might not exist as mount can try to read it from /etc/fstab
	$MOUNT /dev/mapper/$DMDEVICE $2
	if [ $? != 0 ]; then
		echo "${0##*/}: error mounting $DMDEVICE" >&2
		$CRYPTSETUP remove $DMDEVICE
		[ x"$LOOP" = xtrue ] && $LOSETUP -d $DEVICE
		exit 1
	fi
else
	$MOUNT -o $MOUNTOPTIONS /dev/mapper/$DMDEVICE $2
	if [ $? != 0 ]; then
		echo "${0##*/}: error mounting $DMDEVICE" >&2
		$CRYPTSETUP remove $DMDEVICE
		[ x"$LOOP" = xtrue ] && $LOSETUP -d $DEVICE
		exit 1
	fi
fi