diff --git a/README b/README deleted file mode 100644 index 3db2f70..0000000 --- a/README +++ /dev/null @@ -1,3 +0,0 @@ -Cryptobox is a script licensed under the GPLv3, that uses cryptosetup, mkfs and losetup to easily create, mount and unmount encrypted image files based on LUKS. - -Install cryptobox to $PATH and run 'cryptobox' for a list of commands diff --git a/README.md b/README.md new file mode 100644 index 0000000..1edf180 --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +# Cryptobox # + +A script that wraps **cryptsetup**, **mkfs** and **losetup** to make it easy to create, mount and unmount encrypted image files using LUKS. + +## Usage ## + + cryptobox: displays the list of commands + cryptobox c filename.img filesystem size-in-mb: creates an image file __filename.img__ with the filesystem __filesystem__ and size of __size-in-mb__. eg: `cryptobox c myimg.img ext4 128` + cryptobox m filename.img /mount/point: mounts the image file __filename.img__ on the directory __/mount/point__. eg: `cryptobox m myimg.img /mnt/cryptoimg` + cryptobox u /mount/point: unmounts the image file mounted on the directory __/mount/point__. eg: `cryptobox u /mnt/cryptoimg` + +## License ## + +This script is open source and licensed under the [GPLv3](http://www.gnu.org/copyleft/gpl.html). diff --git a/cryptobox b/cryptobox index 1b94886..3233016 100755 --- a/cryptobox +++ b/cryptobox @@ -1,32 +1,33 @@ -#!/bin/sh +#!/usr/bin/env bash -# --------------------------------------------------- # -# cryptobox # -# --------------------------------------------------- # -# cryptobox is a script used with cryptosetup, mkfs # -# and losetup, designed to create, mount and umount # -# encrypted disk images. # -# # -# Developer: Prurigro # -# Contact: prurigro at gmail dot com # -# Version: 1.0 # -# # -# If you find this script useful but have ideas about # -# how to make it better, found bugs, need help using # -# it or anything else; feel free to contect me. # -# # -# Distributed under the GPLv3; copies can be obtained # -# on gnu.org @ http://www.gnu.org/copyleft/gpl.html # -# --------------------------------------------------- # +############################################################## +# # +# CryptoBox # +# # +# A script that wraps cryptsetup, mkfs and losetup to make # +# it easy to create, mount and unmount encrypted image # +# files using LUKS. # +# # +############################################################## -NAME="cryptobox" +NAME=`echo $0 | grep -o -e "[^\/]*$"` -if [ $(lsmod | grep -c loop) = 0 ]; then echo "loading 'loop' module"; modprobe loop || echo "failed to load 'loop' module" && exit 1; fi -if [ $(lsmod | grep -c dm_mod) = 0 ]; then echo "loading 'dm_mod' module"; modprobe dm_mod || echo "failed to load 'dm_mod' module" && exit 1; fi +# Check for root +[[ "$UID" -ne 0 ]] && (echo -e "Error: Please run this script with root permissions\n"; exit 1) + +# Check dependencies +[[ `type -P dd` ]] || (echo "Error: The 'dd' program is missing"; exit 1) +[[ `type -P losetup` ]] || (echo "Error: The 'losetup' program is missing"; exit 1) +[[ `type -P cryptsetup` ]] || (echo "Error: The 'cryptsetup' program is missing"; exit 1) +[[ `type -P mkfs` ]] || (echo "Error: The 'mkfs' program is missing"; exit 1) + +# Load modules if they aren't present +[[ `lsmod | grep loop` ]] || echo "loading 'loop' module"; modprobe loop || (echo "Error: failed to load 'loop' module"; exit 1) +[[ `lsmod | grep dm_mod` ]] || echo "loading 'dm_mod' module"; modprobe dm_mod || (echo "Error: failed to load 'dm_mod' module"; exit 1) function usage { - echo "Usage: '$NAME option arguments'" - echo "Options:" + echo -e "${NAME}: create and mount encrypted images\n" + echo "Usage: ${NAME} option arguments" echo -e "\tc (create) -> $NAME c filename.img filesystem size-in-mb" echo -e "\tm (mount) -> $NAME m filename.img /mount/point" echo -e "\tu (umount) -> $NAME u /mount/point" @@ -52,8 +53,8 @@ case "$1" in echo "error: $2 already exists" exit 1 fi - LOOPDEV=$(losetup -f) - CONTAINER=$(echo "$2" | sed s/"[^\/]*\/"//g | sed s/"\.".*$//g) + LOOPDEV=`losetup -f` + CONTAINER=`echo "$2" | sed s/"[^\/]*\/"//g | sed s/"\.".*$//g` dd bs=1M count="$4" if=/dev/urandom of="$2" losetup $LOOPDEV "$2" cryptsetup -c aes-xts-plain -y -s 512 luksFormat $LOOPDEV @@ -63,19 +64,9 @@ case "$1" in losetup -d $LOOPDEV ;; "m") - if [ -z "$2" -o -z "$3" ]; then - echo "syntax error" - usage - exit 1 - fi - if [ ! -f "$2" ]; then - echo "error: $2 does not exist" - exit 1 - fi - if [ ! -d "$3" ]; then - echo "error: $3 does not exist" - exit 1 - fi + if [ -z "$2" -o -z "$3" ]; then echo -e "Error: syntax\n"; usage; exit 1; fi + [[ ! -f "$2" ]] && (echo "Error: $2 does not exist"; exit 1) + [[ -d "$3" ]] || (echo "Error: $3 does not exist"; exit 1) LOOPDEV=$(losetup -f) CONTAINER=$(echo $LOOPDEV | sed s/"[^\/]*\/"//g | sed s/"\.".*$//g) losetup $LOOPDEV "$2" @@ -88,13 +79,13 @@ case "$1" in usage exit 1 fi - MOUNT=$(mount | grep $(echo "$2" | sed s/"\/"$//)) + MOUNT=`mount | grep $(echo "$2" | sed s/"\/"$//)` if [ -z "$MOUNT" ]; then echo "error: $2 is not mounted" exit 1 fi - LOOPDEV=$(echo $MOUNT | sed s/\ .*//g | sed s/"\/mapper"//) - CONTAINER=$(echo $LOOPDEV | sed s/"[^\/]*\/"//g | sed s/"\.".*$//g) + LOOPDEV=`echo $MOUNT | sed s/\ .*//g | sed s/"\/mapper"//` + CONTAINER=`echo $LOOPDEV | sed s/"[^\/]*\/"//g | sed s/"\.".*$//g` umount "$2" cryptsetup luksClose $CONTAINER losetup -d $LOOPDEV