4 # cryptcfg by Jakub Schmidtke <sjakub@gmail.com>
6 # Based on netcfg script from Arch's initscripts 0.8-12 package.
9 # Version 0.3 - added LUKS support
16 PATH
="/bin:/usr/bin:/sbin:/usr/sbin:$PATH"
18 PROFILE_DIR
="/etc/crypt-profiles"
23 echo "cryptcfg v$CRYPTCFG_VER"
30 echo "usage: cryptcfg <profile_name>"
31 echo " crypcfg --stop <profile_name>"
32 echo " cryptcfg --menu"
33 echo " cryptcfg --stopall"
35 echo "Crypt profiles are stored in $PROFILE_DIR"
41 [ "$1" = "" ] && return 0
43 [ ! -f $PROFILE_DIR/$1 ] && return 0
45 unset DISK_DEVICE MAP_DEVICE
49 [ "$DISK_DEVICE" = "" ] && return 0
51 [ ! -b $DISK_DEVICE ] && return 0
53 if [ "$CRYPT_METHOD" = "truecrypt" ]; then
55 MAP_DEVICE
=$
( truecrypt
-l |
grep $DISK_DEVICE"$" | cut
-d' ' -f1 )
57 [ ${#MAP_DEVICE} -eq 0 ] && return 0
61 elif [ "$CRYPT_METHOD" = "luks" ]; then
63 [ "$MAP_ALIAS" = "" ] && return 0
65 CRYPT_STATUS
=$
( cryptsetup status
$MAP_ALIAS |
grep /dev
/mapper | cut
-d ' ' -f3 | cut
-d '.' -f 1 | cut
-d ':' -f1 )
67 [ "$CRYPT_STATUS" = "active" ] && return 1
77 if [ "$1" = "" ]; then
78 echo "error: missing profile name"
87 if [ "$ret" = "0" ]; then
88 echo "error: profile $PROF not started"
98 if [ ! -b $DISK_DEVICE ]; then
99 echo "error: $PROF profile contains no valid DISK_DEVICE!"
103 if [ "$CRYPT_METHOD" = "truecrypt" ]; then
105 stat_busy
"Shutting down profile: $PROF"
107 truecrypt
-d $DISK_DEVICE
109 if [ $?
-ne 0 ] ; then
110 echo "error dismounting and/or unmapping $DISK_DEVICE volume"
114 elif [ "$CRYPT_METHOD" = "luks" ]; then
115 if [ ! -b /dev
/mapper
/$MAP_ALIAS ]; then
116 echo "error: /dev/mapper/$MAP_ALIAS is not a valid block device!"
120 stat_busy
"Shutting down profile: $PROF"
122 # space after MAP_ALIAS is important!
123 MOUNT_STATUS
=$
( mount |
grep "/dev/mapper/$MAP_ALIAS " )
125 if [ ! "$MOUNT_STATUS" = "" ]; then
126 stat_busy
"umounting /dev/mapper/$MAP_ALIAS"
128 umount
"/dev/mapper/$MAP_ALIAS"
130 if [ $?
-ne 0 ]; then
131 echo "error umounting /dev/mapper/$MAP_ALIAS"
135 cryptsetup luksClose
$MAP_ALIAS
137 if [ $?
-ne 0 ]; then
138 echo "error closing $MAP_ALIAS crypt device!"
144 echo "error: $PROF profile contains no valid CRYPT_METHOD!"
151 for i
in /dev
/mapper
/truecrypt
*; do
158 for i
in /dev
/mapper
/*; do
159 if [ ! "$i" = "/dev/mapper/control" ]; then
163 cryptsetup luksClose
$i
173 if [ "$DISK_DEVICE" = "" ]; then
174 echo "error: missing DISK_DEVICE"
178 if [ ! -b $DISK_DEVICE ]; then
179 echo "error: $DISK_DEVICE block device does not exist"
183 if [ "$MOUNT_DIR" = "" ]; then
184 echo "error: missing MOUNT_DIR"
188 if [ "$CRYPT_METHOD" = "" ]; then
189 echo "error: missing CRYPT_METHOD"
193 if [ "$CRYPT_METHOD" = "luks" ]; then
194 if [ "$MAP_ALIAS" = "" ]; then
195 echo "error: missing MAP_ALIAS"
202 [ "$NO_ASTERISKS" = "" -o "$NO_ASTERISKS" = "0" ] && D_PARAMS
="--insecure"
204 RESULT
=$
( mktemp
) ||
exit 1
206 if [ "$CRYPT_METHOD" = "truecrypt" ]; then
211 "Enter password for encrypted volume $DISK_DEVICE:\n\n$DESCRIPTION\n " \
212 12 60 | truecrypt
$CRYPT_OPTIONS $DISK_DEVICE > $RESULT 2>&1
213 elif [ "$CRYPT_METHOD" = "luks" ]; then
218 "Enter password for encrypted volume $DISK_DEVICE:\n\n$DESCRIPTION\n " \
219 12 60 | cryptsetup
$CRYPT_OPTIONS luksOpen
$DISK_DEVICE $MAP_ALIAS > $RESULT 2>&1
222 echo "error: Unknown CRYPT_METHOD ($CRYPT_METHOD)"
226 rets
=( ${PIPESTATUS[0]} ${PIPESTATUS[1]} )
234 # If user pressed 'ok'
235 if [ "$ret_dlg" = "0" ]; then
236 if [ ! "$ret_tc" = "0" ]; then
237 dialog
--msgbox "$res" 10 60
238 # Abnormal return status - report it
246 # Otherwise it was cancel, or ESC
252 if [ "$1" = "" ]; then
253 echo "error: missing profile name"
257 if [ ! -f $PROFILE_DIR/$1 ]; then
258 echo "error: $PROFILE_DIR/$1 is missing"
265 if [ "$ret" = "1" ]; then
266 echo "error: $1 profile is already started"
270 stat_busy
"Starting crypt profile: $1"
273 unset DESCRIPTION DISK_DEVICE MAP_DEVICE CRYPT_OPTIONS NO_ASTERISKS MOUNT_DIR CRYPT_METHOD MAP_ALIAS
281 # Password prompt was canceled
282 [ "$ret" = "2" ] && return 2
284 # There was an error in password prompt
285 [ "$ret" = "1" ] && return 1
287 if [ "$CRYPT_METHOD" = "truecrypt" ]; then
289 MAP_DEVICE
=$
( truecrypt
-l |
grep $DISK_DEVICE"$" | cut
-d' ' -f1 )
291 if [ ${#MAP_DEVICE} -eq 0 ]; then
292 echo "$DISK_DEVICE volume not opened"
296 mount
$MAP_DEVICE $MOUNT_DIR
300 [ "$ret" = "0" ] && return 0
304 truecrypt
-d $MAP_DEVICE
306 echo "error mounting $MAP_DEVICE device to $MOUNT_DIR"
309 elif [ "$CRYPT_METHOD" = "luks" ]; then
311 if [ "$MAP_ALIAS" = "" ]; then
312 echo "error: Missing MAP_ALIAS";
316 if [ ! -b "/dev/mapper/$MAP_ALIAS" ]; then
317 echo "/dev/mapper/$MAP_ALIAS is not a block device!"
321 mount
"/dev/mapper/$MAP_ALIAS" $MOUNT_DIR
325 [ "$ret" = "0" ] && return 0
329 cryptsetup luksClose
$MAP_ALIAS
331 echo "error mounting /dev/mapper/$MAP_ALIAS device to $MOUNT_DIR"
336 # Not really needed (as password_prompt should fail already), but for completness
337 echo "Unknown CRYPT_METHOD"
343 if [ "`ls $PROFILE_DIR 2>/dev/null | grep -v ^template$`" = "" -o ! -d $PROFILE_DIR ]; then
344 echo "No profiles found. Add profiles to $PROFILE_DIR"
352 for prof
in $
( ls $PROFILE_DIR ); do
353 # ignore the template
354 [ "$prof" = "template" ] && continue
356 # ignore already started profiles
357 is_started_prof
$prof
360 [ "$ret" = "1" ] && continue
364 # if there's a profile called "main", use that as default
365 [ "$NAME" = "main" ] && DEFAULT
=$NAME
370 if [ "$DESCRIPTION" ]; then
373 profiles
[$i]=$DESCRIPTION
378 # No profiles to be started left
379 if [ ${#profiles} -eq 0 ]; then
380 echo "All available profiles started"
384 # if no default yet, use the first entry
385 [ "$DEFAULT" = "" ] && DEFAULT
=${profiles[0]}
387 # profiles[2] is null - so we have only one profile left.
388 # Don't display list of profiles, just ask fo a password.
389 if [ "${profiles[2]}" = "" ]; then
390 start_profile
$DEFAULT
393 # Since this is the last profile, we can exit the script
394 # if it was successfully opened, or the dialog was canceled.
397 [ "$ret" = "0" ] && exit 0
400 [ "$ret" = "2" ] && exit 0
402 # Otherwise there was an error
407 ANSWER
=`mktemp` ||
exit 1
411 --default-item $DEFAULT \
412 --menu "Select the crypt profile you wish to use" \
414 "${profiles[@]}" >$ANSWER
421 1) exit 0 ;; # cancel
422 255) exit 0 ;; # ESC pressed (or timeout, but we don't use it)
423 0) start_profile
$ans ;; # user selection
425 *) echo "abnormal ret code from dialog: $ret"; exit 1 ;;
433 if [ "`id -u`" != "0" ]; then
434 echo "This script should be run as root."
438 /sbin
/modprobe
-q dm-mod
2>/dev
/null
444 while [ $# -ne 0 ]; do
446 --version) MODE
="ver" ;;
447 --help) MODE
="usage" ;;
448 --menu) MODE
="menu" ;;
449 --stopall) MODE
="stopall" ;;
460 if [ "$MODE" = "profile" -a "$PROFILE" = "" ]; then
464 # Figure out what we're doing...
465 [ "$MODE" = "ver" ] && version
466 [ "$MODE" = "usage" ] && usage
467 [ "$MODE" = "stop" ] && stop_profile
$SPROF
468 [ "$MODE" = "stopall" ] && stop_all
469 [ "$MODE" = "menu" ] && while true
; do menu
; done;
471 if [ "$MODE" = "profile" ]; then
472 start_profile
$PROFILE
475 [ "$ret" = "0" ] && exit 0
476 [ "$ret" = "2" ] && exit 0