aboutsummaryrefslogtreecommitdiffstats
path: root/jsaccess/store.sh
diff options
context:
space:
mode:
Diffstat (limited to 'jsaccess/store.sh')
-rwxr-xr-xjsaccess/store.sh65
1 files changed, 36 insertions, 29 deletions
diff --git a/jsaccess/store.sh b/jsaccess/store.sh
index b49c3e7..2b6a2c4 100755
--- a/jsaccess/store.sh
+++ b/jsaccess/store.sh
@@ -45,20 +45,22 @@ usage_exit() {
echo "By default store is ./store/ or ./jsa/store/"
echo "Use \"unset HISTFILE; export JSA_PASS=mypass\" to avoid typing the passphrase"
echo "Use \"unset JSA_PASS\" to forget the passphrase"
- exit 1
+ clean_exit 1
}
-cleanup() {
+clean_exit() {
+ ret=9
+ [ X"$1" != X"" ] && ret=$1
rm -f $tmp
umask $sumask
- exit 0
+ exit $ret
}
confirm_exit() {
if [ X"$JSA_FORCE" = X"" ]; then
echo -n "Are you sure ? [y/N] "
read r
- [ X"$r" != "y" ] && exit 0
+ [ X"$r" != X"y" ] && clean_exit 0
fi
}
@@ -69,8 +71,10 @@ _store_get() {
[[ X"$1" != X"" ]] && store=$1 # priority 1
[[ -z $store ]] && echo "ERROR: store not found !" && \
echo "Not specified as argument and local stores" \
- "./store/ or ./jsa/store/ not found" && exit 2
- echo "Using store $store"
+ "./store/ or ./jsa/store/ not found" && clean_exit 1
+ [[ ! -d $store ]] && echo "ERROR: specified store does not exist !" && \
+ echo "Cannot access $store" && clean_exit 1
+ echo "Using store \"$store\""
}
_pass_read() {
@@ -81,13 +85,14 @@ _pass_read() {
echo -n "> "
read pass
fi
+ [ -z $pass ] && echo "ERROR: empty passphrase" && clean_exit 1
enc_dir_hash=`echo -n $pass |openssl rmd160 |cut -d' ' -f2`
enc_path="$store/$enc_dir_hash"
}
_index_decrypt() {
if [ -f $enc_path/index.txt ]; then
- echo -n $pass |openssl enc -d -a -aes-256-cbc -in $enc_path/index.txt -out $tmp -pass stdin ||exit $2
+ echo -n $pass |openssl enc -d -a -aes-256-cbc -in $enc_path/index.txt -out $tmp -pass stdin ||clean_exit 2
else
echo > $tmp
fi
@@ -95,8 +100,8 @@ _index_decrypt() {
_index_encrypt() {
rm -f $enc_path/index.txt
- echo -n $pass |openssl enc -e -a -aes-256-cbc -in $tmp -out $enc_path/index.txt -pass stdin ||exit $2
- echo "UPDATED $enc_path/index.txt"
+ echo -n $pass |openssl enc -e -a -aes-256-cbc -in $tmp -out $enc_path/index.txt -pass stdin ||clean_exit 2
+ echo "UPDATED file $enc_path/index.txt"
}
__file_get_encname() {
@@ -111,18 +116,18 @@ _file_add() {
if [ ! -d $enc_path ]; then
mkdir -p $enc_path
touch $enc_path/index.html
- echo "CREATED $enc_path (new passphrase)"
+ echo "CREATED directory $enc_path (new passphrase)"
fi
- base64 -w0 $clear_path > $tmp ||exit 2
- echo -n $pass |openssl enc -e -a -aes-256-cbc -in $tmp -out $enc_path/$enc_name -pass stdin ||exit 2
- echo "CREATED $enc_path/$enc_name"
+ base64 -w0 $clear_path > $tmp ||clean_exit 2
+ echo -n $pass |openssl enc -e -a -aes-256-cbc -in $tmp -out $enc_path/$enc_name -pass stdin ||clean_exit 2
+ echo "CREATED file $enc_path/$enc_name"
}
_file_rm() {
- clear_name=$2
+ clear_name=$1
__file_get_encname $clear_name
- rm $enc_path/$enc_name ||exit 1
- echo "DELETED $enc_path/$enc_name"
+ rm $enc_path/$enc_name ||clean_exit 1
+ echo "DELETED file $enc_path/$enc_name"
}
_rset() {
@@ -137,7 +142,8 @@ _rset() {
_rget() {
[ ! -f $store/.rsync_uri ] && echo "ERROR: no rsync_uri set !" && \
- echo "set it with \"store.sh rset <rsync_uri> $store\"" && exit 1
+ echo "set it with \"store.sh rset <rsync_uri> $store\"" && \
+ clean_exit 1
rsync_uri=`cat $store/.rsync_uri`
}
@@ -145,7 +151,7 @@ action_ls() {
_pass_read
_index_decrypt
[ ! -f $enc_path/index.txt ] && \
- echo "Passphrase not used in store !" && exit 1
+ echo "Passphrase not used in store !" && clean_exit 1
echo "$enc_dir_hash/index.txt:"
cat $tmp
}
@@ -157,7 +163,7 @@ action_add() {
_index_decrypt
if [ `egrep -c "^$clear_name$" $tmp` -ne 0 ]; then
echo "File already encrypted with this passphrase"
- exit 1
+ clean_exit 1
fi
_file_add $clear_path $clear_name
_index_decrypt
@@ -166,17 +172,16 @@ action_add() {
}
action_rm() {
- clear_path=$1
- clear_name=`basename $clear_path`
+ clear_name=$1
_pass_read
_index_decrypt
if [ `egrep -c "^$clear_name$" $tmp` -eq 0 ]; then
echo "File does not exist for this passphrase"
- exit 1
+ clean_exit 1
fi
_file_rm $clear_name
_index_decrypt
- sed -i d/$1/ $tmp
+ sed -i /^$clear_name$/d $tmp
_index_encrypt
}
@@ -185,19 +190,21 @@ action_rmall() {
echo "This will delete all file encrypted with this passphrase"
confirm_exit
rm -rf $enc_path
+ echo "DELETED directory $enc_path"
}
action_init() {
store=$1
- mkdir $store ||exit 1
+ mkdir $store ||clean_exit 1
echo "The monster has emptied me !" > $store/index.html
- echo "CREATED store $store"
+ echo "CREATED store \"$store\""
}
action_wipe() {
echo "This will delete all file encrypted with all passphrases"
confirm_exit
rm -rf $store
+ echo "DELETED store \"$store\""
}
action_rset() {
@@ -223,14 +230,14 @@ action_pull() {
if [ X"`which base64`" == X"" \
-o X"`which openssl`" == X"" ]; then
echo "You need to have openssl and base64 available in your path !"
- exit 1
+ clean_exit 1
fi
# Initialize temporary stuff
sumask=$(umask)
umask 077
tmp=`mktemp ./jsaXXXXXXXX` # Used for storing index / new files
-trap cleanup INT TERM EXIT
+trap clean_exit INT TERM
# Run action
case $1 in
@@ -247,7 +254,7 @@ add)
rm)
[ $# -ne 2 -a $# -ne 3 ] && usage_exit
_store_get $3
- action_rm $1
+ action_rm $2
;;
rmall)
[ $# -ne 1 -a $# -ne 2 ] && usage_exit
@@ -301,5 +308,5 @@ version|-V)
action_add $1
esac
-# cleanup() executed in trap
+clean_exit 0