Skip to content

Commit f04db1a

Browse files
authored
Add support for ssh key (hestiacp#2906)
* Add support for private / public keys * Update backup.sh
1 parent 9ef68d3 commit f04db1a

File tree

2 files changed

+163
-47
lines changed

2 files changed

+163
-47
lines changed

bin/v-add-backup-host

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ EOF
5050

5151
# Defining sftp command function
5252
sftpc() {
53+
if [ "$privatekey" != "yes" ]; then
5354
expect -f "-" <<EOF "$@"
5455
set count 0
5556
spawn /usr/bin/sftp -o StrictHostKeyChecking=no -o Port=$port $user@$host
@@ -98,6 +99,52 @@ sftpc() {
9899
99100
exit \$rc
100101
EOF
102+
else
103+
expect -f "-" <<EOF "$@"
104+
set count 0
105+
spawn /usr/bin/sftp -o StrictHostKeyChecking=no -o Port=$port -i $raw_password $user@$host
106+
expect {
107+
108+
-re "Couldn't|(.*)disconnect|(.*)stalled|(.*)not found" {
109+
set count \$argc
110+
set output "Disconnected."
111+
set rc $E_FTP
112+
exp_continue
113+
}
114+
115+
-re ".*denied.*(publickey|password)." {
116+
set output "Permission denied, wrong publickey or password."
117+
set rc $E_CONNECT
118+
}
119+
120+
"sftp>" {
121+
if {\$count < \$argc} {
122+
set arg [lindex \$argv \$count]
123+
send "\$arg\r"
124+
incr count
125+
} else {
126+
send "exit\r"
127+
set output "Disconnected."
128+
if {[info exists rc] != 1} {
129+
set rc $OK
130+
}
131+
}
132+
exp_continue
133+
}
134+
135+
timeout {
136+
set output "Connection timeout."
137+
set rc $E_CONNECT
138+
}
139+
}
140+
141+
if {[info exists output] == 1} {
142+
puts "\$output"
143+
}
144+
145+
exit \$rc
146+
EOF
147+
fi
101148
}
102149

103150
#----------------------------------------------------------#
@@ -109,7 +156,17 @@ if [ "$type" != 'local' ];then
109156
is_format_valid 'host' 'path' 'port'
110157
is_type_valid 'sftp,ftp,b2' "$type"
111158
is_username_format_valid "$user" "username"
112-
is_password_valid
159+
privatekey="no"
160+
if [ -f "$raw_password" ]; then
161+
if [[ $(cat "$raw_password" | grep "OPENSSH PRIVATE") ]]; then
162+
privatekey="yes"
163+
password="$raw_password"
164+
else
165+
is_password_valid
166+
fi
167+
else
168+
is_password_valid
169+
fi
113170
format_no_quotes "$password" "password"
114171

115172
if [ "$type" = 'sftp' ]; then
@@ -224,7 +281,7 @@ fi
224281
# Adding backup host
225282
if [ $type != 'local' ] && [ $type != 'b2' ]; then
226283
new_timestamp
227-
str="HOST='$host'\nUSERNAME='$user'\nPASSWORD='$password'"
284+
str="HOST='$host'\nUSERNAME='$user'\nPASSWORD='$password'\nPRIVATEKEY='$privatekey'"
228285
str="$str\nBPATH='$path'\nPORT='$port'\nTIME='$time'\nDATE='$date'"
229286
echo -e "$str" > $HESTIA/conf/$type.backup.conf
230287
chmod 660 $HESTIA/conf/$type.backup.conf

func/backup.sh

Lines changed: 104 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -197,60 +197,119 @@ ftp_delete() {
197197
# SFTP Functions
198198
# sftp command function
199199
sftpc() {
200-
expect -f "-" <<EOF "$@"
201-
set timeout 60
202-
set count 0
203-
spawn /usr/bin/sftp -o StrictHostKeyChecking=no \
204-
-o Port=$PORT $USERNAME@$HOST
205-
expect {
206-
-nocase "password:" {
207-
send "$PASSWORD\r"
208-
exp_continue
209-
}
210-
211-
-re "Couldn't|(.*)disconnect|(.*)stalled|(.*)not found" {
212-
set count \$argc
213-
set output "Disconnected."
214-
set rc $E_FTP
215-
exp_continue
216-
}
217-
218-
-re ".*denied.*(publickey|password)." {
219-
set output "Permission denied, wrong publickey or password."
220-
set rc $E_CONNECT
200+
if [ $PRIVATEKEY != "yes" ]; then
201+
expect -f "-" <<EOF "$@"
202+
set timeout 60
203+
set count 0
204+
spawn /usr/bin/sftp -o StrictHostKeyChecking=no \
205+
-o Port=$PORT $USERNAME@$HOST
206+
expect {
207+
-nocase "password:" {
208+
send "$PASSWORD\r"
209+
exp_continue
210+
}
211+
212+
-re "Couldn't|(.*)disconnect|(.*)stalled|(.*)not found" {
213+
set count \$argc
214+
set output "Disconnected."
215+
set rc $E_FTP
216+
exp_continue
217+
}
218+
219+
-re ".*denied.*(publickey|password)." {
220+
set output "Permission denied, wrong publickey or password."
221+
set rc $E_CONNECT
222+
}
223+
224+
-re "\[0-9]*%" {
225+
exp_continue
226+
}
227+
228+
"sftp>" {
229+
if {\$count < \$argc} {
230+
set arg [lindex \$argv \$count]
231+
send "\$arg\r"
232+
incr count
233+
} else {
234+
send "exit\r"
235+
set output "Disconnected."
236+
if {[info exists rc] != 1} {
237+
set rc $OK
238+
}
239+
}
240+
exp_continue
241+
}
242+
243+
timeout {
244+
set output "Connection timeout."
245+
set rc $E_CONNECT
246+
}
221247
}
222-
223-
-re "\[0-9]*%" {
224-
exp_continue
248+
249+
if {[info exists output] == 1} {
250+
puts "\$output"
225251
}
252+
253+
exit \$rc
254+
EOF
255+
else
226256

227-
"sftp>" {
228-
if {\$count < \$argc} {
229-
set arg [lindex \$argv \$count]
230-
send "\$arg\r"
231-
incr count
232-
} else {
233-
send "exit\r"
257+
expect -f "-" <<EOF "$@"
258+
set timeout 60
259+
set count 0
260+
spawn /usr/bin/sftp -o StrictHostKeyChecking=no \
261+
-o Port=$PORT -i $PASSWORD $USERNAME@$HOST
262+
expect {
263+
-nocase "password:" {
264+
send "$PASSWORD\r"
265+
exp_continue
266+
}
267+
268+
-re "Couldn't|(.*)disconnect|(.*)stalled|(.*)not found" {
269+
set count \$argc
234270
set output "Disconnected."
235-
if {[info exists rc] != 1} {
236-
set rc $OK
271+
set rc $E_FTP
272+
exp_continue
273+
}
274+
275+
-re ".*denied.*(publickey|password)." {
276+
set output "Permission denied, wrong publickey or password."
277+
set rc $E_CONNECT
278+
}
279+
280+
-re "\[0-9]*%" {
281+
exp_continue
282+
}
283+
284+
"sftp>" {
285+
if {\$count < \$argc} {
286+
set arg [lindex \$argv \$count]
287+
send "\$arg\r"
288+
incr count
289+
} else {
290+
send "exit\r"
291+
set output "Disconnected."
292+
if {[info exists rc] != 1} {
293+
set rc $OK
294+
}
237295
}
296+
exp_continue
297+
}
298+
299+
timeout {
300+
set output "Connection timeout."
301+
set rc $E_CONNECT
238302
}
239-
exp_continue
240303
}
241-
242-
timeout {
243-
set output "Connection timeout."
244-
set rc $E_CONNECT
304+
305+
if {[info exists output] == 1} {
306+
puts "\$output"
245307
}
246-
}
247-
248-
if {[info exists output] == 1} {
249-
puts "\$output"
250-
}
251-
252-
exit \$rc
308+
309+
exit \$rc
253310
EOF
311+
312+
fi
254313
}
255314

256315
# SFTP backup download function

0 commit comments

Comments
 (0)