-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathencrypted-folder
executable file
·93 lines (79 loc) · 2.86 KB
/
encrypted-folder
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
#!/bin/sh
usage(){
fmt <<EOF
DESCRIPTION
Create, mount, and unmount encfs volumes.
USAGE
encrypted-folder -m,--mount DIRECTORY
Decrypt and mount DIRECTORY into ~/DIRECTORY.decrypted/. If DIRECTORY doesn't exist or isn't an encfs volume, create a new encfs volume.
encrypted-folder -u,--unmount [DIRECTORY]
Unmount all mounted encfs directories matching "~/*.decrypted", or a single specified directory.
EOF
exit
}
die(){ printf "Error: %s\n" "${1}" 1>&2; exit 1; }
require(){ command -v "${1}" > /dev/null 2>&1 || { suggestion=""; if [ ! -z "$2" ]; then suggestion=" $2"; fi; die "$1 is not installed.${suggestion}"; } }
if [ $# -eq 1 ]; then if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then usage; fi fi
# End boilerplate
decryptedRoot="${HOME}"
require "encfs" "Try: apt-get install encfs"
require "pip" "Try: apt-get install python-pip"
# We require the keyring python library
if ! pip freeze 2> /dev/null | grep "keyring" > /dev/null 2>&1; then
die "Python keyring library is not installed. Try: pip install keyring"
fi
if [ $# -eq 0 ]; then
usage
fi
if [ $# -eq 1 ]; then
if [ "$1" = "set-password" ]; then
# This currently doesn't seem to work with 12.04 LTS.
die "Currently broken. Sorry!"
me=$(whoami)
printf "New password:"
stty -echo
read pw
stty echo
python -c "import keyring;print keyring.set_password('""encrypted-folder""', '""${me}""', '""${pw}""')" > /dev/null 2>&1
elif [ "$1" = "--unmount" ] || [ "$1" = "-u" ]; then
find "${decryptedRoot}" -maxdepth 1 -type d -name '*.decrypted' -exec fusermount -u {} \;
find "${decryptedRoot}" -maxdepth 1 -type d -name '*.decrypted' -exec rmdir {} \;
else
die "Invalid options. Use -h for help."
fi
fi
if [ $# -eq 2 ]; then
if [ "$1" = "--mount" ] || [ "$1" = "-m" ]; then
encryptedFolderPath=$(readlink -f "$2")
encryptedFolder=$(basename "${encryptedFolderPath}")
decryptedFolderPath="${decryptedRoot}"/"${encryptedFolder}".decrypted
me=$(whoami)
if [ -d "${decryptedFolderPath}" ] ; then
die "Directory already mounted."
fi
mkdir -p "${encryptedFolderPath}" # in case we're creating a new volume
mkdir -p "${decryptedFolderPath}"
# Do we have a stored password? If so, use it
pw=$(python -c "import keyring;print keyring.get_password('encrypted-folder', '${me}')")
if [ "${pw}" = "None" ]; then
encfs "${encryptedFolderPath}" "${decryptedFolderPath}"
else
printf "%s" "${pw}" | encfs -S "${encryptedFolderPath}" "${decryptedFolderPath}"
fi
if [ "$?" -eq 0 ]; then
printf "Mounted at %s/\n" "${decryptedFolderPath}"
else
rmdir "${decryptedFolderPath}"
fi
elif [ "$1" = "--unmount" ] || [ "$1" = "-u" ]; then
decryptedFolderPath=$(readlink -f "$2")
if [ -d "${decryptedFolderPath}" ] ; then
fusermount -u "${decryptedFolderPath}"
rmdir "${decryptedFolderPath}"
else
die "Not a directory."
fi
else
die "Invalid options. Use -h for help."
fi
fi