-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeliremoteunlock
executable file
·189 lines (155 loc) · 4.47 KB
/
geliremoteunlock
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
#!/bin/sh
# $FreeBSD$
#
# PROVIDE: geliremoteunlock
# REQUIRE: netwait
# BEFORE: jail
. /etc/rc.subr
name=geliremoteunlock
# shellcheck disable=2034
rcvar="${name}_enable"
# shellcheck disable=2034
start_cmd="${name}_start"
# shellcheck disable=2034
stop_cmd=":"
load_rc_config "$name"
: "${geliremoteunlock_enable:="NO"}"
__var() {
pool="$1"
entry="$2"
res=""
eval "res=\${""${name}_${pool}_${entry}""}"
[ -z "$res" ] && eval "res=\${""${name}_${entry}""}"
echo "$res"
}
__in_list() {
match="$1"
elems="$2"
for elem in $elems; do
if [ "$match" = "$elem" ]; then
return 0
fi
done
return 1
}
__is_cmd() {
cmd="$1"
debug "checking for command $cmd"
command -v "$cmd" > /dev/null
return $?
}
__delete() {
obj="$1"
debug "checking for $obj to remove"
if [ -f "$obj" ]; then
if __is_cmd "gshred"; then
info "shredding $obj"
gshred -fxu "$obj"
else
info "deleting $obj"
rm -fP "$obj"
fi
fi
}
geliremoteunlock_start() {
conf_pools="$(__var "_" "pools")"
pools=""
if [ -z "$*" ]; then
pools="$conf_pools"
else
for arg_pool in "$@"; do
if ! __in_list "$arg_pool" "$conf_pools"; then
err 1 "$arg_pool is not configured"
fi
done
pools="$*"
fi
### Helper Functions ###
__pull_file() {
host="$1"
ident="$2"
action="$3"
fname="$4"
tname="$5"
info "retrieving $fname from $host"
if ! res=$(ssh \
"$host" -i "$ident" \
-C "$action $fname" \
); then
warn "$action fetch failed"
else
debug "got $fname from $host - store in $tname"
echo "$res" > "$tname"
fi
}
__decrypt() {
fname="$1"
tname="$2"
pass="$3"
if [ -n "$pass" ]; then
info "decrypting $fname"
mv "$tname" "${tname}.aes"
if ! openssl enc \
-aes-256-cbc -a -pbkdf2 -salt -d \
-in "${tname}.aes" \
-out "$tname" \
-pass "pass:$pass" \
; then
warn "unable to decrypt" "$fname"
fi
__delete "${tname}.aes"
fi
}
### Main part ###
for pool in $pools; do
info "unlocking pool $pool"
devices="$(__var "$pool" "devices")"
keyfile_host="$(__var "$pool" "keyfile_host")"
keyfile_ident="$(__var "$pool" "keyfile_ident")"
keyfile_name="$(__var "$pool" "keyfile_name")"
keyfile_password="$(__var "$pool" "keyfile_password")"
passphrase_host="$(__var "$pool" "passphrase_host")"
passphrase_ident="$(__var "$pool" "passphrase_ident")"
passphrase_name="$(__var "$pool" "passphrase_name")"
passphrase_password="$(__var "$pool" "passphrase_password")"
keyfile_temp="/tmp/${name}_${pool}_temp.key"
passphrase_temp="/tmp/${name}_${pool}_temp.pwd"
__pull_file \
"$keyfile_host" "$keyfile_ident" "keyfile" \
"$keyfile_name" "$keyfile_temp"
__decrypt \
"$keyfile_name" "$keyfile_temp" "$keyfile_password"
if [ -n "$passphrase_host" ] && [ -n "$passphrase_ident" ]; then
__pull_file \
"$passphrase_host" "$passphrase_ident" "passphrase" \
"$passphrase_name" "$passphrase_temp"
__decrypt \
"$passphrase_name" "$passphrase_temp" "$passphrase_password"
fi
for device in $devices; do
info "unlocking device $device for pool $pool"
if [ -n "$passphrase_password" ]; then
if ! geli attach \
-k "${keyfile_temp}" \
-j "${passphrase_temp}" \
"${device}" \
; then
warn "unable to attach $device"
fi
else
if ! geli attach \
-k "${keyfile_temp}" \
-p "${device}" \
; then
warn "unable to attach $device"
fi
fi
done
__delete "$keyfile_temp"
__delete "$passphrase_temp"
info "importing pool $pool"
zpool import "$pool"
done
info "$name done"
}
run_rc_command "$@"