-
Notifications
You must be signed in to change notification settings - Fork 5
/
file.bash
executable file
·129 lines (103 loc) · 2.68 KB
/
file.bash
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
#!/usr/bin/env bash
print_usage() {
echo "Usage: $PROGRAM file action pass-name [path]"
echo "Actions:"
echo " store|add|attach: add new file to password store"
echo " retrieve|show|cat: retrieve file from password store and print it to stdout"
echo " edit|vi: edit a file (warning: unencrypted file will be opened with \$EDITOR)"
exit 0
}
cmd_store() {
local path="$1"
local file="$2"
if [[ ${path: -4} != ".b64" ]]; then
path="${path}.b64"
fi
local passfile="$PREFIX/$path.gpg"
cd $OLDPWD # fix for relative paths
local file_abs_path="$OLDPWD/$file"
check_sneaky_paths "$1"
set_git "$passfile"
if [[ -z $path || -z "$file_abs_path" ]]; then
print_usage
elif [[ ! -f "$file_abs_path" ]]; then
die "Error: $file does not exist."
fi
if [[ -f $passfile ]] && [[ "$PASS_FILE_FORCE_OVERWRITE" != "true" ]]; then
read -r -p "A file with this name already exists in the store. Do you want to overwrite it? [y/N] " response
if [[ $response != [yY] ]]; then
exit 0;
fi
fi
mkdir -p "$(dirname "$passfile")"
set_gpg_recipients "$(dirname "$path")"
base64 $file_abs_path | $GPG -e "${GPG_RECIPIENT_ARGS[@]}" -o "$passfile" "${GPG_OPTS[@]}"
git_add_file $passfile "Store arbitary file for $path to store."
}
cmd_retrieve() {
local path="$1"
if [[ ${path: -4} != ".b64" ]]; then
path="${path}.b64"
fi
local passfile="$PREFIX/$path.gpg"
if [[ -z $path ]]; then
print_usage
else
check_sneaky_paths "$path"
$GPG -d "${GPG_OPTS[@]}" "$passfile" | base64 -d || exit $?
fi
}
cmd_edit() {
local path="$1"
if [[ -z $path ]]; then
print_usage
fi
if [[ ${path: -4} != ".b64" ]]; then
path="${path}.b64"
fi
local passfile="$PREFIX/$path.gpg"
if [[ -z $EDITOR ]]; then
echo "\$EDITOR not set, don't know how to open file."
exit 1
else
local tmpfile=$(mktemp)
local newfile=0
chmod 0600 $tmpfile
if [[ -f $passfile ]]; then
cmd_retrieve $path > $tmpfile
if [[ $? -ne 0 ]]; then
rm $tmpfile
exit 1
fi
else
echo "File does not exist, creating new file..."
sleep 3
fi
$EDITOR $tmpfile
if [[ $? -ne 0 ]]; then
rm $tmpfile
exit 1
fi
PASS_FILE_FORCE_OVERWRITE="true" cmd_store $path $tmpfile
if [[ $? -ne 0 ]]; then
echo "Could not save file, please check yourself."
echo "Tempfile: ${tmpfile}"
exit 1
fi
rm $tmpfile
fi
}
case $1 in
store|add|attach)
shift && cmd_store "$@"
;;
retrieve|show|cat)
shift && cmd_retrieve "$@"
;;
edit|vi)
shift && cmd_edit "$@"
;;
*)
print_usage
;;
esac