aboutsummaryrefslogtreecommitdiffstats
path: root/jsaccess/store.sh
blob: d797245254a74c93ea609246294e8c5323cd1b7f (plain) (blame)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/bin/sh

# jsaccess - private web file sharing using client side crypto
# store.sh: file store manager for encrypting new files and deploy to server

# Copyright (c) 2013 Laurent Ghigonis <laurent@gouloum.fr>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

VERSION=0.2

usage_exit() {
	echo "usage: store.sh [-v] [action] [action arguments...] [store]"
	echo
	echo "actions:"
	echo "  ls                   [store] # default action if no arguments"
	echo "  init                 <store>"
	echo "  add  <file_to_share> [store] # default action if one argument"
	echo "  rm   <file_in_store> [store]"
	echo "  wipe                 <store>"
	echo "  pull                 [store]"
	echo "  push                 [store]"
	echo "  rset <rsync_URI>     [store]"
	echo "  help|-h"
	echo "  version|-V"
	echo
	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
}

cleanup() {
	rm -f $tmp
	umask $sumask
	exit 0
}

_store_get() {
	store=""
	[[ -d ./jsa/store/ ]] && store="`readlink -f ./jsa/store/`" # priority 3
	[[ -d ./store/ ]] && store="`readlink -f ./store/`" # priority 2
	[[ 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"
}

_pass_read() {
	if [ X"$JSA_PASS" != X"" ]; then
		pass=$JSA_PASS
	else
		echo "Enter encryption passphrase"
		echo -n "> "
		read pass
	fi
	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 $?
	else
		echo > $tmp
	fi
}

_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 $?
	echo "UPDATED $enc_path/index.txt"
}

_file_add() {
	# Path / name generation
	clear_path=$1
	clear_name=`basename $clear_path`
	enc_name=`echo -n ${enc_dir_hash}${clear_name} |openssl rmd160 |cut -d' ' -f2`
	mkdir -p $enc_path
	touch $enc_path/index.html

	# Encrypt
	base64 -w0 $clear_path > $tmp
	echo -n $pass |openssl enc -e -a -aes-256-cbc -in $tmp -out $enc_path/$enc_name -pass stdin ||exit $?
	echo "CREATED $enc_path/$enc_name"
}

_file_rm() {
	pass # XXX
}

_rset() {
	pass # XXX
}

_rget() {
	pass # XXX
}

action_ls() {
	_pass_read
	_index_decrypt
	[ ! -f $enc_path/index.txt ] && \
		echo "Passphrase not used in store !" && exit 1
	echo "$enc_dir_hash/index.txt:"
	cat $tmp
}

action_add() {
	_pass_read
	_file_add $1
	_index_decrypt
	echo $1 >> $tmp
	_index_encrypt
}

action_rm() {
	_pass_read
	_file_rm $1
	_index_decrypt
	sed -i d/$1/ $tmp
	_index_encrypt
}

action_wipe() {
	_rset $1
}

action_pull() {
	_rget $1
	rsync $tmp .
}

action_push() {
	_rget $1
	rsync . $tmp
}

action_rset() {
	_rset $1
}

# Check for dependencies
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
fi

# Initialize temporary stuff
sumask=$(umask)
umask 077
tmp=`mktemp ./jsaXXXXXXXX`
trap cleanup INT TERM EXIT

# Run action
case $1 in
ls)
	[ $# -ne 1 -a $# -ne 2 ] && usage_exit
	_store_get $2
	action_ls
	;;
init)
	[ $# -ne 2 ] && usage_exit
	_store_get $2
	action_init
	;;
add)
	[ $# -ne 2 -a $# -ne 3 ] && usage_exit
	_store_get $3
	action_add $2
	;;
rm)
	[ $# -ne 2 -a $# -ne 3 ] && usage_exit
	_store_get $3
	action_rm $1
	;;
wipe)
	[ $# -ne 2 ] && usage_exit
	_store_get $2
	action_wipe
	;;
pull)
	[ $# -ne 1 -a $# -ne 2 ] && usage_exit
	_store_get $2
	action_pull $1
	;;
push)
	[ $# -ne 1 -a $# -ne 2 ] && usage_exit
	_store_get $2
	action_push $1
	;;
rset)
	[ $# -ne 2 -a $# -ne 3 ] && usage_exit
	_store_get $3
	action_rset $1
	;;
help|-h)
	usage_exit
	;;
version|-V)
	echo "v$VERSION"
	usage_exit
	;;
"")
	[ $# -ne 0 ] && usage_exit
	_store_get
	action_ls
	;;
*)
	[ $# -ne 1 ] && usage_exit
	_store_get $2
	action_add $1
esac

# cleanup() executed in trap