File: encrypt.sh

package info (click to toggle)
clifm 1.26.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,816 kB
  • sloc: ansic: 64,595; sh: 3,133; python: 1,851; makefile: 567
file content (153 lines) | stat: -rwxr-xr-x 3,656 bytes parent folder | download
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
#!/bin/sh

# Files encryption plugin for Clifm
# Encrypt files passed as parameters using gpg(1)

# Authors: KlzXS, L. Abramovich
# License: GPL2+

# Dependencies: gpg, tar, sed, fzf, awk, xargs

if [ -z "$1" ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
	name="${CLIFM_PLUGIN_NAME:-$(basename "$0")}"
	printf "Encrypt one or more files and/or directories using GnuPG\n" >&2
	printf "\n\x1b[1mUSAGE\x1b[0m\n  %s FILE...\n\n" "$name" >&2
	printf "Note: Files are first archived into a single file via \x1b[1mtar\x1b[0m(1) and \n\
then encrypted with \x1b[1mgpg\x1b[0m(1), using either a passphrase or a public key.\n\
You will be given the option to remove original files.\n" >&2
	exit 0
fi

# 1. Check deps

if ! type gpg >/dev/null 2>&1; then
	printf "clifm: gpg: Command not found\n" >&2
	exit 127
fi

if ! type tar >/dev/null 2>&1; then
	printf "clifm: tar: Command not found\n" >&2
	exit 127
fi

if ! type sed >/dev/null 2>&1; then
	printf "clifm: sed: Command not found\n" >&2
	exit 127
fi

if ! type xargs >/dev/null 2>&1; then
	printf "clifm: xargs: Command not found\n" >&2
	exit 127
fi

# Fix backspace when taking input via read
stty erase ^H

# 2. Get destination file

if [ -n "$2" ]; then
	while [ "$out_file" = "" ]; do
		printf "Destiny file ('q' to quit): " >&2
		read -r out_file
	done
else
	out_file="$(echo "$1" | sed 's/\\ /_/g')"
fi

if [ -z "$out_file" ] || [ "$out_file" = "q" ]; then
	exit 0
fi

file="${out_file}.tar"

if [ -e "${file}.gpg" ]; then
	printf "clifm: %s: File exists\n" "${file}.gpg" >&2
	exit 1
fi

files="$(echo "$@" | sed 's/\\ /\t/g;s/ /\n/g;s/\t/ /g;s/\\//g')"

if ! echo "$files" | xargs -I{} tar -rf "$file" {}; then
	rm -rf -- "$file"
	exit 1
fi

while [ "$method" != "p" ] && [ "$method" != "k" ] && [ "$method" != "q" ]; do
	printf "Encrypt with passphrase, key, or quit? [p/k/q] "
	read -r method
done

if [ "$method" = "q" ]; then
	rm -f -- "$file"
	exit 0
fi

# 3. Encrypt

if [ "$method" = "p" ]; then
	# a. Symmetric encryption - Passphrase

	if gpg --symmetric "$file"; then
		rm -f -- "$file"
	fi

else
	# b. Asymmetric encryption - Key
	if ! type fzf >/dev/null 2>&1; then
		printf "clifm: fzf: Command not found\n" >&2
		exit 127
	fi

	if ! type awk >/dev/null 2>&1; then
		printf "clifm: awk: Command not found\n" >&2
		exit 127
	fi

	# Source our plugins helper
	if [ -z "$CLIFM_PLUGINS_HELPER" ] || ! [ -f "$CLIFM_PLUGINS_HELPER" ]; then
		printf "clifm: Unable to find plugins-helper file\n" >&2
		exit 1
	fi
	# shellcheck source=/dev/null
	. "$CLIFM_PLUGINS_HELPER"

	# The recipient code has been taken from KlzXS (https://github.com/jarun/nnn/blob/master/plugins/gpge) and modified to fit our needs
	keyids=$(gpg --list-public-keys --with-colons | grep -E "pub:(.*:){10}.*[eE].*:" | awk -F ":" '{print $5}')

	#shellcheck disable=SC2016
	keyuids=$(printf "%s" "$keyids" | xargs -I{} sh -c 'gpg --list-key --with-colons "{}" | grep "uid" | awk -F ":" '\''{printf "%s %s\n", "{}", $10}'\''')

	l=$(echo "$keyuids" | wc -l)

	# shellcheck disable=SC2154
	recipient=$(printf "%s" "$keyuids" | \
		fzf --reverse --info=inline --height $((l + 2)) --color "$(get_fzf_colors)" \
			--prompt "$fzf_prompt" --header "Select a key ID" | \
		awk '{print $1}')

	if [ -z "$recipient" ] || [ "$recipient" = "" ]; then
		rm -f -- "$file"
		exit 0
	fi

	if ! gpg --encrypt --recipient "$recipient" "$file"; then
		rm -f -- "$file"
		exit 1
	fi

	rm -f -- "$file"
fi

while [ "$answer" != "y" ] && [ "$answer" != "n" ]; do
	printf "Remove original files? [y/n] " >&2
	read -r answer
done

if [ "$answer" = "y" ]; then
	printf "%s" "$files" | xargs -I{} rm -rf -- {}

fi

echo "rf" > "$CLIFM_BUS"

exit 0